From 27494e83b3ee4fbabef7dfc07bd1e6ecd5046d6b Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Thu, 19 Sep 2024 15:03:21 +0200 Subject: [PATCH 01/18] adr for valset manager --- docs/architecture/adr-076-valset-manager.md | 77 +++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 docs/architecture/adr-076-valset-manager.md diff --git a/docs/architecture/adr-076-valset-manager.md b/docs/architecture/adr-076-valset-manager.md new file mode 100644 index 00000000000..5cc924e14b8 --- /dev/null +++ b/docs/architecture/adr-076-valset-manager.md @@ -0,0 +1,77 @@ +# ADR 76: Valset Manager + +## Changelog + +* 19-09-2024: Created + +## Status + +PROPOSED: Not Implemented + + +## Abstract + +The downfalls of the current staking module is due to the complexity and extra work that it does to provide features that many do not use. The ValsetManager aims to separate the current module into two. First the valset management module and then the sybil resistance mechanism as a plugin into the valset management module. This separation allows chains to have a generalized valset manager which handles the logic of providing data to the consensus engine. The sybil resistance plugins can be adapted, modified or rewritten to suit the needs of the chain. + +## Context + +Many chains use the staking module out of the box because it is available. The staking module was completed in 2019 for the Cosmos Hub, it was designed as one of the earliest Proof of Stake systems, since then there have been many learnings about the Proof of Stake experiment. Today, if a user would like to implement a sybil resistance mechanism they use the staking module as inspiration or wrap the staking module due to the feature set of the module. This complexity comes at a price, the valset manager is a small module that is meant to provide simple primitives for users to use in order to develop simple or complex sybil resistance mechanisms. + +The ValsetManager is a generalized staking module that allows teams to build sybil resistance mechanisms without having to implement the entire staking module. + +## Alternatives + +* Implement new sybil resistance mechanisms every time a team would like to use one, from the ground up. +* Iterate on the current staking module without allowing for custom sybil resistance mechanisms out of the box. + +## Decision + +The decision is to separate the current staking module into two. First the valset management module and then the sybil resistance mechanism as a plugin into the valset management module. + +> This section describes our response to these forces. It is stated in full +> sentences, with active voice. "We will ..." +> {decision body} + +## Consequences + +> This section describes the resulting context, after applying the decision. All +> consequences should be listed here, not just the "positive" ones. A particular +> decision may have positive, negative, and neutral consequences, but all of them +> affect the team and project in the future. + +### Backwards Compatibility + +> All ADRs that introduce backwards incompatibilities must include a section +> describing these incompatibilities and their severity. The ADR must explain +> how the author proposes to deal with these incompatibilities. ADR submissions +> without a sufficient backwards compatibility treatise may be rejected outright. + +### Positive + +> {positive consequences} + +### Negative + +> {negative consequences} + +### Neutral + +> {neutral consequences} + +## Further Discussions + +> While an ADR is in the DRAFT or PROPOSED stage, this section should contain a +> summary of issues to be solved in future iterations (usually referencing comments +> from a pull-request discussion). +> +> Later, this section can optionally list ideas or improvements the author or +> reviewers found during the analysis of this ADR. + +## Test Cases [optional] + +Test cases for an implementation are mandatory for ADRs that are affecting consensus +changes. Other ADRs can choose to include links to test cases if applicable. + +## References + +* {reference link} From e9d7c1d76f313beca79af4ff0ce7dbff71a4bb68 Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Mon, 23 Sep 2024 16:38:25 +0200 Subject: [PATCH 02/18] staking module removal on mint module --- simapp/app.go | 5 ++-- x/mint/depinject.go | 21 +++++----------- x/mint/epoch_hooks.go | 2 +- x/mint/keeper/abci.go | 4 ++-- x/mint/keeper/genesis.go | 4 ++++ x/mint/keeper/genesis_test.go | 8 +++++-- x/mint/keeper/grpc_query_test.go | 2 -- x/mint/keeper/keeper.go | 41 ++++++++++++++++++-------------- x/mint/keeper/keeper_test.go | 38 +++++++---------------------- x/mint/module.go | 19 ++++----------- x/mint/module_test.go | 5 ++-- 11 files changed, 60 insertions(+), 89 deletions(-) diff --git a/simapp/app.go b/simapp/app.go index 1127b73b8ac..f25025f1c00 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -372,7 +372,8 @@ 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) + app.MintKeeper.SetMintFn(mintkeeper.DefaultMintFn(minttypes.DefaultInflationCalculationFn, app.StakingKeeper, app.MintKeeper)) 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 +468,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/x/mint/depinject.go b/x/mint/depinject.go index e580cc096ca..56c95c63aff 100644 --- a/x/mint/depinject.go +++ b/x/mint/depinject.go @@ -64,31 +64,22 @@ func ProvideModule(in ModuleInputs) ModuleOutputs { panic(err) } + if in.MintFn == nil { + panic("mintFn cannot be nil") + } + 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") - } - - // 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 - } - - in.MintFn = k.DefaultMintFn(in.InflationCalculationFn) - } + k.SetMintFn(in.MintFn) - m := NewAppModule(in.Cdc, k, in.AccountKeeper, in.MintFn) + m := NewAppModule(in.Cdc, k, in.AccountKeeper) return ModuleOutputs{MintKeeper: k, Module: m, EpochHooks: epochstypes.EpochHooksWrapper{EpochHooks: m}} } 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..d8445818a17 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, k.Environment, &minter, "block", -1) if err != nil { return err } diff --git a/x/mint/keeper/genesis.go b/x/mint/keeper/genesis.go index 446b9066512..81c8884d065 100644 --- a/x/mint/keeper/genesis.go +++ b/x/mint/keeper/genesis.go @@ -18,6 +18,10 @@ func (keeper Keeper) InitGenesis(ctx context.Context, ak types.AccountKeeper, da ak.GetModuleAccount(ctx, types.ModuleName) + if keeper.mintFn == nil { + panic("mintFn cannot be nil") + } + return nil } diff --git a/x/mint/keeper/genesis_test.go b/x/mint/keeper/genesis_test.go index 839ca62bcc9..036bef937dc 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" @@ -51,14 +53,16 @@ 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, "", "") + s.keeper.SetMintFn(func(ctx context.Context, env appmodule.Environment, minter *types.Minter, epochId string, epochNumber int64) error { + return nil + }) } func (s *GenesisTestSuite) TestImportExportGenesis() { diff --git a/x/mint/keeper/grpc_query_test.go b/x/mint/keeper/grpc_query_test.go index cc941f87788..9729b1d54bd 100644 --- a/x/mint/keeper/grpc_query_test.go +++ b/x/mint/keeper/grpc_query_test.go @@ -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..9c59b32291f 100644 --- a/x/mint/keeper/keeper.go +++ b/x/mint/keeper/keeper.go @@ -2,6 +2,7 @@ package keeper import ( "context" + "errors" "fmt" "cosmossdk.io/collections" @@ -20,7 +21,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,13 +30,14 @@ type Keeper struct { Schema collections.Schema Params collections.Item[types.Params] Minter collections.Item[types.Minter] + + 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, @@ -51,7 +52,6 @@ func NewKeeper( k := Keeper{ Environment: env, cdc: cdc, - stakingKeeper: sk, bankKeeper: bk, feeCollectorName: feeCollectorName, authority: authority, @@ -67,21 +67,20 @@ func NewKeeper( return k } -// GetAuthority returns the x/mint module's authority. -func (k Keeper) GetAuthority() string { - return k.authority -} +// SetMintFn sets the mint function. +// This is a required call by the application developer +func (k *Keeper) SetMintFn(mintFn types.MintFn) error { + if mintFn == nil { + return errors.New("mintFn cannot be nil") + } -// 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) + 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 @@ -101,7 +100,13 @@ 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 +114,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..e0dca2bf76f 100644 --- a/x/mint/keeper/keeper_test.go +++ b/x/mint/keeper/keeper_test.go @@ -59,7 +59,6 @@ func (s *KeeperTestSuite) SetupTest() { s.mintKeeper = keeper.NewKeeper( encCfg.Codec, env, - stakingKeeper, accountKeeper, bankKeeper, authtypes.FeeCollectorName, @@ -75,40 +74,18 @@ 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) + s.mintKeeper.SetMintFn(keeper.DefaultMintFn(types.DefaultInflationCalculationFn, s.stakingKeeper, s.mintKeeper)) 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 +95,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 +111,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 +121,12 @@ 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) - + s.mintKeeper.SetMintFn(keeper.DefaultMintFn(types.DefaultInflationCalculationFn, s.stakingKeeper, s.mintKeeper)) // 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,9 +135,10 @@ 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 { + s.mintKeeper.SetMintFn(func(ctx context.Context, env appmodule.Environment, minter *types.Minter, epochId string, epochNumber int64) error { return nil }) + err = s.mintKeeper.BeginBlocker(s.ctx) s.NoError(err) // get minter again and compare diff --git a/x/mint/module.go b/x/mint/module.go index 826dba01f6f..c6c5f5ca7ee 100644 --- a/x/mint/module.go +++ b/x/mint/module.go @@ -11,6 +11,7 @@ import ( "cosmossdk.io/core/appmodule" "cosmossdk.io/core/registry" "cosmossdk.io/x/mint/keeper" + mintKeeper "cosmossdk.io/x/mint/keeper" "cosmossdk.io/x/mint/simulation" "cosmossdk.io/x/mint/types" @@ -39,34 +40,22 @@ var ( // AppModule implements an application module for the mint module. type AppModule struct { cdc codec.Codec - keeper keeper.Keeper + keeper mintKeeper.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 mintKeeper.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 +148,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..2113ad4fe89 100644 --- a/x/mint/module_test.go +++ b/x/mint/module_test.go @@ -59,12 +59,13 @@ func (s *ModuleTestSuite) SetupTest() { s.mintKeeper = keeper.NewKeeper( encCfg.Codec, env, - stakingKeeper, accountKeeper, bankKeeper, authtypes.FeeCollectorName, govModuleNameStr, ) + s.mintKeeper.SetMintFn(keeper.DefaultMintFn(types.DefaultInflationCalculationFn, stakingKeeper, s.mintKeeper)) + s.stakingKeeper = stakingKeeper s.bankKeeper = bankKeeper @@ -74,7 +75,7 @@ func (s *ModuleTestSuite) SetupTest() { 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() { From dd4603fba7a0cdb3d20c77e2236c87300ea09e41 Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Mon, 23 Sep 2024 16:39:48 +0200 Subject: [PATCH 03/18] remove adr --- docs/architecture/adr-076-valset-manager.md | 77 --------------------- 1 file changed, 77 deletions(-) delete mode 100644 docs/architecture/adr-076-valset-manager.md diff --git a/docs/architecture/adr-076-valset-manager.md b/docs/architecture/adr-076-valset-manager.md deleted file mode 100644 index 5cc924e14b8..00000000000 --- a/docs/architecture/adr-076-valset-manager.md +++ /dev/null @@ -1,77 +0,0 @@ -# ADR 76: Valset Manager - -## Changelog - -* 19-09-2024: Created - -## Status - -PROPOSED: Not Implemented - - -## Abstract - -The downfalls of the current staking module is due to the complexity and extra work that it does to provide features that many do not use. The ValsetManager aims to separate the current module into two. First the valset management module and then the sybil resistance mechanism as a plugin into the valset management module. This separation allows chains to have a generalized valset manager which handles the logic of providing data to the consensus engine. The sybil resistance plugins can be adapted, modified or rewritten to suit the needs of the chain. - -## Context - -Many chains use the staking module out of the box because it is available. The staking module was completed in 2019 for the Cosmos Hub, it was designed as one of the earliest Proof of Stake systems, since then there have been many learnings about the Proof of Stake experiment. Today, if a user would like to implement a sybil resistance mechanism they use the staking module as inspiration or wrap the staking module due to the feature set of the module. This complexity comes at a price, the valset manager is a small module that is meant to provide simple primitives for users to use in order to develop simple or complex sybil resistance mechanisms. - -The ValsetManager is a generalized staking module that allows teams to build sybil resistance mechanisms without having to implement the entire staking module. - -## Alternatives - -* Implement new sybil resistance mechanisms every time a team would like to use one, from the ground up. -* Iterate on the current staking module without allowing for custom sybil resistance mechanisms out of the box. - -## Decision - -The decision is to separate the current staking module into two. First the valset management module and then the sybil resistance mechanism as a plugin into the valset management module. - -> This section describes our response to these forces. It is stated in full -> sentences, with active voice. "We will ..." -> {decision body} - -## Consequences - -> This section describes the resulting context, after applying the decision. All -> consequences should be listed here, not just the "positive" ones. A particular -> decision may have positive, negative, and neutral consequences, but all of them -> affect the team and project in the future. - -### Backwards Compatibility - -> All ADRs that introduce backwards incompatibilities must include a section -> describing these incompatibilities and their severity. The ADR must explain -> how the author proposes to deal with these incompatibilities. ADR submissions -> without a sufficient backwards compatibility treatise may be rejected outright. - -### Positive - -> {positive consequences} - -### Negative - -> {negative consequences} - -### Neutral - -> {neutral consequences} - -## Further Discussions - -> While an ADR is in the DRAFT or PROPOSED stage, this section should contain a -> summary of issues to be solved in future iterations (usually referencing comments -> from a pull-request discussion). -> -> Later, this section can optionally list ideas or improvements the author or -> reviewers found during the analysis of this ADR. - -## Test Cases [optional] - -Test cases for an implementation are mandatory for ADRs that are affecting consensus -changes. Other ADRs can choose to include links to test cases if applicable. - -## References - -* {reference link} From 0d771e4bc01ebc98341e73dcca1b4feb3ffd9ff6 Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Mon, 23 Sep 2024 16:41:28 +0200 Subject: [PATCH 04/18] cleanup --- x/mint/keeper/abci.go | 2 +- x/mint/keeper/keeper.go | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/x/mint/keeper/abci.go b/x/mint/keeper/abci.go index d8445818a17..85b7d315597 100644 --- a/x/mint/keeper/abci.go +++ b/x/mint/keeper/abci.go @@ -22,7 +22,7 @@ func (k Keeper) BeginBlocker(ctx context.Context) 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 = k.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/keeper.go b/x/mint/keeper/keeper.go index 9c59b32291f..5894a95d2cc 100644 --- a/x/mint/keeper/keeper.go +++ b/x/mint/keeper/keeper.go @@ -31,6 +31,9 @@ type Keeper struct { 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. + // If mintFn is nil, the default minting logic is used. mintFn types.MintFn } From 3954247588f48dcf4ce8a59d7bfc71d08741c523 Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Mon, 23 Sep 2024 16:42:29 +0200 Subject: [PATCH 05/18] check errors --- x/mint/keeper/genesis_test.go | 3 ++- x/mint/keeper/keeper_test.go | 9 ++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/x/mint/keeper/genesis_test.go b/x/mint/keeper/genesis_test.go index 036bef937dc..1c96da156ab 100644 --- a/x/mint/keeper/genesis_test.go +++ b/x/mint/keeper/genesis_test.go @@ -60,9 +60,10 @@ func (s *GenesisTestSuite) SetupTest() { accountKeeper.EXPECT().GetModuleAccount(s.sdkCtx, minterAcc.Name).Return(minterAcc) s.keeper = keeper.NewKeeper(s.cdc, runtime.NewEnvironment(runtime.NewKVStoreService(key), log.NewNopLogger()), accountKeeper, bankKeeper, "", "") - s.keeper.SetMintFn(func(ctx context.Context, env appmodule.Environment, minter *types.Minter, epochId string, epochNumber int64) error { + 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/keeper_test.go b/x/mint/keeper/keeper_test.go index e0dca2bf76f..fd6c8579556 100644 --- a/x/mint/keeper/keeper_test.go +++ b/x/mint/keeper/keeper_test.go @@ -80,7 +80,8 @@ func (s *KeeperTestSuite) TestDefaultMintFn() { 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) - s.mintKeeper.SetMintFn(keeper.DefaultMintFn(types.DefaultInflationCalculationFn, s.stakingKeeper, s.mintKeeper)) + 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) @@ -121,7 +122,8 @@ 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) - s.mintKeeper.SetMintFn(keeper.DefaultMintFn(types.DefaultInflationCalculationFn, s.stakingKeeper, s.mintKeeper)) + 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) @@ -135,9 +137,10 @@ func (s *KeeperTestSuite) TestBeginBlocker() { s.NotEqual(minter, newMinter) // now use a mintfn that doesn't do anything - s.mintKeeper.SetMintFn(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) From bad626464af7f251182139a58e589ab19860e41b Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Tue, 24 Sep 2024 12:40:19 +0200 Subject: [PATCH 06/18] make optional for depinject --- x/accounts/README.md | 6 +++--- x/mint/depinject.go | 8 +++----- 2 files changed, 6 insertions(+), 8 deletions(-) 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/depinject.go b/x/mint/depinject.go index 56c95c63aff..096ccac3424 100644 --- a/x/mint/depinject.go +++ b/x/mint/depinject.go @@ -64,10 +64,6 @@ func ProvideModule(in ModuleInputs) ModuleOutputs { panic(err) } - if in.MintFn == nil { - panic("mintFn cannot be nil") - } - k := keeper.NewKeeper( in.Cdc, in.Environment, @@ -76,7 +72,9 @@ func ProvideModule(in ModuleInputs) ModuleOutputs { feeCollectorName, as, ) - + if in.MintFn == nil { + in.MintFn = keeper.DefaultMintFn(types.DefaultInflationCalculationFn, in.StakingKeeper, k) + } k.SetMintFn(in.MintFn) m := NewAppModule(in.Cdc, k, in.AccountKeeper) From 321ea1890eb61a4f01b974242602e0006ab80112 Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Tue, 24 Sep 2024 12:42:57 +0200 Subject: [PATCH 07/18] address comments --- x/mint/keeper/keeper.go | 5 +++-- x/mint/module.go | 5 ++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/x/mint/keeper/keeper.go b/x/mint/keeper/keeper.go index 5894a95d2cc..6ec6005740c 100644 --- a/x/mint/keeper/keeper.go +++ b/x/mint/keeper/keeper.go @@ -70,8 +70,9 @@ func NewKeeper( return k } -// SetMintFn sets the mint function. -// This is a required call by the application developer +// 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. +// If mintFn is nil, the default minting logic is used. func (k *Keeper) SetMintFn(mintFn types.MintFn) error { if mintFn == nil { return errors.New("mintFn cannot be nil") diff --git a/x/mint/module.go b/x/mint/module.go index c6c5f5ca7ee..cbddc56f608 100644 --- a/x/mint/module.go +++ b/x/mint/module.go @@ -11,7 +11,6 @@ import ( "cosmossdk.io/core/appmodule" "cosmossdk.io/core/registry" "cosmossdk.io/x/mint/keeper" - mintKeeper "cosmossdk.io/x/mint/keeper" "cosmossdk.io/x/mint/simulation" "cosmossdk.io/x/mint/types" @@ -40,7 +39,7 @@ var ( // AppModule implements an application module for the mint module. type AppModule struct { cdc codec.Codec - keeper mintKeeper.Keeper + keeper keeper.Keeper authKeeper types.AccountKeeper } @@ -48,7 +47,7 @@ type AppModule struct { // If the mintFn argument is nil, then the default minting function will be used. func NewAppModule( cdc codec.Codec, - keeper mintKeeper.Keeper, + keeper keeper.Keeper, ak types.AccountKeeper, ) AppModule { From 246e4e1d71234c0b0e943a08b3059b0bc05a9a0e Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Tue, 24 Sep 2024 14:37:53 +0200 Subject: [PATCH 08/18] linting fixes --- server/v2/cometbft/mempool/noop.go | 6 ++++-- simapp/app.go | 4 +++- tests/integration/example/example_test.go | 4 ++-- x/mint/depinject.go | 4 +++- x/mint/module.go | 1 - x/mint/module_test.go | 5 +++-- 6 files changed, 15 insertions(+), 9 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 f25025f1c00..7d8d7ef5744 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -373,7 +373,9 @@ func NewSimApp( ) 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) - app.MintKeeper.SetMintFn(mintkeeper.DefaultMintFn(minttypes.DefaultInflationCalculationFn, app.StakingKeeper, app.MintKeeper)) + 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) 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/mint/depinject.go b/x/mint/depinject.go index 096ccac3424..25ae45ce023 100644 --- a/x/mint/depinject.go +++ b/x/mint/depinject.go @@ -75,7 +75,9 @@ func ProvideModule(in ModuleInputs) ModuleOutputs { if in.MintFn == nil { in.MintFn = keeper.DefaultMintFn(types.DefaultInflationCalculationFn, in.StakingKeeper, k) } - k.SetMintFn(in.MintFn) + if err := k.SetMintFn(in.MintFn); err != nil { + panic(err) + } m := NewAppModule(in.Cdc, k, in.AccountKeeper) diff --git a/x/mint/module.go b/x/mint/module.go index cbddc56f608..517879e826b 100644 --- a/x/mint/module.go +++ b/x/mint/module.go @@ -50,7 +50,6 @@ func NewAppModule( keeper keeper.Keeper, ak types.AccountKeeper, ) AppModule { - return AppModule{ cdc: cdc, keeper: keeper, diff --git a/x/mint/module_test.go b/x/mint/module_test.go index 2113ad4fe89..d0853c8f9e6 100644 --- a/x/mint/module_test.go +++ b/x/mint/module_test.go @@ -64,12 +64,13 @@ func (s *ModuleTestSuite) SetupTest() { authtypes.FeeCollectorName, govModuleNameStr, ) - s.mintKeeper.SetMintFn(keeper.DefaultMintFn(types.DefaultInflationCalculationFn, stakingKeeper, s.mintKeeper)) + 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())) From 870debdef621b11383db506a5219eb9dc082d760 Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Tue, 24 Sep 2024 14:51:55 +0200 Subject: [PATCH 09/18] add changelog --- x/mint/CHANGELOG.md | 2 ++ x/mint/depinject.go | 6 ++++-- x/mint/keeper/keeper.go | 1 - 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/x/mint/CHANGELOG.md b/x/mint/CHANGELOG.md index 285fab6b5c3..ebcb6108bd4 100644 --- a/x/mint/CHANGELOG.md +++ b/x/mint/CHANGELOG.md @@ -36,5 +36,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * [#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. +* [#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. ### Bug Fixes diff --git a/x/mint/depinject.go b/x/mint/depinject.go index 25ae45ce023..16d86a752f4 100644 --- a/x/mint/depinject.go +++ b/x/mint/depinject.go @@ -36,7 +36,7 @@ type ModuleInputs struct { AccountKeeper types.AccountKeeper BankKeeper types.BankKeeper - StakingKeeper types.StakingKeeper + StakingKeeper types.StakingKeeper `optional:true` } type ModuleOutputs struct { @@ -72,7 +72,9 @@ func ProvideModule(in ModuleInputs) ModuleOutputs { feeCollectorName, as, ) - if in.MintFn == nil { + if in.MintFn == nil && in.StakingKeeper != nil { + panic("custom minting function or staking keeper must be supplied or available") + } else if in.MintFn == nil { in.MintFn = keeper.DefaultMintFn(types.DefaultInflationCalculationFn, in.StakingKeeper, k) } if err := k.SetMintFn(in.MintFn); err != nil { diff --git a/x/mint/keeper/keeper.go b/x/mint/keeper/keeper.go index 6ec6005740c..f6e80477369 100644 --- a/x/mint/keeper/keeper.go +++ b/x/mint/keeper/keeper.go @@ -72,7 +72,6 @@ func NewKeeper( // 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. -// If mintFn is nil, the default minting logic is used. func (k *Keeper) SetMintFn(mintFn types.MintFn) error { if mintFn == nil { return errors.New("mintFn cannot be nil") From b55219b0cfb4c7476c4f0c6d3c56526117a0aab8 Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Tue, 24 Sep 2024 14:54:00 +0200 Subject: [PATCH 10/18] address comments --- x/mint/CHANGELOG.md | 2 ++ x/mint/depinject.go | 13 ++++++------- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/x/mint/CHANGELOG.md b/x/mint/CHANGELOG.md index ebcb6108bd4..50e8cc9c134 100644 --- a/x/mint/CHANGELOG.md +++ b/x/mint/CHANGELOG.md @@ -38,5 +38,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * [#19367](https://github.com/cosmos/cosmos-sdk/pull/19398) `appmodule.Environment` is received on the Keeper to get access to different application services. * [#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. + ### Bug Fixes diff --git a/x/mint/depinject.go b/x/mint/depinject.go index 16d86a752f4..1af95f2ab6b 100644 --- a/x/mint/depinject.go +++ b/x/mint/depinject.go @@ -27,16 +27,15 @@ func init() { 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 + ModuleKey depinject.OwnModuleKey + Config *modulev1.Module + Environment appmodule.Environment + Cdc codec.Codec + MintFn types.MintFn `optional:"true"` AccountKeeper types.AccountKeeper BankKeeper types.BankKeeper - StakingKeeper types.StakingKeeper `optional:true` + StakingKeeper types.StakingKeeper `optional:"true"` } type ModuleOutputs struct { From ab8fc1d9c276aec4f5b34f63a60e4cc706f622e7 Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Tue, 24 Sep 2024 15:14:51 +0200 Subject: [PATCH 11/18] invoker --- x/mint/depinject.go | 25 +++++++++++++++++-------- x/mint/keeper/keeper.go | 2 +- x/mint/keeper/keeper_test.go | 4 ++-- x/mint/module_test.go | 2 +- 4 files changed, 21 insertions(+), 12 deletions(-) diff --git a/x/mint/depinject.go b/x/mint/depinject.go index 1af95f2ab6b..9a1f7bb3d63 100644 --- a/x/mint/depinject.go +++ b/x/mint/depinject.go @@ -21,6 +21,7 @@ func (am AppModule) IsOnePerModuleType() {} func init() { appconfig.RegisterModule(&modulev1.Module{}, appconfig.Provide(ProvideModule), + appconfig.Invoke(InvokeMintFnCreation), ) } @@ -71,16 +72,24 @@ func ProvideModule(in ModuleInputs) ModuleOutputs { feeCollectorName, as, ) - if in.MintFn == nil && in.StakingKeeper != nil { - panic("custom minting function or staking keeper must be supplied or available") - } else if in.MintFn == nil { - in.MintFn = keeper.DefaultMintFn(types.DefaultInflationCalculationFn, in.StakingKeeper, k) - } - if err := k.SetMintFn(in.MintFn); err != nil { - panic(err) - } m := NewAppModule(in.Cdc, k, in.AccountKeeper) return ModuleOutputs{MintKeeper: k, Module: m, EpochHooks: epochstypes.EpochHooksWrapper{EpochHooks: m}} } + +func InvokeMintFnCreation(config *modulev1.Module, mintFn types.MintFn, stakingKeeper types.StakingKeeper, mintKeeper *keeper.Keeper) error { + // all arguments to invokers are optional + if mintKeeper == nil || config == nil { + return nil + } + + if mintFn == nil { + mintFn = keeper.DefaultMintFn(types.DefaultInflationCalculationFn, stakingKeeper, mintKeeper) + } + if err := mintKeeper.SetMintFn(mintFn); err != nil { + return err + } + + return nil +} diff --git a/x/mint/keeper/keeper.go b/x/mint/keeper/keeper.go index f6e80477369..7fb50d2cd08 100644 --- a/x/mint/keeper/keeper.go +++ b/x/mint/keeper/keeper.go @@ -109,7 +109,7 @@ func (k Keeper) MintFn(ctx context.Context, minter *types.Minter, epochId string // 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 { +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. diff --git a/x/mint/keeper/keeper_test.go b/x/mint/keeper/keeper_test.go index fd6c8579556..913f4fa0223 100644 --- a/x/mint/keeper/keeper_test.go +++ b/x/mint/keeper/keeper_test.go @@ -80,7 +80,7 @@ func (s *KeeperTestSuite) TestDefaultMintFn() { 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)) + err := s.mintKeeper.SetMintFn(keeper.DefaultMintFn(types.DefaultInflationCalculationFn, s.stakingKeeper, &s.mintKeeper)) s.NoError(err) minter, err := s.mintKeeper.Minter.Get(s.ctx) @@ -122,7 +122,7 @@ 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)) + 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) diff --git a/x/mint/module_test.go b/x/mint/module_test.go index d0853c8f9e6..bf962133e03 100644 --- a/x/mint/module_test.go +++ b/x/mint/module_test.go @@ -64,7 +64,7 @@ func (s *ModuleTestSuite) SetupTest() { authtypes.FeeCollectorName, govModuleNameStr, ) - err := s.mintKeeper.SetMintFn(keeper.DefaultMintFn(types.DefaultInflationCalculationFn, stakingKeeper, s.mintKeeper)) + err := s.mintKeeper.SetMintFn(keeper.DefaultMintFn(types.DefaultInflationCalculationFn, stakingKeeper, &s.mintKeeper)) s.NoError(err) s.stakingKeeper = stakingKeeper From f21026bb683e7fbeeb78e29d36e12db5fa848009 Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Tue, 24 Sep 2024 15:16:33 +0200 Subject: [PATCH 12/18] simapp test --- simapp/app.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/simapp/app.go b/simapp/app.go index 7d8d7ef5744..5d202466c2c 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -373,7 +373,7 @@ func NewSimApp( ) 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 { + if err := app.MintKeeper.SetMintFn(mintkeeper.DefaultMintFn(minttypes.DefaultInflationCalculationFn, app.StakingKeeper, &app.MintKeeper)); err != nil { panic(err) } From dbde3913a2012520d5c310704d2332c3fb0e4aa7 Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Tue, 24 Sep 2024 15:32:06 +0200 Subject: [PATCH 13/18] fix --- x/mint/keeper/genesis.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/x/mint/keeper/genesis.go b/x/mint/keeper/genesis.go index 81c8884d065..446b9066512 100644 --- a/x/mint/keeper/genesis.go +++ b/x/mint/keeper/genesis.go @@ -18,10 +18,6 @@ func (keeper Keeper) InitGenesis(ctx context.Context, ak types.AccountKeeper, da ak.GetModuleAccount(ctx, types.ModuleName) - if keeper.mintFn == nil { - panic("mintFn cannot be nil") - } - return nil } From 3154eb4d39dc08f2a9f76dc92004d7831872a979 Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Tue, 24 Sep 2024 15:33:39 +0200 Subject: [PATCH 14/18] fix --- x/mint/depinject.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/x/mint/depinject.go b/x/mint/depinject.go index 9a1f7bb3d63..c2b18508b3d 100644 --- a/x/mint/depinject.go +++ b/x/mint/depinject.go @@ -84,7 +84,9 @@ func InvokeMintFnCreation(config *modulev1.Module, mintFn types.MintFn, stakingK return nil } - if mintFn == nil { + if mintFn == nil && stakingKeeper != nil { + panic("custom minting function or staking keeper must be supplied or available") + } else if mintFn == nil { mintFn = keeper.DefaultMintFn(types.DefaultInflationCalculationFn, stakingKeeper, mintKeeper) } if err := mintKeeper.SetMintFn(mintFn); err != nil { From 3d0fe07845a1019812785a40ba464fdbf15e246b Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Tue, 24 Sep 2024 15:35:51 +0200 Subject: [PATCH 15/18] fix --- x/mint/depinject.go | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/x/mint/depinject.go b/x/mint/depinject.go index c2b18508b3d..277acda13a3 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" @@ -78,14 +80,9 @@ func ProvideModule(in ModuleInputs) ModuleOutputs { return ModuleOutputs{MintKeeper: k, Module: m, EpochHooks: epochstypes.EpochHooksWrapper{EpochHooks: m}} } -func InvokeMintFnCreation(config *modulev1.Module, mintFn types.MintFn, stakingKeeper types.StakingKeeper, mintKeeper *keeper.Keeper) error { - // all arguments to invokers are optional - if mintKeeper == nil || config == nil { - return nil - } - +func InvokeMintFnCreation(mintFn types.MintFn, stakingKeeper types.StakingKeeper, mintKeeper *keeper.Keeper) error { if mintFn == nil && stakingKeeper != nil { - panic("custom minting function or staking keeper must be supplied or available") + 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) } From ace9fd4d8a1a79a2780dd3d54a0bb2a579e744ef Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Tue, 24 Sep 2024 16:13:14 +0200 Subject: [PATCH 16/18] fix --- x/mint/depinject.go | 8 +++----- x/mint/keeper/keeper.go | 6 ------ 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/x/mint/depinject.go b/x/mint/depinject.go index 277acda13a3..cff705feca1 100644 --- a/x/mint/depinject.go +++ b/x/mint/depinject.go @@ -34,17 +34,15 @@ type ModuleInputs struct { Config *modulev1.Module Environment appmodule.Environment Cdc codec.Codec - MintFn types.MintFn `optional:"true"` AccountKeeper types.AccountKeeper BankKeeper types.BankKeeper - StakingKeeper types.StakingKeeper `optional:"true"` } type ModuleOutputs struct { depinject.Out - MintKeeper keeper.Keeper + MintKeeper *keeper.Keeper Module appmodule.AppModule EpochHooks epochstypes.EpochHooksWrapper } @@ -77,11 +75,11 @@ func ProvideModule(in ModuleInputs) ModuleOutputs { m := NewAppModule(in.Cdc, k, in.AccountKeeper) - return ModuleOutputs{MintKeeper: k, Module: m, EpochHooks: epochstypes.EpochHooksWrapper{EpochHooks: m}} + return ModuleOutputs{MintKeeper: &k, Module: m, EpochHooks: epochstypes.EpochHooksWrapper{EpochHooks: m}} } func InvokeMintFnCreation(mintFn types.MintFn, stakingKeeper types.StakingKeeper, mintKeeper *keeper.Keeper) error { - if mintFn == nil && stakingKeeper != nil { + 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) diff --git a/x/mint/keeper/keeper.go b/x/mint/keeper/keeper.go index 7fb50d2cd08..78edf2e4467 100644 --- a/x/mint/keeper/keeper.go +++ b/x/mint/keeper/keeper.go @@ -2,7 +2,6 @@ package keeper import ( "context" - "errors" "fmt" "cosmossdk.io/collections" @@ -33,7 +32,6 @@ type Keeper struct { // 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 } @@ -73,10 +71,6 @@ func NewKeeper( // 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 { - if mintFn == nil { - return errors.New("mintFn cannot be nil") - } - k.mintFn = mintFn return nil } From da9336973087de25bc9d898200412f94eaea596c Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Tue, 24 Sep 2024 21:39:42 +0200 Subject: [PATCH 17/18] correct pointer --- simapp/app.go | 4 ++-- x/mint/depinject.go | 10 +++------- x/mint/keeper/genesis_test.go | 2 +- x/mint/keeper/grpc_query.go | 4 ++-- x/mint/keeper/grpc_query_test.go | 2 +- x/mint/keeper/keeper.go | 13 +++++++------ x/mint/keeper/keeper_test.go | 6 +++--- x/mint/keeper/migrator.go | 4 ++-- x/mint/keeper/msg_server.go | 4 ++-- x/mint/module.go | 4 ++-- x/mint/module_test.go | 4 ++-- 11 files changed, 27 insertions(+), 30 deletions(-) diff --git a/simapp/app.go b/simapp/app.go index 5d202466c2c..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 @@ -373,7 +373,7 @@ func NewSimApp( ) 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 { + if err := app.MintKeeper.SetMintFn(mintkeeper.DefaultMintFn(minttypes.DefaultInflationCalculationFn, app.StakingKeeper, app.MintKeeper)); err != nil { panic(err) } diff --git a/x/mint/depinject.go b/x/mint/depinject.go index cff705feca1..841e318a5e5 100644 --- a/x/mint/depinject.go +++ b/x/mint/depinject.go @@ -30,7 +30,6 @@ func init() { type ModuleInputs struct { depinject.In - ModuleKey depinject.OwnModuleKey Config *modulev1.Module Environment appmodule.Environment Cdc codec.Codec @@ -75,18 +74,15 @@ func ProvideModule(in ModuleInputs) ModuleOutputs { m := NewAppModule(in.Cdc, k, in.AccountKeeper) - return ModuleOutputs{MintKeeper: &k, Module: m, EpochHooks: epochstypes.EpochHooksWrapper{EpochHooks: m}} + return ModuleOutputs{MintKeeper: k, Module: m, EpochHooks: epochstypes.EpochHooksWrapper{EpochHooks: m}} } -func InvokeMintFnCreation(mintFn types.MintFn, stakingKeeper types.StakingKeeper, mintKeeper *keeper.Keeper) error { +func InvokeMintFnCreation(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) } - if err := mintKeeper.SetMintFn(mintFn); err != nil { - return err - } - return nil + return mintKeeper.SetMintFn(mintFn) } diff --git a/x/mint/keeper/genesis_test.go b/x/mint/keeper/genesis_test.go index 1c96da156ab..4c1b17ccafa 100644 --- a/x/mint/keeper/genesis_test.go +++ b/x/mint/keeper/genesis_test.go @@ -32,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 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 9729b1d54bd..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() { diff --git a/x/mint/keeper/keeper.go b/x/mint/keeper/keeper.go index 78edf2e4467..5bb607c0449 100644 --- a/x/mint/keeper/keeper.go +++ b/x/mint/keeper/keeper.go @@ -43,7 +43,7 @@ func NewKeeper( 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)) @@ -65,7 +65,8 @@ func NewKeeper( panic(err) } k.Schema = schema - return k + + return &k } // SetMintFn is used to mint new coins during BeginBlock. The mintFn function is in charge of @@ -76,13 +77,13 @@ func (k *Keeper) SetMintFn(mintFn types.MintFn) error { } // GetAuthority returns the x/mint module's authority. -func (k Keeper) GetAuthority() string { +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 @@ -93,11 +94,11 @@ 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) MintFn(ctx context.Context, minter *types.Minter, epochId string, epochNumber int64) error { +func (k *Keeper) MintFn(ctx context.Context, minter *types.Minter, epochId string, epochNumber int64) error { return k.mintFn(ctx, k.Environment, minter, epochId, epochNumber) } diff --git a/x/mint/keeper/keeper_test.go b/x/mint/keeper/keeper_test.go index 913f4fa0223..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 @@ -80,7 +80,7 @@ func (s *KeeperTestSuite) TestDefaultMintFn() { 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)) + err := s.mintKeeper.SetMintFn(keeper.DefaultMintFn(types.DefaultInflationCalculationFn, s.stakingKeeper, s.mintKeeper)) s.NoError(err) minter, err := s.mintKeeper.Minter.Get(s.ctx) @@ -122,7 +122,7 @@ 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)) + 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) 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 517879e826b..52c6324b4a1 100644 --- a/x/mint/module.go +++ b/x/mint/module.go @@ -39,7 +39,7 @@ 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 } @@ -47,7 +47,7 @@ type AppModule struct { // 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, ) AppModule { return AppModule{ diff --git a/x/mint/module_test.go b/x/mint/module_test.go index bf962133e03..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 @@ -64,7 +64,7 @@ func (s *ModuleTestSuite) SetupTest() { authtypes.FeeCollectorName, govModuleNameStr, ) - err := s.mintKeeper.SetMintFn(keeper.DefaultMintFn(types.DefaultInflationCalculationFn, stakingKeeper, &s.mintKeeper)) + err := s.mintKeeper.SetMintFn(keeper.DefaultMintFn(types.DefaultInflationCalculationFn, stakingKeeper, s.mintKeeper)) s.NoError(err) s.stakingKeeper = stakingKeeper From 1b006bf3a31bea8b5bcf43681c38b65927e441d1 Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Tue, 24 Sep 2024 21:44:05 +0200 Subject: [PATCH 18/18] cl + nit --- x/mint/CHANGELOG.md | 6 +++--- x/mint/depinject.go | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/x/mint/CHANGELOG.md b/x/mint/CHANGELOG.md index 50e8cc9c134..749a0680d1e 100644 --- a/x/mint/CHANGELOG.md +++ b/x/mint/CHANGELOG.md @@ -32,13 +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. +* [#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. - - -### Bug Fixes diff --git a/x/mint/depinject.go b/x/mint/depinject.go index 841e318a5e5..db36920101d 100644 --- a/x/mint/depinject.go +++ b/x/mint/depinject.go @@ -23,7 +23,7 @@ func (am AppModule) IsOnePerModuleType() {} func init() { appconfig.RegisterModule(&modulev1.Module{}, appconfig.Provide(ProvideModule), - appconfig.Invoke(InvokeMintFnCreation), + appconfig.Invoke(InvokeSetMintFn), ) } @@ -77,7 +77,7 @@ func ProvideModule(in ModuleInputs) ModuleOutputs { return ModuleOutputs{MintKeeper: k, Module: m, EpochHooks: epochstypes.EpochHooksWrapper{EpochHooks: m}} } -func InvokeMintFnCreation(mintKeeper *keeper.Keeper, mintFn types.MintFn, stakingKeeper types.StakingKeeper) error { +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 {