From fb257ae9d94d5695c083fc1ed097d86184a0014f Mon Sep 17 00:00:00 2001 From: Likhita Polavarapu <78951027+likhita-809@users.noreply.github.com> Date: Tue, 11 Apr 2023 20:25:55 +0530 Subject: [PATCH] feat: add Context as arg to NewIntegrationApp (#15779) ## Description Closes: #XXXX This PR adds `sdk.Context` arg to `NewIntegrationApp`. This allows us to set the context updatable while verifying `begin/end block` logic in integration tests. --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... * [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title * [ ] added `!` to the type prefix if API or client breaking change * [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting)) * [ ] provided a link to the relevant issue or specification * [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/docs/building-modules) * [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing) * [ ] added a changelog entry to `CHANGELOG.md` * [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) * [ ] updated the relevant documentation or specification * [ ] reviewed "Files changed" and left comments if necessary * [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... * [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title * [ ] confirmed `!` in the type prefix if API or client breaking change * [ ] confirmed all author checklist items have been addressed * [ ] reviewed state machine logic * [ ] reviewed API design and naming * [ ] reviewed documentation is accurate * [ ] reviewed tests and test coverage * [ ] manually tested (if applicable) --- testutil/integration/example_test.go | 25 ++++++++++++++++++++----- testutil/integration/router.go | 17 +++++++++++++++-- x/tx/decode/decode_test.go | 11 +++++------ 3 files changed, 40 insertions(+), 13 deletions(-) diff --git a/testutil/integration/example_test.go b/testutil/integration/example_test.go index 4811109483..1286cda0ce 100644 --- a/testutil/integration/example_test.go +++ b/testutil/integration/example_test.go @@ -6,6 +6,10 @@ import ( "cosmossdk.io/log" storetypes "cosmossdk.io/store/types" + cmtproto "github.com/cometbft/cometbft/proto/tendermint/types" + + "github.com/google/go-cmp/cmp" + "github.com/cosmos/cosmos-sdk/runtime" "github.com/cosmos/cosmos-sdk/testutil/integration" sdk "github.com/cosmos/cosmos-sdk/types" @@ -17,7 +21,6 @@ import ( "github.com/cosmos/cosmos-sdk/x/mint" mintkeeper "github.com/cosmos/cosmos-sdk/x/mint/keeper" minttypes "github.com/cosmos/cosmos-sdk/x/mint/types" - "github.com/google/go-cmp/cmp" ) // Example shows how to use the integration test framework to test the integration of SDK modules. @@ -29,6 +32,12 @@ func Example() { keys := storetypes.NewKVStoreKeys(authtypes.StoreKey, minttypes.StoreKey) authority := authtypes.NewModuleAddress("gov").String() + // replace the logger by testing values in a real test case (e.g. log.NewTestLogger(t)) + logger := log.NewLogger(io.Discard, log.OutputJSONOption()) + + cms := integration.CreateMultiStore(keys, logger) + newCtx := sdk.NewContext(cms, cmtproto.Header{}, true, logger) + accountKeeper := authkeeper.NewAccountKeeper( encodingCfg.Codec, runtime.NewKVStoreService(keys[authtypes.StoreKey]), @@ -47,9 +56,9 @@ func Example() { mintModule := mint.NewAppModule(encodingCfg.Codec, mintKeeper, accountKeeper, nil, nil) // create the application and register all the modules from the previous step - // replace the logger by testing values in a real test case (e.g. log.NewTestLogger(t)) integrationApp := integration.NewIntegrationApp( - log.NewLogger(io.Discard, log.OutputJSONOption()), + newCtx, + logger, keys, encodingCfg.Codec, authModule, mintModule, @@ -104,6 +113,12 @@ func Example_oneModule() { keys := storetypes.NewKVStoreKeys(authtypes.StoreKey) authority := authtypes.NewModuleAddress("gov").String() + // replace the logger by testing values in a real test case (e.g. log.NewTestLogger(t)) + logger := log.NewLogger(io.Discard) + + cms := integration.CreateMultiStore(keys, logger) + newCtx := sdk.NewContext(cms, cmtproto.Header{}, true, logger) + accountKeeper := authkeeper.NewAccountKeeper( encodingCfg.Codec, runtime.NewKVStoreService(keys[authtypes.StoreKey]), @@ -117,9 +132,9 @@ func Example_oneModule() { authModule := auth.NewAppModule(encodingCfg.Codec, accountKeeper, authsims.RandomGenesisAccounts, nil) // create the application and register all the modules from the previous step - // replace the logger by testing values in a real test case (e.g. log.NewTestLogger(t)) integrationApp := integration.NewIntegrationApp( - log.NewLogger(io.Discard), + newCtx, + logger, keys, encodingCfg.Codec, authModule, diff --git a/testutil/integration/router.go b/testutil/integration/router.go index 3cbf34172b..8326436675 100644 --- a/testutil/integration/router.go +++ b/testutil/integration/router.go @@ -8,6 +8,8 @@ import ( cmtproto "github.com/cometbft/cometbft/proto/tendermint/types" "cosmossdk.io/log" + "cosmossdk.io/store" + "cosmossdk.io/store/metrics" storetypes "cosmossdk.io/store/types" dbm "github.com/cosmos/cosmos-db" @@ -31,7 +33,7 @@ type App struct { } // NewIntegrationApp creates an application for testing purposes. This application is able to route messages to their respective handlers. -func NewIntegrationApp(logger log.Logger, keys map[string]*storetypes.KVStoreKey, appCodec codec.Codec, modules ...module.AppModule) *App { +func NewIntegrationApp(sdkCtx sdk.Context, logger log.Logger, keys map[string]*storetypes.KVStoreKey, appCodec codec.Codec, modules ...module.AppModule) *App { db := dbm.NewMemDB() interfaceRegistry := codectypes.NewInterfaceRegistry() @@ -72,7 +74,7 @@ func NewIntegrationApp(logger log.Logger, keys map[string]*storetypes.KVStoreKey bApp.InitChain(cmtabcitypes.RequestInitChain{ChainId: appName}) bApp.Commit() - ctx := bApp.NewContext(true, cmtproto.Header{ChainID: appName}) + ctx := sdkCtx.WithBlockHeader(cmtproto.Header{ChainID: appName}).WithIsCheckTx(true) return &App{ BaseApp: bApp, @@ -146,3 +148,14 @@ func (app *App) Context() context.Context { func (app *App) QueryHelper() *baseapp.QueryServiceTestHelper { return app.queryHelper } + +// CreateMultiStore is a helper for setting up multiple stores for provided modules. +func CreateMultiStore(keys map[string]*storetypes.KVStoreKey, logger log.Logger) storetypes.CommitMultiStore { + db := dbm.NewMemDB() + cms := store.NewCommitMultiStore(db, logger, metrics.NewNoOpMetrics()) + for key := range keys { + cms.MountStoreWithDB(keys[key], storetypes.StoreTypeIAVL, db) + } + _ = cms.LoadLatestVersion() + return cms +} diff --git a/x/tx/decode/decode_test.go b/x/tx/decode/decode_test.go index caaf06e390..a7169a9989 100644 --- a/x/tx/decode/decode_test.go +++ b/x/tx/decode/decode_test.go @@ -4,11 +4,6 @@ import ( "fmt" "testing" - "github.com/cosmos/cosmos-proto/anyutil" - "github.com/stretchr/testify/require" - "google.golang.org/protobuf/proto" - "google.golang.org/protobuf/types/known/anypb" - bankv1beta1 "cosmossdk.io/api/cosmos/bank/v1beta1" basev1beta1 "cosmossdk.io/api/cosmos/base/v1beta1" "cosmossdk.io/api/cosmos/crypto/secp256k1" @@ -16,6 +11,11 @@ import ( txv1beta1 "cosmossdk.io/api/cosmos/tx/v1beta1" "cosmossdk.io/x/tx/decode" "cosmossdk.io/x/tx/internal/testpb" + + "github.com/cosmos/cosmos-proto/anyutil" + "github.com/stretchr/testify/require" + "google.golang.org/protobuf/proto" + "google.golang.org/protobuf/types/known/anypb" ) func TestDecode(t *testing.T) { @@ -98,5 +98,4 @@ func TestDecode(t *testing.T) { decodeTx.Tx.Body.Messages[0].TypeUrl) }) } - }