forked from leanEthereum/leanSpec
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_equivocation.py
More file actions
532 lines (504 loc) · 18.4 KB
/
Copy pathtest_equivocation.py
File metadata and controls
532 lines (504 loc) · 18.4 KB
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
"""Equivocating Proposer Tests."""
import pytest
from consensus_testing import (
AggregatedAttestationSpec,
AttestationCheck,
AttestationStep,
BlockSpec,
BlockStep,
ForkChoiceTestFiller,
GossipAggregatedAttestationStep,
GossipAttestationSpec,
StoreChecks,
TickStep,
build_genesis_state,
)
from lean_spec.spec.forks import Slot, ValidatorIndex
pytestmark = pytest.mark.valid_until("Lstar")
def test_equivocating_proposer_two_blocks_at_same_slot(
fork_choice_test: ForkChoiceTestFiller,
) -> None:
"""
Two blocks at the same slot from one proposer are both accepted.
Given
-----
- 4 validators; a slot needs 3 votes (2/3) to be justified.
- the chain:
genesis -> block_1(1)
- equivocation_a(2)
- equivocation_b(2)
- equivocation_a includes V0's vote for block_1.
- equivocation_b includes V1's vote for block_1.
- the differing votes give the two blocks different roots.
- neither block gains a weight advantage from its vote.
When
----
- the proposer publishes both blocks at slot 2.
Then
----
- the store accepts both blocks.
- head is equivocation_a after the first block.
- the two blocks have equal weight after the second block.
- the tiebreaker picks the head by lexicographic root.
- head stays at slot 2 throughout.
"""
fork_choice_test(
steps=[
BlockStep(
block=BlockSpec(slot=Slot(1), label="block_1"),
checks=StoreChecks(
head_slot=Slot(1),
head_root_label="block_1",
),
),
BlockStep(
block=BlockSpec(
slot=Slot(2),
parent_label="block_1",
label="equivocation_a",
attestations=[
AggregatedAttestationSpec(
validator_indices=[ValidatorIndex(0)],
slot=Slot(1),
target_slot=Slot(1),
target_root_label="block_1",
),
],
),
valid=True,
checks=StoreChecks(
head_slot=Slot(2),
head_root_label="equivocation_a",
),
),
BlockStep(
block=BlockSpec(
slot=Slot(2),
parent_label="block_1",
label="equivocation_b",
attestations=[
AggregatedAttestationSpec(
validator_indices=[ValidatorIndex(1)],
slot=Slot(1),
target_slot=Slot(1),
target_root_label="block_1",
),
],
),
valid=True,
checks=StoreChecks(
head_slot=Slot(2),
lexicographic_head_among=["equivocation_a", "equivocation_b"],
),
),
],
)
def test_equivocating_proposer_with_split_attestations(
fork_choice_test: ForkChoiceTestFiller,
) -> None:
"""
Votes split across equivocating forks move the head to the heavier fork.
Given
-----
- 6 validators; a slot needs 4 votes (2/3) to be justified.
- the chain:
genesis -> block_1(1)
- fork_a(2)
- fork_b(2)
- fork_a includes V0's vote for block_1.
- fork_b includes V1's vote for block_1.
- the differing votes give the two forks different roots.
- neither fork gains a weight advantage from its vote.
When
----
- V0 and V1 gossip-vote for fork_a, V2 and V3 gossip-vote for fork_b.
- then V4 gossip-votes for fork_b.
Then
----
- the forks have equal weight at 2 each.
- the tiebreaker picks the head by lexicographic root.
- V4's vote gives fork_b weight 3 against fork_a's 2.
- head moves to fork_b.
- both forks remain in the store throughout.
"""
fork_choice_test(
anchor_state=build_genesis_state(num_validators=6),
steps=[
BlockStep(
block=BlockSpec(slot=Slot(1), label="block_1"),
checks=StoreChecks(
head_slot=Slot(1),
head_root_label="block_1",
),
),
BlockStep(
block=BlockSpec(
slot=Slot(2),
parent_label="block_1",
label="fork_a",
attestations=[
AggregatedAttestationSpec(
validator_indices=[ValidatorIndex(0)],
slot=Slot(1),
target_slot=Slot(1),
target_root_label="block_1",
),
],
),
valid=True,
checks=StoreChecks(
head_slot=Slot(2),
head_root_label="fork_a",
),
),
BlockStep(
block=BlockSpec(
slot=Slot(2),
parent_label="block_1",
label="fork_b",
attestations=[
AggregatedAttestationSpec(
validator_indices=[ValidatorIndex(1)],
slot=Slot(1),
target_slot=Slot(1),
target_root_label="block_1",
),
],
),
valid=True,
checks=StoreChecks(
head_slot=Slot(2),
lexicographic_head_among=["fork_a", "fork_b"],
),
),
AttestationStep(
attestation=GossipAttestationSpec(
validator_index=ValidatorIndex(0),
slot=Slot(2),
target_slot=Slot(2),
target_root_label="fork_a",
),
),
AttestationStep(
attestation=GossipAttestationSpec(
validator_index=ValidatorIndex(1),
slot=Slot(2),
target_slot=Slot(2),
target_root_label="fork_a",
),
),
AttestationStep(
attestation=GossipAttestationSpec(
validator_index=ValidatorIndex(2),
slot=Slot(2),
target_slot=Slot(2),
target_root_label="fork_b",
),
),
AttestationStep(
attestation=GossipAttestationSpec(
validator_index=ValidatorIndex(3),
slot=Slot(2),
target_slot=Slot(2),
target_root_label="fork_b",
),
),
TickStep(
time=12,
checks=StoreChecks(
head_slot=Slot(2),
lexicographic_head_among=["fork_a", "fork_b"],
),
),
AttestationStep(
attestation=GossipAttestationSpec(
validator_index=ValidatorIndex(4),
slot=Slot(2),
target_slot=Slot(2),
target_root_label="fork_b",
),
),
TickStep(
time=16,
checks=StoreChecks(
head_slot=Slot(2),
head_root_label="fork_b",
),
),
],
)
def test_same_slot_equivocating_attesters_count_once(
fork_choice_test: ForkChoiceTestFiller,
) -> None:
"""
An equivocating validator is counted once, on the canonical-root fork.
Given
-----
- 8 validators; a slot needs 6 votes (2/3) to be justified.
- the chain:
genesis -> common(1)
- fork_a(2)
- fork_b(3)
- one vote at slot 3 targets fork_a from V0, V1, V2.
- one vote at slot 3 targets fork_b from V0, V1, V3, V4.
- V0 and V1 equivocate by voting on both forks at the same slot.
- the two votes tie on slot, so the larger attestation-data root wins.
- the fork_a vote carries the larger root.
When
----
- both votes arrive by gossip and are accepted.
Then
----
- V0 and V1 count once, toward fork_a.
- fork_a has effective weight 3 from V0, V1, V2.
- fork_b has effective weight 2 from V3, V4.
- head is fork_a.
- no slot is justified by the below-threshold votes.
"""
fork_choice_test(
anchor_state=build_genesis_state(num_validators=8),
steps=[
BlockStep(
block=BlockSpec(slot=Slot(1), label="common"),
checks=StoreChecks(head_slot=Slot(1), head_root_label="common"),
),
BlockStep(
block=BlockSpec(slot=Slot(2), parent_label="common", label="fork_a"),
checks=StoreChecks(head_slot=Slot(2), head_root_label="fork_a"),
),
BlockStep(
block=BlockSpec(slot=Slot(3), parent_label="common", label="fork_b"),
checks=StoreChecks(lexicographic_head_among=["fork_a", "fork_b"]),
),
TickStep(interval=18),
GossipAggregatedAttestationStep(
attestation=AggregatedAttestationSpec(
validator_indices=[
ValidatorIndex(0),
ValidatorIndex(1),
ValidatorIndex(2),
],
slot=Slot(3),
target_slot=Slot(2),
target_root_label="fork_a",
),
),
GossipAggregatedAttestationStep(
attestation=AggregatedAttestationSpec(
validator_indices=[
ValidatorIndex(0),
ValidatorIndex(1),
ValidatorIndex(3),
ValidatorIndex(4),
],
slot=Slot(3),
target_slot=Slot(3),
target_root_label="fork_b",
),
),
TickStep(
time=16,
checks=StoreChecks(
head_slot=Slot(2),
head_root_label="fork_a",
latest_justified_slot=Slot(0),
latest_finalized_slot=Slot(0),
latest_known_aggregated_target_slots=[Slot(2), Slot(3)],
attestation_checks=[
AttestationCheck(
validator=ValidatorIndex(0),
location="known",
attestation_slot=Slot(3),
target_slot=Slot(2),
),
AttestationCheck(
validator=ValidatorIndex(1),
location="known",
attestation_slot=Slot(3),
target_slot=Slot(2),
),
AttestationCheck(
validator=ValidatorIndex(2),
location="known",
attestation_slot=Slot(3),
target_slot=Slot(2),
),
AttestationCheck(
validator=ValidatorIndex(3),
location="known",
attestation_slot=Slot(3),
target_slot=Slot(3),
),
AttestationCheck(
validator=ValidatorIndex(4),
location="known",
attestation_slot=Slot(3),
target_slot=Slot(3),
),
],
),
),
],
)
def test_equivocation_head_independent_of_arrival_order_a_then_b(
fork_choice_test: ForkChoiceTestFiller,
) -> None:
"""
Head ignores arrival order: fork_a arriving first still picks the canonical-root fork.
Given
-----
- 6 validators; a slot needs 4 votes (2/3) to be justified.
- the chain:
genesis -> common(1)
- fork_a(2)
- fork_b(3)
- one vote at slot 3 targets fork_a from V0, V1.
- one vote at slot 3 targets fork_b from V0, V2.
- V0 equivocates by voting on both forks at the same slot.
- V1 backs fork_a alone; V2 backs fork_b alone.
- without V0 the two forks tie one-to-one, so V0's vote decides the head.
- the two votes tie on slot, so the larger attestation-data root wins.
- the fork_b vote carries the larger root.
When
----
- the fork_a vote arrives first, then the fork_b vote.
Then
----
- V0 counts once, toward the larger-root fork.
- the larger-root fork has effective weight 2.
- the smaller-root fork has effective weight 1.
- head is fork_b.
- no slot is justified by the below-threshold votes.
"""
fork_choice_test(
anchor_state=build_genesis_state(num_validators=6),
steps=[
BlockStep(
block=BlockSpec(slot=Slot(1), label="common"),
checks=StoreChecks(head_slot=Slot(1), head_root_label="common"),
),
BlockStep(
block=BlockSpec(slot=Slot(2), parent_label="common", label="fork_a"),
checks=StoreChecks(head_slot=Slot(2), head_root_label="fork_a"),
),
BlockStep(
block=BlockSpec(slot=Slot(3), parent_label="common", label="fork_b"),
checks=StoreChecks(lexicographic_head_among=["fork_a", "fork_b"]),
),
TickStep(interval=18),
GossipAggregatedAttestationStep(
attestation=AggregatedAttestationSpec(
validator_indices=[ValidatorIndex(0), ValidatorIndex(1)],
slot=Slot(3),
target_slot=Slot(2),
target_root_label="fork_a",
),
),
GossipAggregatedAttestationStep(
attestation=AggregatedAttestationSpec(
validator_indices=[ValidatorIndex(0), ValidatorIndex(2)],
slot=Slot(3),
target_slot=Slot(3),
target_root_label="fork_b",
),
),
TickStep(
time=16,
checks=StoreChecks(
head_slot=Slot(3),
head_root_label="fork_b",
latest_justified_slot=Slot(0),
latest_finalized_slot=Slot(0),
attestation_checks=[
AttestationCheck(
validator=ValidatorIndex(0),
location="known",
attestation_slot=Slot(3),
target_slot=Slot(3),
),
],
),
),
],
)
def test_equivocation_head_independent_of_arrival_order_b_then_a(
fork_choice_test: ForkChoiceTestFiller,
) -> None:
"""
Head ignores arrival order: fork_b arriving first still picks the canonical-root fork.
Given
-----
- 6 validators; a slot needs 4 votes (2/3) to be justified.
- the chain:
genesis -> common(1)
- fork_a(2)
- fork_b(3)
- one vote at slot 3 targets fork_a from V0, V1.
- one vote at slot 3 targets fork_b from V0, V2.
- V0 equivocates by voting on both forks at the same slot.
- V1 backs fork_a alone; V2 backs fork_b alone.
- without V0 the two forks tie one-to-one, so V0's vote decides the head.
- the two votes tie on slot, so the larger attestation-data root wins.
- the fork_b vote carries the larger root.
When
----
- the fork_b vote arrives first, then the fork_a vote.
Then
----
- V0 counts once, toward the larger-root fork.
- the larger-root fork has effective weight 2.
- the smaller-root fork has effective weight 1.
- head is fork_b, matching the opposite arrival order.
- no slot is justified by the below-threshold votes.
"""
fork_choice_test(
anchor_state=build_genesis_state(num_validators=6),
steps=[
BlockStep(
block=BlockSpec(slot=Slot(1), label="common"),
checks=StoreChecks(head_slot=Slot(1), head_root_label="common"),
),
BlockStep(
block=BlockSpec(slot=Slot(2), parent_label="common", label="fork_a"),
checks=StoreChecks(head_slot=Slot(2), head_root_label="fork_a"),
),
BlockStep(
block=BlockSpec(slot=Slot(3), parent_label="common", label="fork_b"),
checks=StoreChecks(lexicographic_head_among=["fork_a", "fork_b"]),
),
TickStep(interval=18),
GossipAggregatedAttestationStep(
attestation=AggregatedAttestationSpec(
validator_indices=[ValidatorIndex(0), ValidatorIndex(2)],
slot=Slot(3),
target_slot=Slot(3),
target_root_label="fork_b",
),
),
GossipAggregatedAttestationStep(
attestation=AggregatedAttestationSpec(
validator_indices=[ValidatorIndex(0), ValidatorIndex(1)],
slot=Slot(3),
target_slot=Slot(2),
target_root_label="fork_a",
),
),
TickStep(
time=16,
checks=StoreChecks(
head_slot=Slot(3),
head_root_label="fork_b",
latest_justified_slot=Slot(0),
latest_finalized_slot=Slot(0),
attestation_checks=[
AttestationCheck(
validator=ValidatorIndex(0),
location="known",
attestation_slot=Slot(3),
target_slot=Slot(3),
),
],
),
),
],
)