Skip to content

Commit

Permalink
feat: Municipal Inflation: Handler optimisation & Validation fix (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
pbukva authored Aug 11, 2023
1 parent 49044fc commit 6027a27
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 13 deletions.
13 changes: 6 additions & 7 deletions x/mint/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,15 @@ import (

// HandleMunicipalInflation iterates through all other native tokens specified in the minter.MunicipalInflation structure, and processes
// the minting of new coins in line with the respective inflation rate of each denomination
func HandleMunicipalInflation(ctx sdk.Context, k keeper.Keeper) {
minter := k.GetMinter(ctx)
params := k.GetParams(ctx)

func HandleMunicipalInflation(minter *types.Minter, params *types.Params, ctx *sdk.Context, k *keeper.Keeper) {
cache.GMunicipalInflationCache.RefreshIfNecessary(&minter.MunicipalInflation, params.BlocksPerYear)

// iterate through native denominations
for _, pair := range minter.MunicipalInflation {
targetAddress := pair.Inflation.TargetAddress

// gather supply value & calculate number of new tokens created from relevant inflation
totalDenomSupply := k.BankKeeper.GetSupply(ctx, pair.Denom)
totalDenomSupply := k.BankKeeper.GetSupply(*ctx, pair.Denom)

cacheItem, exists := cache.GMunicipalInflationCache.GetInflation(pair.Denom)

Expand All @@ -34,7 +31,7 @@ func HandleMunicipalInflation(ctx sdk.Context, k keeper.Keeper) {

coinsToMint := types.CalculateInflationIssuance(cacheItem.PerBlockInflation, totalDenomSupply)

err := k.MintCoins(ctx, coinsToMint)
err := k.MintCoins(*ctx, coinsToMint)
if err != nil {
panic(err)
}
Expand All @@ -48,7 +45,7 @@ func HandleMunicipalInflation(ctx sdk.Context, k keeper.Keeper) {
panic(err)
}

err = k.BankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, acc, coinsToMint)
err = k.BankKeeper.SendCoinsFromModuleToAccount(*ctx, types.ModuleName, acc, coinsToMint)
if err != nil {
panic(err)
}
Expand All @@ -72,6 +69,8 @@ func BeginBlocker(ctx sdk.Context, k keeper.Keeper) {
minter := k.GetMinter(ctx)
params := k.GetParams(ctx)

HandleMunicipalInflation(&minter, &params, &ctx, &k)

// recalculate inflation rate
totalStakingSupply := k.StakingTokenSupply(ctx)
minter.Inflation = minter.NextInflationRate(params)
Expand Down
3 changes: 1 addition & 2 deletions x/mint/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,8 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
// ConsensusVersion implements AppModule/ConsensusVersion.
func (AppModule) ConsensusVersion() uint64 { return 1 }

// BeginBlock returns the begin blocker for the mint module.
// BeginBlock implements begin block handler for the mint module.
func (am AppModule) BeginBlock(ctx sdk.Context, _ abci.RequestBeginBlock) {
HandleMunicipalInflation(ctx, am.keeper)
BeginBlocker(ctx, am.keeper)
}

Expand Down
5 changes: 3 additions & 2 deletions x/mint/types/inflations.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,15 @@ func (inflation *MunicipalInflation) Validate() error {
}

func ValidateMunicipalInflations(inflations *[]*MunicipalInflationPair) (err error) {
var denoms map[string]struct{}
denoms := map[string]struct{}{}
for _, pair := range *inflations {

_, exists := denoms[pair.Denom]
if exists {
return fmt.Errorf("municipal inflation: denomination \"%s\" defined more than once", pair.Denom)
}

denoms[pair.Denom] = struct{}{}

err = sdk.ValidateDenom(pair.Denom)
if err != nil {
return fmt.Errorf("inflation object param, denom: %s", err)
Expand Down
5 changes: 3 additions & 2 deletions x/mint/types/inflations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func TestBulkValidationOfMunicipalInflations(t *testing.T) {

expectedToFail2_DuplicatedDenom := append(expectedToPass, &types.MunicipalInflationPair{"stake0", types.NewMunicipalInflation(targetAccounts[0].Address.String(), onePercent)})
err = types.ValidateMunicipalInflations(&expectedToFail2_DuplicatedDenom)
require.NoError(t, err)
require.Error(t, err)
}

func TestHandleMunicipalInflation(t *testing.T) {
Expand Down Expand Up @@ -241,8 +241,9 @@ func TestHandleMunicipalInflation(t *testing.T) {

// vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
// TEST SUBJECT: Calling production code for as many times as there is number of blocks in a year

for i := 0; i < int(params.BlocksPerYear); i++ {
mint.HandleMunicipalInflation(ctx, keeper)
mint.HandleMunicipalInflation(&minter, &params, &ctx, &keeper)
}
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Expand Down

0 comments on commit 6027a27

Please sign in to comment.