Skip to content

Commit

Permalink
Migrate x/genutil to use TxConfig (cosmos#6734)
Browse files Browse the repository at this point in the history
* Update genutil collect and gentx to use TxGenerator

* Remove print statement

* Use Tx in genutil DeliverGenTxs

* Use Tx in genutil genesis_state

* Use Tx in ValidateGenesis

* Use amino txJSONDecoder and txBinaryEncoder in genutil InitGenesis

* Use TxConfig in place of TxGenerator

* Add gentx tests

* Remove commented line

* Test fixes

* Apply suggestions from code review

Co-authored-by: Alexander Bezobchuk <[email protected]>

* Fixes

* Fixes

* Fixes

* Fixes

* Remove unneeded test case (doesn't apply to proto marshaling)

* linting

* Refactor to use new TxEncodingConfig interface in genutil module

* Replace golang/protobuf with gogo/protobuf package

* Use TxEncodingConfig in InitTestnet

* Remove old amino.go file

* Use TxJSONDecoder in genutil ValidateGenesis

* Add parameter to ValidateGenesis to resolve the tx JSON decoder issue

* Address review feedback

Co-authored-by: Jack Zampolin <[email protected]>
Co-authored-by: Aaron Craelius <[email protected]>
Co-authored-by: Aaron Craelius <[email protected]>
Co-authored-by: Alexander Bezobchuk <[email protected]>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
6 people authored Jul 25, 2020
1 parent 3bafd82 commit f59df68
Show file tree
Hide file tree
Showing 40 changed files with 485 additions and 160 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 11 additions & 5 deletions client/tx_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,24 @@ 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
WrapTxBuilder(sdk.Tx) (TxBuilder, error)
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
Expand Down
5 changes: 4 additions & 1 deletion simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
encodingConfig.TxConfig,
),
auth.NewAppModule(appCodec, app.AccountKeeper),
bank.NewAppModule(appCodec, app.BankKeeper, app.AccountKeeper),
capability.NewAppModule(appCodec, *app.CapabilityKeeper),
Expand Down
2 changes: 1 addition & 1 deletion simapp/params/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions simapp/simd/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}),
Expand Down
30 changes: 16 additions & 14 deletions simapp/simd/cmd/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, mbm, genBalIterator, outputDir, chainID, minGasPrices,
nodeDirPrefix, nodeDaemonHome, nodeCLIHome, startingIPAddress, keyringBackend, numValidators,
)
},
Expand All @@ -97,7 +97,8 @@ 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,
nodeCLIHome, startingIPAddress, keyringBackend string, numValidators int,
Expand Down Expand Up @@ -132,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)
Expand All @@ -145,22 +146,22 @@ func InitTestnet(
return err
}

config.Moniker = nodeDirName
nodeConfig.Moniker = nodeDirName

ip, err := getIP(i, startingIPAddress)
if err != nil {
_ = os.RemoveAll(outputDir)
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 {
Expand Down Expand Up @@ -240,7 +241,7 @@ func InitTestnet(
}

err := collectGenFiles(
cdc, config, chainID, nodeIDs, valPubKeys, numValidators,
cdc, txEncodingConfig, nodeConfig, chainID, nodeIDs, valPubKeys, numValidators,
outputDir, nodeDirPrefix, nodeDaemonHome, genBalIterator,
)
if err != nil {
Expand Down Expand Up @@ -294,7 +295,8 @@ func initGenFiles(
}

func collectGenFiles(
cdc codec.JSONMarshaler, config *tmconfig.Config, chainID string,
cdc codec.JSONMarshaler, txEncodingConfig client.TxEncodingConfig,
nodeConfig *tmconfig.Config, chainID string,
nodeIDs []string, valPubKeys []crypto.PubKey,
numValidators int, outputDir, nodeDirPrefix, nodeDaemonHome string,
genBalIterator banktypes.GenesisBalancesIterator,
Expand All @@ -307,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, config, initCfg, *genDoc, genBalIterator)
nodeAppState, err := genutil.GenAppStateFromConfig(cdc, txEncodingConfig, nodeConfig, initCfg, *genDoc, genBalIterator)
if err != nil {
return err
}
Expand All @@ -329,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 {
Expand Down
24 changes: 12 additions & 12 deletions tests/mocks/types_module_module.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 12 additions & 5 deletions testutil/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
require.NoError(t, txBuilder.SetMsgs(createValMsg))
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))

Expand Down
3 changes: 2 additions & 1 deletion testutil/network/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ 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,
tmCfg, initCfg, *genDoc, banktypes.GenesisBalancesIterator{})
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions types/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
}
Expand Down
6 changes: 3 additions & 3 deletions types/module/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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{})

Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion x/auth/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading

0 comments on commit f59df68

Please sign in to comment.