mirror of
https://github.com/status-im/eth2.0-specs.git
synced 2025-02-21 23:08:12 +00:00
port finality tests to new format. still has excessive deepcopies
This commit is contained in:
parent
e5a50bc8a0
commit
8e619f8ab9
@ -1,7 +1,5 @@
|
|||||||
from copy import deepcopy
|
from copy import deepcopy
|
||||||
|
|
||||||
import pytest
|
|
||||||
|
|
||||||
import eth2spec.phase0.spec as spec
|
import eth2spec.phase0.spec as spec
|
||||||
|
|
||||||
from eth2spec.phase0.state_transition import (
|
from eth2spec.phase0.state_transition import (
|
||||||
@ -16,8 +14,7 @@ from .helpers import (
|
|||||||
next_epoch,
|
next_epoch,
|
||||||
)
|
)
|
||||||
|
|
||||||
# mark entire file as 'state'
|
from .context import spec_state_test
|
||||||
pytestmark = pytest.mark.state
|
|
||||||
|
|
||||||
|
|
||||||
def check_finality(state,
|
def check_finality(state,
|
||||||
@ -73,126 +70,130 @@ def next_epoch_with_attestations(state,
|
|||||||
return state, blocks, post_state
|
return state, blocks, post_state
|
||||||
|
|
||||||
|
|
||||||
|
@spec_state_test
|
||||||
def test_finality_rule_4(state):
|
def test_finality_rule_4(state):
|
||||||
test_state = deepcopy(state)
|
yield 'pre', state
|
||||||
|
|
||||||
blocks = []
|
blocks = []
|
||||||
for epoch in range(4):
|
for epoch in range(4):
|
||||||
prev_state, new_blocks, test_state = next_epoch_with_attestations(test_state, True, False)
|
prev_state, new_blocks, state = next_epoch_with_attestations(state, True, False)
|
||||||
blocks += new_blocks
|
blocks += new_blocks
|
||||||
|
|
||||||
# justification/finalization skipped at GENESIS_EPOCH
|
# justification/finalization skipped at GENESIS_EPOCH
|
||||||
if epoch == 0:
|
if epoch == 0:
|
||||||
check_finality(test_state, prev_state, False, False, False)
|
check_finality(state, prev_state, False, False, False)
|
||||||
# justification/finalization skipped at GENESIS_EPOCH + 1
|
# justification/finalization skipped at GENESIS_EPOCH + 1
|
||||||
elif epoch == 1:
|
elif epoch == 1:
|
||||||
check_finality(test_state, prev_state, False, False, False)
|
check_finality(state, prev_state, False, False, False)
|
||||||
elif epoch == 2:
|
elif epoch == 2:
|
||||||
check_finality(test_state, prev_state, True, False, False)
|
check_finality(state, prev_state, True, False, False)
|
||||||
elif epoch >= 3:
|
elif epoch >= 3:
|
||||||
# rule 4 of finality
|
# rule 4 of finality
|
||||||
check_finality(test_state, prev_state, True, True, True)
|
check_finality(state, prev_state, True, True, True)
|
||||||
assert test_state.finalized_epoch == prev_state.current_justified_epoch
|
assert state.finalized_epoch == prev_state.current_justified_epoch
|
||||||
assert test_state.finalized_root == prev_state.current_justified_root
|
assert state.finalized_root == prev_state.current_justified_root
|
||||||
|
|
||||||
return state, blocks, test_state
|
yield 'blocks', [blocks], [spec.BeaconBlock]
|
||||||
|
yield 'post', state
|
||||||
|
|
||||||
|
|
||||||
|
@spec_state_test
|
||||||
def test_finality_rule_1(state):
|
def test_finality_rule_1(state):
|
||||||
# get past first two epochs that finality does not run on
|
# get past first two epochs that finality does not run on
|
||||||
next_epoch(state)
|
next_epoch(state)
|
||||||
next_epoch(state)
|
next_epoch(state)
|
||||||
|
|
||||||
pre_state = deepcopy(state)
|
yield 'pre', state
|
||||||
test_state = deepcopy(state)
|
|
||||||
|
|
||||||
blocks = []
|
blocks = []
|
||||||
for epoch in range(3):
|
for epoch in range(3):
|
||||||
prev_state, new_blocks, test_state = next_epoch_with_attestations(test_state, False, True)
|
prev_state, new_blocks, state = next_epoch_with_attestations(state, False, True)
|
||||||
blocks += new_blocks
|
blocks += new_blocks
|
||||||
|
|
||||||
if epoch == 0:
|
if epoch == 0:
|
||||||
check_finality(test_state, prev_state, True, False, False)
|
check_finality(state, prev_state, True, False, False)
|
||||||
elif epoch == 1:
|
elif epoch == 1:
|
||||||
check_finality(test_state, prev_state, True, True, False)
|
check_finality(state, prev_state, True, True, False)
|
||||||
elif epoch == 2:
|
elif epoch == 2:
|
||||||
# finalized by rule 1
|
# finalized by rule 1
|
||||||
check_finality(test_state, prev_state, True, True, True)
|
check_finality(state, prev_state, True, True, True)
|
||||||
assert test_state.finalized_epoch == prev_state.previous_justified_epoch
|
assert state.finalized_epoch == prev_state.previous_justified_epoch
|
||||||
assert test_state.finalized_root == prev_state.previous_justified_root
|
assert state.finalized_root == prev_state.previous_justified_root
|
||||||
|
|
||||||
return pre_state, blocks, test_state
|
yield 'blocks', [blocks], [spec.BeaconBlock]
|
||||||
|
yield 'post', state
|
||||||
|
|
||||||
|
|
||||||
|
@spec_state_test
|
||||||
def test_finality_rule_2(state):
|
def test_finality_rule_2(state):
|
||||||
# get past first two epochs that finality does not run on
|
# get past first two epochs that finality does not run on
|
||||||
next_epoch(state)
|
next_epoch(state)
|
||||||
next_epoch(state)
|
next_epoch(state)
|
||||||
|
|
||||||
pre_state = deepcopy(state)
|
yield 'pre', state
|
||||||
test_state = deepcopy(state)
|
|
||||||
|
|
||||||
blocks = []
|
blocks = []
|
||||||
for epoch in range(3):
|
for epoch in range(3):
|
||||||
if epoch == 0:
|
if epoch == 0:
|
||||||
prev_state, new_blocks, test_state = next_epoch_with_attestations(test_state, True, False)
|
prev_state, new_blocks, state = next_epoch_with_attestations(state, True, False)
|
||||||
check_finality(test_state, prev_state, True, False, False)
|
check_finality(state, prev_state, True, False, False)
|
||||||
elif epoch == 1:
|
elif epoch == 1:
|
||||||
prev_state, new_blocks, test_state = next_epoch_with_attestations(test_state, False, False)
|
prev_state, new_blocks, state = next_epoch_with_attestations(state, False, False)
|
||||||
check_finality(test_state, prev_state, False, True, False)
|
check_finality(state, prev_state, False, True, False)
|
||||||
elif epoch == 2:
|
elif epoch == 2:
|
||||||
prev_state, new_blocks, test_state = next_epoch_with_attestations(test_state, False, True)
|
prev_state, new_blocks, state = next_epoch_with_attestations(state, False, True)
|
||||||
# finalized by rule 2
|
# finalized by rule 2
|
||||||
check_finality(test_state, prev_state, True, False, True)
|
check_finality(state, prev_state, True, False, True)
|
||||||
assert test_state.finalized_epoch == prev_state.previous_justified_epoch
|
assert state.finalized_epoch == prev_state.previous_justified_epoch
|
||||||
assert test_state.finalized_root == prev_state.previous_justified_root
|
assert state.finalized_root == prev_state.previous_justified_root
|
||||||
|
|
||||||
blocks += new_blocks
|
blocks += new_blocks
|
||||||
|
|
||||||
return pre_state, blocks, test_state
|
yield 'blocks', [blocks], [spec.BeaconBlock]
|
||||||
|
yield 'post', state
|
||||||
|
|
||||||
|
|
||||||
|
@spec_state_test
|
||||||
def test_finality_rule_3(state):
|
def test_finality_rule_3(state):
|
||||||
"""
|
"""
|
||||||
Test scenario described here
|
Test scenario described here
|
||||||
https://github.com/ethereum/eth2.0-specs/issues/611#issuecomment-463612892
|
https://github.com/ethereum/eth2.0-specs/issues/611#issuecomment-463612892
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# get past first two epochs that finality does not run on
|
# get past first two epochs that finality does not run on
|
||||||
next_epoch(state)
|
next_epoch(state)
|
||||||
next_epoch(state)
|
next_epoch(state)
|
||||||
|
|
||||||
pre_state = deepcopy(state)
|
yield 'pre', state
|
||||||
test_state = deepcopy(state)
|
|
||||||
|
|
||||||
blocks = []
|
blocks = []
|
||||||
prev_state, new_blocks, test_state = next_epoch_with_attestations(test_state, True, False)
|
prev_state, new_blocks, state = next_epoch_with_attestations(state, True, False)
|
||||||
blocks += new_blocks
|
blocks += new_blocks
|
||||||
check_finality(test_state, prev_state, True, False, False)
|
check_finality(state, prev_state, True, False, False)
|
||||||
|
|
||||||
# In epoch N, JE is set to N, prev JE is set to N-1
|
# In epoch N, JE is set to N, prev JE is set to N-1
|
||||||
prev_state, new_blocks, test_state = next_epoch_with_attestations(test_state, True, False)
|
prev_state, new_blocks, state = next_epoch_with_attestations(state, True, False)
|
||||||
blocks += new_blocks
|
blocks += new_blocks
|
||||||
check_finality(test_state, prev_state, True, True, True)
|
check_finality(state, prev_state, True, True, True)
|
||||||
|
|
||||||
# In epoch N+1, JE is N, prev JE is N-1, and not enough messages get in to do anything
|
# In epoch N+1, JE is N, prev JE is N-1, and not enough messages get in to do anything
|
||||||
prev_state, new_blocks, test_state = next_epoch_with_attestations(test_state, False, False)
|
prev_state, new_blocks, state = next_epoch_with_attestations(state, False, False)
|
||||||
blocks += new_blocks
|
blocks += new_blocks
|
||||||
check_finality(test_state, prev_state, False, True, False)
|
check_finality(state, prev_state, False, True, False)
|
||||||
|
|
||||||
# In epoch N+2, JE is N, prev JE is N, and enough messages from the previous epoch get in to justify N+1.
|
# 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
|
# N+1 now becomes the JE. Not enough messages from epoch N+2 itself get in to justify N+2
|
||||||
prev_state, new_blocks, test_state = next_epoch_with_attestations(test_state, False, True)
|
prev_state, new_blocks, state = next_epoch_with_attestations(state, False, True)
|
||||||
blocks += new_blocks
|
blocks += new_blocks
|
||||||
# rule 2
|
# rule 2
|
||||||
check_finality(test_state, prev_state, True, False, True)
|
check_finality(state, prev_state, True, False, True)
|
||||||
|
|
||||||
# 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.
|
# 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.
|
||||||
prev_state, new_blocks, test_state = next_epoch_with_attestations(test_state, True, True)
|
prev_state, new_blocks, state = next_epoch_with_attestations(state, True, True)
|
||||||
blocks += new_blocks
|
blocks += new_blocks
|
||||||
# rule 3
|
# rule 3
|
||||||
check_finality(test_state, prev_state, True, True, True)
|
check_finality(state, prev_state, True, True, True)
|
||||||
assert test_state.finalized_epoch == prev_state.current_justified_epoch
|
assert state.finalized_epoch == prev_state.current_justified_epoch
|
||||||
assert test_state.finalized_root == prev_state.current_justified_root
|
assert state.finalized_root == prev_state.current_justified_root
|
||||||
|
|
||||||
return pre_state, blocks, test_state
|
yield 'blocks', [blocks], [spec.BeaconBlock]
|
||||||
|
yield 'post', state
|
||||||
|
@ -5,11 +5,7 @@ import eth2spec.phase0.spec as spec
|
|||||||
|
|
||||||
from eth2spec.utils.minimal_ssz import signing_root
|
from eth2spec.utils.minimal_ssz import signing_root
|
||||||
from eth2spec.phase0.spec import (
|
from eth2spec.phase0.spec import (
|
||||||
# constants
|
|
||||||
ZERO_HASH,
|
|
||||||
# SSZ
|
# SSZ
|
||||||
Deposit,
|
|
||||||
Transfer,
|
|
||||||
VoluntaryExit,
|
VoluntaryExit,
|
||||||
# functions
|
# functions
|
||||||
get_active_validator_indices,
|
get_active_validator_indices,
|
||||||
@ -19,20 +15,12 @@ from eth2spec.phase0.spec import (
|
|||||||
get_domain,
|
get_domain,
|
||||||
advance_slot,
|
advance_slot,
|
||||||
cache_state,
|
cache_state,
|
||||||
verify_merkle_branch,
|
|
||||||
hash,
|
|
||||||
)
|
)
|
||||||
from eth2spec.phase0.state_transition import (
|
from eth2spec.phase0.state_transition import (
|
||||||
state_transition,
|
state_transition,
|
||||||
)
|
)
|
||||||
from eth2spec.utils.merkle_minimal import (
|
|
||||||
calc_merkle_tree_from_leaves,
|
|
||||||
get_merkle_proof,
|
|
||||||
get_merkle_root,
|
|
||||||
)
|
|
||||||
from .helpers import (
|
from .helpers import (
|
||||||
get_balance,
|
get_balance,
|
||||||
build_deposit_data,
|
|
||||||
build_empty_block_for_next_slot,
|
build_empty_block_for_next_slot,
|
||||||
get_state_root,
|
get_state_root,
|
||||||
get_valid_attestation,
|
get_valid_attestation,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user