From f6d7a92779556c9355c10da4da4d87f82ebb5083 Mon Sep 17 00:00:00 2001 From: Marko Date: Tue, 24 Sep 2024 21:52:31 +0200 Subject: [PATCH] refactor(x/mint): remove staking as a required module (#21858) Co-authored-by: Julien Robert --- server/v2/cometbft/mempool/noop.go | 6 ++- simapp/app.go | 9 +++-- tests/integration/example/example_test.go | 4 +- x/accounts/README.md | 6 +-- x/mint/CHANGELOG.md | 8 +++- x/mint/depinject.go | 38 ++++++++---------- x/mint/epoch_hooks.go | 2 +- x/mint/keeper/abci.go | 4 +- x/mint/keeper/genesis_test.go | 11 ++++-- x/mint/keeper/grpc_query.go | 4 +- x/mint/keeper/grpc_query_test.go | 4 +- x/mint/keeper/keeper.go | 47 ++++++++++++----------- x/mint/keeper/keeper_test.go | 43 ++++++--------------- x/mint/keeper/migrator.go | 4 +- x/mint/keeper/msg_server.go | 4 +- x/mint/module.go | 19 ++------- x/mint/module_test.go | 10 +++-- 17 files changed, 101 insertions(+), 122 deletions(-) diff --git a/server/v2/cometbft/mempool/noop.go b/server/v2/cometbft/mempool/noop.go index 25cffcf6997..3d3e4feab2d 100644 --- a/server/v2/cometbft/mempool/noop.go +++ b/server/v2/cometbft/mempool/noop.go @@ -8,8 +8,10 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" ) -var _ Mempool[sdk.Tx] = (*NoOpMempool[sdk.Tx])(nil) // verify interface at compile time -var _ Mempool[transaction.Tx] = (*NoOpMempool[transaction.Tx])(nil) +var ( + _ Mempool[sdk.Tx] = (*NoOpMempool[sdk.Tx])(nil) // verify interface at compile time + _ Mempool[transaction.Tx] = (*NoOpMempool[transaction.Tx])(nil) +) // NoOpMempool defines a no-op mempool. Transactions are completely discarded and // ignored when BaseApp interacts with the mempool. diff --git a/simapp/app.go b/simapp/app.go index 1127b73b8ac..50b6d21df39 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -165,7 +165,7 @@ type SimApp struct { BankKeeper bankkeeper.BaseKeeper StakingKeeper *stakingkeeper.Keeper SlashingKeeper slashingkeeper.Keeper - MintKeeper mintkeeper.Keeper + MintKeeper *mintkeeper.Keeper DistrKeeper distrkeeper.Keeper GovKeeper govkeeper.Keeper UpgradeKeeper *upgradekeeper.Keeper @@ -372,7 +372,10 @@ func NewSimApp( cometService, ) - app.MintKeeper = mintkeeper.NewKeeper(appCodec, runtime.NewEnvironment(runtime.NewKVStoreService(keys[minttypes.StoreKey]), logger.With(log.ModuleKey, "x/mint")), app.StakingKeeper, app.AuthKeeper, app.BankKeeper, authtypes.FeeCollectorName, govModuleAddr) + app.MintKeeper = mintkeeper.NewKeeper(appCodec, runtime.NewEnvironment(runtime.NewKVStoreService(keys[minttypes.StoreKey]), logger.With(log.ModuleKey, "x/mint")), app.AuthKeeper, app.BankKeeper, authtypes.FeeCollectorName, govModuleAddr) + if err := app.MintKeeper.SetMintFn(mintkeeper.DefaultMintFn(minttypes.DefaultInflationCalculationFn, app.StakingKeeper, app.MintKeeper)); err != nil { + panic(err) + } app.PoolKeeper = poolkeeper.NewKeeper(appCodec, runtime.NewEnvironment(runtime.NewKVStoreService(keys[pooltypes.StoreKey]), logger.With(log.ModuleKey, "x/protocolpool")), app.AuthKeeper, app.BankKeeper, app.StakingKeeper, govModuleAddr) @@ -467,7 +470,7 @@ func NewSimApp( bank.NewAppModule(appCodec, app.BankKeeper, app.AuthKeeper), feegrantmodule.NewAppModule(appCodec, app.FeeGrantKeeper, app.interfaceRegistry), gov.NewAppModule(appCodec, &app.GovKeeper, app.AuthKeeper, app.BankKeeper, app.PoolKeeper), - mint.NewAppModule(appCodec, app.MintKeeper, app.AuthKeeper, nil), + mint.NewAppModule(appCodec, app.MintKeeper, app.AuthKeeper), slashing.NewAppModule(appCodec, app.SlashingKeeper, app.AuthKeeper, app.BankKeeper, app.StakingKeeper, app.interfaceRegistry, cometService), distr.NewAppModule(appCodec, app.DistrKeeper, app.StakingKeeper), staking.NewAppModule(appCodec, app.StakingKeeper), diff --git a/tests/integration/example/example_test.go b/tests/integration/example/example_test.go index ced052da114..7ff625634ac 100644 --- a/tests/integration/example/example_test.go +++ b/tests/integration/example/example_test.go @@ -74,8 +74,8 @@ func Example() { // here bankkeeper and staking keeper is nil because we are not testing them // subspace is nil because we don't test params (which is legacy anyway) - mintKeeper := mintkeeper.NewKeeper(encodingCfg.Codec, runtime.NewEnvironment(runtime.NewKVStoreService(keys[minttypes.StoreKey]), logger), nil, accountKeeper, nil, authtypes.FeeCollectorName, authority) - mintModule := mint.NewAppModule(encodingCfg.Codec, mintKeeper, accountKeeper, nil) + mintKeeper := mintkeeper.NewKeeper(encodingCfg.Codec, runtime.NewEnvironment(runtime.NewKVStoreService(keys[minttypes.StoreKey]), logger), accountKeeper, nil, authtypes.FeeCollectorName, authority) + mintModule := mint.NewAppModule(encodingCfg.Codec, mintKeeper, accountKeeper) // create the application and register all the modules from the previous step integrationApp := integration.NewIntegrationApp( diff --git a/x/accounts/README.md b/x/accounts/README.md index 8214220d856..39dd65af48b 100644 --- a/x/accounts/README.md +++ b/x/accounts/README.md @@ -56,8 +56,8 @@ func (a Account) AuthRetroCompatibility(ctx context.Context, _ *authtypes.QueryL ## Usage Notes -- Implement this handler only for account types you want to expose via x/auth gRPC methods. -- The `info` field in the response can be nil if your account doesn't fit the `BaseAccount` structure. +* Implement this handler only for account types you want to expose via x/auth gRPC methods. +* The `info` field in the response can be nil if your account doesn't fit the `BaseAccount` structure. # Genesis @@ -102,4 +102,4 @@ For example, given the following `genesis.json` file: } ``` -The accounts module will run the lockup account initialization message. \ No newline at end of file +The accounts module will run the lockup account initialization message. diff --git a/x/mint/CHANGELOG.md b/x/mint/CHANGELOG.md index 285fab6b5c3..749a0680d1e 100644 --- a/x/mint/CHANGELOG.md +++ b/x/mint/CHANGELOG.md @@ -32,9 +32,13 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Improvements +### Bug Fixes + ### API Breaking Changes * [#20363](https://github.com/cosmos/cosmos-sdk/pull/20363) Deprecated InflationCalculationFn in favor of MintFn, `keeper.DefaultMintFn` wrapper must be used in order to continue using it in `NewAppModule`. This is not breaking for depinject users, as both `MintFn` and `InflationCalculationFn` are accepted. * [#19367](https://github.com/cosmos/cosmos-sdk/pull/19398) `appmodule.Environment` is received on the Keeper to get access to different application services. - -### Bug Fixes +* [#21858](https://github.com/cosmos/cosmos-sdk/pull/21858) `NewKeeper` now returns a pointer to `Keeper`. +* [#21858](https://github.com/cosmos/cosmos-sdk/pull/21858) `DefaultMintFn` now takes `StakingKeeper` and `MintKeeper` as arguments to avoid staking keeper being required by mint. + * `SetMintFn` is used to replace the default minting function. + * `InflationCalculationFn` is not passed through depinject any longer, a MintFn is required instead. diff --git a/x/mint/depinject.go b/x/mint/depinject.go index e580cc096ca..db36920101d 100644 --- a/x/mint/depinject.go +++ b/x/mint/depinject.go @@ -1,6 +1,8 @@ package mint import ( + "fmt" + modulev1 "cosmossdk.io/api/cosmos/mint/module/v1" "cosmossdk.io/core/appmodule" "cosmossdk.io/depinject" @@ -21,28 +23,25 @@ func (am AppModule) IsOnePerModuleType() {} func init() { appconfig.RegisterModule(&modulev1.Module{}, appconfig.Provide(ProvideModule), + appconfig.Invoke(InvokeSetMintFn), ) } type ModuleInputs struct { depinject.In - ModuleKey depinject.OwnModuleKey - Config *modulev1.Module - Environment appmodule.Environment - Cdc codec.Codec - MintFn types.MintFn `optional:"true"` - InflationCalculationFn types.InflationCalculationFn `optional:"true"` // deprecated + Config *modulev1.Module + Environment appmodule.Environment + Cdc codec.Codec AccountKeeper types.AccountKeeper BankKeeper types.BankKeeper - StakingKeeper types.StakingKeeper } type ModuleOutputs struct { depinject.Out - MintKeeper keeper.Keeper + MintKeeper *keeper.Keeper Module appmodule.AppModule EpochHooks epochstypes.EpochHooksWrapper } @@ -67,28 +66,23 @@ func ProvideModule(in ModuleInputs) ModuleOutputs { k := keeper.NewKeeper( in.Cdc, in.Environment, - in.StakingKeeper, in.AccountKeeper, in.BankKeeper, feeCollectorName, as, ) - if in.MintFn != nil && in.InflationCalculationFn != nil { - panic("MintFn and InflationCalculationFn cannot both be set") - } + m := NewAppModule(in.Cdc, k, in.AccountKeeper) - // if no mintFn is provided, use the default minting function - if in.MintFn == nil { - // if no inflationCalculationFn is provided, use the default inflation calculation function - if in.InflationCalculationFn == nil { - in.InflationCalculationFn = types.DefaultInflationCalculationFn - } + return ModuleOutputs{MintKeeper: k, Module: m, EpochHooks: epochstypes.EpochHooksWrapper{EpochHooks: m}} +} - in.MintFn = k.DefaultMintFn(in.InflationCalculationFn) +func InvokeSetMintFn(mintKeeper *keeper.Keeper, mintFn types.MintFn, stakingKeeper types.StakingKeeper) error { + if mintFn == nil && stakingKeeper == nil { + return fmt.Errorf("custom minting function or staking keeper must be supplied or available") + } else if mintFn == nil { + mintFn = keeper.DefaultMintFn(types.DefaultInflationCalculationFn, stakingKeeper, mintKeeper) } - m := NewAppModule(in.Cdc, k, in.AccountKeeper, in.MintFn) - - return ModuleOutputs{MintKeeper: k, Module: m, EpochHooks: epochstypes.EpochHooksWrapper{EpochHooks: m}} + return mintKeeper.SetMintFn(mintFn) } diff --git a/x/mint/epoch_hooks.go b/x/mint/epoch_hooks.go index f2a3daf0d8d..d4d9fbe572b 100644 --- a/x/mint/epoch_hooks.go +++ b/x/mint/epoch_hooks.go @@ -17,7 +17,7 @@ func (am AppModule) BeforeEpochStart(ctx context.Context, epochIdentifier string oldMinter := minter - err = am.mintFn(ctx, am.keeper.Environment, &minter, epochIdentifier, epochNumber) + err = am.keeper.MintFn(ctx, &minter, epochIdentifier, epochNumber) if err != nil { return err } diff --git a/x/mint/keeper/abci.go b/x/mint/keeper/abci.go index e59eb174789..85b7d315597 100644 --- a/x/mint/keeper/abci.go +++ b/x/mint/keeper/abci.go @@ -9,7 +9,7 @@ import ( ) // BeginBlocker mints new tokens for the previous block. -func (k Keeper) BeginBlocker(ctx context.Context, mintFn types.MintFn) error { +func (k Keeper) BeginBlocker(ctx context.Context) error { defer telemetry.ModuleMeasureSince(types.ModuleName, telemetry.Now(), telemetry.MetricKeyBeginBlocker) // fetch stored minter & params @@ -22,7 +22,7 @@ func (k Keeper) BeginBlocker(ctx context.Context, mintFn types.MintFn) error { // we pass -1 as epoch number to indicate that this is not an epoch minting, // but a regular block minting. Same with epoch id "block". - err = mintFn(ctx, k.Environment, &minter, "block", -1) + err = k.MintFn(ctx, &minter, "block", -1) if err != nil { return err } diff --git a/x/mint/keeper/genesis_test.go b/x/mint/keeper/genesis_test.go index 839ca62bcc9..4c1b17ccafa 100644 --- a/x/mint/keeper/genesis_test.go +++ b/x/mint/keeper/genesis_test.go @@ -1,12 +1,14 @@ package keeper_test import ( + "context" "testing" "github.com/golang/mock/gomock" "github.com/stretchr/testify/suite" "cosmossdk.io/collections" + "cosmossdk.io/core/appmodule" "cosmossdk.io/log" "cosmossdk.io/math" storetypes "cosmossdk.io/store/types" @@ -30,7 +32,7 @@ type GenesisTestSuite struct { suite.Suite sdkCtx sdk.Context - keeper keeper.Keeper + keeper *keeper.Keeper cdc codec.BinaryCodec accountKeeper types.AccountKeeper key *storetypes.KVStoreKey @@ -51,14 +53,17 @@ func (s *GenesisTestSuite) SetupTest() { s.sdkCtx = testCtx.Ctx s.key = key - stakingKeeper := minttestutil.NewMockStakingKeeper(ctrl) accountKeeper := minttestutil.NewMockAccountKeeper(ctrl) bankKeeper := minttestutil.NewMockBankKeeper(ctrl) s.accountKeeper = accountKeeper accountKeeper.EXPECT().GetModuleAddress(minterAcc.Name).Return(minterAcc.GetAddress()) accountKeeper.EXPECT().GetModuleAccount(s.sdkCtx, minterAcc.Name).Return(minterAcc) - s.keeper = keeper.NewKeeper(s.cdc, runtime.NewEnvironment(runtime.NewKVStoreService(key), log.NewNopLogger()), stakingKeeper, accountKeeper, bankKeeper, "", "") + s.keeper = keeper.NewKeeper(s.cdc, runtime.NewEnvironment(runtime.NewKVStoreService(key), log.NewNopLogger()), accountKeeper, bankKeeper, "", "") + err := s.keeper.SetMintFn(func(ctx context.Context, env appmodule.Environment, minter *types.Minter, epochId string, epochNumber int64) error { + return nil + }) + s.NoError(err) } func (s *GenesisTestSuite) TestImportExportGenesis() { diff --git a/x/mint/keeper/grpc_query.go b/x/mint/keeper/grpc_query.go index 162be9343b3..ad04f607776 100644 --- a/x/mint/keeper/grpc_query.go +++ b/x/mint/keeper/grpc_query.go @@ -8,12 +8,12 @@ import ( var _ types.QueryServer = queryServer{} -func NewQueryServerImpl(k Keeper) types.QueryServer { +func NewQueryServerImpl(k *Keeper) types.QueryServer { return queryServer{k} } type queryServer struct { - k Keeper + k *Keeper } // Params returns params of the mint module. diff --git a/x/mint/keeper/grpc_query_test.go b/x/mint/keeper/grpc_query_test.go index cc941f87788..75b17808af0 100644 --- a/x/mint/keeper/grpc_query_test.go +++ b/x/mint/keeper/grpc_query_test.go @@ -28,7 +28,7 @@ type MintTestSuite struct { ctx sdk.Context queryClient types.QueryClient - mintKeeper keeper.Keeper + mintKeeper *keeper.Keeper } func (suite *MintTestSuite) SetupTest() { @@ -42,14 +42,12 @@ func (suite *MintTestSuite) SetupTest() { ctrl := gomock.NewController(suite.T()) accountKeeper := minttestutil.NewMockAccountKeeper(ctrl) bankKeeper := minttestutil.NewMockBankKeeper(ctrl) - stakingKeeper := minttestutil.NewMockStakingKeeper(ctrl) accountKeeper.EXPECT().GetModuleAddress("mint").Return(sdk.AccAddress{}) suite.mintKeeper = keeper.NewKeeper( encCfg.Codec, env, - stakingKeeper, accountKeeper, bankKeeper, authtypes.FeeCollectorName, diff --git a/x/mint/keeper/keeper.go b/x/mint/keeper/keeper.go index 14ee19cde86..5bb607c0449 100644 --- a/x/mint/keeper/keeper.go +++ b/x/mint/keeper/keeper.go @@ -20,7 +20,6 @@ type Keeper struct { appmodule.Environment cdc codec.BinaryCodec - stakingKeeper types.StakingKeeper bankKeeper types.BankKeeper feeCollectorName string // the address capable of executing a MsgUpdateParams message. Typically, this @@ -30,18 +29,21 @@ type Keeper struct { Schema collections.Schema Params collections.Item[types.Params] Minter collections.Item[types.Minter] + + // mintFn is used to mint new coins during BeginBlock. This function is in charge of + // minting new coins based on arbitrary logic, previously done through InflationCalculationFn. + mintFn types.MintFn } // NewKeeper creates a new mint Keeper instance func NewKeeper( cdc codec.BinaryCodec, env appmodule.Environment, - sk types.StakingKeeper, ak types.AccountKeeper, bk types.BankKeeper, feeCollectorName string, authority string, -) Keeper { +) *Keeper { // ensure mint module account is set if addr := ak.GetModuleAddress(types.ModuleName); addr == nil { panic(fmt.Sprintf("the x/%s module account has not been set", types.ModuleName)) @@ -51,7 +53,6 @@ func NewKeeper( k := Keeper{ Environment: env, cdc: cdc, - stakingKeeper: sk, bankKeeper: bk, feeCollectorName: feeCollectorName, authority: authority, @@ -64,29 +65,25 @@ func NewKeeper( panic(err) } k.Schema = schema - return k -} -// GetAuthority returns the x/mint module's authority. -func (k Keeper) GetAuthority() string { - return k.authority + return &k } -// StakingTokenSupply implements an alias call to the underlying staking keeper's -// StakingTokenSupply to be used in BeginBlocker. -func (k Keeper) StakingTokenSupply(ctx context.Context) (math.Int, error) { - return k.stakingKeeper.StakingTokenSupply(ctx) +// SetMintFn is used to mint new coins during BeginBlock. The mintFn function is in charge of +// minting new coins based on arbitrary logic, previously done through InflationCalculationFn. +func (k *Keeper) SetMintFn(mintFn types.MintFn) error { + k.mintFn = mintFn + return nil } -// BondedRatio implements an alias call to the underlying staking keeper's -// BondedRatio to be used in BeginBlocker. -func (k Keeper) BondedRatio(ctx context.Context) (math.LegacyDec, error) { - return k.stakingKeeper.BondedRatio(ctx) +// GetAuthority returns the x/mint module's authority. +func (k *Keeper) GetAuthority() string { + return k.authority } // MintCoins implements an alias call to the underlying supply keeper's // MintCoins to be used in BeginBlocker. -func (k Keeper) MintCoins(ctx context.Context, newCoins sdk.Coins) error { +func (k *Keeper) MintCoins(ctx context.Context, newCoins sdk.Coins) error { if newCoins.Empty() { // skip as no coins need to be minted return nil @@ -97,11 +94,17 @@ func (k Keeper) MintCoins(ctx context.Context, newCoins sdk.Coins) error { // AddCollectedFees implements an alias call to the underlying supply keeper's // AddCollectedFees to be used in BeginBlocker. -func (k Keeper) AddCollectedFees(ctx context.Context, fees sdk.Coins) error { +func (k *Keeper) AddCollectedFees(ctx context.Context, fees sdk.Coins) error { return k.bankKeeper.SendCoinsFromModuleToModule(ctx, types.ModuleName, k.feeCollectorName, fees) } -func (k Keeper) DefaultMintFn(ic types.InflationCalculationFn) types.MintFn { +func (k *Keeper) MintFn(ctx context.Context, minter *types.Minter, epochId string, epochNumber int64) error { + return k.mintFn(ctx, k.Environment, minter, epochId, epochNumber) +} + +// DefaultMintFn returns a default mint function. It requires the Staking module and the mint keeper. +// The default Mintfn has a requirement on staking as it uses bond to calculate inflation. +func DefaultMintFn(ic types.InflationCalculationFn, staking types.StakingKeeper, k *Keeper) types.MintFn { return func(ctx context.Context, env appmodule.Environment, minter *types.Minter, epochId string, epochNumber int64) error { // the default mint function is called every block, so we only check if epochId is "block" which is // a special value to indicate that this is not an epoch minting, but a regular block minting. @@ -109,12 +112,12 @@ func (k Keeper) DefaultMintFn(ic types.InflationCalculationFn) types.MintFn { return nil } - stakingTokenSupply, err := k.StakingTokenSupply(ctx) + stakingTokenSupply, err := staking.StakingTokenSupply(ctx) if err != nil { return err } - bondedRatio, err := k.BondedRatio(ctx) + bondedRatio, err := staking.BondedRatio(ctx) if err != nil { return err } diff --git a/x/mint/keeper/keeper_test.go b/x/mint/keeper/keeper_test.go index 454967c1fc0..6bec00095a1 100644 --- a/x/mint/keeper/keeper_test.go +++ b/x/mint/keeper/keeper_test.go @@ -29,7 +29,7 @@ const govModuleNameStr = "cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn" type KeeperTestSuite struct { suite.Suite - mintKeeper keeper.Keeper + mintKeeper *keeper.Keeper ctx sdk.Context msgServer types.MsgServer stakingKeeper *minttestutil.MockStakingKeeper @@ -59,7 +59,6 @@ func (s *KeeperTestSuite) SetupTest() { s.mintKeeper = keeper.NewKeeper( encCfg.Codec, env, - stakingKeeper, accountKeeper, bankKeeper, authtypes.FeeCollectorName, @@ -75,40 +74,19 @@ func (s *KeeperTestSuite) SetupTest() { s.msgServer = keeper.NewMsgServerImpl(s.mintKeeper) } -func (s *KeeperTestSuite) TestAliasFunctions() { - stakingTokenSupply := math.NewIntFromUint64(100000000000) - s.stakingKeeper.EXPECT().StakingTokenSupply(s.ctx).Return(stakingTokenSupply, nil) - tokenSupply, err := s.mintKeeper.StakingTokenSupply(s.ctx) - s.NoError(err) - s.Equal(tokenSupply, stakingTokenSupply) - - bondedRatio := math.LegacyNewDecWithPrec(15, 2) - s.stakingKeeper.EXPECT().BondedRatio(s.ctx).Return(bondedRatio, nil) - ratio, err := s.mintKeeper.BondedRatio(s.ctx) - s.NoError(err) - s.Equal(ratio, bondedRatio) - - coins := sdk.NewCoins(sdk.NewCoin("stake", math.NewInt(1000000))) - s.bankKeeper.EXPECT().MintCoins(s.ctx, types.ModuleName, coins).Return(nil) - s.Equal(s.mintKeeper.MintCoins(s.ctx, sdk.NewCoins()), nil) - s.Nil(s.mintKeeper.MintCoins(s.ctx, coins)) - - fees := sdk.NewCoins(sdk.NewCoin("stake", math.NewInt(1000))) - s.bankKeeper.EXPECT().SendCoinsFromModuleToModule(s.ctx, types.ModuleName, authtypes.FeeCollectorName, fees).Return(nil) - s.Nil(s.mintKeeper.AddCollectedFees(s.ctx, fees)) -} - func (s *KeeperTestSuite) TestDefaultMintFn() { s.stakingKeeper.EXPECT().StakingTokenSupply(s.ctx).Return(math.NewIntFromUint64(100000000000), nil).AnyTimes() bondedRatio := math.LegacyNewDecWithPrec(15, 2) s.stakingKeeper.EXPECT().BondedRatio(s.ctx).Return(bondedRatio, nil).AnyTimes() s.bankKeeper.EXPECT().MintCoins(s.ctx, types.ModuleName, sdk.NewCoins(sdk.NewCoin("stake", math.NewInt(792)))).Return(nil) s.bankKeeper.EXPECT().SendCoinsFromModuleToModule(s.ctx, types.ModuleName, authtypes.FeeCollectorName, gomock.Any()).Return(nil) + err := s.mintKeeper.SetMintFn(keeper.DefaultMintFn(types.DefaultInflationCalculationFn, s.stakingKeeper, s.mintKeeper)) + s.NoError(err) minter, err := s.mintKeeper.Minter.Get(s.ctx) s.NoError(err) - err = s.mintKeeper.DefaultMintFn(types.DefaultInflationCalculationFn)(s.ctx, s.mintKeeper.Environment, &minter, "block", 0) + err = s.mintKeeper.MintFn(s.ctx, &minter, "block", 0) s.NoError(err) // set a maxSupply and call again. totalSupply will be bigger than maxSupply. @@ -118,7 +96,7 @@ func (s *KeeperTestSuite) TestDefaultMintFn() { err = s.mintKeeper.Params.Set(s.ctx, params) s.NoError(err) - err = s.mintKeeper.DefaultMintFn(types.DefaultInflationCalculationFn)(s.ctx, s.mintKeeper.Environment, &minter, "block", 0) + err = s.mintKeeper.MintFn(s.ctx, &minter, "block", 0) s.NoError(err) // modify max supply to be almost reached @@ -134,7 +112,7 @@ func (s *KeeperTestSuite) TestDefaultMintFn() { s.bankKeeper.EXPECT().MintCoins(s.ctx, types.ModuleName, sdk.NewCoins(sdk.NewCoin("stake", math.NewInt(2000)))).Return(nil) s.bankKeeper.EXPECT().SendCoinsFromModuleToModule(s.ctx, types.ModuleName, authtypes.FeeCollectorName, gomock.Any()).Return(nil) - err = s.mintKeeper.DefaultMintFn(types.DefaultInflationCalculationFn)(s.ctx, s.mintKeeper.Environment, &minter, "block", 0) + err = s.mintKeeper.MintFn(s.ctx, &minter, "block", 0) s.NoError(err) } @@ -144,12 +122,13 @@ func (s *KeeperTestSuite) TestBeginBlocker() { s.stakingKeeper.EXPECT().BondedRatio(s.ctx).Return(bondedRatio, nil).AnyTimes() s.bankKeeper.EXPECT().MintCoins(s.ctx, types.ModuleName, sdk.NewCoins(sdk.NewCoin("stake", math.NewInt(792)))).Return(nil) s.bankKeeper.EXPECT().SendCoinsFromModuleToModule(s.ctx, types.ModuleName, authtypes.FeeCollectorName, gomock.Any()).Return(nil) - + err := s.mintKeeper.SetMintFn(keeper.DefaultMintFn(types.DefaultInflationCalculationFn, s.stakingKeeper, s.mintKeeper)) + s.NoError(err) // get minter (it should get modified afterwards) minter, err := s.mintKeeper.Minter.Get(s.ctx) s.NoError(err) - err = s.mintKeeper.BeginBlocker(s.ctx, s.mintKeeper.DefaultMintFn(types.DefaultInflationCalculationFn)) + err = s.mintKeeper.BeginBlocker(s.ctx) s.NoError(err) // get minter again and compare @@ -158,10 +137,12 @@ func (s *KeeperTestSuite) TestBeginBlocker() { s.NotEqual(minter, newMinter) // now use a mintfn that doesn't do anything - err = s.mintKeeper.BeginBlocker(s.ctx, func(ctx context.Context, env appmodule.Environment, minter *types.Minter, epochId string, epochNumber int64) error { + err = s.mintKeeper.SetMintFn(func(ctx context.Context, env appmodule.Environment, minter *types.Minter, epochId string, epochNumber int64) error { return nil }) s.NoError(err) + err = s.mintKeeper.BeginBlocker(s.ctx) + s.NoError(err) // get minter again and compare unchangedMinter, err := s.mintKeeper.Minter.Get(s.ctx) diff --git a/x/mint/keeper/migrator.go b/x/mint/keeper/migrator.go index 199757c1f1a..7677fd37c96 100644 --- a/x/mint/keeper/migrator.go +++ b/x/mint/keeper/migrator.go @@ -8,11 +8,11 @@ import ( // Migrator is a struct for handling in-place state migrations. type Migrator struct { - keeper Keeper + keeper *Keeper } // NewMigrator returns Migrator instance for the state migration. -func NewMigrator(k Keeper) Migrator { +func NewMigrator(k *Keeper) Migrator { return Migrator{ keeper: k, } diff --git a/x/mint/keeper/msg_server.go b/x/mint/keeper/msg_server.go index 98cf65ac632..ea72c333dc8 100644 --- a/x/mint/keeper/msg_server.go +++ b/x/mint/keeper/msg_server.go @@ -11,11 +11,11 @@ var _ types.MsgServer = msgServer{} // msgServer is a wrapper of Keeper. type msgServer struct { - Keeper + *Keeper } // NewMsgServerImpl returns an implementation of the x/mint MsgServer interface. -func NewMsgServerImpl(k Keeper) types.MsgServer { +func NewMsgServerImpl(k *Keeper) types.MsgServer { return &msgServer{ Keeper: k, } diff --git a/x/mint/module.go b/x/mint/module.go index 826dba01f6f..52c6324b4a1 100644 --- a/x/mint/module.go +++ b/x/mint/module.go @@ -39,34 +39,21 @@ var ( // AppModule implements an application module for the mint module. type AppModule struct { cdc codec.Codec - keeper keeper.Keeper + keeper *keeper.Keeper authKeeper types.AccountKeeper - - // mintFn is used to mint new coins during BeginBlock. This function is in charge of - // minting new coins based on arbitrary logic, previously done through InflationCalculationFn. - // If mintFn is nil, the default minting logic is used. - mintFn types.MintFn } // NewAppModule creates a new AppModule object. // If the mintFn argument is nil, then the default minting function will be used. func NewAppModule( cdc codec.Codec, - keeper keeper.Keeper, + keeper *keeper.Keeper, ak types.AccountKeeper, - mintFn types.MintFn, ) AppModule { - // If mintFn is nil, use the default minting function. - // This check also happens in ProvideModule when used with depinject. - if mintFn == nil { - mintFn = keeper.DefaultMintFn(types.DefaultInflationCalculationFn) - } - return AppModule{ cdc: cdc, keeper: keeper, authKeeper: ak, - mintFn: mintFn, } } @@ -159,7 +146,7 @@ func (AppModule) ConsensusVersion() uint64 { return ConsensusVersion } // BeginBlock returns the begin blocker for the mint module. func (am AppModule) BeginBlock(ctx context.Context) error { - return am.keeper.BeginBlocker(ctx, am.mintFn) + return am.keeper.BeginBlocker(ctx) } // AppModuleSimulation functions diff --git a/x/mint/module_test.go b/x/mint/module_test.go index d1a618459db..1ba64d0bd8f 100644 --- a/x/mint/module_test.go +++ b/x/mint/module_test.go @@ -27,7 +27,7 @@ const govModuleNameStr = "cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn" type ModuleTestSuite struct { suite.Suite - mintKeeper keeper.Keeper + mintKeeper *keeper.Keeper ctx sdk.Context msgServer types.MsgServer stakingKeeper *minttestutil.MockStakingKeeper @@ -59,22 +59,24 @@ func (s *ModuleTestSuite) SetupTest() { s.mintKeeper = keeper.NewKeeper( encCfg.Codec, env, - stakingKeeper, accountKeeper, bankKeeper, authtypes.FeeCollectorName, govModuleNameStr, ) + err := s.mintKeeper.SetMintFn(keeper.DefaultMintFn(types.DefaultInflationCalculationFn, stakingKeeper, s.mintKeeper)) + s.NoError(err) + s.stakingKeeper = stakingKeeper s.bankKeeper = bankKeeper - err := s.mintKeeper.Params.Set(s.ctx, types.DefaultParams()) + err = s.mintKeeper.Params.Set(s.ctx, types.DefaultParams()) s.NoError(err) s.NoError(s.mintKeeper.Minter.Set(s.ctx, types.DefaultInitialMinter())) s.msgServer = keeper.NewMsgServerImpl(s.mintKeeper) - s.appmodule = mint.NewAppModule(encCfg.Codec, s.mintKeeper, accountKeeper, s.mintKeeper.DefaultMintFn(types.DefaultInflationCalculationFn)) + s.appmodule = mint.NewAppModule(encCfg.Codec, s.mintKeeper, accountKeeper) } func (s *ModuleTestSuite) TestEpochHooks() {