Skip to content

Commit 69315cd

Browse files
committed
stale keys tests
Signed-off-by: Ignacio Hagopian <[email protected]>
1 parent d01e5ad commit 69315cd

File tree

2 files changed

+152
-0
lines changed

2 files changed

+152
-0
lines changed

tests/verkle/eip7748/stale_keys.py

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
import pytest
2+
3+
from ethereum_test_tools import BlockchainTestFiller, Transaction, Account, TestAddress
4+
from ethereum_test_tools.vm.opcode import Opcodes as Op
5+
from .utils import stride, _state_conversion, accounts, ConversionTx
6+
7+
REFERENCE_SPEC_GIT_PATH = "EIPS/eip-7748.md"
8+
REFERENCE_SPEC_VERSION = "TODO"
9+
10+
11+
@pytest.mark.valid_from("EIP6800Transition")
12+
@pytest.mark.parametrize(
13+
"storage_slot_write",
14+
[
15+
0,
16+
1,
17+
300,
18+
301,
19+
],
20+
ids=[
21+
"Stale storage slot in the header",
22+
"New storage slot in the account header",
23+
"Stale storage slot outside the header",
24+
"New storage slot outside the header",
25+
],
26+
)
27+
@pytest.mark.parametrize(
28+
"stale_basic_data",
29+
[True, False],
30+
)
31+
def test_stale_contract_writes(
32+
blockchain_test: BlockchainTestFiller,
33+
storage_slot_write: int,
34+
stale_basic_data: bool,
35+
):
36+
"""
37+
Test converting a contract where a previous transaction writes to:
38+
- Existing storage slots (i.e., storage slots that must not be converted (stale))
39+
- New storage slots (i.e., storage slots that must not be converted (not overriden with zeros))
40+
- Basic data (i.e., balance/nonce which must not be converted (stale))
41+
"""
42+
pre_state = {}
43+
pre_state[TestAddress] = Account(balance=1000000000000000000000)
44+
45+
# TODO(hack): today the testing-framework does not support us signaling that we want to
46+
# put the `ConversionTx(tx, **0**)` at the first block after genesis. To simulate that, we have
47+
# to do this here so we "shift" the target account to the second block in the fork.
48+
# If this is ever supported, remove this.
49+
for i in range(stride):
50+
pre_state[accounts[i]] = Account(balance=100 + 1000 * i)
51+
52+
target_account = accounts[stride]
53+
pre_state[target_account] = Account(
54+
balance=1_000,
55+
nonce=0,
56+
code=Op.SSTORE(storage_slot_write, 9999),
57+
storage={
58+
0: 100,
59+
300: 200,
60+
},
61+
)
62+
63+
tx = Transaction(
64+
ty=0x0,
65+
chain_id=0x01,
66+
to=target_account,
67+
value=100 if stale_basic_data else 0,
68+
gas_limit=100_000,
69+
gas_price=10,
70+
)
71+
72+
_state_conversion(blockchain_test, pre_state, stride, 1, [ConversionTx(tx, 0)])
73+
74+
75+
# @pytest.mark.valid_from("EIP6800Transition")
76+
# @pytest.mark.parametrize(
77+
# "num_storage_slots, num_stale_storage_slots",
78+
# [
79+
# (0, 0),
80+
# (4, 0),
81+
# (4, 2),
82+
# (stride, stride),
83+
# ],
84+
# ids=[
85+
# "EOA",
86+
# "Contract without stale storage",
87+
# "Contract with some stale storage",
88+
# "Contract with all stale storage",
89+
# ],
90+
# )
91+
# @pytest.mark.parametrize(
92+
# "stale_basic_data",
93+
# [True, False],
94+
# )
95+
# @pytest.mark.parametrize(
96+
# "fill_first_block",
97+
# [True, False],
98+
# )
99+
# @pytest.mark.parametrize(
100+
# "fill_last_block",
101+
# [True, False],
102+
# )
103+
# def test_stale_keys(
104+
# blockchain_test: BlockchainTestFiller,
105+
# account_configs: list[AccountConfig],
106+
# fill_first_block: bool,
107+
# fill_last_block: bool,
108+
# ):
109+
# """
110+
# Test account conversion with full/partial stale storage slots and basic data.
111+
# """
112+
# _generic_conversion(blockchain_test, account_configs, fill_first_block, fill_last_block)
113+
114+
115+
# @pytest.mark.valid_from("EIP6800Transition")
116+
# @pytest.mark.parametrize(
117+
# "fill_first_block",
118+
# [True, False],
119+
# )
120+
# @pytest.mark.parametrize(
121+
# "fill_last_block",
122+
# [True, False],
123+
# )
124+
# def test_stride_stale_eoas(
125+
# blockchain_test: BlockchainTestFiller,
126+
# account_configs: list[AccountConfig],
127+
# fill_first_block: bool,
128+
# fill_last_block: bool,
129+
# ):
130+
# """
131+
# Test converting a stride number of stale EOAs.
132+
# """
133+
# _generic_conversion(blockchain_test, account_configs, fill_first_block, fill_last_block)

tests/verkle/eip7748/utils.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
Address,
66
Block,
77
BlockchainTestFiller,
8+
Transaction,
89
Environment,
910
)
1011

@@ -19,6 +20,12 @@ def __init__(self, code_length: int, storage_slot_count: int):
1920
self.storage_slots_count = storage_slot_count
2021

2122

23+
class StaleBasicDataTx:
24+
def __init__(self, account_config_idx: int, block_num: int):
25+
self.account_config_idx = account_config_idx
26+
self.block_num = block_num
27+
28+
2229
def _generic_conversion(
2330
blockchain_test: BlockchainTestFiller,
2431
account_configs: list[AccountConfig],
@@ -34,6 +41,7 @@ def _generic_conversion(
3441
pre_state[accounts[account_idx]] = Account(balance=100 + 1000 * i)
3542
account_idx += 1
3643

44+
target_accounts: list[Address] = {}
3745
for i, account_config in enumerate(account_configs):
3846
storage = {}
3947
for j in range(account_config.storage_slots_count):
@@ -46,6 +54,7 @@ def _generic_conversion(
4654
code=Op.JUMPDEST * account_config.code_length,
4755
storage=storage,
4856
)
57+
target_accounts.append(accounts[account_idx])
4958
account_idx += 1
5059

5160
conversion_units += 1 # Account basic data
@@ -62,11 +71,18 @@ def _generic_conversion(
6271
_state_conversion(blockchain_test, pre_state, stride, math.ceil(conversion_units / stride))
6372

6473

74+
class ConversionTx:
75+
def __init__(self, tx: Transaction, block_num: int):
76+
self.tx = tx
77+
self.block_num = block_num
78+
79+
6580
def _state_conversion(
6681
blockchain_test: BlockchainTestFiller,
6782
pre_state: dict[Address, Account],
6883
stride: int,
6984
num_blocks: int,
85+
txs: list[ConversionTx] = [],
7086
):
7187
# TODO: test library should allow passing stride
7288
env = Environment(
@@ -79,6 +95,9 @@ def _state_conversion(
7995
for i in range(num_blocks):
8096
blocks.append(Block(txs=[]))
8197

98+
for tx in txs:
99+
blocks[tx.block_num].txs.append(tx.tx)
100+
82101
# TODO: witness assertion
83102
# TODO: see if possible last block switch to finished conversion
84103

0 commit comments

Comments
 (0)