From 6634190997c563c7d2e6db71648c7ed735bc6e77 Mon Sep 17 00:00:00 2001 From: Peter Bukva Date: Fri, 11 Aug 2023 15:55:41 +0100 Subject: [PATCH 1/3] Municipal Inflation: Handler optimisation --- x/mint/abci.go | 13 ++++++------- x/mint/module.go | 3 +-- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/x/mint/abci.go b/x/mint/abci.go index a69aa42966..283cb57bdc 100644 --- a/x/mint/abci.go +++ b/x/mint/abci.go @@ -13,10 +13,7 @@ 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 @@ -24,7 +21,7 @@ func HandleMunicipalInflation(ctx sdk.Context, k keeper.Keeper) { 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) @@ -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) } @@ -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) } @@ -72,6 +69,8 @@ func BeginBlocker(ctx sdk.Context, k keeper.Keeper) { minter := k.GetMinter(ctx) params := k.GetParams(ctx) + HandleMunicipalInflation(&minter, ¶ms, &ctx, &k) + // recalculate inflation rate totalStakingSupply := k.StakingTokenSupply(ctx) minter.Inflation = minter.NextInflationRate(params) diff --git a/x/mint/module.go b/x/mint/module.go index fa17e8f3f3..e397e3cbc1 100644 --- a/x/mint/module.go +++ b/x/mint/module.go @@ -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) } From a9a046f952e08fe3c1e9e1e1bed4f308d2663da4 Mon Sep 17 00:00:00 2001 From: Peter Bukva Date: Fri, 11 Aug 2023 16:24:17 +0100 Subject: [PATCH 2/3] Fixing the `ValidateMunicipalInflations(...)` and unit tests --- x/mint/types/inflations.go | 5 +++-- x/mint/types/inflations_test.go | 8 ++++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/x/mint/types/inflations.go b/x/mint/types/inflations.go index d0c27fb6a7..8198448b1e 100644 --- a/x/mint/types/inflations.go +++ b/x/mint/types/inflations.go @@ -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) diff --git a/x/mint/types/inflations_test.go b/x/mint/types/inflations_test.go index 433045e8fd..32a4c6ca04 100644 --- a/x/mint/types/inflations_test.go +++ b/x/mint/types/inflations_test.go @@ -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) { @@ -241,8 +241,12 @@ 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) + minter := keeper.GetMinter(ctx) + params := keeper.GetParams(ctx) + + mint.HandleMunicipalInflation(&minter, ¶ms, &ctx, &keeper) } // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ From 65d8f3c6e0cf5f67555f77f697ae7de51ae86d83 Mon Sep 17 00:00:00 2001 From: Peter Bukva Date: Fri, 11 Aug 2023 16:28:46 +0100 Subject: [PATCH 3/3] Dropping unnecessary instantiation in unit test --- x/mint/types/inflations_test.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/x/mint/types/inflations_test.go b/x/mint/types/inflations_test.go index 32a4c6ca04..1375f7b79a 100644 --- a/x/mint/types/inflations_test.go +++ b/x/mint/types/inflations_test.go @@ -243,9 +243,6 @@ func TestHandleMunicipalInflation(t *testing.T) { // 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++ { - minter := keeper.GetMinter(ctx) - params := keeper.GetParams(ctx) - mint.HandleMunicipalInflation(&minter, ¶ms, &ctx, &keeper) } // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^