From a8e38d62e2f735f20d62c6ba07e18f46ee2944c9 Mon Sep 17 00:00:00 2001 From: Bryan White Date: Mon, 2 Dec 2024 15:42:31 +0100 Subject: [PATCH] [Tokenomics] refactor: `GlobalInflationPerClaim` var usage to param usage (#953) ## Summary Replace usage of `tlm.GlobalInflationPerClaim` with the new param and delete it. ## Issue - `TODO_BETA` ## Type of change Select one or more from the following: - [ ] New feature, functionality or library - [ ] Consensus breaking; add the `consensus-breaking` label if so. See #791 for details - [ ] Bug fix - [x] Code health or cleanup - [ ] Documentation - [ ] Other (specify) ## Testing - [ ] **Documentation**: `make docusaurus_start`; only needed if you make doc changes - [x] **Unit Tests**: `make go_develop_and_test` - [ ] **LocalNet E2E Tests**: `make test_e2e` - [x] **DevNet E2E Tests**: Add the `devnet-test-e2e` label to the PR. ## Sanity Checklist - [x] I have tested my changes using the available tooling - [ ] I have commented my code - [x] I have performed a self-review of my own code; both comments & source code - [ ] I create and reference any new tickets, if applicable - [ ] I have left TODOs throughout the codebase, if applicable --------- Co-authored-by: red-0ne --- .../integration/application/min_stake_test.go | 6 ++++-- x/tokenomics/keeper/token_logic_modules.go | 11 +++++----- .../keeper/token_logic_modules_test.go | 20 +++++++++---------- .../token_logic_module/global_mint.go | 18 +++++++---------- .../reimbursement_requests.go | 6 ++++-- 5 files changed, 30 insertions(+), 31 deletions(-) diff --git a/tests/integration/application/min_stake_test.go b/tests/integration/application/min_stake_test.go index b8211d6ec..a12788cc3 100644 --- a/tests/integration/application/min_stake_test.go +++ b/tests/integration/application/min_stake_test.go @@ -239,7 +239,8 @@ func (s *applicationMinStakeTestSuite) getExpectedApp(claim *prooftypes.Claim) * expectedBurnCoin, err := claim.GetClaimeduPOKT(sharedParams, relayMiningDifficulty) require.NoError(s.T(), err) - globalInflationAmt, _ := tlm.CalculateGlobalPerClaimMintInflationFromSettlementAmount(expectedBurnCoin) + globalInflationPerClaim := s.keepers.Keeper.GetParams(s.ctx).GlobalInflationPerClaim + globalInflationAmt, _ := tlm.CalculateGlobalPerClaimMintInflationFromSettlementAmount(expectedBurnCoin, globalInflationPerClaim) expectedEndStake := s.appStake.Sub(expectedBurnCoin).Sub(globalInflationAmt) return &apptypes.Application{ Address: s.appBech32, @@ -314,7 +315,8 @@ func (s *applicationMinStakeTestSuite) assertAppStakeIsReturnedToBalance() { expectedAppBurn := int64(s.numRelays * s.numComputeUnitsPerRelay * sharedtypes.DefaultComputeUnitsToTokensMultiplier) expectedAppBurnCoin := cosmostypes.NewInt64Coin(volatile.DenomuPOKT, expectedAppBurn) - globalInflationCoin, _ := tlm.CalculateGlobalPerClaimMintInflationFromSettlementAmount(expectedAppBurnCoin) + globalInflationPerClaim := s.keepers.Keeper.GetParams(s.ctx).GlobalInflationPerClaim + globalInflationCoin, _ := tlm.CalculateGlobalPerClaimMintInflationFromSettlementAmount(expectedAppBurnCoin, globalInflationPerClaim) expectedAppBalance := s.appStake.Sub(expectedAppBurnCoin).Sub(globalInflationCoin) appBalance := s.getAppBalance() diff --git a/x/tokenomics/keeper/token_logic_modules.go b/x/tokenomics/keeper/token_logic_modules.go index fbf95db66..876dddb28 100644 --- a/x/tokenomics/keeper/token_logic_modules.go +++ b/x/tokenomics/keeper/token_logic_modules.go @@ -293,7 +293,8 @@ func (k Keeper) ensureClaimAmountLimits( // The application should have enough stake to cover for the global mint reimbursement. // This amount is deducted from the maximum claimable amount. - globalInflationCoin, _ := tlm.CalculateGlobalPerClaimMintInflationFromSettlementAmount(claimSettlementCoin) + globalInflationPerClaim := k.GetParams(ctx).GlobalInflationPerClaim + globalInflationCoin, _ := tlm.CalculateGlobalPerClaimMintInflationFromSettlementAmount(claimSettlementCoin, globalInflationPerClaim) globalInflationAmt := globalInflationCoin.Amount minRequiredAppStakeAmt := claimSettlementCoin.Amount.Add(globalInflationAmt) totalClaimedCoin := sdk.NewCoin(volatile.DenomuPOKT, minRequiredAppStakeAmt) @@ -312,7 +313,7 @@ func (k Keeper) ensureClaimAmountLimits( maxClaimableAmt := appStake.Amount. Quo(math.NewInt(numSuppliersPerSession)). Quo(math.NewInt(numPendingSessions)) - maxClaimSettlementAmt := supplierAppStakeToMaxSettlementAmount(maxClaimableAmt) + maxClaimSettlementAmt := supplierAppStakeToMaxSettlementAmount(maxClaimableAmt, globalInflationPerClaim) // Check if the claimable amount is capped by the max claimable amount. // As per the Relay Mining paper, the Supplier claim MUST NOT exceed the application's @@ -323,7 +324,7 @@ func (k Keeper) ensureClaimAmountLimits( supplier.GetOperatorAddress(), application.GetAddress(), maxClaimableAmt, claimSettlementCoin.Amount)) minRequiredAppStakeAmt = maxClaimableAmt - maxClaimSettlementAmt = supplierAppStakeToMaxSettlementAmount(minRequiredAppStakeAmt) + maxClaimSettlementAmt = supplierAppStakeToMaxSettlementAmount(minRequiredAppStakeAmt, globalInflationPerClaim) } // Nominal case: The claimable amount is within the limits set by Relay Mining. @@ -361,9 +362,9 @@ func (k Keeper) ensureClaimAmountLimits( // stake = maxSettlementAmt + (maxSettlementAmt * GlobalInflationPerClaim) // stake = maxSettlementAmt * (1 + GlobalInflationPerClaim) // maxSettlementAmt = stake / (1 + GlobalInflationPerClaim) -func supplierAppStakeToMaxSettlementAmount(stakeAmount math.Int) math.Int { +func supplierAppStakeToMaxSettlementAmount(stakeAmount math.Int, globalInflationPerClaim float64) math.Int { stakeAmountFloat := big.NewFloat(0).SetInt(stakeAmount.BigInt()) - maxSettlementAmountFloat := big.NewFloat(0).Quo(stakeAmountFloat, big.NewFloat(1+tlm.GlobalInflationPerClaim)) + maxSettlementAmountFloat := big.NewFloat(0).Quo(stakeAmountFloat, big.NewFloat(1+globalInflationPerClaim)) settlementAmount, _ := maxSettlementAmountFloat.Int(nil) return math.NewIntFromBigInt(settlementAmount) diff --git a/x/tokenomics/keeper/token_logic_modules_test.go b/x/tokenomics/keeper/token_logic_modules_test.go index 4a1254db8..b70d32608 100644 --- a/x/tokenomics/keeper/token_logic_modules_test.go +++ b/x/tokenomics/keeper/token_logic_modules_test.go @@ -76,11 +76,10 @@ func TestProcessTokenLogicModules_TLMBurnEqualsMint_Valid(t *testing.T) { require.NoError(t, err) // TODO_TECHDEBT: Setting inflation to zero so we are testing the BurnEqualsMint logic exclusively. // Once it is a governance param, update it using the keeper above. - prevInflationValue := tlm.GlobalInflationPerClaim - tlm.GlobalInflationPerClaim = 0 - t.Cleanup(func() { - tlm.GlobalInflationPerClaim = prevInflationValue - }) + tokenomicsParams := keepers.Keeper.GetParams(ctx) + tokenomicsParams.GlobalInflationPerClaim = 0 + err = keepers.Keeper.SetParams(ctx, tokenomicsParams) + require.NoError(t, err) // Add a new application with non-zero app stake end balance to assert against. appStake := cosmostypes.NewCoin(volatile.DenomuPOKT, appInitialStake) @@ -218,11 +217,10 @@ func TestProcessTokenLogicModules_TLMBurnEqualsMint_Valid_SupplierExceedsMaxClai require.NoError(t, err) // TODO_TECHDEBT: Setting inflation to zero so we are testing the BurnEqualsMint logic exclusively. // Once it is a governance param, update it using the keeper above. - prevInflationValue := tlm.GlobalInflationPerClaim - tlm.GlobalInflationPerClaim = 0 - t.Cleanup(func() { - tlm.GlobalInflationPerClaim = prevInflationValue - }) + tokenomicsParams := keepers.Keeper.GetParams(ctx) + tokenomicsParams.GlobalInflationPerClaim = 0 + err = keepers.Keeper.SetParams(ctx, tokenomicsParams) + require.NoError(t, err) // Add a new application with non-zero app stake end balance to assert against. appStake := cosmostypes.NewCoin(volatile.DenomuPOKT, appInitialStake) @@ -443,7 +441,7 @@ func TestProcessTokenLogicModules_TLMGlobalMint_Valid_MintDistributionCorrect(t } // Compute mint per actor - numTokensMinted := numTokensClaimed * tlm.GlobalInflationPerClaim + numTokensMinted := numTokensClaimed * keepers.Keeper.GetParams(ctx).GlobalInflationPerClaim numTokensMintedInt := cosmosmath.NewIntFromUint64(uint64(numTokensMinted)) daoMint := cosmosmath.NewInt(int64(numTokensMinted * tokenomicsParams.MintAllocationPercentages.Dao)) propMint := cosmosmath.NewInt(int64(numTokensMinted * tokenomicsParams.MintAllocationPercentages.Proposer)) diff --git a/x/tokenomics/token_logic_module/global_mint.go b/x/tokenomics/token_logic_module/global_mint.go index 4880eb6de..02edf9e22 100644 --- a/x/tokenomics/token_logic_module/global_mint.go +++ b/x/tokenomics/token_logic_module/global_mint.go @@ -30,14 +30,7 @@ const ( MintDistributionAllowableToleranceAbs = 5.0 // 5 uPOKT ) -var ( - // TODO_BETA(@red-0ne, #732): Make this a governance parameter and give it a non-zero value + tests. - // GlobalInflationPerClaim is the percentage of the claim amount that is minted - // by TLMGlobalMint to reward the actors in the network. - GlobalInflationPerClaim = 0.1 - - _ TokenLogicModule = (*tlmGlobalMint)(nil) -) +var _ TokenLogicModule = (*tlmGlobalMint)(nil) type tlmGlobalMint struct{} @@ -61,13 +54,15 @@ func (tlm tlmGlobalMint) Process( "session_id", tlmCtx.Result.GetSessionId(), ) - if GlobalInflationPerClaim == 0 { + globalInflationPerClaim := tlmCtx.TokenomicsParams.GetGlobalInflationPerClaim() + + if globalInflationPerClaim == 0 { logger.Warn("global inflation is set to zero. Skipping Global Mint TLM.") return nil } // Determine how much new uPOKT to mint based on global inflation - newMintCoin, newMintAmtFloat := CalculateGlobalPerClaimMintInflationFromSettlementAmount(tlmCtx.SettlementCoin) + newMintCoin, newMintAmtFloat := CalculateGlobalPerClaimMintInflationFromSettlementAmount(tlmCtx.SettlementCoin, globalInflationPerClaim) if newMintCoin.IsZero() { return tokenomicstypes.ErrTokenomicsCoinIsZero.Wrapf("newMintCoin cannot be zero, TLMContext: %+v", tlmCtx) } @@ -254,11 +249,12 @@ func sendRewardsToAccount( // the settlement amount for a particular claim(s) or session(s). func CalculateGlobalPerClaimMintInflationFromSettlementAmount( settlementCoin cosmostypes.Coin, + globalInflationPerClaim float64, ) (cosmostypes.Coin, big.Float) { // Determine how much new uPOKT to mint based on global per claim inflation. // TODO_MAINNET: Consider using fixed point arithmetic for deterministic results. settlementAmtFloat := new(big.Float).SetUint64(settlementCoin.Amount.Uint64()) - newMintAmtFloat := new(big.Float).Mul(settlementAmtFloat, big.NewFloat(GlobalInflationPerClaim)) + newMintAmtFloat := new(big.Float).Mul(settlementAmtFloat, big.NewFloat(globalInflationPerClaim)) // DEV_NOTE: If new mint is less than 1 and more than 0, ceil it to 1 so that // we never expect to process a claim with 0 minted tokens. if newMintAmtFloat.Cmp(big.NewFloat(1)) < 0 && newMintAmtFloat.Cmp(big.NewFloat(0)) > 0 { diff --git a/x/tokenomics/token_logic_module/reimbursement_requests.go b/x/tokenomics/token_logic_module/reimbursement_requests.go index 71173ae65..9a1f7f402 100644 --- a/x/tokenomics/token_logic_module/reimbursement_requests.go +++ b/x/tokenomics/token_logic_module/reimbursement_requests.go @@ -39,14 +39,16 @@ func (tlm tlmGlobalMintReimbursementRequest) Process( logger = logger.With("method", "TokenLogicModuleGlobalMintReimbursementRequest") + globalInflationPerClaim := tlmCtx.TokenomicsParams.GetGlobalInflationPerClaim() + // Do not process the reimbursement request if there is no global inflation. - if GlobalInflationPerClaim == 0 { + if globalInflationPerClaim == 0 { logger.Warn("global inflation is set to zero. Skipping Global Mint Reimbursement Request TLM.") return nil } // Determine how much new uPOKT to mint based on global inflation - newMintCoin, _ := CalculateGlobalPerClaimMintInflationFromSettlementAmount(actualSettlementCoin) + newMintCoin, _ := CalculateGlobalPerClaimMintInflationFromSettlementAmount(actualSettlementCoin, globalInflationPerClaim) if newMintCoin.Amount.Int64() == 0 { return tokenomicstypes.ErrTokenomicsCoinIsZero }