Skip to content

Commit cb99c5f

Browse files
authored
Ensure EL block hash is updated when beacon parent root is overridden (#3881)
1 parent c8ba4d2 commit cb99c5f

File tree

4 files changed

+47
-8
lines changed

4 files changed

+47
-8
lines changed

tests/core/pyspec/eth2spec/test/helpers/execution_payload.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,17 +191,19 @@ def get_consolidation_request_rlp_bytes(consolidation_request):
191191
return b"\x02" + encode(values, sedes)
192192

193193

194-
def compute_el_block_hash(spec, payload, pre_state):
194+
def compute_el_block_hash_with_parent_root(spec, payload, parent_beacon_block_root):
195+
if payload == spec.ExecutionPayload():
196+
return spec.Hash32()
197+
195198
transactions_trie_root = compute_trie_root_from_indexed_data(payload.transactions)
196199

197200
withdrawals_trie_root = None
198-
parent_beacon_block_root = None
199201

200202
if is_post_capella(spec):
201203
withdrawals_encoded = [get_withdrawal_rlp(withdrawal) for withdrawal in payload.withdrawals]
202204
withdrawals_trie_root = compute_trie_root_from_indexed_data(withdrawals_encoded)
203-
if is_post_deneb(spec):
204-
parent_beacon_block_root = pre_state.latest_block_header.hash_tree_root()
205+
if not is_post_deneb(spec):
206+
parent_beacon_block_root = None
205207

206208
payload_header = get_execution_payload_header(spec, payload)
207209

@@ -214,6 +216,24 @@ def compute_el_block_hash(spec, payload, pre_state):
214216
)
215217

216218

219+
def compute_el_block_hash(spec, payload, pre_state):
220+
parent_beacon_block_root = None
221+
222+
if is_post_deneb(spec):
223+
previous_block_header = pre_state.latest_block_header.copy()
224+
if previous_block_header.state_root == spec.Root():
225+
previous_block_header.state_root = pre_state.hash_tree_root()
226+
parent_beacon_block_root = previous_block_header.hash_tree_root()
227+
228+
return compute_el_block_hash_with_parent_root(
229+
spec, payload, parent_beacon_block_root)
230+
231+
232+
def compute_el_block_hash_for_block(spec, block):
233+
return compute_el_block_hash_with_parent_root(
234+
spec, block.body.execution_payload, block.parent_root)
235+
236+
217237
def build_empty_post_eip7732_execution_payload_header(spec, state):
218238
if not is_post_eip7732(spec):
219239
return

tests/core/pyspec/eth2spec/test/phase0/block_processing/test_process_block_header.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from eth2spec.test.context import spec_state_test, expect_assertion_error, with_all_phases
44
from eth2spec.test.helpers.block import build_empty_block_for_next_slot
5+
from eth2spec.test.helpers.execution_payload import compute_el_block_hash_for_block
6+
from eth2spec.test.helpers.forks import is_post_bellatrix
57
from eth2spec.test.helpers.state import next_slot
68

79

@@ -65,6 +67,8 @@ def test_invalid_proposer_index(spec, state):
6567
def test_invalid_parent_root(spec, state):
6668
block = build_empty_block_for_next_slot(spec, state)
6769
block.parent_root = b'\12' * 32 # invalid prev root
70+
if is_post_bellatrix(spec):
71+
block.body.execution_payload.block_hash = compute_el_block_hash_for_block(spec, block)
6872

6973
yield from run_block_header_processing(spec, state, block, valid=False)
7074

@@ -81,6 +85,8 @@ def test_invalid_multiple_blocks_single_slot(spec, state):
8185

8286
child_block = block.copy()
8387
child_block.parent_root = block.hash_tree_root()
88+
if is_post_bellatrix(spec):
89+
child_block.body.execution_payload.block_hash = compute_el_block_hash_for_block(spec, child_block)
8490

8591
yield from run_block_header_processing(spec, state, child_block, prepare_state=False, valid=False)
8692

tests/core/pyspec/eth2spec/test/phase0/fork_choice/test_on_block.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
transition_unsigned_block,
1919
sign_block,
2020
)
21+
from eth2spec.test.helpers.execution_payload import (
22+
compute_el_block_hash_for_block,
23+
)
2124
from eth2spec.test.helpers.fork_choice import (
2225
get_genesis_forkchoice_store_and_block,
2326
on_tick_and_append_step,
@@ -28,6 +31,9 @@
2831
is_ready_to_justify,
2932
find_next_justifying_slot,
3033
)
34+
from eth2spec.test.helpers.forks import (
35+
is_post_bellatrix,
36+
)
3137
from eth2spec.test.helpers.state import (
3238
next_epoch,
3339
next_slots,
@@ -152,6 +158,8 @@ def test_on_block_bad_parent_root(spec, state):
152158
block.state_root = state.hash_tree_root()
153159

154160
block.parent_root = b'\x45' * 32
161+
if is_post_bellatrix(spec):
162+
block.body.execution_payload.block_hash = compute_el_block_hash_for_block(spec, block)
155163

156164
signed_block = sign_block(spec, state, block)
157165

tests/core/pyspec/eth2spec/test/phase0/sanity/test_blocks.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020
from eth2spec.test.helpers.proposer_slashings import get_valid_proposer_slashing, check_proposer_slashing_effect
2121
from eth2spec.test.helpers.attestations import get_valid_attestation
2222
from eth2spec.test.helpers.deposits import prepare_state_and_deposit
23-
from eth2spec.test.helpers.execution_payload import build_empty_execution_payload
23+
from eth2spec.test.helpers.execution_payload import (
24+
build_empty_execution_payload,
25+
compute_el_block_hash_for_block,
26+
)
2427
from eth2spec.test.helpers.voluntary_exits import prepare_signed_exits
2528
from eth2spec.test.helpers.multi_operations import (
2629
run_slash_and_exit,
@@ -158,7 +161,7 @@ def process_and_sign_block_without_header_validations(spec, state, block):
158161
if is_post_altair(spec):
159162
spec.process_sync_aggregate(state, block.body.sync_aggregate)
160163

161-
# Insert post-state rot
164+
# Insert post-state root
162165
block.state_root = state.hash_tree_root()
163166

164167
# Sign block
@@ -197,11 +200,13 @@ def test_invalid_parent_from_same_slot(spec, state):
197200
signed_parent_block = state_transition_and_sign_block(spec, state, parent_block)
198201

199202
child_block = parent_block.copy()
200-
child_block.parent_root = state.latest_block_header.hash_tree_root()
201-
202203
if is_post_bellatrix(spec):
203204
child_block.body.execution_payload = build_empty_execution_payload(spec, state)
204205

206+
child_block.parent_root = state.latest_block_header.hash_tree_root()
207+
if is_post_bellatrix(spec):
208+
child_block.body.execution_payload.block_hash = compute_el_block_hash_for_block(spec, child_block)
209+
205210
# Show that normal path through transition fails
206211
failed_state = state.copy()
207212
expect_assertion_error(

0 commit comments

Comments
 (0)