Skip to content

Commit

Permalink
refactor: move x/auth, x/authz, x/slashing and x/mint Init/Export gen…
Browse files Browse the repository at this point in the history
…esis to keeper (#11871)
  • Loading branch information
likhita-809 committed May 4, 2022
1 parent 710b57c commit 6e18f58
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 71 deletions.
7 changes: 3 additions & 4 deletions x/auth/genesis.go → x/auth/keeper/genesis.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
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"
)

// InitGenesis - Init store state from genesis data
//
// 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)
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions x/auth/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
53 changes: 53 additions & 0 deletions x/authz/keeper/genesis.go
Original file line number Diff line number Diff line change
@@ -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)
}
47 changes: 0 additions & 47 deletions x/authz/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
3 changes: 1 addition & 2 deletions x/gov/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 3 additions & 4 deletions x/mint/genesis.go → x/mint/keeper/genesis.go
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
4 changes: 2 additions & 2 deletions x/mint/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
7 changes: 3 additions & 4 deletions x/slashing/genesis.go → x/slashing/keeper/genesis.go
Original file line number Diff line number Diff line change
@@ -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()
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package slashing_test
package keeper_test

import (
"testing"
Expand All @@ -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"
)
Expand All @@ -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)
Expand All @@ -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]))
Expand Down
4 changes: 2 additions & 2 deletions x/slashing/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down

0 comments on commit 6e18f58

Please sign in to comment.