eth2.0-specs/test_libs/pyspec/eth2spec/test/test_fork_choice.py

68 lines
2.1 KiB
Python
Raw Normal View History

2019-06-16 19:53:42 +00:00
from typing import List
from eth2spec.utils.ssz.ssz_impl import signing_root, hash_tree_root
2019-06-15 23:13:56 +00:00
from eth2spec.test.context import with_all_phases, spec_state_test
from eth2spec.test.helpers.block import build_empty_block_for_next_slot
from eth2spec.test.helpers.attestations import get_valid_attestation
from eth2spec.test.helpers.state import next_slot
@with_all_phases
@spec_state_test
def test_basic(spec, state):
2019-06-16 19:53:42 +00:00
state.latest_block_header = spec.BeaconBlockHeader(body_root=hash_tree_root(spec.BeaconBlockBody()))
2019-06-16 13:52:52 +00:00
yield 'pre', state
2019-06-15 23:13:56 +00:00
# Initialization
2019-06-16 13:52:52 +00:00
store = spec.get_genesis_store(state)
blocks = []
time = 100
spec.on_tick(store, time)
assert store.time == time
2019-06-15 23:13:56 +00:00
# On receiving a block of `GENESIS_SLOT + 1` slot
2019-06-16 13:52:52 +00:00
block = build_empty_block_for_next_slot(spec, state)
blocks.append(block)
spec.on_block(store, block)
assert store.blocks[signing_root(block)] == block
2019-06-15 23:13:56 +00:00
# On receiving a block of next epoch
2019-06-16 13:52:52 +00:00
store.time = time + spec.SECONDS_PER_SLOT * spec.SLOTS_PER_EPOCH
block = build_empty_block_for_next_slot(spec, state)
block.slot += spec.SLOTS_PER_EPOCH
blocks.append(block)
2019-06-15 23:13:56 +00:00
2019-06-16 13:52:52 +00:00
spec.on_block(store, block)
assert store.blocks[signing_root(block)] == block
yield 'blocks', blocks, List[spec.BeaconBlock]
2019-06-15 23:13:56 +00:00
# TODO: add tests for justified_root and finalized_root
2019-06-16 13:52:52 +00:00
yield 'post', state
2019-06-15 23:13:56 +00:00
@with_all_phases
@spec_state_test
def test_on_attestation(spec, state):
2019-06-16 13:52:52 +00:00
yield 'pre', state
store = spec.get_genesis_store(state)
time = 100
spec.on_tick(store, time)
next_slot(spec, state)
attestation = get_valid_attestation(spec, state, slot=1)
yield 'attestation', attestation
indexed_attestation = spec.convert_to_indexed(state, attestation)
spec.on_attestation(store, attestation)
assert (
store.latest_targets[indexed_attestation.custody_bit_0_indices[0]] ==
spec.Target(
epoch = attestation.data.target_epoch,
2019-06-16 16:17:31 +00:00
root = attestation.data.target_root,
2019-06-16 13:52:52 +00:00
)
)
yield 'post', state