From 38d0336e42df5c4761ce52977c7d85b99dd92176 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Thu, 11 Oct 2018 06:37:52 -0700 Subject: [PATCH 01/83] Add arg to PrintUnsignedStdTx() to actually operate in offline mode Finalize gaiacli tx create-validator --genesis-format --- cmd/gaia/init/testnet.go | 3 ++- x/stake/client/cli/tx.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/cmd/gaia/init/testnet.go b/cmd/gaia/init/testnet.go index 48b714f2de21..4c9d812485ca 100644 --- a/cmd/gaia/init/testnet.go +++ b/cmd/gaia/init/testnet.go @@ -2,10 +2,11 @@ package init import ( "fmt" - "github.com/cosmos/cosmos-sdk/server" "net" "path/filepath" + "github.com/cosmos/cosmos-sdk/server" + "github.com/spf13/cobra" gc "github.com/cosmos/cosmos-sdk/server/config" diff --git a/x/stake/client/cli/tx.go b/x/stake/client/cli/tx.go index 9640c5c05069..1e1d3515f0c1 100644 --- a/x/stake/client/cli/tx.go +++ b/x/stake/client/cli/tx.go @@ -7,15 +7,24 @@ import ( "github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/client/utils" "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/server" sdk "github.com/cosmos/cosmos-sdk/types" authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli" authtxb "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder" "github.com/cosmos/cosmos-sdk/x/stake" + "github.com/tendermint/tendermint/p2p" + "github.com/spf13/cobra" "github.com/spf13/viper" ) +const ( + flagGenesisFormat = "genesis-format" + flagIP = "ip" + flagNodeKeyFile = "node-key-file" +) + // GetCmdCreateValidator implements the create validator command handler. func GetCmdCreateValidator(cdc *codec.Codec) *cobra.Command { cmd := &cobra.Command{ @@ -87,6 +96,25 @@ func GetCmdCreateValidator(cdc *codec.Codec) *cobra.Command { ) } + if viper.GetBool(flagGenesisFormat) { + ip := viper.GetString(flagIP) + if len(ip) == 0 { + eip, err := server.ExternalIP() + if err != nil { + return err + } + ip = eip + } + + nodeKey, err := p2p.LoadOrGenNodeKey(viper.GetString(flagNodeKeyFile)) + if err != nil { + return err + } + txBldr = txBldr.WithMemo(fmt.Sprintf("%s@%s:26656", string(nodeKey.ID()), ip)) + + return utils.PrintUnsignedStdTx(txBldr, cliCtx, []sdk.Msg{msg}, true) + } + if cliCtx.GenerateOnly { return utils.PrintUnsignedStdTx(txBldr, cliCtx, []sdk.Msg{msg}, true) } @@ -101,6 +129,9 @@ func GetCmdCreateValidator(cdc *codec.Codec) *cobra.Command { cmd.Flags().AddFlagSet(fsDescriptionCreate) cmd.Flags().AddFlagSet(fsCommissionCreate) cmd.Flags().AddFlagSet(fsDelegator) + cmd.Flags().Bool(flagGenesisFormat, false, "Export the transaction in gen-tx format; it implies --generate-only") + cmd.Flags().String(flagIP, "", fmt.Sprintf("External IP to use as node's public address; if left blank IP will be retrieved from this machine. It takes effect only when used in combination with --%s", flagGenesisFormat)) + cmd.Flags().String(flagNodeKeyFile, "", "Path to to node_key.json") return cmd } From 36845cfbc47008495b6073ce59f5db17a59ced92 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Thu, 11 Oct 2018 06:38:46 -0700 Subject: [PATCH 02/83] New func processStdTxs() --- server/init.go | 1 + 1 file changed, 1 insertion(+) diff --git a/server/init.go b/server/init.go index 091ffa9484e6..b9c945877f19 100644 --- a/server/init.go +++ b/server/init.go @@ -3,6 +3,7 @@ package server import ( "encoding/json" "fmt" + "github.com/cosmos/cosmos-sdk/crypto/keys" "github.com/pkg/errors" "github.com/spf13/pflag" From ffb5e46f860d624975e03bfe7fd82eee7deff922 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Tue, 2 Oct 2018 16:01:32 +0200 Subject: [PATCH 03/83] New function to create genesis account from MsgCreateValidator --- cmd/gaia/app/genesis.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/cmd/gaia/app/genesis.go b/cmd/gaia/app/genesis.go index 690d92502cc1..d15b05af2715 100644 --- a/cmd/gaia/app/genesis.go +++ b/cmd/gaia/app/genesis.go @@ -240,6 +240,14 @@ func genesisAccountFromGenTx(genTx GaiaGenTx) GenesisAccount { return NewGenesisAccount(&accAuth) } +func genesisAccountFromStdTx(genTx auth.StdTx) GenesisAccount { + m := genTx.GetMsgs()[0] + msg := m.(stake.MsgCreateValidator) + accAuth := auth.NewBaseAccountWithAddress(msg.ValidatorAddr) + acc.Coins = sdk.Coin{msg.Delegation} + return NewGenesisAccount(&accAuth) +} + // 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) From 70608391154f021882a01e5cfa895a9f4f593426 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Thu, 11 Oct 2018 06:39:20 -0700 Subject: [PATCH 04/83] Remove gen-tx command --- server/init.go | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/server/init.go b/server/init.go index b9c945877f19..fb0ce1fbab73 100644 --- a/server/init.go +++ b/server/init.go @@ -5,6 +5,8 @@ import ( "fmt" "github.com/cosmos/cosmos-sdk/crypto/keys" + "github.com/cosmos/cosmos-sdk/x/auth" + "github.com/cosmos/cosmos-sdk/x/stake" "github.com/pkg/errors" "github.com/spf13/pflag" "github.com/tendermint/tendermint/crypto" @@ -61,19 +63,19 @@ type AppInit struct { FlagsAppGenTx *pflag.FlagSet // create the application genesis tx - AppGenTx func(cdc *codec.Codec, pk crypto.PubKey, genTxConfig serverconfig.GenTx) ( - appGenTx, cliPrint json.RawMessage, validator tmtypes.GenesisValidator, err error) + // AppGenTx func(cdc *codec.Codec, pk crypto.PubKey, genTxConfig serverconfig.GenTx) ( + // appGenTx, cliPrint json.RawMessage, validator tmtypes.GenesisValidator, err error) // AppGenState creates the core parameters initialization. It takes in a // pubkey meant to represent the pubkey of the validator of this machine. - AppGenState func(cdc *codec.Codec, appGenTxs []json.RawMessage) (appState json.RawMessage, err error) + AppGenState func(cdc *codec.Codec, appGenTx []auth.StdTx) (appState json.RawMessage, err error) } //_____________________________________________________________________ // simple default application init var DefaultAppInit = AppInit{ - AppGenTx: SimpleAppGenTx, + // AppGenTx: SimpleAppGenTx, AppGenState: SimpleAppGenState, } @@ -116,19 +118,20 @@ func SimpleAppGenTx(cdc *codec.Codec, pk crypto.PubKey, genTxConfig serverconfig } // create the genesis app state -func SimpleAppGenState(cdc *codec.Codec, appGenTxs []json.RawMessage) (appState json.RawMessage, err error) { +func SimpleAppGenState(cdc *codec.Codec, appGenTxs []auth.StdTx) (appState json.RawMessage, err error) { if len(appGenTxs) != 1 { err = errors.New("must provide a single genesis transaction") return } - var genTx SimpleGenTx - err = cdc.UnmarshalJSON(appGenTxs[0], &genTx) - if err != nil { + msgs := appGenTxs[0].GetMsgs() + if len(msgs) != 1 { + err = errors.New("must provide a single genesis message") return } + msg := msgs[0].(stake.MsgCreateValidator) appState = json.RawMessage(fmt.Sprintf(`{ "accounts": [{ "address": "%s", @@ -139,7 +142,7 @@ func SimpleAppGenState(cdc *codec.Codec, appGenTxs []json.RawMessage) (appState } ] }] -}`, genTx.Addr)) +}`, msg.ValidatorAddr)) return } From d365b1107265511cb062b51bc66166d66cc0a98c Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Wed, 17 Oct 2018 14:50:55 -0700 Subject: [PATCH 05/83] Update gaia to work with auth.StdTx --- cmd/gaia/app/genesis.go | 45 +++++++++++++++++------------------------ 1 file changed, 19 insertions(+), 26 deletions(-) diff --git a/cmd/gaia/app/genesis.go b/cmd/gaia/app/genesis.go index d15b05af2715..cf1afa68ce7e 100644 --- a/cmd/gaia/app/genesis.go +++ b/cmd/gaia/app/genesis.go @@ -81,8 +81,6 @@ func GaiaAppInit() server.AppInit { return server.AppInit{ FlagsAppGenState: fsAppGenState, FlagsAppGenTx: fsAppGenTx, - AppGenTx: GaiaAppGenTx, - AppGenState: GaiaAppGenStateJSON, } } @@ -171,27 +169,34 @@ func GaiaAppGenState(cdc *codec.Codec, appGenTxs []json.RawMessage) (genesisStat // start with the default staking genesis state stakeData := stake.DefaultGenesisState() - slashingData := slashing.DefaultGenesisState() // get genesis flag account information genaccs := make([]GenesisAccount, len(appGenTxs)) - for i, appGenTx := range appGenTxs { - var genTx GaiaGenTx - err = cdc.UnmarshalJSON(appGenTx, &genTx) + for i, jsonRawMessage := range appGenTxs { + var genTx auth.StdTx + err = cdc.UnmarshalJSON(jsonRawMessage, &genTx) if err != nil { return } + msgs := genTx.GetMsgs() + if len(msgs) == 0 { + err = errors.New("must provide at least genesis message") + return + } + msg := msgs[0].(stake.MsgCreateValidator) + // genaccs[i] = genesisAccountFromGenTx(genTx) // create the genesis account, give'm few steaks and a buncha token with there name - genaccs[i] = genesisAccountFromGenTx(genTx) + genaccs[i] = genesisAccountFromMsgCreateValidator(msg) stakeData.Pool.LooseTokens = stakeData.Pool.LooseTokens.Add(sdk.NewDecFromInt(freeFermionsAcc)) // increase the supply // add the validator - if len(genTx.Name) > 0 { - stakeData = addValidatorToStakeData(genTx, stakeData) + if len(msg.Description.Moniker) > 0 { + stakeData = addValidatorToStakeData(msg, stakeData) } + } // create the final app state @@ -206,10 +211,9 @@ func GaiaAppGenState(cdc *codec.Codec, appGenTxs []json.RawMessage) (genesisStat return } -func addValidatorToStakeData(genTx GaiaGenTx, stakeData stake.GenesisState) stake.GenesisState { - desc := stake.NewDescription(genTx.Name, "", "", "") +func addValidatorToStakeData(msg stake.MsgCreateValidator, stakeData stake.GenesisState) stake.GenesisState { validator := stake.NewValidator( - sdk.ValAddress(genTx.Address), sdk.MustGetConsPubKeyBech32(genTx.PubKey), desc, + sdk.ValAddress(msg.ValidatorAddr), sdk.MustGetConsPubKeyBech32(msg.PubKey), genTx.Description, ) stakeData.Pool.LooseTokens = stakeData.Pool.LooseTokens.Add(sdk.NewDec(freeFermionVal)) // increase the supply @@ -231,20 +235,9 @@ func addValidatorToStakeData(genTx GaiaGenTx, stakeData stake.GenesisState) stak return stakeData } -func genesisAccountFromGenTx(genTx GaiaGenTx) GenesisAccount { - accAuth := auth.NewBaseAccountWithAddress(genTx.Address) - accAuth.Coins = sdk.Coins{ - {genTx.Name + "Token", sdk.NewInt(1000)}, - {"steak", freeFermionsAcc}, - } - return NewGenesisAccount(&accAuth) -} - -func genesisAccountFromStdTx(genTx auth.StdTx) GenesisAccount { - m := genTx.GetMsgs()[0] - msg := m.(stake.MsgCreateValidator) - accAuth := auth.NewBaseAccountWithAddress(msg.ValidatorAddr) - acc.Coins = sdk.Coin{msg.Delegation} +func genesisAccountFromMsgCreateValidator(msg stake.MsgCreateValidator) GenesisAccount { + accAuth := auth.NewBaseAccountWithAddress(sdk.AccAddress(msg.ValidatorAddr)) + accAuth.Coins = sdk.Coins{msg.Delegation} return NewGenesisAccount(&accAuth) } From cf63a53caf91d692210b2d31b0098ccf67a08809 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Thu, 11 Oct 2018 06:46:05 -0700 Subject: [PATCH 06/83] Temporarily disable dead code --- cmd/gaia/init/testnet.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/cmd/gaia/init/testnet.go b/cmd/gaia/init/testnet.go index 4c9d812485ca..ab6ee166090e 100644 --- a/cmd/gaia/init/testnet.go +++ b/cmd/gaia/init/testnet.go @@ -3,16 +3,13 @@ package init import ( "fmt" "net" + "os" "path/filepath" "github.com/cosmos/cosmos-sdk/server" "github.com/spf13/cobra" - gc "github.com/cosmos/cosmos-sdk/server/config" - - "os" - "github.com/cosmos/cosmos-sdk/codec" "github.com/spf13/viper" cfg "github.com/tendermint/tendermint/config" @@ -46,9 +43,12 @@ Example: gaiad testnet --v 4 --o ./output --starting-ip-address 192.168.10.2 `, RunE: func(_ *cobra.Command, _ []string) error { - config := ctx.Config - err := testnetWithConfig(config, cdc, appInit) - return err + /* + config := ctx.Config + err := testnetWithConfig(config, cdc, appInit) + return err + */ + return nil }, } cmd.Flags().Int(nValidators, 4, From 73138e5fae411de0c2259f10c5522c235a1bd196 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Thu, 11 Oct 2018 06:47:33 -0700 Subject: [PATCH 07/83] Fix build failures --- cmd/gaia/app/genesis.go | 8 ++++---- server/init.go | 2 -- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/cmd/gaia/app/genesis.go b/cmd/gaia/app/genesis.go index cf1afa68ce7e..2559b5a0b7cf 100644 --- a/cmd/gaia/app/genesis.go +++ b/cmd/gaia/app/genesis.go @@ -81,6 +81,7 @@ func GaiaAppInit() server.AppInit { return server.AppInit{ FlagsAppGenState: fsAppGenState, FlagsAppGenTx: fsAppGenTx, + AppGenState: GaiaAppGenStateJSON, } } @@ -213,7 +214,7 @@ func GaiaAppGenState(cdc *codec.Codec, appGenTxs []json.RawMessage) (genesisStat func addValidatorToStakeData(msg stake.MsgCreateValidator, stakeData stake.GenesisState) stake.GenesisState { validator := stake.NewValidator( - sdk.ValAddress(msg.ValidatorAddr), sdk.MustGetConsPubKeyBech32(msg.PubKey), genTx.Description, + sdk.ValAddress(msg.ValidatorAddr), msg.PubKey, msg.Description, ) stakeData.Pool.LooseTokens = stakeData.Pool.LooseTokens.Add(sdk.NewDec(freeFermionVal)) // increase the supply @@ -237,7 +238,7 @@ func addValidatorToStakeData(msg stake.MsgCreateValidator, stakeData stake.Genes func genesisAccountFromMsgCreateValidator(msg stake.MsgCreateValidator) GenesisAccount { accAuth := auth.NewBaseAccountWithAddress(sdk.AccAddress(msg.ValidatorAddr)) - accAuth.Coins = sdk.Coins{msg.Delegation} + accAuth.Coins = []sdk.Coin{msg.Delegation} return NewGenesisAccount(&accAuth) } @@ -272,8 +273,7 @@ func validateGenesisStateAccounts(accs []GenesisAccount) (err error) { } // GaiaAppGenState but with JSON -func GaiaAppGenStateJSON(cdc *codec.Codec, appGenTxs []json.RawMessage) (appState json.RawMessage, err error) { - +func GaiaAppGenStateJSON(cdc *codec.Codec, appGenTxs []auth.StdTx) (appState json.RawMessage, err error) { // create the final app state genesisState, err := GaiaAppGenState(cdc, appGenTxs) if err != nil { diff --git a/server/init.go b/server/init.go index fb0ce1fbab73..f0275649a49d 100644 --- a/server/init.go +++ b/server/init.go @@ -51,8 +51,6 @@ type InitConfig struct { Overwrite bool } -//________________________________________________________________________________________ - //_____________________________________________________________________ // Core functionality passed from the application to the server init command From 622acc1dd8bc1cf79c9c0dfe8bc9541e7bfed7f7 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Wed, 3 Oct 2018 11:54:27 +0200 Subject: [PATCH 08/83] Remove dead code --- cmd/gaia/app/genesis.go | 73 ----------------------------------------- server/init.go | 43 ------------------------ 2 files changed, 116 deletions(-) diff --git a/cmd/gaia/app/genesis.go b/cmd/gaia/app/genesis.go index 2559b5a0b7cf..651373044118 100644 --- a/cmd/gaia/app/genesis.go +++ b/cmd/gaia/app/genesis.go @@ -5,10 +5,8 @@ import ( "errors" "fmt" - "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/server" - "github.com/cosmos/cosmos-sdk/server/config" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth" distr "github.com/cosmos/cosmos-sdk/x/distribution" @@ -17,9 +15,6 @@ import ( "github.com/cosmos/cosmos-sdk/x/stake" "github.com/spf13/pflag" - - "github.com/tendermint/tendermint/crypto" - tmtypes "github.com/tendermint/tendermint/types" ) // DefaultKeyPass contains the default key password for genesis transactions @@ -92,74 +87,6 @@ type GaiaGenTx struct { PubKey string `json:"pub_key"` } -// GaiaAppGenTx generates a Gaia genesis transaction. -func GaiaAppGenTx( - cdc *codec.Codec, pk crypto.PubKey, genTxConfig config.GenTx, -) (appGenTx, cliPrint json.RawMessage, validator tmtypes.GenesisValidator, err error) { - if genTxConfig.Name == "" { - return nil, nil, tmtypes.GenesisValidator{}, errors.New("Must specify --name (validator moniker)") - } - - buf := client.BufferStdin() - prompt := fmt.Sprintf("Password for account '%s' (default %s):", genTxConfig.Name, DefaultKeyPass) - - keyPass, err := client.GetPassword(prompt, buf) - if err != nil && keyPass != "" { - // An error was returned that either failed to read the password from - // STDIN or the given password is not empty but failed to meet minimum - // length requirements. - return appGenTx, cliPrint, validator, err - } - - if keyPass == "" { - keyPass = DefaultKeyPass - } - - addr, secret, err := server.GenerateSaveCoinKey( - genTxConfig.CliRoot, - genTxConfig.Name, - keyPass, - genTxConfig.Overwrite, - ) - if err != nil { - return appGenTx, cliPrint, validator, err - } - - mm := map[string]string{"secret": secret} - bz, err := cdc.MarshalJSON(mm) - if err != nil { - return appGenTx, cliPrint, validator, err - } - - cliPrint = json.RawMessage(bz) - appGenTx, _, validator, err = GaiaAppGenTxNF(cdc, pk, addr, genTxConfig.Name) - - return appGenTx, cliPrint, validator, err -} - -// Generate a gaia genesis transaction without flags -func GaiaAppGenTxNF(cdc *codec.Codec, pk crypto.PubKey, addr sdk.AccAddress, name string) ( - appGenTx, cliPrint json.RawMessage, validator tmtypes.GenesisValidator, err error) { - - var bz []byte - gaiaGenTx := GaiaGenTx{ - Name: name, - Address: addr, - PubKey: sdk.MustBech32ifyConsPub(pk), - } - bz, err = codec.MarshalJSONIndent(cdc, gaiaGenTx) - if err != nil { - return - } - appGenTx = json.RawMessage(bz) - - validator = tmtypes.GenesisValidator{ - PubKey: pk, - Power: freeFermionVal, - } - return -} - // Create the core parameters for genesis initialization for gaia // note that the pubkey input is this machines pubkey func GaiaAppGenState(cdc *codec.Codec, appGenTxs []json.RawMessage) (genesisState GenesisState, err error) { diff --git a/server/init.go b/server/init.go index f0275649a49d..9583c9627c22 100644 --- a/server/init.go +++ b/server/init.go @@ -16,7 +16,6 @@ import ( clkeys "github.com/cosmos/cosmos-sdk/client/keys" "github.com/cosmos/cosmos-sdk/codec" - serverconfig "github.com/cosmos/cosmos-sdk/server/config" sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -60,10 +59,6 @@ type AppInit struct { FlagsAppGenState *pflag.FlagSet FlagsAppGenTx *pflag.FlagSet - // create the application genesis tx - // AppGenTx func(cdc *codec.Codec, pk crypto.PubKey, genTxConfig serverconfig.GenTx) ( - // appGenTx, cliPrint json.RawMessage, validator tmtypes.GenesisValidator, err error) - // AppGenState creates the core parameters initialization. It takes in a // pubkey meant to represent the pubkey of the validator of this machine. AppGenState func(cdc *codec.Codec, appGenTx []auth.StdTx) (appState json.RawMessage, err error) @@ -77,44 +72,6 @@ var DefaultAppInit = AppInit{ AppGenState: SimpleAppGenState, } -// simple genesis tx -type SimpleGenTx struct { - Addr sdk.AccAddress `json:"addr"` -} - -// Generate a genesis transaction -func SimpleAppGenTx(cdc *codec.Codec, pk crypto.PubKey, genTxConfig serverconfig.GenTx) ( - appGenTx, cliPrint json.RawMessage, validator tmtypes.GenesisValidator, err error) { - - var addr sdk.AccAddress - var secret string - addr, secret, err = GenerateCoinKey() - if err != nil { - return - } - - var bz []byte - simpleGenTx := SimpleGenTx{addr} - bz, err = cdc.MarshalJSON(simpleGenTx) - if err != nil { - return - } - appGenTx = json.RawMessage(bz) - - mm := map[string]string{"secret": secret} - bz, err = cdc.MarshalJSON(mm) - if err != nil { - return - } - cliPrint = json.RawMessage(bz) - - validator = tmtypes.GenesisValidator{ - PubKey: pk, - Power: 10, - } - return -} - // create the genesis app state func SimpleAppGenState(cdc *codec.Codec, appGenTxs []auth.StdTx) (appState json.RawMessage, err error) { From 9879a177721a9f3aede56081736119c994be6e3d Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Thu, 11 Oct 2018 06:48:32 -0700 Subject: [PATCH 09/83] Keep on cleaning up --- server/init.go | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/server/init.go b/server/init.go index 9583c9627c22..d54b020f5739 100644 --- a/server/init.go +++ b/server/init.go @@ -9,10 +9,8 @@ import ( "github.com/cosmos/cosmos-sdk/x/stake" "github.com/pkg/errors" "github.com/spf13/pflag" - "github.com/tendermint/tendermint/crypto" dbm "github.com/tendermint/tendermint/libs/db" - tmtypes "github.com/tendermint/tendermint/types" clkeys "github.com/cosmos/cosmos-sdk/client/keys" "github.com/cosmos/cosmos-sdk/codec" @@ -34,14 +32,6 @@ var ( FlagChainID = "chain-id" ) -// genesis piece structure for creating combined genesis -type GenesisTx struct { - NodeID string `json:"node_id"` - IP string `json:"ip"` - Validator tmtypes.GenesisValidator `json:"validator"` - AppGenTx json.RawMessage `json:"app_gen_tx"` -} - // Storage for init command input parameters type InitConfig struct { ChainID string @@ -50,8 +40,6 @@ type InitConfig struct { Overwrite bool } -//_____________________________________________________________________ - // Core functionality passed from the application to the server init command type AppInit struct { From 0b76c47a68a6305ea0ebcde0b80fa65a6447e701 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Thu, 11 Oct 2018 06:48:47 -0700 Subject: [PATCH 10/83] Cleanup, return validators as they need to be written into genesis.json --- cmd/gaia/app/genesis.go | 7 ------- server/init.go | 1 - 2 files changed, 8 deletions(-) diff --git a/cmd/gaia/app/genesis.go b/cmd/gaia/app/genesis.go index 651373044118..cb07d80f482b 100644 --- a/cmd/gaia/app/genesis.go +++ b/cmd/gaia/app/genesis.go @@ -67,15 +67,8 @@ func (ga *GenesisAccount) ToAccount() (acc *auth.BaseAccount) { func GaiaAppInit() server.AppInit { fsAppGenState := pflag.NewFlagSet("", pflag.ContinueOnError) - fsAppGenTx := pflag.NewFlagSet("", pflag.ContinueOnError) - fsAppGenTx.String(server.FlagName, "", "validator moniker, required") - fsAppGenTx.String(server.FlagClientHome, DefaultCLIHome, - "home directory for the client, used for key generation") - fsAppGenTx.Bool(server.FlagOWK, false, "overwrite the accounts created") - return server.AppInit{ FlagsAppGenState: fsAppGenState, - FlagsAppGenTx: fsAppGenTx, AppGenState: GaiaAppGenStateJSON, } } diff --git a/server/init.go b/server/init.go index d54b020f5739..b2d889aab90e 100644 --- a/server/init.go +++ b/server/init.go @@ -45,7 +45,6 @@ type AppInit struct { // flags required for application init functions FlagsAppGenState *pflag.FlagSet - FlagsAppGenTx *pflag.FlagSet // AppGenState creates the core parameters initialization. It takes in a // pubkey meant to represent the pubkey of the validator of this machine. From 8f3fc6839e90d7afd5be5fed66fde670c12ce019 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Thu, 11 Oct 2018 06:49:06 -0700 Subject: [PATCH 11/83] Cleanup, exclude node from persistent peers --- cmd/gaia/app/genesis.go | 1 - server/init.go | 15 ++++----------- 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/cmd/gaia/app/genesis.go b/cmd/gaia/app/genesis.go index cb07d80f482b..0b6dd5ae8d22 100644 --- a/cmd/gaia/app/genesis.go +++ b/cmd/gaia/app/genesis.go @@ -108,7 +108,6 @@ func GaiaAppGenState(cdc *codec.Codec, appGenTxs []json.RawMessage) (genesisStat } msg := msgs[0].(stake.MsgCreateValidator) - // genaccs[i] = genesisAccountFromGenTx(genTx) // create the genesis account, give'm few steaks and a buncha token with there name genaccs[i] = genesisAccountFromMsgCreateValidator(msg) stakeData.Pool.LooseTokens = stakeData.Pool.LooseTokens.Add(sdk.NewDecFromInt(freeFermionsAcc)) // increase the supply diff --git a/server/init.go b/server/init.go index b2d889aab90e..f071bd91e5ea 100644 --- a/server/init.go +++ b/server/init.go @@ -17,17 +17,10 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" ) -//Parameter names, for init gen-tx command -var ( - FlagName = "name" - FlagClientHome = "home-client" - FlagOWK = "owk" -) - -//parameter names, init command -var ( +// parameter names, init command +const ( + FlagMoniker = "moniker" FlagOverwrite = "overwrite" - FlagWithTxs = "with-txs" FlagIP = "ip" FlagChainID = "chain-id" ) @@ -35,8 +28,8 @@ var ( // Storage for init command input parameters type InitConfig struct { ChainID string - GenTxs bool GenTxsDir string + Moniker string Overwrite bool } From bca74a25dc4a67241e9b83e6134dd7bf0a093435 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Thu, 11 Oct 2018 06:51:15 -0700 Subject: [PATCH 12/83] Update test_helpers --- client/lcd/test_helpers.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/client/lcd/test_helpers.go b/client/lcd/test_helpers.go index 3585023cdb33..f390727f3b47 100644 --- a/client/lcd/test_helpers.go +++ b/client/lcd/test_helpers.go @@ -2,7 +2,6 @@ package lcd import ( "bytes" - "encoding/json" "fmt" "io/ioutil" "net" @@ -22,6 +21,7 @@ import ( "github.com/cosmos/cosmos-sdk/tests" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth" + stake "github.com/cosmos/cosmos-sdk/x/stake/types" "github.com/spf13/viper" "github.com/stretchr/testify/require" @@ -225,18 +225,18 @@ func InitializeTestLCD( ) } - var appGenTxs []json.RawMessage + var validatorsPKs []crypto.PubKey + // NOTE: It's bad practice to reuse public key address for the operator + // address but doing in the test for simplicity. + var appGenTxs []auth.StdTx for _, gdValidator := range genDoc.Validators { operAddr := ed25519.GenPrivKey().PubKey().Address() pk := gdValidator.PubKey - valConsPubKeys = append(valConsPubKeys, pk) - valOperAddrs = append(valOperAddrs, sdk.ValAddress(operAddr)) - - appGenTx, _, _, err := gapp.GaiaAppGenTxNF(cdc, pk, sdk.AccAddress(operAddr), gdValidator.Name) - require.NoError(t, err) - + desc := stake.NewDescription("test_val1", "", "", "") + msg := stake.NewMsgCreateValidator(sdk.ValAddress(pk.Address()), pk, sdk.NewInt64Coin("stake", 100), desc, stake.CommissionMsg{}) + appGenTx := auth.NewStdTx([]sdk.Msg{msg}, auth.StdFee{}, nil, "") appGenTxs = append(appGenTxs, appGenTx) } From e3c2157a9ec348190f32d1903e6fa019d9471c08 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Thu, 11 Oct 2018 06:52:33 -0700 Subject: [PATCH 13/83] Experimental branch --- cmd/gaia/app/genesis.go | 61 +++++++++++++++++++++++++++++++++++++++++ server/init.go | 18 +++++++++--- 2 files changed, 75 insertions(+), 4 deletions(-) diff --git a/cmd/gaia/app/genesis.go b/cmd/gaia/app/genesis.go index 0b6dd5ae8d22..f7fb626e452d 100644 --- a/cmd/gaia/app/genesis.go +++ b/cmd/gaia/app/genesis.go @@ -5,14 +5,18 @@ import ( "errors" "fmt" + "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/server" + "github.com/cosmos/cosmos-sdk/server/config" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth" distr "github.com/cosmos/cosmos-sdk/x/distribution" "github.com/cosmos/cosmos-sdk/x/gov" "github.com/cosmos/cosmos-sdk/x/slashing" "github.com/cosmos/cosmos-sdk/x/stake" + stakeTypes "github.com/cosmos/cosmos-sdk/x/stake/types" + "github.com/tendermint/tendermint/crypto" "github.com/spf13/pflag" ) @@ -66,10 +70,17 @@ func (ga *GenesisAccount) ToAccount() (acc *auth.BaseAccount) { // get app init parameters for server init command func GaiaAppInit() server.AppInit { fsAppGenState := pflag.NewFlagSet("", pflag.ContinueOnError) + fsAppGenTx := pflag.NewFlagSet("", pflag.ContinueOnError) + fsAppGenTx.String(server.FlagName, "", "validator moniker, required") + fsAppGenTx.String(server.FlagClientHome, DefaultCLIHome, + "home directory for the client, used for key generation") + fsAppGenTx.Bool(server.FlagOWK, false, "overwrite the accounts created") return server.AppInit{ FlagsAppGenState: fsAppGenState, + FlagsAppGenTx: fsAppGenTx, AppGenState: GaiaAppGenStateJSON, + AppGenTx: GaiaAppGenTx, } } @@ -80,6 +91,56 @@ type GaiaGenTx struct { PubKey string `json:"pub_key"` } +// GaiaAppGenTx generates a Gaia genesis transaction. +func GaiaAppGenTx( + cdc *codec.Codec, pk crypto.PubKey, genTxConfig config.GenTx, +) (appGenTx auth.StdTx, cliPrint json.RawMessage, err error) { + if genTxConfig.Name == "" { + err = errors.New("Must specify --name (validator moniker)") + return + } + + buf := client.BufferStdin() + prompt := fmt.Sprintf("Password for account '%s' (default %s):", genTxConfig.Name, DefaultKeyPass) + + keyPass, err := client.GetPassword(prompt, buf) + if err != nil && keyPass != "" { + // An error was returned that either failed to read the password from + // STDIN or the given password is not empty but failed to meet minimum + // length requirements. + return + } + + if keyPass == "" { + keyPass = DefaultKeyPass + } + + addr, secret, err := server.GenerateSaveCoinKey( + genTxConfig.CliRoot, + genTxConfig.Name, + keyPass, + genTxConfig.Overwrite, + ) + if err != nil { + return + } + + mm := map[string]string{"secret": secret} + bz, err := cdc.MarshalJSON(mm) + if err != nil { + return + } + + desc := stake.NewDescription(genTxConfig.Name, "", "", "") + comm := stakeTypes.CommissionMsg{} + msg := stake.NewMsgCreateValidator(sdk.ValAddress(addr), pk, sdk.NewInt64Coin("steak", 50), desc, comm) + appGenTx = auth.NewStdTx([]sdk.Msg{msg}, auth.StdFee{}, nil, "") + + cliPrint = json.RawMessage(bz) + + return +} + // Create the core parameters for genesis initialization for gaia // note that the pubkey input is this machines pubkey func GaiaAppGenState(cdc *codec.Codec, appGenTxs []json.RawMessage) (genesisState GenesisState, err error) { diff --git a/server/init.go b/server/init.go index f071bd91e5ea..d382cc674a01 100644 --- a/server/init.go +++ b/server/init.go @@ -10,19 +10,24 @@ import ( "github.com/pkg/errors" "github.com/spf13/pflag" + "github.com/tendermint/tendermint/crypto" dbm "github.com/tendermint/tendermint/libs/db" clkeys "github.com/cosmos/cosmos-sdk/client/keys" "github.com/cosmos/cosmos-sdk/codec" + serverconfig "github.com/cosmos/cosmos-sdk/server/config" sdk "github.com/cosmos/cosmos-sdk/types" ) // parameter names, init command const ( - FlagMoniker = "moniker" - FlagOverwrite = "overwrite" - FlagIP = "ip" - FlagChainID = "chain-id" + FlagMoniker = "moniker" + FlagOverwrite = "overwrite" + FlagIP = "ip" + FlagChainID = "chain-id" + FlagName = "name" + FlagClientHome = "home-client" + FlagOWK = "owk" ) // Storage for init command input parameters @@ -38,6 +43,11 @@ type AppInit struct { // flags required for application init functions FlagsAppGenState *pflag.FlagSet + FlagsAppGenTx *pflag.FlagSet + + // create the application genesis tx + AppGenTx func(cdc *codec.Codec, pk crypto.PubKey, genTxConfig serverconfig.GenTx) ( + appGenTx auth.StdTx, cliPrint json.RawMessage, err error) // AppGenState creates the core parameters initialization. It takes in a // pubkey meant to represent the pubkey of the validator of this machine. From 90aca4d5c2888db22a27372d4d3a66ce3c9983ba Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Thu, 11 Oct 2018 06:52:47 -0700 Subject: [PATCH 14/83] Modify gaiad init to allow auto-create of stdTx --- cmd/gaia/app/genesis.go | 6 +- server/init.go | 5 +- server/testnet.go | 198 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 204 insertions(+), 5 deletions(-) create mode 100644 server/testnet.go diff --git a/cmd/gaia/app/genesis.go b/cmd/gaia/app/genesis.go index f7fb626e452d..91d2d395a2d7 100644 --- a/cmd/gaia/app/genesis.go +++ b/cmd/gaia/app/genesis.go @@ -92,9 +92,9 @@ type GaiaGenTx struct { } // GaiaAppGenTx generates a Gaia genesis transaction. -func GaiaAppGenTx( - cdc *codec.Codec, pk crypto.PubKey, genTxConfig config.GenTx, -) (appGenTx auth.StdTx, cliPrint json.RawMessage, err error) { +func GaiaAppGenTx(cdc *codec.Codec, pk crypto.PubKey, genTxConfig config.GenTx) ( + appGenTx auth.StdTx, cliPrint json.RawMessage, err error) { + if genTxConfig.Name == "" { err = errors.New("Must specify --name (validator moniker)") return diff --git a/server/init.go b/server/init.go index d382cc674a01..20c03f3435ab 100644 --- a/server/init.go +++ b/server/init.go @@ -21,11 +21,11 @@ import ( // parameter names, init command const ( - FlagMoniker = "moniker" + FlagName = "name" FlagOverwrite = "overwrite" + FlagWithTxs = "with-txs" FlagIP = "ip" FlagChainID = "chain-id" - FlagName = "name" FlagClientHome = "home-client" FlagOWK = "owk" ) @@ -33,6 +33,7 @@ const ( // Storage for init command input parameters type InitConfig struct { ChainID string + GenTxs bool GenTxsDir string Moniker string Overwrite bool diff --git a/server/testnet.go b/server/testnet.go new file mode 100644 index 000000000000..a584718810b0 --- /dev/null +++ b/server/testnet.go @@ -0,0 +1,198 @@ +package server + +import ( + "fmt" + "net" + "os" + "path/filepath" + + "github.com/spf13/cobra" + + "github.com/cosmos/cosmos-sdk/codec" + gc "github.com/cosmos/cosmos-sdk/server/config" + "github.com/spf13/viper" + cfg "github.com/tendermint/tendermint/config" + cmn "github.com/tendermint/tendermint/libs/common" +) + +var ( + nodeDirPrefix = "node-dir-prefix" + nValidators = "v" + outputDir = "output-dir" + nodeDaemonHome = "node-daemon-home" + nodeCliHome = "node-cli-home" + + startingIPAddress = "starting-ip-address" +) + +const nodeDirPerm = 0755 + +// get cmd to initialize all files for tendermint testnet and application +func TestnetFilesCmd(ctx *Context, cdc *codec.Codec, appInit AppInit) *cobra.Command { + cmd := &cobra.Command{ + Use: "testnet", + Short: "Initialize files for a Gaiad testnet", + Long: `testnet will create "v" number of directories and populate each with +necessary files (private validator, genesis, config, etc.). + +Note, strict routability for addresses is turned off in the config file. + +Example: + + gaiad testnet --v 4 --o ./output --starting-ip-address 192.168.10.2 + `, + RunE: func(_ *cobra.Command, _ []string) error { + /* + config := ctx.Config + err := testnetWithConfig(config, cdc, appInit) + return err + */ + return nil + }, + } + cmd.Flags().Int(nValidators, 4, + "Number of validators to initialize the testnet with") + cmd.Flags().StringP(outputDir, "o", "./mytestnet", + "Directory to store initialization data for the testnet") + cmd.Flags().String(nodeDirPrefix, "node", + "Prefix the directory name for each node with (node results in node0, node1, ...)") + cmd.Flags().String(nodeDaemonHome, "gaiad", + "Home directory of the node's daemon configuration") + cmd.Flags().String(nodeCliHome, "gaiacli", + "Home directory of the node's cli configuration") + + cmd.Flags().String(startingIPAddress, "192.168.0.1", + "Starting IP address (192.168.0.1 results in persistent peers list ID0@192.168.0.1:46656, ID1@192.168.0.2:46656, ...)") + return cmd +} + +func testnetWithConfig(config *cfg.Config, cdc *codec.Codec, appInit AppInit) error { + outDir := viper.GetString(outputDir) + numValidators := viper.GetInt(nValidators) + + // Generate private key, node ID, initial transaction + for i := 0; i < numValidators; i++ { + nodeDirName := fmt.Sprintf("%s%d", viper.GetString(nodeDirPrefix), i) + nodeDaemonHomeName := viper.GetString(nodeDaemonHome) + nodeCliHomeName := viper.GetString(nodeCliHome) + nodeDir := filepath.Join(outDir, nodeDirName, nodeDaemonHomeName) + clientDir := filepath.Join(outDir, nodeDirName, nodeCliHomeName) + gentxsDir := filepath.Join(outDir, "gentxs") + config.SetRoot(nodeDir) + + err := os.MkdirAll(filepath.Join(nodeDir, "config"), nodeDirPerm) + if err != nil { + _ = os.RemoveAll(outDir) + return err + } + + err = os.MkdirAll(clientDir, nodeDirPerm) + if err != nil { + _ = os.RemoveAll(outDir) + return err + } + + config.Moniker = nodeDirName + ip, err := getIP(i) + if err != nil { + return err + } + + genTxConfig := gc.GenTx{ + nodeDirName, + clientDir, + true, + ip, + } + + // Run `init gen-tx` and generate initial transactions + cliPrint, genTxFile, err := gentxWithConfig(cdc, appInit, config, genTxConfig) + if err != nil { + return err + } + + // Save private key seed words + name := fmt.Sprintf("%v.json", "key_seed") + err = writeFile(name, clientDir, cliPrint) + if err != nil { + return err + } + + // Gather gentxs folder + name = fmt.Sprintf("%v.json", nodeDirName) + err = writeFile(name, gentxsDir, genTxFile) + if err != nil { + return err + } + } + + // Generate genesis.json and config.toml + chainID := "chain-" + cmn.RandStr(6) + for i := 0; i < numValidators; i++ { + + nodeDirName := fmt.Sprintf("%s%d", viper.GetString(nodeDirPrefix), i) + nodeDaemonHomeName := viper.GetString(nodeDaemonHome) + nodeDir := filepath.Join(outDir, nodeDirName, nodeDaemonHomeName) + gentxsDir := filepath.Join(outDir, "gentxs") + initConfig := InitConfig{ + chainID, + true, + gentxsDir, + nodeDirName, + true, + } + config.Moniker = nodeDirName + config.SetRoot(nodeDir) + + // Run `init` and generate genesis.json and config.toml + _, _, err := initWithConfig(cdc, appInit, config, initConfig) + if err != nil { + return err + } + } + + fmt.Printf("Successfully initialized %v node directories\n", viper.GetInt(nValidators)) + return nil +} + +func getIP(i int) (ip string, err error) { + ip = viper.GetString(startingIPAddress) + if len(ip) == 0 { + ip, err = ExternalIP() + if err != nil { + return "", err + } + } else { + ip, err = calculateIP(ip, i) + if err != nil { + return "", err + } + } + return ip, nil +} + +func writeFile(name string, dir string, contents []byte) error { + writePath := filepath.Join(dir) + file := filepath.Join(writePath, name) + err := cmn.EnsureDir(writePath, 0700) + if err != nil { + return err + } + err = cmn.WriteFile(file, contents, 0600) + if err != nil { + return err + } + return nil +} + +func calculateIP(ip string, i int) (string, error) { + ipv4 := net.ParseIP(ip).To4() + if ipv4 == nil { + return "", fmt.Errorf("%v: non ipv4 address", ip) + } + + for j := 0; j < i; j++ { + ipv4[3]++ + } + return ipv4.String(), nil +} From 73391ac6c2a6ef0e71e745c00c5745fb64997205 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Sun, 7 Oct 2018 17:28:22 +0100 Subject: [PATCH 15/83] Fix FTBFS in democoin --- examples/democoin/cmd/democoind/main.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/democoin/cmd/democoind/main.go b/examples/democoin/cmd/democoind/main.go index 7f8c7c54d17f..692e4074ff08 100644 --- a/examples/democoin/cmd/democoind/main.go +++ b/examples/democoin/cmd/democoind/main.go @@ -17,16 +17,16 @@ import ( "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/examples/democoin/app" "github.com/cosmos/cosmos-sdk/server" + auth "github.com/cosmos/cosmos-sdk/x/auth" ) // init parameters var CoolAppInit = server.AppInit{ AppGenState: CoolAppGenState, - AppGenTx: server.SimpleAppGenTx, } // coolGenAppParams sets up the app_state and appends the cool app state -func CoolAppGenState(cdc *codec.Codec, appGenTxs []json.RawMessage) (appState json.RawMessage, err error) { +func CoolAppGenState(cdc *codec.Codec, appGenTxs []auth.StdTx) (appState json.RawMessage, err error) { appState, err = server.SimpleAppGenState(cdc, appGenTxs) if err != nil { return From 7663114efbf867dd18ee675b5d0bdf94e0bcda30 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Thu, 11 Oct 2018 06:54:03 -0700 Subject: [PATCH 16/83] Refactoring, fix test cases --- cmd/gaia/app/genesis.go | 58 +------------------------ examples/democoin/cmd/democoind/main.go | 1 + server/export_test.go | 53 ++++++++++++++++++++++ server/init.go | 4 +- server/init_test.go | 47 ++++++++++++++++++++ server/mock/app.go | 19 ++------ server/start_test.go | 52 ++++++++++++++++++++++ server/test_helpers.go | 2 + 8 files changed, 162 insertions(+), 74 deletions(-) create mode 100644 server/export_test.go create mode 100644 server/init_test.go create mode 100644 server/start_test.go diff --git a/cmd/gaia/app/genesis.go b/cmd/gaia/app/genesis.go index 91d2d395a2d7..bbe6b5675c87 100644 --- a/cmd/gaia/app/genesis.go +++ b/cmd/gaia/app/genesis.go @@ -5,10 +5,8 @@ import ( "errors" "fmt" - "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/server" - "github.com/cosmos/cosmos-sdk/server/config" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth" distr "github.com/cosmos/cosmos-sdk/x/distribution" @@ -16,14 +14,10 @@ import ( "github.com/cosmos/cosmos-sdk/x/slashing" "github.com/cosmos/cosmos-sdk/x/stake" stakeTypes "github.com/cosmos/cosmos-sdk/x/stake/types" - "github.com/tendermint/tendermint/crypto" "github.com/spf13/pflag" ) -// DefaultKeyPass contains the default key password for genesis transactions -const DefaultKeyPass = "12345678" - var ( // bonded tokens given to genesis validators/accounts freeFermionVal = int64(100) @@ -80,7 +74,7 @@ func GaiaAppInit() server.AppInit { FlagsAppGenState: fsAppGenState, FlagsAppGenTx: fsAppGenTx, AppGenState: GaiaAppGenStateJSON, - AppGenTx: GaiaAppGenTx, + AppGenTx: server.SimpleAppGenTx, } } @@ -91,56 +85,6 @@ type GaiaGenTx struct { PubKey string `json:"pub_key"` } -// GaiaAppGenTx generates a Gaia genesis transaction. -func GaiaAppGenTx(cdc *codec.Codec, pk crypto.PubKey, genTxConfig config.GenTx) ( - appGenTx auth.StdTx, cliPrint json.RawMessage, err error) { - - if genTxConfig.Name == "" { - err = errors.New("Must specify --name (validator moniker)") - return - } - - buf := client.BufferStdin() - prompt := fmt.Sprintf("Password for account '%s' (default %s):", genTxConfig.Name, DefaultKeyPass) - - keyPass, err := client.GetPassword(prompt, buf) - if err != nil && keyPass != "" { - // An error was returned that either failed to read the password from - // STDIN or the given password is not empty but failed to meet minimum - // length requirements. - return - } - - if keyPass == "" { - keyPass = DefaultKeyPass - } - - addr, secret, err := server.GenerateSaveCoinKey( - genTxConfig.CliRoot, - genTxConfig.Name, - keyPass, - genTxConfig.Overwrite, - ) - if err != nil { - return - } - - mm := map[string]string{"secret": secret} - bz, err := cdc.MarshalJSON(mm) - if err != nil { - return - } - - desc := stake.NewDescription(genTxConfig.Name, "", "", "") - comm := stakeTypes.CommissionMsg{} - msg := stake.NewMsgCreateValidator(sdk.ValAddress(addr), pk, sdk.NewInt64Coin("steak", 50), desc, comm) - appGenTx = auth.NewStdTx([]sdk.Msg{msg}, auth.StdFee{}, nil, "") - - cliPrint = json.RawMessage(bz) - - return -} - // Create the core parameters for genesis initialization for gaia // note that the pubkey input is this machines pubkey func GaiaAppGenState(cdc *codec.Codec, appGenTxs []json.RawMessage) (genesisState GenesisState, err error) { diff --git a/examples/democoin/cmd/democoind/main.go b/examples/democoin/cmd/democoind/main.go index 692e4074ff08..b359c9e62aae 100644 --- a/examples/democoin/cmd/democoind/main.go +++ b/examples/democoin/cmd/democoind/main.go @@ -23,6 +23,7 @@ import ( // init parameters var CoolAppInit = server.AppInit{ AppGenState: CoolAppGenState, + AppGenTx: server.SimpleAppGenTx, } // coolGenAppParams sets up the app_state and appends the cool app state diff --git a/server/export_test.go b/server/export_test.go new file mode 100644 index 000000000000..b647a72d5099 --- /dev/null +++ b/server/export_test.go @@ -0,0 +1,53 @@ +package server + +import ( + "bytes" + "io" + "os" + "testing" + + "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/server/mock" + "github.com/stretchr/testify/require" + tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands" + "github.com/tendermint/tendermint/libs/log" +) + +func TestEmptyState(t *testing.T) { + defer setupViper(t)() + logger := log.NewNopLogger() + cfg, err := tcmd.ParseConfig() + require.Nil(t, err) + ctx := NewContext(cfg, logger) + cdc := codec.New() + appInit := AppInit{ + AppGenTx: SimpleAppGenTx, + AppGenState: mock.AppGenStateEmpty, + } + cmd := InitCmd(ctx, cdc, appInit) + err = cmd.RunE(nil, nil) + require.NoError(t, err) + + old := os.Stdout + r, w, _ := os.Pipe() + os.Stdout = w + cmd = ExportCmd(ctx, cdc, nil) + err = cmd.RunE(nil, nil) + require.NoError(t, err) + + outC := make(chan string) + go func() { + var buf bytes.Buffer + io.Copy(&buf, r) + outC <- buf.String() + }() + + w.Close() + os.Stdout = old + out := <-outC + require.Contains(t, out, "WARNING: State is not initialized") + require.Contains(t, out, "genesis_time") + require.Contains(t, out, "chain_id") + require.Contains(t, out, "consensus_params") + require.Contains(t, out, "app_hash") +} diff --git a/server/init.go b/server/init.go index 20c03f3435ab..aaf68a8bc59f 100644 --- a/server/init.go +++ b/server/init.go @@ -30,6 +30,9 @@ const ( FlagOWK = "owk" ) +// DefaultKeyPass contains the default key password for genesis transactions +const DefaultKeyPass = "12345678" + // Storage for init command input parameters type InitConfig struct { ChainID string @@ -59,7 +62,6 @@ type AppInit struct { // simple default application init var DefaultAppInit = AppInit{ - // AppGenTx: SimpleAppGenTx, AppGenState: SimpleAppGenState, } diff --git a/server/init_test.go b/server/init_test.go new file mode 100644 index 000000000000..ca8d47d72ce0 --- /dev/null +++ b/server/init_test.go @@ -0,0 +1,47 @@ +package server + +import ( + "testing" + + "github.com/stretchr/testify/require" + + "github.com/tendermint/tendermint/libs/log" + + "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/server/mock" + tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands" +) + +// TODO update +func TestInitCmd(t *testing.T) { + defer setupViper(t)() + + logger := log.NewNopLogger() + cfg, err := tcmd.ParseConfig() + require.Nil(t, err) + ctx := NewContext(cfg, logger) + cdc := codec.New() + appInit := AppInit{ + AppGenState: mock.AppGenState, + AppGenTx: SimpleAppGenTx, + } + cmd := InitCmd(ctx, cdc, appInit) + err = cmd.RunE(nil, nil) + require.NoError(t, err) +} + +func TestGenTxCmd(t *testing.T) { + // TODO +} + +func TestTestnetFilesCmd(t *testing.T) { + // TODO +} + +func TestSimpleAppGenTx(t *testing.T) { + // TODO +} + +func TestSimpleAppGenState(t *testing.T) { + // TODO +} diff --git a/server/mock/app.go b/server/mock/app.go index abdec6be5081..74dc0b01c4d4 100644 --- a/server/mock/app.go +++ b/server/mock/app.go @@ -6,15 +6,13 @@ import ( "path/filepath" abci "github.com/tendermint/tendermint/abci/types" - "github.com/tendermint/tendermint/crypto" dbm "github.com/tendermint/tendermint/libs/db" "github.com/tendermint/tendermint/libs/log" - tmtypes "github.com/tendermint/tendermint/types" bam "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/codec" - gc "github.com/cosmos/cosmos-sdk/server/config" sdk "github.com/cosmos/cosmos-sdk/types" + auth "github.com/cosmos/cosmos-sdk/x/auth" ) // NewApp creates a simple mock kvstore app for testing. It should work @@ -105,7 +103,7 @@ 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, _ []auth.StdTx) (appState json.RawMessage, err error) { appState = json.RawMessage(`{ "values": [ { @@ -122,18 +120,7 @@ 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, _ []auth.StdTx) (appState json.RawMessage, err error) { appState = json.RawMessage(``) return } - -// Return a validator, not much else -func AppGenTx(_ *codec.Codec, pk crypto.PubKey, genTxConfig gc.GenTx) ( - appGenTx, cliPrint json.RawMessage, validator tmtypes.GenesisValidator, err error) { - - validator = tmtypes.GenesisValidator{ - PubKey: pk, - Power: 10, - } - return -} diff --git a/server/start_test.go b/server/start_test.go new file mode 100644 index 000000000000..3fc795a02f35 --- /dev/null +++ b/server/start_test.go @@ -0,0 +1,52 @@ +package server + +import ( + "io/ioutil" + "os" + "testing" + "time" + + "github.com/stretchr/testify/require" + + "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/server/mock" + "github.com/tendermint/tendermint/abci/server" + tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands" + "github.com/tendermint/tendermint/libs/log" +) + +func TestStartStandAlone(t *testing.T) { + home, err := ioutil.TempDir("", "mock-sdk-cmd") + require.Nil(t, err) + defer func() { + os.RemoveAll(home) + }() + + logger := log.NewNopLogger() + cfg, err := tcmd.ParseConfig() + require.Nil(t, err) + ctx := NewContext(cfg, logger) + cdc := codec.New() + appInit := AppInit{ + AppGenState: mock.AppGenState, + AppGenTx: SimpleAppGenTx, + } + initCmd := InitCmd(ctx, cdc, appInit) + err = initCmd.RunE(nil, nil) + require.NoError(t, err) + + app, err := mock.NewApp(home, logger) + require.Nil(t, err) + svrAddr, _, err := FreeTCPAddr() + require.Nil(t, err) + svr, err := server.NewServer(svrAddr, "socket", app) + require.Nil(t, err, "error creating listener") + svr.SetLogger(logger.With("module", "abci-server")) + svr.Start() + + timer := time.NewTimer(time.Duration(2) * time.Second) + select { + case <-timer.C: + svr.Stop() + } +} diff --git a/server/test_helpers.go b/server/test_helpers.go index 8de164e08ae4..69edd5c6a4b6 100644 --- a/server/test_helpers.go +++ b/server/test_helpers.go @@ -42,6 +42,8 @@ func SetupViper(t *testing.T) func() { rootDir, err := ioutil.TempDir("", "mock-sdk-cmd") require.Nil(t, err) viper.Set(cli.HomeFlag, rootDir) + viper.Set(FlagName, "moniker") + viper.Set(FlagOWK, true) return func() { err := os.RemoveAll(rootDir) if err != nil { From 5f654e72c2c3078b1e1d66c916032c2f43ac4314 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Mon, 8 Oct 2018 10:19:24 +0100 Subject: [PATCH 17/83] Fix genesis tests --- cmd/gaia/app/genesis_test.go | 38 +++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/cmd/gaia/app/genesis_test.go b/cmd/gaia/app/genesis_test.go index 9cbd1f49fc70..a8776e8fef2e 100644 --- a/cmd/gaia/app/genesis_test.go +++ b/cmd/gaia/app/genesis_test.go @@ -25,25 +25,29 @@ var ( emptyPubkey crypto.PubKey ) -func makeGenesisState(genTxs []GaiaGenTx) GenesisState { +func makeGenesisState(t *testing.T, genTxs []auth.StdTx) GenesisState { // start with the default staking genesis state stakeData := stake.DefaultGenesisState() + genAccs := make([]GenesisAccount, len(genTxs)) - // get genesis flag account information - genaccs := make([]GenesisAccount, len(genTxs)) for i, genTx := range genTxs { - genaccs[i] = genesisAccountFromGenTx(genTx) + msgs := genTx.GetMsgs() + require.Equal(t, 1, len(msgs)) + msg := msgs[0].(stake.MsgCreateValidator) + + // get genesis flag account information + genAccs[i] = genesisAccountFromMsgCreateValidator(msg) stakeData.Pool.LooseTokens = stakeData.Pool.LooseTokens.Add(sdk.NewDecFromInt(freeFermionsAcc)) // increase the supply // add the validator - if len(genTx.Name) > 0 { - stakeData = addValidatorToStakeData(genTx, stakeData) + if len(msg.Name()) > 0 { + stakeData = addValidatorToStakeData(msg, stakeData) } } // create the final app state return GenesisState{ - Accounts: genaccs, + Accounts: genAccs, StakeData: stakeData, GovData: gov.DefaultGenesisState(), } @@ -75,17 +79,23 @@ func TestGaiaAppGenState(t *testing.T) { // TODO correct: genesis account created, canididates created, pool token variance } +func makeMsg(name string, pk crypto.PubKey) auth.StdTx { + desc := stake.NewDescription(name, "", "", "") + comm := stakeTypes.CommissionMsg{} + msg := stake.NewMsgCreateValidator(sdk.ValAddress(pk.Address()), pk, sdk.NewInt64Coin("steak", 50), desc, comm) + return auth.NewStdTx([]sdk.Msg{msg}, auth.StdFee{}, nil, "") +} + func TestGaiaGenesisValidation(t *testing.T) { - genTxs := make([]GaiaGenTx, 2) - addr := pk1.Address() + genTxs := make([]auth.StdTx, 2) // Test duplicate accounts fails - genTxs[0] = GaiaGenTx{"", sdk.AccAddress(addr), ""} - genTxs[1] = GaiaGenTx{"", sdk.AccAddress(addr), ""} - genesisState := makeGenesisState(genTxs) + genTxs[0] = makeMsg("test-0", pk1) + genTxs[1] = makeMsg("test-1", pk1) + genesisState := makeGenesisState(t, genTxs) err := GaiaValidateGenesisState(genesisState) require.NotNil(t, err) // Test bonded + jailed validator fails - genesisState = makeGenesisState(genTxs[:1]) + genesisState = makeGenesisState(t, genTxs) val1 := stakeTypes.NewValidator(addr1, pk1, stakeTypes.Description{Moniker: "test #2"}) val1.Jailed = true val1.Status = sdk.Bonded @@ -94,7 +104,7 @@ func TestGaiaGenesisValidation(t *testing.T) { require.NotNil(t, err) // Test duplicate validator fails val1.Jailed = false - genesisState = makeGenesisState(genTxs[:1]) + genesisState = makeGenesisState(t, genTxs) val2 := stakeTypes.NewValidator(addr1, pk1, stakeTypes.Description{Moniker: "test #3"}) genesisState.StakeData.Validators = append(genesisState.StakeData.Validators, val1) genesisState.StakeData.Validators = append(genesisState.StakeData.Validators, val2) From 7a7120b53102979951f421dfe7485db0a6a20629 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Thu, 11 Oct 2018 07:31:00 -0700 Subject: [PATCH 18/83] Remove server/testnet.go --- server/testnet.go | 198 ---------------------------------------------- 1 file changed, 198 deletions(-) delete mode 100644 server/testnet.go diff --git a/server/testnet.go b/server/testnet.go deleted file mode 100644 index a584718810b0..000000000000 --- a/server/testnet.go +++ /dev/null @@ -1,198 +0,0 @@ -package server - -import ( - "fmt" - "net" - "os" - "path/filepath" - - "github.com/spf13/cobra" - - "github.com/cosmos/cosmos-sdk/codec" - gc "github.com/cosmos/cosmos-sdk/server/config" - "github.com/spf13/viper" - cfg "github.com/tendermint/tendermint/config" - cmn "github.com/tendermint/tendermint/libs/common" -) - -var ( - nodeDirPrefix = "node-dir-prefix" - nValidators = "v" - outputDir = "output-dir" - nodeDaemonHome = "node-daemon-home" - nodeCliHome = "node-cli-home" - - startingIPAddress = "starting-ip-address" -) - -const nodeDirPerm = 0755 - -// get cmd to initialize all files for tendermint testnet and application -func TestnetFilesCmd(ctx *Context, cdc *codec.Codec, appInit AppInit) *cobra.Command { - cmd := &cobra.Command{ - Use: "testnet", - Short: "Initialize files for a Gaiad testnet", - Long: `testnet will create "v" number of directories and populate each with -necessary files (private validator, genesis, config, etc.). - -Note, strict routability for addresses is turned off in the config file. - -Example: - - gaiad testnet --v 4 --o ./output --starting-ip-address 192.168.10.2 - `, - RunE: func(_ *cobra.Command, _ []string) error { - /* - config := ctx.Config - err := testnetWithConfig(config, cdc, appInit) - return err - */ - return nil - }, - } - cmd.Flags().Int(nValidators, 4, - "Number of validators to initialize the testnet with") - cmd.Flags().StringP(outputDir, "o", "./mytestnet", - "Directory to store initialization data for the testnet") - cmd.Flags().String(nodeDirPrefix, "node", - "Prefix the directory name for each node with (node results in node0, node1, ...)") - cmd.Flags().String(nodeDaemonHome, "gaiad", - "Home directory of the node's daemon configuration") - cmd.Flags().String(nodeCliHome, "gaiacli", - "Home directory of the node's cli configuration") - - cmd.Flags().String(startingIPAddress, "192.168.0.1", - "Starting IP address (192.168.0.1 results in persistent peers list ID0@192.168.0.1:46656, ID1@192.168.0.2:46656, ...)") - return cmd -} - -func testnetWithConfig(config *cfg.Config, cdc *codec.Codec, appInit AppInit) error { - outDir := viper.GetString(outputDir) - numValidators := viper.GetInt(nValidators) - - // Generate private key, node ID, initial transaction - for i := 0; i < numValidators; i++ { - nodeDirName := fmt.Sprintf("%s%d", viper.GetString(nodeDirPrefix), i) - nodeDaemonHomeName := viper.GetString(nodeDaemonHome) - nodeCliHomeName := viper.GetString(nodeCliHome) - nodeDir := filepath.Join(outDir, nodeDirName, nodeDaemonHomeName) - clientDir := filepath.Join(outDir, nodeDirName, nodeCliHomeName) - gentxsDir := filepath.Join(outDir, "gentxs") - config.SetRoot(nodeDir) - - err := os.MkdirAll(filepath.Join(nodeDir, "config"), nodeDirPerm) - if err != nil { - _ = os.RemoveAll(outDir) - return err - } - - err = os.MkdirAll(clientDir, nodeDirPerm) - if err != nil { - _ = os.RemoveAll(outDir) - return err - } - - config.Moniker = nodeDirName - ip, err := getIP(i) - if err != nil { - return err - } - - genTxConfig := gc.GenTx{ - nodeDirName, - clientDir, - true, - ip, - } - - // Run `init gen-tx` and generate initial transactions - cliPrint, genTxFile, err := gentxWithConfig(cdc, appInit, config, genTxConfig) - if err != nil { - return err - } - - // Save private key seed words - name := fmt.Sprintf("%v.json", "key_seed") - err = writeFile(name, clientDir, cliPrint) - if err != nil { - return err - } - - // Gather gentxs folder - name = fmt.Sprintf("%v.json", nodeDirName) - err = writeFile(name, gentxsDir, genTxFile) - if err != nil { - return err - } - } - - // Generate genesis.json and config.toml - chainID := "chain-" + cmn.RandStr(6) - for i := 0; i < numValidators; i++ { - - nodeDirName := fmt.Sprintf("%s%d", viper.GetString(nodeDirPrefix), i) - nodeDaemonHomeName := viper.GetString(nodeDaemonHome) - nodeDir := filepath.Join(outDir, nodeDirName, nodeDaemonHomeName) - gentxsDir := filepath.Join(outDir, "gentxs") - initConfig := InitConfig{ - chainID, - true, - gentxsDir, - nodeDirName, - true, - } - config.Moniker = nodeDirName - config.SetRoot(nodeDir) - - // Run `init` and generate genesis.json and config.toml - _, _, err := initWithConfig(cdc, appInit, config, initConfig) - if err != nil { - return err - } - } - - fmt.Printf("Successfully initialized %v node directories\n", viper.GetInt(nValidators)) - return nil -} - -func getIP(i int) (ip string, err error) { - ip = viper.GetString(startingIPAddress) - if len(ip) == 0 { - ip, err = ExternalIP() - if err != nil { - return "", err - } - } else { - ip, err = calculateIP(ip, i) - if err != nil { - return "", err - } - } - return ip, nil -} - -func writeFile(name string, dir string, contents []byte) error { - writePath := filepath.Join(dir) - file := filepath.Join(writePath, name) - err := cmn.EnsureDir(writePath, 0700) - if err != nil { - return err - } - err = cmn.WriteFile(file, contents, 0600) - if err != nil { - return err - } - return nil -} - -func calculateIP(ip string, i int) (string, error) { - ipv4 := net.ParseIP(ip).To4() - if ipv4 == nil { - return "", fmt.Errorf("%v: non ipv4 address", ip) - } - - for j := 0; j < i; j++ { - ipv4[3]++ - } - return ipv4.String(), nil -} From a0b936a7e821af5a96e01e2963614186e54426e6 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Thu, 11 Oct 2018 09:59:40 -0700 Subject: [PATCH 19/83] Don't load node_key.json, which might not be available --- x/stake/client/cli/tx.go | 33 ++++++++------------------------- 1 file changed, 8 insertions(+), 25 deletions(-) diff --git a/x/stake/client/cli/tx.go b/x/stake/client/cli/tx.go index 1e1d3515f0c1..ffb283f06e2c 100644 --- a/x/stake/client/cli/tx.go +++ b/x/stake/client/cli/tx.go @@ -7,23 +7,15 @@ import ( "github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/client/utils" "github.com/cosmos/cosmos-sdk/codec" - "github.com/cosmos/cosmos-sdk/server" sdk "github.com/cosmos/cosmos-sdk/types" authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli" authtxb "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder" "github.com/cosmos/cosmos-sdk/x/stake" - "github.com/tendermint/tendermint/p2p" - "github.com/spf13/cobra" "github.com/spf13/viper" ) -const ( - flagGenesisFormat = "genesis-format" - flagIP = "ip" - flagNodeKeyFile = "node-key-file" -) // GetCmdCreateValidator implements the create validator command handler. func GetCmdCreateValidator(cdc *codec.Codec) *cobra.Command { @@ -96,21 +88,12 @@ func GetCmdCreateValidator(cdc *codec.Codec) *cobra.Command { ) } - if viper.GetBool(flagGenesisFormat) { - ip := viper.GetString(flagIP) - if len(ip) == 0 { - eip, err := server.ExternalIP() - if err != nil { - return err - } - ip = eip - } - - nodeKey, err := p2p.LoadOrGenNodeKey(viper.GetString(flagNodeKeyFile)) - if err != nil { - return err + if viper.GetBool(FlagGenesisFormat) { + ip := viper.GetString(FlagIP) + nodeID := viper.GetString(FlagNodeID) + if nodeID != "" && ip != "" { + txBldr = txBldr.WithMemo(fmt.Sprintf("%s@%s:26656", nodeID, ip)) } - txBldr = txBldr.WithMemo(fmt.Sprintf("%s@%s:26656", string(nodeKey.ID()), ip)) return utils.PrintUnsignedStdTx(txBldr, cliCtx, []sdk.Msg{msg}, true) } @@ -129,9 +112,9 @@ func GetCmdCreateValidator(cdc *codec.Codec) *cobra.Command { cmd.Flags().AddFlagSet(fsDescriptionCreate) cmd.Flags().AddFlagSet(fsCommissionCreate) cmd.Flags().AddFlagSet(fsDelegator) - cmd.Flags().Bool(flagGenesisFormat, false, "Export the transaction in gen-tx format; it implies --generate-only") - cmd.Flags().String(flagIP, "", fmt.Sprintf("External IP to use as node's public address; if left blank IP will be retrieved from this machine. It takes effect only when used in combination with --%s", flagGenesisFormat)) - cmd.Flags().String(flagNodeKeyFile, "", "Path to to node_key.json") + cmd.Flags().Bool(FlagGenesisFormat, false, "Export the transaction in gen-tx format; it implies --generate-only") + cmd.Flags().String(FlagIP, "", fmt.Sprintf("Node's public IP. It takes effect only when used in combination with --%s", FlagGenesisFormat)) + cmd.Flags().String(FlagNodeID, "", "Node's ID") return cmd } From d2b39fabca3140938f4be42734596099733d5ed4 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Thu, 11 Oct 2018 10:21:55 -0700 Subject: [PATCH 20/83] Add missing trailing new space --- client/utils/utils.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/utils/utils.go b/client/utils/utils.go index 364b9e692fb6..76894ebce861 100644 --- a/client/utils/utils.go +++ b/client/utils/utils.go @@ -122,7 +122,7 @@ func SignStdTx(txBldr authtxb.TxBuilder, cliCtx context.CLIContext, name string, // Check whether the address is a signer if !isTxSigner(sdk.AccAddress(addr), stdTx.GetSigners()) { - fmt.Fprintf(os.Stderr, "WARNING: The generated transaction's intended signer does not match the given signer: '%v'", name) + fmt.Fprintf(os.Stderr, "WARNING: The generated transaction's intended signer does not match the given signer: '%v'\n", name) } if txBldr.AccountNumber == 0 { From b5e709fbfc833a2d6fc8af1503084c8b07bdbfe4 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Wed, 17 Oct 2018 14:53:05 -0700 Subject: [PATCH 21/83] WIP! --- client/lcd/test_helpers.go | 7 +- client/utils/utils.go | 6 +- cmd/gaia/app/app.go | 16 +++ cmd/gaia/app/genesis.go | 105 +++++++++++--- cmd/gaia/app/test_utils.go | 24 ++-- cmd/gaia/cmd/gaiad/main.go | 2 +- cmd/gaia/init/init.go | 243 +++++++-------------------------- cmd/gaia/init/testnet.go | 263 ++++++++++++++++++------------------ server/init.go | 11 +- x/auth/client/cli/sign.go | 4 +- x/stake/client/cli/flags.go | 4 + 11 files changed, 312 insertions(+), 373 deletions(-) diff --git a/client/lcd/test_helpers.go b/client/lcd/test_helpers.go index f390727f3b47..399e0b4b62fb 100644 --- a/client/lcd/test_helpers.go +++ b/client/lcd/test_helpers.go @@ -2,6 +2,7 @@ package lcd import ( "bytes" + "encoding/json" "fmt" "io/ioutil" "net" @@ -225,13 +226,13 @@ func InitializeTestLCD( ) } - var validatorsPKs []crypto.PubKey + //var validatorsPKs []crypto.PubKey // NOTE: It's bad practice to reuse public key address for the operator // address but doing in the test for simplicity. var appGenTxs []auth.StdTx for _, gdValidator := range genDoc.Validators { - operAddr := ed25519.GenPrivKey().PubKey().Address() + //operAddr := ed25519.GenPrivKey().PubKey().Address() pk := gdValidator.PubKey desc := stake.NewDescription("test_val1", "", "", "") @@ -240,7 +241,7 @@ func InitializeTestLCD( appGenTxs = append(appGenTxs, appGenTx) } - genesisState, err := gapp.NewTestGaiaAppGenState(cdc, appGenTxs[:], genDoc.Validators, valOperAddrs) + genesisState, err := gapp.NewTestGaiaAppGenState(cdc, []json.RawMessage{}, genDoc.Validators, valOperAddrs) require.NoError(t, err) // add some tokens to init accounts diff --git a/client/utils/utils.go b/client/utils/utils.go index 76894ebce861..a6287991dcdd 100644 --- a/client/utils/utils.go +++ b/client/utils/utils.go @@ -107,7 +107,7 @@ func PrintUnsignedStdTx(txBldr authtxb.TxBuilder, cliCtx context.CLIContext, msg // SignStdTx appends a signature to a StdTx and returns a copy of a it. If appendSig // is false, it replaces the signatures already attached with the new signature. -func SignStdTx(txBldr authtxb.TxBuilder, cliCtx context.CLIContext, name string, stdTx auth.StdTx, appendSig bool) (auth.StdTx, error) { +func SignStdTx(txBldr authtxb.TxBuilder, cliCtx context.CLIContext, name string, stdTx auth.StdTx, appendSig bool, offline bool) (auth.StdTx, error) { var signedStdTx auth.StdTx keybase, err := keys.GetKeyBase() @@ -125,7 +125,7 @@ func SignStdTx(txBldr authtxb.TxBuilder, cliCtx context.CLIContext, name string, fmt.Fprintf(os.Stderr, "WARNING: The generated transaction's intended signer does not match the given signer: '%v'\n", name) } - if txBldr.AccountNumber == 0 { + if !offline && txBldr.AccountNumber == 0 { accNum, err := cliCtx.GetAccountNumber(addr) if err != nil { return signedStdTx, err @@ -133,7 +133,7 @@ func SignStdTx(txBldr authtxb.TxBuilder, cliCtx context.CLIContext, name string, txBldr = txBldr.WithAccountNumber(accNum) } - if txBldr.Sequence == 0 { + if !offline && txBldr.Sequence == 0 { accSeq, err := cliCtx.GetAccountSequence(addr) if err != nil { return signedStdTx, err diff --git a/cmd/gaia/app/app.go b/cmd/gaia/app/app.go index 2c72e3aaf931..2598973567be 100644 --- a/cmd/gaia/app/app.go +++ b/cmd/gaia/app/app.go @@ -238,6 +238,22 @@ func (app *GaiaApp) initChainer(ctx sdk.Context, req abci.RequestInitChain) abci panic(err) // TODO find a way to do this w/o panics } + if len(genesisState.Txs) > 0 { + for _, genTx := range genesisState.Txs { + var tx auth.StdTx + err = app.cdc.UnmarshalJSON(genTx, &tx) + if err != nil { + panic(err) + } + res := app.BaseApp.Deliver(tx) + if !res.IsOK() { + panic(res.Log) + } + } + + validators = app.stakeKeeper.ApplyAndReturnValidatorSetUpdates(ctx) + } + return abci.ResponseInitChain{ Validators: validators, } diff --git a/cmd/gaia/app/genesis.go b/cmd/gaia/app/genesis.go index bbe6b5675c87..332b35eaeecc 100644 --- a/cmd/gaia/app/genesis.go +++ b/cmd/gaia/app/genesis.go @@ -4,6 +4,11 @@ import ( "encoding/json" "errors" "fmt" + "io/ioutil" + "os" + "path" + "sort" + "strings" "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/server" @@ -13,9 +18,9 @@ import ( "github.com/cosmos/cosmos-sdk/x/gov" "github.com/cosmos/cosmos-sdk/x/slashing" "github.com/cosmos/cosmos-sdk/x/stake" - stakeTypes "github.com/cosmos/cosmos-sdk/x/stake/types" - "github.com/spf13/pflag" + + tmtypes "github.com/tendermint/tendermint/types" ) var ( @@ -31,6 +36,7 @@ type GenesisState struct { DistrData distr.GenesisState `json:"distr"` GovData gov.GenesisState `json:"gov"` SlashingData slashing.GenesisState `json:"slashing"` + Txs []json.RawMessage `json:"txs"` } // GenesisAccount doesn't need pubkey or sequence @@ -74,17 +80,10 @@ func GaiaAppInit() server.AppInit { FlagsAppGenState: fsAppGenState, FlagsAppGenTx: fsAppGenTx, AppGenState: GaiaAppGenStateJSON, - AppGenTx: server.SimpleAppGenTx, + //AppGenTx: server.SimpleAppGenTx, } } -// simple genesis tx -type GaiaGenTx struct { - Name string `json:"name"` - Address sdk.AccAddress `json:"address"` - PubKey string `json:"pub_key"` -} - // Create the core parameters for genesis initialization for gaia // note that the pubkey input is this machines pubkey func GaiaAppGenState(cdc *codec.Codec, appGenTxs []json.RawMessage) (genesisState GenesisState, err error) { @@ -100,13 +99,13 @@ func GaiaAppGenState(cdc *codec.Codec, appGenTxs []json.RawMessage) (genesisStat // get genesis flag account information genaccs := make([]GenesisAccount, len(appGenTxs)) - for i, jsonRawMessage := range appGenTxs { - var genTx auth.StdTx - err = cdc.UnmarshalJSON(jsonRawMessage, &genTx) + for i, genTx := range appGenTxs { + var tx auth.StdTx + err = cdc.UnmarshalJSON(genTx, &tx) if err != nil { return } - msgs := genTx.GetMsgs() + msgs := tx.GetMsgs() if len(msgs) == 0 { err = errors.New("must provide at least genesis message") return @@ -118,9 +117,9 @@ func GaiaAppGenState(cdc *codec.Codec, appGenTxs []json.RawMessage) (genesisStat stakeData.Pool.LooseTokens = stakeData.Pool.LooseTokens.Add(sdk.NewDecFromInt(freeFermionsAcc)) // increase the supply // add the validator - if len(msg.Description.Moniker) > 0 { - stakeData = addValidatorToStakeData(msg, stakeData) - } + //if len(msg.Description.Moniker) > 0 { + // stakeData = addValidatorToStakeData(msg, stakeData) + //} } @@ -131,6 +130,7 @@ func GaiaAppGenState(cdc *codec.Codec, appGenTxs []json.RawMessage) (genesisStat DistrData: distr.DefaultGenesisState(), GovData: gov.DefaultGenesisState(), SlashingData: slashingData, + Txs: appGenTxs, } return @@ -175,11 +175,11 @@ func GaiaValidateGenesisState(genesisState GenesisState) (err error) { if err != nil { return } - err = stake.ValidateGenesis(genesisState.StakeData) - if err != nil { - return + // skip stakeData validation as genesis is created from txs + if len(genesisState.Txs) > 0 { + return nil } - return + return stake.ValidateGenesis(genesisState.StakeData) } // Ensures that there are no duplicate accounts in the genesis state, @@ -197,7 +197,7 @@ func validateGenesisStateAccounts(accs []GenesisAccount) (err error) { } // GaiaAppGenState but with JSON -func GaiaAppGenStateJSON(cdc *codec.Codec, appGenTxs []auth.StdTx) (appState json.RawMessage, err error) { +func GaiaAppGenStateJSON(cdc *codec.Codec, appGenTxs []json.RawMessage) (appState json.RawMessage, err error) { // create the final app state genesisState, err := GaiaAppGenState(cdc, appGenTxs) if err != nil { @@ -206,3 +206,64 @@ func GaiaAppGenStateJSON(cdc *codec.Codec, appGenTxs []auth.StdTx) (appState jso appState, err = codec.MarshalJSONIndent(cdc, genesisState) return } + + +// ProcessStdTxs processes and validates application's genesis StdTxs and returns the list of validators, +// appGenTxs, and persistent peers required to generate genesis.json. +func ProcessStdTxs(moniker string, genTxsDir string, cdc *codec.Codec) ( + validators []tmtypes.GenesisValidator, appGenTxs []auth.StdTx, persistentPeers string, err error) { + var fos []os.FileInfo + fos, err = ioutil.ReadDir(genTxsDir) + if err != nil { + return + } + + var addresses []string + for _, fo := range fos { + filename := path.Join(genTxsDir, fo.Name()) + if !fo.IsDir() && (path.Ext(filename) != ".json") { + continue + } + + // get the genStdTx + var jsonRawTx []byte + jsonRawTx, err = ioutil.ReadFile(filename) + if err != nil { + return + } + var genStdTx auth.StdTx + err = cdc.UnmarshalJSON(jsonRawTx, &genStdTx) + if err != nil { + return + } + appGenTxs = append(appGenTxs , genStdTx) + + nodeAddr := genStdTx.GetMemo() + if len(nodeAddr) == 0 { + err = fmt.Errorf("couldn't find node's address in %s", fo.Name()) + return + } + + msgs := genStdTx.GetMsgs() + if len(msgs) != 1 { + err = errors.New("each genesis transaction must provide a single genesis message") + return + } + msg := msgs[0].(stake.MsgCreateValidator) + validators = append(validators, tmtypes.GenesisValidator{ + PubKey: msg.PubKey, + Power: freeFermionVal, + Name: msg.Description.Moniker, + }) + + // exclude itself from persistent peers + if msg.Description.Moniker != moniker { + addresses = append(addresses, nodeAddr) + } + } + + sort.Strings(addresses) + persistentPeers = strings.Join(addresses, ",") + + return +} diff --git a/cmd/gaia/app/test_utils.go b/cmd/gaia/app/test_utils.go index 18946b3972ae..1baabd76845b 100644 --- a/cmd/gaia/app/test_utils.go +++ b/cmd/gaia/app/test_utils.go @@ -32,18 +32,18 @@ func NewTestGaiaAppGenState( // get genesis account information genAccs := make([]GenesisAccount, len(appGenTxs)) - for i, appGenTx := range appGenTxs { - - var genTx GaiaGenTx - if err := cdc.UnmarshalJSON(appGenTx, &genTx); err != nil { - return GenesisState{}, err - } - - stakeData.Pool.LooseTokens = stakeData.Pool.LooseTokens.Add(sdk.NewDecFromInt(freeFermionsAcc)) - - // create the genesis account for the given genesis tx - genAccs[i] = genesisAccountFromGenTx(genTx) - } + //for _, appGenTx := range appGenTxs { + // + // var genTx GaiaGenTx + // if err := cdc.UnmarshalJSON(appGenTx, &genTx); err != nil { + // return GenesisState{}, err + // } + // + // stakeData.Pool.LooseTokens = stakeData.Pool.LooseTokens.Add(sdk.NewDecFromInt(freeFermionsAcc)) + // + // // create the genesis account for the given genesis tx + // //genAccs[i] = genesisAccountFromGenTx(genTx) + //} for i, tmVal := range tmVals { var issuedDelShares sdk.Dec diff --git a/cmd/gaia/cmd/gaiad/main.go b/cmd/gaia/cmd/gaiad/main.go index adfb12d5735d..7d2c68f4d815 100644 --- a/cmd/gaia/cmd/gaiad/main.go +++ b/cmd/gaia/cmd/gaiad/main.go @@ -31,7 +31,7 @@ func main() { } appInit := app.GaiaAppInit() rootCmd.AddCommand(gaiaInit.InitCmd(ctx, cdc, appInit)) - rootCmd.AddCommand(gaiaInit.TestnetFilesCmd(ctx, cdc, appInit)) + //rootCmd.AddCommand(gaiaInit.TestnetFilesCmd(ctx, cdc, appInit)) server.AddCommands(ctx, cdc, rootCmd, appInit, newApp, exportAppStateAndTMValidators) diff --git a/cmd/gaia/init/init.go b/cmd/gaia/init/init.go index a04c1d2ae77e..274b530959f7 100644 --- a/cmd/gaia/init/init.go +++ b/cmd/gaia/init/init.go @@ -3,121 +3,23 @@ package init import ( "encoding/json" "fmt" - "io/ioutil" - "os" - "path" + "errors" + "github.com/cosmos/cosmos-sdk/cmd/gaia/app" + "github.com/cosmos/cosmos-sdk/x/auth" + "path/filepath" - "sort" "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/server" - servercfg "github.com/cosmos/cosmos-sdk/server/config" "github.com/spf13/cobra" "github.com/spf13/viper" cfg "github.com/tendermint/tendermint/config" - "github.com/tendermint/tendermint/crypto" "github.com/tendermint/tendermint/libs/cli" "github.com/tendermint/tendermint/libs/common" "github.com/tendermint/tendermint/p2p" - "github.com/tendermint/tendermint/privval" "github.com/tendermint/tendermint/types" ) -// get cmd to initialize all files for tendermint and application -func GenTxCmd(ctx *server.Context, cdc *codec.Codec, appInit server.AppInit) *cobra.Command { - cmd := &cobra.Command{ - Use: "gen-tx", - Short: "Create genesis transaction file (under [--home]/config/gentx/gentx-[nodeID].json)", - Args: cobra.NoArgs, - RunE: func(_ *cobra.Command, args []string) error { - config := ctx.Config - config.SetRoot(viper.GetString(cli.HomeFlag)) - - ip := viper.GetString(server.FlagIP) - if len(ip) == 0 { - eip, err := server.ExternalIP() - if err != nil { - return err - } - ip = eip - } - - genTxConfig := servercfg.GenTx{ - viper.GetString(server.FlagName), - viper.GetString(server.FlagClientHome), - viper.GetBool(server.FlagOWK), - ip, - } - cliPrint, genTxFile, err := gentxWithConfig(cdc, appInit, config, genTxConfig) - if err != nil { - return err - } - toPrint := struct { - AppMessage json.RawMessage `json:"app_message"` - GenTxFile json.RawMessage `json:"gen_tx_file"` - }{ - cliPrint, - genTxFile, - } - out, err := codec.MarshalJSONIndent(cdc, toPrint) - if err != nil { - return err - } - fmt.Println(string(out)) - return nil - }, - } - cmd.Flags().String(server.FlagIP, "", "external facing IP to use if left blank IP will be retrieved from this machine") - cmd.Flags().AddFlagSet(appInit.FlagsAppGenTx) - return cmd -} - -// NOTE: This will update (write) the config file with -// updated name (moniker) for node. -func gentxWithConfig(cdc *codec.Codec, appInit server.AppInit, config *cfg.Config, genTxConfig servercfg.GenTx) ( - cliPrint json.RawMessage, genTxFile json.RawMessage, err error) { - nodeKey, err := p2p.LoadOrGenNodeKey(config.NodeKeyFile()) - if err != nil { - return - } - nodeID := string(nodeKey.ID()) - pubKey := readOrCreatePrivValidator(config) - - appGenTx, cliPrint, validator, err := appInit.AppGenTx(cdc, pubKey, genTxConfig) - if err != nil { - return - } - - tx := server.GenesisTx{ - NodeID: nodeID, - IP: genTxConfig.IP, - Validator: validator, - AppGenTx: appGenTx, - } - bz, err := codec.MarshalJSONIndent(cdc, tx) - if err != nil { - return - } - genTxFile = json.RawMessage(bz) - name := fmt.Sprintf("gentx-%v.json", nodeID) - writePath := filepath.Join(config.RootDir, "config", "gentx") - file := filepath.Join(writePath, name) - err = common.EnsureDir(writePath, 0700) - if err != nil { - return - } - err = common.WriteFile(file, bz, 0644) - if err != nil { - return - } - - // Write updated config with moniker - config.Moniker = genTxConfig.Name - configFilePath := filepath.Join(config.RootDir, "config", "config.toml") - cfg.WriteConfigFile(configFilePath, config) - - return -} // get cmd to initialize all files for tendermint and application // nolint: golint @@ -134,6 +36,7 @@ func InitCmd(ctx *server.Context, cdc *codec.Codec, appInit server.AppInit) *cob viper.GetString(server.FlagChainID), viper.GetBool(server.FlagWithTxs), filepath.Join(config.RootDir, "config", "gentx"), + viper.GetString(server.FlagName), viper.GetBool(server.FlagOverwrite), } @@ -164,7 +67,6 @@ func InitCmd(ctx *server.Context, cdc *codec.Codec, appInit server.AppInit) *cob cmd.Flags().Bool(server.FlagWithTxs, false, "apply existing genesis transactions from [--home]/config/gentx/") cmd.Flags().AddFlagSet(appInit.FlagsAppGenState) cmd.Flags().AddFlagSet(appInit.FlagsAppGenTx) // need to add this flagset for when no GenTx's provided - cmd.AddCommand(GenTxCmd(ctx, cdc, appInit)) return cmd } @@ -175,7 +77,7 @@ func initWithConfig(cdc *codec.Codec, appInit server.AppInit, config *cfg.Config return } nodeID = string(nodeKey.ID()) - pubKey := readOrCreatePrivValidator(config) + //pubKey := readOrCreatePrivValidator(config) if initConfig.ChainID == "" { initConfig.ChainID = fmt.Sprintf("test-chain-%v", common.RandStr(6)) @@ -189,119 +91,66 @@ func initWithConfig(cdc *codec.Codec, appInit server.AppInit, config *cfg.Config } // process genesis transactions, or otherwise create one for defaults - var appGenTxs []json.RawMessage - var validators []types.GenesisValidator + var appGenTxs []auth.StdTx var persistentPeers string + var genTxs []json.RawMessage if initConfig.GenTxs { - validators, appGenTxs, persistentPeers, err = processGenTxs(initConfig.GenTxsDir, cdc) + _, appGenTxs, persistentPeers, err = app.ProcessStdTxs(initConfig.Moniker, initConfig.GenTxsDir, cdc) if err != nil { return } + genTxs = make([]json.RawMessage, len(appGenTxs)) config.P2P.PersistentPeers = persistentPeers configFilePath := filepath.Join(config.RootDir, "config", "config.toml") cfg.WriteConfigFile(configFilePath, config) - } else { - genTxConfig := servercfg.GenTx{ - viper.GetString(server.FlagName), - viper.GetString(server.FlagClientHome), - viper.GetBool(server.FlagOWK), - "127.0.0.1", - } - - // Write updated config with moniker - config.Moniker = genTxConfig.Name - configFilePath := filepath.Join(config.RootDir, "config", "config.toml") - cfg.WriteConfigFile(configFilePath, config) - appGenTx, am, validator, err := appInit.AppGenTx(cdc, pubKey, genTxConfig) - appMessage = am - if err != nil { - return "", "", nil, err + for i, stdTx := range appGenTxs { + var jsonRawTx json.RawMessage + jsonRawTx, err = cdc.MarshalJSON(stdTx) + if err != nil { + return + } + genTxs[i] = jsonRawTx } - validators = []types.GenesisValidator{validator} - appGenTxs = []json.RawMessage{appGenTx} - } - - appState, err := appInit.AppGenState(cdc, appGenTxs) - if err != nil { - return - } - - err = writeGenesisFile(cdc, genFile, initConfig.ChainID, validators, appState) + } else { + panic(errors.New("WIP")) + //genTxConfig := servercfg.GenTx{ + // viper.GetString(server.FlagName), + // viper.GetString(server.FlagClientHome), + // viper.GetBool(server.FlagOWK), + // "127.0.0.1", + //} + // + //// Write updated config with moniker + //config.Moniker = genTxConfig.Name + //configFilePath := filepath.Join(config.RootDir, "config", "config.toml") + //cfg.WriteConfigFile(configFilePath, config) + //_, am, err := appInit.AppGenTx(cdc, pubKey, genTxConfig) + //appMessage = am + //if err != nil { + // return "", "", nil, err + //} + //validators = []types.GenesisValidator{validator) + //jsonMsg, err := json.Marshal(appGenTx) + //if err != nil { + // return + //} + //appGenTxs = []json.RawMessage{jsonMsg} + } + + appState, err := app.GaiaAppGenStateJSON(cdc, genTxs) if err != nil { return } - - return -} - -// append a genesis-piece -func processGenTxs(genTxsDir string, cdc *codec.Codec) ( - validators []types.GenesisValidator, appGenTxs []json.RawMessage, persistentPeers string, err error) { - - var fos []os.FileInfo - fos, err = ioutil.ReadDir(genTxsDir) + + err = writeGenesisFile(cdc, genFile, initConfig.ChainID, nil, appState) if err != nil { return } - genTxs := make(map[string]server.GenesisTx) - var nodeIDs []string - for _, fo := range fos { - filename := path.Join(genTxsDir, fo.Name()) - if !fo.IsDir() && (path.Ext(filename) != ".json") { - continue - } - - // get the genTx - var bz []byte - bz, err = ioutil.ReadFile(filename) - if err != nil { - return - } - var genTx server.GenesisTx - err = cdc.UnmarshalJSON(bz, &genTx) - if err != nil { - return - } - - genTxs[genTx.NodeID] = genTx - nodeIDs = append(nodeIDs, genTx.NodeID) - } - - sort.Strings(nodeIDs) - - for _, nodeID := range nodeIDs { - genTx := genTxs[nodeID] - - // combine some stuff - validators = append(validators, genTx.Validator) - appGenTxs = append(appGenTxs, genTx.AppGenTx) - - // Add a persistent peer - comma := "," - if len(persistentPeers) == 0 { - comma = "" - } - persistentPeers += fmt.Sprintf("%s%s@%s:26656", comma, genTx.NodeID, genTx.IP) - } - return } -// read of create the private key file for this config -func readOrCreatePrivValidator(tmConfig *cfg.Config) crypto.PubKey { - // private validator - privValFile := tmConfig.PrivValidatorFile() - var privValidator *privval.FilePV - if common.FileExists(privValFile) { - privValidator = privval.LoadFilePV(privValFile) - } else { - privValidator = privval.GenFilePV(privValFile) - privValidator.Save() - } - return privValidator.GetPubKey() -} // writeGenesisFile creates and writes the genesis configuration to disk. An // error is returned if building or writing the configuration to file fails. diff --git a/cmd/gaia/init/testnet.go b/cmd/gaia/init/testnet.go index ab6ee166090e..fcc20b2d92e0 100644 --- a/cmd/gaia/init/testnet.go +++ b/cmd/gaia/init/testnet.go @@ -3,16 +3,16 @@ package init import ( "fmt" "net" - "os" +// "os" "path/filepath" "github.com/cosmos/cosmos-sdk/server" - "github.com/spf13/cobra" +// "github.com/spf13/cobra" - "github.com/cosmos/cosmos-sdk/codec" +// "github.com/cosmos/cosmos-sdk/codec" "github.com/spf13/viper" - cfg "github.com/tendermint/tendermint/config" +// cfg "github.com/tendermint/tendermint/config" cmn "github.com/tendermint/tendermint/libs/common" ) @@ -27,133 +27,134 @@ var ( ) const nodeDirPerm = 0755 - -// get cmd to initialize all files for tendermint testnet and application -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", - Long: `testnet will create "v" number of directories and populate each with -necessary files (private validator, genesis, config, etc.). - -Note, strict routability for addresses is turned off in the config file. - -Example: - - gaiad testnet --v 4 --o ./output --starting-ip-address 192.168.10.2 - `, - RunE: func(_ *cobra.Command, _ []string) error { - /* - config := ctx.Config - err := testnetWithConfig(config, cdc, appInit) - return err - */ - return nil - }, - } - cmd.Flags().Int(nValidators, 4, - "Number of validators to initialize the testnet with") - cmd.Flags().StringP(outputDir, "o", "./mytestnet", - "Directory to store initialization data for the testnet") - cmd.Flags().String(nodeDirPrefix, "node", - "Prefix the directory name for each node with (node results in node0, node1, ...)") - cmd.Flags().String(nodeDaemonHome, "gaiad", - "Home directory of the node's daemon configuration") - cmd.Flags().String(nodeCliHome, "gaiacli", - "Home directory of the node's cli configuration") - - cmd.Flags().String(startingIPAddress, "192.168.0.1", - "Starting IP address (192.168.0.1 results in persistent peers list ID0@192.168.0.1:46656, ID1@192.168.0.2:46656, ...)") - return cmd -} - -func testnetWithConfig(config *cfg.Config, cdc *codec.Codec, appInit server.AppInit) error { - outDir := viper.GetString(outputDir) - numValidators := viper.GetInt(nValidators) - - // Generate private key, node ID, initial transaction - for i := 0; i < numValidators; i++ { - nodeDirName := fmt.Sprintf("%s%d", viper.GetString(nodeDirPrefix), i) - nodeDaemonHomeName := viper.GetString(nodeDaemonHome) - nodeCliHomeName := viper.GetString(nodeCliHome) - nodeDir := filepath.Join(outDir, nodeDirName, nodeDaemonHomeName) - clientDir := filepath.Join(outDir, nodeDirName, nodeCliHomeName) - gentxsDir := filepath.Join(outDir, "gentxs") - config.SetRoot(nodeDir) - - err := os.MkdirAll(filepath.Join(nodeDir, "config"), nodeDirPerm) - if err != nil { - _ = os.RemoveAll(outDir) - return err - } - - err = os.MkdirAll(clientDir, nodeDirPerm) - if err != nil { - _ = os.RemoveAll(outDir) - return err - } - - config.Moniker = nodeDirName - ip, err := getIP(i) - if err != nil { - return err - } - - genTxConfig := gc.GenTx{ - nodeDirName, - clientDir, - true, - ip, - } - - // Run `init gen-tx` and generate initial transactions - cliPrint, genTxFile, err := gentxWithConfig(cdc, appInit, config, genTxConfig) - if err != nil { - return err - } - - // Save private key seed words - name := fmt.Sprintf("%v.json", "key_seed") - err = writeFile(name, clientDir, cliPrint) - if err != nil { - return err - } - - // Gather gentxs folder - name = fmt.Sprintf("%v.json", nodeDirName) - err = writeFile(name, gentxsDir, genTxFile) - if err != nil { - return err - } - } - - // Generate genesis.json and config.toml - chainID := "chain-" + cmn.RandStr(6) - for i := 0; i < numValidators; i++ { - - nodeDirName := fmt.Sprintf("%s%d", viper.GetString(nodeDirPrefix), i) - nodeDaemonHomeName := viper.GetString(nodeDaemonHome) - nodeDir := filepath.Join(outDir, nodeDirName, nodeDaemonHomeName) - gentxsDir := filepath.Join(outDir, "gentxs") - initConfig := server.InitConfig{ - chainID, - true, - gentxsDir, - true, - } - config.Moniker = nodeDirName - config.SetRoot(nodeDir) - - // Run `init` and generate genesis.json and config.toml - _, _, _, err := initWithConfig(cdc, appInit, config, initConfig) - if err != nil { - return err - } - } - - fmt.Printf("Successfully initialized %v node directories\n", viper.GetInt(nValidators)) - return nil -} +// +//// get cmd to initialize all files for tendermint testnet and application +//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", +// Long: `testnet will create "v" number of directories and populate each with +//necessary files (private validator, genesis, config, etc.). +// +//Note, strict routability for addresses is turned off in the config file. +// +//Example: +// +// gaiad testnet --v 4 --o ./output --starting-ip-address 192.168.10.2 +// `, +// RunE: func(_ *cobra.Command, _ []string) error { +// /* +// config := ctx.Config +// err := testnetWithConfig(config, cdc, appInit) +// return err +// */ +// return nil +// }, +// } +// cmd.Flags().Int(nValidators, 4, +// "Number of validators to initialize the testnet with") +// cmd.Flags().StringP(outputDir, "o", "./mytestnet", +// "Directory to store initialization data for the testnet") +// cmd.Flags().String(nodeDirPrefix, "node", +// "Prefix the directory name for each node with (node results in node0, node1, ...)") +// cmd.Flags().String(nodeDaemonHome, "gaiad", +// "Home directory of the node's daemon configuration") +// cmd.Flags().String(nodeCliHome, "gaiacli", +// "Home directory of the node's cli configuration") +// +// cmd.Flags().String(startingIPAddress, "192.168.0.1", +// "Starting IP address (192.168.0.1 results in persistent peers list ID0@192.168.0.1:46656, ID1@192.168.0.2:46656, ...)") +// return cmd +//} +// +//func testnetWithConfig(config *cfg.Config, cdc *codec.Codec, appInit server.AppInit) error { +// outDir := viper.GetString(outputDir) +// numValidators := viper.GetInt(nValidators) +// +// // Generate private key, node ID, initial transaction +// for i := 0; i < numValidators; i++ { +// nodeDirName := fmt.Sprintf("%s%d", viper.GetString(nodeDirPrefix), i) +// nodeDaemonHomeName := viper.GetString(nodeDaemonHome) +// nodeCliHomeName := viper.GetString(nodeCliHome) +// nodeDir := filepath.Join(outDir, nodeDirName, nodeDaemonHomeName) +// clientDir := filepath.Join(outDir, nodeDirName, nodeCliHomeName) +// gentxsDir := filepath.Join(outDir, "gentxs") +// config.SetRoot(nodeDir) +// +// err := os.MkdirAll(filepath.Join(nodeDir, "config"), nodeDirPerm) +// if err != nil { +// _ = os.RemoveAll(outDir) +// return err +// } +// +// err = os.MkdirAll(clientDir, nodeDirPerm) +// if err != nil { +// _ = os.RemoveAll(outDir) +// return err +// } +// +// config.Moniker = nodeDirName +// ip, err := getIP(i) +// if err != nil { +// return err +// } +// +// genTxConfig := gc.GenTx{ +// nodeDirName, +// clientDir, +// true, +// ip, +// } +// +// // Run `init gen-tx` and generate initial transactions +// cliPrint, genTxFile, err := gentxWithConfig(cdc, appInit, config, genTxConfig) +// if err != nil { +// return err +// } +// +// // Save private key seed words +// name := fmt.Sprintf("%v.json", "key_seed") +// err = writeFile(name, clientDir, cliPrint) +// if err != nil { +// return err +// } +// +// // Gather gentxs folder +// name = fmt.Sprintf("%v.json", nodeDirName) +// err = writeFile(name, gentxsDir, genTxFile) +// if err != nil { +// return err +// } +// } +// +// // Generate genesis.json and config.toml +// chainID := "chain-" + cmn.RandStr(6) +// for i := 0; i < numValidators; i++ { +// +// nodeDirName := fmt.Sprintf("%s%d", viper.GetString(nodeDirPrefix), i) +// nodeDaemonHomeName := viper.GetString(nodeDaemonHome) +// nodeDir := filepath.Join(outDir, nodeDirName, nodeDaemonHomeName) +// gentxsDir := filepath.Join(outDir, "gentxs") +// initConfig := server.InitConfig{ +// chainID, +// true, +// gentxsDir, +// "test", +// true, +// } +// config.Moniker = nodeDirName +// config.SetRoot(nodeDir) +// +// // Run `init` and generate genesis.json and config.toml +// _, _, _, err := initWithConfig(cdc, appInit, config, initConfig) +// if err != nil { +// return err +// } +// } +// +// fmt.Printf("Successfully initialized %v node directories\n", viper.GetInt(nValidators)) +// return nil +//} func getIP(i int) (ip string, err error) { ip = viper.GetString(startingIPAddress) diff --git a/server/init.go b/server/init.go index aaf68a8bc59f..334803cba7c1 100644 --- a/server/init.go +++ b/server/init.go @@ -55,7 +55,7 @@ type AppInit struct { // AppGenState creates the core parameters initialization. It takes in a // pubkey meant to represent the pubkey of the validator of this machine. - AppGenState func(cdc *codec.Codec, appGenTx []auth.StdTx) (appState json.RawMessage, err error) + AppGenState func(cdc *codec.Codec, appGenTx []json.RawMessage) (appState json.RawMessage, err error) } //_____________________________________________________________________ @@ -66,14 +66,19 @@ var DefaultAppInit = AppInit{ } // create the genesis app state -func SimpleAppGenState(cdc *codec.Codec, appGenTxs []auth.StdTx) (appState json.RawMessage, err error) { +func SimpleAppGenState(cdc *codec.Codec, appGenTxs []json.RawMessage) (appState json.RawMessage, err error) { if len(appGenTxs) != 1 { err = errors.New("must provide a single genesis transaction") return } - msgs := appGenTxs[0].GetMsgs() + var tx auth.StdTx + err = cdc.UnmarshalJSON(appGenTxs[0], &tx) + if err != nil { + return + } + msgs := tx.GetMsgs() if len(msgs) != 1 { err = errors.New("must provide a single genesis message") return diff --git a/x/auth/client/cli/sign.go b/x/auth/client/cli/sign.go index 30704a50027a..07191749010f 100644 --- a/x/auth/client/cli/sign.go +++ b/x/auth/client/cli/sign.go @@ -19,6 +19,7 @@ import ( const ( flagAppend = "append" flagPrintSigs = "print-sigs" + flagOffline = "offline" ) // GetSignCommand returns the sign command @@ -34,6 +35,7 @@ Read a transaction from , sign it, and print its JSON encoding.`, cmd.Flags().String(client.FlagName, "", "Name of private key with which to sign") cmd.Flags().Bool(flagAppend, true, "Append the signature to the existing ones. If disabled, old signatures would be overwritten") cmd.Flags().Bool(flagPrintSigs, false, "Print the addresses that must sign the transaction and those who have already signed it, then exit") + cmd.Flags().Bool(flagOffline, false, "Offline mode. Do not query local cache.") return cmd } @@ -53,7 +55,7 @@ func makeSignCmd(cdc *amino.Codec, decoder auth.AccountDecoder) func(cmd *cobra. cliCtx := context.NewCLIContext().WithCodec(cdc).WithAccountDecoder(decoder) txBldr := authtxb.NewTxBuilderFromCLI() - newTx, err := utils.SignStdTx(txBldr, cliCtx, name, stdTx, viper.GetBool(flagAppend)) + newTx, err := utils.SignStdTx(txBldr, cliCtx, name, stdTx, viper.GetBool(flagAppend), viper.GetBool(flagOffline)) if err != nil { return err } diff --git a/x/stake/client/cli/flags.go b/x/stake/client/cli/flags.go index bec76298f3fc..aa1f24cac500 100644 --- a/x/stake/client/cli/flags.go +++ b/x/stake/client/cli/flags.go @@ -25,6 +25,10 @@ const ( FlagCommissionRate = "commission-rate" FlagCommissionMaxRate = "commission-max-rate" FlagCommissionMaxChangeRate = "commission-max-change-rate" + + FlagGenesisFormat = "genesis-format" + FlagNodeID = "node-id" + FlagIP = "ip" ) // common flagsets to add to various functions From 2b054b18c0e6620e7a0aaf807922c2dbcbc699c0 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Fri, 12 Oct 2018 11:54:47 -0700 Subject: [PATCH 22/83] So long GenTx --- cmd/gaia/app/genesis.go | 7 ------- cmd/gaia/init/init.go | 1 - server/init.go | 7 ------- 3 files changed, 15 deletions(-) diff --git a/cmd/gaia/app/genesis.go b/cmd/gaia/app/genesis.go index 332b35eaeecc..bdb9c80333f2 100644 --- a/cmd/gaia/app/genesis.go +++ b/cmd/gaia/app/genesis.go @@ -70,17 +70,10 @@ func (ga *GenesisAccount) ToAccount() (acc *auth.BaseAccount) { // get app init parameters for server init command func GaiaAppInit() server.AppInit { fsAppGenState := pflag.NewFlagSet("", pflag.ContinueOnError) - fsAppGenTx := pflag.NewFlagSet("", pflag.ContinueOnError) - fsAppGenTx.String(server.FlagName, "", "validator moniker, required") - fsAppGenTx.String(server.FlagClientHome, DefaultCLIHome, - "home directory for the client, used for key generation") - fsAppGenTx.Bool(server.FlagOWK, false, "overwrite the accounts created") return server.AppInit{ FlagsAppGenState: fsAppGenState, - FlagsAppGenTx: fsAppGenTx, AppGenState: GaiaAppGenStateJSON, - //AppGenTx: server.SimpleAppGenTx, } } diff --git a/cmd/gaia/init/init.go b/cmd/gaia/init/init.go index 274b530959f7..1b8db63dc50f 100644 --- a/cmd/gaia/init/init.go +++ b/cmd/gaia/init/init.go @@ -66,7 +66,6 @@ func InitCmd(ctx *server.Context, cdc *codec.Codec, appInit server.AppInit) *cob cmd.Flags().String(server.FlagChainID, "", "genesis file chain-id, if left blank will be randomly created") cmd.Flags().Bool(server.FlagWithTxs, false, "apply existing genesis transactions from [--home]/config/gentx/") cmd.Flags().AddFlagSet(appInit.FlagsAppGenState) - cmd.Flags().AddFlagSet(appInit.FlagsAppGenTx) // need to add this flagset for when no GenTx's provided return cmd } diff --git a/server/init.go b/server/init.go index 334803cba7c1..64b52b8b4dc5 100644 --- a/server/init.go +++ b/server/init.go @@ -10,12 +10,10 @@ import ( "github.com/pkg/errors" "github.com/spf13/pflag" - "github.com/tendermint/tendermint/crypto" dbm "github.com/tendermint/tendermint/libs/db" clkeys "github.com/cosmos/cosmos-sdk/client/keys" "github.com/cosmos/cosmos-sdk/codec" - serverconfig "github.com/cosmos/cosmos-sdk/server/config" sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -47,11 +45,6 @@ type AppInit struct { // flags required for application init functions FlagsAppGenState *pflag.FlagSet - FlagsAppGenTx *pflag.FlagSet - - // create the application genesis tx - AppGenTx func(cdc *codec.Codec, pk crypto.PubKey, genTxConfig serverconfig.GenTx) ( - appGenTx auth.StdTx, cliPrint json.RawMessage, err error) // AppGenState creates the core parameters initialization. It takes in a // pubkey meant to represent the pubkey of the validator of this machine. From 37dee5091affa6071a5020cbe5bbebf6ed9670e1 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Fri, 12 Oct 2018 11:58:01 -0700 Subject: [PATCH 23/83] Get rid of FlagOWK --- server/init.go | 1 - server/test_helpers.go | 1 - 2 files changed, 2 deletions(-) diff --git a/server/init.go b/server/init.go index 64b52b8b4dc5..33b2ae5d6d6d 100644 --- a/server/init.go +++ b/server/init.go @@ -25,7 +25,6 @@ const ( FlagIP = "ip" FlagChainID = "chain-id" FlagClientHome = "home-client" - FlagOWK = "owk" ) // DefaultKeyPass contains the default key password for genesis transactions diff --git a/server/test_helpers.go b/server/test_helpers.go index 69edd5c6a4b6..680d8876c18e 100644 --- a/server/test_helpers.go +++ b/server/test_helpers.go @@ -43,7 +43,6 @@ func SetupViper(t *testing.T) func() { require.Nil(t, err) viper.Set(cli.HomeFlag, rootDir) viper.Set(FlagName, "moniker") - viper.Set(FlagOWK, true) return func() { err := os.RemoveAll(rootDir) if err != nil { From 34b883820f9849073e59368c5aa65e4e9edef52c Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Fri, 12 Oct 2018 11:58:51 -0700 Subject: [PATCH 24/83] Get rid of FlagClientHome --- server/init.go | 1 - 1 file changed, 1 deletion(-) diff --git a/server/init.go b/server/init.go index 33b2ae5d6d6d..a4cbf62b3504 100644 --- a/server/init.go +++ b/server/init.go @@ -24,7 +24,6 @@ const ( FlagWithTxs = "with-txs" FlagIP = "ip" FlagChainID = "chain-id" - FlagClientHome = "home-client" ) // DefaultKeyPass contains the default key password for genesis transactions From 0f3274f833f6d2722a0c32c2e8547239f63ae30c Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Fri, 12 Oct 2018 11:59:27 -0700 Subject: [PATCH 25/83] FlagIP - ditto --- server/init.go | 1 - 1 file changed, 1 deletion(-) diff --git a/server/init.go b/server/init.go index a4cbf62b3504..a4c90c7fb3af 100644 --- a/server/init.go +++ b/server/init.go @@ -22,7 +22,6 @@ const ( FlagName = "name" FlagOverwrite = "overwrite" FlagWithTxs = "with-txs" - FlagIP = "ip" FlagChainID = "chain-id" ) From 1c1a3270d8ecb88e40fe7f1bf627429fca49d1d3 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Sat, 13 Oct 2018 18:57:04 -0700 Subject: [PATCH 26/83] Commit just not to lose history --- cmd/gaia/app/app.go | 2 + cmd/gaia/app/genesis.go | 35 ++++++++++-- cmd/gaia/init/init.go | 120 ++++++++++++++++++++------------------- x/auth/ante.go | 3 +- x/stake/client/cli/tx.go | 1 - 5 files changed, 95 insertions(+), 66 deletions(-) diff --git a/cmd/gaia/app/app.go b/cmd/gaia/app/app.go index 2598973567be..ff48eb029043 100644 --- a/cmd/gaia/app/app.go +++ b/cmd/gaia/app/app.go @@ -2,6 +2,7 @@ package app import ( "encoding/json" + "fmt" "io" "os" @@ -254,6 +255,7 @@ func (app *GaiaApp) initChainer(ctx sdk.Context, req abci.RequestInitChain) abci validators = app.stakeKeeper.ApplyAndReturnValidatorSetUpdates(ctx) } + fmt.Printf("%+v\n", validators) return abci.ResponseInitChain{ Validators: validators, } diff --git a/cmd/gaia/app/genesis.go b/cmd/gaia/app/genesis.go index bdb9c80333f2..11d53882ca08 100644 --- a/cmd/gaia/app/genesis.go +++ b/cmd/gaia/app/genesis.go @@ -10,6 +10,7 @@ import ( "sort" "strings" + "github.com/tendermint/tendermint/crypto" "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/server" sdk "github.com/cosmos/cosmos-sdk/types" @@ -107,7 +108,7 @@ func GaiaAppGenState(cdc *codec.Codec, appGenTxs []json.RawMessage) (genesisStat // create the genesis account, give'm few steaks and a buncha token with there name genaccs[i] = genesisAccountFromMsgCreateValidator(msg) - stakeData.Pool.LooseTokens = stakeData.Pool.LooseTokens.Add(sdk.NewDecFromInt(freeFermionsAcc)) // increase the supply + stakeData.Pool.LooseTokens = stakeData.Pool.LooseTokens.Add(sdk.NewDecFromInt(msg.Delegation.Amount)) // increase the supply // add the validator //if len(msg.Description.Moniker) > 0 { @@ -129,11 +130,26 @@ func GaiaAppGenState(cdc *codec.Codec, appGenTxs []json.RawMessage) (genesisStat return } -func addValidatorToStakeData(msg stake.MsgCreateValidator, stakeData stake.GenesisState) stake.GenesisState { - validator := stake.NewValidator( - sdk.ValAddress(msg.ValidatorAddr), msg.PubKey, msg.Description, - ) +func DefaultState(moniker string, pubKey crypto.PubKey) (genesisState GenesisState, genValidator tmtypes.GenesisValidator) { + acc := NewDefaultGenesisAccount(sdk.AccAddress(pubKey.Address())) + stakeData := stake.DefaultGenesisState() + validator := stake.NewValidator(sdk.ValAddress(acc.Address), pubKey, stake.NewDescription(moniker, "", "", "")) + stakeData = addValidatorToStakeData(validator, stakeData) + stakeData.Pool.LooseTokens = stakeData.Pool.LooseTokens.Add(sdk.NewDecFromInt(freeFermionsAcc)) + genesisState = GenesisState{ + Accounts: []GenesisAccount{acc}, + StakeData: stakeData, + GovData: gov.DefaultGenesisState(), + } + genValidator = tmtypes.GenesisValidator{ + Name: moniker, + PubKey: pubKey, + Power: freeFermionVal, + } + return +} +func addValidatorToStakeData(validator stake.Validator, stakeData stake.GenesisState) stake.GenesisState { stakeData.Pool.LooseTokens = stakeData.Pool.LooseTokens.Add(sdk.NewDec(freeFermionVal)) // increase the supply // add some new shares to the validator @@ -260,3 +276,12 @@ func ProcessStdTxs(moniker string, genTxsDir string, cdc *codec.Codec) ( return } + +func NewDefaultGenesisAccount(addr sdk.AccAddress) GenesisAccount { + accAuth := auth.NewBaseAccountWithAddress(addr) + accAuth.Coins = []sdk.Coin{ + {"fooToken", sdk.NewInt(1000)}, + {"steak", freeFermionsAcc}, + } + return NewGenesisAccount(&accAuth) +} diff --git a/cmd/gaia/init/init.go b/cmd/gaia/init/init.go index 1b8db63dc50f..448d17b64633 100644 --- a/cmd/gaia/init/init.go +++ b/cmd/gaia/init/init.go @@ -3,10 +3,10 @@ package init import ( "encoding/json" "fmt" - "errors" "github.com/cosmos/cosmos-sdk/cmd/gaia/app" "github.com/cosmos/cosmos-sdk/x/auth" - + "github.com/tendermint/tendermint/crypto" + "github.com/tendermint/tendermint/privval" "path/filepath" "github.com/cosmos/cosmos-sdk/codec" @@ -20,6 +20,13 @@ import ( "github.com/tendermint/tendermint/types" ) +const ( + flagChainID = "chain-id" + flagWithTxs = "with-txs" + flagMoniker = "moniker" + flagOverwrite = "overwrite" +) + // get cmd to initialize all files for tendermint and application // nolint: golint @@ -32,19 +39,17 @@ func InitCmd(ctx *server.Context, cdc *codec.Codec, appInit server.AppInit) *cob config := ctx.Config config.SetRoot(viper.GetString(cli.HomeFlag)) - initConfig := server.InitConfig{ - viper.GetString(server.FlagChainID), - viper.GetBool(server.FlagWithTxs), - filepath.Join(config.RootDir, "config", "gentx"), - viper.GetString(server.FlagName), - viper.GetBool(server.FlagOverwrite), - } - - chainID, nodeID, appMessage, err := initWithConfig(cdc, appInit, config, initConfig) - if err != nil { - return err + chainID := viper.GetString(flagChainID) + if chainID == "" { + chainID = fmt.Sprintf("test-chain-%v", common.RandStr(6)) } + genTxsDir := filepath.Join(config.RootDir, "config", "gentx") + moniker := viper.GetString(flagMoniker) + withTxs := viper.GetBool(flagWithTxs) + overwriteGenesis := viper.GetBool(flagOverwrite) + nodeID, appMessage, err := initWithConfig(cdc, appInit, config, chainID, moniker, genTxsDir, withTxs, overwriteGenesis) // print out some key information + toPrint := struct { ChainID string `json:"chain_id"` NodeID string `json:"node_id"` @@ -62,29 +67,25 @@ func InitCmd(ctx *server.Context, cdc *codec.Codec, appInit server.AppInit) *cob return nil }, } - cmd.Flags().BoolP(server.FlagOverwrite, "o", false, "overwrite the genesis.json file") - cmd.Flags().String(server.FlagChainID, "", "genesis file chain-id, if left blank will be randomly created") - cmd.Flags().Bool(server.FlagWithTxs, false, "apply existing genesis transactions from [--home]/config/gentx/") - cmd.Flags().AddFlagSet(appInit.FlagsAppGenState) + + cmd.Flags().BoolP(flagOverwrite, "o", false, "overwrite the genesis.json file") + cmd.Flags().String(flagChainID, "", "genesis file chain-id, if left blank will be randomly created") + cmd.Flags().Bool(flagWithTxs, false, "apply existing genesis transactions from [--home]/config/gentx/") + cmd.Flags().String(flagMoniker, "", "moniker") + //cmd.Flags().AddFlagSet(appInit.FlagsAppGenState) return cmd } -func initWithConfig(cdc *codec.Codec, appInit server.AppInit, config *cfg.Config, initConfig server.InitConfig) ( - chainID string, nodeID string, appMessage json.RawMessage, err error) { +func initWithConfig(cdc *codec.Codec, appInit server.AppInit, config *cfg.Config, chainID, moniker, genTxsDir string, withGenTxs, overwriteGenesis bool) ( + nodeID string, appMessage json.RawMessage, err error) { nodeKey, err := p2p.LoadOrGenNodeKey(config.NodeKeyFile()) if err != nil { return } nodeID = string(nodeKey.ID()) - //pubKey := readOrCreatePrivValidator(config) - - if initConfig.ChainID == "" { - initConfig.ChainID = fmt.Sprintf("test-chain-%v", common.RandStr(6)) - } - chainID = initConfig.ChainID genFile := config.GenesisFile() - if !initConfig.Overwrite && common.FileExists(genFile) { + if !overwriteGenesis && common.FileExists(genFile) { err = fmt.Errorf("genesis.json file already exists: %v", genFile) return } @@ -93,16 +94,16 @@ func initWithConfig(cdc *codec.Codec, appInit server.AppInit, config *cfg.Config var appGenTxs []auth.StdTx var persistentPeers string var genTxs []json.RawMessage + var appState json.RawMessage + var validators []types.GenesisValidator - if initConfig.GenTxs { - _, appGenTxs, persistentPeers, err = app.ProcessStdTxs(initConfig.Moniker, initConfig.GenTxsDir, cdc) + if withGenTxs { + _, appGenTxs, persistentPeers, err = app.ProcessStdTxs(moniker, genTxsDir, cdc) if err != nil { return } genTxs = make([]json.RawMessage, len(appGenTxs)) config.P2P.PersistentPeers = persistentPeers - configFilePath := filepath.Join(config.RootDir, "config", "config.toml") - cfg.WriteConfigFile(configFilePath, config) for i, stdTx := range appGenTxs { var jsonRawTx json.RawMessage jsonRawTx, err = cdc.MarshalJSON(stdTx) @@ -111,38 +112,25 @@ func initWithConfig(cdc *codec.Codec, appInit server.AppInit, config *cfg.Config } genTxs[i] = jsonRawTx } + + appState, err = app.GaiaAppGenStateJSON(cdc, genTxs) + if err != nil { + return + } } else { - panic(errors.New("WIP")) - //genTxConfig := servercfg.GenTx{ - // viper.GetString(server.FlagName), - // viper.GetString(server.FlagClientHome), - // viper.GetBool(server.FlagOWK), - // "127.0.0.1", - //} - // - //// Write updated config with moniker - //config.Moniker = genTxConfig.Name - //configFilePath := filepath.Join(config.RootDir, "config", "config.toml") - //cfg.WriteConfigFile(configFilePath, config) - //_, am, err := appInit.AppGenTx(cdc, pubKey, genTxConfig) - //appMessage = am - //if err != nil { - // return "", "", nil, err - //} - //validators = []types.GenesisValidator{validator) - //jsonMsg, err := json.Marshal(appGenTx) - //if err != nil { - // return - //} - //appGenTxs = []json.RawMessage{jsonMsg} + var genesisState app.GenesisState + pubKey := readOrCreatePrivValidator(config) + config.Moniker = moniker + genesisState, genValidator := app.DefaultState(config.Moniker, pubKey) + appState, err = codec.MarshalJSONIndent(cdc, genesisState) + if err != nil { + return + } + validators = []types.GenesisValidator{genValidator} } - appState, err := app.GaiaAppGenStateJSON(cdc, genTxs) - if err != nil { - return - } - - err = writeGenesisFile(cdc, genFile, initConfig.ChainID, nil, appState) + cfg.WriteConfigFile(filepath.Join(config.RootDir, "config", "config.toml"), config) + err = writeGenesisFile(cdc, genFile, chainID, validators, appState) if err != nil { return } @@ -167,3 +155,17 @@ func writeGenesisFile(cdc *codec.Codec, genesisFile, chainID string, validators return genDoc.SaveAs(genesisFile) } + +// read of create the private key file for this config +func readOrCreatePrivValidator(tmConfig *cfg.Config) crypto.PubKey { + // private validator + privValFile := tmConfig.PrivValidatorFile() + var privValidator *privval.FilePV + if common.FileExists(privValFile) { + privValidator = privval.LoadFilePV(privValFile) + } else { + privValidator = privval.GenFilePV(privValFile) + privValidator.Save() + } + return privValidator.GetPubKey() +} diff --git a/x/auth/ante.go b/x/auth/ante.go index b6f88025453b..c6d9ae818dea 100644 --- a/x/auth/ante.go +++ b/x/auth/ante.go @@ -4,11 +4,11 @@ import ( "bytes" "encoding/hex" "fmt" - sdk "github.com/cosmos/cosmos-sdk/types" "github.com/tendermint/tendermint/crypto" "github.com/tendermint/tendermint/crypto/ed25519" "github.com/tendermint/tendermint/crypto/secp256k1" + "os" ) const ( @@ -33,6 +33,7 @@ func NewAnteHandler(am AccountMapper, fck FeeCollectionKeeper) sdk.AnteHandler { if !ok { return ctx, sdk.ErrInternal("tx must be StdTx").Result(), true } + fmt.Fprintf(os.Stderr,"Block Height: %d", ctx.BlockHeight()) // Ensure that the provided fees meet a minimum threshold for the validator, if this is a CheckTx. // This is only for local mempool purposes, and thus is only ran on check tx. diff --git a/x/stake/client/cli/tx.go b/x/stake/client/cli/tx.go index ffb283f06e2c..00688f23c968 100644 --- a/x/stake/client/cli/tx.go +++ b/x/stake/client/cli/tx.go @@ -2,7 +2,6 @@ package cli import ( "fmt" - "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/client/utils" From 8ab1f2fdd542e52dd69046ca87d0f6d800cc49d9 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Sun, 14 Oct 2018 21:01:07 -0700 Subject: [PATCH 27/83] Get the txs through DeliverTx --- cmd/gaia/app/app.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/cmd/gaia/app/app.go b/cmd/gaia/app/app.go index ff48eb029043..6bd1ddb88022 100644 --- a/cmd/gaia/app/app.go +++ b/cmd/gaia/app/app.go @@ -246,7 +246,11 @@ func (app *GaiaApp) initChainer(ctx sdk.Context, req abci.RequestInitChain) abci if err != nil { panic(err) } - res := app.BaseApp.Deliver(tx) + bz, err := app.cdc.MarshalBinary(tx) + if err != nil { + panic(err) + } + res := app.BaseApp.DeliverTx(bz) if !res.IsOK() { panic(res.Log) } From 4b451a1ff61955f07dba2cb00f27616fdc2d7b59 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Mon, 15 Oct 2018 10:57:18 -0700 Subject: [PATCH 28/83] Add app.slashingKeeper.AddValidators at the end of genesis --- cmd/gaia/app/app.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/cmd/gaia/app/app.go b/cmd/gaia/app/app.go index 6bd1ddb88022..79e2b088514c 100644 --- a/cmd/gaia/app/app.go +++ b/cmd/gaia/app/app.go @@ -257,9 +257,16 @@ func (app *GaiaApp) initChainer(ctx sdk.Context, req abci.RequestInitChain) abci } validators = app.stakeKeeper.ApplyAndReturnValidatorSetUpdates(ctx) + app.slashingKeeper.AddValidators(ctx, validators) + } + + // sanity check + if len(req.Validators) > 0 { + if len(req.Validators) != len(validators) { + panic(fmt.Errorf("len(RequestInitChain.Validators) != len(validators): ")) + } } - fmt.Printf("%+v\n", validators) return abci.ResponseInitChain{ Validators: validators, } From 52970e53d8de44a485b94e73295308da025dd6eb Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Mon, 15 Oct 2018 14:47:16 -0700 Subject: [PATCH 29/83] On InitChain(), Signature's account number must be 0 --- x/auth/ante.go | 18 +++++++----- x/auth/ante_test.go | 67 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 7 deletions(-) diff --git a/x/auth/ante.go b/x/auth/ante.go index c6d9ae818dea..a0fe71b3c33a 100644 --- a/x/auth/ante.go +++ b/x/auth/ante.go @@ -8,7 +8,6 @@ import ( "github.com/tendermint/tendermint/crypto" "github.com/tendermint/tendermint/crypto/ed25519" "github.com/tendermint/tendermint/crypto/secp256k1" - "os" ) const ( @@ -33,7 +32,6 @@ func NewAnteHandler(am AccountMapper, fck FeeCollectionKeeper) sdk.AnteHandler { if !ok { return ctx, sdk.ErrInternal("tx must be StdTx").Result(), true } - fmt.Fprintf(os.Stderr,"Block Height: %d", ctx.BlockHeight()) // Ensure that the provided fees meet a minimum threshold for the validator, if this is a CheckTx. // This is only for local mempool purposes, and thus is only ran on check tx. @@ -82,7 +80,7 @@ func NewAnteHandler(am AccountMapper, fck FeeCollectionKeeper) sdk.AnteHandler { if !res.IsOK() { return newCtx, res, true } - res = validateAccNumAndSequence(signerAccs, stdSigs) + res = validateAccNumAndSequence(ctx, signerAccs, stdSigs) if !res.IsOK() { return newCtx, res, true } @@ -150,17 +148,23 @@ func getSignerAccs(ctx sdk.Context, am AccountMapper, addrs []sdk.AccAddress) (a return } -func validateAccNumAndSequence(accs []Account, sigs []StdSignature) sdk.Result { +func validateAccNumAndSequence(ctx sdk.Context, accs []Account, sigs []StdSignature) sdk.Result { for i := 0; i < len(accs); i++ { - accnum := accs[i].GetAccountNumber() - seq := accs[i].GetSequence() + // On InitChain, make sure account number == 0 + if ctx.BlockHeight() == 0 && sigs[i].AccountNumber != 0 { + return sdk.ErrInvalidSequence( + fmt.Sprintf("Invalid account number for BlockHeight == 0. Got %d, expected 0", sigs[i].AccountNumber)).Result() + } + // Check account number. - if accnum != sigs[i].AccountNumber { + accnum := accs[i].GetAccountNumber() + if ctx.BlockHeight() != 0 && accnum != sigs[i].AccountNumber { return sdk.ErrInvalidSequence( fmt.Sprintf("Invalid account number. Got %d, expected %d", sigs[i].AccountNumber, accnum)).Result() } // Check sequence number. + seq := accs[i].GetSequence() if seq != sigs[i].Sequence { return sdk.ErrInvalidSequence( fmt.Sprintf("Invalid sequence. Got %d, expected %d", sigs[i].Sequence, seq)).Result() diff --git a/x/auth/ante_test.go b/x/auth/ante_test.go index 2a289f317bf8..2d60b28013ab 100644 --- a/x/auth/ante_test.go +++ b/x/auth/ante_test.go @@ -169,6 +169,7 @@ func TestAnteHandlerAccountNumbers(t *testing.T) { feeCollector := NewFeeCollectionKeeper(cdc, capKey2) anteHandler := NewAnteHandler(mapper, feeCollector) ctx := sdk.NewContext(ms, abci.Header{ChainID: "mychainid"}, false, log.NewNopLogger()) + ctx = ctx.WithBlockHeight(1) // keys and addresses priv1, addr1 := privAndAddr() @@ -218,6 +219,66 @@ func TestAnteHandlerAccountNumbers(t *testing.T) { checkValidTx(t, anteHandler, ctx, tx, false) } +// Test logic around account number checking with many signers when BlockHeight is 0. +func TestAnteHandlerAccountNumbersAtBlockHeightZero(t *testing.T) { + // setup + ms, capKey, capKey2 := setupMultiStore() + cdc := codec.New() + RegisterBaseAccount(cdc) + mapper := NewAccountMapper(cdc, capKey, ProtoBaseAccount) + feeCollector := NewFeeCollectionKeeper(cdc, capKey2) + anteHandler := NewAnteHandler(mapper, feeCollector) + ctx := sdk.NewContext(ms, abci.Header{ChainID: "mychainid"}, false, log.NewNopLogger()) + ctx = ctx.WithBlockHeight(0) + + // keys and addresses + priv1, addr1 := privAndAddr() + priv2, addr2 := privAndAddr() + + // set the accounts + acc1 := mapper.NewAccountWithAddress(ctx, addr1) + acc1.SetCoins(newCoins()) + mapper.SetAccount(ctx, acc1) + acc2 := mapper.NewAccountWithAddress(ctx, addr2) + acc2.SetCoins(newCoins()) + mapper.SetAccount(ctx, acc2) + + // msg and signatures + var tx sdk.Tx + msg := newTestMsg(addr1) + fee := newStdFee() + + msgs := []sdk.Msg{msg} + + // test good tx from one signer + privs, accnums, seqs := []crypto.PrivKey{priv1}, []int64{0}, []int64{0} + tx = newTestTx(ctx, msgs, privs, accnums, seqs, fee) + checkValidTx(t, anteHandler, ctx, tx, false) + + // new tx from wrong account number + seqs = []int64{1} + tx = newTestTx(ctx, msgs, privs, []int64{1}, seqs, fee) + checkInvalidTx(t, anteHandler, ctx, tx, false, sdk.CodeInvalidSequence) + + // from correct account number + seqs = []int64{1} + tx = newTestTx(ctx, msgs, privs, []int64{0}, seqs, fee) + checkValidTx(t, anteHandler, ctx, tx, false) + + // new tx with another signer and incorrect account numbers + msg1 := newTestMsg(addr1, addr2) + msg2 := newTestMsg(addr2, addr1) + msgs = []sdk.Msg{msg1, msg2} + privs, accnums, seqs = []crypto.PrivKey{priv1, priv2}, []int64{1, 0}, []int64{2, 0} + tx = newTestTx(ctx, msgs, privs, accnums, seqs, fee) + checkInvalidTx(t, anteHandler, ctx, tx, false, sdk.CodeInvalidSequence) + + // correct account numbers + privs, accnums, seqs = []crypto.PrivKey{priv1, priv2}, []int64{0, 0}, []int64{2, 0} + tx = newTestTx(ctx, msgs, privs, accnums, seqs, fee) + checkValidTx(t, anteHandler, ctx, tx, false) +} + // Test logic around sequence checking with one signer and many signers. func TestAnteHandlerSequences(t *testing.T) { // setup @@ -228,6 +289,7 @@ func TestAnteHandlerSequences(t *testing.T) { feeCollector := NewFeeCollectionKeeper(cdc, capKey2) anteHandler := NewAnteHandler(mapper, feeCollector) ctx := sdk.NewContext(ms, abci.Header{ChainID: "mychainid"}, false, log.NewNopLogger()) + ctx = ctx.WithBlockHeight(1) // keys and addresses priv1, addr1 := privAndAddr() @@ -274,6 +336,7 @@ func TestAnteHandlerSequences(t *testing.T) { tx = newTestTx(ctx, msgs, privs, accnums, seqs, fee) checkValidTx(t, anteHandler, ctx, tx, false) + // replay fails checkInvalidTx(t, anteHandler, ctx, tx, false, sdk.CodeInvalidSequence) @@ -348,6 +411,7 @@ func TestAnteHandlerMemoGas(t *testing.T) { feeCollector := NewFeeCollectionKeeper(cdc, capKey2) anteHandler := NewAnteHandler(mapper, feeCollector) ctx := sdk.NewContext(ms, abci.Header{ChainID: "mychainid"}, false, log.NewNopLogger()) + ctx = ctx.WithBlockHeight(1) // keys and addresses priv1, addr1 := privAndAddr() @@ -391,6 +455,7 @@ func TestAnteHandlerMultiSigner(t *testing.T) { feeCollector := NewFeeCollectionKeeper(cdc, capKey2) anteHandler := NewAnteHandler(mapper, feeCollector) ctx := sdk.NewContext(ms, abci.Header{ChainID: "mychainid"}, false, log.NewNopLogger()) + ctx = ctx.WithBlockHeight(1) // keys and addresses priv1, addr1 := privAndAddr() @@ -442,6 +507,7 @@ func TestAnteHandlerBadSignBytes(t *testing.T) { feeCollector := NewFeeCollectionKeeper(cdc, capKey2) anteHandler := NewAnteHandler(mapper, feeCollector) ctx := sdk.NewContext(ms, abci.Header{ChainID: "mychainid"}, false, log.NewNopLogger()) + ctx = ctx.WithBlockHeight(1) // keys and addresses priv1, addr1 := privAndAddr() @@ -523,6 +589,7 @@ func TestAnteHandlerSetPubKey(t *testing.T) { feeCollector := NewFeeCollectionKeeper(cdc, capKey2) anteHandler := NewAnteHandler(mapper, feeCollector) ctx := sdk.NewContext(ms, abci.Header{ChainID: "mychainid"}, false, log.NewNopLogger()) + ctx = ctx.WithBlockHeight(1) // keys and addresses priv1, addr1 := privAndAddr() From 8a1296c14e73262794652b656cbb695ae52f5a44 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Mon, 15 Oct 2018 14:58:52 -0700 Subject: [PATCH 30/83] Add (tentative?) command to generate {node_key,priv_validator}.json files --- cmd/gaia/init/init.go | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/cmd/gaia/init/init.go b/cmd/gaia/init/init.go index 448d17b64633..b96413388c16 100644 --- a/cmd/gaia/init/init.go +++ b/cmd/gaia/init/init.go @@ -7,8 +7,10 @@ import ( "github.com/cosmos/cosmos-sdk/x/auth" "github.com/tendermint/tendermint/crypto" "github.com/tendermint/tendermint/privval" + "os" "path/filepath" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/server" "github.com/spf13/cobra" @@ -21,10 +23,11 @@ import ( ) const ( - flagChainID = "chain-id" - flagWithTxs = "with-txs" - flagMoniker = "moniker" - flagOverwrite = "overwrite" + flagChainID = "chain-id" + flagWithTxs = "with-txs" + flagMoniker = "moniker" + flagOverwrite = "overwrite" + flagEnsurePrivValNodeKey = "ensure-privval-nodekey" ) @@ -72,7 +75,7 @@ func InitCmd(ctx *server.Context, cdc *codec.Codec, appInit server.AppInit) *cob cmd.Flags().String(flagChainID, "", "genesis file chain-id, if left blank will be randomly created") cmd.Flags().Bool(flagWithTxs, false, "apply existing genesis transactions from [--home]/config/gentx/") cmd.Flags().String(flagMoniker, "", "moniker") - //cmd.Flags().AddFlagSet(appInit.FlagsAppGenState) + cmd.Flags().Bool(flagEnsurePrivValNodeKey, false, "Ensure priv_validator.json and node_key.json files are created and exit") return cmd } @@ -83,6 +86,16 @@ func initWithConfig(cdc *codec.Codec, appInit server.AppInit, config *cfg.Config return } nodeID = string(nodeKey.ID()) + if viper.GetBool(flagEnsurePrivValNodeKey) { + privValFile := config.PrivValidatorFile() + if !common.FileExists(privValFile) { + fmt.Fprintf(os.Stderr, "%s does not exist, creating ...\n", privValFile) + } + pk := readOrCreatePrivValidator(privValFile) + fmt.Fprintf(os.Stderr,"validator public key: %s\n", sdk.MustBech32ifyConsPub(pk)) + return + } + genFile := config.GenesisFile() if !overwriteGenesis && common.FileExists(genFile) { @@ -119,7 +132,7 @@ func initWithConfig(cdc *codec.Codec, appInit server.AppInit, config *cfg.Config } } else { var genesisState app.GenesisState - pubKey := readOrCreatePrivValidator(config) + pubKey := readOrCreatePrivValidator(config.PrivValidatorFile()) config.Moniker = moniker genesisState, genValidator := app.DefaultState(config.Moniker, pubKey) appState, err = codec.MarshalJSONIndent(cdc, genesisState) @@ -157,9 +170,8 @@ func writeGenesisFile(cdc *codec.Codec, genesisFile, chainID string, validators } // read of create the private key file for this config -func readOrCreatePrivValidator(tmConfig *cfg.Config) crypto.PubKey { +func readOrCreatePrivValidator(privValFile string) crypto.PubKey { // private validator - privValFile := tmConfig.PrivValidatorFile() var privValidator *privval.FilePV if common.FileExists(privValFile) { privValidator = privval.LoadFilePV(privValFile) From 8afc1aef1aa75e843029353cea980371221ecda3 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Mon, 15 Oct 2018 17:44:58 -0700 Subject: [PATCH 31/83] Refactoring test_helpers --- client/lcd/test_helpers.go | 89 +++++++++++++++++++++++--------------- cmd/gaia/app/app.go | 2 +- x/auth/ante.go | 2 +- 3 files changed, 56 insertions(+), 37 deletions(-) diff --git a/client/lcd/test_helpers.go b/client/lcd/test_helpers.go index 399e0b4b62fb..b18a8234b4a4 100644 --- a/client/lcd/test_helpers.go +++ b/client/lcd/test_helpers.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/json" "fmt" + "github.com/cosmos/cosmos-sdk/x/stake" "io/ioutil" "net" "net/http" @@ -14,7 +15,7 @@ import ( "testing" "github.com/cosmos/cosmos-sdk/client" - keys "github.com/cosmos/cosmos-sdk/client/keys" + "github.com/cosmos/cosmos-sdk/client/keys" gapp "github.com/cosmos/cosmos-sdk/cmd/gaia/app" "github.com/cosmos/cosmos-sdk/codec" crkeys "github.com/cosmos/cosmos-sdk/crypto/keys" @@ -22,7 +23,6 @@ import ( "github.com/cosmos/cosmos-sdk/tests" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth" - stake "github.com/cosmos/cosmos-sdk/x/stake/types" "github.com/spf13/viper" "github.com/stretchr/testify/require" @@ -40,6 +40,7 @@ import ( "github.com/tendermint/tendermint/proxy" tmrpc "github.com/tendermint/tendermint/rpc/lib/server" tmtypes "github.com/tendermint/tendermint/types" + txbuilder "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder" ) // makePathname creates a unique pathname for each test. It will panic if it @@ -205,43 +206,61 @@ func InitializeTestLCD( cdc = gapp.MakeCodec() genesisFile := config.GenesisFile() + println(genesisFile) genDoc, err := tmtypes.GenesisDocFromFile(genesisFile) - require.NoError(t, err) - - // append initial (proposing) validator - genDoc.Validators[0] = tmtypes.GenesisValidator{ - PubKey: privVal.GetPubKey(), - Power: 100, // create enough power to enable 2/3 voting power - Name: "validator-1", + genDoc.Validators = nil + + operPrivKey := ed25519.GenPrivKey() + operAddr := operPrivKey.PubKey().Address() + msg := stake.NewMsgCreateValidator( + sdk.ValAddress(operAddr), + privVal.PubKey, + sdk.NewCoin("steak", sdk.NewInt(1)), + stake.Description{Moniker: fmt.Sprintf("validator-%d", 0)}, + stake.NewCommissionMsg(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()), + ) + stdSignMsg := txbuilder.StdSignMsg{ + ChainID: genDoc.ChainID, + Msgs: []sdk.Msg{msg}, } + sig, err := operPrivKey.Sign(stdSignMsg.Bytes()) + require.Nil(t, err) + tx := auth.NewStdTx([]sdk.Msg{msg}, auth.StdFee{}, []auth.StdSignature{{Signature: sig, PubKey: operPrivKey.PubKey()}}, "") + txBytes, err := cdc.MarshalJSON(tx) + require.Nil(t, err) +// fee := auth.StdFee{} + // append initial (proposing) validator + genTxs := []json.RawMessage{txBytes} // append any additional (non-proposing) validators - for i := 1; i < nValidators; i++ { - genDoc.Validators = append(genDoc.Validators, - tmtypes.GenesisValidator{ - PubKey: ed25519.GenPrivKey().PubKey(), - Power: 1, - Name: fmt.Sprintf("validator-%d", i+1), - }, - ) - } - - //var validatorsPKs []crypto.PubKey - - // NOTE: It's bad practice to reuse public key address for the operator - // address but doing in the test for simplicity. - var appGenTxs []auth.StdTx - for _, gdValidator := range genDoc.Validators { - //operAddr := ed25519.GenPrivKey().PubKey().Address() - pk := gdValidator.PubKey - - desc := stake.NewDescription("test_val1", "", "", "") - msg := stake.NewMsgCreateValidator(sdk.ValAddress(pk.Address()), pk, sdk.NewInt64Coin("stake", 100), desc, stake.CommissionMsg{}) - appGenTx := auth.NewStdTx([]sdk.Msg{msg}, auth.StdFee{}, nil, "") - appGenTxs = append(appGenTxs, appGenTx) - } - - genesisState, err := gapp.NewTestGaiaAppGenState(cdc, []json.RawMessage{}, genDoc.Validators, valOperAddrs) + //for i:=0 ; i 0 { if len(req.Validators) != len(validators) { - panic(fmt.Errorf("len(RequestInitChain.Validators) != len(validators): ")) + panic(fmt.Errorf("len(RequestInitChain.Validators) != len(validators) (%d != %d) ", len(req.Validators), len(validators))) } } diff --git a/x/auth/ante.go b/x/auth/ante.go index a0fe71b3c33a..8a10a0239b23 100644 --- a/x/auth/ante.go +++ b/x/auth/ante.go @@ -292,7 +292,7 @@ func ensureSufficientMempoolFees(ctx sdk.Context, stdTx StdTx) sdk.Result { func setGasMeter(simulate bool, ctx sdk.Context, stdTx StdTx) sdk.Context { // set the gas meter - if simulate { + if simulate || ctx.BlockHeight() == 0 { return ctx.WithGasMeter(sdk.NewInfiniteGasMeter()) } return ctx.WithGasMeter(sdk.NewGasMeter(stdTx.Fee.Gas)) From f4c2b394edefff3539d6639b055b99f86d93c4d0 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Mon, 15 Oct 2018 18:30:13 -0700 Subject: [PATCH 32/83] WIP --- client/lcd/lcd_test.go | 2 +- client/lcd/test_helpers.go | 56 +++++++++++++++++++------------------- 2 files changed, 29 insertions(+), 29 deletions(-) diff --git a/client/lcd/lcd_test.go b/client/lcd/lcd_test.go index 7aea16d868c6..c1c01877855d 100644 --- a/client/lcd/lcd_test.go +++ b/client/lcd/lcd_test.go @@ -496,7 +496,7 @@ func TestValidatorsQuery(t *testing.T) { require.Equal(t, 1, len(operAddrs)) validators := getValidators(t, port) - require.Equal(t, len(validators), 1) + require.Equal(t, 1, len(validators), fmt.Sprintf("%+v", validators)) // make sure all the validators were found (order unknown because sorted by operator addr) foundVal := false diff --git a/client/lcd/test_helpers.go b/client/lcd/test_helpers.go index b18a8234b4a4..4ab929e594f2 100644 --- a/client/lcd/test_helpers.go +++ b/client/lcd/test_helpers.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "github.com/cosmos/cosmos-sdk/x/stake" + "github.com/tendermint/tendermint/crypto/secp256k1" "io/ioutil" "net" "net/http" @@ -206,16 +207,16 @@ func InitializeTestLCD( cdc = gapp.MakeCodec() genesisFile := config.GenesisFile() - println(genesisFile) genDoc, err := tmtypes.GenesisDocFromFile(genesisFile) genDoc.Validators = nil + genDoc.SaveAs(genesisFile) - operPrivKey := ed25519.GenPrivKey() + operPrivKey := secp256k1.GenPrivKey() operAddr := operPrivKey.PubKey().Address() msg := stake.NewMsgCreateValidator( sdk.ValAddress(operAddr), privVal.PubKey, - sdk.NewCoin("steak", sdk.NewInt(1)), + sdk.NewCoin("steak", sdk.NewInt(10000000000)), stake.Description{Moniker: fmt.Sprintf("validator-%d", 0)}, stake.NewCommissionMsg(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()), ) @@ -233,31 +234,30 @@ func InitializeTestLCD( genTxs := []json.RawMessage{txBytes} // append any additional (non-proposing) validators - //for i:=0 ; i Date: Tue, 16 Oct 2018 11:14:21 -0700 Subject: [PATCH 33/83] Fix creation of accounts --- cmd/gaia/app/genesis.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/cmd/gaia/app/genesis.go b/cmd/gaia/app/genesis.go index 11d53882ca08..76dbfc70d09c 100644 --- a/cmd/gaia/app/genesis.go +++ b/cmd/gaia/app/genesis.go @@ -108,7 +108,7 @@ func GaiaAppGenState(cdc *codec.Codec, appGenTxs []json.RawMessage) (genesisStat // create the genesis account, give'm few steaks and a buncha token with there name genaccs[i] = genesisAccountFromMsgCreateValidator(msg) - stakeData.Pool.LooseTokens = stakeData.Pool.LooseTokens.Add(sdk.NewDecFromInt(msg.Delegation.Amount)) // increase the supply + stakeData.Pool.LooseTokens = stakeData.Pool.LooseTokens.Add(sdk.NewDecFromInt(freeFermionsAcc)) // increase the supply // add the validator //if len(msg.Description.Moniker) > 0 { @@ -171,7 +171,10 @@ func addValidatorToStakeData(validator stake.Validator, stakeData stake.GenesisS func genesisAccountFromMsgCreateValidator(msg stake.MsgCreateValidator) GenesisAccount { accAuth := auth.NewBaseAccountWithAddress(sdk.AccAddress(msg.ValidatorAddr)) - accAuth.Coins = []sdk.Coin{msg.Delegation} + accAuth.Coins = []sdk.Coin{ + {msg.Description.Moniker + "Token", sdk.NewInt(1000)}, + {"steak", freeFermionsAcc}, + } return NewGenesisAccount(&accAuth) } From 5e70c2e8c7f4d67b0129fceb6618063e615735fd Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Wed, 17 Oct 2018 14:53:58 -0700 Subject: [PATCH 34/83] Fix coins --- Gopkg.lock | 86 ++++++++++++++++++++++++++++++++++++++ client/lcd/test_helpers.go | 2 +- cmd/gaia/init/init.go | 26 +++++++++++- 3 files changed, 112 insertions(+), 2 deletions(-) diff --git a/Gopkg.lock b/Gopkg.lock index 3817fd238e9a..20cc5aaa83c0 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -655,54 +655,140 @@ analyzer-name = "dep" analyzer-version = 1 input-imports = [ + "github.com/ZondaX/hid-go", "github.com/bartekn/go-bip39", + "github.com/beorn7/perks/quantile", "github.com/bgentry/speakeasy", "github.com/btcsuite/btcd/btcec", "github.com/cosmos/go-bip39", "github.com/golang/protobuf/proto", + "github.com/golang/protobuf/ptypes", + "github.com/golang/protobuf/ptypes/any", + "github.com/golang/protobuf/ptypes/duration", + "github.com/golang/protobuf/ptypes/timestamp", + "github.com/golang/snappy", + "github.com/gorilla/context", "github.com/gorilla/mux", + "github.com/gorilla/websocket", + "github.com/hashicorp/hcl", + "github.com/hashicorp/hcl/hcl/ast", + "github.com/hashicorp/hcl/hcl/parser", + "github.com/hashicorp/hcl/hcl/scanner", + "github.com/hashicorp/hcl/hcl/strconv", + "github.com/hashicorp/hcl/hcl/token", + "github.com/hashicorp/hcl/json/parser", + "github.com/hashicorp/hcl/json/scanner", + "github.com/hashicorp/hcl/json/token", + "github.com/inconshreveable/mousetrap", + "github.com/jmhodges/levigo", + "github.com/kr/logfmt", + "github.com/magiconair/properties", "github.com/mattn/go-isatty", + "github.com/matttproud/golang_protobuf_extensions/pbutil", "github.com/mitchellh/go-homedir", + "github.com/mitchellh/mapstructure", "github.com/pelletier/go-toml", "github.com/pkg/errors", + "github.com/pmezard/go-difflib/difflib", + "github.com/prometheus/client_golang/prometheus", + "github.com/prometheus/client_golang/prometheus/promhttp", + "github.com/prometheus/client_model/go", + "github.com/prometheus/common/expfmt", + "github.com/prometheus/common/internal/bitbucket.org/ww/goautoneg", + "github.com/prometheus/common/model", + "github.com/prometheus/procfs", + "github.com/prometheus/procfs/internal/util", + "github.com/prometheus/procfs/nfs", + "github.com/prometheus/procfs/xfs", "github.com/rakyll/statik/fs", + "github.com/rcrowley/go-metrics", + "github.com/spf13/afero", + "github.com/spf13/afero/mem", + "github.com/spf13/cast", "github.com/spf13/cobra", + "github.com/spf13/jwalterweatherman", "github.com/spf13/pflag", "github.com/spf13/viper", "github.com/stretchr/testify/assert", "github.com/stretchr/testify/require", + "github.com/syndtr/goleveldb/leveldb", + "github.com/syndtr/goleveldb/leveldb/cache", + "github.com/syndtr/goleveldb/leveldb/comparer", + "github.com/syndtr/goleveldb/leveldb/errors", + "github.com/syndtr/goleveldb/leveldb/filter", + "github.com/syndtr/goleveldb/leveldb/iterator", + "github.com/syndtr/goleveldb/leveldb/journal", + "github.com/syndtr/goleveldb/leveldb/memdb", + "github.com/syndtr/goleveldb/leveldb/opt", + "github.com/syndtr/goleveldb/leveldb/storage", + "github.com/syndtr/goleveldb/leveldb/table", + "github.com/syndtr/goleveldb/leveldb/util", + "github.com/tendermint/btcd/btcec", + "github.com/tendermint/ed25519", + "github.com/tendermint/ed25519/edwards25519", + "github.com/tendermint/ed25519/extra25519", "github.com/tendermint/go-amino", "github.com/tendermint/iavl", + "github.com/tendermint/tendermint/abci/client", + "github.com/tendermint/tendermint/abci/example/code", + "github.com/tendermint/tendermint/abci/example/kvstore", "github.com/tendermint/tendermint/abci/server", "github.com/tendermint/tendermint/abci/types", + "github.com/tendermint/tendermint/blockchain", "github.com/tendermint/tendermint/cmd/tendermint/commands", "github.com/tendermint/tendermint/config", + "github.com/tendermint/tendermint/consensus", + "github.com/tendermint/tendermint/consensus/types", "github.com/tendermint/tendermint/crypto", "github.com/tendermint/tendermint/crypto/armor", "github.com/tendermint/tendermint/crypto/ed25519", "github.com/tendermint/tendermint/crypto/encoding/amino", "github.com/tendermint/tendermint/crypto/merkle", + "github.com/tendermint/tendermint/crypto/multisig", + "github.com/tendermint/tendermint/crypto/multisig/bitarray", "github.com/tendermint/tendermint/crypto/secp256k1", "github.com/tendermint/tendermint/crypto/tmhash", "github.com/tendermint/tendermint/crypto/xsalsa20symmetric", + "github.com/tendermint/tendermint/evidence", + "github.com/tendermint/tendermint/libs/autofile", "github.com/tendermint/tendermint/libs/bech32", "github.com/tendermint/tendermint/libs/cli", "github.com/tendermint/tendermint/libs/cli/flags", + "github.com/tendermint/tendermint/libs/clist", "github.com/tendermint/tendermint/libs/common", "github.com/tendermint/tendermint/libs/db", + "github.com/tendermint/tendermint/libs/errors", + "github.com/tendermint/tendermint/libs/events", + "github.com/tendermint/tendermint/libs/flowrate", "github.com/tendermint/tendermint/libs/log", + "github.com/tendermint/tendermint/libs/pubsub", + "github.com/tendermint/tendermint/libs/pubsub/query", "github.com/tendermint/tendermint/lite", + "github.com/tendermint/tendermint/lite/client", "github.com/tendermint/tendermint/lite/errors", "github.com/tendermint/tendermint/lite/proxy", + "github.com/tendermint/tendermint/mempool", "github.com/tendermint/tendermint/node", "github.com/tendermint/tendermint/p2p", + "github.com/tendermint/tendermint/p2p/conn", + "github.com/tendermint/tendermint/p2p/pex", + "github.com/tendermint/tendermint/p2p/upnp", "github.com/tendermint/tendermint/privval", "github.com/tendermint/tendermint/proxy", "github.com/tendermint/tendermint/rpc/client", + "github.com/tendermint/tendermint/rpc/core", "github.com/tendermint/tendermint/rpc/core/types", + "github.com/tendermint/tendermint/rpc/grpc", + "github.com/tendermint/tendermint/rpc/lib", "github.com/tendermint/tendermint/rpc/lib/client", "github.com/tendermint/tendermint/rpc/lib/server", + "github.com/tendermint/tendermint/rpc/lib/types", + "github.com/tendermint/tendermint/state", + "github.com/tendermint/tendermint/state/txindex", + "github.com/tendermint/tendermint/state/txindex/kv", + "github.com/tendermint/tendermint/state/txindex/null", "github.com/tendermint/tendermint/types", + "github.com/tendermint/tendermint/types/time", "github.com/tendermint/tendermint/version", "github.com/zondax/ledger-goclient", "golang.org/x/crypto/bcrypt", diff --git a/client/lcd/test_helpers.go b/client/lcd/test_helpers.go index 4ab929e594f2..1a0f3bd8722d 100644 --- a/client/lcd/test_helpers.go +++ b/client/lcd/test_helpers.go @@ -216,7 +216,7 @@ func InitializeTestLCD( msg := stake.NewMsgCreateValidator( sdk.ValAddress(operAddr), privVal.PubKey, - sdk.NewCoin("steak", sdk.NewInt(10000000000)), + sdk.NewCoin("steak", sdk.NewInt(50)), stake.Description{Moniker: fmt.Sprintf("validator-%d", 0)}, stake.NewCommissionMsg(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()), ) diff --git a/cmd/gaia/init/init.go b/cmd/gaia/init/init.go index b96413388c16..5f95461dc830 100644 --- a/cmd/gaia/init/init.go +++ b/cmd/gaia/init/init.go @@ -3,6 +3,8 @@ package init import ( "encoding/json" "fmt" + "github.com/cosmos/cosmos-sdk/client/context" + "github.com/cosmos/cosmos-sdk/client/keys" "github.com/cosmos/cosmos-sdk/cmd/gaia/app" "github.com/cosmos/cosmos-sdk/x/auth" "github.com/tendermint/tendermint/crypto" @@ -20,6 +22,8 @@ import ( "github.com/tendermint/tendermint/libs/common" "github.com/tendermint/tendermint/p2p" "github.com/tendermint/tendermint/types" + authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli" + authtxb "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder" ) const ( @@ -28,6 +32,8 @@ const ( flagMoniker = "moniker" flagOverwrite = "overwrite" flagEnsurePrivValNodeKey = "ensure-privval-nodekey" + flagClientHome = "home-client" + flagOWK = "owk" ) @@ -76,6 +82,8 @@ func InitCmd(ctx *server.Context, cdc *codec.Codec, appInit server.AppInit) *cob cmd.Flags().Bool(flagWithTxs, false, "apply existing genesis transactions from [--home]/config/gentx/") cmd.Flags().String(flagMoniker, "", "moniker") cmd.Flags().Bool(flagEnsurePrivValNodeKey, false, "Ensure priv_validator.json and node_key.json files are created and exit") + cmd.Flags().String(flagClientHome, "", "client's home directory") + cmd.Flags().Bool(flagOWK, false, "overwrite client's keys") return cmd } @@ -134,7 +142,23 @@ func initWithConfig(cdc *codec.Codec, appInit server.AppInit, config *cfg.Config var genesisState app.GenesisState pubKey := readOrCreatePrivValidator(config.PrivValidatorFile()) config.Moniker = moniker - genesisState, genValidator := app.DefaultState(config.Moniker, pubKey) + ip, err := server.ExternalIP() + if err != nil { + return + } + txBldr := authtxb.NewTxBuilderFromCLI().WithCodec(cdc) + cliCtx := context.NewCLIContext().WithCodec(cdc).WithAccountDecoder(authcmd.GetAccountDecoder(cdc)) + clientRoot := viper.GetString(flagClientHome) + addr, secret, err := server.GenerateSaveCoinKey(clientRoot, moniker, server.DefaultKeyPass, viper.GetBool(flagOWK)) + if err != nil { + return + } + appMessage, err = json.Marshal(map[string]string{"secret": secret}) + if err != nil { + return + } + + genesisState, genValidator := app.DefaultState(cliCtx.Codec, config.Moniker, pubKey, server.DefaultKeyPass) appState, err = codec.MarshalJSONIndent(cdc, genesisState) if err != nil { return From 0901e036145a6ff941a3fc7bebfc168d1dd2105e Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Tue, 16 Oct 2018 12:45:55 -0700 Subject: [PATCH 35/83] Reintroduce gaiad testnet --- cmd/gaia/cmd/gaiad/main.go | 2 +- cmd/gaia/init/init.go | 60 ++++---- cmd/gaia/init/testnet.go | 282 +++++++++++++++++++------------------ 3 files changed, 177 insertions(+), 167 deletions(-) diff --git a/cmd/gaia/cmd/gaiad/main.go b/cmd/gaia/cmd/gaiad/main.go index 7d2c68f4d815..adfb12d5735d 100644 --- a/cmd/gaia/cmd/gaiad/main.go +++ b/cmd/gaia/cmd/gaiad/main.go @@ -31,7 +31,7 @@ func main() { } appInit := app.GaiaAppInit() rootCmd.AddCommand(gaiaInit.InitCmd(ctx, cdc, appInit)) - //rootCmd.AddCommand(gaiaInit.TestnetFilesCmd(ctx, cdc, appInit)) + rootCmd.AddCommand(gaiaInit.TestnetFilesCmd(ctx, cdc, appInit)) server.AddCommands(ctx, cdc, rootCmd, appInit, newApp, exportAppStateAndTMValidators) diff --git a/cmd/gaia/init/init.go b/cmd/gaia/init/init.go index 5f95461dc830..34e4aa1cd961 100644 --- a/cmd/gaia/init/init.go +++ b/cmd/gaia/init/init.go @@ -3,8 +3,6 @@ package init import ( "encoding/json" "fmt" - "github.com/cosmos/cosmos-sdk/client/context" - "github.com/cosmos/cosmos-sdk/client/keys" "github.com/cosmos/cosmos-sdk/cmd/gaia/app" "github.com/cosmos/cosmos-sdk/x/auth" "github.com/tendermint/tendermint/crypto" @@ -12,9 +10,9 @@ import ( "os" "path/filepath" - sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/server" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/spf13/cobra" "github.com/spf13/viper" cfg "github.com/tendermint/tendermint/config" @@ -22,8 +20,6 @@ import ( "github.com/tendermint/tendermint/libs/common" "github.com/tendermint/tendermint/p2p" "github.com/tendermint/tendermint/types" - authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli" - authtxb "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder" ) const ( @@ -56,7 +52,7 @@ func InitCmd(ctx *server.Context, cdc *codec.Codec, appInit server.AppInit) *cob moniker := viper.GetString(flagMoniker) withTxs := viper.GetBool(flagWithTxs) overwriteGenesis := viper.GetBool(flagOverwrite) - nodeID, appMessage, err := initWithConfig(cdc, appInit, config, chainID, moniker, genTxsDir, withTxs, overwriteGenesis) + nodeID, appMessage, err := initWithConfig(cdc, config, chainID, moniker, genTxsDir, withTxs, overwriteGenesis) // print out some key information toPrint := struct { @@ -87,7 +83,7 @@ func InitCmd(ctx *server.Context, cdc *codec.Codec, appInit server.AppInit) *cob return cmd } -func initWithConfig(cdc *codec.Codec, appInit server.AppInit, config *cfg.Config, chainID, moniker, genTxsDir string, withGenTxs, overwriteGenesis bool) ( +func initWithConfig(cdc *codec.Codec, config *cfg.Config, chainID, moniker, genTxsDir string, withGenTxs, overwriteGenesis bool) ( nodeID string, appMessage json.RawMessage, err error) { nodeKey, err := p2p.LoadOrGenNodeKey(config.NodeKeyFile()) if err != nil { @@ -139,31 +135,31 @@ func initWithConfig(cdc *codec.Codec, appInit server.AppInit, config *cfg.Config return } } else { - var genesisState app.GenesisState - pubKey := readOrCreatePrivValidator(config.PrivValidatorFile()) - config.Moniker = moniker - ip, err := server.ExternalIP() - if err != nil { - return - } - txBldr := authtxb.NewTxBuilderFromCLI().WithCodec(cdc) - cliCtx := context.NewCLIContext().WithCodec(cdc).WithAccountDecoder(authcmd.GetAccountDecoder(cdc)) - clientRoot := viper.GetString(flagClientHome) - addr, secret, err := server.GenerateSaveCoinKey(clientRoot, moniker, server.DefaultKeyPass, viper.GetBool(flagOWK)) - if err != nil { - return - } - appMessage, err = json.Marshal(map[string]string{"secret": secret}) - if err != nil { - return - } - - genesisState, genValidator := app.DefaultState(cliCtx.Codec, config.Moniker, pubKey, server.DefaultKeyPass) - appState, err = codec.MarshalJSONIndent(cdc, genesisState) - if err != nil { - return - } - validators = []types.GenesisValidator{genValidator} + //var genesisState app.GenesisState + //pubKey := readOrCreatePrivValidator(config.PrivValidatorFile()) + //config.Moniker = moniker + //_, err := server.ExternalIP() + //if err != nil { + // return + //} + //txBldr := authtxb.NewTxBuilderFromCLI().WithCodec(cdc) + //cliCtx := context.NewCLIContext().WithCodec(cdc).WithAccountDecoder(authcmd.GetAccountDecoder(cdc)) + //clientRoot := viper.GetString(flagClientHome) + //addr, secret, err := server.GenerateSaveCoinKey(clientRoot, moniker, server.DefaultKeyPass, viper.GetBool(flagOWK)) + //if err != nil { + // return + //} + //appMessage, err = json.Marshal(map[string]string{"secret": secret}) + //if err != nil { + // return + //} + // + //genesisState, genValidator := app.DefaultState(cliCtx.Codec, config.Moniker, pubKey, server.DefaultKeyPass) + //appState, err = codec.MarshalJSONIndent(cdc, genesisState) + //if err != nil { + // return + //} + //validators = []types.GenesisValidator{genValidator} } cfg.WriteConfigFile(filepath.Join(config.RootDir, "config", "config.toml"), config) diff --git a/cmd/gaia/init/testnet.go b/cmd/gaia/init/testnet.go index fcc20b2d92e0..35a740625989 100644 --- a/cmd/gaia/init/testnet.go +++ b/cmd/gaia/init/testnet.go @@ -2,17 +2,20 @@ package init import ( "fmt" + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/auth" + authtx "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder" + "github.com/cosmos/cosmos-sdk/x/stake" + "github.com/tendermint/tendermint/p2p" "net" -// "os" + "os" "path/filepath" "github.com/cosmos/cosmos-sdk/server" - -// "github.com/spf13/cobra" - -// "github.com/cosmos/cosmos-sdk/codec" + "github.com/spf13/cobra" "github.com/spf13/viper" -// cfg "github.com/tendermint/tendermint/config" + cfg "github.com/tendermint/tendermint/config" cmn "github.com/tendermint/tendermint/libs/common" ) @@ -27,134 +30,145 @@ var ( ) const nodeDirPerm = 0755 -// -//// get cmd to initialize all files for tendermint testnet and application -//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", -// Long: `testnet will create "v" number of directories and populate each with -//necessary files (private validator, genesis, config, etc.). -// -//Note, strict routability for addresses is turned off in the config file. -// -//Example: -// -// gaiad testnet --v 4 --o ./output --starting-ip-address 192.168.10.2 -// `, -// RunE: func(_ *cobra.Command, _ []string) error { -// /* -// config := ctx.Config -// err := testnetWithConfig(config, cdc, appInit) -// return err -// */ -// return nil -// }, -// } -// cmd.Flags().Int(nValidators, 4, -// "Number of validators to initialize the testnet with") -// cmd.Flags().StringP(outputDir, "o", "./mytestnet", -// "Directory to store initialization data for the testnet") -// cmd.Flags().String(nodeDirPrefix, "node", -// "Prefix the directory name for each node with (node results in node0, node1, ...)") -// cmd.Flags().String(nodeDaemonHome, "gaiad", -// "Home directory of the node's daemon configuration") -// cmd.Flags().String(nodeCliHome, "gaiacli", -// "Home directory of the node's cli configuration") -// -// cmd.Flags().String(startingIPAddress, "192.168.0.1", -// "Starting IP address (192.168.0.1 results in persistent peers list ID0@192.168.0.1:46656, ID1@192.168.0.2:46656, ...)") -// return cmd -//} -// -//func testnetWithConfig(config *cfg.Config, cdc *codec.Codec, appInit server.AppInit) error { -// outDir := viper.GetString(outputDir) -// numValidators := viper.GetInt(nValidators) -// -// // Generate private key, node ID, initial transaction -// for i := 0; i < numValidators; i++ { -// nodeDirName := fmt.Sprintf("%s%d", viper.GetString(nodeDirPrefix), i) -// nodeDaemonHomeName := viper.GetString(nodeDaemonHome) -// nodeCliHomeName := viper.GetString(nodeCliHome) -// nodeDir := filepath.Join(outDir, nodeDirName, nodeDaemonHomeName) -// clientDir := filepath.Join(outDir, nodeDirName, nodeCliHomeName) -// gentxsDir := filepath.Join(outDir, "gentxs") -// config.SetRoot(nodeDir) -// -// err := os.MkdirAll(filepath.Join(nodeDir, "config"), nodeDirPerm) -// if err != nil { -// _ = os.RemoveAll(outDir) -// return err -// } -// -// err = os.MkdirAll(clientDir, nodeDirPerm) -// if err != nil { -// _ = os.RemoveAll(outDir) -// return err -// } -// -// config.Moniker = nodeDirName -// ip, err := getIP(i) -// if err != nil { -// return err -// } -// -// genTxConfig := gc.GenTx{ -// nodeDirName, -// clientDir, -// true, -// ip, -// } -// -// // Run `init gen-tx` and generate initial transactions -// cliPrint, genTxFile, err := gentxWithConfig(cdc, appInit, config, genTxConfig) -// if err != nil { -// return err -// } -// -// // Save private key seed words -// name := fmt.Sprintf("%v.json", "key_seed") -// err = writeFile(name, clientDir, cliPrint) -// if err != nil { -// return err -// } -// -// // Gather gentxs folder -// name = fmt.Sprintf("%v.json", nodeDirName) -// err = writeFile(name, gentxsDir, genTxFile) -// if err != nil { -// return err -// } -// } -// -// // Generate genesis.json and config.toml -// chainID := "chain-" + cmn.RandStr(6) -// for i := 0; i < numValidators; i++ { -// -// nodeDirName := fmt.Sprintf("%s%d", viper.GetString(nodeDirPrefix), i) -// nodeDaemonHomeName := viper.GetString(nodeDaemonHome) -// nodeDir := filepath.Join(outDir, nodeDirName, nodeDaemonHomeName) -// gentxsDir := filepath.Join(outDir, "gentxs") -// initConfig := server.InitConfig{ -// chainID, -// true, -// gentxsDir, -// "test", -// true, -// } -// config.Moniker = nodeDirName -// config.SetRoot(nodeDir) -// -// // Run `init` and generate genesis.json and config.toml -// _, _, _, err := initWithConfig(cdc, appInit, config, initConfig) -// if err != nil { -// return err -// } -// } -// -// fmt.Printf("Successfully initialized %v node directories\n", viper.GetInt(nValidators)) -// return nil -//} + +// get cmd to initialize all files for tendermint testnet and application +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", + Long: `testnet will create "v" number of directories and populate each with +necessary files (private validator, genesis, config, etc.). + +Note, strict routability for addresses is turned off in the config file. + +Example: + + gaiad testnet --v 4 --o ./output --starting-ip-address 192.168.10.2 + `, + RunE: func(_ *cobra.Command, _ []string) error { + config := ctx.Config + err := testnetWithConfig(config, cdc, appInit) + return err + return nil + }, + } + cmd.Flags().Int(nValidators, 4, + "Number of validators to initialize the testnet with") + cmd.Flags().StringP(outputDir, "o", "./mytestnet", + "Directory to store initialization data for the testnet") + cmd.Flags().String(nodeDirPrefix, "node", + "Prefix the directory name for each node with (node results in node0, node1, ...)") + cmd.Flags().String(nodeDaemonHome, "gaiad", + "Home directory of the node's daemon configuration") + cmd.Flags().String(nodeCliHome, "gaiacli", + "Home directory of the node's cli configuration") + + cmd.Flags().String(startingIPAddress, "192.168.0.1", + "Starting IP address (192.168.0.1 results in persistent peers list ID0@192.168.0.1:46656, ID1@192.168.0.2:46656, ...)") + return cmd +} + +func testnetWithConfig(config *cfg.Config, cdc *codec.Codec, appInit server.AppInit) error { + outDir := viper.GetString(outputDir) + numValidators := viper.GetInt(nValidators) + + // Generate genesis.json and config.toml + chainID := "chain-" + cmn.RandStr(6) + monikers := make([]string, numValidators) + + // Generate private key, node ID, initial transaction + for i := 0; i < numValidators; i++ { + nodeDirName := fmt.Sprintf("%s%d", viper.GetString(nodeDirPrefix), i) + nodeDaemonHomeName := viper.GetString(nodeDaemonHome) + nodeCliHomeName := viper.GetString(nodeCliHome) + nodeDir := filepath.Join(outDir, nodeDirName, nodeDaemonHomeName) + clientDir := filepath.Join(outDir, nodeDirName, nodeCliHomeName) + gentxsDir := filepath.Join(outDir, "gentxs") + config.SetRoot(nodeDir) + + err := os.MkdirAll(filepath.Join(nodeDir, "config"), nodeDirPerm) + if err != nil { + _ = os.RemoveAll(outDir) + return err + } + + err = os.MkdirAll(clientDir, nodeDirPerm) + if err != nil { + _ = os.RemoveAll(outDir) + return err + } + + monikers = append(monikers, nodeDirName) + config.Moniker = nodeDirName + ip, err := getIP(i) + if err != nil { + _ = os.RemoveAll(outDir) + return err + } + nodeKey, err := p2p.LoadOrGenNodeKey(config.NodeKeyFile()) + if err != nil { + _ = os.RemoveAll(outDir) + return err + } + nodeID := string(nodeKey.ID()) + memo := fmt.Sprintf("%s@%s:26656", nodeID, ip) + + addr, _, err := server.GenerateSaveCoinKey(clientDir, nodeDirName, server.DefaultKeyPass, true) + if err != nil { + _ = os.RemoveAll(outDir) + return err + } + + valPubKey := readOrCreatePrivValidator(config.PrivValidatorFile()) + msg := stake.NewMsgCreateValidator( + sdk.ValAddress(addr), + valPubKey, + sdk.NewInt64Coin("steak", 100), + stake.NewDescription(nodeDir, "", "", ""), + stake.NewCommissionMsg(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()), + ) + tx := auth.NewStdTx([]sdk.Msg{msg}, auth.StdFee{}, []auth.StdSignature{}, memo) + txBldr := authtx.NewTxBuilderFromCLI().WithChainID(chainID).WithMemo(memo) + signedTx, err := txBldr.SignStdTx(nodeDirName, server.DefaultKeyPass, tx, false) + if err != nil { + _ = os.RemoveAll(outDir) + return err + } + + txBytes, err := cdc.MarshalJSON(signedTx) + if err != nil { + _ = os.RemoveAll(outDir) + return err + } + + // Gather gentxs folder + name := fmt.Sprintf("%v.json", nodeDirName) + err = writeFile(name, gentxsDir, txBytes) + if err != nil { + return err + } + } + + for i := 0; i < numValidators; i++ { + + nodeDirName := fmt.Sprintf("%s%d", viper.GetString(nodeDirPrefix), i) + nodeDaemonHomeName := viper.GetString(nodeDaemonHome) + nodeDir := filepath.Join(outDir, nodeDirName, nodeDaemonHomeName) + gentxsDir := filepath.Join(outDir, "gentxs") + moniker := monikers[i] + config.Moniker = nodeDirName + config.SetRoot(nodeDir) + + // Run `init` and generate genesis.json and config.toml + _, _, err := initWithConfig(cdc, config, chainID, moniker, gentxsDir, true, true) + if err != nil { + return err + } + } + + fmt.Printf("Successfully initialized %v node directories\n", viper.GetInt(nValidators)) + return nil +} func getIP(i int) (ip string, err error) { ip = viper.GetString(startingIPAddress) From eca8f1cff429c4df757f350cee4f1531f8553ff5 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Tue, 16 Oct 2018 13:12:45 -0700 Subject: [PATCH 36/83] prompt user for passwords --- cmd/gaia/init/testnet.go | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/cmd/gaia/init/testnet.go b/cmd/gaia/init/testnet.go index 35a740625989..97acff46892b 100644 --- a/cmd/gaia/init/testnet.go +++ b/cmd/gaia/init/testnet.go @@ -1,7 +1,9 @@ package init import ( + "encoding/json" "fmt" + "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth" @@ -113,11 +115,33 @@ func testnetWithConfig(config *cfg.Config, cdc *codec.Codec, appInit server.AppI nodeID := string(nodeKey.ID()) memo := fmt.Sprintf("%s@%s:26656", nodeID, ip) - addr, _, err := server.GenerateSaveCoinKey(clientDir, nodeDirName, server.DefaultKeyPass, true) + keyPass, err := client.GetPassword(prompt, buf) + if err != nil && keyPass != "" { + // An error was returned that either failed to read the password from + // STDIN or the given password is not empty but failed to meet minimum + // length requirements. + return err + } + if keyPass == "" { + keyPass = server.DefaultKeyPass + } + + addr, secret, err := server.GenerateSaveCoinKey(clientDir, nodeDirName, keyPass, true) if err != nil { _ = os.RemoveAll(outDir) return err } + info := map[string]string{"secret": secret} + cliPrint, err := json.Marshal(info) + if err != nil { + return err + } + // Save private key seed words + name := fmt.Sprintf("%v.json", "key_seed") + err = writeFile(name, clientDir, cliPrint) + if err != nil { + return err + } valPubKey := readOrCreatePrivValidator(config.PrivValidatorFile()) msg := stake.NewMsgCreateValidator( From bdac9ac6e6abda0c4d7353d3c423f122dff4f3c8 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Tue, 16 Oct 2018 13:19:22 -0700 Subject: [PATCH 37/83] Fix monikers --- cmd/gaia/init/testnet.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/cmd/gaia/init/testnet.go b/cmd/gaia/init/testnet.go index 97acff46892b..48d1f8fd5ede 100644 --- a/cmd/gaia/init/testnet.go +++ b/cmd/gaia/init/testnet.go @@ -115,6 +115,8 @@ func testnetWithConfig(config *cfg.Config, cdc *codec.Codec, appInit server.AppI nodeID := string(nodeKey.ID()) memo := fmt.Sprintf("%s@%s:26656", nodeID, ip) + buf := client.BufferStdin() + prompt := fmt.Sprintf("Password for account '%s' (default %s):", nodeDirName, server.DefaultKeyPass) keyPass, err := client.GetPassword(prompt, buf) if err != nil && keyPass != "" { // An error was returned that either failed to read the password from @@ -137,8 +139,7 @@ func testnetWithConfig(config *cfg.Config, cdc *codec.Codec, appInit server.AppI return err } // Save private key seed words - name := fmt.Sprintf("%v.json", "key_seed") - err = writeFile(name, clientDir, cliPrint) + err = writeFile(fmt.Sprintf("%v.json", "key_seed"), clientDir, cliPrint) if err != nil { return err } @@ -148,7 +149,7 @@ func testnetWithConfig(config *cfg.Config, cdc *codec.Codec, appInit server.AppI sdk.ValAddress(addr), valPubKey, sdk.NewInt64Coin("steak", 100), - stake.NewDescription(nodeDir, "", "", ""), + stake.NewDescription(nodeDirName, "", "", ""), stake.NewCommissionMsg(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()), ) tx := auth.NewStdTx([]sdk.Msg{msg}, auth.StdFee{}, []auth.StdSignature{}, memo) @@ -166,8 +167,7 @@ func testnetWithConfig(config *cfg.Config, cdc *codec.Codec, appInit server.AppI } // Gather gentxs folder - name := fmt.Sprintf("%v.json", nodeDirName) - err = writeFile(name, gentxsDir, txBytes) + err = writeFile(fmt.Sprintf("%v.json", nodeDirName), gentxsDir, txBytes) if err != nil { return err } From 875ffef5524524fc0fa6b29411aafc86bb3105a7 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Tue, 16 Oct 2018 15:17:44 -0700 Subject: [PATCH 38/83] Fix all lcd_tests --- client/lcd/test_helpers.go | 34 +++++++++------------------------- cmd/gaia/app/app.go | 5 ++--- cmd/gaia/app/genesis.go | 10 +++++----- 3 files changed, 16 insertions(+), 33 deletions(-) diff --git a/client/lcd/test_helpers.go b/client/lcd/test_helpers.go index 1a0f3bd8722d..ce4f72931dfa 100644 --- a/client/lcd/test_helpers.go +++ b/client/lcd/test_helpers.go @@ -210,38 +210,22 @@ func InitializeTestLCD( genDoc, err := tmtypes.GenesisDocFromFile(genesisFile) genDoc.Validators = nil genDoc.SaveAs(genesisFile) - - operPrivKey := secp256k1.GenPrivKey() - operAddr := operPrivKey.PubKey().Address() - msg := stake.NewMsgCreateValidator( - sdk.ValAddress(operAddr), - privVal.PubKey, - sdk.NewCoin("steak", sdk.NewInt(50)), - stake.Description{Moniker: fmt.Sprintf("validator-%d", 0)}, - stake.NewCommissionMsg(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()), - ) - stdSignMsg := txbuilder.StdSignMsg{ - ChainID: genDoc.ChainID, - Msgs: []sdk.Msg{msg}, - } - sig, err := operPrivKey.Sign(stdSignMsg.Bytes()) - require.Nil(t, err) - tx := auth.NewStdTx([]sdk.Msg{msg}, auth.StdFee{}, []auth.StdSignature{{Signature: sig, PubKey: operPrivKey.PubKey()}}, "") - txBytes, err := cdc.MarshalJSON(tx) - require.Nil(t, err) -// fee := auth.StdFee{} - // append initial (proposing) validator - genTxs := []json.RawMessage{txBytes} + genTxs := []json.RawMessage{} // append any additional (non-proposing) validators for i:=0 ; i 0 { + pubKey = ed25519.GenPrivKey().PubKey() + delegation = 1 + } msg := stake.NewMsgCreateValidator( sdk.ValAddress(operAddr), pubKey, - sdk.NewCoin("steak", sdk.NewInt(1)), + sdk.NewCoin("steak", sdk.NewInt(int64(delegation))), stake.Description{Moniker: fmt.Sprintf("validator-%d", i+1)}, stake.NewCommissionMsg(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()), ) @@ -363,7 +347,7 @@ func Request(t *testing.T, port, method, path string, payload []byte) (*http.Res res *http.Response ) url := fmt.Sprintf("http://localhost:%v%v", port, path) - fmt.Println("REQUEST " + method + " " + url) + //fmt.Println("REQUEST " + method + " " + url) req, err := http.NewRequest(method, url, bytes.NewBuffer(payload)) require.Nil(t, err) diff --git a/cmd/gaia/app/app.go b/cmd/gaia/app/app.go index b79a9d57c73b..3cbde281ec3e 100644 --- a/cmd/gaia/app/app.go +++ b/cmd/gaia/app/app.go @@ -3,14 +3,13 @@ package app import ( "encoding/json" "fmt" - "io" - "os" - abci "github.com/tendermint/tendermint/abci/types" cmn "github.com/tendermint/tendermint/libs/common" dbm "github.com/tendermint/tendermint/libs/db" "github.com/tendermint/tendermint/libs/log" tmtypes "github.com/tendermint/tendermint/types" + "io" + "os" bam "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/codec" diff --git a/cmd/gaia/app/genesis.go b/cmd/gaia/app/genesis.go index 76dbfc70d09c..9020ef5bed68 100644 --- a/cmd/gaia/app/genesis.go +++ b/cmd/gaia/app/genesis.go @@ -27,7 +27,7 @@ import ( var ( // bonded tokens given to genesis validators/accounts freeFermionVal = int64(100) - freeFermionsAcc = sdk.NewInt(50) + freeFermionsAcc = sdk.NewInt(150) ) // State to Unmarshal @@ -107,7 +107,7 @@ func GaiaAppGenState(cdc *codec.Codec, appGenTxs []json.RawMessage) (genesisStat msg := msgs[0].(stake.MsgCreateValidator) // create the genesis account, give'm few steaks and a buncha token with there name - genaccs[i] = genesisAccountFromMsgCreateValidator(msg) + genaccs[i] = genesisAccountFromMsgCreateValidator(msg, freeFermionsAcc) stakeData.Pool.LooseTokens = stakeData.Pool.LooseTokens.Add(sdk.NewDecFromInt(freeFermionsAcc)) // increase the supply // add the validator @@ -135,7 +135,7 @@ func DefaultState(moniker string, pubKey crypto.PubKey) (genesisState GenesisSta stakeData := stake.DefaultGenesisState() validator := stake.NewValidator(sdk.ValAddress(acc.Address), pubKey, stake.NewDescription(moniker, "", "", "")) stakeData = addValidatorToStakeData(validator, stakeData) - stakeData.Pool.LooseTokens = stakeData.Pool.LooseTokens.Add(sdk.NewDecFromInt(freeFermionsAcc)) + //stakeData.Pool.LooseTokens = stakeData.Pool.LooseTokens.Add(sdk.NewDecFromInt(freeFermionsAcc)) genesisState = GenesisState{ Accounts: []GenesisAccount{acc}, StakeData: stakeData, @@ -169,11 +169,11 @@ func addValidatorToStakeData(validator stake.Validator, stakeData stake.GenesisS return stakeData } -func genesisAccountFromMsgCreateValidator(msg stake.MsgCreateValidator) GenesisAccount { +func genesisAccountFromMsgCreateValidator(msg stake.MsgCreateValidator, amount sdk.Int) GenesisAccount { accAuth := auth.NewBaseAccountWithAddress(sdk.AccAddress(msg.ValidatorAddr)) accAuth.Coins = []sdk.Coin{ {msg.Description.Moniker + "Token", sdk.NewInt(1000)}, - {"steak", freeFermionsAcc}, + {"steak", amount}, } return NewGenesisAccount(&accAuth) } From df0e4a42c25a7cca484dddd1f676b40d0dd03ec2 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Tue, 16 Oct 2018 15:21:48 -0700 Subject: [PATCH 39/83] Switch tests output back on --- client/lcd/test_helpers.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/lcd/test_helpers.go b/client/lcd/test_helpers.go index ce4f72931dfa..4c87709469f4 100644 --- a/client/lcd/test_helpers.go +++ b/client/lcd/test_helpers.go @@ -347,7 +347,7 @@ func Request(t *testing.T, port, method, path string, payload []byte) (*http.Res res *http.Response ) url := fmt.Sprintf("http://localhost:%v%v", port, path) - //fmt.Println("REQUEST " + method + " " + url) + fmt.Println("REQUEST " + method + " " + url) req, err := http.NewRequest(method, url, bytes.NewBuffer(payload)) require.Nil(t, err) From bf206f3734caaffdd9ea751c379994badcdd080a Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Wed, 17 Oct 2018 14:54:20 -0700 Subject: [PATCH 40/83] Remove test_utils, NewTestGaiaAppGenState is now deprecated --- cmd/gaia/app/test_utils.go | 80 -------------------------------------- 1 file changed, 80 deletions(-) delete mode 100644 cmd/gaia/app/test_utils.go diff --git a/cmd/gaia/app/test_utils.go b/cmd/gaia/app/test_utils.go deleted file mode 100644 index 1baabd76845b..000000000000 --- a/cmd/gaia/app/test_utils.go +++ /dev/null @@ -1,80 +0,0 @@ -package app - -import ( - "encoding/json" - "errors" - - "github.com/cosmos/cosmos-sdk/codec" - sdk "github.com/cosmos/cosmos-sdk/types" - distr "github.com/cosmos/cosmos-sdk/x/distribution" - "github.com/cosmos/cosmos-sdk/x/gov" - "github.com/cosmos/cosmos-sdk/x/slashing" - "github.com/cosmos/cosmos-sdk/x/stake" - tmtypes "github.com/tendermint/tendermint/types" -) - -// NewTestGaiaAppGenState creates the core parameters for a test genesis -// initialization given a set of genesis txs, TM validators and their respective -// operating addresses. -func NewTestGaiaAppGenState( - cdc *codec.Codec, appGenTxs []json.RawMessage, tmVals []tmtypes.GenesisValidator, valOperAddrs []sdk.ValAddress, -) (GenesisState, error) { - - switch { - case len(appGenTxs) == 0: - return GenesisState{}, errors.New("must provide at least genesis transaction") - case len(tmVals) != len(valOperAddrs): - return GenesisState{}, errors.New("number of TM validators does not match number of operator addresses") - } - - // start with the default staking genesis state - stakeData := stake.DefaultGenesisState() - - // get genesis account information - genAccs := make([]GenesisAccount, len(appGenTxs)) - //for _, appGenTx := range appGenTxs { - // - // var genTx GaiaGenTx - // if err := cdc.UnmarshalJSON(appGenTx, &genTx); err != nil { - // return GenesisState{}, err - // } - // - // stakeData.Pool.LooseTokens = stakeData.Pool.LooseTokens.Add(sdk.NewDecFromInt(freeFermionsAcc)) - // - // // create the genesis account for the given genesis tx - // //genAccs[i] = genesisAccountFromGenTx(genTx) - //} - - for i, tmVal := range tmVals { - var issuedDelShares sdk.Dec - - // increase total supply by validator's power - power := sdk.NewInt(tmVal.Power) - stakeData.Pool.LooseTokens = stakeData.Pool.LooseTokens.Add(sdk.NewDecFromInt(power)) - - // add the validator - desc := stake.NewDescription(tmVal.Name, "", "", "") - validator := stake.NewValidator(valOperAddrs[i], tmVal.PubKey, desc) - - validator, stakeData.Pool, issuedDelShares = validator.AddTokensFromDel(stakeData.Pool, power) - stakeData.Validators = append(stakeData.Validators, validator) - - // create the self-delegation from the issuedDelShares - selfDel := stake.Delegation{ - DelegatorAddr: sdk.AccAddress(validator.OperatorAddr), - ValidatorAddr: validator.OperatorAddr, - Shares: issuedDelShares, - Height: 0, - } - - stakeData.Bonds = append(stakeData.Bonds, selfDel) - } - - return GenesisState{ - Accounts: genAccs, - StakeData: stakeData, - DistrData: distr.DefaultGenesisState(), - SlashingData: slashing.DefaultGenesisState(), - GovData: gov.DefaultGenesisState(), - }, nil -} From 3e81151fdd6cb9fc683ce47b06fa44b7b118ab3d Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Tue, 16 Oct 2018 16:36:22 -0700 Subject: [PATCH 41/83] Fix all things :) --- client/lcd/lcd_test.go | 2 +- client/lcd/test_helpers.go | 1 - cmd/gaia/init/init.go | 141 ++++++++++++++++++++++--------------- cmd/gaia/init/testnet.go | 10 ++- 4 files changed, 96 insertions(+), 58 deletions(-) diff --git a/client/lcd/lcd_test.go b/client/lcd/lcd_test.go index c1c01877855d..c114a070b73c 100644 --- a/client/lcd/lcd_test.go +++ b/client/lcd/lcd_test.go @@ -566,7 +566,7 @@ func TestBonding(t *testing.T) { // create unbond TX resultTx = doBeginUnbonding(t, port, seed, name, password, addr, operAddrs[0], 60) - tests.WaitForHeight(resultTx.Height+1, port) + tests.WaitForHeight(resultTx.Height+2, port) require.Equal(t, uint32(0), resultTx.CheckTx.Code) require.Equal(t, uint32(0), resultTx.DeliverTx.Code) diff --git a/client/lcd/test_helpers.go b/client/lcd/test_helpers.go index 4c87709469f4..a107d9246edf 100644 --- a/client/lcd/test_helpers.go +++ b/client/lcd/test_helpers.go @@ -243,7 +243,6 @@ func InitializeTestLCD( valOperAddrs = append(valOperAddrs, sdk.ValAddress(operAddr)) } - //genesisState, err := gapp.NewTestGaiaAppGenState(cdc, nil, genDoc.Validators, valOperAddrs) genesisState, err := gapp.GaiaAppGenState(cdc, genTxs) require.NoError(t, err) diff --git a/cmd/gaia/init/init.go b/cmd/gaia/init/init.go index 34e4aa1cd961..e47950de06ac 100644 --- a/cmd/gaia/init/init.go +++ b/cmd/gaia/init/init.go @@ -3,8 +3,11 @@ package init import ( "encoding/json" "fmt" + "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/cmd/gaia/app" "github.com/cosmos/cosmos-sdk/x/auth" + authtx "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder" + "github.com/cosmos/cosmos-sdk/x/stake" "github.com/tendermint/tendermint/crypto" "github.com/tendermint/tendermint/privval" "os" @@ -27,11 +30,19 @@ const ( flagWithTxs = "with-txs" flagMoniker = "moniker" flagOverwrite = "overwrite" - flagEnsurePrivValNodeKey = "ensure-privval-nodekey" flagClientHome = "home-client" flagOWK = "owk" ) +type InitConfig struct { + ChainID string + GenTxsDir string + Moniker string + ClientHome string + WithTxs bool + Overwrite bool + OverwriteKeys bool +} // get cmd to initialize all files for tendermint and application // nolint: golint @@ -48,11 +59,16 @@ func InitCmd(ctx *server.Context, cdc *codec.Codec, appInit server.AppInit) *cob if chainID == "" { chainID = fmt.Sprintf("test-chain-%v", common.RandStr(6)) } - genTxsDir := filepath.Join(config.RootDir, "config", "gentx") - moniker := viper.GetString(flagMoniker) - withTxs := viper.GetBool(flagWithTxs) - overwriteGenesis := viper.GetBool(flagOverwrite) - nodeID, appMessage, err := initWithConfig(cdc, config, chainID, moniker, genTxsDir, withTxs, overwriteGenesis) + initCfg := InitConfig{ + ChainID: chainID, + GenTxsDir: viper.GetString(filepath.Join(config.RootDir, "config", "gentx")), + Moniker: viper.GetString(flagMoniker), + ClientHome: viper.GetString(flagClientHome), + WithTxs: viper.GetBool(flagWithTxs), + Overwrite: viper.GetBool(flagOverwrite), + OverwriteKeys: viper.GetBool(flagOWK), + } + nodeID, appMessage, err := initWithConfig(cdc, config, initCfg) // print out some key information toPrint := struct { @@ -68,105 +84,120 @@ func InitCmd(ctx *server.Context, cdc *codec.Codec, appInit server.AppInit) *cob if err != nil { return err } - fmt.Println(string(out)) + fmt.Fprintf(os.Stderr, "%s\n", string(out)) return nil }, } + cmd.Flags().String(cli.HomeFlag, app.DefaultNodeHome, "node's home directory") cmd.Flags().BoolP(flagOverwrite, "o", false, "overwrite the genesis.json file") cmd.Flags().String(flagChainID, "", "genesis file chain-id, if left blank will be randomly created") cmd.Flags().Bool(flagWithTxs, false, "apply existing genesis transactions from [--home]/config/gentx/") cmd.Flags().String(flagMoniker, "", "moniker") - cmd.Flags().Bool(flagEnsurePrivValNodeKey, false, "Ensure priv_validator.json and node_key.json files are created and exit") - cmd.Flags().String(flagClientHome, "", "client's home directory") + cmd.Flags().String(flagClientHome, app.DefaultCLIHome, "client's home directory") cmd.Flags().Bool(flagOWK, false, "overwrite client's keys") return cmd } -func initWithConfig(cdc *codec.Codec, config *cfg.Config, chainID, moniker, genTxsDir string, withGenTxs, overwriteGenesis bool) ( +func initWithConfig(cdc *codec.Codec, config *cfg.Config, initCfg InitConfig) ( nodeID string, appMessage json.RawMessage, err error) { nodeKey, err := p2p.LoadOrGenNodeKey(config.NodeKeyFile()) if err != nil { return } nodeID = string(nodeKey.ID()) - if viper.GetBool(flagEnsurePrivValNodeKey) { - privValFile := config.PrivValidatorFile() - if !common.FileExists(privValFile) { - fmt.Fprintf(os.Stderr, "%s does not exist, creating ...\n", privValFile) - } - pk := readOrCreatePrivValidator(privValFile) - fmt.Fprintf(os.Stderr,"validator public key: %s\n", sdk.MustBech32ifyConsPub(pk)) - return - } - - + privValFile := config.PrivValidatorFile() + readOrCreatePrivValidator(privValFile) genFile := config.GenesisFile() - if !overwriteGenesis && common.FileExists(genFile) { + if !initCfg.Overwrite && common.FileExists(genFile) { err = fmt.Errorf("genesis.json file already exists: %v", genFile) return } - // process genesis transactions, or otherwise create one for defaults + // process genesis transactions, else create default genesis.json var appGenTxs []auth.StdTx var persistentPeers string var genTxs []json.RawMessage var appState json.RawMessage - var validators []types.GenesisValidator + var jsonRawTx json.RawMessage + moniker := initCfg.Moniker + chainID := initCfg.ChainID - if withGenTxs { - _, appGenTxs, persistentPeers, err = app.ProcessStdTxs(moniker, genTxsDir, cdc) + if initCfg.WithTxs { + _, appGenTxs, persistentPeers, err = app.ProcessStdTxs(moniker, initCfg.GenTxsDir, cdc) if err != nil { return } genTxs = make([]json.RawMessage, len(appGenTxs)) config.P2P.PersistentPeers = persistentPeers for i, stdTx := range appGenTxs { - var jsonRawTx json.RawMessage jsonRawTx, err = cdc.MarshalJSON(stdTx) if err != nil { return } genTxs[i] = jsonRawTx } + } else { + var ip, keyPass, secret string + var addr sdk.AccAddress + var signedTx auth.StdTx - appState, err = app.GaiaAppGenStateJSON(cdc, genTxs) + config.Moniker = moniker + ip, err = server.ExternalIP() if err != nil { return } - } else { - //var genesisState app.GenesisState - //pubKey := readOrCreatePrivValidator(config.PrivValidatorFile()) - //config.Moniker = moniker - //_, err := server.ExternalIP() - //if err != nil { - // return - //} - //txBldr := authtxb.NewTxBuilderFromCLI().WithCodec(cdc) - //cliCtx := context.NewCLIContext().WithCodec(cdc).WithAccountDecoder(authcmd.GetAccountDecoder(cdc)) - //clientRoot := viper.GetString(flagClientHome) - //addr, secret, err := server.GenerateSaveCoinKey(clientRoot, moniker, server.DefaultKeyPass, viper.GetBool(flagOWK)) - //if err != nil { - // return - //} - //appMessage, err = json.Marshal(map[string]string{"secret": secret}) - //if err != nil { - // return - //} - // - //genesisState, genValidator := app.DefaultState(cliCtx.Codec, config.Moniker, pubKey, server.DefaultKeyPass) - //appState, err = codec.MarshalJSONIndent(cdc, genesisState) - //if err != nil { - // return - //} - //validators = []types.GenesisValidator{genValidator} + memo := fmt.Sprintf("%s@%s:26656", nodeID, ip) + buf := client.BufferStdin() + prompt := fmt.Sprintf("Password for account '%s' (default %s):", moniker, server.DefaultKeyPass) + keyPass, err = client.GetPassword(prompt, buf) + if err != nil && keyPass != "" { + // An error was returned that either failed to read the password from + // STDIN or the given password is not empty but failed to meet minimum + // length requirements. + return + } + if keyPass == "" { + keyPass = server.DefaultKeyPass + } + + addr, secret, err = server.GenerateSaveCoinKey(initCfg.ClientHome, moniker, keyPass, initCfg.OverwriteKeys) + if err != nil { + return + } + appMessage, err = json.Marshal(map[string]string{"secret": secret}) + if err != nil { + return + } + + valPubKey := readOrCreatePrivValidator(config.PrivValidatorFile()) + msg := stake.NewMsgCreateValidator( + sdk.ValAddress(addr), + valPubKey, + sdk.NewInt64Coin("steak", 100), + stake.NewDescription(moniker, "", "", ""), + stake.NewCommissionMsg(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()), + ) + txBldr := authtx.NewTxBuilderFromCLI().WithCodec(cdc).WithMemo(memo).WithChainID(chainID) + signedTx, err = txBldr.SignStdTx( + moniker, keyPass, auth.NewStdTx([]sdk.Msg{msg}, auth.StdFee{}, []auth.StdSignature{}, memo), false, + ) + if err != nil { + return + } + jsonRawTx, err = cdc.MarshalJSON(signedTx) + if err != nil { + return + } + genTxs = []json.RawMessage{jsonRawTx} } cfg.WriteConfigFile(filepath.Join(config.RootDir, "config", "config.toml"), config) - err = writeGenesisFile(cdc, genFile, chainID, validators, appState) + appState, err = app.GaiaAppGenStateJSON(cdc, genTxs) if err != nil { return } + err = writeGenesisFile(cdc, genFile, chainID, nil, appState) return } diff --git a/cmd/gaia/init/testnet.go b/cmd/gaia/init/testnet.go index 48d1f8fd5ede..f1c5a9c23b86 100644 --- a/cmd/gaia/init/testnet.go +++ b/cmd/gaia/init/testnet.go @@ -184,7 +184,15 @@ func testnetWithConfig(config *cfg.Config, cdc *codec.Codec, appInit server.AppI config.SetRoot(nodeDir) // Run `init` and generate genesis.json and config.toml - _, _, err := initWithConfig(cdc, config, chainID, moniker, gentxsDir, true, true) + initCfg := InitConfig{ + ChainID: chainID, + GenTxsDir: gentxsDir, + Moniker: moniker, + WithTxs: true, + Overwrite: true, + OverwriteKeys: false, + } + _, _, err := initWithConfig(cdc, config, initCfg) if err != nil { return err } From 4796c9925267cdf7a6829b64904087a2e86fa9c4 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Tue, 16 Oct 2018 17:31:15 -0700 Subject: [PATCH 42/83] Fix ./cmd/gaia/init tests --- cmd/gaia/init/init.go | 11 ++++++----- cmd/gaia/init/init_test.go | 32 +++++++++++++++++++++++++------- server/init.go | 3 ++- server/mock/app.go | 5 ++--- 4 files changed, 35 insertions(+), 16 deletions(-) diff --git a/cmd/gaia/init/init.go b/cmd/gaia/init/init.go index e47950de06ac..ee3f190ed180 100644 --- a/cmd/gaia/init/init.go +++ b/cmd/gaia/init/init.go @@ -28,7 +28,6 @@ import ( const ( flagChainID = "chain-id" flagWithTxs = "with-txs" - flagMoniker = "moniker" flagOverwrite = "overwrite" flagClientHome = "home-client" flagOWK = "owk" @@ -62,7 +61,7 @@ func InitCmd(ctx *server.Context, cdc *codec.Codec, appInit server.AppInit) *cob initCfg := InitConfig{ ChainID: chainID, GenTxsDir: viper.GetString(filepath.Join(config.RootDir, "config", "gentx")), - Moniker: viper.GetString(flagMoniker), + Moniker: viper.GetString(client.FlagName), ClientHome: viper.GetString(flagClientHome), WithTxs: viper.GetBool(flagWithTxs), Overwrite: viper.GetBool(flagOverwrite), @@ -70,6 +69,9 @@ func InitCmd(ctx *server.Context, cdc *codec.Codec, appInit server.AppInit) *cob } nodeID, appMessage, err := initWithConfig(cdc, config, initCfg) // print out some key information + if err != nil { + return err + } toPrint := struct { ChainID string `json:"chain_id"` @@ -93,7 +95,7 @@ func InitCmd(ctx *server.Context, cdc *codec.Codec, appInit server.AppInit) *cob cmd.Flags().BoolP(flagOverwrite, "o", false, "overwrite the genesis.json file") cmd.Flags().String(flagChainID, "", "genesis file chain-id, if left blank will be randomly created") cmd.Flags().Bool(flagWithTxs, false, "apply existing genesis transactions from [--home]/config/gentx/") - cmd.Flags().String(flagMoniker, "", "moniker") + cmd.Flags().String(client.FlagName, "", "moniker") cmd.Flags().String(flagClientHome, app.DefaultCLIHome, "client's home directory") cmd.Flags().Bool(flagOWK, false, "overwrite client's keys") return cmd @@ -107,7 +109,7 @@ func initWithConfig(cdc *codec.Codec, config *cfg.Config, initCfg InitConfig) ( } nodeID = string(nodeKey.ID()) privValFile := config.PrivValidatorFile() - readOrCreatePrivValidator(privValFile) + valPubKey := readOrCreatePrivValidator(privValFile) genFile := config.GenesisFile() if !initCfg.Overwrite && common.FileExists(genFile) { err = fmt.Errorf("genesis.json file already exists: %v", genFile) @@ -170,7 +172,6 @@ func initWithConfig(cdc *codec.Codec, config *cfg.Config, initCfg InitConfig) ( return } - valPubKey := readOrCreatePrivValidator(config.PrivValidatorFile()) msg := stake.NewMsgCreateValidator( sdk.ValAddress(addr), valPubKey, diff --git a/cmd/gaia/init/init_test.go b/cmd/gaia/init/init_test.go index 3a7f0a358d47..37db7d7927c1 100644 --- a/cmd/gaia/init/init_test.go +++ b/cmd/gaia/init/init_test.go @@ -2,47 +2,63 @@ package init import ( "bytes" + "github.com/cosmos/cosmos-sdk/cmd/gaia/app" + "github.com/tendermint/tendermint/libs/cli" "io" "io/ioutil" "os" "testing" "time" - "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/server" "github.com/cosmos/cosmos-sdk/server/mock" "github.com/stretchr/testify/require" abciServer "github.com/tendermint/tendermint/abci/server" tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands" "github.com/tendermint/tendermint/libs/log" + + "github.com/spf13/viper" ) func TestInitCmd(t *testing.T) { defer server.SetupViper(t)() + defer setupClientHome(t)() logger := log.NewNopLogger() cfg, err := tcmd.ParseConfig() require.Nil(t, err) ctx := server.NewContext(cfg, logger) - cdc := codec.New() + cdc := app.MakeCodec() appInit := server.AppInit{ AppGenState: mock.AppGenState, - AppGenTx: mock.AppGenTx, } cmd := InitCmd(ctx, cdc, appInit) err = cmd.RunE(nil, nil) require.NoError(t, err) } +func setupClientHome(t *testing.T) func() { + clientDir, err := ioutil.TempDir("", "mock-sdk-cmd") + require.Nil(t, err) + viper.Set(flagClientHome, clientDir) + viper.Set(flagOWK, true) + return func() { + if err := os.RemoveAll(clientDir); err != nil { + // TODO: Handle with #870 + panic(err) + } + } +} + func TestEmptyState(t *testing.T) { defer server.SetupViper(t)() + defer setupClientHome(t)() logger := log.NewNopLogger() cfg, err := tcmd.ParseConfig() require.Nil(t, err) ctx := server.NewContext(cfg, logger) - cdc := codec.New() + cdc := app.MakeCodec() appInit := server.AppInit{ - AppGenTx: mock.AppGenTx, AppGenState: mock.AppGenStateEmpty, } cmd := InitCmd(ctx, cdc, appInit) @@ -80,15 +96,17 @@ func TestStartStandAlone(t *testing.T) { defer func() { os.RemoveAll(home) }() + viper.Set(cli.HomeFlag, home) + viper.Set(server.FlagName, "moniker") + defer setupClientHome(t)() logger := log.NewNopLogger() cfg, err := tcmd.ParseConfig() require.Nil(t, err) ctx := server.NewContext(cfg, logger) - cdc := codec.New() + cdc := app.MakeCodec() appInit := server.AppInit{ AppGenState: mock.AppGenState, - AppGenTx: mock.AppGenTx, } initCmd := InitCmd(ctx, cdc, appInit) err = initCmd.RunE(nil, nil) diff --git a/server/init.go b/server/init.go index a4c90c7fb3af..595a01f403e8 100644 --- a/server/init.go +++ b/server/init.go @@ -123,7 +123,8 @@ func GenerateSaveCoinKey(clientRoot, keyName, keyPass string, overwrite bool) (s if !overwrite { _, err := keybase.Get(keyName) if err == nil { - return sdk.AccAddress([]byte{}), "", errors.New("key already exists, overwrite is disabled") + return sdk.AccAddress([]byte{}), "", fmt.Errorf( + "key already exists, overwrite is disabled (clientRoot: %s)", clientRoot) } } diff --git a/server/mock/app.go b/server/mock/app.go index 74dc0b01c4d4..18afa164ec64 100644 --- a/server/mock/app.go +++ b/server/mock/app.go @@ -12,7 +12,6 @@ import ( bam "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" - auth "github.com/cosmos/cosmos-sdk/x/auth" ) // NewApp creates a simple mock kvstore app for testing. It should work @@ -103,7 +102,7 @@ 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, _ []auth.StdTx) (appState json.RawMessage, err error) { +func AppGenState(_ *codec.Codec, _ []json.RawMessage) (appState json.RawMessage, err error) { appState = json.RawMessage(`{ "values": [ { @@ -120,7 +119,7 @@ func AppGenState(_ *codec.Codec, _ []auth.StdTx) (appState json.RawMessage, err } // AppGenStateEmpty returns an empty transaction state for mocking. -func AppGenStateEmpty(_ *codec.Codec, _ []auth.StdTx) (appState json.RawMessage, err error) { +func AppGenStateEmpty(_ *codec.Codec, _ []json.RawMessage) (appState json.RawMessage, err error) { appState = json.RawMessage(``) return } From 984da2abc01a2f44f4886aab93c111937f1b7b15 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Wed, 17 Oct 2018 09:10:12 -0700 Subject: [PATCH 43/83] Fix tests --- cmd/gaia/app/genesis_test.go | 7 +---- server/export_test.go | 53 ------------------------------------ server/init_test.go | 47 -------------------------------- server/start_test.go | 52 ----------------------------------- 4 files changed, 1 insertion(+), 158 deletions(-) delete mode 100644 server/export_test.go delete mode 100644 server/init_test.go delete mode 100644 server/start_test.go diff --git a/cmd/gaia/app/genesis_test.go b/cmd/gaia/app/genesis_test.go index a8776e8fef2e..1acc9f39314b 100644 --- a/cmd/gaia/app/genesis_test.go +++ b/cmd/gaia/app/genesis_test.go @@ -36,13 +36,8 @@ func makeGenesisState(t *testing.T, genTxs []auth.StdTx) GenesisState { msg := msgs[0].(stake.MsgCreateValidator) // get genesis flag account information - genAccs[i] = genesisAccountFromMsgCreateValidator(msg) + genAccs[i] = genesisAccountFromMsgCreateValidator(msg, freeFermionsAcc) stakeData.Pool.LooseTokens = stakeData.Pool.LooseTokens.Add(sdk.NewDecFromInt(freeFermionsAcc)) // increase the supply - - // add the validator - if len(msg.Name()) > 0 { - stakeData = addValidatorToStakeData(msg, stakeData) - } } // create the final app state diff --git a/server/export_test.go b/server/export_test.go deleted file mode 100644 index b647a72d5099..000000000000 --- a/server/export_test.go +++ /dev/null @@ -1,53 +0,0 @@ -package server - -import ( - "bytes" - "io" - "os" - "testing" - - "github.com/cosmos/cosmos-sdk/codec" - "github.com/cosmos/cosmos-sdk/server/mock" - "github.com/stretchr/testify/require" - tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands" - "github.com/tendermint/tendermint/libs/log" -) - -func TestEmptyState(t *testing.T) { - defer setupViper(t)() - logger := log.NewNopLogger() - cfg, err := tcmd.ParseConfig() - require.Nil(t, err) - ctx := NewContext(cfg, logger) - cdc := codec.New() - appInit := AppInit{ - AppGenTx: SimpleAppGenTx, - AppGenState: mock.AppGenStateEmpty, - } - cmd := InitCmd(ctx, cdc, appInit) - err = cmd.RunE(nil, nil) - require.NoError(t, err) - - old := os.Stdout - r, w, _ := os.Pipe() - os.Stdout = w - cmd = ExportCmd(ctx, cdc, nil) - err = cmd.RunE(nil, nil) - require.NoError(t, err) - - outC := make(chan string) - go func() { - var buf bytes.Buffer - io.Copy(&buf, r) - outC <- buf.String() - }() - - w.Close() - os.Stdout = old - out := <-outC - require.Contains(t, out, "WARNING: State is not initialized") - require.Contains(t, out, "genesis_time") - require.Contains(t, out, "chain_id") - require.Contains(t, out, "consensus_params") - require.Contains(t, out, "app_hash") -} diff --git a/server/init_test.go b/server/init_test.go deleted file mode 100644 index ca8d47d72ce0..000000000000 --- a/server/init_test.go +++ /dev/null @@ -1,47 +0,0 @@ -package server - -import ( - "testing" - - "github.com/stretchr/testify/require" - - "github.com/tendermint/tendermint/libs/log" - - "github.com/cosmos/cosmos-sdk/codec" - "github.com/cosmos/cosmos-sdk/server/mock" - tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands" -) - -// TODO update -func TestInitCmd(t *testing.T) { - defer setupViper(t)() - - logger := log.NewNopLogger() - cfg, err := tcmd.ParseConfig() - require.Nil(t, err) - ctx := NewContext(cfg, logger) - cdc := codec.New() - appInit := AppInit{ - AppGenState: mock.AppGenState, - AppGenTx: SimpleAppGenTx, - } - cmd := InitCmd(ctx, cdc, appInit) - err = cmd.RunE(nil, nil) - require.NoError(t, err) -} - -func TestGenTxCmd(t *testing.T) { - // TODO -} - -func TestTestnetFilesCmd(t *testing.T) { - // TODO -} - -func TestSimpleAppGenTx(t *testing.T) { - // TODO -} - -func TestSimpleAppGenState(t *testing.T) { - // TODO -} diff --git a/server/start_test.go b/server/start_test.go deleted file mode 100644 index 3fc795a02f35..000000000000 --- a/server/start_test.go +++ /dev/null @@ -1,52 +0,0 @@ -package server - -import ( - "io/ioutil" - "os" - "testing" - "time" - - "github.com/stretchr/testify/require" - - "github.com/cosmos/cosmos-sdk/codec" - "github.com/cosmos/cosmos-sdk/server/mock" - "github.com/tendermint/tendermint/abci/server" - tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands" - "github.com/tendermint/tendermint/libs/log" -) - -func TestStartStandAlone(t *testing.T) { - home, err := ioutil.TempDir("", "mock-sdk-cmd") - require.Nil(t, err) - defer func() { - os.RemoveAll(home) - }() - - logger := log.NewNopLogger() - cfg, err := tcmd.ParseConfig() - require.Nil(t, err) - ctx := NewContext(cfg, logger) - cdc := codec.New() - appInit := AppInit{ - AppGenState: mock.AppGenState, - AppGenTx: SimpleAppGenTx, - } - initCmd := InitCmd(ctx, cdc, appInit) - err = initCmd.RunE(nil, nil) - require.NoError(t, err) - - app, err := mock.NewApp(home, logger) - require.Nil(t, err) - svrAddr, _, err := FreeTCPAddr() - require.Nil(t, err) - svr, err := server.NewServer(svrAddr, "socket", app) - require.Nil(t, err, "error creating listener") - svr.SetLogger(logger.With("module", "abci-server")) - svr.Start() - - timer := time.NewTimer(time.Duration(2) * time.Second) - select { - case <-timer.C: - svr.Stop() - } -} From 93913aebc8c4503cb08f7c75ad44cddba5dadd66 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Wed, 17 Oct 2018 09:12:45 -0700 Subject: [PATCH 44/83] Fix democoind --- examples/democoin/cmd/democoind/main.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/examples/democoin/cmd/democoind/main.go b/examples/democoin/cmd/democoind/main.go index b359c9e62aae..711d403f9391 100644 --- a/examples/democoin/cmd/democoind/main.go +++ b/examples/democoin/cmd/democoind/main.go @@ -17,17 +17,15 @@ import ( "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/examples/democoin/app" "github.com/cosmos/cosmos-sdk/server" - auth "github.com/cosmos/cosmos-sdk/x/auth" ) // init parameters var CoolAppInit = server.AppInit{ AppGenState: CoolAppGenState, - AppGenTx: server.SimpleAppGenTx, } // coolGenAppParams sets up the app_state and appends the cool app state -func CoolAppGenState(cdc *codec.Codec, appGenTxs []auth.StdTx) (appState json.RawMessage, err error) { +func CoolAppGenState(cdc *codec.Codec, appGenTxs []json.RawMessage) (appState json.RawMessage, err error) { appState, err = server.SimpleAppGenState(cdc, appGenTxs) if err != nil { return From 45b6ff65bb9a397a0b65416bd596cf6de6317b08 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Wed, 17 Oct 2018 11:43:02 -0700 Subject: [PATCH 45/83] Get rid of FlagsAppGenState --- cmd/gaia/app/genesis.go | 6 +----- server/init.go | 6 ------ 2 files changed, 1 insertion(+), 11 deletions(-) diff --git a/cmd/gaia/app/genesis.go b/cmd/gaia/app/genesis.go index 9020ef5bed68..41a94e62d23f 100644 --- a/cmd/gaia/app/genesis.go +++ b/cmd/gaia/app/genesis.go @@ -10,7 +10,6 @@ import ( "sort" "strings" - "github.com/tendermint/tendermint/crypto" "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/server" sdk "github.com/cosmos/cosmos-sdk/types" @@ -19,8 +18,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/gov" "github.com/cosmos/cosmos-sdk/x/slashing" "github.com/cosmos/cosmos-sdk/x/stake" - "github.com/spf13/pflag" - + "github.com/tendermint/tendermint/crypto" tmtypes "github.com/tendermint/tendermint/types" ) @@ -70,10 +68,8 @@ func (ga *GenesisAccount) ToAccount() (acc *auth.BaseAccount) { // get app init parameters for server init command func GaiaAppInit() server.AppInit { - fsAppGenState := pflag.NewFlagSet("", pflag.ContinueOnError) return server.AppInit{ - FlagsAppGenState: fsAppGenState, AppGenState: GaiaAppGenStateJSON, } } diff --git a/server/init.go b/server/init.go index 595a01f403e8..ecd141bff8f6 100644 --- a/server/init.go +++ b/server/init.go @@ -8,8 +8,6 @@ import ( "github.com/cosmos/cosmos-sdk/x/auth" "github.com/cosmos/cosmos-sdk/x/stake" "github.com/pkg/errors" - "github.com/spf13/pflag" - dbm "github.com/tendermint/tendermint/libs/db" clkeys "github.com/cosmos/cosmos-sdk/client/keys" @@ -39,10 +37,6 @@ type InitConfig struct { // Core functionality passed from the application to the server init command type AppInit struct { - - // flags required for application init functions - FlagsAppGenState *pflag.FlagSet - // AppGenState creates the core parameters initialization. It takes in a // pubkey meant to represent the pubkey of the validator of this machine. AppGenState func(cdc *codec.Codec, appGenTx []json.RawMessage) (appState json.RawMessage, err error) From f1e86a3805477ef163fc71fcbd575ce8776dd677 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Wed, 17 Oct 2018 13:01:02 -0700 Subject: [PATCH 46/83] Fix more tests TODO: Find out how to get past genesis when using MockApp --- x/bank/app_test.go | 4 ++-- x/stake/app_test.go | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/x/bank/app_test.go b/x/bank/app_test.go index 77991f94b78d..7c320a9bd0e7 100644 --- a/x/bank/app_test.go +++ b/x/bank/app_test.go @@ -210,7 +210,7 @@ func TestSengMsgMultipleInOut(t *testing.T) { testCases := []appTestCase{ { msgs: []sdk.Msg{sendMsg3}, - accNums: []int64{0, 2}, + accNums: []int64{0, 0}, accSeqs: []int64{0, 0}, expSimPass: true, expPass: true, @@ -258,7 +258,7 @@ func TestMsgSendDependent(t *testing.T) { }, { msgs: []sdk.Msg{sendMsg4}, - accNums: []int64{1}, + accNums: []int64{0}, accSeqs: []int64{0}, expSimPass: true, expPass: true, diff --git a/x/stake/app_test.go b/x/stake/app_test.go index 4ab210f70a85..faafb6664f7e 100644 --- a/x/stake/app_test.go +++ b/x/stake/app_test.go @@ -137,7 +137,7 @@ func TestStakeMsgs(t *testing.T) { addr1, sdk.ValAddress(addr2), priv2.PubKey(), bondCoin, description, commissionMsg, ) - mock.SignCheckDeliver(t, mApp.BaseApp, []sdk.Msg{createValidatorMsgOnBehalfOf}, []int64{0, 1}, []int64{1, 0}, true, true, priv1, priv2) + mock.SignCheckDeliver(t, mApp.BaseApp, []sdk.Msg{createValidatorMsgOnBehalfOf}, []int64{0, 0}, []int64{1, 0}, true, true, priv1, priv2) mock.CheckBalance(t, mApp, addr1, sdk.Coins{genCoin.Minus(bondCoin).Minus(bondCoin)}) mApp.BeginBlock(abci.RequestBeginBlock{}) @@ -161,13 +161,13 @@ func TestStakeMsgs(t *testing.T) { mock.CheckBalance(t, mApp, addr2, sdk.Coins{genCoin}) delegateMsg := NewMsgDelegate(addr2, sdk.ValAddress(addr1), bondCoin) - mock.SignCheckDeliver(t, mApp.BaseApp, []sdk.Msg{delegateMsg}, []int64{1}, []int64{1}, true, true, priv2) + mock.SignCheckDeliver(t, mApp.BaseApp, []sdk.Msg{delegateMsg}, []int64{0}, []int64{1}, true, true, priv2) mock.CheckBalance(t, mApp, addr2, sdk.Coins{genCoin.Minus(bondCoin)}) checkDelegation(t, mApp, keeper, addr2, sdk.ValAddress(addr1), true, sdk.NewDec(10)) // begin unbonding beginUnbondingMsg := NewMsgBeginUnbonding(addr2, sdk.ValAddress(addr1), sdk.NewDec(10)) - mock.SignCheckDeliver(t, mApp.BaseApp, []sdk.Msg{beginUnbondingMsg}, []int64{1}, []int64{2}, true, true, priv2) + mock.SignCheckDeliver(t, mApp.BaseApp, []sdk.Msg{beginUnbondingMsg}, []int64{0}, []int64{2}, true, true, priv2) // delegation should exist anymore checkDelegation(t, mApp, keeper, addr2, sdk.ValAddress(addr1), false, sdk.Dec{}) From db9c1d7bd5e43b06de6412fa04df09c99d25ad02 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Wed, 17 Oct 2018 13:35:54 -0700 Subject: [PATCH 47/83] Fix integration tests, move DefaultKeyPass into app package --- cmd/gaia/app/app.go | 2 ++ cmd/gaia/cli_test/cli_test.go | 30 +++++++++++++++--------------- cmd/gaia/init/init.go | 4 ++-- cmd/gaia/init/testnet.go | 5 +++-- server/init.go | 3 --- tests/gobash.go | 8 +++++--- 6 files changed, 27 insertions(+), 25 deletions(-) diff --git a/cmd/gaia/app/app.go b/cmd/gaia/app/app.go index 3cbde281ec3e..a445206a0c0d 100644 --- a/cmd/gaia/app/app.go +++ b/cmd/gaia/app/app.go @@ -25,6 +25,8 @@ import ( const ( appName = "GaiaApp" + // DefaultKeyPass contains the default key password for genesis transactions + DefaultKeyPass = "12345678" ) // default home directories for expected binaries diff --git a/cmd/gaia/cli_test/cli_test.go b/cmd/gaia/cli_test/cli_test.go index 04bc4c84a590..1639e143e5e6 100644 --- a/cmd/gaia/cli_test/cli_test.go +++ b/cmd/gaia/cli_test/cli_test.go @@ -311,7 +311,7 @@ func TestGaiaCLISubmitProposal(t *testing.T) { fooAcc := executeGetAccount(t, fmt.Sprintf("gaiacli query account %s %v", fooAddr, flags)) require.Equal(t, int64(50), fooAcc.GetCoins().AmountOf("steak").Int64()) - proposalsQuery := tests.ExecuteT(t, fmt.Sprintf("gaiacli query proposals %v", flags), "") + proposalsQuery, _ := tests.ExecuteT(t, fmt.Sprintf("gaiacli query proposals %v", flags), "") require.Equal(t, "No matching proposals found", proposalsQuery) // submit a test proposal @@ -346,7 +346,7 @@ func TestGaiaCLISubmitProposal(t *testing.T) { require.Equal(t, int64(1), proposal1.GetProposalID()) require.Equal(t, gov.StatusDepositPeriod, proposal1.GetStatus()) - proposalsQuery = tests.ExecuteT(t, fmt.Sprintf("gaiacli query proposals %v", flags), "") + proposalsQuery, _ = tests.ExecuteT(t, fmt.Sprintf("gaiacli query proposals %v", flags), "") require.Equal(t, " 1 - Test", proposalsQuery) depositStr := fmt.Sprintf("gaiacli tx deposit %v", flags) @@ -400,10 +400,10 @@ func TestGaiaCLISubmitProposal(t *testing.T) { require.Equal(t, int64(1), votes[0].ProposalID) require.Equal(t, gov.OptionYes, votes[0].Option) - proposalsQuery = tests.ExecuteT(t, fmt.Sprintf("gaiacli query proposals --status=DepositPeriod %v", flags), "") + proposalsQuery, _ = tests.ExecuteT(t, fmt.Sprintf("gaiacli query proposals --status=DepositPeriod %v", flags), "") require.Equal(t, "No matching proposals found", proposalsQuery) - proposalsQuery = tests.ExecuteT(t, fmt.Sprintf("gaiacli query proposals --status=VotingPeriod %v", flags), "") + proposalsQuery, _ = tests.ExecuteT(t, fmt.Sprintf("gaiacli query proposals --status=VotingPeriod %v", flags), "") require.Equal(t, " 1 - Test", proposalsQuery) // submit a second test proposal @@ -417,7 +417,7 @@ func TestGaiaCLISubmitProposal(t *testing.T) { executeWrite(t, spStr, app.DefaultKeyPass) tests.WaitForNextNBlocksTM(2, port) - proposalsQuery = tests.ExecuteT(t, fmt.Sprintf("gaiacli query proposals --latest=1 %v", flags), "") + proposalsQuery, _ = tests.ExecuteT(t, fmt.Sprintf("gaiacli query proposals --latest=1 %v", flags), "") require.Equal(t, " 2 - Apples", proposalsQuery) } @@ -628,10 +628,10 @@ func executeWriteRetStdStreams(t *testing.T, cmdStr string, writes ...string) (b } func executeInit(t *testing.T, cmdStr string) (chainID string) { - out := tests.ExecuteT(t, cmdStr, app.DefaultKeyPass) + _, stderr := tests.ExecuteT(t, cmdStr, app.DefaultKeyPass) var initRes map[string]json.RawMessage - err := json.Unmarshal([]byte(out), &initRes) + err := json.Unmarshal([]byte(stderr), &initRes) require.NoError(t, err) err = json.Unmarshal(initRes["chain_id"], &chainID) @@ -641,7 +641,7 @@ func executeInit(t *testing.T, cmdStr string) (chainID string) { } func executeGetAddrPK(t *testing.T, cmdStr string) (sdk.AccAddress, crypto.PubKey) { - out := tests.ExecuteT(t, cmdStr, "") + out, _ := tests.ExecuteT(t, cmdStr, "") var ko keys.KeyOutput keys.UnmarshalJSON([]byte(out), &ko) @@ -655,7 +655,7 @@ func executeGetAddrPK(t *testing.T, cmdStr string) (sdk.AccAddress, crypto.PubKe } func executeGetAccount(t *testing.T, cmdStr string) auth.BaseAccount { - out := tests.ExecuteT(t, cmdStr, "") + out, _ := tests.ExecuteT(t, cmdStr, "") var initRes map[string]json.RawMessage err := json.Unmarshal([]byte(out), &initRes) require.NoError(t, err, "out %v, err %v", out, err) @@ -672,7 +672,7 @@ func executeGetAccount(t *testing.T, cmdStr string) auth.BaseAccount { // stake func executeGetValidator(t *testing.T, cmdStr string) stake.Validator { - out := tests.ExecuteT(t, cmdStr, "") + out, _ := tests.ExecuteT(t, cmdStr, "") var validator stake.Validator cdc := app.MakeCodec() err := cdc.UnmarshalJSON([]byte(out), &validator) @@ -681,7 +681,7 @@ func executeGetValidator(t *testing.T, cmdStr string) stake.Validator { } func executeGetPool(t *testing.T, cmdStr string) stake.Pool { - out := tests.ExecuteT(t, cmdStr, "") + out, _ := tests.ExecuteT(t, cmdStr, "") var pool stake.Pool cdc := app.MakeCodec() err := cdc.UnmarshalJSON([]byte(out), &pool) @@ -690,7 +690,7 @@ func executeGetPool(t *testing.T, cmdStr string) stake.Pool { } func executeGetParams(t *testing.T, cmdStr string) stake.Params { - out := tests.ExecuteT(t, cmdStr, "") + out, _ := tests.ExecuteT(t, cmdStr, "") var params stake.Params cdc := app.MakeCodec() err := cdc.UnmarshalJSON([]byte(out), ¶ms) @@ -702,7 +702,7 @@ func executeGetParams(t *testing.T, cmdStr string) stake.Params { // gov func executeGetProposal(t *testing.T, cmdStr string) gov.Proposal { - out := tests.ExecuteT(t, cmdStr, "") + out, _ := tests.ExecuteT(t, cmdStr, "") var proposal gov.Proposal cdc := app.MakeCodec() err := cdc.UnmarshalJSON([]byte(out), &proposal) @@ -711,7 +711,7 @@ func executeGetProposal(t *testing.T, cmdStr string) gov.Proposal { } func executeGetVote(t *testing.T, cmdStr string) gov.Vote { - out := tests.ExecuteT(t, cmdStr, "") + out, _ := tests.ExecuteT(t, cmdStr, "") var vote gov.Vote cdc := app.MakeCodec() err := cdc.UnmarshalJSON([]byte(out), &vote) @@ -720,7 +720,7 @@ func executeGetVote(t *testing.T, cmdStr string) gov.Vote { } func executeGetVotes(t *testing.T, cmdStr string) []gov.Vote { - out := tests.ExecuteT(t, cmdStr, "") + out, _ := tests.ExecuteT(t, cmdStr, "") var votes []gov.Vote cdc := app.MakeCodec() err := cdc.UnmarshalJSON([]byte(out), &votes) diff --git a/cmd/gaia/init/init.go b/cmd/gaia/init/init.go index ee3f190ed180..0ccebe0cb414 100644 --- a/cmd/gaia/init/init.go +++ b/cmd/gaia/init/init.go @@ -151,7 +151,7 @@ func initWithConfig(cdc *codec.Codec, config *cfg.Config, initCfg InitConfig) ( } memo := fmt.Sprintf("%s@%s:26656", nodeID, ip) buf := client.BufferStdin() - prompt := fmt.Sprintf("Password for account '%s' (default %s):", moniker, server.DefaultKeyPass) + prompt := fmt.Sprintf("Password for account '%s' (default %s):", moniker, app.DefaultKeyPass) keyPass, err = client.GetPassword(prompt, buf) if err != nil && keyPass != "" { // An error was returned that either failed to read the password from @@ -160,7 +160,7 @@ func initWithConfig(cdc *codec.Codec, config *cfg.Config, initCfg InitConfig) ( return } if keyPass == "" { - keyPass = server.DefaultKeyPass + keyPass = app.DefaultKeyPass } addr, secret, err = server.GenerateSaveCoinKey(initCfg.ClientHome, moniker, keyPass, initCfg.OverwriteKeys) diff --git a/cmd/gaia/init/testnet.go b/cmd/gaia/init/testnet.go index f1c5a9c23b86..cfc20271d214 100644 --- a/cmd/gaia/init/testnet.go +++ b/cmd/gaia/init/testnet.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/cmd/gaia/app" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth" @@ -116,7 +117,7 @@ func testnetWithConfig(config *cfg.Config, cdc *codec.Codec, appInit server.AppI memo := fmt.Sprintf("%s@%s:26656", nodeID, ip) buf := client.BufferStdin() - prompt := fmt.Sprintf("Password for account '%s' (default %s):", nodeDirName, server.DefaultKeyPass) + prompt := fmt.Sprintf("Password for account '%s' (default %s):", nodeDirName, app.DefaultKeyPass) keyPass, err := client.GetPassword(prompt, buf) if err != nil && keyPass != "" { // An error was returned that either failed to read the password from @@ -125,7 +126,7 @@ func testnetWithConfig(config *cfg.Config, cdc *codec.Codec, appInit server.AppI return err } if keyPass == "" { - keyPass = server.DefaultKeyPass + keyPass = app.DefaultKeyPass } addr, secret, err := server.GenerateSaveCoinKey(clientDir, nodeDirName, keyPass, true) diff --git a/server/init.go b/server/init.go index ecd141bff8f6..c63c1346a7b7 100644 --- a/server/init.go +++ b/server/init.go @@ -23,9 +23,6 @@ const ( FlagChainID = "chain-id" ) -// DefaultKeyPass contains the default key password for genesis transactions -const DefaultKeyPass = "12345678" - // Storage for init command input parameters type InitConfig struct { ChainID string diff --git a/tests/gobash.go b/tests/gobash.go index 6282f2fda4c4..87d56a2974d2 100644 --- a/tests/gobash.go +++ b/tests/gobash.go @@ -14,7 +14,7 @@ import ( // ExecuteT executes the command, pipes any input to STDIN and return STDOUT, // logging STDOUT/STDERR to t. // nolint: errcheck -func ExecuteT(t *testing.T, cmd, input string) (out string) { +func ExecuteT(t *testing.T, cmd, input string) (stdout, stderr string) { t.Log("Running", cmn.Cyan(cmd)) // split cmd to name and args @@ -50,8 +50,10 @@ func ExecuteT(t *testing.T, cmd, input string) (out string) { t.Log("Stderr:", cmn.Red(string(errbz))) } - out = strings.Trim(string(outbz), "\n") - return out + stdout = strings.Trim(string(outbz), "\n") + stderr = strings.Trim(string(errbz), "\n") + + return } // Execute the command, launch goroutines to log stdout/err to t. From 4c5b583ffd942f35f03887849f6249a0600d184b Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Wed, 17 Oct 2018 15:10:24 -0700 Subject: [PATCH 48/83] Rebased on develop --- cmd/gaia/init/testnet.go | 2 +- examples/basecoin/cli_test/cli_test.go | 4 ++-- examples/democoin/cli_test/cli_test.go | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/cmd/gaia/init/testnet.go b/cmd/gaia/init/testnet.go index cfc20271d214..0c8c02712935 100644 --- a/cmd/gaia/init/testnet.go +++ b/cmd/gaia/init/testnet.go @@ -155,7 +155,7 @@ func testnetWithConfig(config *cfg.Config, cdc *codec.Codec, appInit server.AppI ) tx := auth.NewStdTx([]sdk.Msg{msg}, auth.StdFee{}, []auth.StdSignature{}, memo) txBldr := authtx.NewTxBuilderFromCLI().WithChainID(chainID).WithMemo(memo) - signedTx, err := txBldr.SignStdTx(nodeDirName, server.DefaultKeyPass, tx, false) + signedTx, err := txBldr.SignStdTx(nodeDirName, app.DefaultKeyPass, tx, false) if err != nil { _ = os.RemoveAll(outDir) return err diff --git a/examples/basecoin/cli_test/cli_test.go b/examples/basecoin/cli_test/cli_test.go index 3a33135e399e..f74947cf662a 100644 --- a/examples/basecoin/cli_test/cli_test.go +++ b/examples/basecoin/cli_test/cli_test.go @@ -32,8 +32,8 @@ func executeInit(t *testing.T) { chainID string initRes map[string]json.RawMessage ) - out := tests.ExecuteT(t, fmt.Sprintf("basecoind --home=%s init", basecoindHome), "") - err := json.Unmarshal([]byte(out), &initRes) + _, stderr := tests.ExecuteT(t, fmt.Sprintf("basecoind --home=%s init", basecoindHome), "") + err := json.Unmarshal([]byte(stderr), &initRes) require.NoError(t, err) err = json.Unmarshal(initRes["chain_id"], &chainID) require.NoError(t, err) diff --git a/examples/democoin/cli_test/cli_test.go b/examples/democoin/cli_test/cli_test.go index 2db2fff09641..48c5108d5558 100644 --- a/examples/democoin/cli_test/cli_test.go +++ b/examples/democoin/cli_test/cli_test.go @@ -32,8 +32,8 @@ func executeInit(t *testing.T) { chainID string initRes map[string]json.RawMessage ) - out := tests.ExecuteT(t, fmt.Sprintf("democoind --home=%s init", democoindHome), "") - err := json.Unmarshal([]byte(out), &initRes) + _, stderr := tests.ExecuteT(t, fmt.Sprintf("democoind --home=%s init", democoindHome), "") + err := json.Unmarshal([]byte(stderr), &initRes) require.NoError(t, err) err = json.Unmarshal(initRes["chain_id"], &chainID) require.NoError(t, err) From e0763c41cd84c0906557d25da52455028fb930f7 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Wed, 17 Oct 2018 16:21:41 -0700 Subject: [PATCH 49/83] Refresh Gopkg.* --- Gopkg.lock | 108 +++-------------------------------------------------- Gopkg.toml | 4 +- 2 files changed, 8 insertions(+), 104 deletions(-) diff --git a/Gopkg.lock b/Gopkg.lock index 20cc5aaa83c0..06d2f341f62b 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -423,18 +423,6 @@ pruneopts = "UT" revision = "e5840949ff4fff0c56f9b6a541e22b63581ea9df" -[[projects]] - branch = "master" - digest = "1:087aaa7920e5d0bf79586feb57ce01c35c830396ab4392798112e8aae8c47722" - name = "github.com/tendermint/ed25519" - packages = [ - ".", - "edwards25519", - "extra25519", - ] - pruneopts = "UT" - revision = "d8387025d2b9d158cf4efb07e7ebf814bcce2057" - [[projects]] digest = "1:2c971a45c89ca2ccc735af50919cdee05fbdc54d4bf50625073693300e31ead8" name = "github.com/tendermint/go-amino" @@ -452,7 +440,7 @@ version = "v0.11.0" [[projects]] - digest = "1:a69eebd15b05045ffdb10a984e001fadc5666f74383de3d2a9ee5862ee99cfdc" + digest = "1:46b0d485c59804b2adfd925480d4e4e7749f3ba766ff718db8fba6e8caa4db7b" name = "github.com/tendermint/tendermint" packages = [ "abci/client", @@ -518,8 +506,8 @@ "version", ] pruneopts = "UT" - revision = "0c9c3292c918617624f6f3fbcd95eceade18bcd5" - version = "v0.25.0" + revision = "37928cb9907a60ae79d5b387d1d89ffc43dc07d8" + version = "v0.26.0-dev0" [[projects]] digest = "1:7886f86064faff6f8d08a3eb0e8c773648ff5a2e27730831e2bfbf07467f6666" @@ -530,13 +518,15 @@ version = "v0.1.0" [[projects]] - digest = "1:aaff04fa01d9b824fde6799759cc597b3ac3671b9ad31924c28b6557d0ee5284" + digest = "1:6f6dc6060c4e9ba73cf28aa88f12a69a030d3d19d518ef8e931879eaa099628d" name = "golang.org/x/crypto" packages = [ "bcrypt", "blowfish", "chacha20poly1305", "curve25519", + "ed25519", + "ed25519/internal/edwards25519", "hkdf", "internal/chacha20", "internal/subtle", @@ -655,140 +645,54 @@ analyzer-name = "dep" analyzer-version = 1 input-imports = [ - "github.com/ZondaX/hid-go", "github.com/bartekn/go-bip39", - "github.com/beorn7/perks/quantile", "github.com/bgentry/speakeasy", "github.com/btcsuite/btcd/btcec", "github.com/cosmos/go-bip39", "github.com/golang/protobuf/proto", - "github.com/golang/protobuf/ptypes", - "github.com/golang/protobuf/ptypes/any", - "github.com/golang/protobuf/ptypes/duration", - "github.com/golang/protobuf/ptypes/timestamp", - "github.com/golang/snappy", - "github.com/gorilla/context", "github.com/gorilla/mux", - "github.com/gorilla/websocket", - "github.com/hashicorp/hcl", - "github.com/hashicorp/hcl/hcl/ast", - "github.com/hashicorp/hcl/hcl/parser", - "github.com/hashicorp/hcl/hcl/scanner", - "github.com/hashicorp/hcl/hcl/strconv", - "github.com/hashicorp/hcl/hcl/token", - "github.com/hashicorp/hcl/json/parser", - "github.com/hashicorp/hcl/json/scanner", - "github.com/hashicorp/hcl/json/token", - "github.com/inconshreveable/mousetrap", - "github.com/jmhodges/levigo", - "github.com/kr/logfmt", - "github.com/magiconair/properties", "github.com/mattn/go-isatty", - "github.com/matttproud/golang_protobuf_extensions/pbutil", "github.com/mitchellh/go-homedir", - "github.com/mitchellh/mapstructure", "github.com/pelletier/go-toml", "github.com/pkg/errors", - "github.com/pmezard/go-difflib/difflib", - "github.com/prometheus/client_golang/prometheus", - "github.com/prometheus/client_golang/prometheus/promhttp", - "github.com/prometheus/client_model/go", - "github.com/prometheus/common/expfmt", - "github.com/prometheus/common/internal/bitbucket.org/ww/goautoneg", - "github.com/prometheus/common/model", - "github.com/prometheus/procfs", - "github.com/prometheus/procfs/internal/util", - "github.com/prometheus/procfs/nfs", - "github.com/prometheus/procfs/xfs", "github.com/rakyll/statik/fs", - "github.com/rcrowley/go-metrics", - "github.com/spf13/afero", - "github.com/spf13/afero/mem", - "github.com/spf13/cast", "github.com/spf13/cobra", - "github.com/spf13/jwalterweatherman", "github.com/spf13/pflag", "github.com/spf13/viper", "github.com/stretchr/testify/assert", "github.com/stretchr/testify/require", - "github.com/syndtr/goleveldb/leveldb", - "github.com/syndtr/goleveldb/leveldb/cache", - "github.com/syndtr/goleveldb/leveldb/comparer", - "github.com/syndtr/goleveldb/leveldb/errors", - "github.com/syndtr/goleveldb/leveldb/filter", - "github.com/syndtr/goleveldb/leveldb/iterator", - "github.com/syndtr/goleveldb/leveldb/journal", - "github.com/syndtr/goleveldb/leveldb/memdb", - "github.com/syndtr/goleveldb/leveldb/opt", - "github.com/syndtr/goleveldb/leveldb/storage", - "github.com/syndtr/goleveldb/leveldb/table", - "github.com/syndtr/goleveldb/leveldb/util", - "github.com/tendermint/btcd/btcec", - "github.com/tendermint/ed25519", - "github.com/tendermint/ed25519/edwards25519", - "github.com/tendermint/ed25519/extra25519", "github.com/tendermint/go-amino", "github.com/tendermint/iavl", - "github.com/tendermint/tendermint/abci/client", - "github.com/tendermint/tendermint/abci/example/code", - "github.com/tendermint/tendermint/abci/example/kvstore", "github.com/tendermint/tendermint/abci/server", "github.com/tendermint/tendermint/abci/types", - "github.com/tendermint/tendermint/blockchain", "github.com/tendermint/tendermint/cmd/tendermint/commands", "github.com/tendermint/tendermint/config", - "github.com/tendermint/tendermint/consensus", - "github.com/tendermint/tendermint/consensus/types", "github.com/tendermint/tendermint/crypto", "github.com/tendermint/tendermint/crypto/armor", "github.com/tendermint/tendermint/crypto/ed25519", "github.com/tendermint/tendermint/crypto/encoding/amino", "github.com/tendermint/tendermint/crypto/merkle", - "github.com/tendermint/tendermint/crypto/multisig", - "github.com/tendermint/tendermint/crypto/multisig/bitarray", "github.com/tendermint/tendermint/crypto/secp256k1", "github.com/tendermint/tendermint/crypto/tmhash", "github.com/tendermint/tendermint/crypto/xsalsa20symmetric", - "github.com/tendermint/tendermint/evidence", - "github.com/tendermint/tendermint/libs/autofile", "github.com/tendermint/tendermint/libs/bech32", "github.com/tendermint/tendermint/libs/cli", "github.com/tendermint/tendermint/libs/cli/flags", - "github.com/tendermint/tendermint/libs/clist", "github.com/tendermint/tendermint/libs/common", "github.com/tendermint/tendermint/libs/db", - "github.com/tendermint/tendermint/libs/errors", - "github.com/tendermint/tendermint/libs/events", - "github.com/tendermint/tendermint/libs/flowrate", "github.com/tendermint/tendermint/libs/log", - "github.com/tendermint/tendermint/libs/pubsub", - "github.com/tendermint/tendermint/libs/pubsub/query", "github.com/tendermint/tendermint/lite", - "github.com/tendermint/tendermint/lite/client", "github.com/tendermint/tendermint/lite/errors", "github.com/tendermint/tendermint/lite/proxy", - "github.com/tendermint/tendermint/mempool", "github.com/tendermint/tendermint/node", "github.com/tendermint/tendermint/p2p", - "github.com/tendermint/tendermint/p2p/conn", - "github.com/tendermint/tendermint/p2p/pex", - "github.com/tendermint/tendermint/p2p/upnp", "github.com/tendermint/tendermint/privval", "github.com/tendermint/tendermint/proxy", "github.com/tendermint/tendermint/rpc/client", - "github.com/tendermint/tendermint/rpc/core", "github.com/tendermint/tendermint/rpc/core/types", - "github.com/tendermint/tendermint/rpc/grpc", - "github.com/tendermint/tendermint/rpc/lib", "github.com/tendermint/tendermint/rpc/lib/client", "github.com/tendermint/tendermint/rpc/lib/server", - "github.com/tendermint/tendermint/rpc/lib/types", - "github.com/tendermint/tendermint/state", - "github.com/tendermint/tendermint/state/txindex", - "github.com/tendermint/tendermint/state/txindex/kv", - "github.com/tendermint/tendermint/state/txindex/null", "github.com/tendermint/tendermint/types", - "github.com/tendermint/tendermint/types/time", "github.com/tendermint/tendermint/version", "github.com/zondax/ledger-goclient", "golang.org/x/crypto/bcrypt", diff --git a/Gopkg.toml b/Gopkg.toml index 05140857dd47..baf01473a957 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -57,11 +57,11 @@ [[override]] name = "github.com/tendermint/tendermint" - version = "=0.25.0" + version = "=0.26.0-dev0" ## deps without releases: -[[constraint]] +[[override]] name = "golang.org/x/crypto" source = "https://github.com/tendermint/crypto" revision = "3764759f34a542a3aef74d6b02e35be7ab893bba" From 37bbf2db5c540102ce16031a1b9ec3d5f4ee866d Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Wed, 17 Oct 2018 20:23:29 -0700 Subject: [PATCH 50/83] Pin tendermint to 0.25.1 --- Gopkg.lock | 22 ++++++++++++++++------ Gopkg.toml | 2 +- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/Gopkg.lock b/Gopkg.lock index 06d2f341f62b..c679450b9c0e 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -423,6 +423,18 @@ pruneopts = "UT" revision = "e5840949ff4fff0c56f9b6a541e22b63581ea9df" +[[projects]] + branch = "master" + digest = "1:087aaa7920e5d0bf79586feb57ce01c35c830396ab4392798112e8aae8c47722" + name = "github.com/tendermint/ed25519" + packages = [ + ".", + "edwards25519", + "extra25519", + ] + pruneopts = "UT" + revision = "d8387025d2b9d158cf4efb07e7ebf814bcce2057" + [[projects]] digest = "1:2c971a45c89ca2ccc735af50919cdee05fbdc54d4bf50625073693300e31ead8" name = "github.com/tendermint/go-amino" @@ -440,7 +452,7 @@ version = "v0.11.0" [[projects]] - digest = "1:46b0d485c59804b2adfd925480d4e4e7749f3ba766ff718db8fba6e8caa4db7b" + digest = "1:f9c7a1f3ee087476f4883c33cc7c1bdbe56b9670b2fb27855ea2f386393272f5" name = "github.com/tendermint/tendermint" packages = [ "abci/client", @@ -506,8 +518,8 @@ "version", ] pruneopts = "UT" - revision = "37928cb9907a60ae79d5b387d1d89ffc43dc07d8" - version = "v0.26.0-dev0" + revision = "90eda9bfb6e6daeed1c8015df41cb36772d91778" + version = "v0.25.1-rc0" [[projects]] digest = "1:7886f86064faff6f8d08a3eb0e8c773648ff5a2e27730831e2bfbf07467f6666" @@ -518,15 +530,13 @@ version = "v0.1.0" [[projects]] - digest = "1:6f6dc6060c4e9ba73cf28aa88f12a69a030d3d19d518ef8e931879eaa099628d" + digest = "1:aaff04fa01d9b824fde6799759cc597b3ac3671b9ad31924c28b6557d0ee5284" name = "golang.org/x/crypto" packages = [ "bcrypt", "blowfish", "chacha20poly1305", "curve25519", - "ed25519", - "ed25519/internal/edwards25519", "hkdf", "internal/chacha20", "internal/subtle", diff --git a/Gopkg.toml b/Gopkg.toml index baf01473a957..0902bc412607 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -57,7 +57,7 @@ [[override]] name = "github.com/tendermint/tendermint" - version = "=0.26.0-dev0" + version = "=0.25.1-rc0" ## deps without releases: From f8c2e75918de15ecec0d34cf325761472a56966c Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Wed, 17 Oct 2018 21:47:17 -0700 Subject: [PATCH 51/83] Fix {base,demo}coin tests --- cmd/gaia/init/init.go | 45 ++++++++------- cmd/gaia/init/init_test.go | 3 +- cmd/gaia/init/testnet.go | 2 +- examples/basecoin/cli_test/cli_test.go | 11 ++-- examples/basecoin/cmd/basecoind/main.go | 74 ++++++++++++++++++++++++- examples/democoin/cli_test/cli_test.go | 11 ++-- examples/democoin/cmd/democoind/main.go | 73 +++++++++++++++++++++++- server/init.go | 59 +++++++++++++------- server/test_helpers.go | 3 +- 9 files changed, 225 insertions(+), 56 deletions(-) diff --git a/cmd/gaia/init/init.go b/cmd/gaia/init/init.go index 0ccebe0cb414..4d7c0b4eff0e 100644 --- a/cmd/gaia/init/init.go +++ b/cmd/gaia/init/init.go @@ -26,20 +26,20 @@ import ( ) const ( - flagChainID = "chain-id" - flagWithTxs = "with-txs" - flagOverwrite = "overwrite" - flagClientHome = "home-client" - flagOWK = "owk" + flagChainID = "chain-id" + flagWithTxs = "with-txs" + flagOverwrite = "overwrite" + flagClientHome = "home-client" + flagOWK = "owk" ) type InitConfig struct { - ChainID string - GenTxsDir string - Moniker string - ClientHome string - WithTxs bool - Overwrite bool + ChainID string + GenTxsDir string + Moniker string + ClientHome string + WithTxs bool + Overwrite bool OverwriteKeys bool } @@ -59,12 +59,12 @@ func InitCmd(ctx *server.Context, cdc *codec.Codec, appInit server.AppInit) *cob chainID = fmt.Sprintf("test-chain-%v", common.RandStr(6)) } initCfg := InitConfig{ - ChainID: chainID, - GenTxsDir: viper.GetString(filepath.Join(config.RootDir, "config", "gentx")), - Moniker: viper.GetString(client.FlagName), - ClientHome: viper.GetString(flagClientHome), - WithTxs: viper.GetBool(flagWithTxs), - Overwrite: viper.GetBool(flagOverwrite), + ChainID: chainID, + GenTxsDir: viper.GetString(filepath.Join(config.RootDir, "config", "gentx")), + Moniker: viper.GetString(client.FlagName), + ClientHome: viper.GetString(flagClientHome), + WithTxs: viper.GetBool(flagWithTxs), + Overwrite: viper.GetBool(flagOverwrite), OverwriteKeys: viper.GetBool(flagOWK), } nodeID, appMessage, err := initWithConfig(cdc, config, initCfg) @@ -109,7 +109,7 @@ func initWithConfig(cdc *codec.Codec, config *cfg.Config, initCfg InitConfig) ( } nodeID = string(nodeKey.ID()) privValFile := config.PrivValidatorFile() - valPubKey := readOrCreatePrivValidator(privValFile) + valPubKey := ReadOrCreatePrivValidator(privValFile) genFile := config.GenesisFile() if !initCfg.Overwrite && common.FileExists(genFile) { err = fmt.Errorf("genesis.json file already exists: %v", genFile) @@ -198,16 +198,15 @@ func initWithConfig(cdc *codec.Codec, config *cfg.Config, initCfg InitConfig) ( if err != nil { return } - err = writeGenesisFile(cdc, genFile, chainID, nil, appState) + err = WriteGenesisFile(cdc, genFile, chainID, nil, appState) return } - -// writeGenesisFile creates and writes the genesis configuration to disk. An +// WriteGenesisFile creates and writes the genesis configuration to disk. An // error is returned if building or writing the configuration to file fails. // nolint: unparam -func writeGenesisFile(cdc *codec.Codec, genesisFile, chainID string, validators []types.GenesisValidator, appState json.RawMessage) error { +func WriteGenesisFile(cdc *codec.Codec, genesisFile, chainID string, validators []types.GenesisValidator, appState json.RawMessage) error { genDoc := types.GenesisDoc{ ChainID: chainID, Validators: validators, @@ -222,7 +221,7 @@ func writeGenesisFile(cdc *codec.Codec, genesisFile, chainID string, validators } // read of create the private key file for this config -func readOrCreatePrivValidator(privValFile string) crypto.PubKey { +func ReadOrCreatePrivValidator(privValFile string) crypto.PubKey { // private validator var privValidator *privval.FilePV if common.FileExists(privValFile) { diff --git a/cmd/gaia/init/init_test.go b/cmd/gaia/init/init_test.go index 37db7d7927c1..f5ad22d4b77a 100644 --- a/cmd/gaia/init/init_test.go +++ b/cmd/gaia/init/init_test.go @@ -2,6 +2,7 @@ package init import ( "bytes" + "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/cmd/gaia/app" "github.com/tendermint/tendermint/libs/cli" "io" @@ -97,7 +98,7 @@ func TestStartStandAlone(t *testing.T) { os.RemoveAll(home) }() viper.Set(cli.HomeFlag, home) - viper.Set(server.FlagName, "moniker") + viper.Set(client.FlagName, "moniker") defer setupClientHome(t)() logger := log.NewNopLogger() diff --git a/cmd/gaia/init/testnet.go b/cmd/gaia/init/testnet.go index 0c8c02712935..91fe03f2ff59 100644 --- a/cmd/gaia/init/testnet.go +++ b/cmd/gaia/init/testnet.go @@ -145,7 +145,7 @@ func testnetWithConfig(config *cfg.Config, cdc *codec.Codec, appInit server.AppI return err } - valPubKey := readOrCreatePrivValidator(config.PrivValidatorFile()) + valPubKey := ReadOrCreatePrivValidator(config.PrivValidatorFile()) msg := stake.NewMsgCreateValidator( sdk.ValAddress(addr), valPubKey, diff --git a/examples/basecoin/cli_test/cli_test.go b/examples/basecoin/cli_test/cli_test.go index f74947cf662a..3b82325cd118 100644 --- a/examples/basecoin/cli_test/cli_test.go +++ b/examples/basecoin/cli_test/cli_test.go @@ -6,6 +6,7 @@ import ( "os" "testing" + "github.com/cosmos/cosmos-sdk/cmd/gaia/app" "github.com/cosmos/cosmos-sdk/server" "github.com/cosmos/cosmos-sdk/tests" "github.com/stretchr/testify/require" @@ -13,10 +14,11 @@ import ( var ( basecoindHome = "" + basecliHome = "" ) func init() { - basecoindHome = getTestingHomeDir() + basecoindHome, basecliHome = getTestingHomeDirs() } func TestInitStartSequence(t *testing.T) { @@ -32,7 +34,7 @@ func executeInit(t *testing.T) { chainID string initRes map[string]json.RawMessage ) - _, stderr := tests.ExecuteT(t, fmt.Sprintf("basecoind --home=%s init", basecoindHome), "") + _, stderr := tests.ExecuteT(t, fmt.Sprintf("basecoind --home=%s --home-client=%s init", basecoindHome, basecliHome), app.DefaultKeyPass) err := json.Unmarshal([]byte(stderr), &initRes) require.NoError(t, err) err = json.Unmarshal(initRes["chain_id"], &chainID) @@ -45,8 +47,9 @@ func executeStart(t *testing.T, servAddr, port string) { tests.WaitForTMStart(port) } -func getTestingHomeDir() string { +func getTestingHomeDirs() (string, string) { tmpDir := os.TempDir() basecoindHome := fmt.Sprintf("%s%s.test_basecoind", tmpDir, string(os.PathSeparator)) - return basecoindHome + basecliHome := fmt.Sprintf("%s%s.test_basecli", tmpDir, string(os.PathSeparator)) + return basecoindHome, basecliHome } diff --git a/examples/basecoin/cmd/basecoind/main.go b/examples/basecoin/cmd/basecoind/main.go index 36219354449b..f29645b26cd2 100644 --- a/examples/basecoin/cmd/basecoind/main.go +++ b/examples/basecoin/cmd/basecoind/main.go @@ -2,23 +2,32 @@ package main import ( "encoding/json" + "fmt" + "github.com/tendermint/tendermint/p2p" "io" "os" "github.com/cosmos/cosmos-sdk/baseapp" gaiaInit "github.com/cosmos/cosmos-sdk/cmd/gaia/init" + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/examples/basecoin/app" "github.com/cosmos/cosmos-sdk/server" "github.com/spf13/cobra" "github.com/spf13/viper" abci "github.com/tendermint/tendermint/abci/types" "github.com/tendermint/tendermint/libs/cli" + "github.com/tendermint/tendermint/libs/common" dbm "github.com/tendermint/tendermint/libs/db" "github.com/tendermint/tendermint/libs/log" tmtypes "github.com/tendermint/tendermint/types" ) +const ( + flagClientHome = "home-client" +) + func main() { cdc := app.MakeCodec() ctx := server.NewDefaultContext() @@ -30,7 +39,7 @@ func main() { } appInit := server.DefaultAppInit - rootCmd.AddCommand(gaiaInit.InitCmd(ctx, cdc, appInit)) + rootCmd.AddCommand(InitCmd(ctx, cdc, appInit)) rootCmd.AddCommand(gaiaInit.TestnetFilesCmd(ctx, cdc, appInit)) server.AddCommands(ctx, cdc, rootCmd, appInit, @@ -47,6 +56,68 @@ func main() { } } +// get cmd to initialize all files for tendermint and application +// nolint: golint +func InitCmd(ctx *server.Context, cdc *codec.Codec, appInit server.AppInit) *cobra.Command { + cmd := &cobra.Command{ + Use: "init", + Short: "Initialize genesis config, priv-validator file, and p2p-node file", + Args: cobra.NoArgs, + RunE: func(_ *cobra.Command, _ []string) error { + + config := ctx.Config + config.SetRoot(viper.GetString(cli.HomeFlag)) + chainID := viper.GetString(client.FlagChainID) + if chainID == "" { + chainID = fmt.Sprintf("test-chain-%v", common.RandStr(6)) + } + + nodeKey, err := p2p.LoadOrGenNodeKey(config.NodeKeyFile()) + if err != nil { + return err + } + nodeID := string(nodeKey.ID()) + + pk := gaiaInit.ReadOrCreatePrivValidator(config.PrivValidatorFile()) + genTx, appMessage, validator, err := server.SimpleAppGenTx(cdc, pk) + if err != nil { + return err + } + + appState, err := appInit.AppGenState(cdc, []json.RawMessage{genTx}) + if err != nil { + return err + } + appStateJSON, err := cdc.MarshalJSON(appState) + if err != nil { + return err + } + + toPrint := struct { + ChainID string `json:"chain_id"` + NodeID string `json:"noide_id"` + AppMessage json.RawMessage `json:"app_message"` + }{ + chainID, + nodeID, + appMessage, + } + out, err := codec.MarshalJSONIndent(cdc, toPrint) + if err != nil { + return err + } + fmt.Fprintf(os.Stderr, "%s\n", string(out)) + return gaiaInit.WriteGenesisFile(cdc, config.GenesisFile(), chainID, []tmtypes.GenesisValidator{validator}, appStateJSON) + }, + } + + cmd.Flags().String(cli.HomeFlag, "", "node's home directory") + cmd.Flags().String(flagClientHome, "", "client's home directory") + cmd.Flags().String(client.FlagChainID, "", "genesis file chain-id, if left blank will be randomly created") + cmd.Flags().String(client.FlagName, "", "moniker") + return cmd +} + func newApp(logger log.Logger, db dbm.DB, storeTracer io.Writer) abci.Application { return app.NewBasecoinApp(logger, db, baseapp.SetPruning(viper.GetString("pruning"))) } @@ -55,3 +126,4 @@ func exportAppStateAndTMValidators(logger log.Logger, db dbm.DB, storeTracer io. bapp := app.NewBasecoinApp(logger, db) return bapp.ExportAppStateAndValidators() } + diff --git a/examples/democoin/cli_test/cli_test.go b/examples/democoin/cli_test/cli_test.go index 48c5108d5558..6826f98a1ff6 100644 --- a/examples/democoin/cli_test/cli_test.go +++ b/examples/democoin/cli_test/cli_test.go @@ -3,6 +3,7 @@ package clitest import ( "encoding/json" "fmt" + "github.com/cosmos/cosmos-sdk/cmd/gaia/app" "os" "testing" @@ -13,10 +14,11 @@ import ( var ( democoindHome = "" + democliHome = "" ) func init() { - democoindHome = getTestingHomeDir() + democoindHome, democliHome = getTestingHomeDirs() } func TestInitStartSequence(t *testing.T) { @@ -32,7 +34,7 @@ func executeInit(t *testing.T) { chainID string initRes map[string]json.RawMessage ) - _, stderr := tests.ExecuteT(t, fmt.Sprintf("democoind --home=%s init", democoindHome), "") + _, stderr := tests.ExecuteT(t, fmt.Sprintf("democoind --home=%s --home-client=%s init", democoindHome, democliHome), app.DefaultKeyPass) err := json.Unmarshal([]byte(stderr), &initRes) require.NoError(t, err) err = json.Unmarshal(initRes["chain_id"], &chainID) @@ -45,8 +47,9 @@ func executeStart(t *testing.T, servAddr, port string) { tests.WaitForTMStart(port) } -func getTestingHomeDir() string { +func getTestingHomeDirs() (string, string) { tmpDir := os.TempDir() democoindHome := fmt.Sprintf("%s%s.test_democoind", tmpDir, string(os.PathSeparator)) - return democoindHome + democliHome := fmt.Sprintf("%s%s.test_democli", tmpDir, string(os.PathSeparator)) + return democoindHome, democliHome } diff --git a/examples/democoin/cmd/democoind/main.go b/examples/democoin/cmd/democoind/main.go index 711d403f9391..5b260ca42372 100644 --- a/examples/democoin/cmd/democoind/main.go +++ b/examples/democoin/cmd/democoind/main.go @@ -2,6 +2,11 @@ package main import ( "encoding/json" + "fmt" + "github.com/cosmos/cosmos-sdk/client" + "github.com/spf13/viper" + "github.com/tendermint/tendermint/libs/common" + "github.com/tendermint/tendermint/p2p" "io" "os" @@ -19,6 +24,10 @@ import ( "github.com/cosmos/cosmos-sdk/server" ) +const ( + flagClientHome = "home-client" +) + // init parameters var CoolAppInit = server.AppInit{ AppGenState: CoolAppGenState, @@ -51,6 +60,68 @@ func CoolAppGenState(cdc *codec.Codec, appGenTxs []json.RawMessage) (appState js return } +// get cmd to initialize all files for tendermint and application +// nolint: golint +func InitCmd(ctx *server.Context, cdc *codec.Codec, appInit server.AppInit) *cobra.Command { + cmd := &cobra.Command{ + Use: "init", + Short: "Initialize genesis config, priv-validator file, and p2p-node file", + Args: cobra.NoArgs, + RunE: func(_ *cobra.Command, _ []string) error { + + config := ctx.Config + config.SetRoot(viper.GetString(cli.HomeFlag)) + chainID := viper.GetString(client.FlagChainID) + if chainID == "" { + chainID = fmt.Sprintf("test-chain-%v", common.RandStr(6)) + } + + nodeKey, err := p2p.LoadOrGenNodeKey(config.NodeKeyFile()) + if err != nil { + return err + } + nodeID := string(nodeKey.ID()) + + pk := gaiaInit.ReadOrCreatePrivValidator(config.PrivValidatorFile()) + genTx, appMessage, validator, err := server.SimpleAppGenTx(cdc, pk) + if err != nil { + return err + } + + appState, err := appInit.AppGenState(cdc, []json.RawMessage{genTx}) + if err != nil { + return err + } + appStateJSON, err := cdc.MarshalJSON(appState) + if err != nil { + return err + } + + toPrint := struct { + ChainID string `json:"chain_id"` + NodeID string `json:"noide_id"` + AppMessage json.RawMessage `json:"app_message"` + }{ + chainID, + nodeID, + appMessage, + } + out, err := codec.MarshalJSONIndent(cdc, toPrint) + if err != nil { + return err + } + fmt.Fprintf(os.Stderr, "%s\n", string(out)) + return gaiaInit.WriteGenesisFile(cdc, config.GenesisFile(), chainID, []tmtypes.GenesisValidator{validator}, appStateJSON) + }, + } + + cmd.Flags().String(cli.HomeFlag, "", "node's home directory") + cmd.Flags().String(flagClientHome, "", "client's home directory") + cmd.Flags().String(client.FlagChainID, "", "genesis file chain-id, if left blank will be randomly created") + cmd.Flags().String(client.FlagName, "", "moniker") + return cmd +} + func newApp(logger log.Logger, db dbm.DB, _ io.Writer) abci.Application { return app.NewDemocoinApp(logger, db) } @@ -70,7 +141,7 @@ func main() { PersistentPreRunE: server.PersistentPreRunEFn(ctx), } - rootCmd.AddCommand(gaiaInit.InitCmd(ctx, cdc, CoolAppInit)) + rootCmd.AddCommand(InitCmd(ctx, cdc, CoolAppInit)) rootCmd.AddCommand(gaiaInit.TestnetFilesCmd(ctx, cdc, CoolAppInit)) server.AddCommands(ctx, cdc, rootCmd, CoolAppInit, diff --git a/server/init.go b/server/init.go index c63c1346a7b7..42d58c143c2a 100644 --- a/server/init.go +++ b/server/init.go @@ -2,25 +2,17 @@ package server import ( "encoding/json" + "errors" "fmt" - "github.com/cosmos/cosmos-sdk/crypto/keys" - "github.com/cosmos/cosmos-sdk/x/auth" - "github.com/cosmos/cosmos-sdk/x/stake" - "github.com/pkg/errors" + "github.com/tendermint/tendermint/crypto" dbm "github.com/tendermint/tendermint/libs/db" + "github.com/tendermint/tendermint/types" clkeys "github.com/cosmos/cosmos-sdk/client/keys" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" -) - -// parameter names, init command -const ( - FlagName = "name" - FlagOverwrite = "overwrite" - FlagWithTxs = "with-txs" - FlagChainID = "chain-id" + tmtypes "github.com/tendermint/tendermint/types" ) // Storage for init command input parameters @@ -39,6 +31,11 @@ type AppInit struct { AppGenState func(cdc *codec.Codec, appGenTx []json.RawMessage) (appState json.RawMessage, err error) } +// SimpleGenTx is a simple genesis tx +type SimpleGenTx struct { + Addr sdk.AccAddress `json:"addr"` +} + //_____________________________________________________________________ // simple default application init @@ -46,6 +43,34 @@ var DefaultAppInit = AppInit{ AppGenState: SimpleAppGenState, } +// Generate a genesis transaction +func SimpleAppGenTx(cdc *codec.Codec, pk crypto.PubKey) (appGenTx, cliPrint json.RawMessage, validator types.GenesisValidator, err error) { + var addr sdk.AccAddress + var secret string + addr, secret, err = GenerateCoinKey() + if err != nil { + return + } + var bz []byte + simpleGenTx := SimpleGenTx{Addr: addr} + bz, err = cdc.MarshalJSON(simpleGenTx) + if err != nil { + return + } + appGenTx = json.RawMessage(bz) + mm := map[string]string{"secret": secret} + bz, err = cdc.MarshalJSON(mm) + if err != nil { + return + } + cliPrint = json.RawMessage(bz) + validator = tmtypes.GenesisValidator{ + PubKey: pk, + Power: 10, + } + return +} + // create the genesis app state func SimpleAppGenState(cdc *codec.Codec, appGenTxs []json.RawMessage) (appState json.RawMessage, err error) { @@ -54,18 +79,12 @@ func SimpleAppGenState(cdc *codec.Codec, appGenTxs []json.RawMessage) (appState return } - var tx auth.StdTx + var tx SimpleGenTx err = cdc.UnmarshalJSON(appGenTxs[0], &tx) if err != nil { return } - msgs := tx.GetMsgs() - if len(msgs) != 1 { - err = errors.New("must provide a single genesis message") - return - } - msg := msgs[0].(stake.MsgCreateValidator) appState = json.RawMessage(fmt.Sprintf(`{ "accounts": [{ "address": "%s", @@ -76,7 +95,7 @@ func SimpleAppGenState(cdc *codec.Codec, appGenTxs []json.RawMessage) (appState } ] }] -}`, msg.ValidatorAddr)) +}`, tx.Addr)) return } diff --git a/server/test_helpers.go b/server/test_helpers.go index 680d8876c18e..4347bad6c354 100644 --- a/server/test_helpers.go +++ b/server/test_helpers.go @@ -2,6 +2,7 @@ package server import ( "fmt" + "github.com/cosmos/cosmos-sdk/client" "io/ioutil" "net" "os" @@ -42,7 +43,7 @@ func SetupViper(t *testing.T) func() { rootDir, err := ioutil.TempDir("", "mock-sdk-cmd") require.Nil(t, err) viper.Set(cli.HomeFlag, rootDir) - viper.Set(FlagName, "moniker") + viper.Set(client.FlagName, "moniker") return func() { err := os.RemoveAll(rootDir) if err != nil { From a48bc86cd557b9f06a72dabde5d8bba825f72779 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Wed, 17 Oct 2018 22:03:41 -0700 Subject: [PATCH 52/83] Fix lintian warnings, formatting --- client/lcd/test_helpers.go | 7 ++-- cmd/gaia/app/app.go | 2 +- cmd/gaia/app/genesis.go | 49 ++----------------------- cmd/gaia/init/init.go | 6 +-- cmd/gaia/init/testnet.go | 18 ++++----- examples/basecoin/cmd/basecoind/main.go | 1 - x/stake/client/cli/flags.go | 2 +- 7 files changed, 21 insertions(+), 64 deletions(-) diff --git a/client/lcd/test_helpers.go b/client/lcd/test_helpers.go index a107d9246edf..ee7fdbdef5f7 100644 --- a/client/lcd/test_helpers.go +++ b/client/lcd/test_helpers.go @@ -28,6 +28,7 @@ import ( "github.com/spf13/viper" "github.com/stretchr/testify/require" + txbuilder "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder" abci "github.com/tendermint/tendermint/abci/types" tmcfg "github.com/tendermint/tendermint/config" "github.com/tendermint/tendermint/crypto" @@ -41,7 +42,6 @@ import ( "github.com/tendermint/tendermint/proxy" tmrpc "github.com/tendermint/tendermint/rpc/lib/server" tmtypes "github.com/tendermint/tendermint/types" - txbuilder "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder" ) // makePathname creates a unique pathname for each test. It will panic if it @@ -208,12 +208,13 @@ func InitializeTestLCD( genesisFile := config.GenesisFile() genDoc, err := tmtypes.GenesisDocFromFile(genesisFile) + require.Nil(t, err) genDoc.Validators = nil genDoc.SaveAs(genesisFile) genTxs := []json.RawMessage{} // append any additional (non-proposing) validators - for i:=0 ; i Date: Wed, 17 Oct 2018 22:19:36 -0700 Subject: [PATCH 53/83] Make linter happy --- cmd/gaia/init/init.go | 2 +- examples/basecoin/cmd/basecoind/main.go | 2 +- examples/democoin/cmd/democoind/main.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/gaia/init/init.go b/cmd/gaia/init/init.go index 3f4731c99bdc..11c3b6c83aec 100644 --- a/cmd/gaia/init/init.go +++ b/cmd/gaia/init/init.go @@ -44,7 +44,7 @@ type initConfig struct { } // get cmd to initialize all files for tendermint and application -// nolint: golint +// nolint func InitCmd(ctx *server.Context, cdc *codec.Codec, appInit server.AppInit) *cobra.Command { cmd := &cobra.Command{ Use: "init", diff --git a/examples/basecoin/cmd/basecoind/main.go b/examples/basecoin/cmd/basecoind/main.go index 5dc512534304..4dcd0741df50 100644 --- a/examples/basecoin/cmd/basecoind/main.go +++ b/examples/basecoin/cmd/basecoind/main.go @@ -57,7 +57,7 @@ func main() { } // get cmd to initialize all files for tendermint and application -// nolint: golint +// nolint: errcheck func InitCmd(ctx *server.Context, cdc *codec.Codec, appInit server.AppInit) *cobra.Command { cmd := &cobra.Command{ Use: "init", diff --git a/examples/democoin/cmd/democoind/main.go b/examples/democoin/cmd/democoind/main.go index 5b260ca42372..22b98cc61d81 100644 --- a/examples/democoin/cmd/democoind/main.go +++ b/examples/democoin/cmd/democoind/main.go @@ -61,7 +61,7 @@ func CoolAppGenState(cdc *codec.Codec, appGenTxs []json.RawMessage) (appState js } // get cmd to initialize all files for tendermint and application -// nolint: golint +// nolint: errcheck func InitCmd(ctx *server.Context, cdc *codec.Codec, appInit server.AppInit) *cobra.Command { cmd := &cobra.Command{ Use: "init", From 142984a97c6af33b7f8b3e89b5616fd015d127d6 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Wed, 17 Oct 2018 22:19:52 -0700 Subject: [PATCH 54/83] Run make format --- client/lcd/certificates.go | 2 +- x/auth/ante_test.go | 1 - x/distribution/types/validator_info.go | 6 +++--- x/stake/client/cli/tx.go | 1 - 4 files changed, 4 insertions(+), 6 deletions(-) diff --git a/client/lcd/certificates.go b/client/lcd/certificates.go index 1516ed35afaf..f47f2397c72e 100644 --- a/client/lcd/certificates.go +++ b/client/lcd/certificates.go @@ -43,7 +43,7 @@ func generateSelfSignedCert(host string) (certBytes []byte, priv *ecdsa.PrivateK KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign, ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, BasicConstraintsValid: true, - IsCA: true, + IsCA: true, } hosts := strings.Split(host, ",") for _, h := range hosts { diff --git a/x/auth/ante_test.go b/x/auth/ante_test.go index 2d60b28013ab..bacb3013f901 100644 --- a/x/auth/ante_test.go +++ b/x/auth/ante_test.go @@ -336,7 +336,6 @@ func TestAnteHandlerSequences(t *testing.T) { tx = newTestTx(ctx, msgs, privs, accnums, seqs, fee) checkValidTx(t, anteHandler, ctx, tx, false) - // replay fails checkInvalidTx(t, anteHandler, ctx, tx, false, sdk.CodeInvalidSequence) diff --git a/x/distribution/types/validator_info.go b/x/distribution/types/validator_info.go index c8a02569cba6..18aef8bde6e4 100644 --- a/x/distribution/types/validator_info.go +++ b/x/distribution/types/validator_info.go @@ -19,9 +19,9 @@ func NewValidatorDistInfo(operatorAddr sdk.ValAddress, currentHeight int64) Vali return ValidatorDistInfo{ OperatorAddr: operatorAddr, FeePoolWithdrawalHeight: currentHeight, - Pool: DecCoins{}, - PoolCommission: DecCoins{}, - DelAccum: NewTotalAccum(currentHeight), + Pool: DecCoins{}, + PoolCommission: DecCoins{}, + DelAccum: NewTotalAccum(currentHeight), } } diff --git a/x/stake/client/cli/tx.go b/x/stake/client/cli/tx.go index 00688f23c968..e74ca95f6a7a 100644 --- a/x/stake/client/cli/tx.go +++ b/x/stake/client/cli/tx.go @@ -15,7 +15,6 @@ import ( "github.com/spf13/viper" ) - // GetCmdCreateValidator implements the create validator command handler. func GetCmdCreateValidator(cdc *codec.Codec) *cobra.Command { cmd := &cobra.Command{ From 83f0503d9389036255e8c34a849aa9a1e1de316f Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Wed, 17 Oct 2018 22:38:00 -0700 Subject: [PATCH 55/83] Incorporate Jae/Chris comments --- cmd/gaia/app/app.go | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/cmd/gaia/app/app.go b/cmd/gaia/app/app.go index b53d94368392..f1518df691fa 100644 --- a/cmd/gaia/app/app.go +++ b/cmd/gaia/app/app.go @@ -3,14 +3,6 @@ package app import ( "encoding/json" "fmt" - abci "github.com/tendermint/tendermint/abci/types" - cmn "github.com/tendermint/tendermint/libs/common" - dbm "github.com/tendermint/tendermint/libs/db" - "github.com/tendermint/tendermint/libs/log" - tmtypes "github.com/tendermint/tendermint/types" - "io" - "os" - bam "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" @@ -21,6 +13,14 @@ import ( "github.com/cosmos/cosmos-sdk/x/params" "github.com/cosmos/cosmos-sdk/x/slashing" "github.com/cosmos/cosmos-sdk/x/stake" + abci "github.com/tendermint/tendermint/abci/types" + cmn "github.com/tendermint/tendermint/libs/common" + dbm "github.com/tendermint/tendermint/libs/db" + "github.com/tendermint/tendermint/libs/log" + tmtypes "github.com/tendermint/tendermint/types" + "io" + "os" + "sort" ) const ( @@ -247,10 +247,7 @@ func (app *GaiaApp) initChainer(ctx sdk.Context, req abci.RequestInitChain) abci if err != nil { panic(err) } - bz, err := app.cdc.MarshalBinary(tx) - if err != nil { - panic(err) - } + bz := app.cdc.MustMarshalBinary(tx) res := app.BaseApp.DeliverTx(bz) if !res.IsOK() { panic(res.Log) @@ -266,6 +263,13 @@ func (app *GaiaApp) initChainer(ctx sdk.Context, req abci.RequestInitChain) abci if len(req.Validators) != len(validators) { panic(fmt.Errorf("len(RequestInitChain.Validators) != len(validators) (%d != %d) ", len(req.Validators), len(validators))) } + sort.Sort(abci.ValidatorUpdates(req.Validators)) + sort.Sort(abci.ValidatorUpdates(validators)) + for i, val := range validators { + if !val.Equal(req.Validators[i]) { + panic(fmt.Errorf("validators[%d] != req.Validators[%d] ", i, i)) + } + } } return abci.ResponseInitChain{ From 67bbc1a3fa0d39450e2f3b0ba5c6f18f7d632cb1 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Wed, 17 Oct 2018 22:51:42 -0700 Subject: [PATCH 56/83] Combine --genesis-format and --generate-only --- x/stake/client/cli/tx.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/x/stake/client/cli/tx.go b/x/stake/client/cli/tx.go index e74ca95f6a7a..58fe774d0f05 100644 --- a/x/stake/client/cli/tx.go +++ b/x/stake/client/cli/tx.go @@ -92,11 +92,9 @@ func GetCmdCreateValidator(cdc *codec.Codec) *cobra.Command { if nodeID != "" && ip != "" { txBldr = txBldr.WithMemo(fmt.Sprintf("%s@%s:26656", nodeID, ip)) } - - return utils.PrintUnsignedStdTx(txBldr, cliCtx, []sdk.Msg{msg}, true) } - if cliCtx.GenerateOnly { + if viper.GetBool(FlagGenesisFormat) || cliCtx.GenerateOnly { return utils.PrintUnsignedStdTx(txBldr, cliCtx, []sdk.Msg{msg}, true) } From 7f2fab30133bd630506f9f984aa2c0c71421536f Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Wed, 17 Oct 2018 23:12:41 -0700 Subject: [PATCH 57/83] Improve sign command's --offline flag documentation --- client/utils/utils.go | 1 + x/auth/client/cli/sign.go | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/client/utils/utils.go b/client/utils/utils.go index a6287991dcdd..f5cf04168c48 100644 --- a/client/utils/utils.go +++ b/client/utils/utils.go @@ -107,6 +107,7 @@ func PrintUnsignedStdTx(txBldr authtxb.TxBuilder, cliCtx context.CLIContext, msg // SignStdTx appends a signature to a StdTx and returns a copy of a it. If appendSig // is false, it replaces the signatures already attached with the new signature. +// Don't perform online validation or lookups if offline is true. func SignStdTx(txBldr authtxb.TxBuilder, cliCtx context.CLIContext, name string, stdTx auth.StdTx, appendSig bool, offline bool) (auth.StdTx, error) { var signedStdTx auth.StdTx diff --git a/x/auth/client/cli/sign.go b/x/auth/client/cli/sign.go index 07191749010f..67c64dc2ad37 100644 --- a/x/auth/client/cli/sign.go +++ b/x/auth/client/cli/sign.go @@ -28,7 +28,11 @@ func GetSignCommand(codec *amino.Codec, decoder auth.AccountDecoder) *cobra.Comm Use: "sign ", Short: "Sign transactions generated offline", Long: `Sign transactions created with the --generate-only flag. -Read a transaction from , sign it, and print its JSON encoding.`, +Read a transaction from , sign it, and print its JSON encoding. + +The --offline flag makes sure that the client will not reach out to the local cache. +Thus account number or sequence number lookups will not be performed and it is +recommended to set such parameters manually.`, RunE: makeSignCmd(codec, decoder), Args: cobra.ExactArgs(1), } From e5addfdbdee2b85b918531adeba97f7dce9f518a Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Wed, 17 Oct 2018 23:29:47 -0700 Subject: [PATCH 58/83] Rename ProcessStdTx as per Jae's comment - drop TODO comment following conversation with Chris --- cmd/gaia/app/genesis.go | 7 +++++-- cmd/gaia/init/init.go | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/cmd/gaia/app/genesis.go b/cmd/gaia/app/genesis.go index 296128064903..5b8555bc71c1 100644 --- a/cmd/gaia/app/genesis.go +++ b/cmd/gaia/app/genesis.go @@ -175,9 +175,9 @@ func GaiaAppGenStateJSON(cdc *codec.Codec, appGenTxs []json.RawMessage) (appStat return } -// ProcessStdTxs processes and validates application's genesis StdTxs and returns the list of validators, +// CollectStdTxs processes and validates application's genesis StdTxs and returns the list of validators, // appGenTxs, and persistent peers required to generate genesis.json. -func ProcessStdTxs(moniker string, genTxsDir string, cdc *codec.Codec) ( +func CollectStdTxs(moniker string, genTxsDir string, cdc *codec.Codec) ( validators []tmtypes.GenesisValidator, appGenTxs []auth.StdTx, persistentPeers string, err error) { var fos []os.FileInfo fos, err = ioutil.ReadDir(genTxsDir) @@ -216,6 +216,9 @@ func ProcessStdTxs(moniker string, genTxsDir string, cdc *codec.Codec) ( err = errors.New("each genesis transaction must provide a single genesis message") return } + + // TODO: this could be decoupled from stake.MsgCreateValidator + // TODO: and we likely want to do it for real world Gaia msg := msgs[0].(stake.MsgCreateValidator) validators = append(validators, tmtypes.GenesisValidator{ PubKey: msg.PubKey, diff --git a/cmd/gaia/init/init.go b/cmd/gaia/init/init.go index 11c3b6c83aec..c79617e912a0 100644 --- a/cmd/gaia/init/init.go +++ b/cmd/gaia/init/init.go @@ -126,7 +126,7 @@ func initWithConfig(cdc *codec.Codec, config *cfg.Config, initCfg initConfig) ( chainID := initCfg.ChainID if initCfg.WithTxs { - _, appGenTxs, persistentPeers, err = app.ProcessStdTxs(moniker, initCfg.GenTxsDir, cdc) + _, appGenTxs, persistentPeers, err = app.CollectStdTxs(moniker, initCfg.GenTxsDir, cdc) if err != nil { return } From 05214f1fdd4fd5551e06f82ffdf9fb0590a40a49 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Wed, 17 Oct 2018 23:32:15 -0700 Subject: [PATCH 59/83] Remove dead code --- server/init.go | 9 --------- 1 file changed, 9 deletions(-) diff --git a/server/init.go b/server/init.go index 42d58c143c2a..65a896e2ac19 100644 --- a/server/init.go +++ b/server/init.go @@ -15,15 +15,6 @@ import ( tmtypes "github.com/tendermint/tendermint/types" ) -// Storage for init command input parameters -type InitConfig struct { - ChainID string - GenTxs bool - GenTxsDir string - Moniker string - Overwrite bool -} - // Core functionality passed from the application to the server init command type AppInit struct { // AppGenState creates the core parameters initialization. It takes in a From 88aafb3b9bb63907b224771084f2bd7222e20ba1 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Wed, 17 Oct 2018 23:34:09 -0700 Subject: [PATCH 60/83] Code cleanup --- server/config/config.go | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/server/config/config.go b/server/config/config.go index bd0d966e35b4..239097b13737 100644 --- a/server/config/config.go +++ b/server/config/config.go @@ -35,16 +35,3 @@ func (c *Config) MinimumFees() sdk.Coins { // DefaultConfig returns server's default configuration. func DefaultConfig() *Config { return &Config{BaseConfig{MinFees: defaultMinimumFees}} } - -//_____________________________________________________________________ - -// Configuration structure for command functions that share configuration. -// For example: init, init gen-tx and testnet commands need similar input and run the same code - -// Storage for init gen-tx command input parameters -type GenTx struct { - Name string - CliRoot string - Overwrite bool - IP string -} From d1773d88368645673117d8e6861e18a29372390f Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Wed, 17 Oct 2018 23:43:24 -0700 Subject: [PATCH 61/83] Moniker must be set --- cmd/gaia/init/init.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cmd/gaia/init/init.go b/cmd/gaia/init/init.go index c79617e912a0..498fdf47074c 100644 --- a/cmd/gaia/init/init.go +++ b/cmd/gaia/init/init.go @@ -98,6 +98,7 @@ func InitCmd(ctx *server.Context, cdc *codec.Codec, appInit server.AppInit) *cob cmd.Flags().String(client.FlagName, "", "moniker") cmd.Flags().String(flagClientHome, app.DefaultCLIHome, "client's home directory") cmd.Flags().Bool(flagOWK, false, "overwrite client's keys") + cmd.MarkFlagRequired(client.FlagName) return cmd } From 6c461e6fdaf19646b094d742bdcc78271832e585 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Wed, 17 Oct 2018 23:44:20 -0700 Subject: [PATCH 62/83] Code cleanup --- cmd/gaia/app/genesis.go | 6 ------ 1 file changed, 6 deletions(-) diff --git a/cmd/gaia/app/genesis.go b/cmd/gaia/app/genesis.go index 5b8555bc71c1..5a66b58c4044 100644 --- a/cmd/gaia/app/genesis.go +++ b/cmd/gaia/app/genesis.go @@ -104,12 +104,6 @@ func GaiaAppGenState(cdc *codec.Codec, appGenTxs []json.RawMessage) (genesisStat // create the genesis account, give'm few steaks and a buncha token with there name genaccs[i] = genesisAccountFromMsgCreateValidator(msg, freeFermionsAcc) stakeData.Pool.LooseTokens = stakeData.Pool.LooseTokens.Add(sdk.NewDecFromInt(freeFermionsAcc)) // increase the supply - - // add the validator - //if len(msg.Description.Moniker) > 0 { - // stakeData = addValidatorToStakeData(msg, stakeData) - //} - } // create the final app state From a6340c433b097a1012b6be502639fafdb06decfb Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Wed, 17 Oct 2018 23:55:28 -0700 Subject: [PATCH 63/83] Improve examples flags handling --- examples/basecoin/app/app.go | 6 ++++++ examples/basecoin/cli_test/cli_test.go | 2 +- examples/basecoin/cmd/basecli/main.go | 4 +--- examples/basecoin/cmd/basecoind/main.go | 7 ++++--- examples/democoin/app/app.go | 6 ++++++ examples/democoin/cli_test/cli_test.go | 2 +- examples/democoin/cmd/democli/main.go | 4 +--- examples/democoin/cmd/democoind/main.go | 7 ++++--- 8 files changed, 24 insertions(+), 14 deletions(-) diff --git a/examples/basecoin/app/app.go b/examples/basecoin/app/app.go index 657cbcf0fdb8..76dfbb1def3c 100644 --- a/examples/basecoin/app/app.go +++ b/examples/basecoin/app/app.go @@ -2,6 +2,7 @@ package app import ( "encoding/json" + "os" bam "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/codec" @@ -21,6 +22,11 @@ const ( appName = "BasecoinApp" ) +var ( + DefaultCLIHome = os.ExpandEnv("$HOME/.basecli") + DefaultNodeHome = os.ExpandEnv("$HOME/.basecoind") +) + // BasecoinApp implements an extended ABCI application. It contains a BaseApp, // a codec for serialization, KVStore keys for multistore state management, and // various mappers and keepers to manage getting, setting, and serializing the diff --git a/examples/basecoin/cli_test/cli_test.go b/examples/basecoin/cli_test/cli_test.go index 3b82325cd118..635b54c3c015 100644 --- a/examples/basecoin/cli_test/cli_test.go +++ b/examples/basecoin/cli_test/cli_test.go @@ -34,7 +34,7 @@ func executeInit(t *testing.T) { chainID string initRes map[string]json.RawMessage ) - _, stderr := tests.ExecuteT(t, fmt.Sprintf("basecoind --home=%s --home-client=%s init", basecoindHome, basecliHome), app.DefaultKeyPass) + _, stderr := tests.ExecuteT(t, fmt.Sprintf("basecoind --home=%s --home-client=%s init --name=test", basecoindHome, basecliHome), app.DefaultKeyPass) err := json.Unmarshal([]byte(stderr), &initRes) require.NoError(t, err) err = json.Unmarshal(initRes["chain_id"], &chainID) diff --git a/examples/basecoin/cmd/basecli/main.go b/examples/basecoin/cmd/basecli/main.go index cbfae5fe0cb9..94c5c6e01692 100644 --- a/examples/basecoin/cmd/basecli/main.go +++ b/examples/basecoin/cmd/basecli/main.go @@ -1,8 +1,6 @@ package main import ( - "os" - "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/keys" "github.com/cosmos/cosmos-sdk/client/lcd" @@ -86,7 +84,7 @@ func main() { ) // prepare and add flags - executor := cli.PrepareMainCmd(rootCmd, "BC", os.ExpandEnv("$HOME/.basecli")) + executor := cli.PrepareMainCmd(rootCmd, "BC", app.DefaultCLIHome) err := executor.Execute() if err != nil { // Note: Handle with #870 diff --git a/examples/basecoin/cmd/basecoind/main.go b/examples/basecoin/cmd/basecoind/main.go index 4dcd0741df50..0d821de48e47 100644 --- a/examples/basecoin/cmd/basecoind/main.go +++ b/examples/basecoin/cmd/basecoind/main.go @@ -111,10 +111,11 @@ func InitCmd(ctx *server.Context, cdc *codec.Codec, appInit server.AppInit) *cob }, } - cmd.Flags().String(cli.HomeFlag, "", "node's home directory") - cmd.Flags().String(flagClientHome, "", "client's home directory") + cmd.Flags().String(cli.HomeFlag, app.DefaultNodeHome, "node's home directory") + cmd.Flags().String(flagClientHome, app.DefaultCLIHome, "client's home directory") cmd.Flags().String(client.FlagChainID, "", "genesis file chain-id, if left blank will be randomly created") - cmd.Flags().String(client.FlagName, "", "moniker") + cmd.Flags().String(client.FlagName, "", "validator's moniker") + cmd.MarkFlagRequired(client.FlagName) return cmd } diff --git a/examples/democoin/app/app.go b/examples/democoin/app/app.go index 0449f6251edb..127d80c39178 100644 --- a/examples/democoin/app/app.go +++ b/examples/democoin/app/app.go @@ -2,6 +2,7 @@ package app import ( "encoding/json" + "os" abci "github.com/tendermint/tendermint/abci/types" cmn "github.com/tendermint/tendermint/libs/common" @@ -27,6 +28,11 @@ const ( appName = "DemocoinApp" ) +var ( + DefaultCLIHome = os.ExpandEnv("$HOME/.democli") + DefaultNodeHome = os.ExpandEnv("$HOME/.democoind") +) + // Extended ABCI application type DemocoinApp struct { *bam.BaseApp diff --git a/examples/democoin/cli_test/cli_test.go b/examples/democoin/cli_test/cli_test.go index 6826f98a1ff6..8df97c987e82 100644 --- a/examples/democoin/cli_test/cli_test.go +++ b/examples/democoin/cli_test/cli_test.go @@ -34,7 +34,7 @@ func executeInit(t *testing.T) { chainID string initRes map[string]json.RawMessage ) - _, stderr := tests.ExecuteT(t, fmt.Sprintf("democoind --home=%s --home-client=%s init", democoindHome, democliHome), app.DefaultKeyPass) + _, stderr := tests.ExecuteT(t, fmt.Sprintf("democoind --home=%s --home-client=%s init --name=test", democoindHome, democliHome), app.DefaultKeyPass) err := json.Unmarshal([]byte(stderr), &initRes) require.NoError(t, err) err = json.Unmarshal(initRes["chain_id"], &chainID) diff --git a/examples/democoin/cmd/democli/main.go b/examples/democoin/cmd/democli/main.go index 43f86504eb27..08f131168710 100644 --- a/examples/democoin/cmd/democli/main.go +++ b/examples/democoin/cmd/democli/main.go @@ -1,8 +1,6 @@ package main import ( - "os" - "github.com/spf13/cobra" "github.com/tendermint/tendermint/libs/cli" @@ -91,7 +89,7 @@ func main() { ) // prepare and add flags - executor := cli.PrepareMainCmd(rootCmd, "BC", os.ExpandEnv("$HOME/.democli")) + executor := cli.PrepareMainCmd(rootCmd, "BC", app.DefaultCLIHome) err := executor.Execute() if err != nil { // handle with #870 diff --git a/examples/democoin/cmd/democoind/main.go b/examples/democoin/cmd/democoind/main.go index 22b98cc61d81..faa8c1cfa717 100644 --- a/examples/democoin/cmd/democoind/main.go +++ b/examples/democoin/cmd/democoind/main.go @@ -115,10 +115,11 @@ func InitCmd(ctx *server.Context, cdc *codec.Codec, appInit server.AppInit) *cob }, } - cmd.Flags().String(cli.HomeFlag, "", "node's home directory") - cmd.Flags().String(flagClientHome, "", "client's home directory") + cmd.Flags().String(cli.HomeFlag, app.DefaultNodeHome, "node's home directory") + cmd.Flags().String(flagClientHome, app.DefaultCLIHome, "client's home directory") cmd.Flags().String(client.FlagChainID, "", "genesis file chain-id, if left blank will be randomly created") - cmd.Flags().String(client.FlagName, "", "moniker") + cmd.Flags().String(client.FlagName, "", "validator's moniker") + cmd.MarkFlagRequired(client.FlagName) return cmd } From b050686534ce4ae8519a89b08beecd4f938fd606 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Thu, 18 Oct 2018 09:50:40 -0700 Subject: [PATCH 64/83] Make linter happy --- examples/basecoin/app/app.go | 1 + examples/democoin/app/app.go | 1 + 2 files changed, 2 insertions(+) diff --git a/examples/basecoin/app/app.go b/examples/basecoin/app/app.go index 76dfbb1def3c..3d9ac81736c4 100644 --- a/examples/basecoin/app/app.go +++ b/examples/basecoin/app/app.go @@ -22,6 +22,7 @@ const ( appName = "BasecoinApp" ) +// default home directories for expected binaries var ( DefaultCLIHome = os.ExpandEnv("$HOME/.basecli") DefaultNodeHome = os.ExpandEnv("$HOME/.basecoind") diff --git a/examples/democoin/app/app.go b/examples/democoin/app/app.go index 127d80c39178..97dcf4e3584b 100644 --- a/examples/democoin/app/app.go +++ b/examples/democoin/app/app.go @@ -28,6 +28,7 @@ const ( appName = "DemocoinApp" ) +// default home directories for expected binaries var ( DefaultCLIHome = os.ExpandEnv("$HOME/.democli") DefaultNodeHome = os.ExpandEnv("$HOME/.democoind") From b87b6d4193898a9890864acea43e2b9145abc049 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Thu, 18 Oct 2018 09:57:38 -0700 Subject: [PATCH 65/83] Replace path with filepath --- cmd/gaia/app/genesis.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/gaia/app/genesis.go b/cmd/gaia/app/genesis.go index 5a66b58c4044..6dfec4a7ed9f 100644 --- a/cmd/gaia/app/genesis.go +++ b/cmd/gaia/app/genesis.go @@ -6,7 +6,7 @@ import ( "fmt" "io/ioutil" "os" - "path" + "path/filepath" "sort" "strings" @@ -181,8 +181,8 @@ func CollectStdTxs(moniker string, genTxsDir string, cdc *codec.Codec) ( var addresses []string for _, fo := range fos { - filename := path.Join(genTxsDir, fo.Name()) - if !fo.IsDir() && (path.Ext(filename) != ".json") { + filename := filepath.Join(genTxsDir, fo.Name()) + if !fo.IsDir() && (filepath.Ext(filename) != ".json") { continue } From 9c5d1c4e790ac2c1c858294526b5ac8250fa7c02 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Thu, 18 Oct 2018 10:05:25 -0700 Subject: [PATCH 66/83] Call app.slashingKeeper.AddValidators() even if len(txs) == 0 --- cmd/gaia/app/app.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/gaia/app/app.go b/cmd/gaia/app/app.go index f1518df691fa..859ae583ad31 100644 --- a/cmd/gaia/app/app.go +++ b/cmd/gaia/app/app.go @@ -255,8 +255,8 @@ func (app *GaiaApp) initChainer(ctx sdk.Context, req abci.RequestInitChain) abci } validators = app.stakeKeeper.ApplyAndReturnValidatorSetUpdates(ctx) - app.slashingKeeper.AddValidators(ctx, validators) } + app.slashingKeeper.AddValidators(ctx, validators) // sanity check if len(req.Validators) > 0 { From a33a88028b2648cbb7e539ba6e8a03882aeb1c2d Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Thu, 18 Oct 2018 12:06:12 -0700 Subject: [PATCH 67/83] Refactoring, introduce gaiad init --skip-genesis, code cleanup --- cmd/gaia/init/init.go | 86 ++++++++++++++++--------- cmd/gaia/init/init_test.go | 16 +++++ cmd/gaia/init/testnet.go | 8 ++- examples/basecoin/cmd/basecoind/main.go | 2 +- examples/democoin/cmd/democoind/main.go | 2 +- 5 files changed, 81 insertions(+), 33 deletions(-) diff --git a/cmd/gaia/init/init.go b/cmd/gaia/init/init.go index 498fdf47074c..4bdcdb3a87de 100644 --- a/cmd/gaia/init/init.go +++ b/cmd/gaia/init/init.go @@ -26,11 +26,12 @@ import ( ) const ( - flagChainID = "chain-id" - flagWithTxs = "with-txs" - flagOverwrite = "overwrite" - flagClientHome = "home-client" - flagOWK = "owk" + flagChainID = "chain-id" + flagWithTxs = "with-txs" + flagOverwrite = "overwrite" + flagClientHome = "home-client" + flagOWK = "owk" + flagSkipGenesis = "skip-genesis" ) type initConfig struct { @@ -41,6 +42,24 @@ type initConfig struct { WithTxs bool Overwrite bool OverwriteKeys bool + NodeID string + ValPubKey crypto.PubKey +} + +type printInfo struct { + ChainID string `json:"chain_id"` + NodeID string `json:"node_id"` + AppMessage json.RawMessage `json:"app_message"` +} + +// nolint: errcheck +func displayInfo(cdc *codec.Codec, info printInfo) error { + out, err := codec.MarshalJSONIndent(cdc, info) + if err != nil { + return err + } + fmt.Fprintf(os.Stderr, "%s\n", string(out)) + return nil } // get cmd to initialize all files for tendermint and application @@ -48,7 +67,9 @@ type initConfig struct { func InitCmd(ctx *server.Context, cdc *codec.Codec, appInit server.AppInit) *cobra.Command { cmd := &cobra.Command{ Use: "init", - Short: "Initialize genesis config, priv-validator file, and p2p-node file", + Short: "Initialize private validator, p2p, genesis, and application configuration files", + Long: `This command initializes validators's and node's configuration files. +Genesis file will not be generated if run with --skip-genesis.`, Args: cobra.NoArgs, RunE: func(_ *cobra.Command, _ []string) error { @@ -58,6 +79,18 @@ func InitCmd(ctx *server.Context, cdc *codec.Codec, appInit server.AppInit) *cob if chainID == "" { chainID = fmt.Sprintf("test-chain-%v", common.RandStr(6)) } + nodeID, valPubKey, err := initNodeValidatorFiles(config) + if err != nil { + return err + } + toPrint := printInfo{ + ChainID: chainID, + NodeID: nodeID, + } + if viper.GetBool(flagSkipGenesis) { + return displayInfo(cdc, toPrint) + } + initCfg := initConfig{ ChainID: chainID, GenTxsDir: viper.GetString(filepath.Join(config.RootDir, "config", "gentx")), @@ -66,28 +99,17 @@ func InitCmd(ctx *server.Context, cdc *codec.Codec, appInit server.AppInit) *cob WithTxs: viper.GetBool(flagWithTxs), Overwrite: viper.GetBool(flagOverwrite), OverwriteKeys: viper.GetBool(flagOWK), + NodeID: nodeID, + ValPubKey: valPubKey, } - nodeID, appMessage, err := initWithConfig(cdc, config, initCfg) + appMessage, err := initWithConfig(cdc, config, initCfg) // print out some key information if err != nil { return err } - toPrint := struct { - ChainID string `json:"chain_id"` - NodeID string `json:"node_id"` - AppMessage json.RawMessage `json:"app_message"` - }{ - chainID, - nodeID, - appMessage, - } - out, err := codec.MarshalJSONIndent(cdc, toPrint) - if err != nil { - return err - } - fmt.Fprintf(os.Stderr, "%s\n", string(out)) - return nil + toPrint.AppMessage = appMessage + return displayInfo(cdc, toPrint) }, } @@ -98,19 +120,23 @@ func InitCmd(ctx *server.Context, cdc *codec.Codec, appInit server.AppInit) *cob cmd.Flags().String(client.FlagName, "", "moniker") cmd.Flags().String(flagClientHome, app.DefaultCLIHome, "client's home directory") cmd.Flags().Bool(flagOWK, false, "overwrite client's keys") + cmd.Flags().Bool(flagSkipGenesis, false, "do not create genesis.json") cmd.MarkFlagRequired(client.FlagName) return cmd } -func initWithConfig(cdc *codec.Codec, config *cfg.Config, initCfg initConfig) ( - nodeID string, appMessage json.RawMessage, err error) { +func initNodeValidatorFiles(config *cfg.Config) (nodeID string, valPubKey crypto.PubKey, err error) { nodeKey, err := p2p.LoadOrGenNodeKey(config.NodeKeyFile()) if err != nil { return } nodeID = string(nodeKey.ID()) - privValFile := config.PrivValidatorFile() - valPubKey := ReadOrCreatePrivValidator(privValFile) + valPubKey = ReadOrCreatePrivValidator(config.PrivValidatorFile()) + return +} + +func initWithConfig(cdc *codec.Codec, config *cfg.Config, initCfg initConfig) ( + appMessage json.RawMessage, err error) { genFile := config.GenesisFile() if !initCfg.Overwrite && common.FileExists(genFile) { err = fmt.Errorf("genesis.json file already exists: %v", genFile) @@ -150,7 +176,7 @@ func initWithConfig(cdc *codec.Codec, config *cfg.Config, initCfg initConfig) ( if err != nil { return } - memo := fmt.Sprintf("%s@%s:26656", nodeID, ip) + memo := fmt.Sprintf("%s@%s:26656", initCfg.NodeID, ip) buf := client.BufferStdin() prompt := fmt.Sprintf("Password for account '%s' (default %s):", moniker, app.DefaultKeyPass) keyPass, err = client.GetPassword(prompt, buf) @@ -175,7 +201,7 @@ func initWithConfig(cdc *codec.Codec, config *cfg.Config, initCfg initConfig) ( msg := stake.NewMsgCreateValidator( sdk.ValAddress(addr), - valPubKey, + initCfg.ValPubKey, sdk.NewInt64Coin("steak", 100), stake.NewDescription(moniker, "", "", ""), stake.NewCommissionMsg(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()), @@ -199,7 +225,7 @@ func initWithConfig(cdc *codec.Codec, config *cfg.Config, initCfg initConfig) ( if err != nil { return } - err = WriteGenesisFile(cdc, genFile, chainID, nil, appState) + err = WriteGenesisFile(genFile, chainID, nil, appState) return } @@ -207,7 +233,7 @@ func initWithConfig(cdc *codec.Codec, config *cfg.Config, initCfg initConfig) ( // WriteGenesisFile creates and writes the genesis configuration to disk. An // error is returned if building or writing the configuration to file fails. // nolint: unparam -func WriteGenesisFile(cdc *codec.Codec, genesisFile, chainID string, validators []types.GenesisValidator, appState json.RawMessage) error { +func WriteGenesisFile(genesisFile, chainID string, validators []types.GenesisValidator, appState json.RawMessage) error { genDoc := types.GenesisDoc{ ChainID: chainID, Validators: validators, diff --git a/cmd/gaia/init/init_test.go b/cmd/gaia/init/init_test.go index f5ad22d4b77a..f2d2125e6d3b 100644 --- a/cmd/gaia/init/init_test.go +++ b/cmd/gaia/init/init_test.go @@ -128,3 +128,19 @@ func TestStartStandAlone(t *testing.T) { svr.Stop() } } + +func TestInitNodeValidatorFiles(t *testing.T) { + home, err := ioutil.TempDir("", "mock-sdk-cmd") + require.Nil(t, err) + defer func() { + os.RemoveAll(home) + }() + viper.Set(cli.HomeFlag, home) + viper.Set(client.FlagName, "moniker") + cfg, err := tcmd.ParseConfig() + require.Nil(t, err) + nodeID, valPubKey, err := initNodeValidatorFiles(cfg) + require.Nil(t, err) + require.NotEqual(t, "", nodeID) + require.NotEqual(t, 0, len(valPubKey.Bytes())) +} diff --git a/cmd/gaia/init/testnet.go b/cmd/gaia/init/testnet.go index 5e2cc2d13683..c707de1f9ca1 100644 --- a/cmd/gaia/init/testnet.go +++ b/cmd/gaia/init/testnet.go @@ -182,6 +182,10 @@ func testnetWithConfig(config *cfg.Config, cdc *codec.Codec, appInit server.AppI config.Moniker = nodeDirName config.SetRoot(nodeDir) + nodeID, valPubKey, err := initNodeValidatorFiles(config) + if err != nil { + return err + } // Run `init` and generate genesis.json and config.toml initCfg := initConfig{ ChainID: chainID, @@ -190,8 +194,10 @@ func testnetWithConfig(config *cfg.Config, cdc *codec.Codec, appInit server.AppI WithTxs: true, Overwrite: true, OverwriteKeys: false, + NodeID: nodeID, + ValPubKey: valPubKey, } - _, _, err := initWithConfig(cdc, config, initCfg) + _, err = initWithConfig(cdc, config, initCfg) if err != nil { return err } diff --git a/examples/basecoin/cmd/basecoind/main.go b/examples/basecoin/cmd/basecoind/main.go index 0d821de48e47..ac79eaad06c4 100644 --- a/examples/basecoin/cmd/basecoind/main.go +++ b/examples/basecoin/cmd/basecoind/main.go @@ -107,7 +107,7 @@ func InitCmd(ctx *server.Context, cdc *codec.Codec, appInit server.AppInit) *cob return err } fmt.Fprintf(os.Stderr, "%s\n", string(out)) - return gaiaInit.WriteGenesisFile(cdc, config.GenesisFile(), chainID, []tmtypes.GenesisValidator{validator}, appStateJSON) + return gaiaInit.WriteGenesisFile(config.GenesisFile(), chainID, []tmtypes.GenesisValidator{validator}, appStateJSON) }, } diff --git a/examples/democoin/cmd/democoind/main.go b/examples/democoin/cmd/democoind/main.go index faa8c1cfa717..8dfa2f95f627 100644 --- a/examples/democoin/cmd/democoind/main.go +++ b/examples/democoin/cmd/democoind/main.go @@ -111,7 +111,7 @@ func InitCmd(ctx *server.Context, cdc *codec.Codec, appInit server.AppInit) *cob return err } fmt.Fprintf(os.Stderr, "%s\n", string(out)) - return gaiaInit.WriteGenesisFile(cdc, config.GenesisFile(), chainID, []tmtypes.GenesisValidator{validator}, appStateJSON) + return gaiaInit.WriteGenesisFile(config.GenesisFile(), chainID, []tmtypes.GenesisValidator{validator}, appStateJSON) }, } From 1db33c3d88221be3e523e5e863fe587b03dc8450 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Thu, 18 Oct 2018 16:04:37 -0700 Subject: [PATCH 68/83] CLI refactoring --- cmd/gaia/init/init.go | 48 ++++++++++++++++++++++++-------------- cmd/gaia/init/init_test.go | 2 +- cmd/gaia/init/testnet.go | 23 ++++++++---------- 3 files changed, 42 insertions(+), 31 deletions(-) diff --git a/cmd/gaia/init/init.go b/cmd/gaia/init/init.go index 4bdcdb3a87de..9ac495d61f70 100644 --- a/cmd/gaia/init/init.go +++ b/cmd/gaia/init/init.go @@ -32,21 +32,23 @@ const ( flagClientHome = "home-client" flagOWK = "owk" flagSkipGenesis = "skip-genesis" + flagMoniker = "moniker" ) type initConfig struct { ChainID string GenTxsDir string - Moniker string + Name string + NodeID string ClientHome string WithTxs bool Overwrite bool OverwriteKeys bool - NodeID string ValPubKey crypto.PubKey } type printInfo struct { + Moniker string `json:"moniker"` ChainID string `json:"chain_id"` NodeID string `json:"node_id"` AppMessage json.RawMessage `json:"app_message"` @@ -68,38 +70,51 @@ 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: `This command initializes validators's and node's configuration files. -Genesis file will not be generated if run with --skip-genesis.`, + Long: `Initialize validators's and node's configuration files. + +Note that only node's configuration files will be written if the flag --skip-genesis is +enabled, and the genesis file will not be generated. +`, Args: cobra.NoArgs, RunE: func(_ *cobra.Command, _ []string) error { - config := ctx.Config config.SetRoot(viper.GetString(cli.HomeFlag)) + + name := viper.GetString(client.FlagName) chainID := viper.GetString(flagChainID) if chainID == "" { chainID = fmt.Sprintf("test-chain-%v", common.RandStr(6)) } - nodeID, valPubKey, err := initNodeValidatorFiles(config) + nodeID, valPubKey, err := InitNodeValidatorFiles(config) if err != nil { return err } + + if viper.GetString(flagMoniker) != "" { + config.Moniker = viper.GetString(flagMoniker) + } + if config.Moniker == "" { + config.Moniker = name + } toPrint := printInfo{ ChainID: chainID, + Moniker: config.Moniker, NodeID: nodeID, } if viper.GetBool(flagSkipGenesis) { + cfg.WriteConfigFile(filepath.Join(config.RootDir, "config", "config.toml"), config) return displayInfo(cdc, toPrint) } initCfg := initConfig{ ChainID: chainID, GenTxsDir: viper.GetString(filepath.Join(config.RootDir, "config", "gentx")), - Moniker: viper.GetString(client.FlagName), + Name: name, + NodeID: nodeID, ClientHome: viper.GetString(flagClientHome), WithTxs: viper.GetBool(flagWithTxs), Overwrite: viper.GetBool(flagOverwrite), OverwriteKeys: viper.GetBool(flagOWK), - NodeID: nodeID, ValPubKey: valPubKey, } appMessage, err := initWithConfig(cdc, config, initCfg) @@ -117,7 +132,8 @@ Genesis file will not be generated if run with --skip-genesis.`, cmd.Flags().BoolP(flagOverwrite, "o", false, "overwrite the genesis.json file") cmd.Flags().String(flagChainID, "", "genesis file chain-id, if left blank will be randomly created") cmd.Flags().Bool(flagWithTxs, false, "apply existing genesis transactions from [--home]/config/gentx/") - cmd.Flags().String(client.FlagName, "", "moniker") + cmd.Flags().String(client.FlagName, "", "name of private key with which to sign the gentx") + cmd.Flags().String(flagMoniker, "", "overrides --name flag and set the validator's moniker to a different value") cmd.Flags().String(flagClientHome, app.DefaultCLIHome, "client's home directory") cmd.Flags().Bool(flagOWK, false, "overwrite client's keys") cmd.Flags().Bool(flagSkipGenesis, false, "do not create genesis.json") @@ -125,7 +141,7 @@ Genesis file will not be generated if run with --skip-genesis.`, return cmd } -func initNodeValidatorFiles(config *cfg.Config) (nodeID string, valPubKey crypto.PubKey, err error) { +func InitNodeValidatorFiles(config *cfg.Config) (nodeID string, valPubKey crypto.PubKey, err error) { nodeKey, err := p2p.LoadOrGenNodeKey(config.NodeKeyFile()) if err != nil { return @@ -149,11 +165,10 @@ func initWithConfig(cdc *codec.Codec, config *cfg.Config, initCfg initConfig) ( var genTxs []json.RawMessage var appState json.RawMessage var jsonRawTx json.RawMessage - moniker := initCfg.Moniker chainID := initCfg.ChainID if initCfg.WithTxs { - _, appGenTxs, persistentPeers, err = app.CollectStdTxs(moniker, initCfg.GenTxsDir, cdc) + _, appGenTxs, persistentPeers, err = app.CollectStdTxs(config.Moniker, initCfg.GenTxsDir, cdc) if err != nil { return } @@ -171,14 +186,13 @@ func initWithConfig(cdc *codec.Codec, config *cfg.Config, initCfg initConfig) ( var addr sdk.AccAddress var signedTx auth.StdTx - config.Moniker = moniker ip, err = server.ExternalIP() if err != nil { return } memo := fmt.Sprintf("%s@%s:26656", initCfg.NodeID, ip) buf := client.BufferStdin() - prompt := fmt.Sprintf("Password for account '%s' (default %s):", moniker, app.DefaultKeyPass) + prompt := fmt.Sprintf("Password for account %q (default: %q):", initCfg.Name, app.DefaultKeyPass) keyPass, err = client.GetPassword(prompt, buf) if err != nil && keyPass != "" { // An error was returned that either failed to read the password from @@ -190,7 +204,7 @@ func initWithConfig(cdc *codec.Codec, config *cfg.Config, initCfg initConfig) ( keyPass = app.DefaultKeyPass } - addr, secret, err = server.GenerateSaveCoinKey(initCfg.ClientHome, moniker, keyPass, initCfg.OverwriteKeys) + addr, secret, err = server.GenerateSaveCoinKey(initCfg.ClientHome, initCfg.Name, keyPass, initCfg.OverwriteKeys) if err != nil { return } @@ -203,12 +217,12 @@ func initWithConfig(cdc *codec.Codec, config *cfg.Config, initCfg initConfig) ( sdk.ValAddress(addr), initCfg.ValPubKey, sdk.NewInt64Coin("steak", 100), - stake.NewDescription(moniker, "", "", ""), + stake.NewDescription(config.Moniker, "", "", ""), stake.NewCommissionMsg(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()), ) txBldr := authtx.NewTxBuilderFromCLI().WithCodec(cdc).WithMemo(memo).WithChainID(chainID) signedTx, err = txBldr.SignStdTx( - moniker, keyPass, auth.NewStdTx([]sdk.Msg{msg}, auth.StdFee{}, []auth.StdSignature{}, memo), false, + initCfg.Name, keyPass, auth.NewStdTx([]sdk.Msg{msg}, auth.StdFee{}, []auth.StdSignature{}, memo), false, ) if err != nil { return diff --git a/cmd/gaia/init/init_test.go b/cmd/gaia/init/init_test.go index f2d2125e6d3b..2aa297225941 100644 --- a/cmd/gaia/init/init_test.go +++ b/cmd/gaia/init/init_test.go @@ -139,7 +139,7 @@ func TestInitNodeValidatorFiles(t *testing.T) { viper.Set(client.FlagName, "moniker") cfg, err := tcmd.ParseConfig() require.Nil(t, err) - nodeID, valPubKey, err := initNodeValidatorFiles(cfg) + nodeID, valPubKey, err := InitNodeValidatorFiles(cfg) require.Nil(t, err) require.NotEqual(t, "", nodeID) require.NotEqual(t, 0, len(valPubKey.Bytes())) diff --git a/cmd/gaia/init/testnet.go b/cmd/gaia/init/testnet.go index c707de1f9ca1..8b613ac29cf0 100644 --- a/cmd/gaia/init/testnet.go +++ b/cmd/gaia/init/testnet.go @@ -10,7 +10,6 @@ import ( "github.com/cosmos/cosmos-sdk/x/auth" authtx "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder" "github.com/cosmos/cosmos-sdk/x/stake" - "github.com/tendermint/tendermint/p2p" "net" "os" "path/filepath" @@ -18,6 +17,7 @@ import ( "github.com/cosmos/cosmos-sdk/server" "github.com/spf13/cobra" "github.com/spf13/viper" + "github.com/tendermint/tendermint/crypto" cfg "github.com/tendermint/tendermint/config" cmn "github.com/tendermint/tendermint/libs/common" ) @@ -76,6 +76,8 @@ func testnetWithConfig(config *cfg.Config, cdc *codec.Codec, appInit server.AppI // Generate genesis.json and config.toml chainID := "chain-" + cmn.RandStr(6) monikers := make([]string, numValidators) + nodeIDs := make([]string, numValidators) + valPubKeys := make([]crypto.PubKey, numValidators) // Generate private key, node ID, initial transaction for i := 0; i < numValidators; i++ { @@ -106,13 +108,12 @@ func testnetWithConfig(config *cfg.Config, cdc *codec.Codec, appInit server.AppI _ = os.RemoveAll(outDir) return err } - nodeKey, err := p2p.LoadOrGenNodeKey(config.NodeKeyFile()) + nodeIDs[i], valPubKeys[i], err = InitNodeValidatorFiles(config) if err != nil { _ = os.RemoveAll(outDir) return err } - nodeID := string(nodeKey.ID()) - memo := fmt.Sprintf("%s@%s:26656", nodeID, ip) + memo := fmt.Sprintf("%s@%s:26656", nodeIDs[i], ip) buf := client.BufferStdin() prompt := fmt.Sprintf("Password for account '%s' (default %s):", nodeDirName, app.DefaultKeyPass) @@ -143,10 +144,9 @@ func testnetWithConfig(config *cfg.Config, cdc *codec.Codec, appInit server.AppI return err } - valPubKey := ReadOrCreatePrivValidator(config.PrivValidatorFile()) msg := stake.NewMsgCreateValidator( sdk.ValAddress(addr), - valPubKey, + valPubKeys[i], sdk.NewInt64Coin("steak", 100), stake.NewDescription(nodeDirName, "", "", ""), stake.NewCommissionMsg(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()), @@ -168,6 +168,7 @@ func testnetWithConfig(config *cfg.Config, cdc *codec.Codec, appInit server.AppI // Gather gentxs folder err = writeFile(fmt.Sprintf("%v.json", nodeDirName), gentxsDir, txBytes) if err != nil { + _ = os.RemoveAll(outDir) return err } } @@ -182,23 +183,19 @@ func testnetWithConfig(config *cfg.Config, cdc *codec.Codec, appInit server.AppI config.Moniker = nodeDirName config.SetRoot(nodeDir) - nodeID, valPubKey, err := initNodeValidatorFiles(config) - if err != nil { - return err - } + nodeID, valPubKey := nodeIDs[i], valPubKeys[i] // Run `init` and generate genesis.json and config.toml initCfg := initConfig{ ChainID: chainID, GenTxsDir: gentxsDir, - Moniker: moniker, + Name: moniker, WithTxs: true, Overwrite: true, OverwriteKeys: false, NodeID: nodeID, ValPubKey: valPubKey, } - _, err = initWithConfig(cdc, config, initCfg) - if err != nil { + if _, err := initWithConfig(cdc, config, initCfg); err != nil { return err } } From ee362139edd8b55a23d70b728dc489fa2d7af93a Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Thu, 18 Oct 2018 16:49:26 -0700 Subject: [PATCH 69/83] Fix linter warnings, integration tests --- cmd/gaia/init/init.go | 8 +++++--- cmd/gaia/init/init_test.go | 2 +- cmd/gaia/init/testnet.go | 2 +- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/cmd/gaia/init/init.go b/cmd/gaia/init/init.go index 9ac495d61f70..1c6832eed7ad 100644 --- a/cmd/gaia/init/init.go +++ b/cmd/gaia/init/init.go @@ -85,7 +85,7 @@ enabled, and the genesis file will not be generated. if chainID == "" { chainID = fmt.Sprintf("test-chain-%v", common.RandStr(6)) } - nodeID, valPubKey, err := InitNodeValidatorFiles(config) + nodeID, valPubKey, err := InitializeNodeValidatorFiles(config) if err != nil { return err } @@ -133,7 +133,7 @@ enabled, and the genesis file will not be generated. cmd.Flags().String(flagChainID, "", "genesis file chain-id, if left blank will be randomly created") cmd.Flags().Bool(flagWithTxs, false, "apply existing genesis transactions from [--home]/config/gentx/") cmd.Flags().String(client.FlagName, "", "name of private key with which to sign the gentx") - cmd.Flags().String(flagMoniker, "", "overrides --name flag and set the validator's moniker to a different value") + cmd.Flags().String(flagMoniker, "", "overrides --name flag and set the validator's moniker to a different value; ignored if it runs without the --with-txs flag") cmd.Flags().String(flagClientHome, app.DefaultCLIHome, "client's home directory") cmd.Flags().Bool(flagOWK, false, "overwrite client's keys") cmd.Flags().Bool(flagSkipGenesis, false, "do not create genesis.json") @@ -141,7 +141,8 @@ enabled, and the genesis file will not be generated. return cmd } -func InitNodeValidatorFiles(config *cfg.Config) (nodeID string, valPubKey crypto.PubKey, err error) { +// InitializeNodeValidatorFiles creates private validator and p2p configuration files. +func InitializeNodeValidatorFiles(config *cfg.Config) (nodeID string, valPubKey crypto.PubKey, err error) { nodeKey, err := p2p.LoadOrGenNodeKey(config.NodeKeyFile()) if err != nil { return @@ -186,6 +187,7 @@ func initWithConfig(cdc *codec.Codec, config *cfg.Config, initCfg initConfig) ( var addr sdk.AccAddress var signedTx auth.StdTx + config.Moniker = initCfg.Name ip, err = server.ExternalIP() if err != nil { return diff --git a/cmd/gaia/init/init_test.go b/cmd/gaia/init/init_test.go index 2aa297225941..08c70f9e15f0 100644 --- a/cmd/gaia/init/init_test.go +++ b/cmd/gaia/init/init_test.go @@ -139,7 +139,7 @@ func TestInitNodeValidatorFiles(t *testing.T) { viper.Set(client.FlagName, "moniker") cfg, err := tcmd.ParseConfig() require.Nil(t, err) - nodeID, valPubKey, err := InitNodeValidatorFiles(cfg) + nodeID, valPubKey, err := InitializeNodeValidatorFiles(cfg) require.Nil(t, err) require.NotEqual(t, "", nodeID) require.NotEqual(t, 0, len(valPubKey.Bytes())) diff --git a/cmd/gaia/init/testnet.go b/cmd/gaia/init/testnet.go index 8b613ac29cf0..8bca339e0934 100644 --- a/cmd/gaia/init/testnet.go +++ b/cmd/gaia/init/testnet.go @@ -108,7 +108,7 @@ func testnetWithConfig(config *cfg.Config, cdc *codec.Codec, appInit server.AppI _ = os.RemoveAll(outDir) return err } - nodeIDs[i], valPubKeys[i], err = InitNodeValidatorFiles(config) + nodeIDs[i], valPubKeys[i], err = InitializeNodeValidatorFiles(config) if err != nil { _ = os.RemoveAll(outDir) return err From 898883fa3bffc798556059bdfca678aa39f8ca89 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Thu, 18 Oct 2018 17:08:18 -0700 Subject: [PATCH 70/83] Update PENDING.md --- PENDING.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/PENDING.md b/PENDING.md index ca0c316bee65..215c17e3ffec 100644 --- a/PENDING.md +++ b/PENDING.md @@ -20,6 +20,7 @@ BREAKING CHANGES * [cli] [\#2190](https://github.com/cosmos/cosmos-sdk/issues/2190) `gaiacli init --gen-txs` is now `gaiacli init --with-txs` to reduce confusion * [cli] \#2073 --from can now be either an address or a key name * [cli] [\#1184](https://github.com/cosmos/cosmos-sdk/issues/1184) Subcommands reorganisation, see [\#2390](https://github.com/cosmos/cosmos-sdk/pull/2390) for a comprehensive list of changes. + * [cli] [\#2524](https://github.com/cosmos/cosmos-sdk/issues/2524) Add support offline mode to `gaiacli tx sign`. Lookups are not performed if the flag `--offline` is on. * Gaia * Make the transient store key use a distinct store key. [#2013](https://github.com/cosmos/cosmos-sdk/pull/2013) @@ -115,6 +116,7 @@ FEATURES * [cli] \#2220 Add `gaiacli config` feature to interactively create CLI config files to reduce the number of required flags * [stake][cli] [\#1672](https://github.com/cosmos/cosmos-sdk/issues/1672) Introduced new commission flags for validator commands `create-validator` and `edit-validator`. + * [stake][cli] [\#1890](https://github.com/cosmos/cosmos-sdk/issues/1890) Add `--genesis-format` flag to `gaiacli tx create-validator` to produce transactions in genesis-friendly format. * Gaia * [cli] #2170 added ability to show the node's address via `gaiad tendermint show-address` @@ -122,6 +124,11 @@ FEATURES * [cli] [\#1921] (https://github.com/cosmos/cosmos-sdk/issues/1921) * New configuration file `gaiad.toml` is now created to host Gaia-specific configuration. * New --minimum_fees/minimum_fees flag/config option to set a minimum fee. + * [cli] [\#1890](https://github.com/cosmos/cosmos-sdk/issues/1890) Start chain with initial state + sequence of transactions + * Replace `gaiad init gentx` with `gaiacli tx create-validator > gentx.json`. + * Drop `GenesisTx` in favor of a signed `StdTx` with only one `MsgCreateValidator` message. + * Port `gaiad init` and `gaiad testnet` to work with `StdTx` genesis transactions. + * Add `--moniker` flag to `gaiad init` to override moniker when generating `genesis.json` - i.e. it takes effect when running with the `--with-txs` flag, it is ignored otherwise. * SDK * [querier] added custom querier functionality, so ABCI query requests can be handled by keepers From 731d49a4f3a469565a91bf9e059ab434c5c5700a Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Thu, 18 Oct 2018 17:08:35 -0700 Subject: [PATCH 71/83] Run make format --- cmd/gaia/init/init.go | 4 ++-- cmd/gaia/init/testnet.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/gaia/init/init.go b/cmd/gaia/init/init.go index 1c6832eed7ad..84d361689021 100644 --- a/cmd/gaia/init/init.go +++ b/cmd/gaia/init/init.go @@ -75,7 +75,7 @@ func InitCmd(ctx *server.Context, cdc *codec.Codec, appInit server.AppInit) *cob Note that only node's configuration files will be written if the flag --skip-genesis is enabled, and the genesis file will not be generated. `, - Args: cobra.NoArgs, + Args: cobra.NoArgs, RunE: func(_ *cobra.Command, _ []string) error { config := ctx.Config config.SetRoot(viper.GetString(cli.HomeFlag)) @@ -99,7 +99,7 @@ enabled, and the genesis file will not be generated. toPrint := printInfo{ ChainID: chainID, Moniker: config.Moniker, - NodeID: nodeID, + NodeID: nodeID, } if viper.GetBool(flagSkipGenesis) { cfg.WriteConfigFile(filepath.Join(config.RootDir, "config", "config.toml"), config) diff --git a/cmd/gaia/init/testnet.go b/cmd/gaia/init/testnet.go index 8bca339e0934..5b6c105546bd 100644 --- a/cmd/gaia/init/testnet.go +++ b/cmd/gaia/init/testnet.go @@ -17,8 +17,8 @@ import ( "github.com/cosmos/cosmos-sdk/server" "github.com/spf13/cobra" "github.com/spf13/viper" - "github.com/tendermint/tendermint/crypto" cfg "github.com/tendermint/tendermint/config" + "github.com/tendermint/tendermint/crypto" cmn "github.com/tendermint/tendermint/libs/common" ) From f2878af69c337abc2f14ff740a7f2a98b3b91554 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Thu, 18 Oct 2018 18:23:26 -0700 Subject: [PATCH 72/83] Drop unnecessary workaround to make lcd_tests pass --- client/lcd/lcd_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/lcd/lcd_test.go b/client/lcd/lcd_test.go index c114a070b73c..c1c01877855d 100644 --- a/client/lcd/lcd_test.go +++ b/client/lcd/lcd_test.go @@ -566,7 +566,7 @@ func TestBonding(t *testing.T) { // create unbond TX resultTx = doBeginUnbonding(t, port, seed, name, password, addr, operAddrs[0], 60) - tests.WaitForHeight(resultTx.Height+2, port) + tests.WaitForHeight(resultTx.Height+1, port) require.Equal(t, uint32(0), resultTx.CheckTx.Code) require.Equal(t, uint32(0), resultTx.DeliverTx.Code) From e28be2f6a69cf8754db3c355ca4077f9e8768e13 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Thu, 18 Oct 2018 20:28:14 -0700 Subject: [PATCH 73/83] Ensure GenTxsDir is not empty --- cmd/gaia/init/init.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/gaia/init/init.go b/cmd/gaia/init/init.go index 84d361689021..d4bb686225df 100644 --- a/cmd/gaia/init/init.go +++ b/cmd/gaia/init/init.go @@ -108,7 +108,7 @@ enabled, and the genesis file will not be generated. initCfg := initConfig{ ChainID: chainID, - GenTxsDir: viper.GetString(filepath.Join(config.RootDir, "config", "gentx")), + GenTxsDir: filepath.Join(config.RootDir, "config", "gentx"), Name: name, NodeID: nodeID, ClientHome: viper.GetString(flagClientHome), From 75b13d0bae2308fb8c77bb78a14ed38cfe6e8b1f Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Thu, 18 Oct 2018 20:48:33 -0700 Subject: [PATCH 74/83] Reintroduce gentx --- cmd/gaia/cmd/gaiad/main.go | 1 + cmd/gaia/init/gentx.go | 119 ++++++++++++++++++++++++++++++++++++ cmd/gaia/init/init.go | 14 +++-- x/stake/client/cli/flags.go | 2 + x/stake/client/cli/tx.go | 1 + 5 files changed, 132 insertions(+), 5 deletions(-) create mode 100644 cmd/gaia/init/gentx.go diff --git a/cmd/gaia/cmd/gaiad/main.go b/cmd/gaia/cmd/gaiad/main.go index adfb12d5735d..1304b32f3486 100644 --- a/cmd/gaia/cmd/gaiad/main.go +++ b/cmd/gaia/cmd/gaiad/main.go @@ -32,6 +32,7 @@ func main() { appInit := app.GaiaAppInit() rootCmd.AddCommand(gaiaInit.InitCmd(ctx, cdc, appInit)) rootCmd.AddCommand(gaiaInit.TestnetFilesCmd(ctx, cdc, appInit)) + rootCmd.AddCommand(gaiaInit.GenTxCmd(ctx, cdc)) server.AddCommands(ctx, cdc, rootCmd, appInit, newApp, exportAppStateAndTMValidators) diff --git a/cmd/gaia/init/gentx.go b/cmd/gaia/init/gentx.go new file mode 100644 index 000000000000..e349755b94ef --- /dev/null +++ b/cmd/gaia/init/gentx.go @@ -0,0 +1,119 @@ +package init + +import ( + "fmt" + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/cmd/gaia/app" + "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/server" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/stake/client/cli" + "github.com/spf13/cobra" + "github.com/spf13/viper" + cfg "github.com/tendermint/tendermint/config" + "github.com/tendermint/tendermint/crypto" + tmcli "github.com/tendermint/tendermint/libs/cli" + authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli" + "github.com/tendermint/tendermint/libs/common" + "io/ioutil" + "os" + "path/filepath" +) + +const ( + defaultAmount = "100steak" + defaultCommissionRate = "0.1" + defaultCommissionMaxRate = "0.2" + defaultCommissionMaxChangeRate = "0.01" +) + +func GenTxCmd(ctx *server.Context, cdc *codec.Codec) *cobra.Command { + cmd := &cobra.Command{ + Use: "gentx", + Short: "Generate a genesis tx carrying a self delegation", + Long: fmt.Sprintf(`This command is an alias of the 'gaiad tx create-validator' command'. + +It creates a genesis piece carrying a self delegation with the +following delegation and commission default parameters: + + delegation amount: %s + commission rate: %s + commission max rate: %s + commission max change rate: %s +`, defaultAmount, defaultCommissionRate, defaultCommissionMaxRate, defaultCommissionMaxChangeRate), + RunE: func(cmd *cobra.Command, args []string) error { + + config := ctx.Config + config.SetRoot(viper.GetString(tmcli.HomeFlag)) + nodeID, valPubKey, err := InitializeNodeValidatorFiles(ctx.Config) + if err != nil { + return err + } + //chainID := viper.GetString(flagChainID) + ip, err := server.ExternalIP() + if err != nil { + return err + } + + // Run gaiad tx create-validator + prepareFlagsForTxCreateValidator(config, nodeID, ip, valPubKey) + createValidatorCmd := cli.GetCmdCreateValidator(cdc) + + w, err := ioutil.TempFile("", "gentx") + if err != nil { + return err + } + unsignedGenTxFilename := w.Name() + defer os.Remove(unsignedGenTxFilename) + os.Stdout = w + if err = createValidatorCmd.RunE(nil, args); err != nil { + return err + } + w.Close() + + prepareFlagsForTxSign() + signCmd := authcmd.GetSignCommand(cdc, authcmd.GetAccountDecoder(cdc)) + if w, err = prepareOutputFile(config.RootDir, nodeID); err != nil { + return err + } + os.Stdout = w + return signCmd.RunE(nil, []string{unsignedGenTxFilename}) + }, + } + + cmd.Flags().String(flagClientHome, app.DefaultCLIHome, "client's home directory") + cmd.Flags().String(flagChainID, "", "genesis file chain-id") + cmd.Flags().String(client.FlagName, "", "name of private key with which to sign the gentx") + cmd.MarkFlagRequired(client.FlagName) + return cmd +} + +func prepareFlagsForTxCreateValidator(config *cfg.Config, nodeID, ip string, valPubKey crypto.PubKey) { + viper.Set(tmcli.HomeFlag, viper.GetString(flagClientHome)) // --home + viper.Set(client.FlagFrom, viper.GetString(client.FlagName)) // --from + viper.Set(cli.FlagNodeID, nodeID) // --node-id + viper.Set(cli.FlagIP, ip) // --ip + viper.Set(cli.FlagPubKey, sdk.MustBech32ifyConsPub(valPubKey)) // --pubkey + viper.Set(cli.FlagAmount, defaultAmount) // --amount + viper.Set(cli.FlagCommissionRate, defaultCommissionRate) + viper.Set(cli.FlagCommissionMaxRate, defaultCommissionMaxRate) + viper.Set(cli.FlagCommissionMaxChangeRate, defaultCommissionMaxChangeRate) + viper.Set(cli.FlagGenesisFormat, true) // --genesis-format + viper.Set(cli.FlagMoniker, config.Moniker) // --moniker + if config.Moniker == "" { + viper.Set(cli.FlagMoniker, viper.GetString(client.FlagName)) + } +} + +func prepareFlagsForTxSign() { + viper.Set("offline", true) +} + +func prepareOutputFile(rootDir, nodeID string) (w *os.File, err error) { + writePath := filepath.Join(rootDir, "config", "gentx") + if err = common.EnsureDir(writePath, 0700); err != nil { + return + } + filename := filepath.Join(writePath, fmt.Sprintf("gentx-%v.json", nodeID)) + return os.Create(filename) +} diff --git a/cmd/gaia/init/init.go b/cmd/gaia/init/init.go index d4bb686225df..71d33af1d634 100644 --- a/cmd/gaia/init/init.go +++ b/cmd/gaia/init/init.go @@ -1,6 +1,7 @@ package init import ( + "errors" "encoding/json" "fmt" "github.com/cosmos/cosmos-sdk/client" @@ -26,7 +27,6 @@ import ( ) const ( - flagChainID = "chain-id" flagWithTxs = "with-txs" flagOverwrite = "overwrite" flagClientHome = "home-client" @@ -81,7 +81,7 @@ enabled, and the genesis file will not be generated. config.SetRoot(viper.GetString(cli.HomeFlag)) name := viper.GetString(client.FlagName) - chainID := viper.GetString(flagChainID) + chainID := viper.GetString(client.FlagChainID) if chainID == "" { chainID = fmt.Sprintf("test-chain-%v", common.RandStr(6)) } @@ -93,7 +93,7 @@ enabled, and the genesis file will not be generated. if viper.GetString(flagMoniker) != "" { config.Moniker = viper.GetString(flagMoniker) } - if config.Moniker == "" { + if config.Moniker == "" && name != "" { config.Moniker = name } toPrint := printInfo{ @@ -130,14 +130,13 @@ enabled, and the genesis file will not be generated. cmd.Flags().String(cli.HomeFlag, app.DefaultNodeHome, "node's home directory") cmd.Flags().BoolP(flagOverwrite, "o", false, "overwrite the genesis.json file") - cmd.Flags().String(flagChainID, "", "genesis file chain-id, if left blank will be randomly created") + cmd.Flags().String(client.FlagChainID, "", "genesis file chain-id, if left blank will be randomly created") cmd.Flags().Bool(flagWithTxs, false, "apply existing genesis transactions from [--home]/config/gentx/") cmd.Flags().String(client.FlagName, "", "name of private key with which to sign the gentx") cmd.Flags().String(flagMoniker, "", "overrides --name flag and set the validator's moniker to a different value; ignored if it runs without the --with-txs flag") cmd.Flags().String(flagClientHome, app.DefaultCLIHome, "client's home directory") cmd.Flags().Bool(flagOWK, false, "overwrite client's keys") cmd.Flags().Bool(flagSkipGenesis, false, "do not create genesis.json") - cmd.MarkFlagRequired(client.FlagName) return cmd } @@ -187,6 +186,11 @@ func initWithConfig(cdc *codec.Codec, config *cfg.Config, initCfg initConfig) ( var addr sdk.AccAddress var signedTx auth.StdTx + if initCfg.Name == "" { + err = errors.New("must specify validator's moniker (--name)") + return + } + config.Moniker = initCfg.Name ip, err = server.ExternalIP() if err != nil { diff --git a/x/stake/client/cli/flags.go b/x/stake/client/cli/flags.go index 9f282b225764..29237b6ba828 100644 --- a/x/stake/client/cli/flags.go +++ b/x/stake/client/cli/flags.go @@ -29,6 +29,8 @@ const ( FlagGenesisFormat = "genesis-format" FlagNodeID = "node-id" FlagIP = "ip" + + FlagOutputDocument = "output-document" // inspired by wget -O ) // common flagsets to add to various functions diff --git a/x/stake/client/cli/tx.go b/x/stake/client/cli/tx.go index 58fe774d0f05..e3f1060eeffc 100644 --- a/x/stake/client/cli/tx.go +++ b/x/stake/client/cli/tx.go @@ -111,6 +111,7 @@ func GetCmdCreateValidator(cdc *codec.Codec) *cobra.Command { cmd.Flags().Bool(FlagGenesisFormat, false, "Export the transaction in gen-tx format; it implies --generate-only") cmd.Flags().String(FlagIP, "", fmt.Sprintf("Node's public IP. It takes effect only when used in combination with --%s", FlagGenesisFormat)) cmd.Flags().String(FlagNodeID, "", "Node's ID") + cmd.MarkFlagRequired(client.FlagFrom) return cmd } From d6d4b182197ce63c21af01912dd08ac9076c3f9e Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Thu, 18 Oct 2018 21:03:24 -0700 Subject: [PATCH 75/83] Fix FTBFS --- cmd/gaia/init/gentx.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/cmd/gaia/init/gentx.go b/cmd/gaia/init/gentx.go index e349755b94ef..04256ea61639 100644 --- a/cmd/gaia/init/gentx.go +++ b/cmd/gaia/init/gentx.go @@ -49,7 +49,6 @@ following delegation and commission default parameters: if err != nil { return err } - //chainID := viper.GetString(flagChainID) ip, err := server.ExternalIP() if err != nil { return err @@ -82,7 +81,7 @@ following delegation and commission default parameters: } cmd.Flags().String(flagClientHome, app.DefaultCLIHome, "client's home directory") - cmd.Flags().String(flagChainID, "", "genesis file chain-id") + cmd.Flags().String(client.FlagChainID, "", "genesis file chain-id") cmd.Flags().String(client.FlagName, "", "name of private key with which to sign the gentx") cmd.MarkFlagRequired(client.FlagName) return cmd From 3df87825b9481c0b5b9caf2959fe9253fe5210af Mon Sep 17 00:00:00 2001 From: Jae Kwon Date: Thu, 18 Oct 2018 20:54:24 -0700 Subject: [PATCH 76/83] Simple name changes, GenesisState.Txs -> .GenTxs; OWK -> OverwriteKey; OverwriteKeys -> OverwriteKey --- cmd/gaia/app/app.go | 4 +-- cmd/gaia/app/genesis.go | 10 +++---- cmd/gaia/init/init.go | 54 +++++++++++++++++++------------------- cmd/gaia/init/init_test.go | 2 +- cmd/gaia/init/testnet.go | 16 +++++------ 5 files changed, 43 insertions(+), 43 deletions(-) diff --git a/cmd/gaia/app/app.go b/cmd/gaia/app/app.go index 859ae583ad31..6afeae7938f7 100644 --- a/cmd/gaia/app/app.go +++ b/cmd/gaia/app/app.go @@ -240,8 +240,8 @@ func (app *GaiaApp) initChainer(ctx sdk.Context, req abci.RequestInitChain) abci panic(err) // TODO find a way to do this w/o panics } - if len(genesisState.Txs) > 0 { - for _, genTx := range genesisState.Txs { + if len(genesisState.GenTxs) > 0 { + for _, genTx := range genesisState.GenTxs { var tx auth.StdTx err = app.cdc.UnmarshalJSON(genTx, &tx) if err != nil { diff --git a/cmd/gaia/app/genesis.go b/cmd/gaia/app/genesis.go index 6dfec4a7ed9f..9d52c9a5762c 100644 --- a/cmd/gaia/app/genesis.go +++ b/cmd/gaia/app/genesis.go @@ -34,7 +34,7 @@ type GenesisState struct { DistrData distr.GenesisState `json:"distr"` GovData gov.GenesisState `json:"gov"` SlashingData slashing.GenesisState `json:"slashing"` - Txs []json.RawMessage `json:"txs"` + GenTxs []json.RawMessage `json:"gentxs"` } // GenesisAccount doesn't need pubkey or sequence @@ -95,8 +95,8 @@ func GaiaAppGenState(cdc *codec.Codec, appGenTxs []json.RawMessage) (genesisStat return } msgs := tx.GetMsgs() - if len(msgs) == 0 { - err = errors.New("must provide at least genesis message") + if len(msgs) != 1 { + err = errors.New("must provide genesis StdTx with exactly 1 CreateValidator message") return } msg := msgs[0].(stake.MsgCreateValidator) @@ -113,7 +113,7 @@ func GaiaAppGenState(cdc *codec.Codec, appGenTxs []json.RawMessage) (genesisStat DistrData: distr.DefaultGenesisState(), GovData: gov.DefaultGenesisState(), SlashingData: slashingData, - Txs: appGenTxs, + GenTxs: appGenTxs, } return @@ -138,7 +138,7 @@ func GaiaValidateGenesisState(genesisState GenesisState) (err error) { return } // skip stakeData validation as genesis is created from txs - if len(genesisState.Txs) > 0 { + if len(genesisState.GenTxs) > 0 { return nil } return stake.ValidateGenesis(genesisState.StakeData) diff --git a/cmd/gaia/init/init.go b/cmd/gaia/init/init.go index 71d33af1d634..467ea3fc2e1e 100644 --- a/cmd/gaia/init/init.go +++ b/cmd/gaia/init/init.go @@ -1,8 +1,8 @@ package init import ( - "errors" "encoding/json" + "errors" "fmt" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/cmd/gaia/app" @@ -27,24 +27,24 @@ import ( ) const ( - flagWithTxs = "with-txs" - flagOverwrite = "overwrite" - flagClientHome = "home-client" - flagOWK = "owk" - flagSkipGenesis = "skip-genesis" - flagMoniker = "moniker" + flagWithTxs = "with-txs" + flagOverwrite = "overwrite" + flagClientHome = "home-client" + flagOverwriteKey = "overwrite-key" + flagSkipGenesis = "skip-genesis" + flagMoniker = "moniker" ) type initConfig struct { - ChainID string - GenTxsDir string - Name string - NodeID string - ClientHome string - WithTxs bool - Overwrite bool - OverwriteKeys bool - ValPubKey crypto.PubKey + ChainID string + GenTxsDir string + Name string + NodeID string + ClientHome string + WithTxs bool + Overwrite bool + OverwriteKey bool + ValPubKey crypto.PubKey } type printInfo struct { @@ -107,15 +107,15 @@ enabled, and the genesis file will not be generated. } initCfg := initConfig{ - ChainID: chainID, - GenTxsDir: filepath.Join(config.RootDir, "config", "gentx"), - Name: name, - NodeID: nodeID, - ClientHome: viper.GetString(flagClientHome), - WithTxs: viper.GetBool(flagWithTxs), - Overwrite: viper.GetBool(flagOverwrite), - OverwriteKeys: viper.GetBool(flagOWK), - ValPubKey: valPubKey, + ChainID: chainID, + GenTxsDir: filepath.Join(config.RootDir, "config", "gentx"), + Name: name, + NodeID: nodeID, + ClientHome: viper.GetString(flagClientHome), + WithTxs: viper.GetBool(flagWithTxs), + Overwrite: viper.GetBool(flagOverwrite), + OverwriteKey: viper.GetBool(flagOverwriteKey), + ValPubKey: valPubKey, } appMessage, err := initWithConfig(cdc, config, initCfg) // print out some key information @@ -135,7 +135,7 @@ enabled, and the genesis file will not be generated. cmd.Flags().String(client.FlagName, "", "name of private key with which to sign the gentx") cmd.Flags().String(flagMoniker, "", "overrides --name flag and set the validator's moniker to a different value; ignored if it runs without the --with-txs flag") cmd.Flags().String(flagClientHome, app.DefaultCLIHome, "client's home directory") - cmd.Flags().Bool(flagOWK, false, "overwrite client's keys") + cmd.Flags().Bool(flagOverwriteKey, false, "overwrite client's key") cmd.Flags().Bool(flagSkipGenesis, false, "do not create genesis.json") return cmd } @@ -210,7 +210,7 @@ func initWithConfig(cdc *codec.Codec, config *cfg.Config, initCfg initConfig) ( keyPass = app.DefaultKeyPass } - addr, secret, err = server.GenerateSaveCoinKey(initCfg.ClientHome, initCfg.Name, keyPass, initCfg.OverwriteKeys) + addr, secret, err = server.GenerateSaveCoinKey(initCfg.ClientHome, initCfg.Name, keyPass, initCfg.OverwriteKey) if err != nil { return } diff --git a/cmd/gaia/init/init_test.go b/cmd/gaia/init/init_test.go index 08c70f9e15f0..48a5d9247c35 100644 --- a/cmd/gaia/init/init_test.go +++ b/cmd/gaia/init/init_test.go @@ -42,7 +42,7 @@ func setupClientHome(t *testing.T) func() { clientDir, err := ioutil.TempDir("", "mock-sdk-cmd") require.Nil(t, err) viper.Set(flagClientHome, clientDir) - viper.Set(flagOWK, true) + 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 5b6c105546bd..3002b83a0e2a 100644 --- a/cmd/gaia/init/testnet.go +++ b/cmd/gaia/init/testnet.go @@ -186,14 +186,14 @@ func testnetWithConfig(config *cfg.Config, cdc *codec.Codec, appInit server.AppI nodeID, valPubKey := nodeIDs[i], valPubKeys[i] // Run `init` and generate genesis.json and config.toml initCfg := initConfig{ - ChainID: chainID, - GenTxsDir: gentxsDir, - Name: moniker, - WithTxs: true, - Overwrite: true, - OverwriteKeys: false, - NodeID: nodeID, - ValPubKey: valPubKey, + ChainID: chainID, + GenTxsDir: gentxsDir, + Name: moniker, + WithTxs: true, + Overwrite: true, + OverwriteKey: false, + NodeID: nodeID, + ValPubKey: valPubKey, } if _, err := initWithConfig(cdc, config, initCfg); err != nil { return err From cbe5c008d1808e7152ac6148a2ffea3035b0d464 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Thu, 18 Oct 2018 21:10:01 -0700 Subject: [PATCH 77/83] Make linter happy --- cmd/gaia/init/gentx.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cmd/gaia/init/gentx.go b/cmd/gaia/init/gentx.go index 04256ea61639..33520708c1bd 100644 --- a/cmd/gaia/init/gentx.go +++ b/cmd/gaia/init/gentx.go @@ -27,6 +27,8 @@ const ( defaultCommissionMaxChangeRate = "0.01" ) +// GenTxCmd builds the gaiad gentx command. +// nolint: errcheck func GenTxCmd(ctx *server.Context, cdc *codec.Codec) *cobra.Command { cmd := &cobra.Command{ Use: "gentx", From 0641031b0e15ae92d13dd648a8cba554943fd7e6 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Thu, 18 Oct 2018 21:27:22 -0700 Subject: [PATCH 78/83] Update changelog --- PENDING.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/PENDING.md b/PENDING.md index 215c17e3ffec..6d17729da11b 100644 --- a/PENDING.md +++ b/PENDING.md @@ -46,6 +46,11 @@ BREAKING CHANGES * [x/slashing] \#2430 Simulate more slashes, check if validator is jailed before jailing * [x/stake] \#2393 Removed `CompleteUnbonding` and `CompleteRedelegation` Msg types, and instead added unbonding/redelegation queues to endblocker * [x/stake] \#1673 Validators are no longer deleted until they can no longer possibly be slashed + * [\#1890](https://github.com/cosmos/cosmos-sdk/issues/1890) Start chain with initial state + sequence of transactions + * [cli] Rename `gaiad init gentx` to `gaiad gentx`. + * Drop `GenesisTx` in favor of a signed `StdTx` with only one `MsgCreateValidator` message. + * [cli] Port `gaiad init` and `gaiad testnet` to work with `StdTx` genesis transactions. + * [cli] Add `--moniker` flag to `gaiad init` to override moniker when generating `genesis.json` - i.e. it takes effect when running with the `--with-txs` flag, it is ignored otherwise. * SDK * [core] \#2219 Update to Tendermint 0.24.0 @@ -124,11 +129,6 @@ FEATURES * [cli] [\#1921] (https://github.com/cosmos/cosmos-sdk/issues/1921) * New configuration file `gaiad.toml` is now created to host Gaia-specific configuration. * New --minimum_fees/minimum_fees flag/config option to set a minimum fee. - * [cli] [\#1890](https://github.com/cosmos/cosmos-sdk/issues/1890) Start chain with initial state + sequence of transactions - * Replace `gaiad init gentx` with `gaiacli tx create-validator > gentx.json`. - * Drop `GenesisTx` in favor of a signed `StdTx` with only one `MsgCreateValidator` message. - * Port `gaiad init` and `gaiad testnet` to work with `StdTx` genesis transactions. - * Add `--moniker` flag to `gaiad init` to override moniker when generating `genesis.json` - i.e. it takes effect when running with the `--with-txs` flag, it is ignored otherwise. * SDK * [querier] added custom querier functionality, so ABCI query requests can be handled by keepers From 256919ff781897ed54ca8299b6236e7ca17593f8 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Thu, 18 Oct 2018 21:23:54 -0700 Subject: [PATCH 79/83] Update PENDING.md, docs --- PENDING.md | 1 + docs/getting-started/join-testnet.md | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/PENDING.md b/PENDING.md index 6d17729da11b..715fd4a57c1a 100644 --- a/PENDING.md +++ b/PENDING.md @@ -48,6 +48,7 @@ BREAKING CHANGES * [x/stake] \#1673 Validators are no longer deleted until they can no longer possibly be slashed * [\#1890](https://github.com/cosmos/cosmos-sdk/issues/1890) Start chain with initial state + sequence of transactions * [cli] Rename `gaiad init gentx` to `gaiad gentx`. + * [cli] Add `--skip-genesis` flag to `gaiad init` to prevent `genesis.json` generation. * Drop `GenesisTx` in favor of a signed `StdTx` with only one `MsgCreateValidator` message. * [cli] Port `gaiad init` and `gaiad testnet` to work with `StdTx` genesis transactions. * [cli] Add `--moniker` flag to `gaiad init` to override moniker when generating `genesis.json` - i.e. it takes effect when running with the `--with-txs` flag, it is ignored otherwise. diff --git a/docs/getting-started/join-testnet.md b/docs/getting-started/join-testnet.md index 66ec97cad5f1..ce070c4fe967 100644 --- a/docs/getting-started/join-testnet.md +++ b/docs/getting-started/join-testnet.md @@ -15,7 +15,7 @@ These instructions are for setting up a brand new full node from scratch. First, initialize the node and create the necessary config files: ```bash -gaiad init --name +gaiad init --skip-genesis --name ``` ::: warning Note From 0164024a8ab0ee289073244c6980e94f6588d4c0 Mon Sep 17 00:00:00 2001 From: Christopher Goes Date: Fri, 19 Oct 2018 19:08:41 +0200 Subject: [PATCH 80/83] 'make format' --- client/lcd/certificates.go | 2 +- cmd/gaia/init/gentx.go | 20 ++++++++++---------- x/distribution/keeper/hooks.go | 6 +++--- x/distribution/types/validator_info.go | 6 +++--- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/client/lcd/certificates.go b/client/lcd/certificates.go index f47f2397c72e..1516ed35afaf 100644 --- a/client/lcd/certificates.go +++ b/client/lcd/certificates.go @@ -43,7 +43,7 @@ func generateSelfSignedCert(host string) (certBytes []byte, priv *ecdsa.PrivateK KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign, ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, BasicConstraintsValid: true, - IsCA: true, + IsCA: true, } hosts := strings.Split(host, ",") for _, h := range hosts { diff --git a/cmd/gaia/init/gentx.go b/cmd/gaia/init/gentx.go index 33520708c1bd..1b24a357688f 100644 --- a/cmd/gaia/init/gentx.go +++ b/cmd/gaia/init/gentx.go @@ -7,13 +7,13 @@ import ( "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/server" sdk "github.com/cosmos/cosmos-sdk/types" + authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli" "github.com/cosmos/cosmos-sdk/x/stake/client/cli" "github.com/spf13/cobra" "github.com/spf13/viper" cfg "github.com/tendermint/tendermint/config" "github.com/tendermint/tendermint/crypto" tmcli "github.com/tendermint/tendermint/libs/cli" - authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli" "github.com/tendermint/tendermint/libs/common" "io/ioutil" "os" @@ -21,9 +21,9 @@ import ( ) const ( - defaultAmount = "100steak" - defaultCommissionRate = "0.1" - defaultCommissionMaxRate = "0.2" + defaultAmount = "100steak" + defaultCommissionRate = "0.1" + defaultCommissionMaxRate = "0.2" defaultCommissionMaxChangeRate = "0.01" ) @@ -90,16 +90,16 @@ following delegation and commission default parameters: } func prepareFlagsForTxCreateValidator(config *cfg.Config, nodeID, ip string, valPubKey crypto.PubKey) { - viper.Set(tmcli.HomeFlag, viper.GetString(flagClientHome)) // --home - viper.Set(client.FlagFrom, viper.GetString(client.FlagName)) // --from - viper.Set(cli.FlagNodeID, nodeID) // --node-id - viper.Set(cli.FlagIP, ip) // --ip + viper.Set(tmcli.HomeFlag, viper.GetString(flagClientHome)) // --home + viper.Set(client.FlagFrom, viper.GetString(client.FlagName)) // --from + viper.Set(cli.FlagNodeID, nodeID) // --node-id + viper.Set(cli.FlagIP, ip) // --ip viper.Set(cli.FlagPubKey, sdk.MustBech32ifyConsPub(valPubKey)) // --pubkey - viper.Set(cli.FlagAmount, defaultAmount) // --amount + viper.Set(cli.FlagAmount, defaultAmount) // --amount viper.Set(cli.FlagCommissionRate, defaultCommissionRate) viper.Set(cli.FlagCommissionMaxRate, defaultCommissionMaxRate) viper.Set(cli.FlagCommissionMaxChangeRate, defaultCommissionMaxChangeRate) - viper.Set(cli.FlagGenesisFormat, true) // --genesis-format + viper.Set(cli.FlagGenesisFormat, true) // --genesis-format viper.Set(cli.FlagMoniker, config.Moniker) // --moniker if config.Moniker == "" { viper.Set(cli.FlagMoniker, viper.GetString(client.FlagName)) diff --git a/x/distribution/keeper/hooks.go b/x/distribution/keeper/hooks.go index 721a26db1def..d551cc3a3e3a 100644 --- a/x/distribution/keeper/hooks.go +++ b/x/distribution/keeper/hooks.go @@ -14,9 +14,9 @@ func (k Keeper) onValidatorCreated(ctx sdk.Context, addr sdk.ValAddress) { vdi := types.ValidatorDistInfo{ OperatorAddr: addr, FeePoolWithdrawalHeight: height, - Pool: types.DecCoins{}, - PoolCommission: types.DecCoins{}, - DelAccum: types.NewTotalAccum(height), + Pool: types.DecCoins{}, + PoolCommission: types.DecCoins{}, + DelAccum: types.NewTotalAccum(height), } k.SetValidatorDistInfo(ctx, vdi) } diff --git a/x/distribution/types/validator_info.go b/x/distribution/types/validator_info.go index 18aef8bde6e4..c8a02569cba6 100644 --- a/x/distribution/types/validator_info.go +++ b/x/distribution/types/validator_info.go @@ -19,9 +19,9 @@ func NewValidatorDistInfo(operatorAddr sdk.ValAddress, currentHeight int64) Vali return ValidatorDistInfo{ OperatorAddr: operatorAddr, FeePoolWithdrawalHeight: currentHeight, - Pool: DecCoins{}, - PoolCommission: DecCoins{}, - DelAccum: NewTotalAccum(currentHeight), + Pool: DecCoins{}, + PoolCommission: DecCoins{}, + DelAccum: NewTotalAccum(currentHeight), } } From 7ab120fe167091f1ca7898f46768518774557998 Mon Sep 17 00:00:00 2001 From: Christopher Goes Date: Fri, 19 Oct 2018 19:13:20 +0200 Subject: [PATCH 81/83] Start simulation at height 1, like Tendermint --- x/mock/simulation/random_simulate_blocks.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/mock/simulation/random_simulate_blocks.go b/x/mock/simulation/random_simulate_blocks.go index 0af6fbbac11f..c4c27f0671b9 100644 --- a/x/mock/simulation/random_simulate_blocks.go +++ b/x/mock/simulation/random_simulate_blocks.go @@ -82,7 +82,7 @@ func SimulateFromSeed(tb testing.TB, app *baseapp.BaseApp, // Initially this is the same as the initial validator set nextValidators := validators - header := abci.Header{Height: 0, Time: timestamp, ProposerAddress: randomProposer(r, validators)} + header := abci.Header{Height: 1, Time: timestamp, ProposerAddress: randomProposer(r, validators)} opCount := 0 // Setup code to catch SIGTERM's From 7a9c9d92ae001765d0190ece972d155ebe8c27f4 Mon Sep 17 00:00:00 2001 From: Christopher Goes Date: Fri, 19 Oct 2018 19:21:31 +0200 Subject: [PATCH 82/83] Ensure we never fetch from height 0 --- x/mock/simulation/random_simulate_blocks.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/x/mock/simulation/random_simulate_blocks.go b/x/mock/simulation/random_simulate_blocks.go index c4c27f0671b9..ae643b51645d 100644 --- a/x/mock/simulation/random_simulate_blocks.go +++ b/x/mock/simulation/random_simulate_blocks.go @@ -385,6 +385,10 @@ func RandomRequestBeginBlock(r *rand.Rand, validators map[string]mockValidator, vals := voteInfos if r.Float64() < pastEvidenceFraction { height = int64(r.Intn(int(header.Height))) + // we start at height 1 + if height == 0 { + height = 1 + } time = pastTimes[height] vals = pastVoteInfos[height] } From aed1fbad6db89b953f56d705632c80172fae6327 Mon Sep 17 00:00:00 2001 From: Christopher Goes Date: Fri, 19 Oct 2018 19:24:51 +0200 Subject: [PATCH 83/83] Fix it the right way --- x/mock/simulation/random_simulate_blocks.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/x/mock/simulation/random_simulate_blocks.go b/x/mock/simulation/random_simulate_blocks.go index ae643b51645d..e52ca1068c6c 100644 --- a/x/mock/simulation/random_simulate_blocks.go +++ b/x/mock/simulation/random_simulate_blocks.go @@ -384,11 +384,7 @@ func RandomRequestBeginBlock(r *rand.Rand, validators map[string]mockValidator, time := header.Time vals := voteInfos if r.Float64() < pastEvidenceFraction { - height = int64(r.Intn(int(header.Height))) - // we start at height 1 - if height == 0 { - height = 1 - } + height = int64(r.Intn(int(header.Height) - 1)) time = pastTimes[height] vals = pastVoteInfos[height] }