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

61 lines
1.8 KiB
Python
Raw Normal View History

2019-06-16 19:53:42 +00:00
from eth2spec.utils.ssz.ssz_impl import signing_root, hash_tree_root
2019-06-15 23:13:56 +00:00
2019-06-21 11:00:42 +00:00
from eth2spec.test.context import with_all_phases, with_state, bls_switch
2019-06-15 23:13:56 +00:00
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
2019-06-21 11:00:42 +00:00
@with_state
@bls_switch
2019-06-15 23:13:56 +00:00
def test_basic(spec, state):
state.latest_block_header = spec.BeaconBlockHeader(body_root=hash_tree_root(spec.BeaconBlockBody()))
2019-06-16 13:52:52 +00:00
2019-06-15 23:13:56 +00:00
# Initialization
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
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
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
spec.on_block(store, block)
assert store.blocks[signing_root(block)] == block
2019-06-15 23:13:56 +00:00
# TODO: add tests for justified_root and finalized_root
2019-06-15 23:13:56 +00:00
@with_all_phases
2019-06-21 11:00:42 +00:00
@with_state
@bls_switch
2019-06-15 23:13:56 +00:00
def test_on_attestation(spec, 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)
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,
root=attestation.data.target_root,
)
)