diff --git a/x/auth/genesis.go b/x/auth/keeper/genesis.go similarity index 81% rename from x/auth/genesis.go rename to x/auth/keeper/genesis.go index 851b58808..50d36381f 100644 --- a/x/auth/genesis.go +++ b/x/auth/keeper/genesis.go @@ -1,8 +1,7 @@ -package auth +package keeper import ( sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/auth/keeper" "github.com/cosmos/cosmos-sdk/x/auth/types" ) @@ -10,7 +9,7 @@ import ( // // CONTRACT: old coins from the FeeCollectionKeeper need to be transferred through // a genesis port script to the new fee collector account -func InitGenesis(ctx sdk.Context, ak keeper.AccountKeeper, data types.GenesisState) { +func (ak AccountKeeper) InitGenesis(ctx sdk.Context, data types.GenesisState) { ak.SetParams(ctx, data.Params) accounts, err := types.UnpackAccounts(data.Accounts) @@ -28,7 +27,7 @@ func InitGenesis(ctx sdk.Context, ak keeper.AccountKeeper, data types.GenesisSta } // ExportGenesis returns a GenesisState for a given context and keeper -func ExportGenesis(ctx sdk.Context, ak keeper.AccountKeeper) *types.GenesisState { +func (ak AccountKeeper) ExportGenesis(ctx sdk.Context) *types.GenesisState { params := ak.GetParams(ctx) var genAccounts types.GenesisAccounts diff --git a/x/auth/module.go b/x/auth/module.go index 58f567fa2..e1b7ceec2 100644 --- a/x/auth/module.go +++ b/x/auth/module.go @@ -136,14 +136,14 @@ func (am AppModule) RegisterServices(cfg module.Configurator) { func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data json.RawMessage) []abci.ValidatorUpdate { var genesisState types.GenesisState cdc.MustUnmarshalJSON(data, &genesisState) - InitGenesis(ctx, am.accountKeeper, genesisState) + am.accountKeeper.InitGenesis(ctx, genesisState) return []abci.ValidatorUpdate{} } // ExportGenesis returns the exported genesis state as raw bytes for the auth // module. func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.RawMessage { - gs := ExportGenesis(ctx, am.accountKeeper) + gs := am.accountKeeper.ExportGenesis(ctx) return cdc.MustMarshalJSON(gs) } diff --git a/x/authz/keeper/genesis.go b/x/authz/keeper/genesis.go new file mode 100644 index 000000000..f2619cba7 --- /dev/null +++ b/x/authz/keeper/genesis.go @@ -0,0 +1,53 @@ +package keeper + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/authz" +) + +// InitGenesis new authz genesis +func (k Keeper) InitGenesis(ctx sdk.Context, data *authz.GenesisState) { + now := ctx.BlockTime() + for _, entry := range data.Authorization { + // ignore expired authorizations + if entry.Expiration != nil && entry.Expiration.Before(now) { + continue + } + + grantee, err := sdk.AccAddressFromBech32(entry.Grantee) + if err != nil { + panic(err) + } + + granter, err := sdk.AccAddressFromBech32(entry.Granter) + if err != nil { + panic(err) + } + + a, ok := entry.Authorization.GetCachedValue().(authz.Authorization) + if !ok { + panic("expected authorization") + } + + err = k.SaveGrant(ctx, grantee, granter, a, entry.Expiration) + if err != nil { + panic(err) + } + } +} + +// ExportGenesis returns a GenesisState for a given context. +func (k Keeper) ExportGenesis(ctx sdk.Context) *authz.GenesisState { + var entries []authz.GrantAuthorization + k.IterateGrants(ctx, func(granter, grantee sdk.AccAddress, grant authz.Grant) bool { + entries = append(entries, authz.GrantAuthorization{ + Granter: granter.String(), + Grantee: grantee.String(), + Expiration: grant.Expiration, + Authorization: grant.Authorization, + }) + return false + }) + + return authz.NewGenesisState(entries) +} diff --git a/x/authz/keeper/keeper.go b/x/authz/keeper/keeper.go index a87375b04..b879f091d 100644 --- a/x/authz/keeper/keeper.go +++ b/x/authz/keeper/keeper.go @@ -259,53 +259,6 @@ func (k Keeper) IterateGrants(ctx sdk.Context, } } -// ExportGenesis returns a GenesisState for a given context. -func (k Keeper) ExportGenesis(ctx sdk.Context) *authz.GenesisState { - var entries []authz.GrantAuthorization - k.IterateGrants(ctx, func(granter, grantee sdk.AccAddress, grant authz.Grant) bool { - entries = append(entries, authz.GrantAuthorization{ - Granter: granter.String(), - Grantee: grantee.String(), - Expiration: grant.Expiration, - Authorization: grant.Authorization, - }) - return false - }) - - return authz.NewGenesisState(entries) -} - -// InitGenesis new authz genesis -func (k Keeper) InitGenesis(ctx sdk.Context, data *authz.GenesisState) { - now := ctx.BlockTime() - for _, entry := range data.Authorization { - // ignore expired authorizations - if entry.Expiration != nil && entry.Expiration.Before(now) { - continue - } - - grantee, err := sdk.AccAddressFromBech32(entry.Grantee) - if err != nil { - panic(err) - } - - granter, err := sdk.AccAddressFromBech32(entry.Granter) - if err != nil { - panic(err) - } - - a, ok := entry.Authorization.GetCachedValue().(authz.Authorization) - if !ok { - panic("expected authorization") - } - - err = k.SaveGrant(ctx, grantee, granter, a, entry.Expiration) - if err != nil { - panic(err) - } - } -} - func (keeper Keeper) getGrantQueueItem(ctx sdk.Context, expiration time.Time, granter, grantee sdk.AccAddress) (*authz.GrantQueueItem, error) { store := ctx.KVStore(keeper.storeKey) bz := store.Get(GrantQueueKey(expiration, granter, grantee)) diff --git a/x/gov/genesis_test.go b/x/gov/genesis_test.go index 073e1b73f..26d933e6f 100644 --- a/x/gov/genesis_test.go +++ b/x/gov/genesis_test.go @@ -12,7 +12,6 @@ import ( "github.com/cosmos/cosmos-sdk/simapp" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/auth" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" distributiontypes "github.com/cosmos/cosmos-sdk/x/distribution/types" @@ -53,7 +52,7 @@ func TestImportExportQueues(t *testing.T) { require.True(t, proposal1.Status == v1.StatusDepositPeriod) require.True(t, proposal2.Status == v1.StatusVotingPeriod) - authGenState := auth.ExportGenesis(ctx, app.AccountKeeper) + authGenState := app.AccountKeeper.ExportGenesis(ctx) bankGenState := app.BankKeeper.ExportGenesis(ctx) stakingGenState := app.StakingKeeper.ExportGenesis(ctx) distributionGenState := app.DistrKeeper.ExportGenesis(ctx) diff --git a/x/mint/genesis.go b/x/mint/keeper/genesis.go similarity index 63% rename from x/mint/genesis.go rename to x/mint/keeper/genesis.go index be7ac61d5..c6a120bee 100644 --- a/x/mint/genesis.go +++ b/x/mint/keeper/genesis.go @@ -1,20 +1,19 @@ -package mint +package keeper import ( sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/mint/keeper" "github.com/cosmos/cosmos-sdk/x/mint/types" ) // InitGenesis new mint genesis -func InitGenesis(ctx sdk.Context, keeper keeper.Keeper, ak types.AccountKeeper, data *types.GenesisState) { +func (keeper Keeper) InitGenesis(ctx sdk.Context, ak types.AccountKeeper, data *types.GenesisState) { keeper.SetMinter(ctx, data.Minter) keeper.SetParams(ctx, data.Params) ak.GetModuleAccount(ctx, types.ModuleName) } // ExportGenesis returns a GenesisState for a given context and keeper. -func ExportGenesis(ctx sdk.Context, keeper keeper.Keeper) *types.GenesisState { +func (keeper Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState { minter := keeper.GetMinter(ctx) params := keeper.GetParams(ctx) return types.NewGenesisState(minter, params) diff --git a/x/mint/module.go b/x/mint/module.go index fc32ff67f..658d49ecd 100644 --- a/x/mint/module.go +++ b/x/mint/module.go @@ -136,14 +136,14 @@ func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data json. var genesisState types.GenesisState cdc.MustUnmarshalJSON(data, &genesisState) - InitGenesis(ctx, am.keeper, am.authKeeper, &genesisState) + am.keeper.InitGenesis(ctx, am.authKeeper, &genesisState) return []abci.ValidatorUpdate{} } // ExportGenesis returns the exported genesis state as raw bytes for the mint // module. func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.RawMessage { - gs := ExportGenesis(ctx, am.keeper) + gs := am.keeper.ExportGenesis(ctx) return cdc.MustMarshalJSON(gs) } diff --git a/x/slashing/genesis.go b/x/slashing/keeper/genesis.go similarity index 87% rename from x/slashing/genesis.go rename to x/slashing/keeper/genesis.go index 2c8b66757..469947ed3 100644 --- a/x/slashing/genesis.go +++ b/x/slashing/keeper/genesis.go @@ -1,15 +1,14 @@ -package slashing +package keeper import ( sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/slashing/keeper" "github.com/cosmos/cosmos-sdk/x/slashing/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" ) // InitGenesis initialize default parameters // and the keeper's address to pubkey map -func InitGenesis(ctx sdk.Context, keeper keeper.Keeper, stakingKeeper types.StakingKeeper, data *types.GenesisState) { +func (keeper Keeper) InitGenesis(ctx sdk.Context, stakingKeeper types.StakingKeeper, data *types.GenesisState) { stakingKeeper.IterateValidators(ctx, func(index int64, validator stakingtypes.ValidatorI) bool { consPk, err := validator.ConsPubKey() @@ -45,7 +44,7 @@ func InitGenesis(ctx sdk.Context, keeper keeper.Keeper, stakingKeeper types.Stak // ExportGenesis writes the current store values // to a genesis file, which can be imported again // with InitGenesis -func ExportGenesis(ctx sdk.Context, keeper keeper.Keeper) (data *types.GenesisState) { +func (keeper Keeper) ExportGenesis(ctx sdk.Context) (data *types.GenesisState) { params := keeper.GetParams(ctx) signingInfos := make([]types.SigningInfo, 0) missedBlocks := make([]types.ValidatorMissedBlocks, 0) diff --git a/x/slashing/genesis_test.go b/x/slashing/keeper/genesis_test.go similarity index 91% rename from x/slashing/genesis_test.go rename to x/slashing/keeper/genesis_test.go index 7cd0792bd..3cea49ea3 100644 --- a/x/slashing/genesis_test.go +++ b/x/slashing/keeper/genesis_test.go @@ -1,4 +1,4 @@ -package slashing_test +package keeper_test import ( "testing" @@ -9,7 +9,6 @@ import ( "github.com/cosmos/cosmos-sdk/simapp" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/slashing" "github.com/cosmos/cosmos-sdk/x/slashing/testslashing" "github.com/cosmos/cosmos-sdk/x/slashing/types" ) @@ -29,7 +28,7 @@ func TestExportAndInitGenesis(t *testing.T) { app.SlashingKeeper.SetValidatorSigningInfo(ctx, sdk.ConsAddress(addrDels[0]), info1) app.SlashingKeeper.SetValidatorSigningInfo(ctx, sdk.ConsAddress(addrDels[1]), info2) - genesisState := slashing.ExportGenesis(ctx, app.SlashingKeeper) + genesisState := app.SlashingKeeper.ExportGenesis(ctx) require.Equal(t, genesisState.Params, testslashing.TestParams()) require.Len(t, genesisState.SigningInfos, 2) @@ -45,7 +44,8 @@ func TestExportAndInitGenesis(t *testing.T) { newInfo1, ok := app.SlashingKeeper.GetValidatorSigningInfo(ctx, sdk.ConsAddress(addrDels[0])) require.NotEqual(t, info1, newInfo1) // Initialise genesis with genesis state before tombstone - slashing.InitGenesis(ctx, app.SlashingKeeper, app.StakingKeeper, genesisState) + + app.SlashingKeeper.InitGenesis(ctx, app.StakingKeeper, genesisState) // Validator isTombstoned should return false as GenesisState is initialised ok = app.SlashingKeeper.IsTombstoned(ctx, sdk.ConsAddress(addrDels[0])) diff --git a/x/slashing/module.go b/x/slashing/module.go index 7627bbf3a..73991883a 100644 --- a/x/slashing/module.go +++ b/x/slashing/module.go @@ -142,14 +142,14 @@ func (am AppModule) RegisterServices(cfg module.Configurator) { func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data json.RawMessage) []abci.ValidatorUpdate { var genesisState types.GenesisState cdc.MustUnmarshalJSON(data, &genesisState) - InitGenesis(ctx, am.keeper, am.stakingKeeper, &genesisState) + am.keeper.InitGenesis(ctx, am.stakingKeeper, &genesisState) return []abci.ValidatorUpdate{} } // ExportGenesis returns the exported genesis state as raw bytes for the slashing // module. func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.RawMessage { - gs := ExportGenesis(ctx, am.keeper) + gs := am.keeper.ExportGenesis(ctx) return cdc.MustMarshalJSON(gs) }