Skip to content

Commit

Permalink
refactor(test): remove header dependence from tests (#16342)
Browse files Browse the repository at this point in the history
Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com>
  • Loading branch information
tac0turtle and alexanderbez authored May 31, 2023
1 parent 2208693 commit 5097b0c
Show file tree
Hide file tree
Showing 53 changed files with 140 additions and 149 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,10 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (x/crisis) [#16328](https://github.com/cosmos/cosmos-sdk/pull/16328) Use collections for state management:
* Removed: keeper `GetConstantFee`, `SetConstantFee`
* (x/mint) [#16329](https://github.com/cosmos/cosmos-sdk/pull/16329) Use collections for state management:
* Removed: keeper `GetParams`, `SetParams`, `GetMinter`, `SetMinter`.
* Removed: keeper `GetParams`, `SetParams`, `GetMinter`, `SetMinter`.
* (x/*all*) [#16052](https://github.com/cosmos/cosmos-sdk/pull/16062) `GetSignBytes` implementations on messages and global legacy amino codec definitions have been removed from all modules.
* (sims) [#16052](https://github.com/cosmos/cosmos-sdk/pull/16062) `GetOrGenerate` no longer requires a codec argument is now 4-arity instead of 5-arity.
* (baseapp) [#16342](https://github.com/cosmos/cosmos-sdk/pull/16342) NewContext was renamed to NewContextLegacy. The replacement (NewContext) now does not take a header, instead you should set the header via `WithHeaderInfo` or `WithBlockHeight`. Note that `WithBlockHeight` will soon be depreacted and its recommneded to use `WithHeaderInfo`

### Client Breaking Changes

Expand Down
2 changes: 1 addition & 1 deletion baseapp/baseapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ func TestSetMinGasPrices(t *testing.T) {
func TestGetMaximumBlockGas(t *testing.T) {
suite := NewBaseAppSuite(t)
suite.baseApp.InitChain(&abci.RequestInitChain{})
ctx := suite.baseApp.NewContext(true, cmtproto.Header{}) // TODO remove header here
ctx := suite.baseApp.NewContext(true)

suite.baseApp.StoreConsensusParams(ctx, cmtproto.ConsensusParams{Block: &cmtproto.BlockParams{MaxGas: 0}})
require.Equal(t, uint64(0), suite.baseApp.GetMaximumBlockGas(ctx))
Expand Down
3 changes: 1 addition & 2 deletions baseapp/block_gas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

abci "github.com/cometbft/cometbft/abci/types"
cmtjson "github.com/cometbft/cometbft/libs/json"
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
dbm "github.com/cosmos/cosmos-db"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -124,7 +123,7 @@ func TestBaseApp_BlockGas(t *testing.T) {
AppStateBytes: stateBytes,
})

ctx := bapp.NewContext(false, cmtproto.Header{})
ctx := bapp.NewContext(false)

// tx fee
feeCoin := sdk.NewCoin("atom", sdkmath.NewInt(150))
Expand Down
8 changes: 7 additions & 1 deletion baseapp/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ func (app *BaseApp) SimTxFinalizeBlock(txEncoder sdk.TxEncoder, tx sdk.Tx) (sdk.
return gasInfo, result, err
}

func (app *BaseApp) NewContext(isCheckTx bool, header cmtproto.Header) sdk.Context { // todo discuss how to remove header, wrapper or no
// NewContextLegacy returns a new sdk.Context with the provided header
func (app *BaseApp) NewContextLegacy(isCheckTx bool, header cmtproto.Header) sdk.Context {
if isCheckTx {
return sdk.NewContext(app.checkState.ms, header, true, app.logger).
WithMinGasPrices(app.minGasPrices)
Expand All @@ -59,6 +60,11 @@ func (app *BaseApp) NewContext(isCheckTx bool, header cmtproto.Header) sdk.Conte
return sdk.NewContext(app.finalizeBlockState.ms, header, false, app.logger)
}

// NewContext returns a new sdk.Context with a empty header
func (app *BaseApp) NewContext(isCheckTx bool) sdk.Context {
return app.NewContextLegacy(isCheckTx, cmtproto.Header{})
}

func (app *BaseApp) NewUncachedContext(isCheckTx bool, header cmtproto.Header) sdk.Context {
return sdk.NewContext(app.cms, header, isCheckTx, app.logger)
}
Expand Down
3 changes: 1 addition & 2 deletions client/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
sdkmath "cosmossdk.io/math"
abci "github.com/cometbft/cometbft/abci/types"
cmtjson "github.com/cometbft/cometbft/libs/json"
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
dbm "github.com/cosmos/cosmos-db"
"github.com/stretchr/testify/suite"
"google.golang.org/grpc"
Expand Down Expand Up @@ -98,7 +97,7 @@ func (s *IntegrationTestSuite) SetupSuite() {

// end of app init

s.ctx = app.BaseApp.NewContext(false, cmtproto.Header{})
s.ctx = app.BaseApp.NewContext(false)
s.cdc = cdc
queryHelper := baseapp.NewQueryServerTestHelper(s.ctx, interfaceRegistry)
types.RegisterQueryServer(queryHelper, bankKeeper)
Expand Down
6 changes: 3 additions & 3 deletions simapp/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func TestRunMigrations(t *testing.T) {
// version for bank as 1, and for all other modules, we put as
// their latest ConsensusVersion.
_, err = app.ModuleManager.RunMigrations(
app.NewContext(true, cmtproto.Header{Height: app.LastBlockHeight()}), configurator,
app.NewContextLegacy(true, cmtproto.Header{Height: app.LastBlockHeight()}), configurator,
module.VersionMap{
"bank": 1,
"auth": auth.AppModule{}.ConsensusVersion(),
Expand Down Expand Up @@ -222,7 +222,7 @@ func TestRunMigrations(t *testing.T) {
func TestInitGenesisOnMigration(t *testing.T) {
db := dbm.NewMemDB()
app := NewSimApp(log.NewTestLogger(t), db, nil, true, simtestutil.NewAppOptionsWithFlagHome(t.TempDir()))
ctx := app.NewContext(true, cmtproto.Header{Height: app.LastBlockHeight()})
ctx := app.NewContextLegacy(true, cmtproto.Header{Height: app.LastBlockHeight()})

// Create a mock module. This module will serve as the new module we're
// adding during a migration.
Expand Down Expand Up @@ -269,7 +269,7 @@ func TestUpgradeStateOnGenesis(t *testing.T) {
})

// make sure the upgrade keeper has version map in state
ctx := app.NewContext(false, cmtproto.Header{})
ctx := app.NewContext(false)
vm := app.UpgradeKeeper.GetModuleVersionMap(ctx)
for v, i := range app.ModuleManager.Modules {
if i, ok := i.(module.HasConsensusVersion); ok {
Expand Down
2 changes: 1 addition & 1 deletion simapp/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
// file.
func (app *SimApp) ExportAppStateAndValidators(forZeroHeight bool, jailAllowedAddrs, modulesToExport []string) (servertypes.ExportedApp, error) {
// as if they could withdraw from the start of the next block
ctx := app.NewContext(true, cmtproto.Header{Height: app.LastBlockHeight()})
ctx := app.NewContextLegacy(true, cmtproto.Header{Height: app.LastBlockHeight()})

// We export at last height + 1, because that's the height at which
// CometBFT will start InitChain.
Expand Down
2 changes: 1 addition & 1 deletion simapp/sim_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func BenchmarkInvariants(b *testing.B) {
simtestutil.PrintStats(db)
}

ctx := app.NewContext(true, cmtproto.Header{Height: app.LastBlockHeight() + 1})
ctx := app.NewContextLegacy(true, cmtproto.Header{Height: app.LastBlockHeight() + 1})

// 3. Benchmark each invariant separately
//
Expand Down
4 changes: 2 additions & 2 deletions simapp/sim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ func TestAppImportExport(t *testing.T) {
}
}()

ctxA := app.NewContext(true, cmtproto.Header{Height: app.LastBlockHeight()})
ctxB := newApp.NewContext(true, cmtproto.Header{Height: app.LastBlockHeight()})
ctxA := app.NewContextLegacy(true, cmtproto.Header{Height: app.LastBlockHeight()})
ctxB := newApp.NewContextLegacy(true, cmtproto.Header{Height: app.LastBlockHeight()})
newApp.ModuleManager.InitGenesis(ctxB, app.AppCodec(), genesisState)
newApp.StoreConsensusParams(ctxB, exported.ConsensusParams)

Expand Down
3 changes: 1 addition & 2 deletions tests/e2e/upgrade/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package upgrade
import (
"testing"

cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
"github.com/stretchr/testify/suite"

"cosmossdk.io/simapp"
Expand All @@ -18,7 +17,7 @@ func TestE2ETestSuite(t *testing.T) {
cfg.NumValidators = 1

app := simapp.Setup(t, false)
ctx := app.BaseApp.NewContext(false, cmtproto.Header{})
ctx := app.BaseApp.NewContext(false)

app.UpgradeKeeper.SetVersionSetter(app.BaseApp)
app.UpgradeKeeper.SetModuleVersionMap(ctx, app.ModuleManager.GetVersionMap())
Expand Down
3 changes: 1 addition & 2 deletions tests/integration/distribution/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"cosmossdk.io/depinject"
"cosmossdk.io/log"
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
"gotest.tools/v3/assert"

simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
Expand All @@ -26,7 +25,7 @@ func TestItCreatesModuleAccountOnInitBlock(t *testing.T) {
&accountKeeper)
assert.NilError(t, err)

ctx := app.BaseApp.NewContext(false, cmtproto.Header{})
ctx := app.BaseApp.NewContext(false)
acc := accountKeeper.GetAccount(ctx, authtypes.NewModuleAddress(types.ModuleName))
assert.Assert(t, acc != nil)
}
7 changes: 3 additions & 4 deletions tests/integration/gov/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
dbm "github.com/cosmos/cosmos-db"

abci "github.com/cometbft/cometbft/abci/types"
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
"gotest.tools/v3/assert"

"cosmossdk.io/depinject"
Expand Down Expand Up @@ -74,15 +73,15 @@ func TestImportExportQueues(t *testing.T) {
)
assert.NilError(t, err)

ctx := s1.app.BaseApp.NewContext(false, cmtproto.Header{})
ctx := s1.app.BaseApp.NewContext(false)
addrs := simtestutil.AddTestAddrs(s1.BankKeeper, s1.StakingKeeper, ctx, 1, valTokens)

_, err = s1.app.FinalizeBlock(&abci.RequestFinalizeBlock{
Height: s1.app.LastBlockHeight() + 1,
})
assert.NilError(t, err)

ctx = s1.app.BaseApp.NewContext(false, cmtproto.Header{})
ctx = s1.app.BaseApp.NewContext(false)
// Create two proposals, put the second into the voting period
proposal1, err := s1.GovKeeper.SubmitProposal(ctx, []sdk.Msg{mkTestLegacyContent(t)}, "", "test", "description", addrs[0], false)
assert.NilError(t, err)
Expand Down Expand Up @@ -160,7 +159,7 @@ func TestImportExportQueues(t *testing.T) {
})
assert.NilError(t, err)

ctx2 := s2.app.BaseApp.NewContext(false, cmtproto.Header{})
ctx2 := s2.app.BaseApp.NewContext(false)

params, err = s2.GovKeeper.Params.Get(ctx2)
assert.NilError(t, err)
Expand Down
3 changes: 1 addition & 2 deletions tests/integration/gov/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"cosmossdk.io/depinject"
"cosmossdk.io/log"
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
"gotest.tools/v3/assert"

"github.com/cosmos/cosmos-sdk/testutil/configurator"
Expand Down Expand Up @@ -36,7 +35,7 @@ func TestItCreatesModuleAccountOnInitBlock(t *testing.T) {
)
assert.NilError(t, err)

ctx := app.BaseApp.NewContext(false, cmtproto.Header{})
ctx := app.BaseApp.NewContext(false)
acc := accountKeeper.GetAccount(ctx, authtypes.NewModuleAddress(types.ModuleName))
assert.Assert(t, acc != nil)
}
3 changes: 1 addition & 2 deletions tests/integration/runtime/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"testing"

reflectionv1 "cosmossdk.io/api/cosmos/reflection/v1"
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
"github.com/cosmos/gogoproto/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"
Expand Down Expand Up @@ -58,7 +57,7 @@ func initFixture(t assert.TestingT) *fixture {
)
assert.NilError(t, err)

f.ctx = app.BaseApp.NewContext(false, cmtproto.Header{})
f.ctx = app.BaseApp.NewContext(false)
queryHelper := &baseapp.QueryServiceTestHelper{
GRPCQueryRouter: app.BaseApp.GRPCQueryRouter(),
Ctx: f.ctx,
Expand Down
10 changes: 5 additions & 5 deletions tests/integration/store/rootmulti/rollback_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestRollback(t *testing.T) {
app.FinalizeBlock(&abci.RequestFinalizeBlock{
Height: header.Height,
})
ctx := app.NewContext(false, header)
ctx := app.NewContextLegacy(false, header)
store := ctx.KVStore(app.GetKey("bank"))
store.Set([]byte("key"), []byte(fmt.Sprintf("value%d", i)))
app.FinalizeBlock(&abci.RequestFinalizeBlock{
Expand All @@ -43,7 +43,7 @@ func TestRollback(t *testing.T) {
}

assert.Equal(t, ver0+10, app.LastBlockHeight())
store := app.NewContext(true, cmtproto.Header{}).KVStore(app.GetKey("bank"))
store := app.NewContext(true).KVStore(app.GetKey("bank"))
assert.DeepEqual(t, []byte("value10"), store.Get([]byte("key")))

// rollback 5 blocks
Expand All @@ -53,7 +53,7 @@ func TestRollback(t *testing.T) {

// recreate app to have clean check state
app = simapp.NewSimApp(options.Logger, options.DB, nil, true, simtestutil.NewAppOptionsWithFlagHome(t.TempDir()))
store = app.NewContext(true, cmtproto.Header{}).KVStore(app.GetKey("bank"))
store = app.NewContext(true).KVStore(app.GetKey("bank"))
assert.DeepEqual(t, []byte("value5"), store.Get([]byte("key")))

// commit another 5 blocks with different values
Expand All @@ -63,7 +63,7 @@ func TestRollback(t *testing.T) {
AppHash: app.LastCommitID().Hash,
}
app.FinalizeBlock(&abci.RequestFinalizeBlock{Height: header.Height})
ctx := app.NewContext(false, header)
ctx := app.NewContextLegacy(false, header)
store := ctx.KVStore(app.GetKey("bank"))
store.Set([]byte("key"), []byte(fmt.Sprintf("VALUE%d", i)))
app.FinalizeBlock(&abci.RequestFinalizeBlock{
Expand All @@ -73,6 +73,6 @@ func TestRollback(t *testing.T) {
}

assert.Equal(t, ver0+10, app.LastBlockHeight())
store = app.NewContext(true, cmtproto.Header{}).KVStore(app.GetKey("bank"))
store = app.NewContext(true).KVStore(app.GetKey("bank"))
assert.DeepEqual(t, []byte("VALUE10"), store.Get([]byte("key")))
}
2 changes: 1 addition & 1 deletion types/query/pagination_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (s *paginationTestSuite) SetupTest() {

s.NoError(err)

ctx := app.BaseApp.NewContext(false, cmtproto.Header{Height: 1})
ctx := app.BaseApp.NewContextLegacy(false, cmtproto.Header{Height: 1})

s.ctx, s.bankKeeper, s.accountKeeper, s.cdc, s.app, s.interfaceReg = ctx, bankKeeper, accountKeeper, cdc, app, reg
}
Expand Down
4 changes: 2 additions & 2 deletions x/auth/keeper/deterministic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"sync/atomic"
"testing"

cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
"github.com/stretchr/testify/suite"
"pgregory.net/rapid"

"cosmossdk.io/core/header"
corestore "cosmossdk.io/core/store"
storetypes "cosmossdk.io/store/types"

Expand Down Expand Up @@ -56,7 +56,7 @@ func (suite *DeterministicTestSuite) SetupTest() {
key := storetypes.NewKVStoreKey(types.StoreKey)
storeService := runtime.NewKVStoreService(key)
testCtx := testutil.DefaultContextWithDB(suite.T(), key, storetypes.NewTransientStoreKey("transient_test"))
suite.ctx = testCtx.Ctx.WithBlockHeader(cmtproto.Header{})
suite.ctx = testCtx.Ctx.WithHeaderInfo(header.Info{})

maccPerms := map[string][]string{
"fee_collector": nil,
Expand Down
5 changes: 2 additions & 3 deletions x/auth/keeper/keeper_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"cosmossdk.io/depinject"
"cosmossdk.io/log"
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
"github.com/stretchr/testify/require"

simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
Expand All @@ -26,7 +25,7 @@ func BenchmarkAccountMapperGetAccountFound(b *testing.B) {
)
require.NoError(b, err)

ctx := app.BaseApp.NewContext(false, cmtproto.Header{})
ctx := app.BaseApp.NewContext(false)

// assumes b.N < 2**24
for i := 0; i < b.N; i++ {
Expand All @@ -53,7 +52,7 @@ func BenchmarkAccountMapperSetAccount(b *testing.B) {
), &accountKeeper)
require.NoError(b, err)

ctx := app.BaseApp.NewContext(false, cmtproto.Header{})
ctx := app.BaseApp.NewContext(false)

b.ResetTimer()

Expand Down
5 changes: 2 additions & 3 deletions x/auth/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import (

"github.com/stretchr/testify/suite"

cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"

"cosmossdk.io/core/header"
storetypes "cosmossdk.io/store/types"

"github.com/cosmos/cosmos-sdk/baseapp"
Expand Down Expand Up @@ -50,7 +49,7 @@ func (suite *KeeperTestSuite) SetupTest() {
key := storetypes.NewKVStoreKey(types.StoreKey)
storeService := runtime.NewKVStoreService(key)
testCtx := testutil.DefaultContextWithDB(suite.T(), key, storetypes.NewTransientStoreKey("transient_test"))
suite.ctx = testCtx.Ctx.WithBlockHeader(cmtproto.Header{})
suite.ctx = testCtx.Ctx.WithHeaderInfo(header.Info{})

maccPerms := map[string][]string{
"fee_collector": nil,
Expand Down
2 changes: 1 addition & 1 deletion x/auth/migrations/v2/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func TestMigrateVestingAccounts(t *testing.T) {
legacySubspace := newMockSubspace(authtypes.DefaultParams())
require.NoError(t, v4.Migrate(ctx, storeService, legacySubspace, cdc))

ctx = app.BaseApp.NewContext(false, cmtproto.Header{Time: time.Now()})
ctx = app.BaseApp.NewContextLegacy(false, cmtproto.Header{Time: time.Now()})
stakingKeeper.SetParams(ctx, stakingtypes.DefaultParams())
lastAccNum := uint64(1000)
createBaseAccount := func(addr sdk.AccAddress) *authtypes.BaseAccount {
Expand Down
2 changes: 1 addition & 1 deletion x/auth/migrations/v3/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func TestMigrateMapAccAddressToAccNumberKey(t *testing.T) {
randAccNumber := uint64(rand.Intn(100000-10000) + 10000)
acc := authtypes.NewBaseAccount(senderPrivKey.PubKey().Address().Bytes(), senderPrivKey.PubKey(), randAccNumber, 0)

ctx = app.BaseApp.NewContext(false, cmtproto.Header{Time: time.Now()})
ctx = app.BaseApp.NewContextLegacy(false, cmtproto.Header{Time: time.Now()})

// migrator
m := keeper.NewMigrator(accountKeeper, app.GRPCQueryRouter(), legacySubspace)
Expand Down
3 changes: 1 addition & 2 deletions x/auth/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"cosmossdk.io/depinject"
"cosmossdk.io/log"
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
"github.com/stretchr/testify/require"

simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
Expand All @@ -24,7 +23,7 @@ func TestItCreatesModuleAccountOnInitBlock(t *testing.T) {
&accountKeeper)
require.NoError(t, err)

ctx := app.BaseApp.NewContext(false, cmtproto.Header{})
ctx := app.BaseApp.NewContext(false)
acc := accountKeeper.GetAccount(ctx, types.NewModuleAddress(types.FeeCollectorName))
require.NotNil(t, acc)
}
Loading

0 comments on commit 5097b0c

Please sign in to comment.