Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(x/mint): Implementing inflation in BeginBlocker #311

Closed
wants to merge 24 commits into from

Conversation

spoo-bar
Copy link
Contributor

@spoo-bar spoo-bar commented Mar 13, 2023

Ref: #287

x/mint

  • Implemented begin blocker where current block provisions are fetched and minted and distributed
  • Added abci tests
  • Added x/staking as dependency to x/mint so that we can fetch the bond denom and bonded token supply
  • When inflation in distributed, store which recipient got how much. Stored in the KV store.
  • Adding simulation tests of inflation to ensure its whats expected. The simulation matches expected values upto 6 decimal places

x/rewards

  • Added x/mint as dependency to x/rewards so that rewards can track the inflation as well as update consensus fee
  • In begin blocker, fetch if inflation was distributed to x/rewards. If yes, update min consensus fee and also track inflation rewards

note


❗Reviewer pls check 🙏🏻

  • Is this the best way to init an sdk.Coin from a sdk.Dec value
    coin := sdk.NewCoin(denom, amount.TruncateInt()) // as sdk.Coin
  • Verify the implementation from math to code
    tokens = blockInflation.MulInt(bondedTokenSupply.Amount).Mul(sdk.NewDecFromBigInt(big.NewInt(int64(elapsed.Seconds()))).QuoInt64(int64(Year.Seconds()))) // amount := (inflation * bondedTokenSupply) * (elapsed/Year)

@spoo-bar spoo-bar marked this pull request as ready for review March 13, 2023 14:56
@spoo-bar spoo-bar requested a review from fdymylja March 13, 2023 14:56
Copy link
Contributor

@fdymylja fdymylja left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to add a test case.

Mock StakingKeeper: bonded ratio is constant and within bands (eg: 66%).

Inflation is 10%, adjust context to make it run for a year, with block time always <= params.MaxBlockDuration.

Calculate the expected inflation (note: needs compounding calc).


// getting last block info
lbi, found := k.GetLastBlockInfo(ctx)
if !found {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when is the case for which this can be not found?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont think there is a case where this will not be found 🤔

x/mint/keeper/minter.go Outdated Show resolved Hide resolved
x/mint/keeper/minter.go Outdated Show resolved Hide resolved
// amount of bond tokens to mint in this block
bondedTokenSupply := k.GetBondedTokenSupply(ctx)

tokens = blockInflation.MulInt(bondedTokenSupply.Amount).Mul(sdk.NewDecFromBigInt(big.NewInt(int64(elapsed.Seconds()))).QuoInt64(int64(Year.Seconds()))) // amount := (inflation * bondedTokenSupply) * (elapsed/Year)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in the getBlockInflation function, we're multiplying the inflation change by NANOSECONDS whilst here we're dividing by SECONDS.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also can we split the inflation * bondedTokenSupply and the elapsed/year

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can also simplify the calculation timeElapsedRatio = elapsed/year without any conversion, just using the raw duration, and then multiply it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure we can do timeElapsedRatio := elapsed/Year as both are internally just int64 values. and smallInt/largeInt will just give 0.
Doing timeElapsedRate := sdk.NewDecFromBigInt(big.NewInt(int64(elapsed))).QuoInt64(int64(Year)) returns the small decimal value. But yea, there could be better ways to do this🤔

mintedTokens, found := k.GetInflationForRewards(ctx)
if found {
// Track inflation rewards
k.TrackInflationRewards(ctx, mintedTokens)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if it's not found I'm not sure if skipping it is the correct way to do it.
Old params would be applied.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if its not found, that would mean there are no inflation rewards to track right. like it would be 0stake in the current block

x/rewards/keeper/keeper.go Outdated Show resolved Hide resolved
x/rewards/keeper/keeper.go Outdated Show resolved Hide resolved
x/mint/types/keys.go Show resolved Hide resolved
x/mint/abci.go Outdated Show resolved Hide resolved
x/mint/abci.go Outdated Show resolved Hide resolved
@spoo-bar spoo-bar requested a review from fdymylja March 20, 2023 16:25
sdk "github.com/cosmos/cosmos-sdk/types"
)

const Year = 24 * time.Hour * 365
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This gets us 31 536 000 seconds per year.
Should we replace this with 31 557 600 seconds to accommodate 4 year avg to handle leap years?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should prefer the leap year average... it is better for overall financial math calculations;

@zanicar
Copy link
Contributor

zanicar commented May 2, 2023

PR still relevant?

@spoo-bar
Copy link
Contributor Author

☠️ Closing cuz time based inflation RIP

@spoo-bar spoo-bar closed this Aug 25, 2023
@spoo-bar spoo-bar deleted the spoorthi/minting-begin-blocker branch August 25, 2023 10:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants