Skip to content

Commit

Permalink
fix(testutil/integration): use only one context in integration test f…
Browse files Browse the repository at this point in the history
…ramework (#22616)

(cherry picked from commit 2d04a1a)

# Conflicts:
#	CHANGELOG.md
#	tests/integration/gov/keeper/keeper_test.go
  • Loading branch information
julienrbrt authored and mergify[bot] committed Nov 25, 2024
1 parent c9451b7 commit 5049c77
Show file tree
Hide file tree
Showing 18 changed files with 133 additions and 175 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,17 @@ Every module contains its own CHANGELOG.md. Please refer to the module you are i

### Improvements

<<<<<<< HEAD
### Bug Fixes
=======
* (testutil/integration) [#22616](https://github.com/cosmos/cosmos-sdk/pull/22616) Remove double context in integration tests v1.
* Use integrationApp.Context() instead of creating a context prior.

### Bug Fixes

* (sims) [#21906](https://github.com/cosmos/cosmos-sdk/pull/21906) Skip sims test when running dry on validators
* (cli) [#21919](https://github.com/cosmos/cosmos-sdk/pull/21919) Query address-by-acc-num by account_id instead of id.
>>>>>>> 2d04a1af2 (fix(testutil/integration): use only one context in integration test framework (#22616))
### API Breaking Changes

Expand Down
22 changes: 6 additions & 16 deletions tests/integration/accounts/fixture_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ type fixture struct {
t *testing.T

app *integration.App

cdc codec.Codec
ctx sdk.Context

authKeeper authkeeper.AccountKeeper
accountsKeeper accounts.Keeper
Expand All @@ -82,7 +80,7 @@ func (f fixture) runBundle(txBytes ...[]byte) *accountsv1.MsgExecuteBundleRespon

msgSrv := accounts.NewMsgServer(f.accountsKeeper)

resp, err := msgSrv.ExecuteBundle(f.ctx, &accountsv1.MsgExecuteBundle{
resp, err := msgSrv.ExecuteBundle(f.app.Context(), &accountsv1.MsgExecuteBundle{
Bundler: f.bundler,
Txs: txBytes,
})
Expand All @@ -93,16 +91,16 @@ func (f fixture) runBundle(txBytes ...[]byte) *accountsv1.MsgExecuteBundleRespon
func (f fixture) mint(address []byte, coins ...sdk.Coin) {
f.t.Helper()
for _, coin := range coins {
err := f.bankKeeper.MintCoins(f.ctx, minttypes.ModuleName, sdk.NewCoins(coin))
err := f.bankKeeper.MintCoins(f.app.Context(), minttypes.ModuleName, sdk.NewCoins(coin))
require.NoError(f.t, err)
err = f.bankKeeper.SendCoinsFromModuleToAccount(f.ctx, minttypes.ModuleName, address, sdk.NewCoins(coin))
err = f.bankKeeper.SendCoinsFromModuleToAccount(f.app.Context(), minttypes.ModuleName, address, sdk.NewCoins(coin))
require.NoError(f.t, err)
}
}

func (f fixture) balance(recipient, denom string) sdk.Coin {
f.t.Helper()
balances, err := f.bankKeeper.Balance(f.ctx, &banktypes.QueryBalanceRequest{
balances, err := f.bankKeeper.Balance(f.app.Context(), &banktypes.QueryBalanceRequest{
Address: recipient,
Denom: denom,
})
Expand All @@ -119,10 +117,6 @@ func initFixture(t *testing.T, f func(ctx context.Context, msg *account_abstract
cdc := encodingCfg.Codec

logger := log.NewTestLogger(t)
cms := integration.CreateMultiStore(keys, logger)

newCtx := sdk.NewContext(cms, true, logger)

router := baseapp.NewMsgServiceRouter()
queryRouter := baseapp.NewGRPCQueryRouter()

Expand Down Expand Up @@ -169,14 +163,11 @@ func initFixture(t *testing.T, f func(ctx context.Context, msg *account_abstract
authority.String(),
)

params := banktypes.DefaultParams()
require.NoError(t, bankKeeper.SetParams(newCtx, params))

accountsModule := accounts.NewAppModule(cdc, accountsKeeper)
authModule := auth.NewAppModule(cdc, authKeeper, accountsKeeper, authsims.RandomGenesisAccounts, nil)
bankModule := bank.NewAppModule(cdc, bankKeeper, authKeeper)

integrationApp := integration.NewIntegrationApp(newCtx, logger, keys, cdc,
integrationApp := integration.NewIntegrationApp(logger, keys, cdc,
encodingCfg.InterfaceRegistry.SigningContext().AddressCodec(),
encodingCfg.InterfaceRegistry.SigningContext().ValidatorAddressCodec(),
map[string]appmodule.AppModule{
Expand All @@ -194,14 +185,13 @@ func initFixture(t *testing.T, f func(ctx context.Context, msg *account_abstract
banktypes.RegisterMsgServer(router, bankkeeper.NewMsgServerImpl(bankKeeper))

// init account
_, addr, err := accountsKeeper.Init(newCtx, "mock", []byte("system"), &gogotypes.Empty{}, nil)
_, addr, err := accountsKeeper.Init(integrationApp.Context(), "mock", []byte("system"), &gogotypes.Empty{}, nil)
require.NoError(t, err)

fixture := &fixture{
t: t,
app: integrationApp,
cdc: cdc,
ctx: newCtx,
authKeeper: authKeeper,
accountsKeeper: accountsKeeper,
bankKeeper: bankKeeper,
Expand Down
22 changes: 11 additions & 11 deletions tests/integration/auth/keeper/accounts_retro_compatibility_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,49 +74,49 @@ func TestAuthToAccountsGRPCCompat(t *testing.T) {

// init three accounts
for n, a := range accs {
_, addr, err := f.accountsKeeper.Init(f.ctx, n, []byte("me"), &gogotypes.Empty{}, nil)
_, addr, err := f.accountsKeeper.Init(f.app.Context(), n, []byte("me"), &gogotypes.Empty{}, nil)
require.NoError(t, err)
a.(*mockRetroCompatAccount).address = addr
}

qs := authkeeper.NewQueryServer(f.authKeeper)

t.Run("account supports info and account query", func(t *testing.T) {
infoResp, err := qs.AccountInfo(f.ctx, &authtypes.QueryAccountInfoRequest{
infoResp, err := qs.AccountInfo(f.app.Context(), &authtypes.QueryAccountInfoRequest{
Address: f.mustAddr(valid.address),
})
require.NoError(t, err)
require.Equal(t, infoResp.Info, valid.retroCompat.Base)

accountResp, err := qs.Account(f.ctx, &authtypes.QueryAccountRequest{
accountResp, err := qs.Account(f.app.Context(), &authtypes.QueryAccountRequest{
Address: f.mustAddr(noInfo.address),
})
require.NoError(t, err)
require.Equal(t, accountResp.Account, valid.retroCompat.Account)
})

t.Run("account only supports account query, not info", func(t *testing.T) {
_, err := qs.AccountInfo(f.ctx, &authtypes.QueryAccountInfoRequest{
_, err := qs.AccountInfo(f.app.Context(), &authtypes.QueryAccountInfoRequest{
Address: f.mustAddr(noInfo.address),
})
require.Error(t, err)
require.Equal(t, status.Code(err), codes.NotFound)

resp, err := qs.Account(f.ctx, &authtypes.QueryAccountRequest{
resp, err := qs.Account(f.app.Context(), &authtypes.QueryAccountRequest{
Address: f.mustAddr(noInfo.address),
})
require.NoError(t, err)
require.Equal(t, resp.Account, valid.retroCompat.Account)
})

t.Run("account does not support any retro compat", func(t *testing.T) {
_, err := qs.AccountInfo(f.ctx, &authtypes.QueryAccountInfoRequest{
_, err := qs.AccountInfo(f.app.Context(), &authtypes.QueryAccountInfoRequest{
Address: f.mustAddr(noImplement.address),
})
require.Error(t, err)
require.Equal(t, status.Code(err), codes.NotFound)

_, err = qs.Account(f.ctx, &authtypes.QueryAccountRequest{
_, err = qs.Account(f.app.Context(), &authtypes.QueryAccountRequest{
Address: f.mustAddr(noImplement.address),
})

Expand All @@ -132,22 +132,22 @@ func TestAccountsBaseAccountRetroCompat(t *testing.T) {
require.NoError(t, err)

// we init two accounts to have account num not be zero.
_, _, err = f.accountsKeeper.Init(f.ctx, "base", []byte("me"), &basev1.MsgInit{PubKey: anyPk}, nil)
_, _, err = f.accountsKeeper.Init(f.app.Context(), "base", []byte("me"), &basev1.MsgInit{PubKey: anyPk}, nil)
require.NoError(t, err)

_, addr, err := f.accountsKeeper.Init(f.ctx, "base", []byte("me"), &basev1.MsgInit{PubKey: anyPk}, nil)
_, addr, err := f.accountsKeeper.Init(f.app.Context(), "base", []byte("me"), &basev1.MsgInit{PubKey: anyPk}, nil)
require.NoError(t, err)

// try to query it via auth
qs := authkeeper.NewQueryServer(f.authKeeper)

r, err := qs.Account(f.ctx, &authtypes.QueryAccountRequest{
r, err := qs.Account(f.app.Context(), &authtypes.QueryAccountRequest{
Address: f.mustAddr(addr),
})
require.NoError(t, err)
require.NotNil(t, r.Account)

info, err := qs.AccountInfo(f.ctx, &authtypes.QueryAccountInfoRequest{
info, err := qs.AccountInfo(f.app.Context(), &authtypes.QueryAccountInfoRequest{
Address: f.mustAddr(addr),
})
require.NoError(t, err)
Expand Down
10 changes: 1 addition & 9 deletions tests/integration/auth/keeper/fixture_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ type fixture struct {
app *integration.App

cdc codec.Codec
ctx sdk.Context

authKeeper authkeeper.AccountKeeper
accountsKeeper accounts.Keeper
Expand All @@ -57,9 +56,6 @@ func initFixture(t *testing.T, extraAccs map[string]accountstd.Interface) *fixtu
cdc := encodingCfg.Codec

logger := log.NewTestLogger(t)
cms := integration.CreateMultiStore(keys, logger)

newCtx := sdk.NewContext(cms, true, logger)

router := baseapp.NewMsgServiceRouter()
queryRouter := baseapp.NewGRPCQueryRouter()
Expand Down Expand Up @@ -109,14 +105,11 @@ func initFixture(t *testing.T, extraAccs map[string]accountstd.Interface) *fixtu
authority.String(),
)

params := banktypes.DefaultParams()
assert.NilError(t, bankKeeper.SetParams(newCtx, params))

accountsModule := accounts.NewAppModule(cdc, accountsKeeper)
authModule := auth.NewAppModule(cdc, authKeeper, accountsKeeper, authsims.RandomGenesisAccounts, nil)
bankModule := bank.NewAppModule(cdc, bankKeeper, authKeeper)

integrationApp := integration.NewIntegrationApp(newCtx, logger, keys, cdc,
integrationApp := integration.NewIntegrationApp(logger, keys, cdc,
encodingCfg.InterfaceRegistry.SigningContext().AddressCodec(),
encodingCfg.InterfaceRegistry.SigningContext().ValidatorAddressCodec(),
map[string]appmodule.AppModule{
Expand All @@ -136,7 +129,6 @@ func initFixture(t *testing.T, extraAccs map[string]accountstd.Interface) *fixtu
return &fixture{
app: integrationApp,
cdc: cdc,
ctx: newCtx,
accountsKeeper: accountsKeeper,
authKeeper: authKeeper,
bankKeeper: bankKeeper,
Expand Down
22 changes: 11 additions & 11 deletions tests/integration/auth/keeper/migrate_x_accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,20 @@ func TestMigrateToAccounts(t *testing.T) {
Name: "cookies",
Permissions: nil,
}
updatedMod := f.authKeeper.NewAccount(f.ctx, modAcc)
f.authKeeper.SetAccount(f.ctx, updatedMod)
updatedMod := f.authKeeper.NewAccount(f.app.Context(), modAcc)
f.authKeeper.SetAccount(f.app.Context(), updatedMod)

// create account
msgSrv := authkeeper.NewMsgServerImpl(f.authKeeper)
privKey := secp256k1.GenPrivKey()
addr := sdk.AccAddress(privKey.PubKey().Address())

acc := f.authKeeper.NewAccountWithAddress(f.ctx, addr)
acc := f.authKeeper.NewAccountWithAddress(f.app.Context(), addr)
require.NoError(t, acc.SetPubKey(privKey.PubKey()))
f.authKeeper.SetAccount(f.ctx, acc)
f.authKeeper.SetAccount(f.app.Context(), acc)

t.Run("account does not exist", func(t *testing.T) {
resp, err := msgSrv.MigrateAccount(f.ctx, &authtypes.MsgMigrateAccount{
resp, err := msgSrv.MigrateAccount(f.app.Context(), &authtypes.MsgMigrateAccount{
Signer: f.mustAddr([]byte("notexist")),
AccountType: "base",
AccountInitMsg: nil,
Expand All @@ -52,7 +52,7 @@ func TestMigrateToAccounts(t *testing.T) {
})

t.Run("invalid account type", func(t *testing.T) {
resp, err := msgSrv.MigrateAccount(f.ctx, &authtypes.MsgMigrateAccount{
resp, err := msgSrv.MigrateAccount(f.app.Context(), &authtypes.MsgMigrateAccount{
Signer: f.mustAddr(updatedMod.GetAddress()),
AccountType: "base",
AccountInitMsg: nil,
Expand All @@ -73,7 +73,7 @@ func TestMigrateToAccounts(t *testing.T) {
initMsgAny, err := codectypes.NewAnyWithValue(migrateMsg)
require.NoError(t, err)

resp, err := msgSrv.MigrateAccount(f.ctx, &authtypes.MsgMigrateAccount{
resp, err := msgSrv.MigrateAccount(f.app.Context(), &authtypes.MsgMigrateAccount{
Signer: f.mustAddr(addr),
AccountType: "base",
AccountInitMsg: initMsgAny,
Expand All @@ -85,15 +85,15 @@ func TestMigrateToAccounts(t *testing.T) {
require.NotNil(t, resp.InitResponse.Value)

// check the account was removed from x/auth and added to x/accounts
require.Nil(t, f.authKeeper.GetAccount(f.ctx, addr))
require.True(t, f.accountsKeeper.IsAccountsModuleAccount(f.ctx, addr))
require.Nil(t, f.authKeeper.GetAccount(f.app.Context(), addr))
require.True(t, f.accountsKeeper.IsAccountsModuleAccount(f.app.Context(), addr))

// check the init information is correctly propagated.
seq, err := f.accountsKeeper.Query(f.ctx, addr, &basev1.QuerySequence{})
seq, err := f.accountsKeeper.Query(f.app.Context(), addr, &basev1.QuerySequence{})
require.NoError(t, err)
require.Equal(t, migrateMsg.InitSequence, seq.(*basev1.QuerySequenceResponse).Sequence)

pkResp, err := f.accountsKeeper.Query(f.ctx, addr, &basev1.QueryPubKey{})
pkResp, err := f.accountsKeeper.Query(f.app.Context(), addr, &basev1.QueryPubKey{})
require.NoError(t, err)
require.Equal(t, migrateMsg.PubKey, pkResp.(*basev1.QueryPubKeyResponse).PubKey)
})
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/auth/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestAsyncExec(t *testing.T) {
addrs := simtestutil.CreateIncrementalAccounts(2)
coins := sdk.NewCoins(sdk.NewCoin("stake", sdkmath.NewInt(10)))

assert.NilError(t, testutil.FundAccount(f.ctx, f.bankKeeper, addrs[0], sdk.NewCoins(sdk.NewInt64Coin("stake", 500))))
assert.NilError(t, testutil.FundAccount(f.app.Context(), f.bankKeeper, addrs[0], sdk.NewCoins(sdk.NewInt64Coin("stake", 500))))

msg := &banktypes.MsgSend{
FromAddress: addrs[0].String(),
Expand Down
8 changes: 1 addition & 7 deletions tests/integration/bank/keeper/deterministic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,6 @@ func initDeterministicFixture(t *testing.T) *deterministicFixture {
cdc := encodingCfg.Codec

logger := log.NewTestLogger(t)
cms := integration.CreateMultiStore(keys, logger)

newCtx := sdk.NewContext(cms, true, logger)

authority := authtypes.NewModuleAddress("gov")

maccPerms := map[string][]string{
Expand Down Expand Up @@ -114,12 +110,10 @@ func initDeterministicFixture(t *testing.T) *deterministicFixture {
authority.String(),
)

assert.NilError(t, bankKeeper.SetParams(newCtx, banktypes.DefaultParams()))

authModule := auth.NewAppModule(cdc, accountKeeper, acctsModKeeper, authsims.RandomGenesisAccounts, nil)
bankModule := bank.NewAppModule(cdc, bankKeeper, accountKeeper)

integrationApp := integration.NewIntegrationApp(newCtx, logger, keys, cdc,
integrationApp := integration.NewIntegrationApp(logger, keys, cdc,
encodingCfg.InterfaceRegistry.SigningContext().AddressCodec(),
encodingCfg.InterfaceRegistry.SigningContext().ValidatorAddressCodec(),
map[string]appmodule.AppModule{
Expand Down
41 changes: 16 additions & 25 deletions tests/integration/distribution/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,6 @@ func initFixture(t *testing.T) *fixture {
cdc := encodingCfg.Codec

logger := log.NewTestLogger(t)
cms := integration.CreateMultiStore(keys, logger)

newCtx := sdk.NewContext(cms, true, logger)

authority := authtypes.NewModuleAddress("gov")

maccPerms := map[string][]string{
Expand Down Expand Up @@ -128,15 +124,12 @@ func initFixture(t *testing.T) *fixture {
authority.String(),
)

assert.NilError(t, bankKeeper.SetParams(newCtx, banktypes.DefaultParams()))

msgRouter := baseapp.NewMsgServiceRouter()
grpcRouter := baseapp.NewGRPCQueryRouter()
cometService := runtime.NewContextAwareCometInfoService()

consensusParamsKeeper := consensusparamkeeper.NewKeeper(cdc, runtime.NewEnvironment(runtime.NewKVStoreService(keys[consensustypes.StoreKey]), log.NewNopLogger(), runtime.EnvWithQueryRouterService(grpcRouter), runtime.EnvWithMsgRouterService(msgRouter)), authtypes.NewModuleAddress("gov").String())
stakingKeeper := stakingkeeper.NewKeeper(cdc, runtime.NewEnvironment(runtime.NewKVStoreService(keys[stakingtypes.StoreKey]), log.NewNopLogger(), runtime.EnvWithQueryRouterService(grpcRouter), runtime.EnvWithMsgRouterService(msgRouter)), accountKeeper, bankKeeper, consensusParamsKeeper, authority.String(), addresscodec.NewBech32Codec(sdk.Bech32PrefixValAddr), addresscodec.NewBech32Codec(sdk.Bech32PrefixConsAddr), cometService)
require.NoError(t, stakingKeeper.Params.Set(newCtx, stakingtypes.DefaultParams()))

poolKeeper := poolkeeper.NewKeeper(cdc, runtime.NewEnvironment(runtime.NewKVStoreService(keys[pooltypes.StoreKey]), log.NewNopLogger()), accountKeeper, bankKeeper, authority.String())

Expand All @@ -155,23 +148,7 @@ func initFixture(t *testing.T) *fixture {
valAddr := sdk.ValAddress(addr)
valConsAddr := sdk.ConsAddress(valConsPk0.Address())

// set proposer and vote infos
ctx := newCtx.WithProposer(valConsAddr).WithCometInfo(comet.Info{
LastCommit: comet.CommitInfo{
Votes: []comet.VoteInfo{
{
Validator: comet.Validator{
Address: valAddr,
Power: 100,
},
BlockIDFlag: comet.BlockIDFlagCommit,
},
},
},
ProposerAddress: valConsAddr,
})

integrationApp := integration.NewIntegrationApp(ctx, logger, keys, cdc,
integrationApp := integration.NewIntegrationApp(logger, keys, cdc,
encodingCfg.InterfaceRegistry.SigningContext().AddressCodec(),
encodingCfg.InterfaceRegistry.SigningContext().ValidatorAddressCodec(),
map[string]appmodule.AppModule{
Expand All @@ -186,7 +163,21 @@ func initFixture(t *testing.T) *fixture {
grpcRouter,
)

sdkCtx := sdk.UnwrapSDKContext(integrationApp.Context())
// set proposer and vote infos
sdkCtx := sdk.UnwrapSDKContext(integrationApp.Context()).WithProposer(valConsAddr).WithCometInfo(comet.Info{
LastCommit: comet.CommitInfo{
Votes: []comet.VoteInfo{
{
Validator: comet.Validator{
Address: valAddr,
Power: 100,
},
BlockIDFlag: comet.BlockIDFlagCommit,
},
},
},
ProposerAddress: valConsAddr,
})

// Register MsgServer and QueryServer
distrtypes.RegisterMsgServer(integrationApp.MsgServiceRouter(), distrkeeper.NewMsgServerImpl(distrKeeper))
Expand Down
Loading

0 comments on commit 5049c77

Please sign in to comment.