-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
107 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
test_libs/pyspec/eth2spec/test/fork_choice/test_get_head.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
from eth2spec.test.context import with_all_phases, with_state, bls_switch | ||
from eth2spec.test.helpers.block import build_empty_block_for_next_slot | ||
from eth2spec.test.helpers.state import state_transition_and_sign_block | ||
|
||
|
||
def add_block_to_store(spec, store, block): | ||
pre_state = store.block_states[block.parent_root] | ||
block_time = pre_state.genesis_time + block.slot * spec.SECONDS_PER_SLOT | ||
|
||
if store.time < block_time: | ||
spec.on_tick(store, block_time) | ||
|
||
spec.on_block(store, block) | ||
|
||
|
||
@with_all_phases | ||
@with_state | ||
@bls_switch | ||
def test_genesis(spec, state): | ||
# Initialization | ||
store = spec.get_genesis_store(state) | ||
genesis_block = spec.BeaconBlock(state_root=state.hash_tree_root()) | ||
assert spec.get_head(store) == spec.signing_root(genesis_block) | ||
|
||
|
||
@with_all_phases | ||
@with_state | ||
@bls_switch | ||
def test_chain_no_attestations(spec, state): | ||
# Initialization | ||
store = spec.get_genesis_store(state) | ||
genesis_block = spec.BeaconBlock(state_root=state.hash_tree_root()) | ||
assert spec.get_head(store) == spec.signing_root(genesis_block) | ||
|
||
# On receiving a block of `GENESIS_SLOT + 1` slot | ||
block_1 = build_empty_block_for_next_slot(spec, state) | ||
state_transition_and_sign_block(spec, state, block_1) | ||
add_block_to_store(spec, store, block_1) | ||
|
||
# On receiving a block of next epoch | ||
block_2 = build_empty_block_for_next_slot(spec, state) | ||
state_transition_and_sign_block(spec, state, block_2) | ||
add_block_to_store(spec, store, block_2) | ||
|
||
assert spec.get_head(store) == spec.signing_root(block_2) | ||
|
||
|
||
@with_all_phases | ||
@with_state | ||
@bls_switch | ||
def test_split_tie_breaker_no_attestations(spec, state): | ||
genesis_state = state.copy() | ||
|
||
# Initialization | ||
store = spec.get_genesis_store(state) | ||
genesis_block = spec.BeaconBlock(state_root=state.hash_tree_root()) | ||
assert spec.get_head(store) == spec.signing_root(genesis_block) | ||
|
||
# block at slot 1 | ||
block_1_state = genesis_state.copy() | ||
block_1 = build_empty_block_for_next_slot(spec, block_1_state) | ||
state_transition_and_sign_block(spec, block_1_state, block_1) | ||
add_block_to_store(spec, store, block_1) | ||
|
||
# additional block at slot 1 | ||
block_2_state = genesis_state.copy() | ||
block_2 = build_empty_block_for_next_slot(spec, block_2_state) | ||
block_2.body.graffiti = b'\x42' * 32 | ||
state_transition_and_sign_block(spec, block_2_state, block_2) | ||
add_block_to_store(spec, store, block_2) | ||
|
||
highest_root = max(spec.signing_root(block_1), spec.signing_root(block_2)) | ||
|
||
assert spec.get_head(store) == highest_root | ||
|
||
|
||
@with_all_phases | ||
@with_state | ||
@bls_switch | ||
def test_longer_branch_no_attestations(spec, state): | ||
genesis_state = state.copy() | ||
|
||
# Initialization | ||
store = spec.get_genesis_store(state) | ||
genesis_block = spec.BeaconBlock(state_root=state.hash_tree_root()) | ||
assert spec.get_head(store) == spec.signing_root(genesis_block) | ||
|
||
# build longer tree | ||
long_state = genesis_state.copy() | ||
for i in range(3): | ||
long_block = build_empty_block_for_next_slot(spec, long_state) | ||
state_transition_and_sign_block(spec, long_state, long_block) | ||
add_block_to_store(spec, store, long_block) | ||
|
||
# build short tree | ||
short_state = genesis_state.copy() | ||
short_block = build_empty_block_for_next_slot(spec, short_state) | ||
short_block.body.graffiti = b'\x42' * 32 | ||
state_transition_and_sign_block(spec, short_state, short_block) | ||
add_block_to_store(spec, store, short_block) | ||
|
||
assert spec.get_head(store) == spec.signing_root(long_block) |