forked from cosmos/gravity-bridge
-
Notifications
You must be signed in to change notification settings - Fork 25
/
abci.go
390 lines (349 loc) · 16.5 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
package gravity
import (
"sort"
"github.com/althea-net/cosmos-gravity-bridge/module/x/gravity/keeper"
"github.com/althea-net/cosmos-gravity-bridge/module/x/gravity/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// EndBlocker is called at the end of every block
func EndBlocker(ctx sdk.Context, k keeper.Keeper) {
params := k.GetParams(ctx)
slashing(ctx, k)
attestationTally(ctx, k)
cleanupTimedOutBatches(ctx, k)
cleanupTimedOutLogicCalls(ctx, k)
createValsets(ctx, k)
pruneValsets(ctx, k, params)
pruneAttestations(ctx, k)
}
func createValsets(ctx sdk.Context, k keeper.Keeper) {
// Auto ValsetRequest Creation.
// WARNING: do not use k.GetLastObservedValset in this function, it *will* result in losing control of the bridge
// 1. If there are no valset requests, create a new one.
// 2. If there is at least one validator who started unbonding in current block. (we persist last unbonded block height in hooks.go)
// This will make sure the unbonding validator has to provide an attestation to a new Valset
// that excludes him before he completely Unbonds. Otherwise he will be slashed
// 3. If power change between validators of CurrentValset and latest valset request is > 5%
// get the last valsets to compare against
latestValset := k.GetLatestValset(ctx)
lastUnbondingHeight := k.GetLastUnBondingBlockHeight(ctx)
if (latestValset == nil) || (lastUnbondingHeight == uint64(ctx.BlockHeight())) || (types.BridgeValidators(k.GetCurrentValset(ctx).Members).PowerDiff(latestValset.Members) > 0.05) {
// if the conditions are true, put in a new validator set request to be signed and submitted to Ethereum
k.SetValsetRequest(ctx)
}
}
func pruneValsets(ctx sdk.Context, k keeper.Keeper, params types.Params) {
// Validator set pruning
// prune all validator sets with a nonce less than the
// last observed nonce, they can't be submitted any longer
//
// Only prune valsets after the signed valsets window has passed
// so that slashing can occur the block before we remove them
lastObserved := k.GetLastObservedValset(ctx)
currentBlock := uint64(ctx.BlockHeight())
tooEarly := currentBlock < params.SignedValsetsWindow
if lastObserved != nil && !tooEarly {
earliestToPrune := currentBlock - params.SignedValsetsWindow
sets := k.GetValsets(ctx)
for _, set := range sets {
if set.Nonce < lastObserved.Nonce && set.Height < earliestToPrune {
k.DeleteValset(ctx, set.Nonce)
}
}
}
}
func slashing(ctx sdk.Context, k keeper.Keeper) {
params := k.GetParams(ctx)
// Slash validator for not confirming valset requests, batch requests, logic call requests
ValsetSlashing(ctx, k, params)
BatchSlashing(ctx, k, params)
LogicCallSlashing(ctx, k, params)
}
// Iterate over all attestations currently being voted on in order of nonce and
// "Observe" those who have passed the threshold. Break the loop once we see
// an attestation that has not passed the threshold
func attestationTally(ctx sdk.Context, k keeper.Keeper) {
attmap := k.GetAttestationMapping(ctx)
// We make a slice with all the event nonces that are in the attestation mapping
keys := make([]uint64, 0, len(attmap))
for k := range attmap {
keys = append(keys, k)
}
// Then we sort it
sort.Slice(keys, func(i, j int) bool { return keys[i] < keys[j] })
// This iterates over all keys (event nonces) in the attestation mapping. Each value contains
// a slice with one or more attestations at that event nonce. There can be multiple attestations
// at one event nonce when validators disagree about what event happened at that nonce.
for _, nonce := range keys {
// This iterates over all attestations at a particular event nonce.
// They are ordered by when the first attestation at the event nonce was received.
// This order is not important.
for _, att := range attmap[nonce] {
// We check if the event nonce is exactly 1 higher than the last attestation that was
// observed. If it is not, we just move on to the next nonce. This will skip over all
// attestations that have already been observed.
//
// Once we hit an event nonce that is one higher than the last observed event, we stop
// skipping over this conditional and start calling tryAttestation (counting votes)
// Once an attestation at a given event nonce has enough votes and becomes observed,
// every other attestation at that nonce will be skipped, since the lastObservedEventNonce
// will be incremented.
//
// Then we go to the next event nonce in the attestation mapping, if there is one. This
// nonce will once again be one higher than the lastObservedEventNonce.
// If there is an attestation at this event nonce which has enough votes to be observed,
// we skip the other attestations and move on to the next nonce again.
// If no attestation becomes observed, when we get to the next nonce, every attestation in
// it will be skipped. The same will happen for every nonce after that.
if nonce == uint64(k.GetLastObservedEventNonce(ctx))+1 {
k.TryAttestation(ctx, &att)
}
}
}
}
// cleanupTimedOutBatches deletes batches that have passed their expiration on Ethereum
// keep in mind several things when modifying this function
// A) unlike nonces timeouts are not monotonically increasing, meaning batch 5 can have a later timeout than batch 6
// this means that we MUST only cleanup a single batch at a time
// B) it is possible for ethereumHeight to be zero if no events have ever occurred, make sure your code accounts for this
// C) When we compute the timeout we do our best to estimate the Ethereum block height at that very second. But what we work with
// here is the Ethereum block height at the time of the last Deposit or Withdraw to be observed. It's very important we do not
// project, if we do a slowdown on ethereum could cause a double spend. Instead timeouts will *only* occur after the timeout period
// AND any deposit or withdraw has occurred to update the Ethereum block height.
func cleanupTimedOutBatches(ctx sdk.Context, k keeper.Keeper) {
ethereumHeight := k.GetLastObservedEthereumBlockHeight(ctx).EthereumBlockHeight
batches := k.GetOutgoingTxBatches(ctx)
for _, batch := range batches {
if batch.BatchTimeout < ethereumHeight {
k.CancelOutgoingTXBatch(ctx, batch.TokenContract, batch.BatchNonce)
}
}
}
// cleanupTimedOutBatches deletes logic calls that have passed their expiration on Ethereum
// keep in mind several things when modifying this function
// A) unlike nonces timeouts are not monotonically increasing, meaning call 5 can have a later timeout than batch 6
// this means that we MUST only cleanup a single call at a time
// B) it is possible for ethereumHeight to be zero if no events have ever occurred, make sure your code accounts for this
// C) When we compute the timeout we do our best to estimate the Ethereum block height at that very second. But what we work with
// here is the Ethereum block height at the time of the last Deposit or Withdraw to be observed. It's very important we do not
// project, if we do a slowdown on ethereum could cause a double spend. Instead timeouts will *only* occur after the timeout period
// AND any deposit or withdraw has occurred to update the Ethereum block height.
func cleanupTimedOutLogicCalls(ctx sdk.Context, k keeper.Keeper) {
ethereumHeight := k.GetLastObservedEthereumBlockHeight(ctx).EthereumBlockHeight
calls := k.GetOutgoingLogicCalls(ctx)
for _, call := range calls {
if call.Timeout < ethereumHeight {
k.CancelOutgoingLogicCall(ctx, call.InvalidationId, call.InvalidationNonce)
}
}
}
func ValsetSlashing(ctx sdk.Context, k keeper.Keeper, params types.Params) {
// don't slash in the beginning before there aren't even SignedValsetsWindow blocks yet
if uint64(ctx.BlockHeight()) <= params.SignedValsetsWindow {
return
}
unslashedValsets := k.GetUnSlashedValsets(ctx, params.SignedValsetsWindow)
// unslashedValsets are sorted by nonce in ASC order
// Question: do we need to sort each time? See if this can be epoched
for _, vs := range unslashedValsets {
confirms := k.GetValsetConfirms(ctx, vs.Nonce)
// SLASH BONDED VALIDTORS who didn't attest valset request
currentBondedSet := k.StakingKeeper.GetBondedValidatorsByPower(ctx)
for _, val := range currentBondedSet {
consAddr, _ := val.GetConsAddr()
valSigningInfo, exist := k.SlashingKeeper.GetValidatorSigningInfo(ctx, consAddr)
// Slash validator ONLY if he joined before valset is created
if exist && uint64(valSigningInfo.StartHeight) < vs.Height {
// Check if validator has confirmed valset or not
found := false
for _, conf := range confirms {
// problem site for delegate key rotation, see issue #344
ethAddress, foundEthAddress := k.GetEthAddressByValidator(ctx, val.GetOperator())
if foundEthAddress && conf.EthAddress == ethAddress {
found = true
break
}
}
// slash validators for not confirming valsets
if !found {
cons, _ := val.GetConsAddr()
k.StakingKeeper.Slash(ctx, cons, ctx.BlockHeight(), val.ConsensusPower(), params.SlashFractionValset)
if !val.IsJailed() {
k.StakingKeeper.Jail(ctx, cons)
}
}
}
}
// SLASH UNBONDING VALIDATORS who didn't attest valset request
blockTime := ctx.BlockTime().Add(k.StakingKeeper.GetParams(ctx).UnbondingTime)
blockHeight := ctx.BlockHeight()
unbondingValIterator := k.StakingKeeper.ValidatorQueueIterator(ctx, blockTime, blockHeight)
defer unbondingValIterator.Close()
// All unbonding validators
for ; unbondingValIterator.Valid(); unbondingValIterator.Next() {
unbondingValidators := k.DeserializeValidatorIterator(unbondingValIterator.Value())
for _, valAddr := range unbondingValidators.Addresses {
addr, err := sdk.ValAddressFromBech32(valAddr)
if err != nil {
panic(err)
}
validator, _ := k.StakingKeeper.GetValidator(ctx, sdk.ValAddress(addr))
valConsAddr, _ := validator.GetConsAddr()
valSigningInfo, exist := k.SlashingKeeper.GetValidatorSigningInfo(ctx, valConsAddr)
// Only slash validators who joined after valset is created and they are unbonding and UNBOND_SLASHING_WINDOW didn't passed
if exist && valSigningInfo.StartHeight < int64(vs.Height) && validator.IsUnbonding() && vs.Height < uint64(validator.UnbondingHeight)+params.UnbondSlashingValsetsWindow {
// Check if validator has confirmed valset or not
found := false
for _, conf := range confirms {
// TODO this presents problems for delegate key rotation see issue #344
confVal, _ := sdk.AccAddressFromBech32(conf.Orchestrator)
valAddr, foundValidator := k.GetOrchestratorValidator(ctx, confVal)
if foundValidator && valAddr.GetOperator().Equals(validator.GetOperator()) {
found = true
break
}
}
// slash validators for not confirming valsets
if !found {
k.StakingKeeper.Slash(ctx, valConsAddr, ctx.BlockHeight(), validator.ConsensusPower(), params.SlashFractionValset)
if !validator.IsJailed() {
k.StakingKeeper.Jail(ctx, valConsAddr)
}
}
}
}
}
// then we set the latest slashed valset nonce
k.SetLastSlashedValsetNonce(ctx, vs.Nonce)
}
}
func BatchSlashing(ctx sdk.Context, k keeper.Keeper, params types.Params) {
// We look through the full bonded set (the active set)
// and we slash users who haven't signed a batch confirmation that is >15hrs in blocks old
maxHeight := uint64(0)
// don't slash in the beginning before there aren't even SignedBatchesWindow blocks yet
if uint64(ctx.BlockHeight()) > params.SignedBatchesWindow {
maxHeight = uint64(ctx.BlockHeight()) - params.SignedBatchesWindow
} else {
// we can't slash anyone if this window has not yet passed
return
}
unslashedBatches := k.GetUnSlashedBatches(ctx, maxHeight)
for _, batch := range unslashedBatches {
// SLASH BONDED VALIDTORS who didn't attest batch requests
currentBondedSet := k.StakingKeeper.GetBondedValidatorsByPower(ctx)
confirms := k.GetBatchConfirmByNonceAndTokenContract(ctx, batch.BatchNonce, batch.TokenContract)
for _, val := range currentBondedSet {
// Don't slash validators who joined after batch is created
consAddr, _ := val.GetConsAddr()
valSigningInfo, exist := k.SlashingKeeper.GetValidatorSigningInfo(ctx, consAddr)
if exist && valSigningInfo.StartHeight > int64(batch.Block) {
continue
}
found := false
for _, conf := range confirms {
// TODO this presents problems for delegate key rotation see issue #344
confVal, _ := sdk.AccAddressFromBech32(conf.Orchestrator)
valAddr, foundValidator := k.GetOrchestratorValidator(ctx, confVal)
if foundValidator && valAddr.GetOperator().Equals(val.GetOperator()) {
found = true
break
}
}
if !found {
cons, _ := val.GetConsAddr()
k.StakingKeeper.Slash(ctx, cons, ctx.BlockHeight(), val.ConsensusPower(), params.SlashFractionBatch)
if !val.IsJailed() {
k.StakingKeeper.Jail(ctx, cons)
}
}
}
// then we set the latest slashed batch block
k.SetLastSlashedBatchBlock(ctx, batch.Block)
}
}
func LogicCallSlashing(ctx sdk.Context, k keeper.Keeper, params types.Params) {
// We look through the full bonded set (the active set)
// and we slash users who haven't signed a batch confirmation that is >15hrs in blocks old
maxHeight := uint64(0)
// don't slash in the beginning before there aren't even SignedBatchesWindow blocks yet
if uint64(ctx.BlockHeight()) > params.SignedLogicCallsWindow {
maxHeight = uint64(ctx.BlockHeight()) - params.SignedLogicCallsWindow
} else {
// we can't slash anyone if this window has not yet passed
return
}
unslashedLogicCalls := k.GetUnSlashedLogicCalls(ctx, maxHeight)
for _, call := range unslashedLogicCalls {
// SLASH BONDED VALIDTORS who didn't attest batch requests
currentBondedSet := k.StakingKeeper.GetBondedValidatorsByPower(ctx)
confirms := k.GetLogicConfirmByInvalidationIDAndNonce(ctx, call.InvalidationId, call.InvalidationNonce)
for _, val := range currentBondedSet {
// Don't slash validators who joined after batch is created
consAddr, _ := val.GetConsAddr()
valSigningInfo, exist := k.SlashingKeeper.GetValidatorSigningInfo(ctx, consAddr)
if exist && valSigningInfo.StartHeight > int64(call.Block) {
continue
}
found := false
for _, conf := range confirms {
// TODO this presents problems for delegate key rotation see issue #344
confVal, _ := sdk.AccAddressFromBech32(conf.Orchestrator)
valAddr, foundValidator := k.GetOrchestratorValidator(ctx, confVal)
if foundValidator && valAddr.GetOperator().Equals(val.GetOperator()) {
found = true
break
}
}
if !found {
cons, _ := val.GetConsAddr()
k.StakingKeeper.Slash(ctx, cons, ctx.BlockHeight(), val.ConsensusPower(), params.SlashFractionLogicCall)
if !val.IsJailed() {
k.StakingKeeper.Jail(ctx, cons)
}
}
}
// then we set the latest slashed logic call block
k.SetLastSlashedLogicCallBlock(ctx, call.Block)
}
}
// Iterate over all attestations currently being voted on in order of nonce
// and prune those that are older than the current nonce and no longer have any
// use. This could be combined with create attestation and save some computation
// but (A) pruning keeps the iteration small in the first place and (B) there is
// already enough nuance in the other handler that it's best not to complicate it further
func pruneAttestations(ctx sdk.Context, k keeper.Keeper) {
attmap := k.GetAttestationMapping(ctx)
// We make a slice with all the event nonces that are in the attestation mapping
keys := make([]uint64, 0, len(attmap))
for k := range attmap {
keys = append(keys, k)
}
// Then we sort it
sort.Slice(keys, func(i, j int) bool { return keys[i] < keys[j] })
// we delete all attestations earlier than the current event nonce
// minus some buffer value. This buffer value is purely to allow
// frontends and other UI components to view recent oracle history
const eventsToKeep = 1000
lastNonce := uint64(k.GetLastObservedEventNonce(ctx))
var cutoff uint64
if lastNonce <= eventsToKeep {
return
} else {
cutoff = lastNonce - eventsToKeep
}
// This iterates over all keys (event nonces) in the attestation mapping. Each value contains
// a slice with one or more attestations at that event nonce. There can be multiple attestations
// at one event nonce when validators disagree about what event happened at that nonce.
for _, nonce := range keys {
// This iterates over all attestations at a particular event nonce.
// They are ordered by when the first attestation at the event nonce was received.
// This order is not important.
for _, att := range attmap[nonce] {
// delete all before the cutoff
if nonce < cutoff {
k.DeleteAttestation(ctx, att)
}
}
}
}