From 0f7454f78d92bba4fdecf74dc5f054c6f128cf24 Mon Sep 17 00:00:00 2001 From: anilCSE Date: Wed, 30 Mar 2022 13:21:51 +0530 Subject: [PATCH 01/10] Add validate gentx, debug error messages --- x/genutil/collect.go | 4 ++-- x/genutil/gentx.go | 6 ++--- x/genutil/types/genesis_state.go | 41 ++++++++++++++++++++------------ 3 files changed, 31 insertions(+), 20 deletions(-) diff --git a/x/genutil/collect.go b/x/genutil/collect.go index 6ad5f50aafb7..8b06c12f7740 100644 --- a/x/genutil/collect.go +++ b/x/genutil/collect.go @@ -111,8 +111,8 @@ func CollectTxs(cdc codec.JSONCodec, txJSONDecoder sdk.TxDecoder, moniker, genTx return appGenTxs, persistentPeers, err } - var genTx sdk.Tx - if genTx, err = txJSONDecoder(jsonRawTx); err != nil { + genTx, err := types.ValidateAndGetGenTx(jsonRawTx, txJSONDecoder) + if err != nil { return appGenTxs, persistentPeers, err } diff --git a/x/genutil/gentx.go b/x/genutil/gentx.go index 61b237151f2d..e157c571d02a 100644 --- a/x/genutil/gentx.go +++ b/x/genutil/gentx.go @@ -100,17 +100,17 @@ func DeliverGenTxs( for _, genTx := range genTxs { tx, err := txEncodingConfig.TxJSONDecoder()(genTx) if err != nil { - panic(err) + panic(fmt.Sprintf("failed to decode gentx: %s error: %s", genTx, err)) } bz, err := txEncodingConfig.TxEncoder()(tx) if err != nil { - panic(err) + panic(fmt.Sprintf("failed to encode gentx: %s error: %s", genTx, err)) } res := deliverTx(abci.RequestDeliverTx{Tx: bz}) if !res.IsOK() { - panic(res.Log) + panic(fmt.Sprintf("failed to deliverTx: %s error: %s", genTx, res.Log)) } } diff --git a/x/genutil/types/genesis_state.go b/x/genutil/types/genesis_state.go index 657f57a94704..fc885539057d 100644 --- a/x/genutil/types/genesis_state.go +++ b/x/genutil/types/genesis_state.go @@ -2,7 +2,6 @@ package types import ( "encoding/json" - "errors" "fmt" tmos "github.com/tendermint/tendermint/libs/os" @@ -96,24 +95,36 @@ func GenesisStateFromGenFile(genFile string) (genesisState map[string]json.RawMe // ValidateGenesis validates GenTx transactions func ValidateGenesis(genesisState *GenesisState, txJSONDecoder sdk.TxDecoder) error { - for i, genTx := range genesisState.GenTxs { - var tx sdk.Tx - tx, err := txJSONDecoder(genTx) + for _, genTx := range genesisState.GenTxs { + _, err := ValidateAndGetGenTx(genTx, txJSONDecoder) if err != nil { return err } + } + return nil +} - msgs := tx.GetMsgs() - if len(msgs) != 1 { - return errors.New( - "must provide genesis Tx with exactly 1 CreateValidator message") - } +func ValidateAndGetGenTx(genTx json.RawMessage, txJSONDecoder sdk.TxDecoder) (sdk.Tx, error) { + tx, err := txJSONDecoder(genTx) + if err != nil { + return tx, fmt.Errorf("failed to decode gentx: %s, error: %s", genTx, err) + } - // TODO: abstract back to staking - if _, ok := msgs[0].(*stakingtypes.MsgCreateValidator); !ok { - return fmt.Errorf( - "genesis transaction %v does not contain a MsgCreateValidator", i) - } + msgs := tx.GetMsgs() + if len(msgs) != 1 { + return tx, fmt.Errorf( + "must provide genesis Tx with exactly 1 CreateValidator message, got more in gentx %v", tx) } - return nil + + // TODO: abstract back to staking + if _, ok := msgs[0].(*stakingtypes.MsgCreateValidator); !ok { + return tx, fmt.Errorf( + "genesis transaction %v does not contain a MsgCreateValidator", tx) + } + + if err := msgs[0].ValidateBasic(); err != nil { + return tx, fmt.Errorf("invalid gentx: %s, error:%s", msgs[0], err) + } + + return tx, nil } From bfa51af60b908d957cd1a5667d0b6ff21f25f691 Mon Sep 17 00:00:00 2001 From: Anil Kumar Kammari Date: Wed, 30 Mar 2022 22:30:12 +0530 Subject: [PATCH 02/10] Update x/genutil/gentx.go Co-authored-by: Aleksandr Bezobchuk --- x/genutil/gentx.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/genutil/gentx.go b/x/genutil/gentx.go index e157c571d02a..13b22189ae22 100644 --- a/x/genutil/gentx.go +++ b/x/genutil/gentx.go @@ -110,7 +110,7 @@ func DeliverGenTxs( res := deliverTx(abci.RequestDeliverTx{Tx: bz}) if !res.IsOK() { - panic(fmt.Sprintf("failed to deliverTx: %s error: %s", genTx, res.Log)) + panic(fmt.Sprintf("failed to execute DelverTx for '%s': %s", genTx, res.Log)) } } From 14f9209aac48c6fe2e8557f73e9c541abce0cf2b Mon Sep 17 00:00:00 2001 From: Anil Kumar Kammari Date: Wed, 30 Mar 2022 22:30:24 +0530 Subject: [PATCH 03/10] Update x/genutil/gentx.go Co-authored-by: Aleksandr Bezobchuk --- x/genutil/gentx.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/genutil/gentx.go b/x/genutil/gentx.go index 13b22189ae22..4d9fb50b8b07 100644 --- a/x/genutil/gentx.go +++ b/x/genutil/gentx.go @@ -105,7 +105,7 @@ func DeliverGenTxs( bz, err := txEncodingConfig.TxEncoder()(tx) if err != nil { - panic(fmt.Sprintf("failed to encode gentx: %s error: %s", genTx, err)) + panic(fmt.Sprintf("failed to encode GenTx '%s': %s", genTx, err)) } res := deliverTx(abci.RequestDeliverTx{Tx: bz}) From 465b83bbc843e1d8d0cc5b2a7fda243a811a3822 Mon Sep 17 00:00:00 2001 From: Anil Kumar Kammari Date: Wed, 30 Mar 2022 22:30:35 +0530 Subject: [PATCH 04/10] Update x/genutil/gentx.go Co-authored-by: Aleksandr Bezobchuk --- x/genutil/gentx.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/genutil/gentx.go b/x/genutil/gentx.go index 4d9fb50b8b07..750717eb992c 100644 --- a/x/genutil/gentx.go +++ b/x/genutil/gentx.go @@ -100,7 +100,7 @@ func DeliverGenTxs( for _, genTx := range genTxs { tx, err := txEncodingConfig.TxJSONDecoder()(genTx) if err != nil { - panic(fmt.Sprintf("failed to decode gentx: %s error: %s", genTx, err)) + panic(fmt.Sprintf("failed to decode GenTx '%s': %s", genTx, err)) } bz, err := txEncodingConfig.TxEncoder()(tx) From fda9a47a451c48da4d616c44314ea8cd9252e11b Mon Sep 17 00:00:00 2001 From: Anil Kumar Kammari Date: Wed, 30 Mar 2022 22:30:44 +0530 Subject: [PATCH 05/10] Update x/genutil/types/genesis_state.go Co-authored-by: Aleksandr Bezobchuk --- x/genutil/types/genesis_state.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/genutil/types/genesis_state.go b/x/genutil/types/genesis_state.go index fc885539057d..fa49a3fafe53 100644 --- a/x/genutil/types/genesis_state.go +++ b/x/genutil/types/genesis_state.go @@ -123,7 +123,7 @@ func ValidateAndGetGenTx(genTx json.RawMessage, txJSONDecoder sdk.TxDecoder) (sd } if err := msgs[0].ValidateBasic(); err != nil { - return tx, fmt.Errorf("invalid gentx: %s, error:%s", msgs[0], err) + return tx, fmt.Errorf("invalid GenTx '%s': %s", msgs[0], err) } return tx, nil From 9193d8e2af57b94c88e6a04441151e248a7eeaf3 Mon Sep 17 00:00:00 2001 From: Anil Kumar Kammari Date: Wed, 30 Mar 2022 22:31:04 +0530 Subject: [PATCH 06/10] Update x/genutil/types/genesis_state.go Co-authored-by: Aleksandr Bezobchuk --- x/genutil/types/genesis_state.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/x/genutil/types/genesis_state.go b/x/genutil/types/genesis_state.go index fa49a3fafe53..8551c0990347 100644 --- a/x/genutil/types/genesis_state.go +++ b/x/genutil/types/genesis_state.go @@ -112,8 +112,7 @@ func ValidateAndGetGenTx(genTx json.RawMessage, txJSONDecoder sdk.TxDecoder) (sd msgs := tx.GetMsgs() if len(msgs) != 1 { - return tx, fmt.Errorf( - "must provide genesis Tx with exactly 1 CreateValidator message, got more in gentx %v", tx) + return tx, fmt.Errorf("unexpected number of GenTx messages; got: %d, expected: 1", len(msgs)) } // TODO: abstract back to staking From 7662bab0b7c42761a9b2aec468a8b731e20efe8c Mon Sep 17 00:00:00 2001 From: Anil Kumar Kammari Date: Wed, 30 Mar 2022 22:31:35 +0530 Subject: [PATCH 07/10] Update x/genutil/types/genesis_state.go Co-authored-by: Aleksandr Bezobchuk --- x/genutil/types/genesis_state.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/x/genutil/types/genesis_state.go b/x/genutil/types/genesis_state.go index 8551c0990347..67cf918ae610 100644 --- a/x/genutil/types/genesis_state.go +++ b/x/genutil/types/genesis_state.go @@ -117,8 +117,7 @@ func ValidateAndGetGenTx(genTx json.RawMessage, txJSONDecoder sdk.TxDecoder) (sd // TODO: abstract back to staking if _, ok := msgs[0].(*stakingtypes.MsgCreateValidator); !ok { - return tx, fmt.Errorf( - "genesis transaction %v does not contain a MsgCreateValidator", tx) + return tx, fmt.Errorf("unexpected GenTx message type; expected: MsgCreateValidator, got: %T", msgs[0]) } if err := msgs[0].ValidateBasic(); err != nil { From bbf1d245ed9a16038d1f31267ea7b849203c7d9d Mon Sep 17 00:00:00 2001 From: anilCSE Date: Wed, 30 Mar 2022 22:47:00 +0530 Subject: [PATCH 08/10] Add godoc --- CHANGELOG.md | 1 + x/genutil/types/genesis_state.go | 2 ++ 2 files changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ba140fcca942..319ea996cfe3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,6 +39,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Features +* (x/genutil) [\#11500](https://github.com/cosmos/cosmos-sdk/pull/11500) Fix GenTx validation and adjust error messages * [\#11430](https://github.com/cosmos/cosmos-sdk/pull/11430) Introduce a new `grpc-only` flag, such that when enabled, will start the node in a query-only mode. Note, gRPC MUST be enabled with this flag. * (x/upgrade) [\#11116](https://github.com/cosmos/cosmos-sdk/pull/11116) `MsgSoftwareUpgrade` and has been added to support v1beta2 msgs-based gov proposals. * (x/bank) [\#11417](https://github.com/cosmos/cosmos-sdk/pull/11417) Introduce a new `SpendableBalances` gRPC query that retrieves an account's total (paginated) spendable balances. diff --git a/x/genutil/types/genesis_state.go b/x/genutil/types/genesis_state.go index 67cf918ae610..4a4dc957555a 100644 --- a/x/genutil/types/genesis_state.go +++ b/x/genutil/types/genesis_state.go @@ -104,6 +104,8 @@ func ValidateGenesis(genesisState *GenesisState, txJSONDecoder sdk.TxDecoder) er return nil } +// ValidateAndGetGenTx validates the genesis transaction and returns GenTx if valid +// it cannot verify the signature as it is stateless validation func ValidateAndGetGenTx(genTx json.RawMessage, txJSONDecoder sdk.TxDecoder) (sdk.Tx, error) { tx, err := txJSONDecoder(genTx) if err != nil { From 59cf1b5d77f888fd84cb3f3bfa39e20611f96d38 Mon Sep 17 00:00:00 2001 From: anilCSE Date: Thu, 31 Mar 2022 17:27:48 +0530 Subject: [PATCH 09/10] replace panic with error --- x/genutil/gentx.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/x/genutil/gentx.go b/x/genutil/gentx.go index 750717eb992c..39fa600750d1 100644 --- a/x/genutil/gentx.go +++ b/x/genutil/gentx.go @@ -100,17 +100,17 @@ func DeliverGenTxs( for _, genTx := range genTxs { tx, err := txEncodingConfig.TxJSONDecoder()(genTx) if err != nil { - panic(fmt.Sprintf("failed to decode GenTx '%s': %s", genTx, err)) + return nil, fmt.Errorf("failed to decode GenTx '%s': %s", genTx, err) } bz, err := txEncodingConfig.TxEncoder()(tx) if err != nil { - panic(fmt.Sprintf("failed to encode GenTx '%s': %s", genTx, err)) + return nil, fmt.Errorf("failed to encode GenTx '%s': %s", genTx, err) } res := deliverTx(abci.RequestDeliverTx{Tx: bz}) if !res.IsOK() { - panic(fmt.Sprintf("failed to execute DelverTx for '%s': %s", genTx, res.Log)) + return nil, fmt.Errorf("failed to execute DelverTx for '%s': %s", genTx, res.Log) } } From 7e5c3845949f465dadddab75cd332811df65b84f Mon Sep 17 00:00:00 2001 From: anilCSE Date: Fri, 1 Apr 2022 00:41:00 +0530 Subject: [PATCH 10/10] Fix test --- x/genutil/gentx_test.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/x/genutil/gentx_test.go b/x/genutil/gentx_test.go index 5d8289810a09..b08ecb9750f2 100644 --- a/x/genutil/gentx_test.go +++ b/x/genutil/gentx_test.go @@ -269,12 +269,12 @@ func (suite *GenTxTestSuite) TestDeliverGenTxs() { ) }) } else { - suite.Require().Panics(func() { - genutil.DeliverGenTxs( - suite.ctx, genTxs, suite.app.StakingKeeper, suite.app.BaseApp.DeliverTx, - suite.encodingConfig.TxConfig, - ) - }) + _, err := genutil.DeliverGenTxs( + suite.ctx, genTxs, suite.app.StakingKeeper, suite.app.BaseApp.DeliverTx, + suite.encodingConfig.TxConfig, + ) + + suite.Require().Error(err) } }) }