refactor: move x/auth, x/authz, x/slashing and x/mint Init/Export genesis to keeper (#11871)
This commit is contained in:
parent
710b57c0fb
commit
6e18f582bf
|
@ -1,8 +1,7 @@
|
||||||
package auth
|
package keeper
|
||||||
|
|
||||||
import (
|
import (
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
"github.com/cosmos/cosmos-sdk/x/auth/keeper"
|
|
||||||
"github.com/cosmos/cosmos-sdk/x/auth/types"
|
"github.com/cosmos/cosmos-sdk/x/auth/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -10,7 +9,7 @@ import (
|
||||||
//
|
//
|
||||||
// CONTRACT: old coins from the FeeCollectionKeeper need to be transferred through
|
// CONTRACT: old coins from the FeeCollectionKeeper need to be transferred through
|
||||||
// a genesis port script to the new fee collector account
|
// 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)
|
ak.SetParams(ctx, data.Params)
|
||||||
|
|
||||||
accounts, err := types.UnpackAccounts(data.Accounts)
|
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
|
// 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)
|
params := ak.GetParams(ctx)
|
||||||
|
|
||||||
var genAccounts types.GenesisAccounts
|
var genAccounts types.GenesisAccounts
|
|
@ -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 {
|
func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data json.RawMessage) []abci.ValidatorUpdate {
|
||||||
var genesisState types.GenesisState
|
var genesisState types.GenesisState
|
||||||
cdc.MustUnmarshalJSON(data, &genesisState)
|
cdc.MustUnmarshalJSON(data, &genesisState)
|
||||||
InitGenesis(ctx, am.accountKeeper, genesisState)
|
am.accountKeeper.InitGenesis(ctx, genesisState)
|
||||||
return []abci.ValidatorUpdate{}
|
return []abci.ValidatorUpdate{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExportGenesis returns the exported genesis state as raw bytes for the auth
|
// ExportGenesis returns the exported genesis state as raw bytes for the auth
|
||||||
// module.
|
// module.
|
||||||
func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.RawMessage {
|
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)
|
return cdc.MustMarshalJSON(gs)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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)
|
||||||
|
}
|
|
@ -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) {
|
func (keeper Keeper) getGrantQueueItem(ctx sdk.Context, expiration time.Time, granter, grantee sdk.AccAddress) (*authz.GrantQueueItem, error) {
|
||||||
store := ctx.KVStore(keeper.storeKey)
|
store := ctx.KVStore(keeper.storeKey)
|
||||||
bz := store.Get(GrantQueueKey(expiration, granter, grantee))
|
bz := store.Get(GrantQueueKey(expiration, granter, grantee))
|
||||||
|
|
|
@ -12,7 +12,6 @@ import (
|
||||||
|
|
||||||
"github.com/cosmos/cosmos-sdk/simapp"
|
"github.com/cosmos/cosmos-sdk/simapp"
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
"github.com/cosmos/cosmos-sdk/x/auth"
|
|
||||||
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
||||||
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
|
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
|
||||||
distributiontypes "github.com/cosmos/cosmos-sdk/x/distribution/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, proposal1.Status == v1.StatusDepositPeriod)
|
||||||
require.True(t, proposal2.Status == v1.StatusVotingPeriod)
|
require.True(t, proposal2.Status == v1.StatusVotingPeriod)
|
||||||
|
|
||||||
authGenState := auth.ExportGenesis(ctx, app.AccountKeeper)
|
authGenState := app.AccountKeeper.ExportGenesis(ctx)
|
||||||
bankGenState := app.BankKeeper.ExportGenesis(ctx)
|
bankGenState := app.BankKeeper.ExportGenesis(ctx)
|
||||||
stakingGenState := app.StakingKeeper.ExportGenesis(ctx)
|
stakingGenState := app.StakingKeeper.ExportGenesis(ctx)
|
||||||
distributionGenState := app.DistrKeeper.ExportGenesis(ctx)
|
distributionGenState := app.DistrKeeper.ExportGenesis(ctx)
|
||||||
|
|
|
@ -1,20 +1,19 @@
|
||||||
package mint
|
package keeper
|
||||||
|
|
||||||
import (
|
import (
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
"github.com/cosmos/cosmos-sdk/x/mint/keeper"
|
|
||||||
"github.com/cosmos/cosmos-sdk/x/mint/types"
|
"github.com/cosmos/cosmos-sdk/x/mint/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
// InitGenesis new mint genesis
|
// 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.SetMinter(ctx, data.Minter)
|
||||||
keeper.SetParams(ctx, data.Params)
|
keeper.SetParams(ctx, data.Params)
|
||||||
ak.GetModuleAccount(ctx, types.ModuleName)
|
ak.GetModuleAccount(ctx, types.ModuleName)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExportGenesis returns a GenesisState for a given context and keeper.
|
// 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)
|
minter := keeper.GetMinter(ctx)
|
||||||
params := keeper.GetParams(ctx)
|
params := keeper.GetParams(ctx)
|
||||||
return types.NewGenesisState(minter, params)
|
return types.NewGenesisState(minter, params)
|
|
@ -136,14 +136,14 @@ func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data json.
|
||||||
var genesisState types.GenesisState
|
var genesisState types.GenesisState
|
||||||
cdc.MustUnmarshalJSON(data, &genesisState)
|
cdc.MustUnmarshalJSON(data, &genesisState)
|
||||||
|
|
||||||
InitGenesis(ctx, am.keeper, am.authKeeper, &genesisState)
|
am.keeper.InitGenesis(ctx, am.authKeeper, &genesisState)
|
||||||
return []abci.ValidatorUpdate{}
|
return []abci.ValidatorUpdate{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExportGenesis returns the exported genesis state as raw bytes for the mint
|
// ExportGenesis returns the exported genesis state as raw bytes for the mint
|
||||||
// module.
|
// module.
|
||||||
func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.RawMessage {
|
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)
|
return cdc.MustMarshalJSON(gs)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,15 +1,14 @@
|
||||||
package slashing
|
package keeper
|
||||||
|
|
||||||
import (
|
import (
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
"github.com/cosmos/cosmos-sdk/x/slashing/keeper"
|
|
||||||
"github.com/cosmos/cosmos-sdk/x/slashing/types"
|
"github.com/cosmos/cosmos-sdk/x/slashing/types"
|
||||||
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
|
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
// InitGenesis initialize default parameters
|
// InitGenesis initialize default parameters
|
||||||
// and the keeper's address to pubkey map
|
// 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,
|
stakingKeeper.IterateValidators(ctx,
|
||||||
func(index int64, validator stakingtypes.ValidatorI) bool {
|
func(index int64, validator stakingtypes.ValidatorI) bool {
|
||||||
consPk, err := validator.ConsPubKey()
|
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
|
// ExportGenesis writes the current store values
|
||||||
// to a genesis file, which can be imported again
|
// to a genesis file, which can be imported again
|
||||||
// with InitGenesis
|
// 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)
|
params := keeper.GetParams(ctx)
|
||||||
signingInfos := make([]types.SigningInfo, 0)
|
signingInfos := make([]types.SigningInfo, 0)
|
||||||
missedBlocks := make([]types.ValidatorMissedBlocks, 0)
|
missedBlocks := make([]types.ValidatorMissedBlocks, 0)
|
|
@ -1,4 +1,4 @@
|
||||||
package slashing_test
|
package keeper_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -9,7 +9,6 @@ import (
|
||||||
|
|
||||||
"github.com/cosmos/cosmos-sdk/simapp"
|
"github.com/cosmos/cosmos-sdk/simapp"
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
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/testslashing"
|
||||||
"github.com/cosmos/cosmos-sdk/x/slashing/types"
|
"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[0]), info1)
|
||||||
app.SlashingKeeper.SetValidatorSigningInfo(ctx, sdk.ConsAddress(addrDels[1]), info2)
|
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.Equal(t, genesisState.Params, testslashing.TestParams())
|
||||||
require.Len(t, genesisState.SigningInfos, 2)
|
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]))
|
newInfo1, ok := app.SlashingKeeper.GetValidatorSigningInfo(ctx, sdk.ConsAddress(addrDels[0]))
|
||||||
require.NotEqual(t, info1, newInfo1)
|
require.NotEqual(t, info1, newInfo1)
|
||||||
// Initialise genesis with genesis state before tombstone
|
// 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
|
// Validator isTombstoned should return false as GenesisState is initialised
|
||||||
ok = app.SlashingKeeper.IsTombstoned(ctx, sdk.ConsAddress(addrDels[0]))
|
ok = app.SlashingKeeper.IsTombstoned(ctx, sdk.ConsAddress(addrDels[0]))
|
|
@ -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 {
|
func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data json.RawMessage) []abci.ValidatorUpdate {
|
||||||
var genesisState types.GenesisState
|
var genesisState types.GenesisState
|
||||||
cdc.MustUnmarshalJSON(data, &genesisState)
|
cdc.MustUnmarshalJSON(data, &genesisState)
|
||||||
InitGenesis(ctx, am.keeper, am.stakingKeeper, &genesisState)
|
am.keeper.InitGenesis(ctx, am.stakingKeeper, &genesisState)
|
||||||
return []abci.ValidatorUpdate{}
|
return []abci.ValidatorUpdate{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExportGenesis returns the exported genesis state as raw bytes for the slashing
|
// ExportGenesis returns the exported genesis state as raw bytes for the slashing
|
||||||
// module.
|
// module.
|
||||||
func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.RawMessage {
|
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)
|
return cdc.MustMarshalJSON(gs)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue