From 061ecf7d0a870bf5336cffa1a3567054e5978c27 Mon Sep 17 00:00:00 2001 From: Carl Beekhuizen Date: Sat, 15 Jun 2019 19:13:56 -0400 Subject: [PATCH] Adds fork-choice tests --- .../pyspec/eth2spec/test/test_fork_choice.py | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 test_libs/pyspec/eth2spec/test/test_fork_choice.py diff --git a/test_libs/pyspec/eth2spec/test/test_fork_choice.py b/test_libs/pyspec/eth2spec/test/test_fork_choice.py new file mode 100644 index 000000000..46b60b93c --- /dev/null +++ b/test_libs/pyspec/eth2spec/test/test_fork_choice.py @@ -0,0 +1,50 @@ +from eth2spec.utils.ssz.ssz_impl import signing_root + +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): + # Initialization + store = spec.get_genesis_store(state) + time = 100 + spec.on_tick(store, time) + assert store.time == time + + # On receiving a block of `GENESIS_SLOT + 1` slot + block = build_empty_block_for_next_slot(state) + spec.on_block(store, block) + assert store.blocks[signing_root(block)] == block + + # 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(state) + block.slot += spec.SLOTS_PER_EPOCH + + spec.on_block(store, block) + assert store.blocks[signing_root(block)] == block + + # TODO: add tests for justified_root and finalized_root + + +@with_all_phases +@spec_state_test +def test_on_attestation(spec, state): + store = spec.get_genesis_store(state) + time = 100 + spec.on_tick(store, time) + + next_slot(state) + + attestation = get_valid_attestation(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(attestation.data.target_epoch, attestation.data.target_root) + ) \ No newline at end of file