From 525a852b0172a5c44ee152e38d686557d9f33270 Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Mon, 23 Apr 2018 21:32:43 -0400 Subject: [PATCH] bug fixes to init from-pieces command --- cmd/gaia/app/app.go | 18 +++++++++++++++++- server/init.go | 34 +++++++++++++++++++++++++--------- 2 files changed, 42 insertions(+), 10 deletions(-) diff --git a/cmd/gaia/app/app.go b/cmd/gaia/app/app.go index c0a91afc85..37c062595e 100644 --- a/cmd/gaia/app/app.go +++ b/cmd/gaia/app/app.go @@ -186,6 +186,7 @@ func (ga *GenesisAccount) ToAccount() (acc *auth.BaseAccount) { var ( flagAccounts = "accounts" + flagChainID = "chain-id" flagOWK = "overwrite-keys" ) @@ -193,6 +194,7 @@ var ( func GaiaAppInit() server.AppInit { fs := pflag.NewFlagSet("", pflag.ContinueOnError) fs.String(flagAccounts, "foobar-10fermion,10baz-true", "genesis accounts in form: name1-coins-isval:name2-coins-isval:...") + fs.String(flagChainID, "", "genesis file chain-id, if left blank will be randomly created") fs.BoolP(flagOWK, "k", false, "overwrite the for the accounts created, if false and key exists init will fail") return server.AppInit{ Flags: fs, @@ -208,7 +210,11 @@ func GaiaGenAppParams(cdc *wire.Codec, pubKey crypto.PubKey) (chainID string, va printMap := make(map[string]string) var candidates []stake.Candidate poolAssets := int64(0) - chainID = cmn.Fmt("test-chain-%v", cmn.RandStr(6)) + + chainID = viper.GetString(flagChainID) + if len(chainID) == 0 { + chainID = cmn.Fmt("test-chain-%v", cmn.RandStr(6)) + } // get genesis flag account information accountsStr := viper.GetString(flagAccounts) @@ -280,6 +286,7 @@ func GaiaGenAppParams(cdc *wire.Codec, pubKey crypto.PubKey) (chainID string, va // assume everything is bonded from the get-go stakeData.Pool.TotalSupply = poolAssets + stakeData.Pool.BondedPool = poolAssets stakeData.Pool.BondedShares = sdk.NewRat(poolAssets) genesisState := GenesisState{ @@ -304,6 +311,15 @@ func GaiaAppendAppState(cdc *wire.Codec, appState1, appState2 json.RawMessage) ( panic(err) } genState1.Accounts = append(genState1.Accounts, genState2.Accounts...) + genState1.StakeData.Candidates = append(genState1.StakeData.Candidates, genState2.StakeData.Candidates...) + + // pool logic + CombinedSupply := genState1.StakeData.Pool.TotalSupply + genState2.StakeData.Pool.TotalSupply + CombinedBondedPool := genState1.StakeData.Pool.BondedPool + genState2.StakeData.Pool.BondedPool + CombinedBondedShares := genState1.StakeData.Pool.BondedShares.Add(genState2.StakeData.Pool.BondedShares) + genState1.StakeData.Pool.TotalSupply = CombinedSupply + genState1.StakeData.Pool.BondedPool = CombinedBondedPool + genState1.StakeData.Pool.BondedShares = CombinedBondedShares return cdc.MarshalJSON(genState1) } diff --git a/server/init.go b/server/init.go index 7e7d895a10..68eb7f738a 100644 --- a/server/init.go +++ b/server/init.go @@ -77,8 +77,7 @@ func InitCmd(ctx *Context, cdc *wire.Codec, appInit AppInit) *cobra.Command { fmt.Println(string(out)) // write the piece file is path specified - pieceFile := viper.GetString(flagPieceFile) - if len(pieceFile) > 0 { + if viper.GetBool(flagPieceFile) { //create the piece ip, err := externalIP() if err != nil { @@ -95,7 +94,9 @@ func InitCmd(ctx *Context, cdc *wire.Codec, appInit AppInit) *cobra.Command { if err != nil { return err } - return cmn.WriteFile(pieceFile, bz, 0644) + name := fmt.Sprintf("piece%v.json", nodeID) + file := filepath.Join(viper.GetString("home"), name) + return cmn.WriteFile(file, bz, 0644) } return nil @@ -103,7 +104,7 @@ func InitCmd(ctx *Context, cdc *wire.Codec, appInit AppInit) *cobra.Command { } if appInit.AppendAppState != nil { cmd.AddCommand(FromPiecesCmd(ctx, cdc, appInit)) - cmd.Flags().StringP(flagPieceFile, "a", "", "create an append file for others to import") + cmd.Flags().BoolP(flagPieceFile, "a", false, "create an append file (under [--home]/[nodeID]piece.json) for others to import") } cmd.Flags().BoolP(flagOverwrite, "o", false, "overwrite the config file") cmd.Flags().AddFlagSet(appInit.Flags) @@ -144,7 +145,10 @@ func FromPiecesCmd(ctx *Context, cdc *wire.Codec, appInit AppInit) *cobra.Comman os.Remove(genFile) // deterministically walk the directory for genesis-piece files to import - filepath.Walk(pieceDir, appendPiece(ctx, cdc, appInit, nodeKeyFile, genFile)) + err := filepath.Walk(pieceDir, appendPiece(ctx, cdc, appInit, nodeKeyFile, genFile)) + if err != nil { + return err + } return nil }, @@ -157,7 +161,7 @@ func appendPiece(ctx *Context, cdc *wire.Codec, appInit AppInit, nodeKeyFile, ge if err != nil { return err } - if path.Ext(pieceFile) != "json" { + if path.Ext(pieceFile) != ".json" { return nil } @@ -192,7 +196,12 @@ func appendPiece(ctx *Context, cdc *wire.Codec, appInit AppInit, nodeKeyFile, ge appState := genMap["app_state"] // verify chain-ids are the same - if piece.ChainID != string(genMap["chain_id"]) { + var genChainID string + err = cdc.UnmarshalJSON(genMap["chain_id"], &genChainID) + if err != nil { + return err + } + if piece.ChainID != genChainID { return fmt.Errorf("piece chain id's are mismatched, %s != %s", piece.ChainID, genMap["chain_id"]) } @@ -211,7 +220,10 @@ func appendPiece(ctx *Context, cdc *wire.Codec, appInit AppInit, nodeKeyFile, ge } // write the appended genesis file - return WriteGenesisFile(cdc, genFile, piece.ChainID, validators, appState) + err = WriteGenesisFile(cdc, genFile, piece.ChainID, validators, appState) + if err != nil { + return err + } // Add a persistent peer if the config (if it's not me) myIP, err := externalIP() @@ -221,7 +233,11 @@ func appendPiece(ctx *Context, cdc *wire.Codec, appInit AppInit, nodeKeyFile, ge if myIP == piece.IP { return nil } - ctx.Config.P2P.PersistentPeers += fmt.Sprintf(",%s@%s", piece.NodeID, piece.IP) + comma := "," + if len(ctx.Config.P2P.PersistentPeers) == 0 { + comma = "" + } + ctx.Config.P2P.PersistentPeers += fmt.Sprintf("%s%s@%s", comma, piece.NodeID, piece.IP) configFilePath := filepath.Join(viper.GetString("home"), "config", "config.toml") //TODO this is annoying should be easier to get cfg.WriteConfigFile(configFilePath, ctx.Config)