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

refactor(x/mint)!: avoid writing same minter and use millisecond precision for minting #20747

Merged
merged 3 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions simapp/mint_fn.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,19 @@ func ProvideExampleMintFn(bankKeeper MintBankKeeper) minttypes.MintFn {
minter.AnnualProvisions = minter.NextAnnualProvisions(mintParams.Params, stakingTokenSupply.Amount)

// to get a more accurate amount of tokens minted, we get, and later store, last minting time.

// if this is the first time minting, we initialize the minter.Data with the current time - 60s
// to mint tokens at the beginning. Note: this is a custom behavior to avoid breaking tests.
if minter.Data == nil {
minter.Data = make([]byte, 8)
binary.BigEndian.PutUint64(minter.Data, (uint64)(env.HeaderService.HeaderInfo(ctx).Time.Unix()-60))
binary.BigEndian.PutUint64(minter.Data, (uint64)(env.HeaderService.HeaderInfo(ctx).Time.UnixMilli()-60000))
}

lastMint := binary.BigEndian.Uint64(minter.Data)
binary.BigEndian.PutUint64(minter.Data, (uint64)(env.HeaderService.HeaderInfo(ctx).Time.Unix()))
binary.BigEndian.PutUint64(minter.Data, (uint64)(env.HeaderService.HeaderInfo(ctx).Time.UnixMilli()))

// calculate the amount of tokens to mint, based on the time since the last mint
secsSinceLastMint := env.HeaderService.HeaderInfo(ctx).Time.Unix() - (int64)(lastMint)
provisionAmt := minter.AnnualProvisions.QuoInt64(31536000).MulInt64(secsSinceLastMint) // 31536000 = seconds in a year
// calculate the amount of tokens to mint, based on the time since the last mint.
msSinceLastMint := env.HeaderService.HeaderInfo(ctx).Time.UnixMilli() - (int64)(lastMint)
provisionAmt := minter.AnnualProvisions.QuoInt64(31536000000).MulInt64(msSinceLastMint) // 31536000000 = milliseconds in a year
mintedCoin := sdk.NewCoin(mintParams.Params.MintDenom, provisionAmt.TruncateInt())
maxSupply := mintParams.Params.MaxSupply
totalSupply := stakingTokenSupply.Amount
Expand Down
8 changes: 8 additions & 0 deletions x/mint/epoch_hooks.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mint

import (
"bytes"
"context"

epochstypes "cosmossdk.io/x/epochs/types"
Expand All @@ -20,11 +21,18 @@ func (am AppModule) BeforeEpochStart(ctx context.Context, epochIdentifier string
return err
}

oldBz := am.cdc.MustMarshal(&minter)

err = am.mintFn(ctx, am.keeper.Environment, &minter, epochIdentifier, epochNumber)
if err != nil {
return err
}

newBz := am.cdc.MustMarshal(&minter)
if bytes.Equal(oldBz, newBz) {
return nil
}

return am.keeper.Minter.Set(ctx, minter)
}

Expand Down
8 changes: 8 additions & 0 deletions x/mint/keeper/abci.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper

import (
"bytes"
"context"

"cosmossdk.io/x/mint/types"
Expand All @@ -18,12 +19,19 @@
return err
}

oldBz := k.cdc.MustMarshal(&minter)
Fixed Show fixed Hide fixed
facundomedica marked this conversation as resolved.
Show resolved Hide resolved

// we pass -1 as epoch number to indicate that this is not an epoch minting,
// but a regular block minting. Same with epoch id "block".
err = mintFn(ctx, k.Environment, &minter, "block", -1)
if err != nil {
return err
}

newBz := k.cdc.MustMarshal(&minter)
Fixed Show fixed Hide fixed
if bytes.Equal(oldBz, newBz) {
return nil
}

return k.Minter.Set(ctx, minter)
}
Loading