Skip to content

Commit

Permalink
[Tokenomics] refactor: GlobalInflationPerClaim var usage to param u…
Browse files Browse the repository at this point in the history
…sage (#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 <[email protected]>
  • Loading branch information
bryanchriswhite and red-0ne authored Dec 2, 2024
1 parent 88c5321 commit a8e38d6
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 31 deletions.
6 changes: 4 additions & 2 deletions tests/integration/application/min_stake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()
Expand Down
11 changes: 6 additions & 5 deletions x/tokenomics/keeper/token_logic_modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
20 changes: 9 additions & 11 deletions x/tokenomics/keeper/token_logic_modules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand Down
18 changes: 7 additions & 11 deletions x/tokenomics/token_logic_module/global_mint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}

Expand All @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 4 additions & 2 deletions x/tokenomics/token_logic_module/reimbursement_requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit a8e38d6

Please sign in to comment.