Skip to content

Commit eac97c9

Browse files
authored
chore: Using context.Context in x/staking and testutil test helper (#22861)
1 parent b740c7f commit eac97c9

File tree

3 files changed

+29
-14
lines changed

3 files changed

+29
-14
lines changed

Diff for: testutil/configurator/configurator.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ func AuthModule() ModuleOption {
163163
Bech32Prefix: "cosmos",
164164
ModuleAccountPermissions: []*authmodulev1.ModuleAccountPermission{
165165
{Account: "fee_collector"},
166-
{Account: testutil.DistributionModuleName},
166+
{Account: testutil.DistributionModuleName, Permissions: []string{"minter"}},
167167
{Account: testutil.MintModuleName, Permissions: []string{"minter"}},
168168
{Account: "bonded_tokens_pool", Permissions: []string{"burner", testutil.StakingModuleName}},
169169
{Account: "not_bonded_tokens_pool", Permissions: []string{"burner", testutil.StakingModuleName}},
@@ -178,6 +178,18 @@ func AuthModule() ModuleOption {
178178
}
179179
}
180180

181+
func AuthModuleWithMaccPerms(maccPerms []*authmodulev1.ModuleAccountPermission) ModuleOption {
182+
return func(config *Config) {
183+
config.ModuleConfigs[testutil.AuthModuleName] = &appv1alpha1.ModuleConfig{
184+
Name: testutil.AuthModuleName,
185+
Config: appconfig.WrapAny(&authmodulev1.Module{
186+
Bech32Prefix: "cosmos",
187+
ModuleAccountPermissions: maccPerms,
188+
}),
189+
}
190+
}
191+
}
192+
181193
func ParamsModule() ModuleOption {
182194
return func(config *Config) {
183195
config.ModuleConfigs[testutil.ParamsModuleName] = &appv1alpha1.ModuleConfig{

Diff for: testutil/sims/address_helpers.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package sims
22

33
import (
44
"bytes"
5+
"context"
56
"encoding/hex"
67
"fmt"
78
"strconv"
@@ -20,7 +21,7 @@ const mintModuleName = "mint"
2021
type GenerateAccountStrategy func(int) []sdk.AccAddress
2122

2223
// AddTestAddrsFromPubKeys adds the addresses into the SimApp providing only the public keys.
23-
func AddTestAddrsFromPubKeys(bankKeeper BankKeeper, stakingKeeper StakingKeeper, ctx sdk.Context, pubKeys []cryptotypes.PubKey, accAmt math.Int) {
24+
func AddTestAddrsFromPubKeys(bankKeeper BankKeeper, stakingKeeper StakingKeeper, ctx context.Context, pubKeys []cryptotypes.PubKey, accAmt math.Int) {
2425
bondDenom, err := stakingKeeper.BondDenom(ctx)
2526
if err != nil {
2627
panic(err)
@@ -34,16 +35,16 @@ func AddTestAddrsFromPubKeys(bankKeeper BankKeeper, stakingKeeper StakingKeeper,
3435

3536
// AddTestAddrs constructs and returns accNum amount of accounts with an
3637
// initial balance of accAmt in random order
37-
func AddTestAddrs(bankKeeper BankKeeper, stakingKeeper StakingKeeper, ctx sdk.Context, accNum int, accAmt math.Int) []sdk.AccAddress {
38+
func AddTestAddrs(bankKeeper BankKeeper, stakingKeeper StakingKeeper, ctx context.Context, accNum int, accAmt math.Int) []sdk.AccAddress {
3839
return addTestAddrs(bankKeeper, stakingKeeper, ctx, accNum, accAmt, CreateRandomAccounts)
3940
}
4041

4142
// AddTestAddrsIncremental constructs and returns accNum amount of accounts with an initial balance of accAmt in random order
42-
func AddTestAddrsIncremental(bankKeeper BankKeeper, stakingKeeper StakingKeeper, ctx sdk.Context, accNum int, accAmt math.Int) []sdk.AccAddress {
43+
func AddTestAddrsIncremental(bankKeeper BankKeeper, stakingKeeper StakingKeeper, ctx context.Context, accNum int, accAmt math.Int) []sdk.AccAddress {
4344
return addTestAddrs(bankKeeper, stakingKeeper, ctx, accNum, accAmt, CreateIncrementalAccounts)
4445
}
4546

46-
func addTestAddrs(bankKeeper BankKeeper, stakingKeeper StakingKeeper, ctx sdk.Context, accNum int, accAmt math.Int, strategy GenerateAccountStrategy) []sdk.AccAddress {
47+
func addTestAddrs(bankKeeper BankKeeper, stakingKeeper StakingKeeper, ctx context.Context, accNum int, accAmt math.Int, strategy GenerateAccountStrategy) []sdk.AccAddress {
4748
testAddrs := strategy(accNum)
4849
bondDenom, err := stakingKeeper.BondDenom(ctx)
4950
if err != nil {
@@ -58,7 +59,7 @@ func addTestAddrs(bankKeeper BankKeeper, stakingKeeper StakingKeeper, ctx sdk.Co
5859
return testAddrs
5960
}
6061

61-
func initAccountWithCoins(bankKeeper BankKeeper, ctx sdk.Context, addr sdk.AccAddress, coins sdk.Coins) {
62+
func initAccountWithCoins(bankKeeper BankKeeper, ctx context.Context, addr sdk.AccAddress, coins sdk.Coins) {
6263
if err := bankKeeper.MintCoins(ctx, mintModuleName, coins); err != nil {
6364
panic(err)
6465
}

Diff for: x/staking/testutil/helpers.go

+10-8
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ type Helper struct {
2323
msgSrvr stakingtypes.MsgServer
2424
k *keeper.Keeper
2525

26-
Ctx sdk.Context
26+
Ctx context.Context
2727
Commission stakingtypes.CommissionRates
2828
// Coin Denomination
2929
Denom string
3030
}
3131

3232
// NewHelper creates a new instance of Helper.
33-
func NewHelper(t *testing.T, ctx sdk.Context, k *keeper.Keeper) *Helper {
33+
func NewHelper(t *testing.T, ctx context.Context, k *keeper.Keeper) *Helper {
3434
t.Helper()
3535
return &Helper{t, keeper.NewMsgServerImpl(k), k, ctx, ZeroCommission(), sdk.DefaultBondDenom}
3636
}
@@ -125,20 +125,22 @@ func (sh *Helper) CheckValidator(addr sdk.ValAddress, status stakingtypes.BondSt
125125
}
126126

127127
// TurnBlock calls EndBlocker and updates the block time
128-
func (sh *Helper) TurnBlock(newTime time.Time) sdk.Context {
129-
sh.Ctx = sh.Ctx.WithHeaderInfo(header.Info{Time: newTime})
128+
func (sh *Helper) TurnBlock(newTime time.Time) context.Context {
129+
sdkCtx := sdk.UnwrapSDKContext(sh.Ctx)
130+
sh.Ctx = sdkCtx.WithHeaderInfo(header.Info{Time: newTime})
130131
_, err := sh.k.EndBlocker(sh.Ctx)
131132
require.NoError(sh.t, err)
132-
return sh.Ctx
133+
return sdkCtx
133134
}
134135

135136
// TurnBlockTimeDiff calls EndBlocker and updates the block time by adding the
136137
// duration to the current block time
137-
func (sh *Helper) TurnBlockTimeDiff(diff time.Duration) sdk.Context {
138-
sh.Ctx = sh.Ctx.WithHeaderInfo(header.Info{Time: sh.Ctx.HeaderInfo().Time.Add(diff)})
138+
func (sh *Helper) TurnBlockTimeDiff(diff time.Duration) context.Context {
139+
sdkCtx := sdk.UnwrapSDKContext(sh.Ctx)
140+
sh.Ctx = sdkCtx.WithHeaderInfo(header.Info{Time: sdkCtx.HeaderInfo().Time.Add(diff)})
139141
_, err := sh.k.EndBlocker(sh.Ctx)
140142
require.NoError(sh.t, err)
141-
return sh.Ctx
143+
return sdkCtx
142144
}
143145

144146
// ZeroCommission constructs a commission rates with all zeros.

0 commit comments

Comments
 (0)