Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Support for BlockchainTests #2

Merged
merged 19 commits into from
Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/ethereum_test/blockchain_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
JSONEncoder,
)

default_base_fee = 1
lightclient marked this conversation as resolved.
Show resolved Hide resolved
"""
Default base_fee used in the genesis and block 1 for the BlockchainTests.
"""


@dataclass(kw_only=True)
class BlockchainTest(BaseTest):
Expand All @@ -43,8 +48,11 @@ def make_genesis(
"""
Create a genesis block from the state test definition.
"""
if is_london(fork) and self.genesis_environment.base_fee is None:
self.genesis_environment.base_fee = 7
base_fee = self.genesis_environment.base_fee
if is_london(fork) and base_fee is None:
base_fee = default_base_fee
elif not is_london(fork) and base_fee is not None:
base_fee = None
genesis = FixtureHeader(
parent_hash="0x0000000000000000000000000000000000000000000000000000000000000000", # noqa: E501
ommers_hash="0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347", # noqa: E501
Expand All @@ -65,7 +73,7 @@ def make_genesis(
extra_data="0x00",
mix_digest="0x0000000000000000000000000000000000000000000000000000000000000000", # noqa: E501
nonce="0x0000000000000000",
base_fee=self.genesis_environment.base_fee,
base_fee=base_fee,
)

(_, h) = b11r.build(genesis.to_geth_dict(), "", [])
Expand Down
49 changes: 30 additions & 19 deletions src/ethereum_test/state_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@
from evm_block_builder import BlockBuilder
from evm_transition_tool import TransitionTool

from .base_test import (
BaseTest,
remove_transactions_from_rlp,
verify_post_alloc,
verify_transactions,
)
from .base_test import BaseTest, verify_post_alloc, verify_transactions
from .common import EmptyTrieRoot
from .fork import is_london
from .types import (
Expand All @@ -27,6 +22,11 @@
Transaction,
)

default_base_fee = 1
"""
Default base_fee used in the genesis and block 1 for the StateTests.
"""


@dataclass(kw_only=True)
class StateTest(BaseTest):
Expand All @@ -48,6 +48,14 @@ def make_genesis(
"""
Create a genesis block from the state test definition.
"""
base_fee = self.env.base_fee
if is_london(fork) and base_fee is None:
# If there is no base fee specified in the environment, we use a
# default.
base_fee = default_base_fee
elif not is_london(fork) and base_fee is not None:
base_fee = None

genesis = FixtureHeader(
parent_hash="0x0000000000000000000000000000000000000000000000000000000000000000", # noqa: E501
ommers_hash="0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347", # noqa: E501
Expand All @@ -63,12 +71,16 @@ def make_genesis(
difficulty=0x20000,
number=self.env.number - 1,
gas_limit=self.env.gas_limit,
gas_used=0,
# We need the base fee to remain unchanged from the genesis
# to block 1.
# To do that we set the gas used to exactly half of the limit
# so the base fee is unchanged.
gas_used=self.env.gas_limit // 2,
timestamp=0,
extra_data="0x00",
mix_digest="0x0000000000000000000000000000000000000000000000000000000000000000", # noqa: E501
nonce="0x0000000000000000",
base_fee=self.env.base_fee,
base_fee=base_fee,
)

(_, h) = b11r.build(genesis.to_geth_dict(), "", [])
Expand All @@ -92,17 +104,16 @@ def make_blocks(
"""
env = self.env.apply_new_parent(genesis)
if env.base_fee is None and is_london(fork):
env.base_fee = 7
env.base_fee = default_base_fee
pre = json.loads(json.dumps(self.pre, cls=JSONEncoder))
txs = json.loads(json.dumps(self.txs, cls=JSONEncoder))
env = json.loads(json.dumps(env, cls=JSONEncoder))

with tempfile.TemporaryDirectory() as directory:
txsRlp = os.path.join(directory, "txs.rlp")
(alloc, result) = t8n.evaluate(
pre,
txs,
env,
json.loads(json.dumps(env, cls=JSONEncoder)),
fork,
txsPath=txsRlp,
chain_id=chain_id,
Expand All @@ -121,21 +132,21 @@ def make_blocks(

header = result | {
"parentHash": genesis.hash,
"miner": self.env.coinbase,
"miner": env.coinbase,
"transactionsRoot": result.get("txRoot"),
"difficulty": hex(self.env.difficulty)
if self.env.difficulty is not None
"difficulty": hex(env.difficulty)
if env.difficulty is not None
else result.get("currentDifficulty"),
"number": str(self.env.number),
"gasLimit": str(self.env.gas_limit),
"timestamp": str(self.env.timestamp),
"number": str(env.number),
"gasLimit": str(env.gas_limit),
"timestamp": str(env.timestamp),
"extraData": "0x00",
"sha3Uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347", # noqa: E501
"mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000", # noqa: E501
"nonce": "0x0000000000000000",
}
if self.env.base_fee is not None:
header["baseFeePerGas"] = str(self.env.base_fee)
if env.base_fee is not None:
header["baseFeePerGas"] = str(env.base_fee)
block, head = b11r.build(header, txs, [], None)
header["hash"] = head
return (
Expand Down
12 changes: 5 additions & 7 deletions src/evm_transition_tool/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,20 +86,18 @@ def calc_state_root(self, env: Any, alloc: Any, fork: str) -> str:
"""
Calculate the state root for the given `alloc`.
"""
base_fee = env.base_fee
env = {
"currentCoinbase": "0x0000000000000000000000000000000000000000",
"currentDifficulty": "0x0",
"currentGasLimit": "0x0",
"currentNumber": "0",
"currentTimestamp": "0",
"currentBaseFee": hex(env.base_fee)
if env.base_fee is not None
else "7"
if base_fee_required(fork)
else None,
}
if env["currentBaseFee"] is None:
del env["currentBaseFee"]
if base_fee is not None:
env["currentBaseFee"] = str(base_fee)
elif base_fee_required(fork):
env["currentBaseFee"] = "1"

(_, result) = self.evaluate(alloc, [], env, fork)
return result.get("stateRoot")
Expand Down
Loading