From cd24244308a0e2afc9511c3f2a767e21c1d05140 Mon Sep 17 00:00:00 2001 From: Adrian Brink Date: Sun, 13 May 2018 18:19:42 -0400 Subject: [PATCH] Sort all genesis transaction by node id This ensures that users can rename the genesis transactions and they will still be in the same order. --- cmd/gaia/app/genesis.go | 5 +++-- server/init.go | 19 +++++++++++++++---- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/cmd/gaia/app/genesis.go b/cmd/gaia/app/genesis.go index 9d88953cf..7cb7564dd 100644 --- a/cmd/gaia/app/genesis.go +++ b/cmd/gaia/app/genesis.go @@ -66,8 +66,9 @@ func GaiaAppInit() server.AppInit { fsAppGenTx := pflag.NewFlagSet("", pflag.ContinueOnError) fsAppGenTx.String(flagName, "", "validator moniker, if left blank, do not add validator") - fsAppGenTx.String(flagClientHome, DefaultCLIHome, "home directory for the client, used for key generation") - fsAppGenTx.Bool(flagOWK, false, "overwrite the for the accounts created") + fsAppGenTx.String(flagClientHome, DefaultCLIHome, + "home directory for the client, used for key generation") + fsAppGenTx.Bool(flagOWK, false, "overwrite the accounts created") return server.AppInit{ FlagsAppGenState: fsAppGenState, diff --git a/server/init.go b/server/init.go index 2d8be85f8..ffb84f27c 100644 --- a/server/init.go +++ b/server/init.go @@ -7,6 +7,7 @@ import ( "os" "path" "path/filepath" + "sort" "github.com/pkg/errors" "github.com/spf13/cobra" @@ -210,12 +211,14 @@ func InitCmd(ctx *Context, cdc *wire.Codec, appInit AppInit) *cobra.Command { func processGenTxs(genTxsDir string, cdc *wire.Codec, appInit AppInit) ( validators []tmtypes.GenesisValidator, appGenTxs []json.RawMessage, persistentPeers string, err error) { - // XXX sort the files by contents just incase people renamed their files var fos []os.FileInfo fos, err = ioutil.ReadDir(genTxsDir) if err != nil { return } + + genTxs := make(map[string]GenesisTx) + var nodeIDs []string for _, fo := range fos { filename := path.Join(genTxsDir, fo.Name()) if !fo.IsDir() && (path.Ext(filename) != ".json") { @@ -234,16 +237,24 @@ func processGenTxs(genTxsDir string, cdc *wire.Codec, appInit AppInit) ( return } + genTxs[genTx.NodeID] = genTx + nodeIDs = append(nodeIDs, genTx.NodeID) + } + + sort.Strings(nodeIDs) + + for _, nodeID := range nodeIDs { // combine some stuff - validators = append(validators, genTx.Validator) - appGenTxs = append(appGenTxs, genTx.AppGenTx) + validators = append(validators, genTxs[nodeID].Validator) + appGenTxs = append(appGenTxs, genTxs[nodeID].AppGenTx) // Add a persistent peer comma := "," if len(persistentPeers) == 0 { comma = "" } - persistentPeers += fmt.Sprintf("%s%s@%s:46656", comma, genTx.NodeID, genTx.IP) + persistentPeers += fmt.Sprintf("%s%s@%s:46656", comma, genTxs[nodeID].NodeID, + genTxs[nodeID].IP) } return