From 0ba953da60866ce492ee70e5a22745996502bad6 Mon Sep 17 00:00:00 2001 From: atheeshp <59333759+atheeshp@users.noreply.github.com> Date: Tue, 23 Mar 2021 13:01:09 +0530 Subject: [PATCH 1/6] move feegrant ante to auth ante (#8682) * move feegrant ante to auth ante * update ante builder * remove commented code * review changes * fix lint * review changes * review changes * review changes * review changes * fix ante builder * review changes * update ante builder with options struct * review changes * review changes * add changelog Co-authored-by: Amaury <1293565+amaurym@users.noreply.github.com> Co-authored-by: Aaron Craelius --- app.go | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/app.go b/app.go index 97a71d4015e4..e0c4316f30a0 100644 --- a/app.go +++ b/app.go @@ -56,7 +56,6 @@ import ( evidencekeeper "github.com/cosmos/cosmos-sdk/x/evidence/keeper" evidencetypes "github.com/cosmos/cosmos-sdk/x/evidence/types" feegrant "github.com/cosmos/cosmos-sdk/x/feegrant" - feegrantante "github.com/cosmos/cosmos-sdk/x/feegrant/ante" feegrantkeeper "github.com/cosmos/cosmos-sdk/x/feegrant/keeper" feegranttypes "github.com/cosmos/cosmos-sdk/x/feegrant/types" "github.com/cosmos/cosmos-sdk/x/genutil" @@ -375,12 +374,22 @@ func NewSimApp( // initialize BaseApp app.SetInitChainer(app.InitChainer) app.SetBeginBlocker(app.BeginBlocker) - app.SetAnteHandler( - feegrantante.NewAnteHandler( - app.AccountKeeper, app.BankKeeper, app.FeeGrantKeeper, ante.DefaultSigVerificationGasConsumer, - encodingConfig.TxConfig.SignModeHandler(), - ), + + anteHandler, err := ante.NewAnteHandler( + ante.HandlerOptions{ + AccountKeeper: app.AccountKeeper, + BankKeeper: app.BankKeeper, + SignModeHandler: encodingConfig.TxConfig.SignModeHandler(), + FeegrantKeeper: app.FeeGrantKeeper, + SigGasConsumer: ante.DefaultSigVerificationGasConsumer, + }, ) + + if err != nil { + panic(err) + } + + app.SetAnteHandler(anteHandler) app.SetEndBlocker(app.EndBlocker) if loadLatest { From 3e29e9876458fe3f27fc9540c04b1614cba450e8 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Wed, 24 Mar 2021 12:05:33 -0400 Subject: [PATCH 2/6] Merge pull request from GHSA-2f3p-6gfj-jccq * x/bank: update SendCoinsFromModuleToAccount * x/bank: add TestSendCoinsFromModuleToAccount_Blacklist * CL++ * fix tests * godoc++ --- test_helpers.go | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/test_helpers.go b/test_helpers.go index df80e4fccadc..88f8dc8c7d71 100644 --- a/test_helpers.go +++ b/test_helpers.go @@ -437,12 +437,30 @@ func (ao EmptyAppOptions) Get(o string) interface{} { return nil } -// FundAccount is a utility function that funds an account by minting and sending the coins to the address -// TODO(fdymylja): instead of using the mint module account, which has the permission of minting, create a "faucet" account +// FundAccount is a utility function that funds an account by minting and +// sending the coins to the address. This should be used for testing purposes +// only! +// +// TODO: Instead of using the mint module account, which has the +// permission of minting, create a "faucet" account. (@fdymylja) func FundAccount(app *SimApp, ctx sdk.Context, addr sdk.AccAddress, amounts sdk.Coins) error { - err := app.BankKeeper.MintCoins(ctx, minttypes.ModuleName, amounts) - if err != nil { + if err := app.BankKeeper.MintCoins(ctx, minttypes.ModuleName, amounts); err != nil { return err } + return app.BankKeeper.SendCoinsFromModuleToAccount(ctx, minttypes.ModuleName, addr, amounts) } + +// FundModuleAccount is a utility function that funds a module account by +// minting and sending the coins to the address. This should be used for testing +// purposes only! +// +// TODO: Instead of using the mint module account, which has the +// permission of minting, create a "faucet" account. (@fdymylja) +func FundModuleAccount(app *SimApp, ctx sdk.Context, recipientMod string, amounts sdk.Coins) error { + if err := app.BankKeeper.MintCoins(ctx, minttypes.ModuleName, amounts); err != nil { + return err + } + + return app.BankKeeper.SendCoinsFromModuleToModule(ctx, minttypes.ModuleName, recipientMod, amounts) +} From ae952deebf3f6bac86f5a208cbde26d2141777cd Mon Sep 17 00:00:00 2001 From: Amaury <1293565+amaurym@users.noreply.github.com> Date: Thu, 25 Mar 2021 11:52:54 +0100 Subject: [PATCH 3/6] Prefer sending tx_bytes to Simulate gRPC endpoint (#8926) * First run * Remove dead code * Make test pass * Proto gen * Fix lint * Add changelog * Fix tests * Fix test * Update x/auth/tx/service.go Co-authored-by: Marie Gauthier * Remove protoTxProvider * Add grpc-gateway test * Add comment * move to api breaking * lesser diff * remove conflict * empty commit to rerun CI * empty commit to rerun CI Co-authored-by: Marie Gauthier Co-authored-by: Alessio Treglia Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> --- simd/cmd/root.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/simd/cmd/root.go b/simd/cmd/root.go index 836036bcf08c..e3033fc81e62 100644 --- a/simd/cmd/root.go +++ b/simd/cmd/root.go @@ -25,7 +25,6 @@ import ( "github.com/cosmos/cosmos-sdk/snapshots" "github.com/cosmos/cosmos-sdk/store" sdk "github.com/cosmos/cosmos-sdk/types" - authclient "github.com/cosmos/cosmos-sdk/x/auth/client" authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli" "github.com/cosmos/cosmos-sdk/x/auth/types" vestingcli "github.com/cosmos/cosmos-sdk/x/auth/vesting/client/cli" @@ -66,8 +65,6 @@ func NewRootCmd() (*cobra.Command, params.EncodingConfig) { } func initRootCmd(rootCmd *cobra.Command, encodingConfig params.EncodingConfig) { - authclient.Codec = encodingConfig.Marshaler - rootCmd.AddCommand( genutilcli.InitCmd(simapp.ModuleBasics, simapp.DefaultNodeHome), genutilcli.CollectGenTxsCmd(banktypes.GenesisBalancesIterator{}, simapp.DefaultNodeHome), From 8de7c2efc8415b7ebfb0d6ac280550defedaa57f Mon Sep 17 00:00:00 2001 From: Andrei Ivasko Date: Wed, 31 Mar 2021 03:04:33 -0700 Subject: [PATCH 4/6] Add client config subcommand to CLI (#8953) * add client config * addressed reviewers comments * refactored,ready for review * fixed linter issues and addressed reviewers comments * Bump golangci-lint * fix linter warnings * fix some tests Co-authored-by: Alessio Treglia Co-authored-by: Amaury <1293565+amaurym@users.noreply.github.com> --- simd/cmd/root.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/simd/cmd/root.go b/simd/cmd/root.go index e3033fc81e62..30356d3ed53d 100644 --- a/simd/cmd/root.go +++ b/simd/cmd/root.go @@ -14,6 +14,7 @@ import ( "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/client" + config "github.com/cosmos/cosmos-sdk/client/config" "github.com/cosmos/cosmos-sdk/client/debug" "github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/client/keys" @@ -45,12 +46,20 @@ func NewRootCmd() (*cobra.Command, params.EncodingConfig) { WithInput(os.Stdin). WithAccountRetriever(types.AccountRetriever{}). WithBroadcastMode(flags.BroadcastBlock). - WithHomeDir(simapp.DefaultNodeHome) + WithHomeDir(simapp.DefaultNodeHome). + WithViper() rootCmd := &cobra.Command{ Use: "simd", Short: "simulation app", PersistentPreRunE: func(cmd *cobra.Command, _ []string) error { + initClientCtx = client.ReadHomeFlag(initClientCtx, cmd) + + initClientCtx, err := config.ReadFromClientConfig(initClientCtx) + if err != nil { + return err + } + if err := client.SetCmdClientContextHandler(initClientCtx, cmd); err != nil { return err } @@ -75,6 +84,7 @@ func initRootCmd(rootCmd *cobra.Command, encodingConfig params.EncodingConfig) { tmcli.NewCompletionCmd(rootCmd, true), testnetCmd(simapp.ModuleBasics, banktypes.GenesisBalancesIterator{}), debug.Cmd(), + config.Cmd(), ) a := appCreator{encodingConfig} From a31b79af3f5b3c71d7eea5288c2521c12f2ce060 Mon Sep 17 00:00:00 2001 From: technicallyty <48813565+technicallyty@users.noreply.github.com> Date: Fri, 2 Apr 2021 07:11:58 -0700 Subject: [PATCH 5/6] x/upgrade: protocol version tracking (#8897) * -added consensus version tracking to x/upgrade * -added interface to module manager -added e2e test for migrations using consensus version store in x/upgrade -cleaned up x/upgrade Keeper -handler in apply upgrade now handles errors and setting consensus versions -cleaned up migration map keys -removed init chainer method -simapp now implements GetConsensusVersions to assist with testing * protocol version p1 * add protocol version to baseapp * rebase against master * add test * added test case * cleanup * docs and change to bigendian * changelog * cleanup keeper * swap appVersion and version * cleanup * change interface id * update keeper field name to versionSetter * reorder keys and docs * -move interface into exported folder * typo * typo2 * docs on keeper fields * docs for upgrade NewKeeper * cleanup * NewKeeper docs Co-authored-by: technicallyty <48813565+tytech3@users.noreply.github.com> Co-authored-by: Alessio Treglia --- app.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app.go b/app.go index e0c4316f30a0..7d62f2e8445d 100644 --- a/app.go +++ b/app.go @@ -200,7 +200,7 @@ func NewSimApp( bApp := baseapp.NewBaseApp(appName, logger, db, encodingConfig.TxConfig.TxDecoder(), baseAppOptions...) bApp.SetCommitMultiStoreTracer(traceStore) - bApp.SetAppVersion(version.Version) + bApp.SetVersion(version.Version) bApp.SetInterfaceRegistry(interfaceRegistry) keys := sdk.NewKVStoreKeys( @@ -257,7 +257,7 @@ func NewSimApp( ) app.FeeGrantKeeper = feegrantkeeper.NewKeeper(appCodec, keys[feegranttypes.StoreKey], app.AccountKeeper) - app.UpgradeKeeper = upgradekeeper.NewKeeper(skipUpgradeHeights, keys[upgradetypes.StoreKey], appCodec, homePath) + app.UpgradeKeeper = upgradekeeper.NewKeeper(skipUpgradeHeights, keys[upgradetypes.StoreKey], appCodec, homePath, app.BaseApp) // register the staking hooks // NOTE: stakingKeeper above is passed by reference, so that it will contain these hooks From 30a144b3d04a393ec225fc8a9256082c133b9644 Mon Sep 17 00:00:00 2001 From: Amaury <1293565+amaurym@users.noreply.github.com> Date: Fri, 2 Apr 2021 17:41:35 +0200 Subject: [PATCH 6/6] InitGenesis in migrations when fromVersion==0 (#9007) * InitGenesis in migrations when fromVersion==0 * Add test * Fix test * Fix comment * cdc=>codec * Don't break Configurator * Remove method * Add comments * Add more comments * Update types/module/module.go Co-authored-by: technicallyty <48813565+technicallyty@users.noreply.github.com> * Update types/module/module.go Co-authored-by: technicallyty <48813565+technicallyty@users.noreply.github.com> * Update types/module/module.go Co-authored-by: technicallyty <48813565+technicallyty@users.noreply.github.com> Co-authored-by: technicallyty <48813565+technicallyty@users.noreply.github.com> --- app.go | 18 +----------------- app_test.go | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 53 insertions(+), 20 deletions(-) diff --git a/app.go b/app.go index 7d62f2e8445d..e3771e2fc105 100644 --- a/app.go +++ b/app.go @@ -339,7 +339,7 @@ func NewSimApp( app.mm.RegisterInvariants(&app.CrisisKeeper) app.mm.RegisterRoutes(app.Router(), app.QueryRouter(), encodingConfig.Amino) - app.configurator = module.NewConfigurator(app.MsgServiceRouter(), app.GRPCQueryRouter()) + app.configurator = module.NewConfigurator(app.appCodec, app.MsgServiceRouter(), app.GRPCQueryRouter()) app.mm.RegisterServices(app.configurator) // add test gRPC service for testing gRPC queries in isolation @@ -536,22 +536,6 @@ func (app *SimApp) RegisterTendermintService(clientCtx client.Context) { tmservice.RegisterTendermintService(app.BaseApp.GRPCQueryRouter(), clientCtx, app.interfaceRegistry) } -// RunMigrations performs in-place store migrations for all modules. This -// function MUST be only called by x/upgrade UpgradeHandler. -// -// `migrateFromVersions` is a map of moduleName to fromVersion (unit64), where -// fromVersion denotes the version from which we should migrate the module, the -// target version being the module's latest ConsensusVersion. -// -// Example: -// cfg := module.NewConfigurator(...) -// app.UpgradeKeeper.SetUpgradeHandler("store-migration", func(ctx sdk.Context, plan upgradetypes.Plan, vm module.VersionMap) { -// return app.RunMigrations(ctx, vm) -// }) -func (app *SimApp) RunMigrations(ctx sdk.Context, migrateFromVersions module.VersionMap) (module.VersionMap, error) { - return app.mm.RunMigrations(ctx, app.configurator, migrateFromVersions) -} - // RegisterSwaggerAPI registers swagger route with API Server func RegisterSwaggerAPI(ctx client.Context, rtr *mux.Router) { statikFS, err := fs.New() diff --git a/app_test.go b/app_test.go index c4aa9885b5d8..2f9c5e2c3d19 100644 --- a/app_test.go +++ b/app_test.go @@ -5,6 +5,7 @@ import ( "os" "testing" + "github.com/golang/mock/gomock" "github.com/stretchr/testify/require" abci "github.com/tendermint/tendermint/abci/types" "github.com/tendermint/tendermint/libs/log" @@ -12,11 +13,13 @@ import ( dbm "github.com/tendermint/tm-db" "github.com/cosmos/cosmos-sdk/baseapp" + "github.com/cosmos/cosmos-sdk/tests/mocks" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/module" "github.com/cosmos/cosmos-sdk/x/auth" "github.com/cosmos/cosmos-sdk/x/auth/vesting" "github.com/cosmos/cosmos-sdk/x/authz" + "github.com/cosmos/cosmos-sdk/x/bank" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" "github.com/cosmos/cosmos-sdk/x/capability" "github.com/cosmos/cosmos-sdk/x/crisis" @@ -80,7 +83,7 @@ func TestRunMigrations(t *testing.T) { bApp.SetCommitMultiStoreTracer(nil) bApp.SetInterfaceRegistry(encCfg.InterfaceRegistry) app.BaseApp = bApp - app.configurator = module.NewConfigurator(app.MsgServiceRouter(), app.GRPCQueryRouter()) + app.configurator = module.NewConfigurator(app.appCodec, app.MsgServiceRouter(), app.GRPCQueryRouter()) // We register all modules on the Configurator, except x/bank. x/bank will // serve as the test subject on which we run the migration tests. @@ -160,8 +163,8 @@ func TestRunMigrations(t *testing.T) { // Run migrations only for bank. That's why we put the initial // version for bank as 1, and for all other modules, we put as // their latest ConsensusVersion. - _, err = app.RunMigrations( - app.NewContext(true, tmproto.Header{Height: app.LastBlockHeight()}), + _, err = app.mm.RunMigrations( + app.NewContext(true, tmproto.Header{Height: app.LastBlockHeight()}), app.configurator, module.VersionMap{ "bank": 1, "auth": auth.AppModule{}.ConsensusVersion(), @@ -185,12 +188,58 @@ func TestRunMigrations(t *testing.T) { require.EqualError(t, err, tc.expRunErrMsg) } else { require.NoError(t, err) + // Make sure bank's migration is called. require.Equal(t, tc.expCalled, called) } }) } } +func TestInitGenesisOnMigration(t *testing.T) { + db := dbm.NewMemDB() + encCfg := MakeTestEncodingConfig() + logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout)) + app := NewSimApp(logger, db, nil, true, map[int64]bool{}, DefaultNodeHome, 0, encCfg, EmptyAppOptions{}) + ctx := app.NewContext(true, tmproto.Header{Height: app.LastBlockHeight()}) + + // Create a mock module. This module will serve as the new module we're + // adding during a migration. + mockCtrl := gomock.NewController(t) + t.Cleanup(mockCtrl.Finish) + mockModule := mocks.NewMockAppModule(mockCtrl) + mockDefaultGenesis := json.RawMessage(`{"key": "value"}`) + mockModule.EXPECT().DefaultGenesis(gomock.Eq(app.appCodec)).Times(1).Return(mockDefaultGenesis) + mockModule.EXPECT().InitGenesis(gomock.Eq(ctx), gomock.Eq(app.appCodec), gomock.Eq(mockDefaultGenesis)).Times(1).Return(nil) + + app.mm.Modules["mock"] = mockModule + + // Run migrations only for "mock" module. That's why we put the initial + // version for bank as 0 (to run its InitGenesis), and for all other + // modules, we put their latest ConsensusVersion to skip migrations. + _, err := app.mm.RunMigrations(ctx, app.configurator, + module.VersionMap{ + "mock": 0, + "bank": bank.AppModule{}.ConsensusVersion(), + "auth": auth.AppModule{}.ConsensusVersion(), + "authz": authz.AppModule{}.ConsensusVersion(), + "staking": staking.AppModule{}.ConsensusVersion(), + "mint": mint.AppModule{}.ConsensusVersion(), + "distribution": distribution.AppModule{}.ConsensusVersion(), + "slashing": slashing.AppModule{}.ConsensusVersion(), + "gov": gov.AppModule{}.ConsensusVersion(), + "params": params.AppModule{}.ConsensusVersion(), + "upgrade": upgrade.AppModule{}.ConsensusVersion(), + "vesting": vesting.AppModule{}.ConsensusVersion(), + "feegrant": feegrant.AppModule{}.ConsensusVersion(), + "evidence": evidence.AppModule{}.ConsensusVersion(), + "crisis": crisis.AppModule{}.ConsensusVersion(), + "genutil": genutil.AppModule{}.ConsensusVersion(), + "capability": capability.AppModule{}.ConsensusVersion(), + }, + ) + require.NoError(t, err) +} + func TestUpgradeStateOnGenesis(t *testing.T) { encCfg := MakeTestEncodingConfig() db := dbm.NewMemDB()