Skip to content

Commit

Permalink
fix(inflation): num skipped epochs tracking (#1797)
Browse files Browse the repository at this point in the history
* fix: fix bug on inflation

* chore: changelog

* fix: add bool to see wether inflation ever started

* fix: epoch off by one error

* chore: unwire epochs keeper from inflation keeper

* fix: off by one tests

---------

Co-authored-by: Kevin Yang <[email protected]>
  • Loading branch information
matthiasmatt and k-yang committed Feb 15, 2024
1 parent 7b2be2c commit 6efa242
Show file tree
Hide file tree
Showing 13 changed files with 265 additions and 204 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* [#1706](https://github.com/NibiruChain/nibiru/pull/706) - fix: `v1.1.0` upgrade handler
* [#1786](https://github.com/NibiruChain/nibiru/pull/1786) - fix(inflation): fix inflation off-by 2 error
* [#1796](https://github.com/NibiruChain/nibiru/pull/1796) - fix(inflation): fix num skipped epoch when inflation is added to an existing chain
* [#1797](https://github.com/NibiruChain/nibiru/pull/1797) - fix(inflation): fix num skipped epoch updates logic

### Improvements

Expand Down
2 changes: 1 addition & 1 deletion app/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ func (app *NibiruApp) InitKeepers(

app.InflationKeeper = inflationkeeper.NewKeeper(
appCodec, keys[inflationtypes.StoreKey], app.GetSubspace(inflationtypes.ModuleName),
app.AccountKeeper, app.BankKeeper, app.DistrKeeper, app.stakingKeeper, app.SudoKeeper, authtypes.FeeCollectorName, app.EpochsKeeper,
app.AccountKeeper, app.BankKeeper, app.DistrKeeper, app.stakingKeeper, app.SudoKeeper, authtypes.FeeCollectorName,
)

app.EpochsKeeper.SetHooks(
Expand Down
5 changes: 5 additions & 0 deletions proto/nibiru/inflation/v1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,9 @@ message Params {
// max_period is the maximum number of periods that have inflation being
// paid off. After this period, inflation will be disabled.
uint64 max_period = 6;

// has_inflation_started is the parameter that indicates if inflation has
// started. It's set to false at the starts, and stays at true when we toggle
// inflation on. It's used to track num skipped epochs
bool has_inflation_started = 7;
}
2 changes: 0 additions & 2 deletions x/inflation/client/cli/tx.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cli

import (
"fmt"
"strings"

"github.com/cosmos/cosmos-sdk/client"
Expand Down Expand Up @@ -126,7 +125,6 @@ $ nibid tx oracle edit-params --staking-proportion 0.6 --community-pool-proporti

if polynomialFactors, _ := cmd.Flags().GetString("polynomial-factors"); polynomialFactors != "" {
polynomialFactorsArr := strings.Split(polynomialFactors, ",")
fmt.Println(polynomialFactorsArr)
realPolynomialFactors := make([]sdk.Dec, len(polynomialFactorsArr))
for i, factor := range polynomialFactorsArr {
factorDec := sdk.MustNewDecFromStr(factor)
Expand Down
34 changes: 23 additions & 11 deletions x/inflation/keeper/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,22 @@ func (k Keeper) AfterEpochEnd(ctx sdk.Context, epochIdentifier string, epochNumb

// Skip inflation if it is disabled and increment number of skipped epochs
if !params.InflationEnabled {
prevSkippedEpochs := k.NumSkippedEpochs.Next(ctx)
var prevSkippedEpochs uint64
if !params.HasInflationStarted {
// If the inflation never started, we use epochNumber as the number of skipped epochs
// to avoid missing periods when we upgrade a chain.
k.NumSkippedEpochs.Set(ctx, epochNumber)
prevSkippedEpochs = epochNumber
} else {
prevSkippedEpochs = k.NumSkippedEpochs.Next(ctx)
}

k.Logger(ctx).Debug(
"skipping inflation mint and allocation",
"height", ctx.BlockHeight(),
"epoch-id", epochIdentifier,
"epoch-number", epochNumber,
"skipped-epochs", prevSkippedEpochs+1,
"skipped-epochs", prevSkippedEpochs,
)
return
}
Expand Down Expand Up @@ -76,15 +84,19 @@ func (k Keeper) AfterEpochEnd(ctx sdk.Context, epochIdentifier string, epochNumb
// where inflation minted tokens.
//
// Examples:
// Given, epochNumber = 1, period = 0, epochPerPeriod = 365, skippedEpochs = 0
// => 1 - 365 * 0 - 0 < 365 --- nothing to do here
// Given, epochNumber = 741, period = 1, epochPerPeriod = 365, skippedEpochs = 10
// => 741 - 1 * 365 - 10 > 365 --- a period has passed! we set a new period
peek := k.NumSkippedEpochs.Peek(ctx)
// Given, epochNumber = 1, period = 0, epochPerPeriod = 30, skippedEpochs = 0
// => 1 - 30 * 0 - 0 < 30 --- nothing to do here
// Given, epochNumber = 70, period = 1, epochPerPeriod = 30, skippedEpochs = 10
// => 70 - 1 * 30 - 10 >= 30 --- a period has ended! we set a new period
// Given, epochNumber = 42099, period = 0, epochPerPeriod = 30, skippedEpochs = 42069
// => 42099 - 0 * 30 - 42069 >= 30 --- a period has ended! we set a new period
numSkippedEpochs := k.NumSkippedEpochs.Peek(ctx)
if int64(epochNumber)-
int64(epochsPerPeriod*period)-
int64(peek) >= int64(epochsPerPeriod)-1 {
k.CurrentPeriod.Next(ctx)
int64(numSkippedEpochs) >= int64(epochsPerPeriod) {
prevPeriod := k.CurrentPeriod.Next(ctx)

k.Logger(ctx).Info(fmt.Sprintf("setting new period: %d", prevPeriod+1))
}

defer func() {
Expand Down Expand Up @@ -124,8 +136,8 @@ func (k Keeper) AfterEpochEnd(ctx sdk.Context, epochIdentifier string, epochNumb

ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeMint,
sdk.NewAttribute(types.AttributeEpochNumber, fmt.Sprintf("%d", epochNumber)),
types.EventTypeInflation,
sdk.NewAttribute(types.AttributeEpochNumber, fmt.Sprintf("%d", epochNumber-numSkippedEpochs)),
sdk.NewAttribute(types.AttributeKeyEpochProvisions, epochMintProvision.String()),
sdk.NewAttribute(sdk.AttributeKeyAmount, mintedCoin.Amount.String()),
),
Expand Down
Loading

0 comments on commit 6efa242

Please sign in to comment.