1313from eth2spec .test .helpers .block import (
1414 build_empty_block_for_next_slot ,
1515)
16+ from eth2spec .test .helpers .forks import is_post_eip7732
1617from eth2spec .test .helpers .fork_choice import (
1718 check_head_against_root ,
1819 get_genesis_forkchoice_store_and_block ,
20+ get_store_full_state ,
1921 on_tick_and_append_step ,
2022 payload_state_transition ,
23+ payload_state_transition_no_store ,
2124 tick_and_add_block ,
2225 apply_next_epoch_with_attestations ,
2326 find_next_justifying_slot ,
@@ -81,7 +84,6 @@ def test_withholding_attack(spec, state):
8184 # Create two blocks in the honest chain with full attestations, and add to the store
8285 honest_state = state .copy ()
8386 for _ in range (2 ):
84- print ("Trying slot" , honest_state .slot )
8587 signed_block = state_transition_with_full_block (spec , honest_state , True , False )
8688 yield from tick_and_add_block (spec , store , signed_block , test_steps )
8789 honest_state = payload_state_transition (spec , store , signed_block .message ).copy ()
@@ -159,8 +161,8 @@ def test_withholding_attack_unviable_honest_chain(spec, state):
159161 for signed_block in signed_blocks [:- 1 ]:
160162 yield from tick_and_add_block (spec , store , signed_block , test_steps )
161163 check_head_against_root (spec , store , signed_block .message .hash_tree_root ())
162- check_head_against_root (spec , store , signed_blocks [ - 2 ] .message . hash_tree_root () )
163- state = store . block_states [ spec . get_head ( store )] .copy ()
164+ payload_state_transition (spec , store , signed_block .message )
165+ state = get_store_full_state ( spec , store , signed_block . message . hash_tree_root ()) .copy ()
164166 assert spec .compute_epoch_at_slot (state .slot ) == 5
165167 assert spec .compute_epoch_at_slot (spec .get_current_slot (store )) == 5
166168 assert state .current_justified_checkpoint .epoch == store .justified_checkpoint .epoch == 3
@@ -169,11 +171,14 @@ def test_withholding_attack_unviable_honest_chain(spec, state):
169171 next_epoch (spec , state )
170172 assert spec .compute_epoch_at_slot (state .slot ) == 6
171173 assert state .current_justified_checkpoint .epoch == 3
172- # Create two block in the honest chain with full attestations, and add to the store
174+ # Create two blocks in the honest chain with full attestations, and add to the store
173175 for _ in range (2 ):
174176 signed_block = state_transition_with_full_block (spec , state , True , False )
177+ payload_state_transition_no_store (spec , state , signed_block .message )
175178 assert state .current_justified_checkpoint .epoch == 3
176179 yield from tick_and_add_block (spec , store , signed_block , test_steps )
180+ check_head_against_root (spec , store , signed_block .message .hash_tree_root ())
181+ payload_state_transition (spec , store , signed_block .message )
177182 # Create final block in the honest chain that includes the justifying attestations from the attack block
178183 honest_block = build_empty_block_for_next_slot (spec , state )
179184 honest_block .body .attestations = signed_attack_block .message .body .attestations
@@ -182,6 +187,7 @@ def test_withholding_attack_unviable_honest_chain(spec, state):
182187 assert state .current_justified_checkpoint .epoch == 3
183188 # Add the honest block to the store
184189 yield from tick_and_add_block (spec , store , signed_honest_block , test_steps )
190+ payload_state_transition (spec , store , signed_honest_block .message )
185191 current_epoch = spec .compute_epoch_at_slot (spec .get_current_slot (store ))
186192 assert current_epoch == 6
187193 # assert store.voting_source[honest_block_root].epoch == 3
@@ -197,10 +203,16 @@ def test_withholding_attack_unviable_honest_chain(spec, state):
197203 assert state .current_justified_checkpoint .epoch == store .justified_checkpoint .epoch == 3
198204
199205 # Upon revealing the withheld attack block, it should become the head
206+ # Except in EIP-7732 in which it's parent becomes head because of the
207+ # attestations during the attacker's block's committee.
200208 yield from tick_and_add_block (spec , store , signed_attack_block , test_steps )
209+ payload_state_transition (spec , store , signed_attack_block .message )
201210 # The attack block is pulled up and store.justified_checkpoint is updated
202211 assert store .justified_checkpoint .epoch == 5
203- attack_block_root = signed_attack_block .message .hash_tree_root ()
212+ if is_post_eip7732 (spec ):
213+ attack_block_root = signed_attack_block .message .parent_root
214+ else :
215+ attack_block_root = signed_attack_block .message .hash_tree_root ()
204216 check_head_against_root (spec , store , attack_block_root )
205217
206218 # After going to the next epoch, the honest block should become the head
0 commit comments