diff --git a/.clog.yaml b/.clog.yaml index 8691af6d8318..08f0137919e3 100644 --- a/.clog.yaml +++ b/.clog.yaml @@ -10,3 +10,4 @@ tags: - abci - rest - cli + - modules diff --git a/.pending/improvements/modules/_4751-update-genutil b/.pending/improvements/modules/_4751-update-genutil new file mode 100644 index 000000000000..d1bbd4f15133 --- /dev/null +++ b/.pending/improvements/modules/_4751-update-genutil @@ -0,0 +1 @@ +#4751 update `x/genutils` to match module spec diff --git a/x/genaccounts/alias.go b/x/genaccounts/alias.go index 0ae1f55b317a..159e537f1f27 100644 --- a/x/genaccounts/alias.go +++ b/x/genaccounts/alias.go @@ -5,7 +5,7 @@ package genaccounts import ( - types "github.com/cosmos/cosmos-sdk/x/genaccounts/internal/types" + "github.com/cosmos/cosmos-sdk/x/genaccounts/internal/types" ) const ( diff --git a/x/genaccounts/genesis.go b/x/genaccounts/genesis.go index 1bf793d302a4..495d012ac492 100644 --- a/x/genaccounts/genesis.go +++ b/x/genaccounts/genesis.go @@ -4,7 +4,7 @@ import ( "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" authexported "github.com/cosmos/cosmos-sdk/x/auth/exported" - types "github.com/cosmos/cosmos-sdk/x/genaccounts/internal/types" + "github.com/cosmos/cosmos-sdk/x/genaccounts/internal/types" ) // InitGenesis initializes accounts and deliver genesis transactions diff --git a/x/genaccounts/internal/types/codec.go b/x/genaccounts/internal/types/codec.go index 7bd6913e6c4e..35069bba5f11 100644 --- a/x/genaccounts/internal/types/codec.go +++ b/x/genaccounts/internal/types/codec.go @@ -1,4 +1,4 @@ -package genaccounts +package types import ( "github.com/cosmos/cosmos-sdk/codec" diff --git a/x/genaccounts/internal/types/expected_keepers.go b/x/genaccounts/internal/types/expected_keepers.go index bbaa18f58e93..168082ee9502 100644 --- a/x/genaccounts/internal/types/expected_keepers.go +++ b/x/genaccounts/internal/types/expected_keepers.go @@ -1,4 +1,4 @@ -package genaccounts +package types import ( sdk "github.com/cosmos/cosmos-sdk/types" diff --git a/x/genaccounts/internal/types/genesis_account.go b/x/genaccounts/internal/types/genesis_account.go index c695159989b4..1093c7d55462 100644 --- a/x/genaccounts/internal/types/genesis_account.go +++ b/x/genaccounts/internal/types/genesis_account.go @@ -1,4 +1,4 @@ -package genaccounts +package types import ( "errors" diff --git a/x/genaccounts/internal/types/genesis_account_test.go b/x/genaccounts/internal/types/genesis_account_test.go index 985a74496b59..56dd140ec33a 100644 --- a/x/genaccounts/internal/types/genesis_account_test.go +++ b/x/genaccounts/internal/types/genesis_account_test.go @@ -1,4 +1,4 @@ -package genaccounts +package types import ( "errors" diff --git a/x/genaccounts/internal/types/genesis_state.go b/x/genaccounts/internal/types/genesis_state.go index a9d73607e2d1..c639be138c16 100644 --- a/x/genaccounts/internal/types/genesis_state.go +++ b/x/genaccounts/internal/types/genesis_state.go @@ -1,4 +1,4 @@ -package genaccounts +package types import ( "encoding/json" diff --git a/x/genaccounts/internal/types/genesis_state_test.go b/x/genaccounts/internal/types/genesis_state_test.go index de068f2342c6..90ed5ea8cd65 100644 --- a/x/genaccounts/internal/types/genesis_state_test.go +++ b/x/genaccounts/internal/types/genesis_state_test.go @@ -1,4 +1,4 @@ -package genaccounts +package types import ( "testing" diff --git a/x/genaccounts/module.go b/x/genaccounts/module.go index e502cfb740c5..90c01de8b15e 100644 --- a/x/genaccounts/module.go +++ b/x/genaccounts/module.go @@ -13,7 +13,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/module" "github.com/cosmos/cosmos-sdk/x/auth/exported" - types "github.com/cosmos/cosmos-sdk/x/genaccounts/internal/types" + "github.com/cosmos/cosmos-sdk/x/genaccounts/internal/types" ) var ( diff --git a/x/genutil/alias.go b/x/genutil/alias.go new file mode 100644 index 000000000000..92cd6f78b06f --- /dev/null +++ b/x/genutil/alias.go @@ -0,0 +1,36 @@ +// nolint +// autogenerated code using github.com/rigelrozanski/multitool +// aliases generated for the following subdirectories: +// ALIASGEN: github.com/cosmos/cosmos-sdk/x/genutil/types +package genutil + +import ( + "github.com/cosmos/cosmos-sdk/x/genutil/types" +) + +const ( + ModuleName = types.ModuleName +) + +var ( + // functions aliases + NewGenesisState = types.NewGenesisState + NewGenesisStateFromStdTx = types.NewGenesisStateFromStdTx + NewInitConfig = types.NewInitConfig + GetGenesisStateFromAppState = types.GetGenesisStateFromAppState + SetGenesisStateInAppState = types.SetGenesisStateInAppState + GenesisStateFromGenDoc = types.GenesisStateFromGenDoc + GenesisStateFromGenFile = types.GenesisStateFromGenFile + ValidateGenesis = types.ValidateGenesis + + // variable aliases + ModuleCdc = types.ModuleCdc +) + +type ( + GenesisState = types.GenesisState + AppMap = types.AppMap + MigrationCallback = types.MigrationCallback + MigrationMap = types.MigrationMap + InitConfig = types.InitConfig +) diff --git a/x/genutil/client/cli/collect.go b/x/genutil/client/cli/collect.go index 5b2b9912c040..35cc3581b938 100644 --- a/x/genutil/client/cli/collect.go +++ b/x/genutil/client/cli/collect.go @@ -13,13 +13,14 @@ import ( "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/server" "github.com/cosmos/cosmos-sdk/x/genutil" + "github.com/cosmos/cosmos-sdk/x/genutil/types" ) const flagGenTxDir = "gentx-dir" // CollectGenTxsCmd - return the cobra command to collect genesis transactions func CollectGenTxsCmd(ctx *server.Context, cdc *codec.Codec, - genAccIterator genutil.GenesisAccountsIterator, defaultNodeHome string) *cobra.Command { + genAccIterator types.GenesisAccountsIterator, defaultNodeHome string) *cobra.Command { cmd := &cobra.Command{ Use: "collect-gentxs", diff --git a/x/genutil/client/cli/gentx.go b/x/genutil/client/cli/gentx.go index 2ca9bd6294e5..568542259bc7 100644 --- a/x/genutil/client/cli/gentx.go +++ b/x/genutil/client/cli/gentx.go @@ -29,9 +29,10 @@ import ( "github.com/cosmos/cosmos-sdk/x/auth" "github.com/cosmos/cosmos-sdk/x/genutil" + "github.com/cosmos/cosmos-sdk/x/genutil/types" ) -// helpers for message building gen-tx command +// StakingMsgBuildingHelpers helpers for message building gen-tx command type StakingMsgBuildingHelpers interface { CreateValidatorMsgHelpers(ipDefault string) (fs *flag.FlagSet, nodeIDFlag, pubkeyFlag, amountFlag, defaultsDesc string) PrepareFlagsForTxCreateValidator(config *cfg.Config, nodeID, chainID string, valPubKey crypto.PubKey) @@ -41,7 +42,7 @@ type StakingMsgBuildingHelpers interface { // GenTxCmd builds the application's gentx command. // nolint: errcheck func GenTxCmd(ctx *server.Context, cdc *codec.Codec, mbm module.BasicManager, smbh StakingMsgBuildingHelpers, - genAccIterator genutil.GenesisAccountsIterator, defaultNodeHome, defaultCLIHome string) *cobra.Command { + genAccIterator types.GenesisAccountsIterator, defaultNodeHome, defaultCLIHome string) *cobra.Command { ipDefault, _ := server.ExternalIP() fsCreateValidator, flagNodeID, flagPubKey, flagAmount, defaultsDesc := smbh.CreateValidatorMsgHelpers(ipDefault) diff --git a/x/genutil/codec.go b/x/genutil/codec.go deleted file mode 100644 index a9752fa223c5..000000000000 --- a/x/genutil/codec.go +++ /dev/null @@ -1,22 +0,0 @@ -package genutil - -import ( - "github.com/cosmos/cosmos-sdk/codec" - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/auth" - "github.com/cosmos/cosmos-sdk/x/staking" -) - -// generic sealed codec to be used throughout this module -var moduleCdc *codec.Codec - -// TODO abstract genesis transactions registration back to staking -// required for genesis transactions -func init() { - moduleCdc = codec.New() - staking.RegisterCodec(moduleCdc) - auth.RegisterCodec(moduleCdc) - sdk.RegisterCodec(moduleCdc) - codec.RegisterCrypto(moduleCdc) - moduleCdc.Seal() -} diff --git a/x/genutil/collect.go b/x/genutil/collect.go index fe2d81531fa6..7145efb048a6 100644 --- a/x/genutil/collect.go +++ b/x/genutil/collect.go @@ -13,39 +13,20 @@ import ( "strings" cfg "github.com/tendermint/tendermint/config" - "github.com/tendermint/tendermint/crypto" tmtypes "github.com/tendermint/tendermint/types" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/auth" - "github.com/cosmos/cosmos-sdk/x/staking" + authexported "github.com/cosmos/cosmos-sdk/x/auth/exported" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" + "github.com/cosmos/cosmos-sdk/x/genutil/types" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" ) -// common config options for init -type InitConfig struct { - ChainID string - GenTxsDir string - Name string - NodeID string - ValPubKey crypto.PubKey -} - -// NewInitConfig creates a new InitConfig object -func NewInitConfig(chainID, genTxsDir, name, nodeID string, valPubKey crypto.PubKey) InitConfig { - return InitConfig{ - ChainID: chainID, - GenTxsDir: genTxsDir, - Name: name, - NodeID: nodeID, - ValPubKey: valPubKey, - } -} - -// get the genesis app state from the config +// GenAppStateFromConfig gets the genesis app state from the config func GenAppStateFromConfig(cdc *codec.Codec, config *cfg.Config, initCfg InitConfig, genDoc tmtypes.GenesisDoc, - genAccIterator GenesisAccountsIterator, + genAccIterator types.GenesisAccountsIterator, ) (appState json.RawMessage, err error) { // process genesis transactions, else create default genesis.json @@ -83,30 +64,11 @@ func GenAppStateFromConfig(cdc *codec.Codec, config *cfg.Config, return appState, err } -// Set the genesis transactions int the app genesis state -func SetGenTxsInAppGenesisState(cdc *codec.Codec, appGenesisState map[string]json.RawMessage, - genTxs []auth.StdTx) (map[string]json.RawMessage, error) { - - genesisState := GetGenesisStateFromAppState(cdc, appGenesisState) - // convert all the GenTxs to JSON - var genTxsBz []json.RawMessage - for _, genTx := range genTxs { - txBz, err := cdc.MarshalJSON(genTx) - if err != nil { - return appGenesisState, err - } - genTxsBz = append(genTxsBz, txBz) - } - - genesisState.GenTxs = genTxsBz - return SetGenesisStateInAppState(cdc, appGenesisState, genesisState), nil -} - // CollectStdTxs processes and validates application's genesis StdTxs and returns // the list of appGenTxs, and persistent peers required to generate genesis.json. func CollectStdTxs(cdc *codec.Codec, moniker, genTxsDir string, - genDoc tmtypes.GenesisDoc, genAccIterator GenesisAccountsIterator, -) (appGenTxs []auth.StdTx, persistentPeers string, err error) { + genDoc tmtypes.GenesisDoc, genAccIterator types.GenesisAccountsIterator, +) (appGenTxs []authtypes.StdTx, persistentPeers string, err error) { var fos []os.FileInfo fos, err = ioutil.ReadDir(genTxsDir) @@ -121,9 +83,9 @@ func CollectStdTxs(cdc *codec.Codec, moniker, genTxsDir string, return appGenTxs, persistentPeers, err } - addrMap := make(map[string]auth.Account) + addrMap := make(map[string]authexported.Account) genAccIterator.IterateGenesisAccounts(cdc, appState, - func(acc auth.Account) (stop bool) { + func(acc authexported.Account) (stop bool) { addrMap[acc.GetAddress().String()] = acc return false }, @@ -143,7 +105,7 @@ func CollectStdTxs(cdc *codec.Codec, moniker, genTxsDir string, if jsonRawTx, err = ioutil.ReadFile(filename); err != nil { return appGenTxs, persistentPeers, err } - var genStdTx auth.StdTx + var genStdTx authtypes.StdTx if err = cdc.UnmarshalJSON(jsonRawTx, &genStdTx); err != nil { return appGenTxs, persistentPeers, err } @@ -166,7 +128,7 @@ func CollectStdTxs(cdc *codec.Codec, moniker, genTxsDir string, } // TODO abstract out staking message validation back to staking - msg := msgs[0].(staking.MsgCreateValidator) + msg := msgs[0].(stakingtypes.MsgCreateValidator) // validate delegator and validator addresses and funds against the accounts in the state delAddr := msg.DelegatorAddress.String() valAddr := sdk.AccAddress(msg.ValidatorAddress).String() diff --git a/x/genutil/expected.go b/x/genutil/expected.go deleted file mode 100644 index 9fd28cf8d894..000000000000 --- a/x/genutil/expected.go +++ /dev/null @@ -1,32 +0,0 @@ -package genutil - -import ( - "encoding/json" - - 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/auth" -) - -// expected staking keeper -type StakingKeeper interface { - ApplyAndReturnValidatorSetUpdates(sdk.Context) (updates []abci.ValidatorUpdate) -} - -// expected account keeper -type AccountKeeper interface { - NewAccount(sdk.Context, auth.Account) auth.Account - SetAccount(sdk.Context, auth.Account) - IterateAccounts(ctx sdk.Context, process func(auth.Account) (stop bool)) -} - -// The expected interface for iterating genesis accounts object -type GenesisAccountsIterator interface { - IterateGenesisAccounts( - cdc *codec.Codec, - appGenesis map[string]json.RawMessage, - iterateFn func(auth.Account) (stop bool), - ) -} diff --git a/x/genutil/init.go b/x/genutil/genesis.go similarity index 77% rename from x/genutil/init.go rename to x/genutil/genesis.go index 786838719f40..c1a06a8b9aff 100644 --- a/x/genutil/init.go +++ b/x/genutil/genesis.go @@ -5,10 +5,11 @@ import ( "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/genutil/types" ) -// initialize accounts and deliver genesis transactions -func InitGenesis(ctx sdk.Context, cdc *codec.Codec, stakingKeeper StakingKeeper, +// InitGenesis - initialize accounts and deliver genesis transactions +func InitGenesis(ctx sdk.Context, cdc *codec.Codec, stakingKeeper types.StakingKeeper, deliverTx deliverTxfn, genesisState GenesisState) []abci.ValidatorUpdate { var validators []abci.ValidatorUpdate diff --git a/x/genutil/genesis_state_test.go b/x/genutil/genesis_state_test.go deleted file mode 100644 index 78175f214c76..000000000000 --- a/x/genutil/genesis_state_test.go +++ /dev/null @@ -1,47 +0,0 @@ -package genutil - -import ( - "testing" - - "github.com/stretchr/testify/require" - "github.com/tendermint/tendermint/crypto/ed25519" - - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/auth" - "github.com/cosmos/cosmos-sdk/x/staking" -) - -var ( - pk1 = ed25519.GenPrivKey().PubKey() - pk2 = ed25519.GenPrivKey().PubKey() -) - -func TestValidateGenesisMultipleMessages(t *testing.T) { - - desc := staking.NewDescription("testname", "", "", "") - comm := staking.CommissionRates{} - - msg1 := staking.NewMsgCreateValidator(sdk.ValAddress(pk1.Address()), pk1, - sdk.NewInt64Coin(sdk.DefaultBondDenom, 50), desc, comm, sdk.OneInt()) - - msg2 := staking.NewMsgCreateValidator(sdk.ValAddress(pk2.Address()), pk2, - sdk.NewInt64Coin(sdk.DefaultBondDenom, 50), desc, comm, sdk.OneInt()) - - genTxs := auth.NewStdTx([]sdk.Msg{msg1, msg2}, auth.StdFee{}, nil, "") - genesisState := NewGenesisStateFromStdTx([]auth.StdTx{genTxs}) - - err := ValidateGenesis(genesisState) - require.Error(t, err) -} - -func TestValidateGenesisBadMessage(t *testing.T) { - desc := staking.NewDescription("testname", "", "", "") - - msg1 := staking.NewMsgEditValidator(sdk.ValAddress(pk1.Address()), desc, nil, nil) - - genTxs := auth.NewStdTx([]sdk.Msg{msg1}, auth.StdFee{}, nil, "") - genesisState := NewGenesisStateFromStdTx([]auth.StdTx{genTxs}) - - err := ValidateGenesis(genesisState) - require.Error(t, err) -} diff --git a/x/genutil/gentx.go b/x/genutil/gentx.go index 0dc14de225a8..c9f55c3e8447 100644 --- a/x/genutil/gentx.go +++ b/x/genutil/gentx.go @@ -10,31 +10,52 @@ import ( "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/auth" - "github.com/cosmos/cosmos-sdk/x/staking" + authexported "github.com/cosmos/cosmos-sdk/x/auth/exported" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" + "github.com/cosmos/cosmos-sdk/x/genutil/types" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" ) +// SetGenTxsInAppGenesisState - sets the genesis transactions in the app genesis state +func SetGenTxsInAppGenesisState(cdc *codec.Codec, appGenesisState map[string]json.RawMessage, + genTxs []authtypes.StdTx) (map[string]json.RawMessage, error) { + + genesisState := GetGenesisStateFromAppState(cdc, appGenesisState) + // convert all the GenTxs to JSON + var genTxsBz []json.RawMessage + for _, genTx := range genTxs { + txBz, err := cdc.MarshalJSON(genTx) + if err != nil { + return appGenesisState, err + } + genTxsBz = append(genTxsBz, txBz) + } + + genesisState.GenTxs = genTxsBz + return SetGenesisStateInAppState(cdc, appGenesisState, genesisState), nil +} + // ValidateAccountInGenesis checks that the provided key has sufficient // coins in the genesis accounts func ValidateAccountInGenesis(appGenesisState map[string]json.RawMessage, - genAccIterator GenesisAccountsIterator, + genAccIterator types.GenesisAccountsIterator, key sdk.AccAddress, coins sdk.Coins, cdc *codec.Codec) error { accountIsInGenesis := false - // TODO refactor out bond denom to common state area - stakingDataBz := appGenesisState[staking.ModuleName] - var stakingData staking.GenesisState + // TODO: refactor out bond denom to common state area + stakingDataBz := appGenesisState[stakingtypes.ModuleName] + var stakingData stakingtypes.GenesisState cdc.MustUnmarshalJSON(stakingDataBz, &stakingData) bondDenom := stakingData.Params.BondDenom - genUtilDataBz := appGenesisState[staking.ModuleName] + genUtilDataBz := appGenesisState[stakingtypes.ModuleName] var genesisState GenesisState cdc.MustUnmarshalJSON(genUtilDataBz, &genesisState) var err error genAccIterator.IterateGenesisAccounts(cdc, appGenesisState, - func(acc auth.Account) (stop bool) { + func(acc authexported.Account) (stop bool) { accAddress := acc.GetAddress() accCoins := acc.GetCoins() @@ -68,12 +89,12 @@ func ValidateAccountInGenesis(appGenesisState map[string]json.RawMessage, type deliverTxfn func(abci.RequestDeliverTx) abci.ResponseDeliverTx -// deliver a genesis transaction +// DeliverGenTxs - deliver a genesis transaction func DeliverGenTxs(ctx sdk.Context, cdc *codec.Codec, genTxs []json.RawMessage, - stakingKeeper StakingKeeper, deliverTx deliverTxfn) []abci.ValidatorUpdate { + stakingKeeper types.StakingKeeper, deliverTx deliverTxfn) []abci.ValidatorUpdate { for _, genTx := range genTxs { - var tx auth.StdTx + var tx authtypes.StdTx cdc.MustUnmarshalJSON(genTx, &tx) bz := cdc.MustMarshalBinaryLengthPrefixed(tx) res := deliverTx(abci.RequestDeliverTx{Tx: bz}) diff --git a/x/genutil/module.go b/x/genutil/module.go index a8fab323d16e..f6051fccb549 100644 --- a/x/genutil/module.go +++ b/x/genutil/module.go @@ -12,6 +12,7 @@ import ( "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/module" + "github.com/cosmos/cosmos-sdk/x/genutil/types" ) var ( @@ -19,9 +20,6 @@ var ( _ module.AppModuleBasic = AppModuleBasic{} ) -// module name -const ModuleName = "genutil" - // app module basics object type AppModuleBasic struct{} @@ -35,13 +33,13 @@ func (AppModuleBasic) RegisterCodec(cdc *codec.Codec) {} // default genesis state func (AppModuleBasic) DefaultGenesis() json.RawMessage { - return moduleCdc.MustMarshalJSON(GenesisState{}) + return ModuleCdc.MustMarshalJSON(GenesisState{}) } // module validate genesis func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { var data GenesisState - err := moduleCdc.UnmarshalJSON(bz, &data) + err := ModuleCdc.UnmarshalJSON(bz, &data) if err != nil { return err } @@ -61,14 +59,14 @@ func (AppModuleBasic) GetQueryCmd(_ *codec.Codec) *cobra.Command { return nil } // app module type AppModule struct { AppModuleBasic - accountKeeper AccountKeeper - stakingKeeper StakingKeeper + accountKeeper types.AccountKeeper + stakingKeeper types.StakingKeeper deliverTx deliverTxfn } // NewAppModule creates a new AppModule object -func NewAppModule(accountKeeper AccountKeeper, - stakingKeeper StakingKeeper, deliverTx deliverTxfn) module.AppModule { +func NewAppModule(accountKeeper types.AccountKeeper, + stakingKeeper types.StakingKeeper, deliverTx deliverTxfn) module.AppModule { return module.NewGenesisOnlyAppModule(AppModule{ AppModuleBasic: AppModuleBasic{}, @@ -81,8 +79,8 @@ func NewAppModule(accountKeeper AccountKeeper, // module init-genesis func (am AppModule) InitGenesis(ctx sdk.Context, data json.RawMessage) []abci.ValidatorUpdate { var genesisState GenesisState - moduleCdc.MustUnmarshalJSON(data, &genesisState) - return InitGenesis(ctx, moduleCdc, am.stakingKeeper, am.deliverTx, genesisState) + ModuleCdc.MustUnmarshalJSON(data, &genesisState) + return InitGenesis(ctx, ModuleCdc, am.stakingKeeper, am.deliverTx, genesisState) } // module export genesis diff --git a/x/genutil/types.go b/x/genutil/types.go deleted file mode 100644 index 88c304a38f62..000000000000 --- a/x/genutil/types.go +++ /dev/null @@ -1,14 +0,0 @@ -package genutil - -import ( - "encoding/json" -) - -type ( - // AppMap map modules names with their json raw representation - AppMap map[string]json.RawMessage - // MigrationCallback converts a genesis map from the previous version to the targeted one - MigrationCallback func(AppMap) AppMap - // MigrationMap defines a mapping from a version to a MigrationCallback - MigrationMap map[string]MigrationCallback -) diff --git a/x/genutil/types/codec.go b/x/genutil/types/codec.go new file mode 100644 index 000000000000..624be93d138f --- /dev/null +++ b/x/genutil/types/codec.go @@ -0,0 +1,22 @@ +package types + +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" +) + +// ModuleCdc defines a generic sealed codec to be used throughout this module +var ModuleCdc *codec.Codec + +// TODO: abstract genesis transactions registration back to staking +// required for genesis transactions +func init() { + ModuleCdc = codec.New() + stakingtypes.RegisterCodec(ModuleCdc) + authtypes.RegisterCodec(ModuleCdc) + sdk.RegisterCodec(ModuleCdc) + codec.RegisterCrypto(ModuleCdc) + ModuleCdc.Seal() +} diff --git a/x/genutil/types/expected_keepers.go b/x/genutil/types/expected_keepers.go new file mode 100644 index 000000000000..b44236d7d8d1 --- /dev/null +++ b/x/genutil/types/expected_keepers.go @@ -0,0 +1,32 @@ +package types + +import ( + "encoding/json" + + abci "github.com/tendermint/tendermint/abci/types" + + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + authexported "github.com/cosmos/cosmos-sdk/x/auth/exported" +) + +// StakingKeeper defines the expected staking keeper (noalias) +type StakingKeeper interface { + ApplyAndReturnValidatorSetUpdates(sdk.Context) (updates []abci.ValidatorUpdate) +} + +// AccountKeeper defines the expected account keeper (noalias) +type AccountKeeper interface { + NewAccount(sdk.Context, authexported.Account) authexported.Account + SetAccount(sdk.Context, authexported.Account) + IterateAccounts(ctx sdk.Context, process func(authexported.Account) (stop bool)) +} + +// GenesisAccountsIterator defines the expected iterating genesis accounts object (noalias) +type GenesisAccountsIterator interface { + IterateGenesisAccounts( + cdc *codec.Codec, + appGenesis map[string]json.RawMessage, + iterateFn func(authexported.Account) (stop bool), + ) +} diff --git a/x/genutil/genesis_state.go b/x/genutil/types/genesis_state.go similarity index 68% rename from x/genutil/genesis_state.go rename to x/genutil/types/genesis_state.go index 4f4b47e54571..e568bb42076a 100644 --- a/x/genutil/genesis_state.go +++ b/x/genutil/types/genesis_state.go @@ -1,4 +1,4 @@ -package genutil +package types import ( "encoding/json" @@ -9,11 +9,11 @@ import ( tmtypes "github.com/tendermint/tendermint/types" "github.com/cosmos/cosmos-sdk/codec" - "github.com/cosmos/cosmos-sdk/x/auth" - "github.com/cosmos/cosmos-sdk/x/staking" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" ) -// State to Unmarshal +// GenesisState defines the raw genesis transaction in JSON type GenesisState struct { GenTxs []json.RawMessage `json:"gentxs" yaml:"gentxs"` } @@ -27,17 +27,15 @@ func NewGenesisState(genTxs []json.RawMessage) GenesisState { // NewGenesisStateFromStdTx creates a new GenesisState object // from auth transactions -func NewGenesisStateFromStdTx(genTxs []auth.StdTx) GenesisState { +func NewGenesisStateFromStdTx(genTxs []authtypes.StdTx) GenesisState { genTxsBz := make([]json.RawMessage, len(genTxs)) for i, genTx := range genTxs { - genTxsBz[i] = moduleCdc.MustMarshalJSON(genTx) - } - return GenesisState{ - GenTxs: genTxsBz, + genTxsBz[i] = ModuleCdc.MustMarshalJSON(genTx) } + return NewGenesisState(genTxsBz) } -// get the genutil genesis state from the expected app state +// GetGenesisStateFromAppState gets the genutil genesis state from the expected app state func GetGenesisStateFromAppState(cdc *codec.Codec, appState map[string]json.RawMessage) GenesisState { var genesisState GenesisState if appState[ModuleName] != nil { @@ -46,7 +44,7 @@ func GetGenesisStateFromAppState(cdc *codec.Codec, appState map[string]json.RawM return genesisState } -// set the genutil genesis state within the expected app state +// SetGenesisStateInAppState sets the genutil genesis state within the expected app state func SetGenesisStateInAppState(cdc *codec.Codec, appState map[string]json.RawMessage, genesisState GenesisState) map[string]json.RawMessage { @@ -55,7 +53,8 @@ func SetGenesisStateInAppState(cdc *codec.Codec, return appState } -// Create the core parameters for genesis initialization for the application. +// GenesisStateFromGenDoc creates the core parameters for genesis initialization +// for the application. // // NOTE: The pubkey input is this machines pubkey. func GenesisStateFromGenDoc(cdc *codec.Codec, genDoc tmtypes.GenesisDoc, @@ -67,7 +66,8 @@ func GenesisStateFromGenDoc(cdc *codec.Codec, genDoc tmtypes.GenesisDoc, return genesisState, nil } -// Create the core parameters for genesis initialization for the application. +// GenesisStateFromGenFile creates the core parameters for genesis initialization +// for the application. // // NOTE: The pubkey input is this machines pubkey. func GenesisStateFromGenFile(cdc *codec.Codec, genFile string, @@ -86,11 +86,11 @@ func GenesisStateFromGenFile(cdc *codec.Codec, genFile string, return genesisState, genDoc, err } -// validate GenTx transactions +// ValidateGenesis validates GenTx transactions func ValidateGenesis(genesisState GenesisState) error { for i, genTx := range genesisState.GenTxs { - var tx auth.StdTx - if err := moduleCdc.UnmarshalJSON(genTx, &tx); err != nil { + var tx authtypes.StdTx + if err := ModuleCdc.UnmarshalJSON(genTx, &tx); err != nil { return err } @@ -100,10 +100,10 @@ func ValidateGenesis(genesisState GenesisState) error { "must provide genesis StdTx with exactly 1 CreateValidator message") } - // TODO abstract back to staking - if _, ok := msgs[0].(staking.MsgCreateValidator); !ok { + // TODO: abstract back to staking + if _, ok := msgs[0].(stakingtypes.MsgCreateValidator); !ok { return fmt.Errorf( - "Genesis transaction %v does not contain a MsgCreateValidator", i) + "genesis transaction %v does not contain a MsgCreateValidator", i) } } return nil diff --git a/x/genutil/types/genesis_state_test.go b/x/genutil/types/genesis_state_test.go new file mode 100644 index 000000000000..fb15d243521b --- /dev/null +++ b/x/genutil/types/genesis_state_test.go @@ -0,0 +1,46 @@ +package types + +import ( + "testing" + + 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" + "github.com/stretchr/testify/require" + "github.com/tendermint/tendermint/crypto/ed25519" +) + +var ( + pk1 = ed25519.GenPrivKey().PubKey() + pk2 = ed25519.GenPrivKey().PubKey() +) + +func TestValidateGenesisMultipleMessages(t *testing.T) { + + 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()), 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}) + + err := ValidateGenesis(genesisState) + require.Error(t, err) +} + +func TestValidateGenesisBadMessage(t *testing.T) { + desc := stakingtypes.NewDescription("testname", "", "", "") + + msg1 := stakingtypes.NewMsgEditValidator(sdk.ValAddress(pk1.Address()), desc, nil, nil) + + genTxs := authtypes.NewStdTx([]sdk.Msg{msg1}, authtypes.StdFee{}, nil, "") + genesisState := NewGenesisStateFromStdTx([]authtypes.StdTx{genTxs}) + + err := ValidateGenesis(genesisState) + require.Error(t, err) +} diff --git a/x/genutil/types/types.go b/x/genutil/types/types.go new file mode 100644 index 000000000000..f98eb92f70e7 --- /dev/null +++ b/x/genutil/types/types.go @@ -0,0 +1,41 @@ +package types + +import ( + "encoding/json" + + "github.com/tendermint/tendermint/crypto" +) + +// DONTCOVER + +type ( + // AppMap map modules names with their json raw representation + AppMap map[string]json.RawMessage + // MigrationCallback converts a genesis map from the previous version to the targeted one + MigrationCallback func(AppMap) AppMap + // MigrationMap defines a mapping from a version to a MigrationCallback + MigrationMap map[string]MigrationCallback +) + +// ModuleName is genutil +const ModuleName = "genutil" + +// InitConfig common config options for init +type InitConfig struct { + ChainID string + GenTxsDir string + Name string + NodeID string + ValPubKey crypto.PubKey +} + +// NewInitConfig creates a new InitConfig object +func NewInitConfig(chainID, genTxsDir, name, nodeID string, valPubKey crypto.PubKey) InitConfig { + return InitConfig{ + ChainID: chainID, + GenTxsDir: genTxsDir, + Name: name, + NodeID: nodeID, + ValPubKey: valPubKey, + } +}