Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Improve gentx validation & error messages #11500

Merged
merged 14 commits into from
Mar 31, 2022
4 changes: 2 additions & 2 deletions x/genutil/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
6 changes: 3 additions & 3 deletions x/genutil/gentx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
anilcse marked this conversation as resolved.
Show resolved Hide resolved
anilcse marked this conversation as resolved.
Show resolved Hide resolved
}

bz, err := txEncodingConfig.TxEncoder()(tx)
if err != nil {
panic(err)
panic(fmt.Sprintf("failed to encode gentx: %s error: %s", genTx, err))
anilcse marked this conversation as resolved.
Show resolved Hide resolved
anilcse marked this conversation as resolved.
Show resolved Hide resolved
}

res := deliverTx(abci.RequestDeliverTx{Tx: bz})
if !res.IsOK() {
panic(res.Log)
panic(fmt.Sprintf("failed to deliverTx: %s error: %s", genTx, res.Log))
anilcse marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
41 changes: 26 additions & 15 deletions x/genutil/types/genesis_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package types

import (
"encoding/json"
"errors"
"fmt"

tmos "github.com/tendermint/tendermint/libs/os"
Expand Down Expand Up @@ -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) {
anilcse marked this conversation as resolved.
Show resolved Hide resolved
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)
anilcse marked this conversation as resolved.
Show resolved Hide resolved
}
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)
anilcse marked this conversation as resolved.
Show resolved Hide resolved
}

if err := msgs[0].ValidateBasic(); err != nil {
return tx, fmt.Errorf("invalid gentx: %s, error:%s", msgs[0], err)
anilcse marked this conversation as resolved.
Show resolved Hide resolved
}

return tx, nil
}