Merge PR #4757: update x/genutil to match module spec

* reestructure genutil

* use auth and staking types packages

* cleanup

* rename pkg

* chacha cha

* update clog tags and pending

* address comments from code review
This commit is contained in:
Federico Kunze 2019-07-24 23:37:03 +02:00 committed by frog power 4000
parent 231fefadb3
commit bfb6445b05
27 changed files with 268 additions and 220 deletions

View File

@ -10,3 +10,4 @@ tags:
- abci
- rest
- cli
- modules

View File

@ -0,0 +1 @@
#4751 update `x/genutils` to match module spec

View File

@ -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 (

View File

@ -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

View File

@ -1,4 +1,4 @@
package genaccounts
package types
import (
"github.com/cosmos/cosmos-sdk/codec"

View File

@ -1,4 +1,4 @@
package genaccounts
package types
import (
sdk "github.com/cosmos/cosmos-sdk/types"

View File

@ -1,4 +1,4 @@
package genaccounts
package types
import (
"errors"

View File

@ -1,4 +1,4 @@
package genaccounts
package types
import (
"errors"

View File

@ -1,4 +1,4 @@
package genaccounts
package types
import (
"encoding/json"

View File

@ -1,4 +1,4 @@
package genaccounts
package types
import (
"testing"

View File

@ -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 (

36
x/genutil/alias.go Normal file
View File

@ -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
)

View File

@ -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",

View File

@ -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)

View File

@ -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()
}

View File

@ -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()

View File

@ -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),
)
}

View File

@ -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

View File

@ -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)
}

View File

@ -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})

View File

@ -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

View File

@ -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
)

22
x/genutil/types/codec.go Normal file
View File

@ -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()
}

View File

@ -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),
)
}

View File

@ -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

View File

@ -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)
}

41
x/genutil/types/types.go Normal file
View File

@ -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,
}
}