From 2256bf820b6db60eaa41ae4ba94ad8b648d846cb Mon Sep 17 00:00:00 2001 From: Arijit Das Date: Tue, 17 Aug 2021 18:13:05 +0530 Subject: [PATCH 1/3] add custom inflation calculation --- simapp/app.go | 4 ++-- x/mint/abci.go | 8 ++++++-- x/mint/module.go | 15 ++++++++++----- x/mint/types/genesis.go | 11 +++++++++++ 4 files changed, 29 insertions(+), 9 deletions(-) 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..80ff7156a7c1 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,11 @@ 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) + if ic == nil { + minter.Inflation = minter.NextInflationRate(params, bondedRatio) + } else { + 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..a0c70f3a03ec 100644 --- a/x/mint/module.go +++ b/x/mint/module.go @@ -89,14 +89,19 @@ 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 { +func NewAppModule(cdc codec.Codec, keeper keeper.Keeper, ak types.AccountKeeper, ic types.InflationCalculationFn) AppModule { return AppModule{ - AppModuleBasic: AppModuleBasic{cdc: cdc}, - keeper: keeper, - authKeeper: ak, + AppModuleBasic: AppModuleBasic{cdc: cdc}, + keeper: keeper, + authKeeper: ak, + inflationCalculator: ic, } } @@ -149,7 +154,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..0b4ae31cdd29 100644 --- a/x/mint/types/genesis.go +++ b/x/mint/types/genesis.go @@ -1,5 +1,16 @@ 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 + // NewGenesisState creates a new GenesisState object func NewGenesisState(minter Minter, params Params) *GenesisState { return &GenesisState{ From d1c874b9bdec3777972b83bcbb3f838f05131b10 Mon Sep 17 00:00:00 2001 From: Arijit Das Date: Wed, 18 Aug 2021 18:11:53 +0530 Subject: [PATCH 2/3] address review comments --- x/mint/abci.go | 6 +----- x/mint/module.go | 3 +++ x/mint/types/genesis.go | 5 +++++ 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/x/mint/abci.go b/x/mint/abci.go index 80ff7156a7c1..f8f0c8ce411f 100644 --- a/x/mint/abci.go +++ b/x/mint/abci.go @@ -20,11 +20,7 @@ func BeginBlocker(ctx sdk.Context, k keeper.Keeper, ic types.InflationCalculatio // recalculate inflation rate totalStakingSupply := k.StakingTokenSupply(ctx) bondedRatio := k.BondedRatio(ctx) - if ic == nil { - minter.Inflation = minter.NextInflationRate(params, bondedRatio) - } else { - minter.Inflation = ic(ctx, minter, 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 a0c70f3a03ec..975c3cfd2b8c 100644 --- a/x/mint/module.go +++ b/x/mint/module.go @@ -97,6 +97,9 @@ type AppModule struct { // NewAppModule creates a new AppModule object 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, diff --git a/x/mint/types/genesis.go b/x/mint/types/genesis.go index 0b4ae31cdd29..7f57b09178ea 100644 --- a/x/mint/types/genesis.go +++ b/x/mint/types/genesis.go @@ -11,6 +11,11 @@ import ( // 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(ctx 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{ From fae0c349dad7f4e1f8f6686c8e6eae90b44ec232 Mon Sep 17 00:00:00 2001 From: Arijit Das Date: Mon, 1 Nov 2021 14:51:06 +0530 Subject: [PATCH 3/3] review comments --- CHANGELOG.md | 1 + x/mint/module.go | 3 ++- x/mint/types/genesis.go | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) 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/x/mint/module.go b/x/mint/module.go index 975c3cfd2b8c..be5ba7fdf2f9 100644 --- a/x/mint/module.go +++ b/x/mint/module.go @@ -95,7 +95,8 @@ type AppModule struct { inflationCalculator types.InflationCalculationFn } -// NewAppModule creates a new AppModule object +// 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 diff --git a/x/mint/types/genesis.go b/x/mint/types/genesis.go index 7f57b09178ea..75c69502bb02 100644 --- a/x/mint/types/genesis.go +++ b/x/mint/types/genesis.go @@ -12,7 +12,7 @@ import ( 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(ctx sdk.Context, minter Minter, params Params, bondedRatio sdk.Dec) sdk.Dec { +func DefaultInflationCalculationFn(_ sdk.Context, minter Minter, params Params, bondedRatio sdk.Dec) sdk.Dec { return minter.NextInflationRate(params, bondedRatio) }