diff --git a/client/lcd/test_helpers.go b/client/lcd/test_helpers.go index 0ada2a964dcd..5ff1ba353d2b 100644 --- a/client/lcd/test_helpers.go +++ b/client/lcd/test_helpers.go @@ -214,6 +214,7 @@ func InitializeTestLCD( genTxs := []json.RawMessage{} // append any additional (non-proposing) validators + var accs []gapp.GenesisAccount for i := 0; i < nValidators; i++ { operPrivKey := secp256k1.GenPrivKey() operAddr := operPrivKey.PubKey().Address() @@ -242,9 +243,17 @@ func InitializeTestLCD( genTxs = append(genTxs, txBytes) valConsPubKeys = append(valConsPubKeys, pubKey) valOperAddrs = append(valOperAddrs, sdk.ValAddress(operAddr)) + + accAuth := auth.NewBaseAccountWithAddress(sdk.AccAddress(operAddr)) + accAuth.Coins = sdk.Coins{sdk.NewInt64Coin("steak", 150)} + accs = append(accs, gapp.NewGenesisAccount(&accAuth)) } - genesisState, err := gapp.GaiaAppGenState(cdc, tmtypes.GenesisDoc{}, genTxs) + appGenState := gapp.DefaultGenesisState() + appGenState.Accounts = accs + genDoc.AppState, err = cdc.MarshalJSON(appGenState) + require.NoError(t, err) + genesisState, err := gapp.GaiaAppGenState(cdc, *genDoc, genTxs) require.NoError(t, err) // add some tokens to init accounts diff --git a/cmd/gaia/app/genesis.go b/cmd/gaia/app/genesis.go index 515b759c8399..8a489795eb82 100644 --- a/cmd/gaia/app/genesis.go +++ b/cmd/gaia/app/genesis.go @@ -132,6 +132,7 @@ func GaiaAppGenState(cdc *codec.Codec, genDoc tmtypes.GenesisDoc, appGenTxs []js return genesisState, nil } +// DefaultGenesisState generates the default state for gaia. func DefaultGenesisState() GenesisState { return GenesisState{ Accounts: nil, @@ -144,7 +145,6 @@ func DefaultGenesisState() GenesisState { } } - // GaiaValidateGenesisState ensures that the genesis state obeys the expected invariants // TODO: No validators are both bonded and jailed (#2088) // TODO: Error if there is a duplicate validator (#1708) diff --git a/cmd/gaia/app/genesis_test.go b/cmd/gaia/app/genesis_test.go index 1acc9f39314b..568661100a9c 100644 --- a/cmd/gaia/app/genesis_test.go +++ b/cmd/gaia/app/genesis_test.go @@ -5,7 +5,6 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth" - "github.com/cosmos/cosmos-sdk/x/gov" "github.com/cosmos/cosmos-sdk/x/stake" stakeTypes "github.com/cosmos/cosmos-sdk/x/stake/types" "github.com/stretchr/testify/require" @@ -27,7 +26,8 @@ var ( func makeGenesisState(t *testing.T, genTxs []auth.StdTx) GenesisState { // start with the default staking genesis state - stakeData := stake.DefaultGenesisState() + appState := DefaultGenesisState() + stakeData := appState.StakeData genAccs := make([]GenesisAccount, len(genTxs)) for i, genTx := range genTxs { @@ -35,17 +35,15 @@ func makeGenesisState(t *testing.T, genTxs []auth.StdTx) GenesisState { require.Equal(t, 1, len(msgs)) msg := msgs[0].(stake.MsgCreateValidator) - // get genesis flag account information - genAccs[i] = genesisAccountFromMsgCreateValidator(msg, freeFermionsAcc) - stakeData.Pool.LooseTokens = stakeData.Pool.LooseTokens.Add(sdk.NewDecFromInt(freeFermionsAcc)) // increase the supply + acc := auth.NewBaseAccountWithAddress(sdk.AccAddress(msg.ValidatorAddr)) + acc.Coins = sdk.Coins{sdk.NewInt64Coin("steak", 150)} + genAccs[i] = NewGenesisAccount(&acc) + stakeData.Pool.LooseTokens = stakeData.Pool.LooseTokens.Add(sdk.NewDec(150)) // increase the supply } // create the final app state - return GenesisState{ - Accounts: genAccs, - StakeData: stakeData, - GovData: gov.DefaultGenesisState(), - } + appState.Accounts = genAccs + return appState } func TestToAccount(t *testing.T) { diff --git a/cmd/gaia/cli_test/cli_test.go b/cmd/gaia/cli_test/cli_test.go index ad1549641bd9..4ca70f8a7737 100644 --- a/cmd/gaia/cli_test/cli_test.go +++ b/cmd/gaia/cli_test/cli_test.go @@ -5,9 +5,11 @@ package clitest import ( "encoding/json" "fmt" + "github.com/tendermint/tendermint/types" "io/ioutil" "os" "path" + "path/filepath" "testing" "github.com/stretchr/testify/require" @@ -541,7 +543,7 @@ func TestGaiaCLIConfig(t *testing.T) { servAddr, port, err := server.FreeTCPAddr() require.NoError(t, err) node := fmt.Sprintf("%s:%s", servAddr, port) - chainID := executeInit(t, fmt.Sprintf("gaiad init -o --name=foo --home=%s --home-client=%s", gaiadHome, gaiacliHome)) + chainID := executeInit(t, fmt.Sprintf("gaiad init -o --moniker=foo --home=%s", gaiadHome)) executeWrite(t, fmt.Sprintf("gaiacli --home=%s config", gaiadHome), gaiacliHome, node, "y") config, err := ioutil.ReadFile(path.Join(gaiacliHome, "config", "config.toml")) require.NoError(t, err) @@ -591,12 +593,27 @@ func initializeFixtures(t *testing.T) (chainID, servAddr, port string) { tests.ExecuteT(t, fmt.Sprintf("gaiad --home=%s unsafe-reset-all", gaiadHome), "") executeWrite(t, fmt.Sprintf("gaiacli keys delete --home=%s foo", gaiacliHome), app.DefaultKeyPass) executeWrite(t, fmt.Sprintf("gaiacli keys delete --home=%s bar", gaiacliHome), app.DefaultKeyPass) - - chainID = executeInit(t, fmt.Sprintf("gaiad init -o --name=foo --home=%s --home-client=%s", gaiadHome, gaiacliHome)) + executeWrite(t, fmt.Sprintf("gaiacli keys add --home=%s foo", gaiacliHome), app.DefaultKeyPass) executeWrite(t, fmt.Sprintf("gaiacli keys add --home=%s bar", gaiacliHome), app.DefaultKeyPass) - + fooAddr, _ := executeGetAddrPK(t, fmt.Sprintf( + "gaiacli keys show foo --output=json --home=%s", gaiacliHome)) + chainID = executeInit(t, fmt.Sprintf("gaiad init -o --moniker=foo --home=%s", gaiadHome)) + genFile := filepath.Join(gaiadHome, "config", "genesis.json") + genDoc := readGenesisFile(t, genFile) + var appState app.GenesisState + err := codec.Cdc.UnmarshalJSON(genDoc.AppState, &appState) + require.NoError(t, err) + appState.Accounts = []app.GenesisAccount{newGenesisAccount(fooAddr)} + appStateJSON, err := codec.Cdc.MarshalJSON(appState) + require.NoError(t, err) + genDoc.AppState = appStateJSON + genDoc.SaveAs(genFile) + executeWrite(t, fmt.Sprintf( + "gaiad gentx --name=foo --home=%s --home-client=%s", gaiadHome, gaiacliHome), + app.DefaultKeyPass) + executeWrite(t, fmt.Sprintf("gaiad collect-gentxs --home=%s", gaiadHome), app.DefaultKeyPass) // get a free port, also setup some common flags - servAddr, port, err := server.FreeTCPAddr() + servAddr, port, err = server.FreeTCPAddr() require.NoError(t, err) return } @@ -615,6 +632,27 @@ func writeToNewTempFile(t *testing.T, s string) *os.File { return fp } +func readGenesisFile(t *testing.T, genFile string) types.GenesisDoc { + var genDoc types.GenesisDoc + fp, err := os.Open(genFile) + require.NoError(t, err) + fileContents, err := ioutil.ReadAll(fp) + require.NoError(t, err) + defer fp.Close() + err = codec.Cdc.UnmarshalJSON(fileContents, &genDoc) + require.NoError(t, err) + return genDoc +} + +func newGenesisAccount(addr sdk.AccAddress) app.GenesisAccount { + acc := auth.NewBaseAccountWithAddress(addr) + acc.Coins = sdk.Coins{ + sdk.NewInt64Coin("steak", 150), + sdk.NewInt64Coin("fooToken", 1000), + } + return app.NewGenesisAccount(&acc) +} + //___________________________________________________________________________________ // executors diff --git a/cmd/gaia/cmd/gaiad/main.go b/cmd/gaia/cmd/gaiad/main.go index e829c7c6bf53..434a1d1dedd5 100644 --- a/cmd/gaia/cmd/gaiad/main.go +++ b/cmd/gaia/cmd/gaiad/main.go @@ -32,7 +32,7 @@ func main() { appInit := app.GaiaAppInit() rootCmd.AddCommand(gaiaInit.InitCmd(ctx, cdc, appInit)) rootCmd.AddCommand(gaiaInit.CollectGenTxsCmd(ctx, cdc)) - rootCmd.AddCommand(gaiaInit.TestnetFilesCmd(ctx, cdc)) + rootCmd.AddCommand(gaiaInit.TestnetFilesCmd(ctx, cdc, server.AppInit{})) rootCmd.AddCommand(gaiaInit.GenTxCmd(ctx, cdc)) server.AddCommands(ctx, cdc, rootCmd, appInit, diff --git a/cmd/gaia/init/collect.go b/cmd/gaia/init/collect.go index c58781ce3105..04cac0235e2b 100644 --- a/cmd/gaia/init/collect.go +++ b/cmd/gaia/init/collect.go @@ -20,11 +20,11 @@ import ( ) type initConfig struct { - ChainID string - GenTxsDir string - Name string - NodeID string - ValPubKey crypto.PubKey + ChainID string + GenTxsDir string + Name string + NodeID string + ValPubKey crypto.PubKey } // nolint @@ -32,7 +32,7 @@ func CollectGenTxsCmd(ctx *server.Context, cdc *codec.Codec) *cobra.Command { cmd := &cobra.Command{ Use: "collect-gentxs", Short: "Collect genesis txs and output a genesis.json file", - Args: cobra.MaximumNArgs(1), + Args: cobra.MaximumNArgs(1), RunE: func(_ *cobra.Command, _ []string) error { config := ctx.Config config.SetRoot(viper.GetString(cli.HomeFlag)) @@ -49,15 +49,16 @@ func CollectGenTxsCmd(ctx *server.Context, cdc *codec.Codec) *cobra.Command { toPrint := printInfo{ Moniker: config.Moniker, + ChainID: genDoc.ChainID, NodeID: nodeID, } initCfg := initConfig{ - ChainID: genDoc.ChainID, - GenTxsDir: filepath.Join(config.RootDir, "config", "gentx"), - Name: name, - NodeID: nodeID, - ValPubKey: valPubKey, + ChainID: genDoc.ChainID, + GenTxsDir: filepath.Join(config.RootDir, "config", "gentx"), + Name: name, + NodeID: nodeID, + ValPubKey: valPubKey, } appMessage, err := genTxsWithConfig(cdc, config, initCfg, genDoc) if err != nil { @@ -71,7 +72,7 @@ func CollectGenTxsCmd(ctx *server.Context, cdc *codec.Codec) *cobra.Command { } cmd.Flags().String(cli.HomeFlag, app.DefaultNodeHome, "node's home directory") - cmd.Flags().String(client.FlagName, "", "name of private key with which to sign the gentx") + //cmd.Flags().String(client.FlagName, "", "name of private key with which to sign the gentx") return cmd } @@ -85,7 +86,6 @@ func genTxsWithConfig(cdc *codec.Codec, config *cfg.Config, initCfg initConfig, var genTxs []json.RawMessage var jsonRawTx json.RawMessage - appGenTxs, persistentPeers, err = app.CollectStdTxs(config.Moniker, initCfg.GenTxsDir, cdc) if err != nil { return diff --git a/cmd/gaia/init/gentx.go b/cmd/gaia/init/gentx.go index 75b8b87c1ede..1039f87047e1 100644 --- a/cmd/gaia/init/gentx.go +++ b/cmd/gaia/init/gentx.go @@ -55,7 +55,7 @@ following delegation and commission default parameters: if err != nil { return err } - genDoc, err := loadGenesisDoc(cdc, config.GenesisFile()) + genDoc, err := loadGenesisDoc(cdc, config.GenesisFile()) if err != nil { return err } @@ -85,6 +85,7 @@ following delegation and commission default parameters: }, } + cmd.Flags().String(tmcli.HomeFlag, app.DefaultNodeHome, "node's home directory") cmd.Flags().String(flagClientHome, app.DefaultCLIHome, "client's home directory") cmd.Flags().String(client.FlagName, "", "name of private key with which to sign the gentx") cmd.MarkFlagRequired(client.FlagName) @@ -93,7 +94,7 @@ following delegation and commission default parameters: func prepareFlagsForTxCreateValidator(config *cfg.Config, nodeID, ip, chainID string, valPubKey crypto.PubKey) { - viper.Set(tmcli.HomeFlag, viper.GetString(flagClientHome)) // --home + viper.Set(tmcli.HomeFlag, viper.GetString(flagClientHome)) // --home viper.Set(client.FlagChainID, chainID) viper.Set(client.FlagFrom, viper.GetString(client.FlagName)) // --from viper.Set(cli.FlagNodeID, nodeID) // --node-id diff --git a/cmd/gaia/init/init.go b/cmd/gaia/init/init.go index 3b5b2a44b733..e65314947377 100644 --- a/cmd/gaia/init/init.go +++ b/cmd/gaia/init/init.go @@ -22,9 +22,9 @@ import ( ) const ( - flagOverwrite = "overwrite" - flagClientHome = "home-client" - flagMoniker = "moniker" + flagOverwrite = "overwrite" + flagClientHome = "home-client" + flagMoniker = "moniker" ) type printInfo struct { @@ -50,8 +50,8 @@ func InitCmd(ctx *server.Context, cdc *codec.Codec, appInit server.AppInit) *cob cmd := &cobra.Command{ Use: "init", Short: "Initialize private validator, p2p, genesis, and application configuration files", - Long: `Initialize validators's and node's configuration files.`, - Args: cobra.NoArgs, + Long: `Initialize validators's and node's configuration files.`, + Args: cobra.NoArgs, RunE: func(_ *cobra.Command, _ []string) error { config := ctx.Config config.SetRoot(viper.GetString(cli.HomeFlag)) @@ -72,16 +72,16 @@ func InitCmd(ctx *server.Context, cdc *codec.Codec, appInit server.AppInit) *cob genFile := config.GenesisFile() if appState, err = initializeEmptyGenesis(cdc, genFile, chainID, viper.GetBool(flagOverwrite)); err != nil { - return err + return err } if err = WriteGenesisFile(genFile, chainID, nil, appState); err != nil { return err } toPrint := printInfo{ - ChainID: chainID, - Moniker: config.Moniker, - NodeID: nodeID, + ChainID: chainID, + Moniker: config.Moniker, + NodeID: nodeID, AppMessage: appState, } diff --git a/cmd/gaia/init/init_test.go b/cmd/gaia/init/init_test.go index 48a5d9247c35..bd5274c26113 100644 --- a/cmd/gaia/init/init_test.go +++ b/cmd/gaia/init/init_test.go @@ -42,7 +42,6 @@ func setupClientHome(t *testing.T) func() { clientDir, err := ioutil.TempDir("", "mock-sdk-cmd") require.Nil(t, err) viper.Set(flagClientHome, clientDir) - viper.Set(flagOverwriteKey, true) return func() { if err := os.RemoveAll(clientDir); err != nil { // TODO: Handle with #870 diff --git a/cmd/gaia/init/testnet.go b/cmd/gaia/init/testnet.go index 674fcc9ddc7a..59ff590ed738 100644 --- a/cmd/gaia/init/testnet.go +++ b/cmd/gaia/init/testnet.go @@ -36,7 +36,9 @@ var ( const nodeDirPerm = 0755 // get cmd to initialize all files for tendermint testnet and application -func TestnetFilesCmd(ctx *server.Context, cdc *codec.Codec) *cobra.Command { +func TestnetFilesCmd(ctx *server.Context, cdc *codec.Codec, + appInit server.AppInit) *cobra.Command { + cmd := &cobra.Command{ Use: "testnet", Short: "Initialize files for a Gaiad testnet", @@ -196,8 +198,8 @@ func testnetWithConfig(config *cfg.Config, cdc *codec.Codec) error { return err } genDoc := types.GenesisDoc{ - ChainID: chainID, - AppState: appGenStateJSON, + ChainID: chainID, + AppState: appGenStateJSON, Validators: nil, } @@ -220,11 +222,11 @@ func testnetWithConfig(config *cfg.Config, cdc *codec.Codec) error { nodeID, valPubKey := nodeIDs[i], valPubKeys[i] // Run `init` and generate genesis.json and config.toml initCfg := initConfig{ - ChainID: chainID, - GenTxsDir: gentxsDir, - Name: moniker, - NodeID: nodeID, - ValPubKey: valPubKey, + ChainID: chainID, + GenTxsDir: gentxsDir, + Name: moniker, + NodeID: nodeID, + ValPubKey: valPubKey, } genDoc, err := loadGenesisDoc(cdc, config.GenesisFile()) if err != nil { diff --git a/examples/basecoin/cmd/basecoind/main.go b/examples/basecoin/cmd/basecoind/main.go index 239f1c2ec1ad..4f40a145094e 100644 --- a/examples/basecoin/cmd/basecoind/main.go +++ b/examples/basecoin/cmd/basecoind/main.go @@ -41,7 +41,6 @@ func main() { appInit := server.DefaultAppInit rootCmd.AddCommand(InitCmd(ctx, cdc, appInit)) - rootCmd.AddCommand(gaiaInit.TestnetFilesCmd(ctx, cdc)) server.AddCommands(ctx, cdc, rootCmd, appInit, newApp, exportAppStateAndTMValidators) diff --git a/examples/democoin/cmd/democoind/main.go b/examples/democoin/cmd/democoind/main.go index 8f156b599142..8dc15e46883a 100644 --- a/examples/democoin/cmd/democoind/main.go +++ b/examples/democoin/cmd/democoind/main.go @@ -35,7 +35,7 @@ var CoolAppInit = server.AppInit{ } // coolGenAppParams sets up the app_state and appends the cool app state -func CoolAppGenState(cdc *codec.Codec, appGenTxs []json.RawMessage) ( +func CoolAppGenState(cdc *codec.Codec, genDoc tmtypes.GenesisDoc, appGenTxs []json.RawMessage) ( appState json.RawMessage, err error) { appState, err = server.SimpleAppGenState(cdc, tmtypes.GenesisDoc{}, appGenTxs) if err != nil { @@ -90,7 +90,8 @@ func InitCmd(ctx *server.Context, cdc *codec.Codec, appInit server.AppInit) *cob return err } - appState, err := appInit.AppGenState(cdc, []json.RawMessage{genTx}) + appState, err := appInit.AppGenState(cdc, tmtypes.GenesisDoc{}, + []json.RawMessage{genTx}) if err != nil { return err } diff --git a/server/mock/app.go b/server/mock/app.go index 18afa164ec64..dd8f3f5aba7a 100644 --- a/server/mock/app.go +++ b/server/mock/app.go @@ -3,6 +3,7 @@ package mock import ( "encoding/json" "fmt" + "github.com/tendermint/tendermint/types" "path/filepath" abci "github.com/tendermint/tendermint/abci/types" @@ -102,7 +103,8 @@ func InitChainer(key sdk.StoreKey) func(sdk.Context, abci.RequestInitChain) abci // AppGenState can be passed into InitCmd, returns a static string of a few // key-values that can be parsed by InitChainer -func AppGenState(_ *codec.Codec, _ []json.RawMessage) (appState json.RawMessage, err error) { +func AppGenState(_ *codec.Codec, _ types.GenesisDoc, _ []json.RawMessage) (appState json. + RawMessage, err error) { appState = json.RawMessage(`{ "values": [ { @@ -119,7 +121,8 @@ func AppGenState(_ *codec.Codec, _ []json.RawMessage) (appState json.RawMessage, } // AppGenStateEmpty returns an empty transaction state for mocking. -func AppGenStateEmpty(_ *codec.Codec, _ []json.RawMessage) (appState json.RawMessage, err error) { +func AppGenStateEmpty(_ *codec.Codec, _ types.GenesisDoc, _ []json.RawMessage) ( + appState json.RawMessage, err error) { appState = json.RawMessage(``) return } diff --git a/server/mock/app_test.go b/server/mock/app_test.go index 05ec86521943..a5f2a078b263 100644 --- a/server/mock/app_test.go +++ b/server/mock/app_test.go @@ -1,6 +1,7 @@ package mock import ( + "github.com/tendermint/tendermint/types" "testing" "github.com/stretchr/testify/require" @@ -20,7 +21,7 @@ func TestInitApp(t *testing.T) { require.NoError(t, err) // initialize it future-way - appState, err := AppGenState(nil, nil) + appState, err := AppGenState(nil, types.GenesisDoc{}, nil) require.NoError(t, err) //TODO test validators in the init chain?