chore: Using context.Context in x/staking and testutil test helper (#22861)

This commit is contained in:
son trinh 2024-12-13 15:54:59 +07:00 committed by GitHub
parent b740c7f341
commit eac97c9055
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 29 additions and 14 deletions

View File

@ -163,7 +163,7 @@ func AuthModule() ModuleOption {
Bech32Prefix: "cosmos",
ModuleAccountPermissions: []*authmodulev1.ModuleAccountPermission{
{Account: "fee_collector"},
{Account: testutil.DistributionModuleName},
{Account: testutil.DistributionModuleName, Permissions: []string{"minter"}},
{Account: testutil.MintModuleName, Permissions: []string{"minter"}},
{Account: "bonded_tokens_pool", Permissions: []string{"burner", testutil.StakingModuleName}},
{Account: "not_bonded_tokens_pool", Permissions: []string{"burner", testutil.StakingModuleName}},
@ -178,6 +178,18 @@ func AuthModule() ModuleOption {
}
}
func AuthModuleWithMaccPerms(maccPerms []*authmodulev1.ModuleAccountPermission) ModuleOption {
return func(config *Config) {
config.ModuleConfigs[testutil.AuthModuleName] = &appv1alpha1.ModuleConfig{
Name: testutil.AuthModuleName,
Config: appconfig.WrapAny(&authmodulev1.Module{
Bech32Prefix: "cosmos",
ModuleAccountPermissions: maccPerms,
}),
}
}
}
func ParamsModule() ModuleOption {
return func(config *Config) {
config.ModuleConfigs[testutil.ParamsModuleName] = &appv1alpha1.ModuleConfig{

View File

@ -2,6 +2,7 @@ package sims
import (
"bytes"
"context"
"encoding/hex"
"fmt"
"strconv"
@ -20,7 +21,7 @@ const mintModuleName = "mint"
type GenerateAccountStrategy func(int) []sdk.AccAddress
// AddTestAddrsFromPubKeys adds the addresses into the SimApp providing only the public keys.
func AddTestAddrsFromPubKeys(bankKeeper BankKeeper, stakingKeeper StakingKeeper, ctx sdk.Context, pubKeys []cryptotypes.PubKey, accAmt math.Int) {
func AddTestAddrsFromPubKeys(bankKeeper BankKeeper, stakingKeeper StakingKeeper, ctx context.Context, pubKeys []cryptotypes.PubKey, accAmt math.Int) {
bondDenom, err := stakingKeeper.BondDenom(ctx)
if err != nil {
panic(err)
@ -34,16 +35,16 @@ func AddTestAddrsFromPubKeys(bankKeeper BankKeeper, stakingKeeper StakingKeeper,
// AddTestAddrs constructs and returns accNum amount of accounts with an
// initial balance of accAmt in random order
func AddTestAddrs(bankKeeper BankKeeper, stakingKeeper StakingKeeper, ctx sdk.Context, accNum int, accAmt math.Int) []sdk.AccAddress {
func AddTestAddrs(bankKeeper BankKeeper, stakingKeeper StakingKeeper, ctx context.Context, accNum int, accAmt math.Int) []sdk.AccAddress {
return addTestAddrs(bankKeeper, stakingKeeper, ctx, accNum, accAmt, CreateRandomAccounts)
}
// AddTestAddrsIncremental constructs and returns accNum amount of accounts with an initial balance of accAmt in random order
func AddTestAddrsIncremental(bankKeeper BankKeeper, stakingKeeper StakingKeeper, ctx sdk.Context, accNum int, accAmt math.Int) []sdk.AccAddress {
func AddTestAddrsIncremental(bankKeeper BankKeeper, stakingKeeper StakingKeeper, ctx context.Context, accNum int, accAmt math.Int) []sdk.AccAddress {
return addTestAddrs(bankKeeper, stakingKeeper, ctx, accNum, accAmt, CreateIncrementalAccounts)
}
func addTestAddrs(bankKeeper BankKeeper, stakingKeeper StakingKeeper, ctx sdk.Context, accNum int, accAmt math.Int, strategy GenerateAccountStrategy) []sdk.AccAddress {
func addTestAddrs(bankKeeper BankKeeper, stakingKeeper StakingKeeper, ctx context.Context, accNum int, accAmt math.Int, strategy GenerateAccountStrategy) []sdk.AccAddress {
testAddrs := strategy(accNum)
bondDenom, err := stakingKeeper.BondDenom(ctx)
if err != nil {
@ -58,7 +59,7 @@ func addTestAddrs(bankKeeper BankKeeper, stakingKeeper StakingKeeper, ctx sdk.Co
return testAddrs
}
func initAccountWithCoins(bankKeeper BankKeeper, ctx sdk.Context, addr sdk.AccAddress, coins sdk.Coins) {
func initAccountWithCoins(bankKeeper BankKeeper, ctx context.Context, addr sdk.AccAddress, coins sdk.Coins) {
if err := bankKeeper.MintCoins(ctx, mintModuleName, coins); err != nil {
panic(err)
}

View File

@ -23,14 +23,14 @@ type Helper struct {
msgSrvr stakingtypes.MsgServer
k *keeper.Keeper
Ctx sdk.Context
Ctx context.Context
Commission stakingtypes.CommissionRates
// Coin Denomination
Denom string
}
// NewHelper creates a new instance of Helper.
func NewHelper(t *testing.T, ctx sdk.Context, k *keeper.Keeper) *Helper {
func NewHelper(t *testing.T, ctx context.Context, k *keeper.Keeper) *Helper {
t.Helper()
return &Helper{t, keeper.NewMsgServerImpl(k), k, ctx, ZeroCommission(), sdk.DefaultBondDenom}
}
@ -125,20 +125,22 @@ func (sh *Helper) CheckValidator(addr sdk.ValAddress, status stakingtypes.BondSt
}
// TurnBlock calls EndBlocker and updates the block time
func (sh *Helper) TurnBlock(newTime time.Time) sdk.Context {
sh.Ctx = sh.Ctx.WithHeaderInfo(header.Info{Time: newTime})
func (sh *Helper) TurnBlock(newTime time.Time) context.Context {
sdkCtx := sdk.UnwrapSDKContext(sh.Ctx)
sh.Ctx = sdkCtx.WithHeaderInfo(header.Info{Time: newTime})
_, err := sh.k.EndBlocker(sh.Ctx)
require.NoError(sh.t, err)
return sh.Ctx
return sdkCtx
}
// TurnBlockTimeDiff calls EndBlocker and updates the block time by adding the
// duration to the current block time
func (sh *Helper) TurnBlockTimeDiff(diff time.Duration) sdk.Context {
sh.Ctx = sh.Ctx.WithHeaderInfo(header.Info{Time: sh.Ctx.HeaderInfo().Time.Add(diff)})
func (sh *Helper) TurnBlockTimeDiff(diff time.Duration) context.Context {
sdkCtx := sdk.UnwrapSDKContext(sh.Ctx)
sh.Ctx = sdkCtx.WithHeaderInfo(header.Info{Time: sdkCtx.HeaderInfo().Time.Add(diff)})
_, err := sh.k.EndBlocker(sh.Ctx)
require.NoError(sh.t, err)
return sh.Ctx
return sdkCtx
}
// ZeroCommission constructs a commission rates with all zeros.