diff --git a/PENDING.md b/PENDING.md index 7bc7cc683..49ceaf94c 100644 --- a/PENDING.md +++ b/PENDING.md @@ -4,6 +4,7 @@ BREAKING CHANGES * [baseapp] Msgs are no longer run on CheckTx, removed `ctx.IsCheckTx()` * [x/gov] CLI flag changed from `proposalID` to `proposal-id` * [x/stake] Fixed the period check for the inflation calculation +* [x/stake] Inflation doesn't use rationals in calculation (performance boost) * [baseapp] NewBaseApp constructor now takes sdk.TxDecoder as argument instead of wire.Codec * [x/auth] Default TxDecoder can be found in `x/auth` rather than baseapp * \#1606 The following CLI commands have been switched to use `--from` diff --git a/cmd/gaia/app/app.go b/cmd/gaia/app/app.go index 82d1123ea..837ce5a10 100644 --- a/cmd/gaia/app/app.go +++ b/cmd/gaia/app/app.go @@ -143,8 +143,8 @@ func (app *GaiaApp) BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock) ab // application updates every end block // nolint: unparam func (app *GaiaApp) EndBlocker(ctx sdk.Context, req abci.RequestEndBlock) abci.ResponseEndBlock { - validatorUpdates := stake.EndBlocker(ctx, app.stakeKeeper) + validatorUpdates := stake.EndBlocker(ctx, app.stakeKeeper) tags, _ := gov.EndBlocker(ctx, app.govKeeper) return abci.ResponseEndBlock{ diff --git a/x/slashing/tick.go b/x/slashing/tick.go index 01984f870..da157ca7a 100644 --- a/x/slashing/tick.go +++ b/x/slashing/tick.go @@ -11,6 +11,7 @@ import ( // slashing begin block functionality func BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock, sk Keeper) (tags sdk.Tags) { + // Tag the height heightBytes := make([]byte, 8) binary.LittleEndian.PutUint64(heightBytes, uint64(req.Header.Height)) diff --git a/x/stake/handler.go b/x/stake/handler.go index 14fb4f7bf..3cc122e4c 100644 --- a/x/stake/handler.go +++ b/x/stake/handler.go @@ -35,18 +35,16 @@ func NewHandler(k keeper.Keeper) sdk.Handler { // Called every block, process inflation, update validator set func EndBlocker(ctx sdk.Context, k keeper.Keeper) (ValidatorUpdates []abci.Validator) { pool := k.GetPool(ctx) - params := k.GetParams(ctx) - // Process types.Validator Provisions + // Process provision inflation blockTime := ctx.BlockHeader().Time if blockTime-pool.InflationLastTime >= 3600 { + params := k.GetParams(ctx) pool.InflationLastTime = blockTime pool = pool.ProcessProvisions(params) + k.SetPool(ctx, pool) } - // save the params - k.SetPool(ctx, pool) - // reset the intra-transaction counter k.SetIntraTxCounter(ctx, 0) diff --git a/x/stake/types/inflation_test.go b/x/stake/types/inflation_test.go index 555408853..0114b1e05 100644 --- a/x/stake/types/inflation_test.go +++ b/x/stake/types/inflation_test.go @@ -39,11 +39,11 @@ func TestGetInflation(t *testing.T) { // test 7% minimum stop (testing with 100% bonded) {"test 4", sdk.OneRat(), sdk.ZeroRat(), sdk.NewRat(7, 100), sdk.ZeroRat()}, - {"test 5", sdk.OneRat(), sdk.ZeroRat(), sdk.NewRat(70001, 1000000), sdk.NewRat(-1, 1000000).Round(precision)}, + {"test 5", sdk.OneRat(), sdk.ZeroRat(), sdk.NewRat(70001, 1000000), sdk.NewRat(-1, 1000000)}, // test 20% maximum stop (testing with 0% bonded) {"test 6", sdk.ZeroRat(), sdk.ZeroRat(), sdk.NewRat(20, 100), sdk.ZeroRat()}, - {"test 7", sdk.ZeroRat(), sdk.ZeroRat(), sdk.NewRat(199999, 1000000), sdk.NewRat(1, 1000000).Round(precision)}, + {"test 7", sdk.ZeroRat(), sdk.ZeroRat(), sdk.NewRat(199999, 1000000), sdk.NewRat(1, 1000000)}, // perfect balance shouldn't change inflation {"test 8", sdk.NewRat(67), sdk.NewRat(33), sdk.NewRat(15, 100), sdk.ZeroRat()}, @@ -94,8 +94,9 @@ func checkFinalPoolValues(t *testing.T, pool Pool, initialTotalTokens, cumulativ // Processes provisions are added to the pool correctly every hour // Returns expected Provisions, expected Inflation, and pool, to help with cumulative calculations back in main Tests func updateProvisions(t *testing.T, pool Pool, params Params, hr int) (sdk.Rat, sdk.Rat, Pool) { + expInflation := pool.NextInflation(params) - expProvisions := expInflation.Mul(pool.TokenSupply()).Quo(hrsPerYrRat) + expProvisions := expInflation.Mul(pool.TokenSupply().Round(precision)).Quo(hrsPerYrRat) startTotalSupply := pool.TokenSupply() pool = pool.ProcessProvisions(params) diff --git a/x/stake/types/pool.go b/x/stake/types/pool.go index 01dfacada..6ddadf94b 100644 --- a/x/stake/types/pool.go +++ b/x/stake/types/pool.go @@ -80,13 +80,15 @@ func (p Pool) bondedTokensToLoose(bondedTokens sdk.Rat) Pool { //_______________________________________________________________________ // Inflation -const precision = 100000000000 // increased to this precision for accuracy +const precision = 10000 // increased to this precision for accuracy var hrsPerYrRat = sdk.NewRat(8766) // as defined by a julian year of 365.25 days // process provisions for an hour period func (p Pool) ProcessProvisions(params Params) Pool { p.Inflation = p.NextInflation(params) - provisions := p.Inflation.Mul(p.TokenSupply()).Quo(hrsPerYrRat) + provisions := p.Inflation. + Mul(p.TokenSupply().Round(precision)). + Quo(hrsPerYrRat) // TODO add to the fees provisions p.LooseTokens = p.LooseTokens.Add(provisions) @@ -103,7 +105,10 @@ func (p Pool) NextInflation(params Params) (inflation sdk.Rat) { // 7% and 20%. // (1 - bondedRatio/GoalBonded) * InflationRateChange - inflationRateChangePerYear := sdk.OneRat().Sub(p.BondedRatio().Quo(params.GoalBonded)).Mul(params.InflationRateChange) + inflationRateChangePerYear := sdk.OneRat(). + Sub(p.BondedRatio().Round(precision). + Quo(params.GoalBonded)). + Mul(params.InflationRateChange) inflationRateChange := inflationRateChangePerYear.Quo(hrsPerYrRat) // increase the new annual inflation for this next cycle