diff --git a/CHANGELOG.md b/CHANGELOG.md index 787ddee263c3..9fc9e797f3a3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -52,6 +52,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### API Breaking Changes +* (x/mint) [\#10441](https://github.com/cosmos/cosmos-sdk/pull/10441) The `NewAppModule` function now accepts an inflation calculation function as an argument. * [\#10295](https://github.com/cosmos/cosmos-sdk/pull/10295) Remove store type aliases from /types * [\#9695](https://github.com/cosmos/cosmos-sdk/pull/9695) Migrate keys from `Info` -> `Record` * Add new `codec.Codec` argument in: diff --git a/simapp/app.go b/simapp/app.go index 33fcab3e5cbe..6999b0cd5894 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -332,7 +332,7 @@ func NewSimApp( crisis.NewAppModule(&app.CrisisKeeper, skipGenesisInvariants), feegrantmodule.NewAppModule(appCodec, app.AccountKeeper, app.BankKeeper, app.FeeGrantKeeper, app.interfaceRegistry), gov.NewAppModule(appCodec, app.GovKeeper, app.AccountKeeper, app.BankKeeper), - mint.NewAppModule(appCodec, app.MintKeeper, app.AccountKeeper), + mint.NewAppModule(appCodec, app.MintKeeper, app.AccountKeeper, nil), slashing.NewAppModule(appCodec, app.SlashingKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper), distr.NewAppModule(appCodec, app.DistrKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper), staking.NewAppModule(appCodec, app.StakingKeeper, app.AccountKeeper, app.BankKeeper), @@ -384,7 +384,7 @@ func NewSimApp( capability.NewAppModule(appCodec, *app.CapabilityKeeper), feegrantmodule.NewAppModule(appCodec, app.AccountKeeper, app.BankKeeper, app.FeeGrantKeeper, app.interfaceRegistry), gov.NewAppModule(appCodec, app.GovKeeper, app.AccountKeeper, app.BankKeeper), - mint.NewAppModule(appCodec, app.MintKeeper, app.AccountKeeper), + mint.NewAppModule(appCodec, app.MintKeeper, app.AccountKeeper, nil), staking.NewAppModule(appCodec, app.StakingKeeper, app.AccountKeeper, app.BankKeeper), distr.NewAppModule(appCodec, app.DistrKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper), slashing.NewAppModule(appCodec, app.SlashingKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper), diff --git a/x/mint/abci.go b/x/mint/abci.go index 410415d743b8..f8f0c8ce411f 100644 --- a/x/mint/abci.go +++ b/x/mint/abci.go @@ -10,7 +10,7 @@ import ( ) // BeginBlocker mints new tokens for the previous block. -func BeginBlocker(ctx sdk.Context, k keeper.Keeper) { +func BeginBlocker(ctx sdk.Context, k keeper.Keeper, ic types.InflationCalculationFn) { defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyBeginBlocker) // fetch stored minter & params @@ -20,7 +20,7 @@ func BeginBlocker(ctx sdk.Context, k keeper.Keeper) { // recalculate inflation rate totalStakingSupply := k.StakingTokenSupply(ctx) bondedRatio := k.BondedRatio(ctx) - minter.Inflation = minter.NextInflationRate(params, bondedRatio) + minter.Inflation = ic(ctx, minter, params, bondedRatio) minter.AnnualProvisions = minter.NextAnnualProvisions(params, totalStakingSupply) k.SetMinter(ctx, minter) diff --git a/x/mint/module.go b/x/mint/module.go index 27fb7beba05c..be5ba7fdf2f9 100644 --- a/x/mint/module.go +++ b/x/mint/module.go @@ -89,14 +89,23 @@ type AppModule struct { keeper keeper.Keeper authKeeper types.AccountKeeper + + // inflationCalculator is used to calculate the inflation rate during BeginBlock. + // If inflationCalculator is nil, the default inflation calculation logic is used. + inflationCalculator types.InflationCalculationFn } -// NewAppModule creates a new AppModule object -func NewAppModule(cdc codec.Codec, keeper keeper.Keeper, ak types.AccountKeeper) AppModule { +// NewAppModule creates a new AppModule object. If the InflationCalculationFn +// argument is nil, then the SDK's default inflation function will be used. +func NewAppModule(cdc codec.Codec, keeper keeper.Keeper, ak types.AccountKeeper, ic types.InflationCalculationFn) AppModule { + if ic == nil { + ic = types.DefaultInflationCalculationFn + } return AppModule{ - AppModuleBasic: AppModuleBasic{cdc: cdc}, - keeper: keeper, - authKeeper: ak, + AppModuleBasic: AppModuleBasic{cdc: cdc}, + keeper: keeper, + authKeeper: ak, + inflationCalculator: ic, } } @@ -149,7 +158,7 @@ func (AppModule) ConsensusVersion() uint64 { return 1 } // BeginBlock returns the begin blocker for the mint module. func (am AppModule) BeginBlock(ctx sdk.Context, _ abci.RequestBeginBlock) { - BeginBlocker(ctx, am.keeper) + BeginBlocker(ctx, am.keeper, am.inflationCalculator) } // EndBlock returns the end blocker for the mint module. It returns no validator diff --git a/x/mint/types/genesis.go b/x/mint/types/genesis.go index 3d2f761b4a6b..75c69502bb02 100644 --- a/x/mint/types/genesis.go +++ b/x/mint/types/genesis.go @@ -1,5 +1,21 @@ package types +import ( + sdk "github.com/cosmos/cosmos-sdk/types" +) + +// InflationCalculationFn defines the function required to calculate inflation rate during +// BeginBlock. It receives the minter and params stored in the keeper, along with the current +// bondedRatio and returns the newly calculated inflation rate. +// It can be used to specify a custom inflation calculation logic, instead of relying on the +// default logic provided by the sdk. +type InflationCalculationFn func(ctx sdk.Context, minter Minter, params Params, bondedRatio sdk.Dec) sdk.Dec + +// DefaultInflationCalculationFn is the default function used to calculate inflation. +func DefaultInflationCalculationFn(_ sdk.Context, minter Minter, params Params, bondedRatio sdk.Dec) sdk.Dec { + return minter.NextInflationRate(params, bondedRatio) +} + // NewGenesisState creates a new GenesisState object func NewGenesisState(minter Minter, params Params) *GenesisState { return &GenesisState{