2019-06-06 21:30:40 +00:00
|
|
|
from eth2spec.test.context import spec_state_test, never_bls, with_all_phases
|
2019-06-30 22:33:10 +00:00
|
|
|
from eth2spec.test.helpers.state import next_epoch, next_epoch_with_attestations
|
|
|
|
from eth2spec.test.helpers.block import apply_empty_block
|
2019-05-30 20:57:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
def check_finality(spec,
|
|
|
|
state,
|
2019-04-19 00:33:06 +00:00
|
|
|
prev_state,
|
|
|
|
current_justified_changed,
|
|
|
|
previous_justified_changed,
|
|
|
|
finalized_changed):
|
|
|
|
if current_justified_changed:
|
2019-06-22 18:00:26 +00:00
|
|
|
assert state.current_justified_checkpoint.epoch > prev_state.current_justified_checkpoint.epoch
|
|
|
|
assert state.current_justified_checkpoint.root != prev_state.current_justified_checkpoint.root
|
2019-04-19 00:33:06 +00:00
|
|
|
else:
|
2019-06-22 18:00:26 +00:00
|
|
|
assert state.current_justified_checkpoint == prev_state.current_justified_checkpoint
|
2019-04-19 00:33:06 +00:00
|
|
|
|
|
|
|
if previous_justified_changed:
|
2019-06-22 18:00:26 +00:00
|
|
|
assert state.previous_justified_checkpoint.epoch > prev_state.previous_justified_checkpoint.epoch
|
|
|
|
assert state.previous_justified_checkpoint.root != prev_state.previous_justified_checkpoint.root
|
2019-04-19 00:33:06 +00:00
|
|
|
else:
|
2019-06-22 18:00:26 +00:00
|
|
|
assert state.previous_justified_checkpoint == prev_state.previous_justified_checkpoint
|
2019-04-19 00:33:06 +00:00
|
|
|
|
|
|
|
if finalized_changed:
|
2019-06-22 18:00:26 +00:00
|
|
|
assert state.finalized_checkpoint.epoch > prev_state.finalized_checkpoint.epoch
|
|
|
|
assert state.finalized_checkpoint.root != prev_state.finalized_checkpoint.root
|
2019-04-19 00:33:06 +00:00
|
|
|
else:
|
2019-06-22 18:00:26 +00:00
|
|
|
assert state.finalized_checkpoint == prev_state.finalized_checkpoint
|
2019-04-19 00:33:06 +00:00
|
|
|
|
|
|
|
|
2019-06-06 21:30:40 +00:00
|
|
|
@with_all_phases
|
2019-05-20 17:38:18 +00:00
|
|
|
@never_bls
|
2019-05-06 22:23:31 +00:00
|
|
|
@spec_state_test
|
2019-06-21 20:43:55 +00:00
|
|
|
def test_finality_no_updates_at_genesis(spec, state):
|
|
|
|
assert spec.get_current_epoch(state) == spec.GENESIS_EPOCH
|
|
|
|
|
2019-05-06 22:23:31 +00:00
|
|
|
yield 'pre', state
|
2019-04-19 00:33:06 +00:00
|
|
|
|
|
|
|
blocks = []
|
2019-06-21 20:43:55 +00:00
|
|
|
for epoch in range(2):
|
2019-05-30 20:57:18 +00:00
|
|
|
prev_state, new_blocks, state = next_epoch_with_attestations(spec, state, True, False)
|
2019-04-23 19:32:41 +00:00
|
|
|
blocks += new_blocks
|
2019-04-19 00:33:06 +00:00
|
|
|
|
2019-04-24 17:44:03 +00:00
|
|
|
# justification/finalization skipped at GENESIS_EPOCH
|
2019-04-19 00:33:06 +00:00
|
|
|
if epoch == 0:
|
2019-05-30 20:57:18 +00:00
|
|
|
check_finality(spec, state, prev_state, False, False, False)
|
2019-04-24 17:44:03 +00:00
|
|
|
# justification/finalization skipped at GENESIS_EPOCH + 1
|
2019-04-19 00:33:06 +00:00
|
|
|
elif epoch == 1:
|
2019-05-30 20:57:18 +00:00
|
|
|
check_finality(spec, state, prev_state, False, False, False)
|
2019-06-21 20:43:55 +00:00
|
|
|
|
2019-06-22 17:48:06 +00:00
|
|
|
yield 'blocks', blocks
|
2019-06-21 20:43:55 +00:00
|
|
|
yield 'post', state
|
|
|
|
|
|
|
|
|
|
|
|
@with_all_phases
|
|
|
|
@never_bls
|
|
|
|
@spec_state_test
|
|
|
|
def test_finality_rule_4(spec, state):
|
|
|
|
# get past first two epochs that finality does not run on
|
|
|
|
next_epoch(spec, state)
|
|
|
|
apply_empty_block(spec, state)
|
|
|
|
next_epoch(spec, state)
|
|
|
|
apply_empty_block(spec, state)
|
|
|
|
|
|
|
|
yield 'pre', state
|
|
|
|
|
|
|
|
blocks = []
|
|
|
|
for epoch in range(2):
|
|
|
|
prev_state, new_blocks, state = next_epoch_with_attestations(spec, state, True, False)
|
|
|
|
blocks += new_blocks
|
|
|
|
|
|
|
|
if epoch == 0:
|
2019-05-30 20:57:18 +00:00
|
|
|
check_finality(spec, state, prev_state, True, False, False)
|
2019-06-21 20:43:55 +00:00
|
|
|
elif epoch == 1:
|
2019-04-24 17:38:26 +00:00
|
|
|
# rule 4 of finality
|
2019-05-30 20:57:18 +00:00
|
|
|
check_finality(spec, state, prev_state, True, True, True)
|
2019-06-22 18:00:26 +00:00
|
|
|
assert state.finalized_checkpoint == prev_state.current_justified_checkpoint
|
2019-04-19 00:33:06 +00:00
|
|
|
|
2019-06-22 17:48:06 +00:00
|
|
|
yield 'blocks', blocks
|
2019-05-06 22:23:31 +00:00
|
|
|
yield 'post', state
|
2019-04-19 00:33:06 +00:00
|
|
|
|
|
|
|
|
2019-06-06 21:30:40 +00:00
|
|
|
@with_all_phases
|
2019-05-20 17:38:18 +00:00
|
|
|
@never_bls
|
2019-05-06 22:23:31 +00:00
|
|
|
@spec_state_test
|
2019-05-30 20:57:18 +00:00
|
|
|
def test_finality_rule_1(spec, state):
|
2019-04-19 00:33:06 +00:00
|
|
|
# get past first two epochs that finality does not run on
|
2019-05-30 20:57:18 +00:00
|
|
|
next_epoch(spec, state)
|
|
|
|
apply_empty_block(spec, state)
|
|
|
|
next_epoch(spec, state)
|
|
|
|
apply_empty_block(spec, state)
|
2019-04-19 00:33:06 +00:00
|
|
|
|
2019-05-06 22:23:31 +00:00
|
|
|
yield 'pre', state
|
2019-04-19 00:33:06 +00:00
|
|
|
|
|
|
|
blocks = []
|
|
|
|
for epoch in range(3):
|
2019-05-30 20:57:18 +00:00
|
|
|
prev_state, new_blocks, state = next_epoch_with_attestations(spec, state, False, True)
|
2019-04-23 19:32:41 +00:00
|
|
|
blocks += new_blocks
|
2019-04-19 00:33:06 +00:00
|
|
|
|
|
|
|
if epoch == 0:
|
2019-05-30 20:57:18 +00:00
|
|
|
check_finality(spec, state, prev_state, True, False, False)
|
2019-04-19 00:33:06 +00:00
|
|
|
elif epoch == 1:
|
2019-05-30 20:57:18 +00:00
|
|
|
check_finality(spec, state, prev_state, True, True, False)
|
2019-04-19 00:33:06 +00:00
|
|
|
elif epoch == 2:
|
|
|
|
# finalized by rule 1
|
2019-05-30 20:57:18 +00:00
|
|
|
check_finality(spec, state, prev_state, True, True, True)
|
2019-06-22 18:00:26 +00:00
|
|
|
assert state.finalized_checkpoint == prev_state.previous_justified_checkpoint
|
2019-04-19 00:33:06 +00:00
|
|
|
|
2019-06-22 17:48:06 +00:00
|
|
|
yield 'blocks', blocks
|
2019-05-06 22:23:31 +00:00
|
|
|
yield 'post', state
|
2019-04-22 15:00:01 +00:00
|
|
|
|
2019-04-19 00:33:06 +00:00
|
|
|
|
2019-06-06 21:30:40 +00:00
|
|
|
@with_all_phases
|
2019-05-20 17:38:18 +00:00
|
|
|
@never_bls
|
2019-05-06 22:23:31 +00:00
|
|
|
@spec_state_test
|
2019-05-30 20:57:18 +00:00
|
|
|
def test_finality_rule_2(spec, state):
|
2019-04-19 00:33:06 +00:00
|
|
|
# get past first two epochs that finality does not run on
|
2019-05-30 20:57:18 +00:00
|
|
|
next_epoch(spec, state)
|
|
|
|
apply_empty_block(spec, state)
|
|
|
|
next_epoch(spec, state)
|
|
|
|
apply_empty_block(spec, state)
|
2019-04-19 00:33:06 +00:00
|
|
|
|
2019-05-06 22:23:31 +00:00
|
|
|
yield 'pre', state
|
2019-04-19 00:33:06 +00:00
|
|
|
|
|
|
|
blocks = []
|
|
|
|
for epoch in range(3):
|
2019-04-22 15:00:01 +00:00
|
|
|
if epoch == 0:
|
2019-05-30 20:57:18 +00:00
|
|
|
prev_state, new_blocks, state = next_epoch_with_attestations(spec, state, True, False)
|
|
|
|
check_finality(spec, state, prev_state, True, False, False)
|
2019-04-24 17:38:26 +00:00
|
|
|
elif epoch == 1:
|
2019-05-30 20:57:18 +00:00
|
|
|
prev_state, new_blocks, state = next_epoch_with_attestations(spec, state, False, False)
|
|
|
|
check_finality(spec, state, prev_state, False, True, False)
|
2019-04-24 17:38:26 +00:00
|
|
|
elif epoch == 2:
|
2019-05-30 20:57:18 +00:00
|
|
|
prev_state, new_blocks, state = next_epoch_with_attestations(spec, state, False, True)
|
2019-04-22 15:00:01 +00:00
|
|
|
# finalized by rule 2
|
2019-05-30 20:57:18 +00:00
|
|
|
check_finality(spec, state, prev_state, True, False, True)
|
2019-06-22 18:00:26 +00:00
|
|
|
assert state.finalized_checkpoint == prev_state.previous_justified_checkpoint
|
2019-04-23 19:32:41 +00:00
|
|
|
|
|
|
|
blocks += new_blocks
|
|
|
|
|
2019-06-22 17:48:06 +00:00
|
|
|
yield 'blocks', blocks
|
2019-05-06 22:23:31 +00:00
|
|
|
yield 'post', state
|
2019-04-22 15:00:01 +00:00
|
|
|
|
|
|
|
|
2019-06-06 21:30:40 +00:00
|
|
|
@with_all_phases
|
2019-05-20 17:38:18 +00:00
|
|
|
@never_bls
|
2019-05-06 22:23:31 +00:00
|
|
|
@spec_state_test
|
2019-05-30 20:57:18 +00:00
|
|
|
def test_finality_rule_3(spec, state):
|
2019-04-23 19:32:41 +00:00
|
|
|
"""
|
|
|
|
Test scenario described here
|
|
|
|
https://github.com/ethereum/eth2.0-specs/issues/611#issuecomment-463612892
|
|
|
|
"""
|
2019-04-22 15:00:01 +00:00
|
|
|
# get past first two epochs that finality does not run on
|
2019-05-30 20:57:18 +00:00
|
|
|
next_epoch(spec, state)
|
|
|
|
apply_empty_block(spec, state)
|
|
|
|
next_epoch(spec, state)
|
|
|
|
apply_empty_block(spec, state)
|
2019-04-19 00:33:06 +00:00
|
|
|
|
2019-05-06 22:23:31 +00:00
|
|
|
yield 'pre', state
|
2019-04-22 15:00:01 +00:00
|
|
|
|
|
|
|
blocks = []
|
2019-05-30 20:57:18 +00:00
|
|
|
prev_state, new_blocks, state = next_epoch_with_attestations(spec, state, True, False)
|
2019-04-23 19:32:41 +00:00
|
|
|
blocks += new_blocks
|
2019-05-30 20:57:18 +00:00
|
|
|
check_finality(spec, state, prev_state, True, False, False)
|
2019-04-22 15:00:01 +00:00
|
|
|
|
2019-04-24 17:44:03 +00:00
|
|
|
# In epoch N, JE is set to N, prev JE is set to N-1
|
2019-05-30 20:57:18 +00:00
|
|
|
prev_state, new_blocks, state = next_epoch_with_attestations(spec, state, True, False)
|
2019-04-23 19:32:41 +00:00
|
|
|
blocks += new_blocks
|
2019-05-30 20:57:18 +00:00
|
|
|
check_finality(spec, state, prev_state, True, True, True)
|
2019-04-22 15:00:01 +00:00
|
|
|
|
2019-04-24 17:44:03 +00:00
|
|
|
# In epoch N+1, JE is N, prev JE is N-1, and not enough messages get in to do anything
|
2019-05-30 20:57:18 +00:00
|
|
|
prev_state, new_blocks, state = next_epoch_with_attestations(spec, state, False, False)
|
2019-04-23 19:32:41 +00:00
|
|
|
blocks += new_blocks
|
2019-05-30 20:57:18 +00:00
|
|
|
check_finality(spec, state, prev_state, False, True, False)
|
2019-04-22 15:00:01 +00:00
|
|
|
|
2019-04-24 17:44:03 +00:00
|
|
|
# In epoch N+2, JE is N, prev JE is N, and enough messages from the previous epoch get in to justify N+1.
|
|
|
|
# N+1 now becomes the JE. Not enough messages from epoch N+2 itself get in to justify N+2
|
2019-05-30 20:57:18 +00:00
|
|
|
prev_state, new_blocks, state = next_epoch_with_attestations(spec, state, False, True)
|
2019-04-23 19:32:41 +00:00
|
|
|
blocks += new_blocks
|
|
|
|
# rule 2
|
2019-05-30 20:57:18 +00:00
|
|
|
check_finality(spec, state, prev_state, True, False, True)
|
2019-04-22 15:00:01 +00:00
|
|
|
|
2019-04-24 17:44:03 +00:00
|
|
|
# In epoch N+3, LJE is N+1, prev LJE is N, and enough messages get in to justify epochs N+2 and N+3.
|
2019-05-30 20:57:18 +00:00
|
|
|
prev_state, new_blocks, state = next_epoch_with_attestations(spec, state, True, True)
|
2019-04-23 19:32:41 +00:00
|
|
|
blocks += new_blocks
|
|
|
|
# rule 3
|
2019-05-30 20:57:18 +00:00
|
|
|
check_finality(spec, state, prev_state, True, True, True)
|
2019-06-22 18:00:26 +00:00
|
|
|
assert state.finalized_checkpoint == prev_state.current_justified_checkpoint
|
2019-04-22 15:00:01 +00:00
|
|
|
|
2019-06-22 17:48:06 +00:00
|
|
|
yield 'blocks', blocks
|
2019-05-06 22:23:31 +00:00
|
|
|
yield 'post', state
|