-
Notifications
You must be signed in to change notification settings - Fork 0
/
abci.go
161 lines (134 loc) · 5.18 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package oracle
import (
"time"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/tessornetwork/nebula/x/oracle/keeper"
"github.com/tessornetwork/nebula/x/oracle/types"
)
// isPeriodLastBlock returns true if we are at the last block of the period
func isPeriodLastBlock(ctx sdk.Context, blocksPerPeriod uint64) bool {
return (uint64(ctx.BlockHeight())+1)%blocksPerPeriod == 0
}
// EndBlocker is called at the end of every block
func EndBlocker(ctx sdk.Context, k keeper.Keeper, experimental bool) error {
defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyEndBlocker)
params := k.GetParams(ctx)
if isPeriodLastBlock(ctx, params.VotePeriod) {
// Build claim map over all validators in active set
validatorClaimMap := make(map[string]types.Claim)
powerReduction := k.StakingKeeper.PowerReduction(ctx)
for _, v := range k.StakingKeeper.GetBondedValidatorsByPower(ctx) {
addr := v.GetOperator()
validatorClaimMap[addr.String()] = types.NewClaim(v.GetConsensusPower(powerReduction), 0, 0, addr)
}
var (
// voteTargets defines the symbol (ticker) denoms that we require votes on
voteTargets []string
voteTargetDenoms []string
)
for _, v := range params.AcceptList {
voteTargets = append(voteTargets, v.SymbolDenom)
voteTargetDenoms = append(voteTargetDenoms, v.BaseDenom)
}
k.ClearExchangeRates(ctx)
if isPeriodLastBlock(ctx, params.MedianPeriod) && experimental {
k.ClearMedians(ctx)
k.ClearMedianDeviations(ctx)
}
// NOTE: it filters out inactive or jailed validators
ballotDenomSlice := k.OrganizeBallotByDenom(ctx, validatorClaimMap)
// Iterate through ballots and update exchange rates; drop if not enough votes have been achieved.
for _, ballotDenom := range ballotDenomSlice {
// Get weighted median of exchange rates
exchangeRate, err := Tally(ctx, ballotDenom.Ballot, params.RewardBand, validatorClaimMap)
if err != nil {
return err
}
// Set the exchange rate, emit ABCI event
if err = k.SetExchangeRateWithEvent(ctx, ballotDenom.Denom, exchangeRate); err != nil {
return err
}
if experimental {
// Stamp rate every stamp period if asset is set to have historic stats tracked
if isPeriodLastBlock(ctx, params.StampPeriod) && params.HistoricAcceptList.Contains(ballotDenom.Denom) {
k.AddHistoricPrice(ctx, ballotDenom.Denom, exchangeRate)
}
// Set median price every median period if asset is set to have historic stats tracked
if isPeriodLastBlock(ctx, params.MedianPeriod) && params.HistoricAcceptList.Contains(ballotDenom.Denom) {
k.CalcAndSetMedian(ctx, ballotDenom.Denom)
}
}
}
// update miss counting & slashing
voteTargetsLen := len(voteTargets)
claimSlice := types.ClaimMapToSlice(validatorClaimMap)
for _, claim := range claimSlice {
// Skip valid voters
// in MsgAggregateExchangeRateVote we filter tokens from the AcceptList.
if int(claim.TokensVoted) == voteTargetsLen {
continue
}
// Increase miss counter
k.SetMissCounter(ctx, claim.Validator, k.GetMissCounter(ctx, claim.Validator)+1)
}
// Distribute rewards to ballot winners
k.RewardBallotWinners(
ctx,
int64(params.VotePeriod),
int64(params.RewardDistributionWindow),
voteTargetDenoms,
claimSlice,
)
// Clear the ballot
k.ClearVotes(ctx, params.VotePeriod)
}
// Slash oracle providers who missed voting over the threshold and
// reset miss counters of all validators at the last block of slash window
if isPeriodLastBlock(ctx, params.SlashWindow) {
k.SlashAndResetMissCounters(ctx)
}
// Prune historic prices every prune period
if isPeriodLastBlock(ctx, params.PrunePeriod) && experimental {
pruneBlock := uint64(ctx.BlockHeight()) - params.PrunePeriod
for _, v := range params.HistoricAcceptList {
k.DeleteHistoricPrice(ctx, v.String(), pruneBlock)
}
}
return nil
}
// Tally calculates and returns the median. It sets the set of voters to be
// rewarded, i.e. voted within a reasonable spread from the weighted median to
// the store. Note, the ballot is sorted by ExchangeRate.
func Tally(
ctx sdk.Context,
ballot types.ExchangeRateBallot,
rewardBand sdk.Dec,
validatorClaimMap map[string]types.Claim,
) (sdk.Dec, error) {
weightedMedian, err := ballot.WeightedMedian()
if err != nil {
return sdk.ZeroDec(), err
}
standardDeviation, err := ballot.StandardDeviation()
if err != nil {
return sdk.ZeroDec(), err
}
// rewardSpread is the MAX((weightedMedian * (rewardBand/2)), standardDeviation)
rewardSpread := weightedMedian.Mul(rewardBand.QuoInt64(2))
rewardSpread = sdk.MaxDec(rewardSpread, standardDeviation)
for _, tallyVote := range ballot {
// Filter ballot winners. For voters, we filter out the tally vote iff:
// (weightedMedian - rewardSpread) <= ExchangeRate <= (weightedMedian + rewardSpread)
if (tallyVote.ExchangeRate.GTE(weightedMedian.Sub(rewardSpread)) &&
tallyVote.ExchangeRate.LTE(weightedMedian.Add(rewardSpread))) ||
!tallyVote.ExchangeRate.IsPositive() {
key := tallyVote.Voter.String()
claim := validatorClaimMap[key]
claim.Weight += tallyVote.Power
claim.TokensVoted++
validatorClaimMap[key] = claim
}
}
return weightedMedian, nil
}