Skip to content

Commit

Permalink
verify intermidiate block state in blockchain test
Browse files Browse the repository at this point in the history
  • Loading branch information
winsvega committed Jan 16, 2025
1 parent 06abe36 commit 5e87b95
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/ethereum_test_specs/blockchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@ class Block(Header):
"""
Custom list of requests to embed in this block.
"""
expected_post_state: Alloc | None = None
"""
Post state for verification after block execution in BlockchainTest
"""

def set_environment(self, env: Environment) -> Environment:
"""
Expand Down Expand Up @@ -491,10 +495,13 @@ def network_info(self, fork: Fork, eips: Optional[List[int]] = None):
else fork.blockchain_test_network_name()
)

def verify_post_state(self, t8n, alloc: Alloc):
def verify_post_state(self, t8n, t8n_state: Alloc, expected_state: Alloc | None = None):
"""Verify post alloc after all block/s or payload/s are generated."""
try:
self.post.verify_post_alloc(alloc)
if expected_state:
expected_state.verify_post_alloc(t8n_state)
else:
self.post.verify_post_alloc(t8n_state)
except Exception as e:
print_traces(t8n.get_traces())
raise e
Expand Down Expand Up @@ -570,7 +577,12 @@ def make_fixture(
),
)

self.verify_post_state(t8n, alloc)
if block.expected_post_state:
self.verify_post_state(
t8n, t8n_state=alloc, expected_state=block.expected_post_state
)

self.verify_post_state(t8n, t8n_state=alloc)
return Fixture(
fork=self.network_info(fork, eips),
genesis=genesis.header,
Expand Down Expand Up @@ -628,7 +640,7 @@ def make_hive_fixture(
), "A hive fixture was requested but no forkchoice update is defined. The framework should"
" never try to execute this test case."

self.verify_post_state(t8n, alloc)
self.verify_post_state(t8n, t8n_state=alloc)

sync_payload: Optional[FixtureEngineNewPayload] = None
if self.verify_sync:
Expand Down
1 change: 1 addition & 0 deletions tests/frontier/examples/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Test examples, patterns, templates to use in .py tests."""
50 changes: 50 additions & 0 deletions tests/frontier/examples/test_block_intermidiate_state.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"""Test the SELFDESTRUCT opcode."""

import pytest

from ethereum_test_tools import (
Account,
Alloc,
Block,
BlockchainTestFiller,
Environment,
Transaction,
)


@pytest.mark.valid_from("Frontier")
@pytest.mark.valid_until("Homestead")
def test_block_intermidiate_state(blockchain_test: BlockchainTestFiller, pre: Alloc):
"""Verify intermidiate block states."""
env = Environment()
sender = pre.fund_eoa()

tx = Transaction(gas_limit=100_000, to=None, data=b"", sender=sender)
tx_2 = Transaction(gas_limit=100_000, to=None, data=b"", sender=sender)

block_1 = Block(
txs=[tx],
expected_post_state={
sender: Account(
nonce=1,
),
},
)

block_2 = Block(txs=[])

block_3 = Block(
txs=[tx_2],
expected_post_state={
sender: Account(
nonce=2,
),
},
)

blockchain_test(
genesis_environment=env,
pre=pre,
post=block_3.expected_post_state,
blocks=[block_1, block_2, block_3],
)
1 change: 1 addition & 0 deletions whitelist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ isort
isort's
ispkg
itemName
intermidiate
javascripts
jimporter
joinpath
Expand Down

0 comments on commit 5e87b95

Please sign in to comment.