From 1904b47e3e4e74d460eaa355f9d9b9834143f728 Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Fri, 4 Aug 2023 21:41:57 +0800 Subject: [PATCH] Add `test_proposer_boost_is_first_block` test case --- .../test/phase0/fork_choice/test_on_block.py | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/tests/core/pyspec/eth2spec/test/phase0/fork_choice/test_on_block.py b/tests/core/pyspec/eth2spec/test/phase0/fork_choice/test_on_block.py index 840413a36..cd4135049 100644 --- a/tests/core/pyspec/eth2spec/test/phase0/fork_choice/test_on_block.py +++ b/tests/core/pyspec/eth2spec/test/phase0/fork_choice/test_on_block.py @@ -539,6 +539,56 @@ def test_proposer_boost_root_same_slot_untimely_block(spec, state): yield 'steps', test_steps +@with_altair_and_later +@spec_state_test +def test_proposer_boost_is_first_block(spec, state): + test_steps = [] + genesis_state = state.copy() + + # Initialization + store, anchor_block = get_genesis_forkchoice_store_and_block(spec, state) + yield 'anchor_state', state + yield 'anchor_block', anchor_block + + # Build block that serves as head ONLY on timely arrival, and ONLY in that slot + state = genesis_state.copy() + next_slots(spec, state, 3) + pre_state = state.copy() + block_a = build_empty_block_for_next_slot(spec, state) + signed_block_a = state_transition_and_sign_block(spec, state, block_a) + + # Process block on timely arrival just before end of boost interval + time = (store.genesis_time + block_a.slot * spec.config.SECONDS_PER_SLOT + + spec.config.SECONDS_PER_SLOT // spec.INTERVALS_PER_SLOT - 1) + on_tick_and_append_step(spec, store, time, test_steps) + yield from add_block(spec, store, signed_block_a, test_steps) + # `proposer_boost_root` is now `block_a` + assert store.proposer_boost_root == spec.hash_tree_root(block_a) + assert spec.get_weight(store, spec.hash_tree_root(block_a)) > 0 + test_steps.append({ + 'checks': { + 'proposer_boost_root': encode_hex(store.proposer_boost_root), + } + }) + + # make a different block at the same slot + state = pre_state.copy() + block_b = block_a.copy() + block_b.body.graffiti = b'\x34' * 32 + signed_block_b = state_transition_and_sign_block(spec, state, block_b) + yield from add_block(spec, store, signed_block_b, test_steps) + # `proposer_boost_root` is still `block_a` + assert store.proposer_boost_root == spec.hash_tree_root(block_a) + assert spec.get_weight(store, spec.hash_tree_root(block_b)) == 0 + test_steps.append({ + 'checks': { + 'proposer_boost_root': encode_hex(store.proposer_boost_root), + } + }) + + yield 'steps', test_steps + + @with_altair_and_later @spec_state_test @with_presets([MINIMAL], reason="too slow")