Skip to content

Commit

Permalink
src/tools: Refactor & clean up of blockchain_test.
Browse files Browse the repository at this point in the history
  • Loading branch information
spencer-tb committed Oct 4, 2023
1 parent 574ccab commit 4fbafe2
Showing 1 changed file with 39 additions and 59 deletions.
98 changes: 39 additions & 59 deletions src/ethereum_test_tools/spec/blockchain_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
Blockchain test filler.
Ethereum blockchain test spec definition and filler.
"""

from dataclasses import dataclass, field
Expand Down Expand Up @@ -48,7 +48,6 @@ class BlockchainTest(BaseTest):
blocks: List[Block]
genesis_environment: Environment = field(default_factory=Environment)
tag: str = ""

chain_id: int = 1

@classmethod
Expand Down Expand Up @@ -121,7 +120,7 @@ def make_genesis(

return Alloc(new_alloc), genesis_rlp, genesis

def get_block_data(
def generate_block_data(
self,
t8n: TransitionTool,
fork: Fork,
Expand All @@ -133,6 +132,13 @@ def get_block_data(
"""
TODO
"""
if block.rlp and block.exception is not None:
raise Exception(
"test correctness: post-state cannot be verified if the "
+ "block's rlp is supplied and the block is not supposed "
+ "to produce an exception"
)

env = block.set_environment(previous_env)
env = env.set_fork_requirements(fork)

Expand All @@ -148,6 +154,7 @@ def get_block_data(
eips=eips,
debug_output_path=self.get_next_transition_tool_output_path(),
)

try:
rejected_txs = verify_transactions(txs, result)
verify_result(result, env)
Expand All @@ -167,6 +174,7 @@ def get_block_data(
+ "was indeed expected to fail and add the proper "
+ "`block.exception`"
)

env.extra_data = block.extra_data
header = FixtureHeader.collect(
fork=fork,
Expand All @@ -191,14 +199,30 @@ def get_block_data(

return header, rlp, txs, next_alloc, env

def network_info(self, fork, eips=None):
"""
Returns fixture network information for the fork & EIP/s.
"""
return "+".join([fork.name()] + [str(eip) for eip in eips]) if eips else fork.name()

def verify_post_state(self, t8n, alloc):
"""
Verifies the post alloc after all block/s or payload/s are generated.
"""
try:
verify_post_alloc(self.post, alloc)
except Exception as e:
print_traces(t8n.get_traces())
raise e

def make_fixture(
self,
t8n: TransitionTool,
fork: Fork,
eips: Optional[List[int]] = None,
) -> Fixture:
"""
TODO
Create a fixture from the blockchain test definition.
"""
fixture_blocks: List[FixtureBlock | InvalidFixtureBlock] = []

Expand All @@ -207,20 +231,10 @@ def make_fixture(
alloc = to_json(pre)
env = Environment.from_parent_header(genesis)
head = genesis.hash if genesis.hash is not None else Hash(0)

for block in self.blocks:
if block.rlp and block.exception is not None:
raise Exception(
"test correctness: post-state cannot be verified if the "
+ "block's rlp is supplied and the block is not supposed "
+ "to produce an exception"
)
header, rlp, txs, next_alloc, next_env = self.get_block_data(
t8n=t8n,
fork=fork,
block=block,
previous_env=env,
previous_alloc=alloc,
eips=eips,
header, rlp, txs, next_alloc, next_env = self.generate_block_data(
t8n=t8n, fork=fork, block=block, previous_env=env, previous_alloc=alloc, eips=eips
)
if block.rlp is None:
# This is the most common case, the RLP needs to be constructed
Expand Down Expand Up @@ -262,20 +276,9 @@ def make_fixture(
),
)

try:
verify_post_alloc(self.post, alloc)
except Exception as e:
print_traces(t8n.get_traces())
raise e

network_info = (
"+".join([fork.name()] + [str(eip) for eip in eips])
if eips is not None
else fork.name()
)

self.verify_post_state(t8n, alloc)
return Fixture(
fork=network_info,
fork=self.network_info(fork, eips),
genesis=genesis,
genesis_rlp=genesis_rlp,
blocks=fixture_blocks,
Expand All @@ -291,28 +294,17 @@ def make_hive_fixture(
eips: Optional[List[int]] = None,
) -> HiveFixture:
"""
TODO
Create a hive fixture from the blocktest definition.
"""
pre, _, genesis = self.make_genesis(t8n, fork)
fixture_payloads: List[Optional[FixtureEngineNewPayload]] = []

pre, _, genesis = self.make_genesis(t8n, fork)
alloc = to_json(pre)
env = Environment.from_parent_header(genesis)

for block in self.blocks:
if block.rlp and block.exception is not None:
raise Exception(
"test correctness: post-state cannot be verified if the "
+ "block's rlp is supplied and the block is not supposed "
+ "to produce an exception"
)
header, _, txs, next_alloc, env = self.get_block_data(
t8n=t8n,
fork=fork,
block=block,
previous_env=env,
previous_alloc=alloc,
eips=eips,
header, _, txs, next_alloc, env = self.generate_block_data(
t8n=t8n, fork=fork, block=block, previous_env=env, previous_alloc=alloc, eips=eips
)
if block.rlp is None:
fixture_payloads.append(
Expand All @@ -328,23 +320,11 @@ def make_hive_fixture(
if block.exception is None:
alloc = next_alloc
env = env.apply_new_parent(header)

fcu_version = fork.engine_forkchoice_updated_version(header.number, header.timestamp)

network_info = (
"+".join([fork.name()] + [str(eip) for eip in eips])
if eips is not None
else fork.name()
)

try:
verify_post_alloc(self.post, alloc)
except Exception as e:
print_traces(t8n.get_traces())
raise e

self.verify_post_state(t8n, alloc)
return HiveFixture(
fork=network_info,
fork=self.network_info(fork, eips),
genesis=genesis,
payloads=fixture_payloads,
fcu_version=fcu_version,
Expand Down

0 comments on commit 4fbafe2

Please sign in to comment.