From 6748aa7bc6a4b1f6162e30af6f50d8f564e9108a Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Fri, 20 Apr 2018 19:55:22 -0400 Subject: [PATCH] first stab init refactor --- cmd/gaia/app/app.go | 31 +++-- cmd/gaia/cmd/gaiad/main.go | 2 +- mock/app.go | 24 ++-- server/init.go | 251 ++++++++++++++----------------------- server/test_helpers.go | 2 +- server/util.go | 2 +- 6 files changed, 140 insertions(+), 172 deletions(-) diff --git a/cmd/gaia/app/app.go b/cmd/gaia/app/app.go index fd981a3b5..6819b67b2 100644 --- a/cmd/gaia/app/app.go +++ b/cmd/gaia/app/app.go @@ -2,13 +2,17 @@ package app import ( "encoding/json" + "fmt" abci "github.com/tendermint/abci/types" + crypto "github.com/tendermint/go-crypto" + tmtypes "github.com/tendermint/tendermint/types" cmn "github.com/tendermint/tmlibs/common" dbm "github.com/tendermint/tmlibs/db" "github.com/tendermint/tmlibs/log" bam "github.com/cosmos/cosmos-sdk/baseapp" + "github.com/cosmos/cosmos-sdk/server" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/wire" "github.com/cosmos/cosmos-sdk/x/auth" @@ -177,9 +181,24 @@ func (ga *GenesisAccount) ToAccount() (acc *auth.BaseAccount) { } } -// DefaultGenAppState expects two args: an account address +// GaiaGenAppState expects two args: an account address // and a coin denomination, and gives lots of coins to that address. -func DefaultGenAppState(args []string, addr sdk.Address, coinDenom string) (json.RawMessage, error) { +func GaiaGenAppState(pubKey crypto.PubKey) (chainID string, validators []tmtypes.GenesisValidator, appState json.RawMessage, err error) { + + var addr sdk.Address + var secret string + addr, secret, err = server.GenerateCoinKey() + if err != nil { + return + } + fmt.Printf("secret recovery key:\n%s\n", secret) + + chainID = cmn.Fmt("test-chain-%v", cmn.RandStr(6)) + + validators = []tmtypes.GenesisValidator{{ + PubKey: pubKey, + Power: 10, + }} accAuth := auth.NewBaseAccountWithAddress(addr) accAuth.Coins = sdk.Coins{{"fermion", 100000}} @@ -191,10 +210,6 @@ func DefaultGenAppState(args []string, addr sdk.Address, coinDenom string) (json StakeData: stake.GetDefaultGenesisState(), } - stateBytes, err := json.MarshalIndent(genesisState, "", "\t") - if err != nil { - return nil, err - } - - return stateBytes, nil + appState, err = json.MarshalIndent(genesisState, "", "\t") + return } diff --git a/cmd/gaia/cmd/gaiad/main.go b/cmd/gaia/cmd/gaiad/main.go index 199a06152..63485433f 100644 --- a/cmd/gaia/cmd/gaiad/main.go +++ b/cmd/gaia/cmd/gaiad/main.go @@ -36,7 +36,7 @@ func generateApp(rootDir string, logger log.Logger) (abci.Application, error) { } func main() { - server.AddCommands(rootCmd, app.DefaultGenAppState, generateApp, context) + server.AddCommands(rootCmd, app.GaiaGenAppState, generateApp, context) // prepare and add flags rootDir := os.ExpandEnv("$HOME/.gaiad") diff --git a/mock/app.go b/mock/app.go index 631cc3c31..462c5564f 100644 --- a/mock/app.go +++ b/mock/app.go @@ -6,6 +6,9 @@ import ( "path/filepath" abci "github.com/tendermint/abci/types" + crypto "github.com/tendermint/go-crypto" + tmtypes "github.com/tendermint/tendermint/types" + cmn "github.com/tendermint/tmlibs/common" dbm "github.com/tendermint/tmlibs/db" "github.com/tendermint/tmlibs/log" @@ -103,11 +106,18 @@ func InitChainer(key sdk.StoreKey) func(sdk.Context, abci.RequestInitChain) abci } } -// GenInitOptions can be passed into InitCmd, -// returns a static string of a few key-values that can be parsed -// by InitChainer -func GenInitOptions(args []string, addr sdk.Address, coinDenom string) (json.RawMessage, error) { - opts := []byte(`{ +// GenAppState can be passed into InitCmd, returns a static string of a few +// key-values that can be parsed by InitChainer +func GenAppState(pubKey crypto.PubKey) (chainID string, validators []tmtypes.GenesisValidator, appState json.RawMessage, err error) { + + chainID = cmn.Fmt("test-chain-%v", cmn.RandStr(6)) + + validators = []tmtypes.GenesisValidator{{ + PubKey: pubKey, + Power: 10, + }} + + appState = json.RawMessage(fmt.Sprintf(`{ "values": [ { "key": "hello", @@ -118,6 +128,6 @@ func GenInitOptions(args []string, addr sdk.Address, coinDenom string) (json.Raw "value": "bar" } ] -}`) - return opts, nil +}`)) + return } diff --git a/server/init.go b/server/init.go index c432149be..7669e5bc5 100644 --- a/server/init.go +++ b/server/init.go @@ -8,8 +8,8 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/wire" "github.com/spf13/cobra" - "github.com/spf13/pflag" + crypto "github.com/tendermint/go-crypto" "github.com/tendermint/go-crypto/keys" "github.com/tendermint/go-crypto/keys/words" cfg "github.com/tendermint/tendermint/config" @@ -20,183 +20,91 @@ import ( dbm "github.com/tendermint/tmlibs/db" ) -type initCmd struct { - genAppState GenAppState - context *Context -} - -// InitCmd will initialize all files for tendermint, along with proper -// app_state. The application can pass in a function to generate proper state. -// And may want to use GenerateCoinKey to create default account(s). -func InitCmd(gen GenAppState, fs pflag.FlagSet, ctx *Context) *cobra.Command { - cmd := initCmd{ - genAppState: gen, - context: ctx, - } +// get cmd to initialize all files for tendermint and application +func InitCmd(gen GenAppParams, ctx *Context) *cobra.Command { cobraCmd := cobra.Command{ Use: "init", Short: "Initialize genesis files", - RunE: cmd.run, + RunE: func(cmd *cobra.Command, args []string) error { + + config := ctx.Config + pubkey := ReadOrCreatePrivValidator(config) + + chainID, validators, appState, err := gen(pubkey) + if err != nil { + return err + } + + err = CreateGenesisFile(config, chainID, validators, appState) + if err != nil { + return err + } + + nodeKey, err := p2p.LoadOrGenNodeKey(config.NodeKeyFile()) + if err != nil { + return err + } + + // print out some key information + toPrint := struct { + ChainID string `json:"chain_id"` + NodeID string `json:"node_id"` + }{ + chainID, + string(nodeKey.ID()), + } + out, err := wire.MarshalJSONIndent(cdc, toPrint) + if err != nil { + return err + } + fmt.Println(string(out)) + return nil + }, } - cobraCmd.Flags().AddFlagSet(fs) return &cobraCmd } -// defaultPrint contains the info necessary -// to setup a testnet including this account and validator. -type defaultPrint struct { - Secret string `json:"secret"` - ChainID string `json:"chain_id"` - Account string `json:"account"` - Validator tmtypes.GenesisValidator `json:"validator"` - NodeID string `json:"node_id"` -} - -func (c initCmd) run(cmd *cobra.Command, args []string) error { - // Store testnet information as we go - var testnetInfo defaultPrint - - // Run the basic tendermint initialization, - // set up a default genesis with no app_options - config := c.context.Config - err := c.initTendermintFiles(config, &testnetInfo) - if err != nil { - return err - } - - // no app_options, leave like tendermint - if c.genAppState == nil { - return nil - } - - // generate secret and address - //addr, secret, err := GenerateCoinKey() - //if err != nil { - //return err - //} - - // Now, we want to add the custom app_state - appState, err := c.genAppState() - if err != nil { - return err - } - - testnetInfo.Secret = secret - testnetInfo.Account = addr.String() - - // And add them to the genesis file - genFile := config.GenesisFile() - if err := addGenesisState(genFile, appState); err != nil { - return err - } - - nodeKey, err := p2p.LoadOrGenNodeKey(config.NodeKeyFile()) - if err != nil { - return err - } - testnetInfo.NodeID = nodeKey.ID() - - // print the output - out, err := wire.MarshalJSONIndent(cdc, testnetInfo) - if err != nil { - return err - } - fmt.Println(string(out)) - return nil -} - -// This was copied from tendermint/cmd/tendermint/commands/init.go -// so we could pass in the config and the logger. -func (c initCmd) initTendermintFiles(config *cfg.Config, info *defaultPrint) error { +// read of create the private key file for this config +func ReadOrCreatePrivValidator(tmConfig *cfg.Config) crypto.PubKey { // private validator - privValFile := config.PrivValidatorFile() + privValFile := tmConfig.PrivValidatorFile() var privValidator *pvm.FilePV if cmn.FileExists(privValFile) { privValidator = pvm.LoadFilePV(privValFile) - c.context.Logger.Info("Found private validator", "path", privValFile) } else { privValidator = pvm.GenFilePV(privValFile) privValidator.Save() - c.context.Logger.Info("Generated private validator", "path", privValFile) } + return privValidator.GetPubKey() +} - // genesis file - genFile := config.GenesisFile() +// create the genesis file +func CreateGenesisFile(tmConfig *cfg.Config, chainID string, validators []tmtypes.GenesisValidator, appState json.RawMessage) error { + genFile := tmConfig.GenesisFile() if cmn.FileExists(genFile) { - c.context.Logger.Info("Found genesis file", "path", genFile) - } else { - genDoc := tmtypes.GenesisDoc{ - ChainID: cmn.Fmt("test-chain-%v", cmn.RandStr(6)), - } - genDoc.Validators = []tmtypes.GenesisValidator{{ - PubKey: privValidator.GetPubKey(), - Power: 10, - }} - - if err := genDoc.SaveAs(genFile); err != nil { - return err - } - c.context.Logger.Info("Generated genesis file", "path", genFile) + return fmt.Errorf("genesis config file already exists: %v", genFile) } - - // reload the config file and find our validator info - loadedDoc, err := tmtypes.GenesisDocFromFile(genFile) - if err != nil { + genDoc := tmtypes.GenesisDoc{ + ChainID: chainID, + Validators: validators, + } + if err := genDoc.ValidateAndComplete(); err != nil { return err } - for _, validator := range loadedDoc.Validators { - if validator.PubKey == privValidator.GetPubKey() { - info.Validator = validator - } + if err := genDoc.SaveAs(genFile); err != nil { + return err } - info.ChainID = loadedDoc.ChainID - - return nil + return addAppStateToGenesis(genFile, appState) } -//------------------------------------------------------------------- - -// GenAppState takes the command line args, as well as an address and coin -// denomination. It returns a default app_state to be included in in the -// genesis file. This is application-specific -type GenAppState func() (json.RawMessage, error) - -// Create one account with a whole bunch of mycoin in it -func DefaultGenAppState(args []string) (json.RawMessage, error) { - - addr, secret, err := GenerateCoinKey() +// Add one line to the genesis file +func addAppStateToGenesis(genesisConfigPath string, appState json.RawMessage) error { + bz, err := ioutil.ReadFile(genesisConfigPath) if err != nil { return err } - genesisState := fmt.Sprintf(`{ - "accounts": [{ - "address": "%s", - "coins": [ - { - "denom": "mycoin", - "amount": 9007199254740992 - } - ] - }] - }`, addr.String()) - return json.RawMessage(genesisState), nil -} - -//------------------------------------------------------------------- - -// GenesisDoc involves some tendermint-specific structures we don't -// want to parse, so we just grab it into a raw object format, -// so we can add one line. -type GenesisDoc map[string]json.RawMessage - -func addGenesisState(filename string, appState json.RawMessage) error { - bz, err := ioutil.ReadFile(filename) - if err != nil { - return err - } - - var doc GenesisDoc + var doc map[string]json.RawMessage err = cdc.UnmarshalJSON(bz, &doc) if err != nil { return err @@ -207,15 +115,51 @@ func addGenesisState(filename string, appState json.RawMessage) error { if err != nil { return err } - - return ioutil.WriteFile(filename, out, 0600) + return ioutil.WriteFile(genesisConfigPath, out, 0600) } //_____________________________________________________________________ +// GenAppParams creates the core parameters initialization. It takes in a +// pubkey meant to represent the pubkey of the validator of this machine. +type GenAppParams func(crypto.PubKey) (chainID string, validators []tmtypes.GenesisValidator, appState json.RawMessage, err error) + +// Create one account with a whole bunch of mycoin in it +func SimpleGenAppState(pubKey crypto.PubKey) (chainID string, validators []tmtypes.GenesisValidator, appState json.RawMessage, err error) { + + var addr sdk.Address + var secret string + addr, secret, err = GenerateCoinKey() + if err != nil { + return + } + fmt.Printf("secret recovery key:\n%s\n", secret) + + chainID = cmn.Fmt("test-chain-%v", cmn.RandStr(6)) + + validators = []tmtypes.GenesisValidator{{ + PubKey: pubKey, + Power: 10, + }} + + appState = json.RawMessage(fmt.Sprintf(`{ + "accounts": [{ + "address": "%s", + "coins": [ + { + "denom": "mycoin", + "amount": 9007199254740992 + } + ] + }] +}`, addr.String())) + return +} + // GenerateCoinKey returns the address of a public key, along with the secret // phrase to recover the private key. func GenerateCoinKey() (sdk.Address, string, error) { + // construct an in-memory key store codec, err := words.LoadCodec("english") if err != nil { @@ -231,7 +175,6 @@ func GenerateCoinKey() (sdk.Address, string, error) { if err != nil { return nil, "", err } - addr := info.PubKey.Address() return addr, secret, nil } diff --git a/server/test_helpers.go b/server/test_helpers.go index f226ba1b1..01a093f47 100644 --- a/server/test_helpers.go +++ b/server/test_helpers.go @@ -50,7 +50,7 @@ func StartServer(t *testing.T) chan error { // init server ctx := NewContext(cfg, log.NewNopLogger()) - initCmd := InitCmd(mock.GenInitOptions, ctx) + initCmd := InitCmd(mock.GenAppState, ctx) err = initCmd.RunE(nil, nil) require.NoError(t, err) diff --git a/server/util.go b/server/util.go index cf37ec5cc..83f22db82 100644 --- a/server/util.go +++ b/server/util.go @@ -63,7 +63,7 @@ func PersistentPreRunEFn(context *Context) func(*cobra.Command, []string) error // add server commands func AddCommands( rootCmd *cobra.Command, - appState GenAppState, appCreator AppCreator, + appState GenAppParams, appCreator AppCreator, context *Context) { rootCmd.PersistentFlags().String("log_level", context.Config.LogLevel, "Log level")