From 804cadcff9d639b24d642d38a63e80d7e5e42e43 Mon Sep 17 00:00:00 2001 From: Benjamin Bollen <ben@monax.io> Date: Mon, 20 Feb 2017 16:59:01 +0100 Subject: [PATCH] genesis: MakeGenesisDocFromAccountsAndValidators --- genesis/genesis.go | 34 ++++++++++++++++++---------- genesis/genesis_test.go | 28 ----------------------- genesis/types.go | 36 ++++++++++++++++++------------ rpc/tendermint/test/shared.go | 6 ++--- test/testdata/testdata/testdata.go | 4 ++-- 5 files changed, 49 insertions(+), 59 deletions(-) delete mode 100644 genesis/genesis_test.go diff --git a/genesis/genesis.go b/genesis/genesis.go index a33386a1..85bc3a83 100644 --- a/genesis/genesis.go +++ b/genesis/genesis.go @@ -24,12 +24,12 @@ import ( wire "github.com/tendermint/go-wire" ) -// MakeGenesisDocFromAccounts takes a chainName and a slice of GenesisAccount, -// and a slice of GenesisValidator to construct a GenesisDoc, or returns an error on +// MakeGenesisDocFromAccounts takes a chainName and a slice of pointers to GenesisAccount, +// and a slice of pointers to GenesisValidator to construct a GenesisDoc, or returns an error on // failure. In particular MakeGenesisDocFromAccount uses the local time as a // timestamp for the GenesisDoc. -func MakeGenesisDocFromAccounts(chainName string, accounts []*GenesisAccount, validators []*GenesisValidator) ( - GenesisDoc, error) { +func MakeGenesisDocFromAccounts(chainName string, accounts []*GenesisAccount, + validators []*GenesisValidator) (GenesisDoc, error) { // TODO: assert valid accounts and validators // TODO: [ben] expose setting global permissions @@ -37,18 +37,28 @@ func MakeGenesisDocFromAccounts(chainName string, accounts []*GenesisAccount, va genesisParameters := &GenesisParams{ GlobalPermissions: &globalPermissions, } - // copy slices of validators and accounts + // copy slice of pointers to accounts into slice of accounts accountsCopy := make([]GenesisAccount, len(accounts)) - copy(accountsCopy, accounts) - + for _, genesisAccount := range accounts { + accountsCopy = append(accountsCopy, genesisAccount.Clone()) + } + // copy slice of pointers to validators into slice of validators + validatorsCopy := make([]GenesisValidator, len(validators)) + for _, genesisValidator := range validators { + genesisValidatorCopy, err := genesisValidator.Clone() + if err != nil { + return GenesisDoc{}, err + } + validatorsCopy = append(validatorsCopy, genesisValidatorCopy) + } genesisDoc := GenesisDoc{ GenesisTime: time.Now(), // TODO: this needs to be corrected for ChainName, and ChainId // is the derived hash from the GenesisDoc serialised bytes ChainID: chainName, Params: genesisParameters, - Accounts: accounts, - Validators: validators, + Accounts: accountsCopy, + Validators: validatorsCopy, } return genesisDoc, nil } @@ -59,9 +69,9 @@ func MakeGenesisDocFromAccounts(chainName string, accounts []*GenesisAccount, va func GetGenesisFileBytes(genesisDoc *GenesisDoc) ([]byte, error) { // TODO: write JSON in canonical order - - buffer, n, err := new(bytes.Buffer), new(int), new(error) - // write JSON with go-wire type-bytes (for public keys); deprecate + var err error + buffer, n := new(bytes.Buffer), new(int) + // write JSON with go-wire type-bytes (for public keys) wire.WriteJSON(genesisDoc, buffer, n, &err) if err != nil { return nil, err diff --git a/genesis/genesis_test.go b/genesis/genesis_test.go deleted file mode 100644 index 269c28f8..00000000 --- a/genesis/genesis_test.go +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright 2015-2017 Monax Industries Limited. -// This file is part of the Monax platform (Monax) - -// Monax is free software: you can use, redistribute it and/or modify -// it only under the terms of the GNU General Public License, version -// 3, as published by the Free Software Foundation. - -// Monax is distributed WITHOUT ANY WARRANTY pursuant to -// the terms of the Gnu General Public Licence, version 3, including -// (but not limited to) Clause 15 thereof. See the text of the -// GNU General Public License, version 3 for full terms. - -// You should have received a copy of the GNU General Public License, -// version 3, with Monax. If not, see <http://www.gnu.org/licenses/>. - -package genesis - -import ( - "bytes" - "io/ioutil" - "os" - "path/filepath" - "testing" -) - -func TestGenesisFileBytes(t *testing.T) { - -} diff --git a/genesis/types.go b/genesis/types.go index 2e25f32a..292c4851 100644 --- a/genesis/types.go +++ b/genesis/types.go @@ -21,6 +21,7 @@ import ( "time" ptypes "github.com/eris-ltd/eris-db/permission/types" + "github.com/tendermint/go-crypto" "github.com/tendermint/go-wire" ) @@ -121,17 +122,24 @@ func (genesisAccount *GenesisAccount) Clone() GenesisAccount { // GenesisValidator methods // Clone clones the genesis validator -func (genesisValidator *GenesisValidator) Clone() GenesisValidator { - // clone the public key - - // clone the account permissions - accountPermissionsClone := genesisAccount.Permissions.Clone() - return GenesisAccount{ - Address: addressClone, - Amount: genesisAccount.amount, - Name: genesisAccount.Name, - Permissions: &accountPermissionsClone, +func (genesisValidator *GenesisValidator) Clone() (GenesisValidator, error) { + // clone the public key by writing and reading over go-wire serialisation + // TODO! write unit test to see whether this is correct + publicKeyClone, err := crypto.PubKeyFromBytes(genesisValidator.PubKey.Bytes()) + if err != nil { + return GenesisValidator{}, err + } + // clone the addresses to unbond to + unbondToClone := make([]BasicAccount, len(genesisValidator.UnbondTo)) + for _, basicAccount := range genesisValidator.UnbondTo { + unbondToClone = append(unbondToClone, basicAccount.Clone()) } + return GenesisValidator{ + PubKey: publicKeyClone, + Amount: genesisValidator.Amount, + Name: genesisValidator.Name, + UnbondTo: unbondToClone, + }, nil } //------------------------------------------------------------ @@ -142,8 +150,8 @@ func (basicAccount *BasicAccount) Clone() BasicAccount { // clone the address addressClone := make([]byte, len(basicAccount.Address)) copy(addressClone, basicAccount.Address) - return GenesisAccount{ - Address: addressClone, - Amount: basicAccount.Amount, + return BasicAccount{ + Address: addressClone, + Amount: basicAccount.Amount, } -} \ No newline at end of file +} diff --git a/rpc/tendermint/test/shared.go b/rpc/tendermint/test/shared.go index a3d0310c..0d75ea27 100644 --- a/rpc/tendermint/test/shared.go +++ b/rpc/tendermint/test/shared.go @@ -19,7 +19,7 @@ import ( "github.com/eris-ltd/eris-db/txs" "github.com/eris-ltd/eris-db/word256" - state_types "github.com/eris-ltd/eris-db/manager/eris-mint/state/types" + genesis "github.com/eris-ltd/eris-db/genesis" "github.com/spf13/viper" "github.com/tendermint/go-crypto" rpcclient "github.com/tendermint/go-rpc/client" @@ -33,7 +33,7 @@ var ( mempoolCount = 0 chainID string websocketAddr string - genesisDoc *state_types.GenesisDoc + genesisDoc *genesis.GenesisDoc websocketEndpoint string user = makeUsers(5) // make keys jsonRpcClient rpcclient.Client @@ -48,7 +48,7 @@ func initGlobalVariables(ffs *fixtures.FileFixtures) error { rootWorkDir = ffs.AddDir("rootWorkDir") rootDataDir := ffs.AddDir("rootDataDir") genesisFile := ffs.AddFile("rootWorkDir/genesis.json", defaultGenesis) - genesisDoc = state_types.GenesisDocFromJSON([]byte(defaultGenesis)) + genesisDoc = genesis.GenesisDocFromJSON([]byte(defaultGenesis)) if ffs.Error != nil { return ffs.Error diff --git a/test/testdata/testdata/testdata.go b/test/testdata/testdata/testdata.go index 5fd2be00..11120bb9 100644 --- a/test/testdata/testdata/testdata.go +++ b/test/testdata/testdata/testdata.go @@ -5,7 +5,7 @@ import ( consensus_types "github.com/eris-ltd/eris-db/consensus/types" core_types "github.com/eris-ltd/eris-db/core/types" event "github.com/eris-ltd/eris-db/event" - stypes "github.com/eris-ltd/eris-db/manager/eris-mint/state/types" + genesis "github.com/eris-ltd/eris-db/genesis" rpc_v0 "github.com/eris-ltd/eris-db/rpc/v0" "github.com/eris-ltd/eris-db/rpc/v0/shared" transaction "github.com/eris-ltd/eris-db/txs" @@ -540,7 +540,7 @@ var serverDuration uint = 100 type ( ChainData struct { PrivValidator *mintTypes.PrivValidator `json:"priv_validator"` - Genesis *stypes.GenesisDoc `json:"genesis"` + Genesis *genesis.GenesisDoc `json:"genesis"` } GetAccountData struct { -- GitLab