From ab26eacbced417d75415d2afa0830fc3b35685cc Mon Sep 17 00:00:00 2001 From: blushi Date: Wed, 15 Jul 2020 17:20:57 +0200 Subject: [PATCH 01/24] Update genutil collect and gentx to use TxGenerator --- x/genutil/collect.go | 19 ++++++++++++------- x/genutil/gentx.go | 2 +- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/x/genutil/collect.go b/x/genutil/collect.go index 82ebb2a10285..5eda19b06b27 100644 --- a/x/genutil/collect.go +++ b/x/genutil/collect.go @@ -17,7 +17,6 @@ import ( "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" - authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" bankexported "github.com/cosmos/cosmos-sdk/x/bank/exported" "github.com/cosmos/cosmos-sdk/x/genutil/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" @@ -29,7 +28,7 @@ func GenAppStateFromConfig(cdc codec.JSONMarshaler, config *cfg.Config, ) (appState json.RawMessage, err error) { // process genesis transactions, else create default genesis.json - appGenTxs, persistentPeers, err := CollectStdTxs( + appGenTxs, persistentPeers, err := CollectTxs( cdc, config.Moniker, initCfg.GenTxsDir, genDoc, genBalIterator, ) if err != nil { @@ -66,11 +65,11 @@ func GenAppStateFromConfig(cdc codec.JSONMarshaler, config *cfg.Config, return appState, err } -// CollectStdTxs processes and validates application's genesis StdTxs and returns +// CollectTxs processes and validates application's genesis Txs and returns // the list of appGenTxs, and persistent peers required to generate genesis.json. -func CollectStdTxs(cdc codec.JSONMarshaler, moniker, genTxsDir string, +func CollectTxs(cdc codec.JSONMarshaler, moniker, genTxsDir string, genDoc tmtypes.GenesisDoc, genBalIterator types.GenesisBalancesIterator, -) (appGenTxs []authtypes.StdTx, persistentPeers string, err error) { +) (appGenTxs []sdk.Tx, persistentPeers string, err error) { var fos []os.FileInfo fos, err = ioutil.ReadDir(genTxsDir) @@ -111,7 +110,7 @@ func CollectStdTxs(cdc codec.JSONMarshaler, moniker, genTxsDir string, return appGenTxs, persistentPeers, err } - var genStdTx authtypes.StdTx + var genStdTx sdk.Tx if err = cdc.UnmarshalJSON(jsonRawTx, &genStdTx); err != nil { return appGenTxs, persistentPeers, err } @@ -121,7 +120,13 @@ func CollectStdTxs(cdc codec.JSONMarshaler, moniker, genTxsDir string, // the memo flag is used to store // the ip and node-id, for example this may be: // "528fd3df22b31f4969b05652bfe8f0fe921321d5@192.168.2.37:26656" - nodeAddrIP := genStdTx.GetMemo() + + memoTx, ok := genStdTx.(sdk.TxWithMemo) + if !ok { + return appGenTxs, persistentPeers, fmt.Errorf("expected TxWithMemo, got %T", genStdTx) + } + nodeAddrIP := memoTx.GetMemo() + fmt.Printf("NODE: %v", nodeAddrIP) if len(nodeAddrIP) == 0 { return appGenTxs, persistentPeers, fmt.Errorf("failed to find node's address and IP in %s", fo.Name()) } diff --git a/x/genutil/gentx.go b/x/genutil/gentx.go index dea85012e142..e75c02bb111b 100644 --- a/x/genutil/gentx.go +++ b/x/genutil/gentx.go @@ -18,7 +18,7 @@ import ( // SetGenTxsInAppGenesisState - sets the genesis transactions in the app genesis state func SetGenTxsInAppGenesisState( - cdc codec.JSONMarshaler, appGenesisState map[string]json.RawMessage, genTxs []authtypes.StdTx, + cdc codec.JSONMarshaler, appGenesisState map[string]json.RawMessage, genTxs []sdk.Tx, ) (map[string]json.RawMessage, error) { genesisState := types.GetGenesisStateFromAppState(cdc, appGenesisState) From aa5fd3005ffc40399244839480fe0d04a1e51444 Mon Sep 17 00:00:00 2001 From: blushi Date: Wed, 15 Jul 2020 18:17:44 +0200 Subject: [PATCH 02/24] Remove print statement --- x/genutil/collect.go | 1 - 1 file changed, 1 deletion(-) diff --git a/x/genutil/collect.go b/x/genutil/collect.go index 5eda19b06b27..620b88557503 100644 --- a/x/genutil/collect.go +++ b/x/genutil/collect.go @@ -126,7 +126,6 @@ func CollectTxs(cdc codec.JSONMarshaler, moniker, genTxsDir string, return appGenTxs, persistentPeers, fmt.Errorf("expected TxWithMemo, got %T", genStdTx) } nodeAddrIP := memoTx.GetMemo() - fmt.Printf("NODE: %v", nodeAddrIP) if len(nodeAddrIP) == 0 { return appGenTxs, persistentPeers, fmt.Errorf("failed to find node's address and IP in %s", fo.Name()) } From fd5d4001d4fdee3bcd210db12c785174e60627ae Mon Sep 17 00:00:00 2001 From: blushi Date: Thu, 16 Jul 2020 14:03:38 +0200 Subject: [PATCH 03/24] Use Tx in genutil DeliverGenTxs --- x/genutil/gentx.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/x/genutil/gentx.go b/x/genutil/gentx.go index e75c02bb111b..b83a0fd3c47b 100644 --- a/x/genutil/gentx.go +++ b/x/genutil/gentx.go @@ -10,7 +10,6 @@ import ( "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" - authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" bankexported "github.com/cosmos/cosmos-sdk/x/bank/exported" "github.com/cosmos/cosmos-sdk/x/genutil/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" @@ -90,8 +89,8 @@ func ValidateAccountInGenesis( type deliverTxfn func(abci.RequestDeliverTx) abci.ResponseDeliverTx -// DeliverGenTxs iterates over all genesis txs, decodes each into a StdTx and -// invokes the provided deliverTxfn with the decoded StdTx. It returns the result +// DeliverGenTxs iterates over all genesis txs, decodes each into a Tx and +// invokes the provided deliverTxfn with the decoded Tx. It returns the result // of the staking module's ApplyAndReturnValidatorSetUpdates. func DeliverGenTxs( ctx sdk.Context, cdc *codec.Codec, genTxs []json.RawMessage, @@ -99,7 +98,7 @@ func DeliverGenTxs( ) []abci.ValidatorUpdate { for _, genTx := range genTxs { - var tx authtypes.StdTx + var tx sdk.Tx cdc.MustUnmarshalJSON(genTx, &tx) bz := cdc.MustMarshalBinaryBare(tx) From 4259efcd715402174b866f0e747e3a52ec8c0af4 Mon Sep 17 00:00:00 2001 From: blushi Date: Thu, 16 Jul 2020 16:20:50 +0200 Subject: [PATCH 04/24] Use Tx in genutil genesis_state --- x/genutil/types/genesis_state.go | 5 +++-- x/genutil/types/genesis_state_test.go | 25 +++++++++++++++++-------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/x/genutil/types/genesis_state.go b/x/genutil/types/genesis_state.go index ef74b3590441..b42bd8219e4a 100644 --- a/x/genutil/types/genesis_state.go +++ b/x/genutil/types/genesis_state.go @@ -9,6 +9,7 @@ import ( tmtypes "github.com/tendermint/tendermint/types" "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" ) @@ -36,9 +37,9 @@ func DefaultGenesisState() GenesisState { } } -// NewGenesisStateFromStdTx creates a new GenesisState object +// NewGenesisStateFromTx creates a new GenesisState object // from auth transactions -func NewGenesisStateFromStdTx(genTxs []authtypes.StdTx) GenesisState { +func NewGenesisStateFromTx(genTxs []sdk.Tx) GenesisState { genTxsBz := make([]json.RawMessage, len(genTxs)) for i, genTx := range genTxs { genTxsBz[i] = ModuleCdc.MustMarshalJSON(genTx) diff --git a/x/genutil/types/genesis_state_test.go b/x/genutil/types/genesis_state_test.go index b82a89b98a13..cc23a2e9e11c 100644 --- a/x/genutil/types/genesis_state_test.go +++ b/x/genutil/types/genesis_state_test.go @@ -8,8 +8,8 @@ import ( "github.com/stretchr/testify/require" "github.com/tendermint/tendermint/crypto/ed25519" + "github.com/cosmos/cosmos-sdk/simapp/params" sdk "github.com/cosmos/cosmos-sdk/types" - authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" ) @@ -31,7 +31,6 @@ func TestNetGenesisState(t *testing.T) { } func TestValidateGenesisMultipleMessages(t *testing.T) { - desc := stakingtypes.NewDescription("testname", "", "", "", "") comm := stakingtypes.CommissionRates{} @@ -41,10 +40,15 @@ func TestValidateGenesisMultipleMessages(t *testing.T) { msg2 := stakingtypes.NewMsgCreateValidator(sdk.ValAddress(pk2.Address()), pk2, sdk.NewInt64Coin(sdk.DefaultBondDenom, 50), desc, comm, sdk.OneInt()) - genTxs := authtypes.NewStdTx([]sdk.Msg{msg1, msg2}, authtypes.StdFee{}, nil, "") - genesisState := NewGenesisStateFromStdTx([]authtypes.StdTx{genTxs}) + txGen := params.MakeEncodingConfig().TxGenerator + txBuilder := txGen.NewTxBuilder() + err := txBuilder.SetMsgs(msg1, msg2) + require.NoError(t, err) + + tx := txBuilder.GetTx() + genesisState := NewGenesisStateFromTx([]sdk.Tx{tx}) - err := ValidateGenesis(genesisState) + err = ValidateGenesis(genesisState) require.Error(t, err) } @@ -53,9 +57,14 @@ func TestValidateGenesisBadMessage(t *testing.T) { msg1 := stakingtypes.NewMsgEditValidator(sdk.ValAddress(pk1.Address()), desc, nil, nil) - genTxs := authtypes.NewStdTx([]sdk.Msg{msg1}, authtypes.StdFee{}, nil, "") - genesisState := NewGenesisStateFromStdTx([]authtypes.StdTx{genTxs}) + txGen := params.MakeEncodingConfig().TxGenerator + txBuilder := txGen.NewTxBuilder() + err := txBuilder.SetMsgs(msg1) + require.NoError(t, err) + + tx := txBuilder.GetTx() + genesisState := NewGenesisStateFromTx([]sdk.Tx{tx}) - err := ValidateGenesis(genesisState) + err = ValidateGenesis(genesisState) require.Error(t, err) } From cec4fa62faa3a86f548ef5ed9c68fdf4ceeedddd Mon Sep 17 00:00:00 2001 From: blushi Date: Thu, 16 Jul 2020 16:47:52 +0200 Subject: [PATCH 05/24] Use Tx in ValidateGenesis --- x/genutil/collect.go | 14 +++++++------- x/genutil/types/genesis_state.go | 5 ++--- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/x/genutil/collect.go b/x/genutil/collect.go index 620b88557503..43fdece54017 100644 --- a/x/genutil/collect.go +++ b/x/genutil/collect.go @@ -103,27 +103,27 @@ func CollectTxs(cdc codec.JSONMarshaler, moniker, genTxsDir string, continue } - // get the genStdTx + // get the genTx var jsonRawTx []byte if jsonRawTx, err = ioutil.ReadFile(filename); err != nil { return appGenTxs, persistentPeers, err } - var genStdTx sdk.Tx - if err = cdc.UnmarshalJSON(jsonRawTx, &genStdTx); err != nil { + var genTx sdk.Tx + if err = cdc.UnmarshalJSON(jsonRawTx, &genTx); err != nil { return appGenTxs, persistentPeers, err } - appGenTxs = append(appGenTxs, genStdTx) + appGenTxs = append(appGenTxs, genTx) // the memo flag is used to store // the ip and node-id, for example this may be: // "528fd3df22b31f4969b05652bfe8f0fe921321d5@192.168.2.37:26656" - memoTx, ok := genStdTx.(sdk.TxWithMemo) + memoTx, ok := genTx.(sdk.TxWithMemo) if !ok { - return appGenTxs, persistentPeers, fmt.Errorf("expected TxWithMemo, got %T", genStdTx) + return appGenTxs, persistentPeers, fmt.Errorf("expected TxWithMemo, got %T", genTx) } nodeAddrIP := memoTx.GetMemo() if len(nodeAddrIP) == 0 { @@ -131,7 +131,7 @@ func CollectTxs(cdc codec.JSONMarshaler, moniker, genTxsDir string, } // genesis transactions must be single-message - msgs := genStdTx.GetMsgs() + msgs := genTx.GetMsgs() if len(msgs) != 1 { return appGenTxs, persistentPeers, errors.New("each genesis transaction must provide a single genesis message") } diff --git a/x/genutil/types/genesis_state.go b/x/genutil/types/genesis_state.go index b42bd8219e4a..c9bea5af2730 100644 --- a/x/genutil/types/genesis_state.go +++ b/x/genutil/types/genesis_state.go @@ -10,7 +10,6 @@ import ( "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" - authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" ) @@ -101,7 +100,7 @@ func GenesisStateFromGenFile(cdc codec.JSONMarshaler, genFile string) (genesisSt // ValidateGenesis validates GenTx transactions func ValidateGenesis(genesisState GenesisState) error { for i, genTx := range genesisState.GenTxs { - var tx authtypes.StdTx + var tx sdk.Tx if err := ModuleCdc.UnmarshalJSON(genTx, &tx); err != nil { return err } @@ -109,7 +108,7 @@ func ValidateGenesis(genesisState GenesisState) error { msgs := tx.GetMsgs() if len(msgs) != 1 { return errors.New( - "must provide genesis StdTx with exactly 1 CreateValidator message") + "must provide genesis Tx with exactly 1 CreateValidator message") } // TODO: abstract back to staking From b02117c9816933d15621471cd5eaa66a488023f3 Mon Sep 17 00:00:00 2001 From: blushi Date: Fri, 17 Jul 2020 10:10:03 +0200 Subject: [PATCH 06/24] Use amino txJSONDecoder and txBinaryEncoder in genutil InitGenesis --- simapp/amino.go | 19 +++++++++++++++++++ simapp/app.go | 5 ++++- x/bank/module.go | 1 + x/genutil/genesis.go | 6 +++--- x/genutil/gentx.go | 14 ++++++++++---- x/genutil/module.go | 24 +++++++++++++++--------- 6 files changed, 52 insertions(+), 17 deletions(-) create mode 100644 simapp/amino.go diff --git a/simapp/amino.go b/simapp/amino.go new file mode 100644 index 000000000000..c683cf6d5589 --- /dev/null +++ b/simapp/amino.go @@ -0,0 +1,19 @@ +package simapp + +import ( + "github.com/cosmos/cosmos-sdk/simapp/params" + "github.com/cosmos/cosmos-sdk/types" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" +) + +// AminoJSONTxDecoder returns an Amino JSON TxDecoder using the provided cfg Marshaler +func AminoJSONTxDecoder(cfg params.EncodingConfig) types.TxDecoder { + return func(txBytes []byte) (types.Tx, error) { + var tx authtypes.StdTx + err := cfg.Marshaler.UnmarshalJSON(txBytes, &tx) + if err != nil { + return nil, err + } + return tx, nil + } +} diff --git a/simapp/app.go b/simapp/app.go index dc619a85745d..7e636661bd9e 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -289,7 +289,10 @@ func NewSimApp( // NOTE: Any module instantiated in the module manager that is later modified // must be passed by reference here. app.mm = module.NewManager( - genutil.NewAppModule(app.AccountKeeper, app.StakingKeeper, app.BaseApp.DeliverTx), + genutil.NewAppModule( + app.AccountKeeper, app.StakingKeeper, app.BaseApp.DeliverTx, + AminoJSONTxDecoder(encodingConfig), encodingConfig.TxGenerator.TxEncoder(), + ), auth.NewAppModule(appCodec, app.AccountKeeper), bank.NewAppModule(appCodec, app.BankKeeper, app.AccountKeeper), capability.NewAppModule(appCodec, *app.CapabilityKeeper), diff --git a/x/bank/module.go b/x/bank/module.go index 6719d364ce62..5e28de876597 100644 --- a/x/bank/module.go +++ b/x/bank/module.go @@ -125,6 +125,7 @@ func (am AppModule) NewQuerierHandler() sdk.Querier { // no validator updates. func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONMarshaler, data json.RawMessage) []abci.ValidatorUpdate { var genesisState types.GenesisState + cdc.MustUnmarshalJSON(data, &genesisState) am.keeper.InitGenesis(ctx, genesisState) return []abci.ValidatorUpdate{} diff --git a/x/genutil/genesis.go b/x/genutil/genesis.go index 335712a33031..489f55747d77 100644 --- a/x/genutil/genesis.go +++ b/x/genutil/genesis.go @@ -3,20 +3,20 @@ package genutil import ( abci "github.com/tendermint/tendermint/abci/types" - "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/genutil/types" ) // InitGenesis - initialize accounts and deliver genesis transactions func InitGenesis( - ctx sdk.Context, cdc *codec.Codec, stakingKeeper types.StakingKeeper, + ctx sdk.Context, stakingKeeper types.StakingKeeper, deliverTx deliverTxfn, genesisState types.GenesisState, + txJSONDecoder sdk.TxDecoder, txBinaryEncoder sdk.TxEncoder, ) []abci.ValidatorUpdate { var validators []abci.ValidatorUpdate if len(genesisState.GenTxs) > 0 { - validators = DeliverGenTxs(ctx, cdc, genesisState.GenTxs, stakingKeeper, deliverTx) + validators = DeliverGenTxs(ctx, genesisState.GenTxs, stakingKeeper, deliverTx, txJSONDecoder, txBinaryEncoder) } return validators diff --git a/x/genutil/gentx.go b/x/genutil/gentx.go index b83a0fd3c47b..6b3167de52f9 100644 --- a/x/genutil/gentx.go +++ b/x/genutil/gentx.go @@ -93,15 +93,21 @@ type deliverTxfn func(abci.RequestDeliverTx) abci.ResponseDeliverTx // invokes the provided deliverTxfn with the decoded Tx. It returns the result // of the staking module's ApplyAndReturnValidatorSetUpdates. func DeliverGenTxs( - ctx sdk.Context, cdc *codec.Codec, genTxs []json.RawMessage, + ctx sdk.Context, genTxs []json.RawMessage, stakingKeeper types.StakingKeeper, deliverTx deliverTxfn, + txJSONDecoder sdk.TxDecoder, txBinaryEncoder sdk.TxEncoder, ) []abci.ValidatorUpdate { for _, genTx := range genTxs { - var tx sdk.Tx - cdc.MustUnmarshalJSON(genTx, &tx) + tx, err := txJSONDecoder(genTx) + if err != nil { + panic(err) + } - bz := cdc.MustMarshalBinaryBare(tx) + bz, err := txBinaryEncoder(tx) + if err != nil { + panic(err) + } res := deliverTx(abci.RequestDeliverTx{Tx: bz}) if !res.IsOK() { diff --git a/x/genutil/module.go b/x/genutil/module.go index 105a4da55365..1bd473b72bb1 100644 --- a/x/genutil/module.go +++ b/x/genutil/module.go @@ -63,20 +63,26 @@ func (AppModuleBasic) GetQueryCmd(clientCtx client.Context) *cobra.Command { ret type AppModule struct { AppModuleBasic - accountKeeper types.AccountKeeper - stakingKeeper types.StakingKeeper - deliverTx deliverTxfn + accountKeeper types.AccountKeeper + stakingKeeper types.StakingKeeper + deliverTx deliverTxfn + txJSONDecoder sdk.TxDecoder + txBinaryEncoder sdk.TxEncoder } // NewAppModule creates a new AppModule object func NewAppModule(accountKeeper types.AccountKeeper, - stakingKeeper types.StakingKeeper, deliverTx deliverTxfn) module.AppModule { + stakingKeeper types.StakingKeeper, deliverTx deliverTxfn, + txJSONDecoder sdk.TxDecoder, txBinaryEncoder sdk.TxEncoder, +) module.AppModule { return module.NewGenesisOnlyAppModule(AppModule{ - AppModuleBasic: AppModuleBasic{}, - accountKeeper: accountKeeper, - stakingKeeper: stakingKeeper, - deliverTx: deliverTx, + AppModuleBasic: AppModuleBasic{}, + accountKeeper: accountKeeper, + stakingKeeper: stakingKeeper, + deliverTx: deliverTx, + txJSONDecoder: txJSONDecoder, + txBinaryEncoder: txBinaryEncoder, }) } @@ -85,7 +91,7 @@ func NewAppModule(accountKeeper types.AccountKeeper, func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONMarshaler, data json.RawMessage) []abci.ValidatorUpdate { var genesisState types.GenesisState cdc.MustUnmarshalJSON(data, &genesisState) - return InitGenesis(ctx, types.ModuleCdc, am.stakingKeeper, am.deliverTx, genesisState) + return InitGenesis(ctx, am.stakingKeeper, am.deliverTx, genesisState, am.txJSONDecoder, am.txBinaryEncoder) } // ExportGenesis returns the exported genesis state as raw bytes for the genutil From f40eb9cc0c58290e7f616026528772187c427ec2 Mon Sep 17 00:00:00 2001 From: blushi Date: Wed, 22 Jul 2020 10:54:10 +0200 Subject: [PATCH 07/24] Use TxConfig in place of TxGenerator --- simapp/app.go | 2 +- x/genutil/types/genesis_state_test.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/simapp/app.go b/simapp/app.go index ade4cc4ec9c0..799442d2e878 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -291,7 +291,7 @@ func NewSimApp( app.mm = module.NewManager( genutil.NewAppModule( app.AccountKeeper, app.StakingKeeper, app.BaseApp.DeliverTx, - AminoJSONTxDecoder(encodingConfig), encodingConfig.TxGenerator.TxEncoder(), + AminoJSONTxDecoder(encodingConfig), encodingConfig.TxConfig.TxEncoder(), ), auth.NewAppModule(appCodec, app.AccountKeeper), bank.NewAppModule(appCodec, app.BankKeeper, app.AccountKeeper), diff --git a/x/genutil/types/genesis_state_test.go b/x/genutil/types/genesis_state_test.go index cc23a2e9e11c..6f2776da926e 100644 --- a/x/genutil/types/genesis_state_test.go +++ b/x/genutil/types/genesis_state_test.go @@ -40,7 +40,7 @@ func TestValidateGenesisMultipleMessages(t *testing.T) { msg2 := stakingtypes.NewMsgCreateValidator(sdk.ValAddress(pk2.Address()), pk2, sdk.NewInt64Coin(sdk.DefaultBondDenom, 50), desc, comm, sdk.OneInt()) - txGen := params.MakeEncodingConfig().TxGenerator + txGen := params.MakeEncodingConfig().TxConfig txBuilder := txGen.NewTxBuilder() err := txBuilder.SetMsgs(msg1, msg2) require.NoError(t, err) @@ -57,7 +57,7 @@ func TestValidateGenesisBadMessage(t *testing.T) { msg1 := stakingtypes.NewMsgEditValidator(sdk.ValAddress(pk1.Address()), desc, nil, nil) - txGen := params.MakeEncodingConfig().TxGenerator + txGen := params.MakeEncodingConfig().TxConfig txBuilder := txGen.NewTxBuilder() err := txBuilder.SetMsgs(msg1) require.NoError(t, err) From 9e31db926e5dd703e0f38802b0ddf1d3fdef61c6 Mon Sep 17 00:00:00 2001 From: blushi Date: Thu, 23 Jul 2020 15:03:00 +0200 Subject: [PATCH 08/24] Add gentx tests --- x/genutil/gentx_test.go | 316 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 307 insertions(+), 9 deletions(-) diff --git a/x/genutil/gentx_test.go b/x/genutil/gentx_test.go index ea03ce733ce9..24c91cc8ecf7 100644 --- a/x/genutil/gentx_test.go +++ b/x/genutil/gentx_test.go @@ -1,14 +1,312 @@ -package genutil +package genutil_test -import "testing" +import ( + "encoding/json" + "fmt" + "testing" -func TestGenTx(t *testing.T) { + "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/simapp" + "github.com/cosmos/cosmos-sdk/simapp/helpers" + simappparams "github.com/cosmos/cosmos-sdk/simapp/params" + "github.com/cosmos/cosmos-sdk/testutil/testdata" + sdk "github.com/cosmos/cosmos-sdk/types" - // TODO test that key overwrite flags work / no overwrites if set off - // TODO test validator created has provided pubkey - // TODO test the account created has the correct pubkey + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" + "github.com/cosmos/cosmos-sdk/x/genutil" + "github.com/cosmos/cosmos-sdk/x/genutil/types" + staking "github.com/cosmos/cosmos-sdk/x/staking" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" + "github.com/stretchr/testify/suite" + abci "github.com/tendermint/tendermint/abci/types" + "github.com/tendermint/tendermint/crypto/secp256k1" +) - // TODO test must provide at least genesis transaction - // TODO test with both one and two genesis transactions: - // TODO correct: genesis account created, canididates created, pool token variance +var ( + priv1 = secp256k1.GenPrivKey() + priv2 = secp256k1.GenPrivKey() + pk1 = priv1.PubKey() + pk2 = priv2.PubKey() + addr1 = sdk.AccAddress(pk1.Address()) + addr2 = sdk.AccAddress(pk2.Address()) + desc = stakingtypes.NewDescription("testname", "", "", "", "") + comm = stakingtypes.CommissionRates{} + msg1 = stakingtypes.NewMsgCreateValidator(sdk.ValAddress(pk1.Address()), pk1, + sdk.NewInt64Coin(sdk.DefaultBondDenom, 50), desc, comm, sdk.OneInt()) + msg2 = stakingtypes.NewMsgCreateValidator(sdk.ValAddress(pk2.Address()), pk1, + sdk.NewInt64Coin(sdk.DefaultBondDenom, 50), desc, comm, sdk.OneInt()) +) + +// GenTxTestSuite is a test suite to be used with gentx tests. +type GenTxTestSuite struct { + suite.Suite + + ctx sdk.Context + app *simapp.SimApp + encodingConfig simappparams.EncodingConfig +} + +func (suite *GenTxTestSuite) SetupTest() { + checkTx := false + app := simapp.Setup(checkTx) + suite.ctx = app.BaseApp.NewContext(checkTx, abci.Header{}) + suite.app = app + + suite.encodingConfig = simapp.MakeEncodingConfig() + // sdk.RegisterCodec(suite.encodingConfig.Amino) +} + +func (suite *GenTxTestSuite) setAccountBalance(cdc *codec.Codec, addr sdk.AccAddress, amount int64) json.RawMessage { + acc := suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, addr) + suite.app.AccountKeeper.SetAccount(suite.ctx, acc) + + err := suite.app.BankKeeper.SetBalances( + suite.ctx, addr, sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 25)}, + ) + suite.Require().NoError(err) + + bankGenesisState := suite.app.BankKeeper.ExportGenesis(suite.ctx) + bankGenesis, err := cdc.MarshalJSON(bankGenesisState) + suite.Require().NoError(err) + + return bankGenesis +} + +func (suite *GenTxTestSuite) TestSetGenTxsInAppGenesisState() { + var ( + txBuilder = suite.encodingConfig.TxConfig.NewTxBuilder() + cdc *codec.Codec + genTxs []sdk.Tx + ) + + testCases := []struct { + msg string + malleate func() + expPass bool + }{ + { + "unregistered message", + func() { + msg := testdata.NewTestMsg(sdk.AccAddress("some-address")) + err := txBuilder.SetMsgs(msg) + suite.Require().NoError(err) + tx := txBuilder.GetTx() + genTxs = []sdk.Tx{tx} + }, + false, + }, + { + "one genesis transaction", + func() { + err := txBuilder.SetMsgs(msg1) + suite.Require().NoError(err) + tx := txBuilder.GetTx() + genTxs = []sdk.Tx{tx} + }, + true, + }, + { + "two genesis transactions", + func() { + err := txBuilder.SetMsgs(msg1, msg2) + suite.Require().NoError(err) + tx := txBuilder.GetTx() + genTxs = []sdk.Tx{tx} + }, + true, + }, + } + + for _, tc := range testCases { + suite.Run(fmt.Sprintf("Case %s", tc.msg), func() { + suite.SetupTest() + cdc = suite.app.Codec() + + tc.malleate() + appGenesisState, err := genutil.SetGenTxsInAppGenesisState(cdc, make(map[string]json.RawMessage), genTxs) + + if tc.expPass { + suite.Require().NoError(err) + suite.Require().NotNil(appGenesisState[types.ModuleName]) + + var genesisState types.GenesisState + err := cdc.UnmarshalJSON(appGenesisState[types.ModuleName], &genesisState) + suite.Require().NoError(err) + suite.Require().NotNil(genesisState.GenTxs) + } else { + suite.Require().Error(err) + } + }) + } +} + +func (suite *GenTxTestSuite) TestValidateAccountInGenesis() { + var ( + appGenesisState = make(map[string]json.RawMessage) + cdc *codec.Codec + coins sdk.Coins + ) + + testCases := []struct { + msg string + malleate func() + expPass bool + }{ + { + "no accounts", + func() { + coins = sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 0)} + }, + false, + }, + { + "account without balance in the genesis state", + func() { + coins = sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 0)} + appGenesisState[banktypes.ModuleName] = suite.setAccountBalance(cdc, addr2, 50) + }, + false, + }, + { + "account without enough funds of default bond denom", + func() { + coins = sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 50)} + appGenesisState[banktypes.ModuleName] = suite.setAccountBalance(cdc, addr1, 25) + }, + false, + }, + { + "account with enough funds of default bond denom", + func() { + coins = sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 10)} + appGenesisState[banktypes.ModuleName] = suite.setAccountBalance(cdc, addr1, 25) + }, + true, + }, + } + for _, tc := range testCases { + suite.Run(fmt.Sprintf("Case %s", tc.msg), func() { + suite.SetupTest() + cdc = suite.app.Codec() + + suite.app.StakingKeeper.SetParams(suite.ctx, stakingtypes.DefaultParams()) + stakingGenesisState := staking.ExportGenesis(suite.ctx, suite.app.StakingKeeper) + suite.Require().Equal(stakingGenesisState.Params, stakingtypes.DefaultParams()) + stakingGenesis, err := cdc.MarshalJSON(stakingGenesisState) + suite.Require().NoError(err) + appGenesisState[stakingtypes.ModuleName] = stakingGenesis + + tc.malleate() + err = genutil.ValidateAccountInGenesis( + appGenesisState, banktypes.GenesisBalancesIterator{}, + addr1, coins, cdc, + ) + + if tc.expPass { + suite.Require().NoError(err) + } else { + suite.Require().Error(err) + } + + }) + } +} + +func (suite *GenTxTestSuite) TestDeliverGenTxs() { + var ( + genTxs []json.RawMessage + cdc *codec.Codec + txBuilder = suite.encodingConfig.TxConfig.NewTxBuilder() + ) + + testCases := []struct { + msg string + malleate func() + expPass bool + }{ + { + "no signature supplied", + func() { + err := txBuilder.SetMsgs(msg1) + suite.Require().NoError(err) + + genTxs = make([]json.RawMessage, 1) + tx, err := cdc.MarshalJSON(txBuilder.GetTx()) + suite.Require().NoError(err) + genTxs[0] = tx + }, + false, + }, + { + "unregistered message", + func() { + cdc.RegisterConcrete(testdata.TestMsg{}, "cosmos-sdk/Test", nil) + msg := testdata.NewTestMsg(sdk.AccAddress("some-address")) + + err := txBuilder.SetMsgs(msg) + suite.Require().NoError(err) + + genTxs = make([]json.RawMessage, 1) + tx, err := cdc.MarshalJSON(txBuilder.GetTx()) + suite.Require().NoError(err) + genTxs[0] = tx + }, + false, + }, + { + "success", + func() { + _ = suite.setAccountBalance(cdc, addr1, 50) + _ = suite.setAccountBalance(cdc, addr2, 0) + + msg := banktypes.NewMsgSend(addr1, addr2, sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 1)}) + tx, err := helpers.GenTx( + suite.encodingConfig.TxConfig, + []sdk.Msg{msg}, + sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 10)}, + helpers.DefaultGenTxGas, + suite.ctx.ChainID(), + []uint64{0}, + []uint64{0}, + priv1, + ) + suite.Require().NoError(err) + + genTxs = make([]json.RawMessage, 1) + genTx, err := cdc.MarshalJSON(tx) + suite.Require().NoError(err) + genTxs[0] = genTx + }, + true, + }, + } + + for _, tc := range testCases { + suite.Run(fmt.Sprintf("Case %s", tc.msg), func() { + suite.SetupTest() + cdc = suite.app.Codec() + + tc.malleate() + + if tc.expPass { + suite.Require().NotPanics(func() { + genutil.DeliverGenTxs( + suite.ctx, genTxs, suite.app.StakingKeeper, suite.app.BaseApp.DeliverTx, + simapp.AminoJSONTxDecoder(suite.encodingConfig), suite.encodingConfig.TxConfig.TxEncoder(), + ) + }) + } else { + suite.Require().Panics(func() { + genutil.DeliverGenTxs( + suite.ctx, genTxs, suite.app.StakingKeeper, suite.app.BaseApp.DeliverTx, + simapp.AminoJSONTxDecoder(suite.encodingConfig), suite.encodingConfig.TxConfig.TxEncoder(), + ) + }) + } + }) + } +} + +func TestGenTxTestSuite(t *testing.T) { + suite.Run(t, new(GenTxTestSuite)) } From 51a7e8ad12d15e11fa560ffee689efebc93cd167 Mon Sep 17 00:00:00 2001 From: blushi Date: Thu, 23 Jul 2020 15:11:49 +0200 Subject: [PATCH 09/24] Remove commented line --- x/genutil/gentx_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/x/genutil/gentx_test.go b/x/genutil/gentx_test.go index 24c91cc8ecf7..35fc77d7bcf8 100644 --- a/x/genutil/gentx_test.go +++ b/x/genutil/gentx_test.go @@ -53,7 +53,6 @@ func (suite *GenTxTestSuite) SetupTest() { suite.app = app suite.encodingConfig = simapp.MakeEncodingConfig() - // sdk.RegisterCodec(suite.encodingConfig.Amino) } func (suite *GenTxTestSuite) setAccountBalance(cdc *codec.Codec, addr sdk.AccAddress, amount int64) json.RawMessage { From 66190187edb35f35d1337e941631e8442a33fecb Mon Sep 17 00:00:00 2001 From: Aaron Craelius Date: Thu, 23 Jul 2020 14:57:19 -0400 Subject: [PATCH 10/24] Test fixes --- simapp/app.go | 2 +- testutil/network/network.go | 17 +++++++++---- x/genutil/gentx_test.go | 51 +++++++++++++------------------------ 3 files changed, 30 insertions(+), 40 deletions(-) diff --git a/simapp/app.go b/simapp/app.go index 908582714510..988386314b96 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -291,7 +291,7 @@ func NewSimApp( app.mm = module.NewManager( genutil.NewAppModule( app.AccountKeeper, app.StakingKeeper, app.BaseApp.DeliverTx, - AminoJSONTxDecoder(encodingConfig), encodingConfig.TxConfig.TxEncoder(), + encodingConfig.TxConfig.TxJSONDecoder(), encodingConfig.TxConfig.TxEncoder(), ), auth.NewAppModule(appCodec, app.AccountKeeper), bank.NewAppModule(appCodec, app.BankKeeper, app.AccountKeeper), diff --git a/testutil/network/network.go b/testutil/network/network.go index 9c6621bc0f3d..9f4b23f0cc39 100644 --- a/testutil/network/network.go +++ b/testutil/network/network.go @@ -13,6 +13,8 @@ import ( "testing" "time" + "github.com/cosmos/cosmos-sdk/client/tx" + "github.com/stretchr/testify/require" tmcfg "github.com/tendermint/tendermint/config" "github.com/tendermint/tendermint/crypto" @@ -278,16 +280,21 @@ func New(t *testing.T, cfg Config) *Network { require.NoError(t, err) memo := fmt.Sprintf("%s@%s:%s", nodeIDs[i], p2pURL.Hostname(), p2pURL.Port()) - tx := authtypes.NewStdTx([]sdk.Msg{createValMsg}, authtypes.StdFee{}, []authtypes.StdSignature{}, memo) //nolint:staticcheck // SA1019: authtypes.StdFee is deprecated - txBldr := authtypes.TxBuilder{}. + txBuilder := cfg.TxConfig.NewTxBuilder() + err = txBuilder.SetMsgs(createValMsg) + require.NoError(t, err) + txBuilder.SetMemo(memo) + txFactory := tx.Factory{} + txFactory = txFactory. WithChainID(cfg.ChainID). WithMemo(memo). - WithKeybase(kb) + WithKeybase(kb). + WithTxConfig(cfg.TxConfig) - signedTx, err := txBldr.SignStdTx(nodeDirName, tx, false) + err = tx.Sign(txFactory, nodeDirName, txBuilder) require.NoError(t, err) - txBz, err := cfg.Codec.MarshalJSON(signedTx) + txBz, err := cfg.TxConfig.TxJSONEncoder()(txBuilder.GetTx()) require.NoError(t, err) require.NoError(t, writeFile(fmt.Sprintf("%v.json", nodeDirName), gentxsDir, txBz)) diff --git a/x/genutil/gentx_test.go b/x/genutil/gentx_test.go index 35fc77d7bcf8..e96489e8f8e5 100644 --- a/x/genutil/gentx_test.go +++ b/x/genutil/gentx_test.go @@ -12,14 +12,15 @@ import ( "github.com/cosmos/cosmos-sdk/testutil/testdata" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/stretchr/testify/suite" + abci "github.com/tendermint/tendermint/abci/types" + "github.com/tendermint/tendermint/crypto/secp256k1" + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" "github.com/cosmos/cosmos-sdk/x/genutil" "github.com/cosmos/cosmos-sdk/x/genutil/types" staking "github.com/cosmos/cosmos-sdk/x/staking" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" - "github.com/stretchr/testify/suite" - abci "github.com/tendermint/tendermint/abci/types" - "github.com/tendermint/tendermint/crypto/secp256k1" ) var ( @@ -55,7 +56,7 @@ func (suite *GenTxTestSuite) SetupTest() { suite.encodingConfig = simapp.MakeEncodingConfig() } -func (suite *GenTxTestSuite) setAccountBalance(cdc *codec.Codec, addr sdk.AccAddress, amount int64) json.RawMessage { +func (suite *GenTxTestSuite) setAccountBalance(addr sdk.AccAddress, amount int64) json.RawMessage { acc := suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, addr) suite.app.AccountKeeper.SetAccount(suite.ctx, acc) @@ -65,7 +66,7 @@ func (suite *GenTxTestSuite) setAccountBalance(cdc *codec.Codec, addr sdk.AccAdd suite.Require().NoError(err) bankGenesisState := suite.app.BankKeeper.ExportGenesis(suite.ctx) - bankGenesis, err := cdc.MarshalJSON(bankGenesisState) + bankGenesis, err := suite.encodingConfig.Amino.MarshalJSON(bankGenesisState) // TODO switch this to use Marshaler suite.Require().NoError(err) return bankGenesis @@ -162,7 +163,7 @@ func (suite *GenTxTestSuite) TestValidateAccountInGenesis() { "account without balance in the genesis state", func() { coins = sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 0)} - appGenesisState[banktypes.ModuleName] = suite.setAccountBalance(cdc, addr2, 50) + appGenesisState[banktypes.ModuleName] = suite.setAccountBalance(addr2, 50) }, false, }, @@ -170,7 +171,7 @@ func (suite *GenTxTestSuite) TestValidateAccountInGenesis() { "account without enough funds of default bond denom", func() { coins = sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 50)} - appGenesisState[banktypes.ModuleName] = suite.setAccountBalance(cdc, addr1, 25) + appGenesisState[banktypes.ModuleName] = suite.setAccountBalance(addr1, 25) }, false, }, @@ -178,7 +179,7 @@ func (suite *GenTxTestSuite) TestValidateAccountInGenesis() { "account with enough funds of default bond denom", func() { coins = sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 10)} - appGenesisState[banktypes.ModuleName] = suite.setAccountBalance(cdc, addr1, 25) + appGenesisState[banktypes.ModuleName] = suite.setAccountBalance(addr1, 25) }, true, }, @@ -186,12 +187,12 @@ func (suite *GenTxTestSuite) TestValidateAccountInGenesis() { for _, tc := range testCases { suite.Run(fmt.Sprintf("Case %s", tc.msg), func() { suite.SetupTest() - cdc = suite.app.Codec() + cdc = suite.encodingConfig.Amino suite.app.StakingKeeper.SetParams(suite.ctx, stakingtypes.DefaultParams()) stakingGenesisState := staking.ExportGenesis(suite.ctx, suite.app.StakingKeeper) suite.Require().Equal(stakingGenesisState.Params, stakingtypes.DefaultParams()) - stakingGenesis, err := cdc.MarshalJSON(stakingGenesisState) + stakingGenesis, err := cdc.MarshalJSON(stakingGenesisState) // TODO switch this to use Marshaler suite.Require().NoError(err) appGenesisState[stakingtypes.ModuleName] = stakingGenesis @@ -214,7 +215,6 @@ func (suite *GenTxTestSuite) TestValidateAccountInGenesis() { func (suite *GenTxTestSuite) TestDeliverGenTxs() { var ( genTxs []json.RawMessage - cdc *codec.Codec txBuilder = suite.encodingConfig.TxConfig.NewTxBuilder() ) @@ -230,23 +230,7 @@ func (suite *GenTxTestSuite) TestDeliverGenTxs() { suite.Require().NoError(err) genTxs = make([]json.RawMessage, 1) - tx, err := cdc.MarshalJSON(txBuilder.GetTx()) - suite.Require().NoError(err) - genTxs[0] = tx - }, - false, - }, - { - "unregistered message", - func() { - cdc.RegisterConcrete(testdata.TestMsg{}, "cosmos-sdk/Test", nil) - msg := testdata.NewTestMsg(sdk.AccAddress("some-address")) - - err := txBuilder.SetMsgs(msg) - suite.Require().NoError(err) - - genTxs = make([]json.RawMessage, 1) - tx, err := cdc.MarshalJSON(txBuilder.GetTx()) + tx, err := suite.encodingConfig.TxConfig.TxJSONEncoder()(txBuilder.GetTx()) suite.Require().NoError(err) genTxs[0] = tx }, @@ -255,8 +239,8 @@ func (suite *GenTxTestSuite) TestDeliverGenTxs() { { "success", func() { - _ = suite.setAccountBalance(cdc, addr1, 50) - _ = suite.setAccountBalance(cdc, addr2, 0) + _ = suite.setAccountBalance(addr1, 50) + _ = suite.setAccountBalance(addr2, 0) msg := banktypes.NewMsgSend(addr1, addr2, sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 1)}) tx, err := helpers.GenTx( @@ -272,7 +256,7 @@ func (suite *GenTxTestSuite) TestDeliverGenTxs() { suite.Require().NoError(err) genTxs = make([]json.RawMessage, 1) - genTx, err := cdc.MarshalJSON(tx) + genTx, err := suite.encodingConfig.TxConfig.TxJSONEncoder()(tx) suite.Require().NoError(err) genTxs[0] = genTx }, @@ -283,7 +267,6 @@ func (suite *GenTxTestSuite) TestDeliverGenTxs() { for _, tc := range testCases { suite.Run(fmt.Sprintf("Case %s", tc.msg), func() { suite.SetupTest() - cdc = suite.app.Codec() tc.malleate() @@ -291,14 +274,14 @@ func (suite *GenTxTestSuite) TestDeliverGenTxs() { suite.Require().NotPanics(func() { genutil.DeliverGenTxs( suite.ctx, genTxs, suite.app.StakingKeeper, suite.app.BaseApp.DeliverTx, - simapp.AminoJSONTxDecoder(suite.encodingConfig), suite.encodingConfig.TxConfig.TxEncoder(), + suite.encodingConfig.TxConfig.TxJSONDecoder(), suite.encodingConfig.TxConfig.TxEncoder(), ) }) } else { suite.Require().Panics(func() { genutil.DeliverGenTxs( suite.ctx, genTxs, suite.app.StakingKeeper, suite.app.BaseApp.DeliverTx, - simapp.AminoJSONTxDecoder(suite.encodingConfig), suite.encodingConfig.TxConfig.TxEncoder(), + suite.encodingConfig.TxConfig.TxJSONDecoder(), suite.encodingConfig.TxConfig.TxEncoder(), ) }) } From 4d5181be31fba542fd668eaf146eb78bbbb83c18 Mon Sep 17 00:00:00 2001 From: Aaron Craelius Date: Thu, 23 Jul 2020 21:09:32 -0400 Subject: [PATCH 11/24] Apply suggestions from code review Co-authored-by: Alexander Bezobchuk --- testutil/network/network.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/testutil/network/network.go b/testutil/network/network.go index 9f4b23f0cc39..cb81bfb66f27 100644 --- a/testutil/network/network.go +++ b/testutil/network/network.go @@ -281,9 +281,9 @@ func New(t *testing.T, cfg Config) *Network { memo := fmt.Sprintf("%s@%s:%s", nodeIDs[i], p2pURL.Hostname(), p2pURL.Port()) txBuilder := cfg.TxConfig.NewTxBuilder() - err = txBuilder.SetMsgs(createValMsg) - require.NoError(t, err) + require.NoError(t, txBuilder.SetMsgs(createValMsg)) txBuilder.SetMemo(memo) + txFactory := tx.Factory{} txFactory = txFactory. WithChainID(cfg.ChainID). From 0896defc8b2a2e8ed23d625e7d1803e9c134f5e1 Mon Sep 17 00:00:00 2001 From: Aaron Craelius Date: Thu, 23 Jul 2020 21:31:18 -0400 Subject: [PATCH 12/24] Fixes --- simapp/params/proto.go | 2 +- testutil/network/util.go | 2 +- x/auth/tx/builder.go | 19 +++++++++++++------ x/auth/tx/decoder.go | 12 +++++++----- x/auth/tx/encoder.go | 10 ++++++---- x/auth/tx/generator.go | 16 +++++++--------- x/genutil/collect.go | 8 ++++---- 7 files changed, 39 insertions(+), 30 deletions(-) diff --git a/simapp/params/proto.go b/simapp/params/proto.go index 012361d1dc51..a5c859ada421 100644 --- a/simapp/params/proto.go +++ b/simapp/params/proto.go @@ -15,7 +15,7 @@ func MakeEncodingConfig() EncodingConfig { cdc := codec.New() interfaceRegistry := types.NewInterfaceRegistry() marshaler := codec.NewHybridCodec(cdc, interfaceRegistry) - txGen := tx.NewTxConfig(marshaler, std.DefaultPublicKeyCodec{}, tx.DefaultSignModeHandler()) + txGen := tx.NewTxConfig(interfaceRegistry, std.DefaultPublicKeyCodec{}, tx.DefaultSignModeHandler()) return EncodingConfig{ InterfaceRegistry: interfaceRegistry, diff --git a/testutil/network/util.go b/testutil/network/util.go index cb8262c8a77a..ab037cd598df 100644 --- a/testutil/network/util.go +++ b/testutil/network/util.go @@ -105,7 +105,7 @@ func collectGenFiles(cfg Config, vals []*Validator, outputDir string) error { return err } - appState, err := genutil.GenAppStateFromConfig(cfg.Codec, tmCfg, initCfg, *genDoc, banktypes.GenesisBalancesIterator{}) + appState, err := genutil.GenAppStateFromConfig(cfg.Codec, cfg.TxConfig.TxJSONDecoder(), tmCfg, initCfg, *genDoc, banktypes.GenesisBalancesIterator{}) if err != nil { return err } diff --git a/x/auth/tx/builder.go b/x/auth/tx/builder.go index 7df625aa7515..ebb9cfa75180 100644 --- a/x/auth/tx/builder.go +++ b/x/auth/tx/builder.go @@ -3,6 +3,8 @@ package tx import ( "fmt" + "github.com/golang/protobuf/proto" + "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/x/auth/signing/direct" @@ -16,7 +18,6 @@ import ( "github.com/tendermint/tendermint/crypto" - "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/crypto/types" sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" @@ -37,7 +38,6 @@ type builder struct { // or decoded from AuthInfo when GetPubKey's was called pubKeys []crypto.PubKey - marshaler codec.Marshaler pubkeyCodec types.PublicKeyCodec } @@ -47,7 +47,7 @@ var ( _ direct.ProtoTx = &builder{} ) -func newBuilder(marshaler codec.Marshaler, pubkeyCodec types.PublicKeyCodec) *builder { +func newBuilder(pubkeyCodec types.PublicKeyCodec) *builder { return &builder{ tx: &tx.Tx{ Body: &tx.TxBody{}, @@ -55,7 +55,6 @@ func newBuilder(marshaler codec.Marshaler, pubkeyCodec types.PublicKeyCodec) *bu Fee: &tx.Fee{}, }, }, - marshaler: marshaler, pubkeyCodec: pubkeyCodec, } } @@ -131,7 +130,11 @@ func (t *builder) GetBodyBytes() []byte { // this method should always return the correct bytes. Note that after // decoding bodyBz is derived from TxRaw so that it matches what was // transmitted over the wire - t.bodyBz = t.marshaler.MustMarshalBinaryBare(t.tx.Body) + var err error + t.bodyBz, err = proto.Marshal(t.tx.Body) + if err != nil { + panic(err) + } } return t.bodyBz } @@ -143,7 +146,11 @@ func (t *builder) GetAuthInfoBytes() []byte { // this method should always return the correct bytes. Note that after // decoding authInfoBz is derived from TxRaw so that it matches what was // transmitted over the wire - t.authInfoBz = t.marshaler.MustMarshalBinaryBare(t.tx.AuthInfo) + var err error + t.authInfoBz, err = proto.Marshal(t.tx.AuthInfo) + if err != nil { + panic(err) + } } return t.authInfoBz } diff --git a/x/auth/tx/decoder.go b/x/auth/tx/decoder.go index 8ce3b06fa66e..e229c1f35b85 100644 --- a/x/auth/tx/decoder.go +++ b/x/auth/tx/decoder.go @@ -3,15 +3,18 @@ package tx import ( "github.com/tendermint/tendermint/crypto" + "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/codec/types" + "github.com/cosmos/cosmos-sdk/types/tx" - "github.com/cosmos/cosmos-sdk/codec" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" sdk "github.com/cosmos/cosmos-sdk/types" ) // DefaultTxDecoder returns a default protobuf TxDecoder using the provided Marshaler and PublicKeyCodec -func DefaultTxDecoder(cdc codec.Marshaler, keyCodec cryptotypes.PublicKeyCodec) sdk.TxDecoder { +func DefaultTxDecoder(anyUnpacker types.AnyUnpacker, keyCodec cryptotypes.PublicKeyCodec) sdk.TxDecoder { + cdc := codec.NewProtoCodec(anyUnpacker) return func(txBytes []byte) (sdk.Tx, error) { var raw tx.TxRaw err := cdc.UnmarshalBinaryBare(txBytes, &raw) @@ -35,14 +38,14 @@ func DefaultTxDecoder(cdc codec.Marshaler, keyCodec cryptotypes.PublicKeyCodec) bodyBz: raw.BodyBytes, authInfoBz: raw.AuthInfoBytes, pubKeys: pks, - marshaler: cdc, pubkeyCodec: keyCodec, }, nil } } // DefaultTxDecoder returns a default protobuf JSON TxDecoder using the provided Marshaler and PublicKeyCodec -func DefaultJSONTxDecoder(cdc codec.Marshaler, keyCodec cryptotypes.PublicKeyCodec) sdk.TxDecoder { +func DefaultJSONTxDecoder(anyUnpacker types.AnyUnpacker, keyCodec cryptotypes.PublicKeyCodec) sdk.TxDecoder { + cdc := codec.NewProtoCodec(anyUnpacker) return func(txBytes []byte) (sdk.Tx, error) { var theTx tx.Tx err := cdc.UnmarshalJSON(txBytes, &theTx) @@ -58,7 +61,6 @@ func DefaultJSONTxDecoder(cdc codec.Marshaler, keyCodec cryptotypes.PublicKeyCod return &builder{ tx: &theTx, pubKeys: pks, - marshaler: cdc, pubkeyCodec: keyCodec, }, nil } diff --git a/x/auth/tx/encoder.go b/x/auth/tx/encoder.go index 3c39a20a61fd..5884b756ffdc 100644 --- a/x/auth/tx/encoder.go +++ b/x/auth/tx/encoder.go @@ -3,13 +3,15 @@ package tx import ( "fmt" + "github.com/golang/protobuf/proto" + "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/types" txtypes "github.com/cosmos/cosmos-sdk/types/tx" ) // DefaultTxEncoder returns a default protobuf TxEncoder using the provided Marshaler -func DefaultTxEncoder(marshaler codec.Marshaler) types.TxEncoder { +func DefaultTxEncoder() types.TxEncoder { return func(tx types.Tx) ([]byte, error) { wrapper, ok := tx.(*builder) if !ok { @@ -22,18 +24,18 @@ func DefaultTxEncoder(marshaler codec.Marshaler) types.TxEncoder { Signatures: wrapper.tx.Signatures, } - return marshaler.MarshalBinaryBare(raw) + return proto.Marshal(raw) } } // DefaultTxEncoder returns a default protobuf JSON TxEncoder using the provided Marshaler -func DefaultJSONTxEncoder(marshaler codec.Marshaler) types.TxEncoder { +func DefaultJSONTxEncoder() types.TxEncoder { return func(tx types.Tx) ([]byte, error) { wrapper, ok := tx.(*builder) if !ok { return nil, fmt.Errorf("expected %T, got %T", &builder{}, tx) } - return marshaler.MarshalJSON(wrapper.tx) + return codec.ProtoMarshalJSON(wrapper.tx) } } diff --git a/x/auth/tx/generator.go b/x/auth/tx/generator.go index 3d6bb529ee89..c7b09fad905a 100644 --- a/x/auth/tx/generator.go +++ b/x/auth/tx/generator.go @@ -2,14 +2,13 @@ package tx import ( "github.com/cosmos/cosmos-sdk/client" - "github.com/cosmos/cosmos-sdk/codec" + codectypes "github.com/cosmos/cosmos-sdk/codec/types" "github.com/cosmos/cosmos-sdk/crypto/types" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth/signing" ) type generator struct { - marshaler codec.Marshaler pubkeyCodec types.PublicKeyCodec handler signing.SignModeHandler decoder sdk.TxDecoder @@ -19,20 +18,19 @@ type generator struct { } // NewTxConfig returns a new protobuf TxConfig using the provided Marshaler, PublicKeyCodec and SignModeHandler. -func NewTxConfig(marshaler codec.Marshaler, pubkeyCodec types.PublicKeyCodec, signModeHandler signing.SignModeHandler) client.TxConfig { +func NewTxConfig(anyUnpacker codectypes.AnyUnpacker, pubkeyCodec types.PublicKeyCodec, signModeHandler signing.SignModeHandler) client.TxConfig { return &generator{ - marshaler: marshaler, pubkeyCodec: pubkeyCodec, handler: signModeHandler, - decoder: DefaultTxDecoder(marshaler, pubkeyCodec), - encoder: DefaultTxEncoder(marshaler), - jsonDecoder: DefaultJSONTxDecoder(marshaler, pubkeyCodec), - jsonEncoder: DefaultJSONTxEncoder(marshaler), + decoder: DefaultTxDecoder(anyUnpacker, pubkeyCodec), + encoder: DefaultTxEncoder(), + jsonDecoder: DefaultJSONTxDecoder(anyUnpacker, pubkeyCodec), + jsonEncoder: DefaultJSONTxEncoder(), } } func (g generator) NewTxBuilder() client.TxBuilder { - return newBuilder(g.marshaler, g.pubkeyCodec) + return newBuilder(g.pubkeyCodec) } func (g generator) SignModeHandler() signing.SignModeHandler { diff --git a/x/genutil/collect.go b/x/genutil/collect.go index 43fdece54017..3679d5fbed9e 100644 --- a/x/genutil/collect.go +++ b/x/genutil/collect.go @@ -23,13 +23,13 @@ import ( ) // GenAppStateFromConfig gets the genesis app state from the config -func GenAppStateFromConfig(cdc codec.JSONMarshaler, config *cfg.Config, +func GenAppStateFromConfig(cdc codec.JSONMarshaler, txJsonDecoder sdk.TxDecoder, config *cfg.Config, initCfg types.InitConfig, genDoc tmtypes.GenesisDoc, genBalIterator types.GenesisBalancesIterator, ) (appState json.RawMessage, err error) { // process genesis transactions, else create default genesis.json appGenTxs, persistentPeers, err := CollectTxs( - cdc, config.Moniker, initCfg.GenTxsDir, genDoc, genBalIterator, + cdc, txJsonDecoder, config.Moniker, initCfg.GenTxsDir, genDoc, genBalIterator, ) if err != nil { return appState, err @@ -67,7 +67,7 @@ func GenAppStateFromConfig(cdc codec.JSONMarshaler, config *cfg.Config, // CollectTxs processes and validates application's genesis Txs and returns // the list of appGenTxs, and persistent peers required to generate genesis.json. -func CollectTxs(cdc codec.JSONMarshaler, moniker, genTxsDir string, +func CollectTxs(cdc codec.JSONMarshaler, txJsonDecoder sdk.TxDecoder, moniker, genTxsDir string, genDoc tmtypes.GenesisDoc, genBalIterator types.GenesisBalancesIterator, ) (appGenTxs []sdk.Tx, persistentPeers string, err error) { @@ -111,7 +111,7 @@ func CollectTxs(cdc codec.JSONMarshaler, moniker, genTxsDir string, } var genTx sdk.Tx - if err = cdc.UnmarshalJSON(jsonRawTx, &genTx); err != nil { + if genTx, err = txJsonDecoder(jsonRawTx); err != nil { return appGenTxs, persistentPeers, err } From 391dcdcd7b25a09183fa84d5ae5e89c3e666ca94 Mon Sep 17 00:00:00 2001 From: Aaron Craelius Date: Thu, 23 Jul 2020 22:08:44 -0400 Subject: [PATCH 13/24] Fixes --- simapp/simd/cmd/testnet.go | 9 +++++---- x/auth/tx/builder_test.go | 5 ++--- x/genutil/client/cli/collect.go | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/simapp/simd/cmd/testnet.go b/simapp/simd/cmd/testnet.go index f7b8cf4ef946..67001442e3c8 100644 --- a/simapp/simd/cmd/testnet.go +++ b/simapp/simd/cmd/testnet.go @@ -74,7 +74,7 @@ Example: numValidators, _ := cmd.Flags().GetInt(flagNumValidators) return InitTestnet( - cmd, config, cdc, mbm, genBalIterator, outputDir, chainID, minGasPrices, + cmd, config, cdc, clientCtx.TxConfig.TxJSONDecoder(), mbm, genBalIterator, outputDir, chainID, minGasPrices, nodeDirPrefix, nodeDaemonHome, nodeCLIHome, startingIPAddress, keyringBackend, numValidators, ) }, @@ -98,6 +98,7 @@ const nodeDirPerm = 0755 // Initialize the testnet func InitTestnet( cmd *cobra.Command, config *tmconfig.Config, cdc codec.JSONMarshaler, + txJsonDecoder sdk.TxDecoder, mbm module.BasicManager, genBalIterator banktypes.GenesisBalancesIterator, outputDir, chainID, minGasPrices, nodeDirPrefix, nodeDaemonHome, nodeCLIHome, startingIPAddress, keyringBackend string, numValidators int, @@ -240,7 +241,7 @@ func InitTestnet( } err := collectGenFiles( - cdc, config, chainID, nodeIDs, valPubKeys, numValidators, + cdc, txJsonDecoder, config, chainID, nodeIDs, valPubKeys, numValidators, outputDir, nodeDirPrefix, nodeDaemonHome, genBalIterator, ) if err != nil { @@ -294,7 +295,7 @@ func initGenFiles( } func collectGenFiles( - cdc codec.JSONMarshaler, config *tmconfig.Config, chainID string, + cdc codec.JSONMarshaler, txJsonDecoder sdk.TxDecoder, config *tmconfig.Config, chainID string, nodeIDs []string, valPubKeys []crypto.PubKey, numValidators int, outputDir, nodeDirPrefix, nodeDaemonHome string, genBalIterator banktypes.GenesisBalancesIterator, @@ -319,7 +320,7 @@ func collectGenFiles( return err } - nodeAppState, err := genutil.GenAppStateFromConfig(cdc, config, initCfg, *genDoc, genBalIterator) + nodeAppState, err := genutil.GenAppStateFromConfig(cdc, txJsonDecoder, config, initCfg, *genDoc, genBalIterator) if err != nil { return err } diff --git a/x/auth/tx/builder_test.go b/x/auth/tx/builder_test.go index 136078fa762d..c115f474ac83 100644 --- a/x/auth/tx/builder_test.go +++ b/x/auth/tx/builder_test.go @@ -22,7 +22,7 @@ func TestTxBuilder(t *testing.T) { _, pubkey, addr := testdata.KeyTestPubAddr() marshaler := codec.NewHybridCodec(codec.New(), codectypes.NewInterfaceRegistry()) - tx := newBuilder(marshaler, std.DefaultPublicKeyCodec{}) + tx := newBuilder(std.DefaultPublicKeyCodec{}) cdc := std.DefaultPublicKeyCodec{} @@ -125,8 +125,7 @@ func TestBuilderValidateBasic(t *testing.T) { // require to fail validation upon invalid fee badFeeAmount := testdata.NewTestFeeAmount() badFeeAmount[0].Amount = sdk.NewInt(-5) - marshaler := codec.NewHybridCodec(codec.New(), codectypes.NewInterfaceRegistry()) - bldr := newBuilder(marshaler, std.DefaultPublicKeyCodec{}) + bldr := newBuilder(std.DefaultPublicKeyCodec{}) var sig1, sig2 signing.SignatureV2 sig1 = signing.SignatureV2{ diff --git a/x/genutil/client/cli/collect.go b/x/genutil/client/cli/collect.go index 912f994a6d11..b9d2eedd4077 100644 --- a/x/genutil/client/cli/collect.go +++ b/x/genutil/client/cli/collect.go @@ -50,7 +50,7 @@ func CollectGenTxsCmd(genBalIterator types.GenesisBalancesIterator, defaultNodeH toPrint := newPrintInfo(config.Moniker, genDoc.ChainID, nodeID, genTxsDir, json.RawMessage("")) initCfg := types.NewInitConfig(genDoc.ChainID, genTxsDir, nodeID, valPubKey) - appMessage, err := genutil.GenAppStateFromConfig(cdc, config, initCfg, *genDoc, genBalIterator) + appMessage, err := genutil.GenAppStateFromConfig(cdc, clientCtx.TxConfig.TxJSONDecoder(), config, initCfg, *genDoc, genBalIterator) if err != nil { return errors.Wrap(err, "failed to get genesis app state from config") } From 9b77a01052795c8abd419ef5648ec91ef16e332d Mon Sep 17 00:00:00 2001 From: Aaron Craelius Date: Thu, 23 Jul 2020 22:15:08 -0400 Subject: [PATCH 14/24] Fixes --- simapp/simd/cmd/testnet.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/simapp/simd/cmd/testnet.go b/simapp/simd/cmd/testnet.go index 67001442e3c8..7a9fca1a43d0 100644 --- a/simapp/simd/cmd/testnet.go +++ b/simapp/simd/cmd/testnet.go @@ -73,8 +73,10 @@ Example: startingIPAddress, _ := cmd.Flags().GetString(flagStartingIPAddress) numValidators, _ := cmd.Flags().GetInt(flagNumValidators) + txJsonDecoder := clientCtx.TxConfig.TxJSONDecoder() + return InitTestnet( - cmd, config, cdc, clientCtx.TxConfig.TxJSONDecoder(), mbm, genBalIterator, outputDir, chainID, minGasPrices, + cmd, config, cdc, txJsonDecoder, mbm, genBalIterator, outputDir, chainID, minGasPrices, nodeDirPrefix, nodeDaemonHome, nodeCLIHome, startingIPAddress, keyringBackend, numValidators, ) }, From 8545cf38124e33eab6e5725f32743e851199c69a Mon Sep 17 00:00:00 2001 From: Aaron Craelius Date: Thu, 23 Jul 2020 23:20:00 -0400 Subject: [PATCH 15/24] Fixes --- simapp/simd/cmd/testnet.go | 12 +++++++----- testutil/network/util.go | 3 ++- x/genutil/client/cli/collect.go | 4 +++- x/genutil/collect.go | 6 +++--- x/genutil/gentx.go | 6 ++---- x/genutil/gentx_test.go | 10 ++++------ 6 files changed, 21 insertions(+), 20 deletions(-) diff --git a/simapp/simd/cmd/testnet.go b/simapp/simd/cmd/testnet.go index 7a9fca1a43d0..fc2bfb8adc15 100644 --- a/simapp/simd/cmd/testnet.go +++ b/simapp/simd/cmd/testnet.go @@ -74,9 +74,10 @@ Example: numValidators, _ := cmd.Flags().GetInt(flagNumValidators) txJsonDecoder := clientCtx.TxConfig.TxJSONDecoder() + txJsonEncoder := clientCtx.TxConfig.TxJSONEncoder() return InitTestnet( - cmd, config, cdc, txJsonDecoder, mbm, genBalIterator, outputDir, chainID, minGasPrices, + cmd, config, cdc, txJsonDecoder, txJsonEncoder, mbm, genBalIterator, outputDir, chainID, minGasPrices, nodeDirPrefix, nodeDaemonHome, nodeCLIHome, startingIPAddress, keyringBackend, numValidators, ) }, @@ -100,7 +101,7 @@ const nodeDirPerm = 0755 // Initialize the testnet func InitTestnet( cmd *cobra.Command, config *tmconfig.Config, cdc codec.JSONMarshaler, - txJsonDecoder sdk.TxDecoder, + txJsonDecoder sdk.TxDecoder, txJsonEncoder sdk.TxEncoder, mbm module.BasicManager, genBalIterator banktypes.GenesisBalancesIterator, outputDir, chainID, minGasPrices, nodeDirPrefix, nodeDaemonHome, nodeCLIHome, startingIPAddress, keyringBackend string, numValidators int, @@ -243,7 +244,7 @@ func InitTestnet( } err := collectGenFiles( - cdc, txJsonDecoder, config, chainID, nodeIDs, valPubKeys, numValidators, + cdc, txJsonDecoder, txJsonEncoder, config, chainID, nodeIDs, valPubKeys, numValidators, outputDir, nodeDirPrefix, nodeDaemonHome, genBalIterator, ) if err != nil { @@ -297,7 +298,8 @@ func initGenFiles( } func collectGenFiles( - cdc codec.JSONMarshaler, txJsonDecoder sdk.TxDecoder, config *tmconfig.Config, chainID string, + cdc codec.JSONMarshaler, txJsonDecoder sdk.TxDecoder, txJsonEncoder sdk.TxEncoder, + config *tmconfig.Config, chainID string, nodeIDs []string, valPubKeys []crypto.PubKey, numValidators int, outputDir, nodeDirPrefix, nodeDaemonHome string, genBalIterator banktypes.GenesisBalancesIterator, @@ -322,7 +324,7 @@ func collectGenFiles( return err } - nodeAppState, err := genutil.GenAppStateFromConfig(cdc, txJsonDecoder, config, initCfg, *genDoc, genBalIterator) + nodeAppState, err := genutil.GenAppStateFromConfig(cdc, txJsonDecoder, txJsonEncoder, config, initCfg, *genDoc, genBalIterator) if err != nil { return err } diff --git a/testutil/network/util.go b/testutil/network/util.go index ab037cd598df..c6d751984bf4 100644 --- a/testutil/network/util.go +++ b/testutil/network/util.go @@ -105,7 +105,8 @@ func collectGenFiles(cfg Config, vals []*Validator, outputDir string) error { return err } - appState, err := genutil.GenAppStateFromConfig(cfg.Codec, cfg.TxConfig.TxJSONDecoder(), tmCfg, initCfg, *genDoc, banktypes.GenesisBalancesIterator{}) + appState, err := genutil.GenAppStateFromConfig(cfg.Codec, cfg.TxConfig.TxJSONDecoder(), cfg.TxConfig.TxJSONEncoder(), + tmCfg, initCfg, *genDoc, banktypes.GenesisBalancesIterator{}) if err != nil { return err } diff --git a/x/genutil/client/cli/collect.go b/x/genutil/client/cli/collect.go index b9d2eedd4077..6e136c96dd57 100644 --- a/x/genutil/client/cli/collect.go +++ b/x/genutil/client/cli/collect.go @@ -50,7 +50,9 @@ func CollectGenTxsCmd(genBalIterator types.GenesisBalancesIterator, defaultNodeH toPrint := newPrintInfo(config.Moniker, genDoc.ChainID, nodeID, genTxsDir, json.RawMessage("")) initCfg := types.NewInitConfig(genDoc.ChainID, genTxsDir, nodeID, valPubKey) - appMessage, err := genutil.GenAppStateFromConfig(cdc, clientCtx.TxConfig.TxJSONDecoder(), config, initCfg, *genDoc, genBalIterator) + appMessage, err := genutil.GenAppStateFromConfig(cdc, + clientCtx.TxConfig.TxJSONDecoder(), clientCtx.TxConfig.TxJSONEncoder(), + config, initCfg, *genDoc, genBalIterator) if err != nil { return errors.Wrap(err, "failed to get genesis app state from config") } diff --git a/x/genutil/collect.go b/x/genutil/collect.go index 3679d5fbed9e..d43ad07be3ed 100644 --- a/x/genutil/collect.go +++ b/x/genutil/collect.go @@ -23,8 +23,8 @@ import ( ) // GenAppStateFromConfig gets the genesis app state from the config -func GenAppStateFromConfig(cdc codec.JSONMarshaler, txJsonDecoder sdk.TxDecoder, config *cfg.Config, - initCfg types.InitConfig, genDoc tmtypes.GenesisDoc, genBalIterator types.GenesisBalancesIterator, +func GenAppStateFromConfig(cdc codec.JSONMarshaler, txJsonDecoder sdk.TxDecoder, txJsonEncoder sdk.TxEncoder, + config *cfg.Config, initCfg types.InitConfig, genDoc tmtypes.GenesisDoc, genBalIterator types.GenesisBalancesIterator, ) (appState json.RawMessage, err error) { // process genesis transactions, else create default genesis.json @@ -49,7 +49,7 @@ func GenAppStateFromConfig(cdc codec.JSONMarshaler, txJsonDecoder sdk.TxDecoder, return appState, err } - appGenesisState, err = SetGenTxsInAppGenesisState(cdc, appGenesisState, appGenTxs) + appGenesisState, err = SetGenTxsInAppGenesisState(cdc, txJsonEncoder, appGenesisState, appGenTxs) if err != nil { return appState, err } diff --git a/x/genutil/gentx.go b/x/genutil/gentx.go index 6b3167de52f9..40c7ab3640a0 100644 --- a/x/genutil/gentx.go +++ b/x/genutil/gentx.go @@ -1,7 +1,5 @@ package genutil -// DONTCOVER - import ( "encoding/json" "fmt" @@ -17,14 +15,14 @@ import ( // SetGenTxsInAppGenesisState - sets the genesis transactions in the app genesis state func SetGenTxsInAppGenesisState( - cdc codec.JSONMarshaler, appGenesisState map[string]json.RawMessage, genTxs []sdk.Tx, + cdc codec.JSONMarshaler, txJsonEncoder sdk.TxEncoder, appGenesisState map[string]json.RawMessage, genTxs []sdk.Tx, ) (map[string]json.RawMessage, error) { genesisState := types.GetGenesisStateFromAppState(cdc, appGenesisState) genTxsBz := make([]json.RawMessage, 0, len(genTxs)) for _, genTx := range genTxs { - txBz, err := cdc.MarshalJSON(genTx) + txBz, err := txJsonEncoder(genTx) if err != nil { return appGenesisState, err } diff --git a/x/genutil/gentx_test.go b/x/genutil/gentx_test.go index e96489e8f8e5..637586cb5954 100644 --- a/x/genutil/gentx_test.go +++ b/x/genutil/gentx_test.go @@ -5,7 +5,6 @@ import ( "fmt" "testing" - "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/simapp" "github.com/cosmos/cosmos-sdk/simapp/helpers" simappparams "github.com/cosmos/cosmos-sdk/simapp/params" @@ -75,7 +74,6 @@ func (suite *GenTxTestSuite) setAccountBalance(addr sdk.AccAddress, amount int64 func (suite *GenTxTestSuite) TestSetGenTxsInAppGenesisState() { var ( txBuilder = suite.encodingConfig.TxConfig.NewTxBuilder() - cdc *codec.Codec genTxs []sdk.Tx ) @@ -120,10 +118,11 @@ func (suite *GenTxTestSuite) TestSetGenTxsInAppGenesisState() { for _, tc := range testCases { suite.Run(fmt.Sprintf("Case %s", tc.msg), func() { suite.SetupTest() - cdc = suite.app.Codec() + cdc := suite.encodingConfig.Marshaler + txJsonEncoder := suite.encodingConfig.TxConfig.TxJSONEncoder() tc.malleate() - appGenesisState, err := genutil.SetGenTxsInAppGenesisState(cdc, make(map[string]json.RawMessage), genTxs) + appGenesisState, err := genutil.SetGenTxsInAppGenesisState(cdc, txJsonEncoder, make(map[string]json.RawMessage), genTxs) if tc.expPass { suite.Require().NoError(err) @@ -143,7 +142,6 @@ func (suite *GenTxTestSuite) TestSetGenTxsInAppGenesisState() { func (suite *GenTxTestSuite) TestValidateAccountInGenesis() { var ( appGenesisState = make(map[string]json.RawMessage) - cdc *codec.Codec coins sdk.Coins ) @@ -187,7 +185,7 @@ func (suite *GenTxTestSuite) TestValidateAccountInGenesis() { for _, tc := range testCases { suite.Run(fmt.Sprintf("Case %s", tc.msg), func() { suite.SetupTest() - cdc = suite.encodingConfig.Amino + cdc := suite.encodingConfig.Marshaler suite.app.StakingKeeper.SetParams(suite.ctx, stakingtypes.DefaultParams()) stakingGenesisState := staking.ExportGenesis(suite.ctx, suite.app.StakingKeeper) From 0ac6d28ffdd10bf5b3f85f87dfc0671373468214 Mon Sep 17 00:00:00 2001 From: Aaron Craelius Date: Thu, 23 Jul 2020 23:24:30 -0400 Subject: [PATCH 16/24] Remove unneeded test case (doesn't apply to proto marshaling) --- x/genutil/gentx_test.go | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/x/genutil/gentx_test.go b/x/genutil/gentx_test.go index 637586cb5954..bc7625c3d54f 100644 --- a/x/genutil/gentx_test.go +++ b/x/genutil/gentx_test.go @@ -8,7 +8,6 @@ import ( "github.com/cosmos/cosmos-sdk/simapp" "github.com/cosmos/cosmos-sdk/simapp/helpers" simappparams "github.com/cosmos/cosmos-sdk/simapp/params" - "github.com/cosmos/cosmos-sdk/testutil/testdata" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/stretchr/testify/suite" @@ -82,17 +81,6 @@ func (suite *GenTxTestSuite) TestSetGenTxsInAppGenesisState() { malleate func() expPass bool }{ - { - "unregistered message", - func() { - msg := testdata.NewTestMsg(sdk.AccAddress("some-address")) - err := txBuilder.SetMsgs(msg) - suite.Require().NoError(err) - tx := txBuilder.GetTx() - genTxs = []sdk.Tx{tx} - }, - false, - }, { "one genesis transaction", func() { From b2dd715f86240248e82e89f327bdd132d4cd2fd1 Mon Sep 17 00:00:00 2001 From: blushi Date: Fri, 24 Jul 2020 14:22:17 +0200 Subject: [PATCH 17/24] linting --- simapp/simd/cmd/testnet.go | 14 +++++++------- x/genutil/collect.go | 10 +++++----- x/genutil/gentx.go | 4 ++-- x/genutil/gentx_test.go | 4 ++-- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/simapp/simd/cmd/testnet.go b/simapp/simd/cmd/testnet.go index fc2bfb8adc15..297f6a50b8d4 100644 --- a/simapp/simd/cmd/testnet.go +++ b/simapp/simd/cmd/testnet.go @@ -73,11 +73,11 @@ Example: startingIPAddress, _ := cmd.Flags().GetString(flagStartingIPAddress) numValidators, _ := cmd.Flags().GetInt(flagNumValidators) - txJsonDecoder := clientCtx.TxConfig.TxJSONDecoder() - txJsonEncoder := clientCtx.TxConfig.TxJSONEncoder() + txJSONDecoder := clientCtx.TxConfig.TxJSONDecoder() + txJSONEncoder := clientCtx.TxConfig.TxJSONEncoder() return InitTestnet( - cmd, config, cdc, txJsonDecoder, txJsonEncoder, mbm, genBalIterator, outputDir, chainID, minGasPrices, + cmd, config, cdc, txJSONDecoder, txJSONEncoder, mbm, genBalIterator, outputDir, chainID, minGasPrices, nodeDirPrefix, nodeDaemonHome, nodeCLIHome, startingIPAddress, keyringBackend, numValidators, ) }, @@ -101,7 +101,7 @@ const nodeDirPerm = 0755 // Initialize the testnet func InitTestnet( cmd *cobra.Command, config *tmconfig.Config, cdc codec.JSONMarshaler, - txJsonDecoder sdk.TxDecoder, txJsonEncoder sdk.TxEncoder, + txJSONDecoder sdk.TxDecoder, txJSONEncoder sdk.TxEncoder, mbm module.BasicManager, genBalIterator banktypes.GenesisBalancesIterator, outputDir, chainID, minGasPrices, nodeDirPrefix, nodeDaemonHome, nodeCLIHome, startingIPAddress, keyringBackend string, numValidators int, @@ -244,7 +244,7 @@ func InitTestnet( } err := collectGenFiles( - cdc, txJsonDecoder, txJsonEncoder, config, chainID, nodeIDs, valPubKeys, numValidators, + cdc, txJSONDecoder, txJSONEncoder, config, chainID, nodeIDs, valPubKeys, numValidators, outputDir, nodeDirPrefix, nodeDaemonHome, genBalIterator, ) if err != nil { @@ -298,7 +298,7 @@ func initGenFiles( } func collectGenFiles( - cdc codec.JSONMarshaler, txJsonDecoder sdk.TxDecoder, txJsonEncoder sdk.TxEncoder, + cdc codec.JSONMarshaler, txJSONDecoder sdk.TxDecoder, txJSONEncoder sdk.TxEncoder, config *tmconfig.Config, chainID string, nodeIDs []string, valPubKeys []crypto.PubKey, numValidators int, outputDir, nodeDirPrefix, nodeDaemonHome string, @@ -324,7 +324,7 @@ func collectGenFiles( return err } - nodeAppState, err := genutil.GenAppStateFromConfig(cdc, txJsonDecoder, txJsonEncoder, config, initCfg, *genDoc, genBalIterator) + nodeAppState, err := genutil.GenAppStateFromConfig(cdc, txJSONDecoder, txJSONEncoder, config, initCfg, *genDoc, genBalIterator) if err != nil { return err } diff --git a/x/genutil/collect.go b/x/genutil/collect.go index d43ad07be3ed..d0542ecccb6e 100644 --- a/x/genutil/collect.go +++ b/x/genutil/collect.go @@ -23,13 +23,13 @@ import ( ) // GenAppStateFromConfig gets the genesis app state from the config -func GenAppStateFromConfig(cdc codec.JSONMarshaler, txJsonDecoder sdk.TxDecoder, txJsonEncoder sdk.TxEncoder, +func GenAppStateFromConfig(cdc codec.JSONMarshaler, txJSONDecoder sdk.TxDecoder, txJSONEncoder sdk.TxEncoder, config *cfg.Config, initCfg types.InitConfig, genDoc tmtypes.GenesisDoc, genBalIterator types.GenesisBalancesIterator, ) (appState json.RawMessage, err error) { // process genesis transactions, else create default genesis.json appGenTxs, persistentPeers, err := CollectTxs( - cdc, txJsonDecoder, config.Moniker, initCfg.GenTxsDir, genDoc, genBalIterator, + cdc, txJSONDecoder, config.Moniker, initCfg.GenTxsDir, genDoc, genBalIterator, ) if err != nil { return appState, err @@ -49,7 +49,7 @@ func GenAppStateFromConfig(cdc codec.JSONMarshaler, txJsonDecoder sdk.TxDecoder, return appState, err } - appGenesisState, err = SetGenTxsInAppGenesisState(cdc, txJsonEncoder, appGenesisState, appGenTxs) + appGenesisState, err = SetGenTxsInAppGenesisState(cdc, txJSONEncoder, appGenesisState, appGenTxs) if err != nil { return appState, err } @@ -67,7 +67,7 @@ func GenAppStateFromConfig(cdc codec.JSONMarshaler, txJsonDecoder sdk.TxDecoder, // CollectTxs processes and validates application's genesis Txs and returns // the list of appGenTxs, and persistent peers required to generate genesis.json. -func CollectTxs(cdc codec.JSONMarshaler, txJsonDecoder sdk.TxDecoder, moniker, genTxsDir string, +func CollectTxs(cdc codec.JSONMarshaler, txJSONDecoder sdk.TxDecoder, moniker, genTxsDir string, genDoc tmtypes.GenesisDoc, genBalIterator types.GenesisBalancesIterator, ) (appGenTxs []sdk.Tx, persistentPeers string, err error) { @@ -111,7 +111,7 @@ func CollectTxs(cdc codec.JSONMarshaler, txJsonDecoder sdk.TxDecoder, moniker, g } var genTx sdk.Tx - if genTx, err = txJsonDecoder(jsonRawTx); err != nil { + if genTx, err = txJSONDecoder(jsonRawTx); err != nil { return appGenTxs, persistentPeers, err } diff --git a/x/genutil/gentx.go b/x/genutil/gentx.go index 40c7ab3640a0..32e0671cb1df 100644 --- a/x/genutil/gentx.go +++ b/x/genutil/gentx.go @@ -15,14 +15,14 @@ import ( // SetGenTxsInAppGenesisState - sets the genesis transactions in the app genesis state func SetGenTxsInAppGenesisState( - cdc codec.JSONMarshaler, txJsonEncoder sdk.TxEncoder, appGenesisState map[string]json.RawMessage, genTxs []sdk.Tx, + cdc codec.JSONMarshaler, txJSONEncoder sdk.TxEncoder, appGenesisState map[string]json.RawMessage, genTxs []sdk.Tx, ) (map[string]json.RawMessage, error) { genesisState := types.GetGenesisStateFromAppState(cdc, appGenesisState) genTxsBz := make([]json.RawMessage, 0, len(genTxs)) for _, genTx := range genTxs { - txBz, err := txJsonEncoder(genTx) + txBz, err := txJSONEncoder(genTx) if err != nil { return appGenesisState, err } diff --git a/x/genutil/gentx_test.go b/x/genutil/gentx_test.go index bc7625c3d54f..3551ddaeee6b 100644 --- a/x/genutil/gentx_test.go +++ b/x/genutil/gentx_test.go @@ -107,10 +107,10 @@ func (suite *GenTxTestSuite) TestSetGenTxsInAppGenesisState() { suite.Run(fmt.Sprintf("Case %s", tc.msg), func() { suite.SetupTest() cdc := suite.encodingConfig.Marshaler - txJsonEncoder := suite.encodingConfig.TxConfig.TxJSONEncoder() + txJSONEncoder := suite.encodingConfig.TxConfig.TxJSONEncoder() tc.malleate() - appGenesisState, err := genutil.SetGenTxsInAppGenesisState(cdc, txJsonEncoder, make(map[string]json.RawMessage), genTxs) + appGenesisState, err := genutil.SetGenTxsInAppGenesisState(cdc, txJSONEncoder, make(map[string]json.RawMessage), genTxs) if tc.expPass { suite.Require().NoError(err) From 8d463a8982e98deb0ff115bf1190a931ccffa34a Mon Sep 17 00:00:00 2001 From: blushi Date: Fri, 24 Jul 2020 17:01:07 +0200 Subject: [PATCH 18/24] Refactor to use new TxEncodingConfig interface in genutil module --- client/tx_generator.go | 16 +++++++++++----- simapp/app.go | 2 +- testutil/network/network.go | 2 +- x/genutil/genesis.go | 5 +++-- x/genutil/gentx.go | 7 ++++--- x/genutil/gentx_test.go | 4 ++-- x/genutil/module.go | 24 +++++++++++------------- 7 files changed, 33 insertions(+), 27 deletions(-) diff --git a/client/tx_generator.go b/client/tx_generator.go index c5aecb1f4603..7d409ac31e9a 100644 --- a/client/tx_generator.go +++ b/client/tx_generator.go @@ -7,17 +7,23 @@ import ( ) type ( + // TxEncodingConfig defines an interface that contains transaction + // encoders and decoders + TxEncodingConfig interface { + TxEncoder() sdk.TxEncoder + TxDecoder() sdk.TxDecoder + TxJSONEncoder() sdk.TxEncoder + TxJSONDecoder() sdk.TxDecoder + } + // TxConfig defines an interface a client can utilize to generate an // application-defined concrete transaction type. The type returned must // implement TxBuilder. TxConfig interface { + TxEncodingConfig + NewTxBuilder() TxBuilder SignModeHandler() signing.SignModeHandler - - TxEncoder() sdk.TxEncoder - TxDecoder() sdk.TxDecoder - TxJSONEncoder() sdk.TxEncoder - TxJSONDecoder() sdk.TxDecoder } // TxBuilder defines an interface which an application-defined concrete transaction diff --git a/simapp/app.go b/simapp/app.go index 988386314b96..f8d98835b863 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -291,7 +291,7 @@ func NewSimApp( app.mm = module.NewManager( genutil.NewAppModule( app.AccountKeeper, app.StakingKeeper, app.BaseApp.DeliverTx, - encodingConfig.TxConfig.TxJSONDecoder(), encodingConfig.TxConfig.TxEncoder(), + encodingConfig.TxConfig, ), auth.NewAppModule(appCodec, app.AccountKeeper), bank.NewAppModule(appCodec, app.BankKeeper, app.AccountKeeper), diff --git a/testutil/network/network.go b/testutil/network/network.go index cb81bfb66f27..af561b79f48f 100644 --- a/testutil/network/network.go +++ b/testutil/network/network.go @@ -283,7 +283,7 @@ func New(t *testing.T, cfg Config) *Network { txBuilder := cfg.TxConfig.NewTxBuilder() require.NoError(t, txBuilder.SetMsgs(createValMsg)) txBuilder.SetMemo(memo) - + txFactory := tx.Factory{} txFactory = txFactory. WithChainID(cfg.ChainID). diff --git a/x/genutil/genesis.go b/x/genutil/genesis.go index 489f55747d77..9a155cfa1e25 100644 --- a/x/genutil/genesis.go +++ b/x/genutil/genesis.go @@ -3,6 +3,7 @@ package genutil import ( abci "github.com/tendermint/tendermint/abci/types" + "github.com/cosmos/cosmos-sdk/client" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/genutil/types" ) @@ -11,12 +12,12 @@ import ( func InitGenesis( ctx sdk.Context, stakingKeeper types.StakingKeeper, deliverTx deliverTxfn, genesisState types.GenesisState, - txJSONDecoder sdk.TxDecoder, txBinaryEncoder sdk.TxEncoder, + txEncodingConfig client.TxEncodingConfig, ) []abci.ValidatorUpdate { var validators []abci.ValidatorUpdate if len(genesisState.GenTxs) > 0 { - validators = DeliverGenTxs(ctx, genesisState.GenTxs, stakingKeeper, deliverTx, txJSONDecoder, txBinaryEncoder) + validators = DeliverGenTxs(ctx, genesisState.GenTxs, stakingKeeper, deliverTx, txEncodingConfig) } return validators diff --git a/x/genutil/gentx.go b/x/genutil/gentx.go index 32e0671cb1df..10849bde78c2 100644 --- a/x/genutil/gentx.go +++ b/x/genutil/gentx.go @@ -6,6 +6,7 @@ import ( abci "github.com/tendermint/tendermint/abci/types" + "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" bankexported "github.com/cosmos/cosmos-sdk/x/bank/exported" @@ -93,16 +94,16 @@ type deliverTxfn func(abci.RequestDeliverTx) abci.ResponseDeliverTx func DeliverGenTxs( ctx sdk.Context, genTxs []json.RawMessage, stakingKeeper types.StakingKeeper, deliverTx deliverTxfn, - txJSONDecoder sdk.TxDecoder, txBinaryEncoder sdk.TxEncoder, + txEncodingConfig client.TxEncodingConfig, ) []abci.ValidatorUpdate { for _, genTx := range genTxs { - tx, err := txJSONDecoder(genTx) + tx, err := txEncodingConfig.TxJSONDecoder()(genTx) if err != nil { panic(err) } - bz, err := txBinaryEncoder(tx) + bz, err := txEncodingConfig.TxEncoder()(tx) if err != nil { panic(err) } diff --git a/x/genutil/gentx_test.go b/x/genutil/gentx_test.go index 3551ddaeee6b..42b9e7c3eb10 100644 --- a/x/genutil/gentx_test.go +++ b/x/genutil/gentx_test.go @@ -260,14 +260,14 @@ func (suite *GenTxTestSuite) TestDeliverGenTxs() { suite.Require().NotPanics(func() { genutil.DeliverGenTxs( suite.ctx, genTxs, suite.app.StakingKeeper, suite.app.BaseApp.DeliverTx, - suite.encodingConfig.TxConfig.TxJSONDecoder(), suite.encodingConfig.TxConfig.TxEncoder(), + suite.encodingConfig.TxConfig, ) }) } else { suite.Require().Panics(func() { genutil.DeliverGenTxs( suite.ctx, genTxs, suite.app.StakingKeeper, suite.app.BaseApp.DeliverTx, - suite.encodingConfig.TxConfig.TxJSONDecoder(), suite.encodingConfig.TxConfig.TxEncoder(), + suite.encodingConfig.TxConfig, ) }) } diff --git a/x/genutil/module.go b/x/genutil/module.go index 53438f912df0..d2dce5c2bba3 100644 --- a/x/genutil/module.go +++ b/x/genutil/module.go @@ -67,26 +67,24 @@ func (AppModuleBasic) GetQueryCmd() *cobra.Command { return nil } type AppModule struct { AppModuleBasic - accountKeeper types.AccountKeeper - stakingKeeper types.StakingKeeper - deliverTx deliverTxfn - txJSONDecoder sdk.TxDecoder - txBinaryEncoder sdk.TxEncoder + accountKeeper types.AccountKeeper + stakingKeeper types.StakingKeeper + deliverTx deliverTxfn + txEncodingConfig client.TxEncodingConfig } // NewAppModule creates a new AppModule object func NewAppModule(accountKeeper types.AccountKeeper, stakingKeeper types.StakingKeeper, deliverTx deliverTxfn, - txJSONDecoder sdk.TxDecoder, txBinaryEncoder sdk.TxEncoder, + txEncodingConfig client.TxEncodingConfig, ) module.AppModule { return module.NewGenesisOnlyAppModule(AppModule{ - AppModuleBasic: AppModuleBasic{}, - accountKeeper: accountKeeper, - stakingKeeper: stakingKeeper, - deliverTx: deliverTx, - txJSONDecoder: txJSONDecoder, - txBinaryEncoder: txBinaryEncoder, + AppModuleBasic: AppModuleBasic{}, + accountKeeper: accountKeeper, + stakingKeeper: stakingKeeper, + deliverTx: deliverTx, + txEncodingConfig: txEncodingConfig, }) } @@ -95,7 +93,7 @@ func NewAppModule(accountKeeper types.AccountKeeper, func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONMarshaler, data json.RawMessage) []abci.ValidatorUpdate { var genesisState types.GenesisState cdc.MustUnmarshalJSON(data, &genesisState) - return InitGenesis(ctx, am.stakingKeeper, am.deliverTx, genesisState, am.txJSONDecoder, am.txBinaryEncoder) + return InitGenesis(ctx, am.stakingKeeper, am.deliverTx, genesisState, am.txEncodingConfig) } // ExportGenesis returns the exported genesis state as raw bytes for the genutil From 59064d941fd80ac4679d51c66d848eb592557421 Mon Sep 17 00:00:00 2001 From: blushi Date: Fri, 24 Jul 2020 17:05:43 +0200 Subject: [PATCH 19/24] Replace golang/protobuf with gogo/protobuf package --- x/auth/tx/builder.go | 2 +- x/auth/tx/encoder.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/x/auth/tx/builder.go b/x/auth/tx/builder.go index ebb9cfa75180..857d34a08a5f 100644 --- a/x/auth/tx/builder.go +++ b/x/auth/tx/builder.go @@ -3,7 +3,7 @@ package tx import ( "fmt" - "github.com/golang/protobuf/proto" + "github.com/gogo/protobuf/proto" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/x/auth/signing/direct" diff --git a/x/auth/tx/encoder.go b/x/auth/tx/encoder.go index 5884b756ffdc..004728a3e132 100644 --- a/x/auth/tx/encoder.go +++ b/x/auth/tx/encoder.go @@ -3,7 +3,7 @@ package tx import ( "fmt" - "github.com/golang/protobuf/proto" + "github.com/gogo/protobuf/proto" "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/types" From 1203a83e5ccdde90120606691e04f5989488615e Mon Sep 17 00:00:00 2001 From: blushi Date: Fri, 24 Jul 2020 18:09:57 +0200 Subject: [PATCH 20/24] Use TxEncodingConfig in InitTestnet --- simapp/simd/cmd/testnet.go | 13 +++++-------- testutil/network/util.go | 2 +- x/genutil/client/cli/collect.go | 2 +- x/genutil/collect.go | 7 ++++--- 4 files changed, 11 insertions(+), 13 deletions(-) diff --git a/simapp/simd/cmd/testnet.go b/simapp/simd/cmd/testnet.go index 297f6a50b8d4..f44e4501d95c 100644 --- a/simapp/simd/cmd/testnet.go +++ b/simapp/simd/cmd/testnet.go @@ -73,11 +73,8 @@ Example: startingIPAddress, _ := cmd.Flags().GetString(flagStartingIPAddress) numValidators, _ := cmd.Flags().GetInt(flagNumValidators) - txJSONDecoder := clientCtx.TxConfig.TxJSONDecoder() - txJSONEncoder := clientCtx.TxConfig.TxJSONEncoder() - return InitTestnet( - cmd, config, cdc, txJSONDecoder, txJSONEncoder, mbm, genBalIterator, outputDir, chainID, minGasPrices, + cmd, config, cdc, clientCtx.TxConfig, mbm, genBalIterator, outputDir, chainID, minGasPrices, nodeDirPrefix, nodeDaemonHome, nodeCLIHome, startingIPAddress, keyringBackend, numValidators, ) }, @@ -101,7 +98,7 @@ const nodeDirPerm = 0755 // Initialize the testnet func InitTestnet( cmd *cobra.Command, config *tmconfig.Config, cdc codec.JSONMarshaler, - txJSONDecoder sdk.TxDecoder, txJSONEncoder sdk.TxEncoder, + txEncodingConfig client.TxEncodingConfig, mbm module.BasicManager, genBalIterator banktypes.GenesisBalancesIterator, outputDir, chainID, minGasPrices, nodeDirPrefix, nodeDaemonHome, nodeCLIHome, startingIPAddress, keyringBackend string, numValidators int, @@ -244,7 +241,7 @@ func InitTestnet( } err := collectGenFiles( - cdc, txJSONDecoder, txJSONEncoder, config, chainID, nodeIDs, valPubKeys, numValidators, + cdc, txEncodingConfig, config, chainID, nodeIDs, valPubKeys, numValidators, outputDir, nodeDirPrefix, nodeDaemonHome, genBalIterator, ) if err != nil { @@ -298,7 +295,7 @@ func initGenFiles( } func collectGenFiles( - cdc codec.JSONMarshaler, txJSONDecoder sdk.TxDecoder, txJSONEncoder sdk.TxEncoder, + cdc codec.JSONMarshaler, txEncodingConfig client.TxEncodingConfig, config *tmconfig.Config, chainID string, nodeIDs []string, valPubKeys []crypto.PubKey, numValidators int, outputDir, nodeDirPrefix, nodeDaemonHome string, @@ -324,7 +321,7 @@ func collectGenFiles( return err } - nodeAppState, err := genutil.GenAppStateFromConfig(cdc, txJSONDecoder, txJSONEncoder, config, initCfg, *genDoc, genBalIterator) + nodeAppState, err := genutil.GenAppStateFromConfig(cdc, txEncodingConfig, config, initCfg, *genDoc, genBalIterator) if err != nil { return err } diff --git a/testutil/network/util.go b/testutil/network/util.go index c6d751984bf4..86bed50eb34e 100644 --- a/testutil/network/util.go +++ b/testutil/network/util.go @@ -105,7 +105,7 @@ func collectGenFiles(cfg Config, vals []*Validator, outputDir string) error { return err } - appState, err := genutil.GenAppStateFromConfig(cfg.Codec, cfg.TxConfig.TxJSONDecoder(), cfg.TxConfig.TxJSONEncoder(), + appState, err := genutil.GenAppStateFromConfig(cfg.Codec, cfg.TxConfig, tmCfg, initCfg, *genDoc, banktypes.GenesisBalancesIterator{}) if err != nil { return err diff --git a/x/genutil/client/cli/collect.go b/x/genutil/client/cli/collect.go index 6e136c96dd57..1fefbe382a87 100644 --- a/x/genutil/client/cli/collect.go +++ b/x/genutil/client/cli/collect.go @@ -51,7 +51,7 @@ func CollectGenTxsCmd(genBalIterator types.GenesisBalancesIterator, defaultNodeH initCfg := types.NewInitConfig(genDoc.ChainID, genTxsDir, nodeID, valPubKey) appMessage, err := genutil.GenAppStateFromConfig(cdc, - clientCtx.TxConfig.TxJSONDecoder(), clientCtx.TxConfig.TxJSONEncoder(), + clientCtx.TxConfig, config, initCfg, *genDoc, genBalIterator) if err != nil { return errors.Wrap(err, "failed to get genesis app state from config") diff --git a/x/genutil/collect.go b/x/genutil/collect.go index d0542ecccb6e..d1d69c8d9902 100644 --- a/x/genutil/collect.go +++ b/x/genutil/collect.go @@ -15,6 +15,7 @@ import ( cfg "github.com/tendermint/tendermint/config" tmtypes "github.com/tendermint/tendermint/types" + "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" bankexported "github.com/cosmos/cosmos-sdk/x/bank/exported" @@ -23,13 +24,13 @@ import ( ) // GenAppStateFromConfig gets the genesis app state from the config -func GenAppStateFromConfig(cdc codec.JSONMarshaler, txJSONDecoder sdk.TxDecoder, txJSONEncoder sdk.TxEncoder, +func GenAppStateFromConfig(cdc codec.JSONMarshaler, txEncodingConfig client.TxEncodingConfig, config *cfg.Config, initCfg types.InitConfig, genDoc tmtypes.GenesisDoc, genBalIterator types.GenesisBalancesIterator, ) (appState json.RawMessage, err error) { // process genesis transactions, else create default genesis.json appGenTxs, persistentPeers, err := CollectTxs( - cdc, txJSONDecoder, config.Moniker, initCfg.GenTxsDir, genDoc, genBalIterator, + cdc, txEncodingConfig.TxJSONDecoder(), config.Moniker, initCfg.GenTxsDir, genDoc, genBalIterator, ) if err != nil { return appState, err @@ -49,7 +50,7 @@ func GenAppStateFromConfig(cdc codec.JSONMarshaler, txJSONDecoder sdk.TxDecoder, return appState, err } - appGenesisState, err = SetGenTxsInAppGenesisState(cdc, txJSONEncoder, appGenesisState, appGenTxs) + appGenesisState, err = SetGenTxsInAppGenesisState(cdc, txEncodingConfig.TxJSONEncoder(), appGenesisState, appGenTxs) if err != nil { return appState, err } From 5070cd28d318aeab1807e1f2d163a1902de70f26 Mon Sep 17 00:00:00 2001 From: blushi Date: Fri, 24 Jul 2020 18:16:03 +0200 Subject: [PATCH 21/24] Remove old amino.go file --- simapp/amino.go | 19 ------------------- 1 file changed, 19 deletions(-) delete mode 100644 simapp/amino.go diff --git a/simapp/amino.go b/simapp/amino.go deleted file mode 100644 index c683cf6d5589..000000000000 --- a/simapp/amino.go +++ /dev/null @@ -1,19 +0,0 @@ -package simapp - -import ( - "github.com/cosmos/cosmos-sdk/simapp/params" - "github.com/cosmos/cosmos-sdk/types" - authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" -) - -// AminoJSONTxDecoder returns an Amino JSON TxDecoder using the provided cfg Marshaler -func AminoJSONTxDecoder(cfg params.EncodingConfig) types.TxDecoder { - return func(txBytes []byte) (types.Tx, error) { - var tx authtypes.StdTx - err := cfg.Marshaler.UnmarshalJSON(txBytes, &tx) - if err != nil { - return nil, err - } - return tx, nil - } -} From 5242be281540a6ee2d1ac48cebe2f67fe5cf344e Mon Sep 17 00:00:00 2001 From: blushi Date: Fri, 24 Jul 2020 20:53:59 +0200 Subject: [PATCH 22/24] Use TxJSONDecoder in genutil ValidateGenesis --- x/genutil/module.go | 26 ++++++++++++++------------ x/genutil/types/genesis_state.go | 5 +++-- x/genutil/types/genesis_state_test.go | 16 +++++++++------- 3 files changed, 26 insertions(+), 21 deletions(-) diff --git a/x/genutil/module.go b/x/genutil/module.go index d2dce5c2bba3..a6f98047c9a1 100644 --- a/x/genutil/module.go +++ b/x/genutil/module.go @@ -23,7 +23,9 @@ var ( ) // AppModuleBasic defines the basic application module used by the genutil module. -type AppModuleBasic struct{} +type AppModuleBasic struct { + txEncodingConfig client.TxEncodingConfig +} // Name returns the genutil module's name. func (AppModuleBasic) Name() string { @@ -43,13 +45,13 @@ func (AppModuleBasic) DefaultGenesis(cdc codec.JSONMarshaler) json.RawMessage { } // ValidateGenesis performs genesis state validation for the genutil module. -func (AppModuleBasic) ValidateGenesis(cdc codec.JSONMarshaler, bz json.RawMessage) error { +func (b AppModuleBasic) ValidateGenesis(cdc codec.JSONMarshaler, bz json.RawMessage) error { var data types.GenesisState if err := cdc.UnmarshalJSON(bz, &data); err != nil { return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err) } - return types.ValidateGenesis(data) + return types.ValidateGenesis(data, b.txEncodingConfig.TxJSONDecoder()) } // RegisterRESTRoutes registers the REST routes for the genutil module. @@ -67,10 +69,9 @@ func (AppModuleBasic) GetQueryCmd() *cobra.Command { return nil } type AppModule struct { AppModuleBasic - accountKeeper types.AccountKeeper - stakingKeeper types.StakingKeeper - deliverTx deliverTxfn - txEncodingConfig client.TxEncodingConfig + accountKeeper types.AccountKeeper + stakingKeeper types.StakingKeeper + deliverTx deliverTxfn } // NewAppModule creates a new AppModule object @@ -80,11 +81,12 @@ func NewAppModule(accountKeeper types.AccountKeeper, ) module.AppModule { return module.NewGenesisOnlyAppModule(AppModule{ - AppModuleBasic: AppModuleBasic{}, - accountKeeper: accountKeeper, - stakingKeeper: stakingKeeper, - deliverTx: deliverTx, - txEncodingConfig: txEncodingConfig, + AppModuleBasic: AppModuleBasic{ + txEncodingConfig: txEncodingConfig, + }, + accountKeeper: accountKeeper, + stakingKeeper: stakingKeeper, + deliverTx: deliverTx, }) } diff --git a/x/genutil/types/genesis_state.go b/x/genutil/types/genesis_state.go index c9bea5af2730..034f6737795a 100644 --- a/x/genutil/types/genesis_state.go +++ b/x/genutil/types/genesis_state.go @@ -98,10 +98,11 @@ func GenesisStateFromGenFile(cdc codec.JSONMarshaler, genFile string) (genesisSt } // ValidateGenesis validates GenTx transactions -func ValidateGenesis(genesisState GenesisState) error { +func ValidateGenesis(genesisState GenesisState, txJSONDecoder sdk.TxDecoder) error { for i, genTx := range genesisState.GenTxs { var tx sdk.Tx - if err := ModuleCdc.UnmarshalJSON(genTx, &tx); err != nil { + tx, err := txJSONDecoder(genTx) + if err != nil { return err } diff --git a/x/genutil/types/genesis_state_test.go b/x/genutil/types/genesis_state_test.go index 6f2776da926e..3be955e610d4 100644 --- a/x/genutil/types/genesis_state_test.go +++ b/x/genutil/types/genesis_state_test.go @@ -1,4 +1,4 @@ -package types +package types_test import ( "encoding/json" @@ -8,8 +8,10 @@ import ( "github.com/stretchr/testify/require" "github.com/tendermint/tendermint/crypto/ed25519" + "github.com/cosmos/cosmos-sdk/simapp" "github.com/cosmos/cosmos-sdk/simapp/params" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/genutil/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" ) @@ -19,10 +21,10 @@ var ( ) func TestNetGenesisState(t *testing.T) { - gen := NewGenesisState(nil) + gen := types.NewGenesisState(nil) assert.NotNil(t, gen.GenTxs) // https://github.com/cosmos/cosmos-sdk/issues/5086 - gen = NewGenesisState( + gen = types.NewGenesisState( []json.RawMessage{ []byte(`{"foo":"bar"}`), }, @@ -46,9 +48,9 @@ func TestValidateGenesisMultipleMessages(t *testing.T) { require.NoError(t, err) tx := txBuilder.GetTx() - genesisState := NewGenesisStateFromTx([]sdk.Tx{tx}) + genesisState := types.NewGenesisStateFromTx([]sdk.Tx{tx}) - err = ValidateGenesis(genesisState) + err = types.ValidateGenesis(genesisState, simapp.MakeEncodingConfig().TxConfig.TxJSONDecoder()) require.Error(t, err) } @@ -63,8 +65,8 @@ func TestValidateGenesisBadMessage(t *testing.T) { require.NoError(t, err) tx := txBuilder.GetTx() - genesisState := NewGenesisStateFromTx([]sdk.Tx{tx}) + genesisState := types.NewGenesisStateFromTx([]sdk.Tx{tx}) - err = ValidateGenesis(genesisState) + err = types.ValidateGenesis(genesisState, simapp.MakeEncodingConfig().TxConfig.TxJSONDecoder()) require.Error(t, err) } From 6a9c975bbe513f99959dbf54374fc2b03ad2c582 Mon Sep 17 00:00:00 2001 From: Aaron Craelius Date: Fri, 24 Jul 2020 20:08:53 -0400 Subject: [PATCH 23/24] Add parameter to ValidateGenesis to resolve the tx JSON decoder issue --- CHANGELOG.md | 1 + simapp/simd/cmd/root.go | 4 ++-- tests/mocks/types_module_module.go | 24 +++++++++++----------- types/module/module.go | 6 +++--- types/module/module_test.go | 6 +++--- x/auth/module.go | 2 +- x/bank/module.go | 2 +- x/capability/module.go | 2 +- x/crisis/module.go | 2 +- x/distribution/module.go | 2 +- x/evidence/module.go | 2 +- x/genutil/client/cli/gentx.go | 4 ++-- x/genutil/client/cli/validate_genesis.go | 4 ++-- x/genutil/module.go | 26 +++++++++++------------- x/gov/module.go | 2 +- x/ibc-transfer/module.go | 2 +- x/ibc/module.go | 2 +- x/mint/module.go | 2 +- x/params/module.go | 4 +++- x/slashing/module.go | 2 +- x/staking/module.go | 2 +- x/upgrade/module.go | 2 +- 22 files changed, 53 insertions(+), 52 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ff4b4cad4d04..ee3952e90791 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -152,6 +152,7 @@ be used to retrieve the actual proposal `Content`. Also the `NewMsgSubmitProposa * (client/flags) [\#6632](https://github.com/cosmos/cosmos-sdk/pull/6632) Remove NewCompletionCmd(), the function is now available in tendermint. * (crypto) [\#6780](https://github.com/cosmos/cosmos-sdk/issues/6780) Move ledger code to its own package. * (modules) [\#6834](https://github.com/cosmos/cosmos-sdk/issues/6834) Add `RegisterInterfaces` method to `AppModuleBasic` to support registration of protobuf interface types. +* (modules) [\#6734](https://github.com/cosmos/cosmos-sdk/issues/6834) Add `TxEncodingConfig` parameter to `AppModuleBasic.ValidateGenesis` command to support JSON tx decoding in `genutil`. ### Features diff --git a/simapp/simd/cmd/root.go b/simapp/simd/cmd/root.go index c3260b1ff06d..48695b04f480 100644 --- a/simapp/simd/cmd/root.go +++ b/simapp/simd/cmd/root.go @@ -78,8 +78,8 @@ func init() { genutilcli.InitCmd(simapp.ModuleBasics, simapp.DefaultNodeHome), genutilcli.CollectGenTxsCmd(banktypes.GenesisBalancesIterator{}, simapp.DefaultNodeHome), genutilcli.MigrateGenesisCmd(), - genutilcli.GenTxCmd(simapp.ModuleBasics, banktypes.GenesisBalancesIterator{}, simapp.DefaultNodeHome), - genutilcli.ValidateGenesisCmd(simapp.ModuleBasics), + genutilcli.GenTxCmd(simapp.ModuleBasics, encodingConfig.TxConfig, banktypes.GenesisBalancesIterator{}, simapp.DefaultNodeHome), + genutilcli.ValidateGenesisCmd(simapp.ModuleBasics, encodingConfig.TxConfig), AddGenesisAccountCmd(simapp.DefaultNodeHome), cli.NewCompletionCmd(rootCmd, true), testnetCmd(simapp.ModuleBasics, banktypes.GenesisBalancesIterator{}), diff --git a/tests/mocks/types_module_module.go b/tests/mocks/types_module_module.go index 947b750d9420..5270adf681b6 100644 --- a/tests/mocks/types_module_module.go +++ b/tests/mocks/types_module_module.go @@ -94,17 +94,17 @@ func (mr *MockAppModuleBasicMockRecorder) DefaultGenesis(arg0 interface{}) *gomo } // ValidateGenesis mocks base method -func (m *MockAppModuleBasic) ValidateGenesis(arg0 codec.JSONMarshaler, arg1 json.RawMessage) error { +func (m *MockAppModuleBasic) ValidateGenesis(arg0 codec.JSONMarshaler, arg1 client.TxEncodingConfig, arg2 json.RawMessage) error { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "ValidateGenesis", arg0, arg1) + ret := m.ctrl.Call(m, "ValidateGenesis", arg0, arg1, arg2) ret0, _ := ret[0].(error) return ret0 } // ValidateGenesis indicates an expected call of ValidateGenesis -func (mr *MockAppModuleBasicMockRecorder) ValidateGenesis(arg0, arg1 interface{}) *gomock.Call { +func (mr *MockAppModuleBasicMockRecorder) ValidateGenesis(arg0, arg1, arg2 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ValidateGenesis", reflect.TypeOf((*MockAppModuleBasic)(nil).ValidateGenesis), arg0, arg1) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ValidateGenesis", reflect.TypeOf((*MockAppModuleBasic)(nil).ValidateGenesis), arg0, arg1, arg2) } // RegisterRESTRoutes mocks base method @@ -223,17 +223,17 @@ func (mr *MockAppModuleGenesisMockRecorder) DefaultGenesis(arg0 interface{}) *go } // ValidateGenesis mocks base method -func (m *MockAppModuleGenesis) ValidateGenesis(arg0 codec.JSONMarshaler, arg1 json.RawMessage) error { +func (m *MockAppModuleGenesis) ValidateGenesis(arg0 codec.JSONMarshaler, arg1 client.TxEncodingConfig, arg2 json.RawMessage) error { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "ValidateGenesis", arg0, arg1) + ret := m.ctrl.Call(m, "ValidateGenesis", arg0, arg1, arg2) ret0, _ := ret[0].(error) return ret0 } // ValidateGenesis indicates an expected call of ValidateGenesis -func (mr *MockAppModuleGenesisMockRecorder) ValidateGenesis(arg0, arg1 interface{}) *gomock.Call { +func (mr *MockAppModuleGenesisMockRecorder) ValidateGenesis(arg0, arg1, arg2 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ValidateGenesis", reflect.TypeOf((*MockAppModuleGenesis)(nil).ValidateGenesis), arg0, arg1) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ValidateGenesis", reflect.TypeOf((*MockAppModuleGenesis)(nil).ValidateGenesis), arg0, arg1, arg2) } // RegisterRESTRoutes mocks base method @@ -380,17 +380,17 @@ func (mr *MockAppModuleMockRecorder) DefaultGenesis(arg0 interface{}) *gomock.Ca } // ValidateGenesis mocks base method -func (m *MockAppModule) ValidateGenesis(arg0 codec.JSONMarshaler, arg1 json.RawMessage) error { +func (m *MockAppModule) ValidateGenesis(arg0 codec.JSONMarshaler, arg1 client.TxEncodingConfig, arg2 json.RawMessage) error { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "ValidateGenesis", arg0, arg1) + ret := m.ctrl.Call(m, "ValidateGenesis", arg0, arg1, arg2) ret0, _ := ret[0].(error) return ret0 } // ValidateGenesis indicates an expected call of ValidateGenesis -func (mr *MockAppModuleMockRecorder) ValidateGenesis(arg0, arg1 interface{}) *gomock.Call { +func (mr *MockAppModuleMockRecorder) ValidateGenesis(arg0, arg1, arg2 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ValidateGenesis", reflect.TypeOf((*MockAppModule)(nil).ValidateGenesis), arg0, arg1) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ValidateGenesis", reflect.TypeOf((*MockAppModule)(nil).ValidateGenesis), arg0, arg1, arg2) } // RegisterRESTRoutes mocks base method diff --git a/types/module/module.go b/types/module/module.go index a56842992b48..ac8e12593730 100644 --- a/types/module/module.go +++ b/types/module/module.go @@ -52,7 +52,7 @@ type AppModuleBasic interface { RegisterInterfaces(codectypes.InterfaceRegistry) DefaultGenesis(codec.JSONMarshaler) json.RawMessage - ValidateGenesis(codec.JSONMarshaler, json.RawMessage) error + ValidateGenesis(codec.JSONMarshaler, client.TxEncodingConfig, json.RawMessage) error // client functionality RegisterRESTRoutes(client.Context, *mux.Router) @@ -97,9 +97,9 @@ func (bm BasicManager) DefaultGenesis(cdc codec.JSONMarshaler) map[string]json.R } // ValidateGenesis performs genesis state validation for all modules -func (bm BasicManager) ValidateGenesis(cdc codec.JSONMarshaler, genesis map[string]json.RawMessage) error { +func (bm BasicManager) ValidateGenesis(cdc codec.JSONMarshaler, txEncCfg client.TxEncodingConfig, genesis map[string]json.RawMessage) error { for _, b := range bm { - if err := b.ValidateGenesis(cdc, genesis[b.Name()]); err != nil { + if err := b.ValidateGenesis(cdc, txEncCfg, genesis[b.Name()]); err != nil { return err } } diff --git a/types/module/module_test.go b/types/module/module_test.go index cc4fc9af3268..6919cc97d740 100644 --- a/types/module/module_test.go +++ b/types/module/module_test.go @@ -35,7 +35,7 @@ func TestBasicManager(t *testing.T) { mockAppModuleBasic1.EXPECT().Name().AnyTimes().Return("mockAppModuleBasic1") mockAppModuleBasic1.EXPECT().DefaultGenesis(gomock.Eq(cdc)).Times(1).Return(json.RawMessage(``)) - mockAppModuleBasic1.EXPECT().ValidateGenesis(gomock.Eq(cdc), gomock.Eq(wantDefaultGenesis["mockAppModuleBasic1"])).Times(1).Return(errFoo) + mockAppModuleBasic1.EXPECT().ValidateGenesis(gomock.Eq(cdc), gomock.Eq(nil), gomock.Eq(wantDefaultGenesis["mockAppModuleBasic1"])).Times(1).Return(errFoo) mockAppModuleBasic1.EXPECT().RegisterRESTRoutes(gomock.Eq(client.Context{}), gomock.Eq(&mux.Router{})).Times(1) mockAppModuleBasic1.EXPECT().RegisterCodec(gomock.Eq(cdc)).Times(1) mockAppModuleBasic1.EXPECT().RegisterInterfaces(gomock.Eq(interfaceRegistry)).Times(1) @@ -53,7 +53,7 @@ func TestBasicManager(t *testing.T) { var data map[string]string require.Equal(t, map[string]string(nil), data) - require.True(t, errors.Is(errFoo, mm.ValidateGenesis(cdc, wantDefaultGenesis))) + require.True(t, errors.Is(errFoo, mm.ValidateGenesis(cdc, nil, wantDefaultGenesis))) mm.RegisterRESTRoutes(client.Context{}, &mux.Router{}) @@ -63,7 +63,7 @@ func TestBasicManager(t *testing.T) { mm.AddQueryCommands(mockCmd) // validate genesis returns nil - require.Nil(t, module.NewBasicManager().ValidateGenesis(cdc, wantDefaultGenesis)) + require.Nil(t, module.NewBasicManager().ValidateGenesis(cdc, nil, wantDefaultGenesis)) } func TestGenesisOnlyAppModule(t *testing.T) { diff --git a/x/auth/module.go b/x/auth/module.go index 7ca63827ed2a..e54771cd65ff 100644 --- a/x/auth/module.go +++ b/x/auth/module.go @@ -50,7 +50,7 @@ func (AppModuleBasic) DefaultGenesis(cdc codec.JSONMarshaler) json.RawMessage { } // ValidateGenesis performs genesis state validation for the auth module. -func (AppModuleBasic) ValidateGenesis(cdc codec.JSONMarshaler, bz json.RawMessage) error { +func (AppModuleBasic) ValidateGenesis(cdc codec.JSONMarshaler, config client.TxEncodingConfig, bz json.RawMessage) error { var data types.GenesisState if err := cdc.UnmarshalJSON(bz, &data); err != nil { return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err) diff --git a/x/bank/module.go b/x/bank/module.go index 244711257437..63cdc1617559 100644 --- a/x/bank/module.go +++ b/x/bank/module.go @@ -47,7 +47,7 @@ func (AppModuleBasic) DefaultGenesis(cdc codec.JSONMarshaler) json.RawMessage { } // ValidateGenesis performs genesis state validation for the bank module. -func (AppModuleBasic) ValidateGenesis(cdc codec.JSONMarshaler, bz json.RawMessage) error { +func (AppModuleBasic) ValidateGenesis(cdc codec.JSONMarshaler, config client.TxEncodingConfig, bz json.RawMessage) error { var data types.GenesisState if err := cdc.UnmarshalJSON(bz, &data); err != nil { return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err) diff --git a/x/capability/module.go b/x/capability/module.go index 06ee437103ac..869b555201d3 100644 --- a/x/capability/module.go +++ b/x/capability/module.go @@ -60,7 +60,7 @@ func (AppModuleBasic) DefaultGenesis(cdc codec.JSONMarshaler) json.RawMessage { } // ValidateGenesis performs genesis state validation for the capability module. -func (AppModuleBasic) ValidateGenesis(cdc codec.JSONMarshaler, bz json.RawMessage) error { +func (AppModuleBasic) ValidateGenesis(cdc codec.JSONMarshaler, config client.TxEncodingConfig, bz json.RawMessage) error { var genState types.GenesisState if err := cdc.UnmarshalJSON(bz, &genState); err != nil { return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err) diff --git a/x/crisis/module.go b/x/crisis/module.go index 40af92e03c7f..74cc2f86fb8f 100644 --- a/x/crisis/module.go +++ b/x/crisis/module.go @@ -44,7 +44,7 @@ func (AppModuleBasic) DefaultGenesis(cdc codec.JSONMarshaler) json.RawMessage { } // ValidateGenesis performs genesis state validation for the crisis module. -func (AppModuleBasic) ValidateGenesis(cdc codec.JSONMarshaler, bz json.RawMessage) error { +func (AppModuleBasic) ValidateGenesis(cdc codec.JSONMarshaler, config client.TxEncodingConfig, bz json.RawMessage) error { var data types.GenesisState if err := cdc.UnmarshalJSON(bz, &data); err != nil { return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err) diff --git a/x/distribution/module.go b/x/distribution/module.go index fe20c76de228..a85e91fda160 100644 --- a/x/distribution/module.go +++ b/x/distribution/module.go @@ -53,7 +53,7 @@ func (AppModuleBasic) DefaultGenesis(cdc codec.JSONMarshaler) json.RawMessage { } // ValidateGenesis performs genesis state validation for the distribution module. -func (AppModuleBasic) ValidateGenesis(cdc codec.JSONMarshaler, bz json.RawMessage) error { +func (AppModuleBasic) ValidateGenesis(cdc codec.JSONMarshaler, config sdkclient.TxEncodingConfig, bz json.RawMessage) error { var data types.GenesisState if err := cdc.UnmarshalJSON(bz, &data); err != nil { return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err) diff --git a/x/evidence/module.go b/x/evidence/module.go index 5ef5bd17d9a5..cfb039117b8c 100644 --- a/x/evidence/module.go +++ b/x/evidence/module.go @@ -64,7 +64,7 @@ func (AppModuleBasic) DefaultGenesis(cdc codec.JSONMarshaler) json.RawMessage { } // ValidateGenesis performs genesis state validation for the evidence module. -func (AppModuleBasic) ValidateGenesis(cdc codec.JSONMarshaler, bz json.RawMessage) error { +func (AppModuleBasic) ValidateGenesis(cdc codec.JSONMarshaler, config client.TxEncodingConfig, bz json.RawMessage) error { var gs types.GenesisState if err := cdc.UnmarshalJSON(bz, &gs); err != nil { return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err) diff --git a/x/genutil/client/cli/gentx.go b/x/genutil/client/cli/gentx.go index 749defcd40a4..328f0815d1f3 100644 --- a/x/genutil/client/cli/gentx.go +++ b/x/genutil/client/cli/gentx.go @@ -31,7 +31,7 @@ import ( ) // GenTxCmd builds the application's gentx command. -func GenTxCmd(mbm module.BasicManager, genBalIterator types.GenesisBalancesIterator, defaultNodeHome string) *cobra.Command { +func GenTxCmd(mbm module.BasicManager, txEncCfg client.TxEncodingConfig, genBalIterator types.GenesisBalancesIterator, defaultNodeHome string) *cobra.Command { ipDefault, _ := server.ExternalIP() fsCreateValidator, defaultsDesc := cli.CreateValidatorMsgFlagSet(ipDefault) @@ -93,7 +93,7 @@ $ %s gentx my-key-name --home=/path/to/home/dir --keyring-backend=os --chain-id= return errors.Wrap(err, "failed to unmarshal genesis state") } - if err = mbm.ValidateGenesis(cdc, genesisState); err != nil { + if err = mbm.ValidateGenesis(cdc, txEncCfg, genesisState); err != nil { return errors.Wrap(err, "failed to validate genesis state") } diff --git a/x/genutil/client/cli/validate_genesis.go b/x/genutil/client/cli/validate_genesis.go index 8d4dce3a2793..756b06a26ea4 100644 --- a/x/genutil/client/cli/validate_genesis.go +++ b/x/genutil/client/cli/validate_genesis.go @@ -14,7 +14,7 @@ import ( ) // Validate genesis command takes -func ValidateGenesisCmd(mbm module.BasicManager) *cobra.Command { +func ValidateGenesisCmd(mbm module.BasicManager, txEncCfg client.TxEncodingConfig) *cobra.Command { return &cobra.Command{ Use: "validate-genesis [file]", Args: cobra.RangeArgs(0, 1), @@ -45,7 +45,7 @@ func ValidateGenesisCmd(mbm module.BasicManager) *cobra.Command { return fmt.Errorf("error unmarshalling genesis doc %s: %s", genesis, err.Error()) } - if err = mbm.ValidateGenesis(cdc, genState); err != nil { + if err = mbm.ValidateGenesis(cdc, txEncCfg, genState); err != nil { return fmt.Errorf("error validating genesis file %s: %s", genesis, err.Error()) } diff --git a/x/genutil/module.go b/x/genutil/module.go index a6f98047c9a1..dc7bd72e57c6 100644 --- a/x/genutil/module.go +++ b/x/genutil/module.go @@ -23,9 +23,7 @@ var ( ) // AppModuleBasic defines the basic application module used by the genutil module. -type AppModuleBasic struct { - txEncodingConfig client.TxEncodingConfig -} +type AppModuleBasic struct{} // Name returns the genutil module's name. func (AppModuleBasic) Name() string { @@ -45,13 +43,13 @@ func (AppModuleBasic) DefaultGenesis(cdc codec.JSONMarshaler) json.RawMessage { } // ValidateGenesis performs genesis state validation for the genutil module. -func (b AppModuleBasic) ValidateGenesis(cdc codec.JSONMarshaler, bz json.RawMessage) error { +func (b AppModuleBasic) ValidateGenesis(cdc codec.JSONMarshaler, txEncodingConfig client.TxEncodingConfig, bz json.RawMessage) error { var data types.GenesisState if err := cdc.UnmarshalJSON(bz, &data); err != nil { return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err) } - return types.ValidateGenesis(data, b.txEncodingConfig.TxJSONDecoder()) + return types.ValidateGenesis(data, txEncodingConfig.TxJSONDecoder()) } // RegisterRESTRoutes registers the REST routes for the genutil module. @@ -69,9 +67,10 @@ func (AppModuleBasic) GetQueryCmd() *cobra.Command { return nil } type AppModule struct { AppModuleBasic - accountKeeper types.AccountKeeper - stakingKeeper types.StakingKeeper - deliverTx deliverTxfn + accountKeeper types.AccountKeeper + stakingKeeper types.StakingKeeper + deliverTx deliverTxfn + txEncodingConfig client.TxEncodingConfig } // NewAppModule creates a new AppModule object @@ -81,12 +80,11 @@ func NewAppModule(accountKeeper types.AccountKeeper, ) module.AppModule { return module.NewGenesisOnlyAppModule(AppModule{ - AppModuleBasic: AppModuleBasic{ - txEncodingConfig: txEncodingConfig, - }, - accountKeeper: accountKeeper, - stakingKeeper: stakingKeeper, - deliverTx: deliverTx, + AppModuleBasic: AppModuleBasic{}, + accountKeeper: accountKeeper, + stakingKeeper: stakingKeeper, + deliverTx: deliverTx, + txEncodingConfig: txEncodingConfig, }) } diff --git a/x/gov/module.go b/x/gov/module.go index d0dcde556fed..329b7d706988 100644 --- a/x/gov/module.go +++ b/x/gov/module.go @@ -64,7 +64,7 @@ func (AppModuleBasic) DefaultGenesis(cdc codec.JSONMarshaler) json.RawMessage { } // ValidateGenesis performs genesis state validation for the gov module. -func (AppModuleBasic) ValidateGenesis(cdc codec.JSONMarshaler, bz json.RawMessage) error { +func (AppModuleBasic) ValidateGenesis(cdc codec.JSONMarshaler, config client.TxEncodingConfig, bz json.RawMessage) error { var data types.GenesisState if err := cdc.UnmarshalJSON(bz, &data); err != nil { return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err) diff --git a/x/ibc-transfer/module.go b/x/ibc-transfer/module.go index cb1c36294632..541914338c1a 100644 --- a/x/ibc-transfer/module.go +++ b/x/ibc-transfer/module.go @@ -56,7 +56,7 @@ func (AppModuleBasic) DefaultGenesis(cdc codec.JSONMarshaler) json.RawMessage { } // ValidateGenesis performs genesis state validation for the ibc transfer module. -func (AppModuleBasic) ValidateGenesis(cdc codec.JSONMarshaler, bz json.RawMessage) error { +func (AppModuleBasic) ValidateGenesis(cdc codec.JSONMarshaler, config client.TxEncodingConfig, bz json.RawMessage) error { var gs types.GenesisState if err := cdc.UnmarshalJSON(bz, &gs); err != nil { return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err) diff --git a/x/ibc/module.go b/x/ibc/module.go index 34d16dd0ba0e..6db3bd6a15d6 100644 --- a/x/ibc/module.go +++ b/x/ibc/module.go @@ -54,7 +54,7 @@ func (AppModuleBasic) DefaultGenesis(cdc codec.JSONMarshaler) json.RawMessage { } // ValidateGenesis performs genesis state validation for the ibc module. -func (AppModuleBasic) ValidateGenesis(cdc codec.JSONMarshaler, bz json.RawMessage) error { +func (AppModuleBasic) ValidateGenesis(cdc codec.JSONMarshaler, config client.TxEncodingConfig, bz json.RawMessage) error { var gs types.GenesisState if err := cdc.UnmarshalJSON(bz, &gs); err != nil { return fmt.Errorf("failed to unmarshal %s genesis state: %w", host.ModuleName, err) diff --git a/x/mint/module.go b/x/mint/module.go index e88ab00fc408..d048cd018b1f 100644 --- a/x/mint/module.go +++ b/x/mint/module.go @@ -54,7 +54,7 @@ func (AppModuleBasic) DefaultGenesis(cdc codec.JSONMarshaler) json.RawMessage { } // ValidateGenesis performs genesis state validation for the mint module. -func (AppModuleBasic) ValidateGenesis(cdc codec.JSONMarshaler, bz json.RawMessage) error { +func (AppModuleBasic) ValidateGenesis(cdc codec.JSONMarshaler, config client.TxEncodingConfig, bz json.RawMessage) error { var data types.GenesisState if err := cdc.UnmarshalJSON(bz, &data); err != nil { return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err) diff --git a/x/params/module.go b/x/params/module.go index 249e92e794dc..8f1cc9f4aa04 100644 --- a/x/params/module.go +++ b/x/params/module.go @@ -47,7 +47,9 @@ func (AppModuleBasic) RegisterCodec(cdc *codec.Codec) { func (AppModuleBasic) DefaultGenesis(_ codec.JSONMarshaler) json.RawMessage { return nil } // ValidateGenesis performs genesis state validation for the params module. -func (AppModuleBasic) ValidateGenesis(_ codec.JSONMarshaler, _ json.RawMessage) error { return nil } +func (AppModuleBasic) ValidateGenesis(_ codec.JSONMarshaler, config client.TxEncodingConfig, _ json.RawMessage) error { + return nil +} // RegisterRESTRoutes registers the REST routes for the params module. func (AppModuleBasic) RegisterRESTRoutes(_ client.Context, _ *mux.Router) {} diff --git a/x/slashing/module.go b/x/slashing/module.go index c24f28bd9d1b..9a0ad666370e 100644 --- a/x/slashing/module.go +++ b/x/slashing/module.go @@ -61,7 +61,7 @@ func (AppModuleBasic) DefaultGenesis(cdc codec.JSONMarshaler) json.RawMessage { } // ValidateGenesis performs genesis state validation for the slashing module. -func (AppModuleBasic) ValidateGenesis(cdc codec.JSONMarshaler, bz json.RawMessage) error { +func (AppModuleBasic) ValidateGenesis(cdc codec.JSONMarshaler, config client.TxEncodingConfig, bz json.RawMessage) error { var data types.GenesisState if err := cdc.UnmarshalJSON(bz, &data); err != nil { return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err) diff --git a/x/staking/module.go b/x/staking/module.go index 2b58af18dddc..af4b17a7c64d 100644 --- a/x/staking/module.go +++ b/x/staking/module.go @@ -59,7 +59,7 @@ func (AppModuleBasic) DefaultGenesis(cdc codec.JSONMarshaler) json.RawMessage { } // ValidateGenesis performs genesis state validation for the staking module. -func (AppModuleBasic) ValidateGenesis(cdc codec.JSONMarshaler, bz json.RawMessage) error { +func (AppModuleBasic) ValidateGenesis(cdc codec.JSONMarshaler, config client.TxEncodingConfig, bz json.RawMessage) error { var data types.GenesisState if err := cdc.UnmarshalJSON(bz, &data); err != nil { return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err) diff --git a/x/upgrade/module.go b/x/upgrade/module.go index c1ef30f73a48..b233b5938d9b 100644 --- a/x/upgrade/module.go +++ b/x/upgrade/module.go @@ -110,7 +110,7 @@ func (AppModuleBasic) DefaultGenesis(_ codec.JSONMarshaler) json.RawMessage { } // ValidateGenesis is always successful, as we ignore the value -func (AppModuleBasic) ValidateGenesis(_ codec.JSONMarshaler, _ json.RawMessage) error { +func (AppModuleBasic) ValidateGenesis(_ codec.JSONMarshaler, config client.TxEncodingConfig, _ json.RawMessage) error { return nil } From eb633a644ca5f9102f8feb0cc04bc8a2be3384a1 Mon Sep 17 00:00:00 2001 From: Aaron Craelius Date: Fri, 24 Jul 2020 20:10:37 -0400 Subject: [PATCH 24/24] Address review feedback --- simapp/simd/cmd/testnet.go | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/simapp/simd/cmd/testnet.go b/simapp/simd/cmd/testnet.go index f44e4501d95c..bdb329177187 100644 --- a/simapp/simd/cmd/testnet.go +++ b/simapp/simd/cmd/testnet.go @@ -97,7 +97,7 @@ const nodeDirPerm = 0755 // Initialize the testnet func InitTestnet( - cmd *cobra.Command, config *tmconfig.Config, cdc codec.JSONMarshaler, + cmd *cobra.Command, nodeConfig *tmconfig.Config, cdc codec.JSONMarshaler, txEncodingConfig client.TxEncodingConfig, mbm module.BasicManager, genBalIterator banktypes.GenesisBalancesIterator, outputDir, chainID, minGasPrices, nodeDirPrefix, nodeDaemonHome, @@ -133,8 +133,8 @@ func InitTestnet( clientDir := filepath.Join(outputDir, nodeDirName, nodeCLIHome) gentxsDir := filepath.Join(outputDir, "gentxs") - config.SetRoot(nodeDir) - config.RPC.ListenAddress = "tcp://0.0.0.0:26657" + nodeConfig.SetRoot(nodeDir) + nodeConfig.RPC.ListenAddress = "tcp://0.0.0.0:26657" if err := os.MkdirAll(filepath.Join(nodeDir, "config"), nodeDirPerm); err != nil { _ = os.RemoveAll(outputDir) @@ -146,7 +146,7 @@ func InitTestnet( return err } - config.Moniker = nodeDirName + nodeConfig.Moniker = nodeDirName ip, err := getIP(i, startingIPAddress) if err != nil { @@ -154,14 +154,14 @@ func InitTestnet( return err } - nodeIDs[i], valPubKeys[i], err = genutil.InitializeNodeValidatorFiles(config) + nodeIDs[i], valPubKeys[i], err = genutil.InitializeNodeValidatorFiles(nodeConfig) if err != nil { _ = os.RemoveAll(outputDir) return err } memo := fmt.Sprintf("%s@%s:26656", nodeIDs[i], ip) - genFiles = append(genFiles, config.GenesisFile()) + genFiles = append(genFiles, nodeConfig.GenesisFile()) kb, err := keyring.New(sdk.KeyringServiceName(), keyringBackend, clientDir, inBuf) if err != nil { @@ -241,7 +241,7 @@ func InitTestnet( } err := collectGenFiles( - cdc, txEncodingConfig, config, chainID, nodeIDs, valPubKeys, numValidators, + cdc, txEncodingConfig, nodeConfig, chainID, nodeIDs, valPubKeys, numValidators, outputDir, nodeDirPrefix, nodeDaemonHome, genBalIterator, ) if err != nil { @@ -296,7 +296,7 @@ func initGenFiles( func collectGenFiles( cdc codec.JSONMarshaler, txEncodingConfig client.TxEncodingConfig, - config *tmconfig.Config, chainID string, + nodeConfig *tmconfig.Config, chainID string, nodeIDs []string, valPubKeys []crypto.PubKey, numValidators int, outputDir, nodeDirPrefix, nodeDaemonHome string, genBalIterator banktypes.GenesisBalancesIterator, @@ -309,19 +309,19 @@ func collectGenFiles( nodeDirName := fmt.Sprintf("%s%d", nodeDirPrefix, i) nodeDir := filepath.Join(outputDir, nodeDirName, nodeDaemonHome) gentxsDir := filepath.Join(outputDir, "gentxs") - config.Moniker = nodeDirName + nodeConfig.Moniker = nodeDirName - config.SetRoot(nodeDir) + nodeConfig.SetRoot(nodeDir) nodeID, valPubKey := nodeIDs[i], valPubKeys[i] initCfg := genutiltypes.NewInitConfig(chainID, gentxsDir, nodeID, valPubKey) - genDoc, err := types.GenesisDocFromFile(config.GenesisFile()) + genDoc, err := types.GenesisDocFromFile(nodeConfig.GenesisFile()) if err != nil { return err } - nodeAppState, err := genutil.GenAppStateFromConfig(cdc, txEncodingConfig, config, initCfg, *genDoc, genBalIterator) + nodeAppState, err := genutil.GenAppStateFromConfig(cdc, txEncodingConfig, nodeConfig, initCfg, *genDoc, genBalIterator) if err != nil { return err } @@ -331,7 +331,7 @@ func collectGenFiles( appState = nodeAppState } - genFile := config.GenesisFile() + genFile := nodeConfig.GenesisFile() // overwrite each validator's genesis file to have a canonical genesis time if err := genutil.ExportGenesisFileWithTime(genFile, chainID, nil, appState, genTime); err != nil {