/
abci.go
executable file
·166 lines (138 loc) · 5.13 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
162
163
164
165
166
package oracle
import (
"sort"
"time"
"github.com/fibonacci-chain/core/utils/metrics"
"github.com/fibonacci-chain/core/x/oracle/keeper"
"github.com/fibonacci-chain/core/x/oracle/types"
"github.com/fibonacci-chain/core/x/oracle/utils"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
)
func MidBlocker(ctx sdk.Context, k keeper.Keeper) {
defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyMidBlocker)
params := k.GetParams(ctx)
if utils.IsPeriodLastBlock(ctx, params.VotePeriod) {
// Build claim map over all validators in active set
validatorClaimMap := make(map[string]types.Claim)
maxValidators := k.StakingKeeper.MaxValidators(ctx)
iterator := k.StakingKeeper.ValidatorsPowerStoreIterator(ctx)
defer iterator.Close()
powerReduction := k.StakingKeeper.PowerReduction(ctx)
i := 0
powerOrderedValAddrs := []sdk.ValAddress{}
for ; iterator.Valid() && i < int(maxValidators); iterator.Next() {
powerOrderedValAddrs = append(powerOrderedValAddrs, iterator.Value())
}
for _, valAddr := range powerOrderedValAddrs {
validator := k.StakingKeeper.Validator(ctx, valAddr)
// Exclude not bonded validator
if validator.IsBonded() {
valAddr := validator.GetOperator()
validatorClaimMap[valAddr.String()] = types.NewClaim(validator.GetConsensusPower(powerReduction), 0, 0, valAddr, false)
i++
}
}
voteTargets := make(map[string]types.Denom)
totalTargets := 0
k.IterateVoteTargets(ctx, func(denom string, denomInfo types.Denom) bool {
voteTargets[denom] = denomInfo
totalTargets++
return false
})
// Organize votes to ballot by denom
// NOTE: **Filter out inactive or jailed validators**
// NOTE: **Make abstain votes to have zero vote power**
voteMap := k.OrganizeBallotByDenom(ctx, validatorClaimMap)
// belowThresholdVoteMap has assets that failed to meet threshold
referenceDenom, belowThresholdVoteMap := pickReferenceDenom(ctx, k, voteTargets, voteMap)
if referenceDenom != "" {
// make voteMap of Reference denom to calculate cross exchange rates
ballotRD := voteMap[referenceDenom]
voteMapRD := ballotRD.ToMap()
exchangeRateRD := ballotRD.WeightedMedianWithAssertion()
// Iterate through ballots and update exchange rates; drop if not enough votes have been achieved.
keys := make([]string, len(voteMap))
j := 0
for denom := range voteMap {
keys[j] = denom
j++
}
sort.Strings(keys)
for _, denom := range keys {
ballot := voteMap[denom]
// Convert ballot to cross exchange rates
if denom != referenceDenom {
ballot = ballot.ToCrossRateWithSort(voteMapRD)
}
// Get weighted median of cross exchange rates
exchangeRate := Tally(ctx, ballot, params.RewardBand, validatorClaimMap)
// Transform into the original form base/quote
if denom != referenceDenom {
exchangeRate = exchangeRateRD.Quo(exchangeRate)
}
// Set the exchange rate, emit ABCI event
metrics.IncrPriceUpdateDenom(denom)
k.SetBaseExchangeRateWithEvent(ctx, denom, exchangeRate)
}
}
belowThresholdKeys := make([]string, len(belowThresholdVoteMap))
n := 0
for denom := range belowThresholdVoteMap {
belowThresholdKeys[n] = denom
n++
}
sort.Strings(belowThresholdKeys)
// in this case, all assets would be in the belowThresholdVoteMap
for _, denom := range belowThresholdKeys {
ballot := belowThresholdVoteMap[denom]
// perform tally for below threshold assets to calculate total win count
Tally(ctx, ballot, params.RewardBand, validatorClaimMap)
}
//---------------------------
// Do miss counting & slashing
for _, claim := range validatorClaimMap {
// we require validator to have submitted in-range data
// for all assets to not be counted as a miss
if int(claim.WinCount) == totalTargets {
k.IncrementSuccessCount(ctx, claim.Recipient)
continue
}
if !claim.DidVote {
k.IncrementAbstainCount(ctx, claim.Recipient)
continue
}
// Increase miss counter
k.IncrementMissCount(ctx, claim.Recipient)
}
// Clear the ballot
k.ClearBallots(ctx, params.VotePeriod)
// Update vote targets
k.ApplyWhitelist(ctx, params.Whitelist, voteTargets)
priceSnapshotItems := []types.PriceSnapshotItem{}
k.IterateBaseExchangeRates(ctx, func(denom string, exchangeRate types.OracleExchangeRate) bool {
priceSnapshotItem := types.PriceSnapshotItem{
Denom: denom,
OracleExchangeRate: exchangeRate,
}
priceSnapshotItems = append(priceSnapshotItems, priceSnapshotItem)
return false
})
if len(priceSnapshotItems) > 0 {
priceSnapshot := types.PriceSnapshot{
SnapshotTimestamp: ctx.BlockTime().Unix(),
PriceSnapshotItems: priceSnapshotItems,
}
k.AddPriceSnapshot(ctx, priceSnapshot)
}
}
}
func EndBlocker(ctx sdk.Context, k keeper.Keeper) {
defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyEndBlocker)
params := k.GetParams(ctx)
// Do slash who did miss voting over threshold and
// reset miss counters of all validators at the last block of slash window
if utils.IsPeriodLastBlock(ctx, params.SlashWindow) {
k.SlashAndResetCounters(ctx)
}
}