-
Notifications
You must be signed in to change notification settings - Fork 289
/
abci.go
87 lines (73 loc) · 2.79 KB
/
abci.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package mint
import (
"time"
"github.com/celestiaorg/celestia-app/x/mint/keeper"
"github.com/celestiaorg/celestia-app/x/mint/types"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// BeginBlocker updates the inflation rate, annual provisions, and then mints
// the block provision for the current block.
func BeginBlocker(ctx sdk.Context, k keeper.Keeper) {
defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyBeginBlocker)
maybeUpdateMinter(ctx, k)
mintBlockProvision(ctx, k)
setPreviousBlockTime(ctx, k)
}
// maybeUpdateMinter updates the inflation rate and annual provisions if the
// inflation rate has changed. The inflation rate is expected to change once per
// year at the genesis time anniversary until the TargetInflationRate is
// reached.
func maybeUpdateMinter(ctx sdk.Context, k keeper.Keeper) {
minter := k.GetMinter(ctx)
genesisTime := k.GetGenesisTime(ctx).GenesisTime
newInflationRate := minter.CalculateInflationRate(ctx, *genesisTime)
isNonZeroAnnualProvisions := !minter.AnnualProvisions.IsZero()
if newInflationRate.Equal(minter.InflationRate) && isNonZeroAnnualProvisions {
// The minter's InflationRate and AnnualProvisions already reflect the
// values for this year. Exit early because we don't need to update
// them. AnnualProvisions must be updated if it is zero (expected at
// genesis).
return
}
totalSupply := k.StakingTokenSupply(ctx)
minter.InflationRate = newInflationRate
minter.AnnualProvisions = newInflationRate.MulInt(totalSupply)
k.SetMinter(ctx, minter)
}
// mintBlockProvision mints the block provision for the current block.
func mintBlockProvision(ctx sdk.Context, k keeper.Keeper) {
minter := k.GetMinter(ctx)
if minter.PreviousBlockTime == nil {
// exit early if previous block time is nil
// this is expected to happen for block height = 1
return
}
toMintCoin := minter.CalculateBlockProvision(ctx.BlockTime(), *minter.PreviousBlockTime)
toMintCoins := sdk.NewCoins(toMintCoin)
err := k.MintCoins(ctx, toMintCoins)
if err != nil {
panic(err)
}
err = k.SendCoinsToFeeCollector(ctx, toMintCoins)
if err != nil {
panic(err)
}
if toMintCoin.Amount.IsInt64() {
defer telemetry.ModuleSetGauge(types.ModuleName, float32(toMintCoin.Amount.Int64()), "minted_tokens")
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeMint,
sdk.NewAttribute(types.AttributeKeyInflationRate, minter.InflationRate.String()),
sdk.NewAttribute(types.AttributeKeyAnnualProvisions, minter.AnnualProvisions.String()),
sdk.NewAttribute(sdk.AttributeKeyAmount, toMintCoin.Amount.String()),
),
)
}
func setPreviousBlockTime(ctx sdk.Context, k keeper.Keeper) {
minter := k.GetMinter(ctx)
blockTime := ctx.BlockTime()
minter.PreviousBlockTime = &blockTime
k.SetMinter(ctx, minter)
}