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

Migrate x/genutil to use TxConfig #6734

Merged
merged 44 commits into from
Jul 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
ab26eac
Update genutil collect and gentx to use TxGenerator
blushi Jul 15, 2020
aa5fd30
Remove print statement
blushi Jul 15, 2020
fd5d400
Use Tx in genutil DeliverGenTxs
blushi Jul 16, 2020
4259efc
Use Tx in genutil genesis_state
blushi Jul 16, 2020
cec4fa6
Use Tx in ValidateGenesis
blushi Jul 16, 2020
44a5ec0
Merge branch 'master' into marie/5917-x-genutil
blushi Jul 16, 2020
b02117c
Use amino txJSONDecoder and txBinaryEncoder in genutil InitGenesis
blushi Jul 17, 2020
406515c
Merge branch 'master' into marie/5917-x-genutil
blushi Jul 17, 2020
b1692af
Merge branch 'master' into marie/5917-x-genutil
jackzampolin Jul 21, 2020
2ae43ad
Merge branch 'master' into marie/5917-x-genutil
blushi Jul 22, 2020
f40eb9c
Use TxConfig in place of TxGenerator
blushi Jul 22, 2020
9e31db9
Add gentx tests
blushi Jul 23, 2020
7e394b0
Merge branch 'master' into marie/5917-x-genutil
blushi Jul 23, 2020
51a7e8a
Remove commented line
blushi Jul 23, 2020
f14d527
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into ma…
aaronc Jul 23, 2020
6619018
Test fixes
aaronc Jul 23, 2020
10d9153
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into aa…
aaronc Jul 24, 2020
4d5181b
Apply suggestions from code review
aaronc Jul 24, 2020
380b369
Merge remote-tracking branch 'origin/aaronc/6734-updates' into aaronc…
aaronc Jul 24, 2020
1f1e38a
Merge branch 'master' into marie/5917-x-genutil
aaronc Jul 24, 2020
0896def
Fixes
aaronc Jul 24, 2020
77b1c22
Merge branch 'marie/5917-x-genutil' of https://github.com/cosmos/cosm…
aaronc Jul 24, 2020
391dcdc
Fixes
aaronc Jul 24, 2020
9b77a01
Fixes
aaronc Jul 24, 2020
3e6fc8d
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into aa…
aaronc Jul 24, 2020
ee904d5
Merge branch 'master' into marie/5917-x-genutil
aaronc Jul 24, 2020
22b1dcd
Merge branch 'marie/5917-x-genutil' of https://github.com/cosmos/cosm…
aaronc Jul 24, 2020
8545cf3
Fixes
aaronc Jul 24, 2020
0ac6d28
Remove unneeded test case (doesn't apply to proto marshaling)
aaronc Jul 24, 2020
b2dd715
linting
blushi Jul 24, 2020
6a6ae20
Merge branch 'master' into marie/5917-x-genutil
blushi Jul 24, 2020
8d463a8
Refactor to use new TxEncodingConfig interface in genutil module
blushi Jul 24, 2020
59064d9
Replace golang/protobuf with gogo/protobuf package
blushi Jul 24, 2020
1203a83
Use TxEncodingConfig in InitTestnet
blushi Jul 24, 2020
5070cd2
Remove old amino.go file
blushi Jul 24, 2020
5242be2
Use TxJSONDecoder in genutil ValidateGenesis
blushi Jul 24, 2020
72d0d15
Merge branch 'master' into marie/5917-x-genutil
aaronc Jul 24, 2020
5f3f0ed
Merge branch 'master' into marie/5917-x-genutil
aaronc Jul 24, 2020
6a9c975
Add parameter to ValidateGenesis to resolve the tx JSON decoder issue
aaronc Jul 25, 2020
62f9fad
Merge remote-tracking branch 'origin/marie/5917-x-genutil' into marie…
aaronc Jul 25, 2020
fa30ce3
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into ma…
aaronc Jul 25, 2020
eb633a6
Address review feedback
aaronc Jul 25, 2020
37f09cc
Merge branch 'master' into marie/5917-x-genutil
mergify[bot] Jul 25, 2020
abb0c11
Merge branch 'master' into marie/5917-x-genutil
mergify[bot] Jul 25, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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