Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion testutil/configurator/configurator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}},
Expand All @@ -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{
Expand Down
11 changes: 6 additions & 5 deletions testutil/sims/address_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sims

import (
"bytes"
"context"
"encoding/hex"
"fmt"
"strconv"
Expand All @@ -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)
Expand All @@ -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 {
Expand All @@ -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)
}
Expand Down
18 changes: 10 additions & 8 deletions x/staking/testutil/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}
Expand Down Expand Up @@ -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.
Expand Down
Loading