Skip to content

Commit e45bc6d

Browse files
temp
1 parent db06467 commit e45bc6d

2 files changed

Lines changed: 55 additions & 11 deletions

File tree

tests/benchmark/stateful/bloatnet/test_setup_contracts.py

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,22 @@
66
BenchmarkTestFiller,
77
Fork,
88
Hash,
9+
Op,
910
Transaction,
1011
)
1112

1213
from tests.benchmark.stateful.helpers import (
1314
AccountMode,
1415
account_mode_initcode,
16+
account_mode_runtime_size,
1517
pack_transactions_into_blocks,
1618
)
1719

18-
RECEIVER_CONTRACT_COUNT = 5
20+
# Distinct contracts deployed per account mode. Sized for the most
21+
# demanding consumer, test_account_access, whose cheapest iteration is
22+
# ~2,691 gas: a 300M block reaches ~111,482 distinct salts, rounded up
23+
# with margin.
24+
RECEIVER_CONTRACT_COUNT = 120_000
1925

2026
CONTRACT_MODES = [
2127
AccountMode.EXISTING_CONTRACT_MINIMAL,
@@ -24,11 +30,31 @@
2430
AccountMode.EXISTING_CONTRACT_JUMPDEST,
2531
]
2632

27-
# Generous per-deployment gas limit. A max-size contract deposit costs
28-
# ~54M under EIP-8037; the single-byte runtime is far cheaper but the
29-
# same limit is harmless. Kept well below the block budget so many
30-
# deployments pack into one block.
31-
DEPLOYMENT_TX_GAS_LIMIT = 80_000_000
33+
34+
def deployment_gas_limit(
35+
fork: Fork, initcode: bytes, runtime_size: int
36+
) -> int:
37+
"""
38+
Return the gas limit for one CREATE2 deployment, derived from the
39+
intrinsic, CREATE2, and code-deposit costs with a small margin.
40+
"""
41+
intrinsic = fork.transaction_intrinsic_cost_calculator()(
42+
calldata=Hash(0) + initcode
43+
)
44+
create_cost = Op.CREATE2(
45+
value=0,
46+
offset=0,
47+
size=len(initcode),
48+
salt=0,
49+
init_code_size=len(initcode),
50+
).gas_cost(fork)
51+
deposit_cost = Op.RETURN(
52+
0,
53+
runtime_size,
54+
code_deposit_size=runtime_size,
55+
).gas_cost(fork)
56+
base = intrinsic + create_cost + deposit_cost
57+
return base + base // 16
3258

3359

3460
def test_deploy_existing_contracts(
@@ -43,20 +69,23 @@ def test_deploy_existing_contracts(
4369
4470
Runs once as a global setup before the bloatnet benchmarks; the
4571
transactions intentionally carry no test phase so that the payload
46-
pipeline replays them ahead of every scenario. Deployments are
47-
packed into blocks up to the block gas budget so each block carries
48-
many transactions instead of one.
72+
pipeline replays them ahead of every scenario. Each deployment's
73+
gas limit is computed from its cost, then transactions are packed
74+
into blocks up to the block gas budget.
4975
"""
5076
txs = []
5177
for account_mode in CONTRACT_MODES:
5278
initcode = account_mode_initcode(fork, account_mode)
79+
gas_limit = deployment_gas_limit(
80+
fork, initcode, account_mode_runtime_size(fork, account_mode)
81+
)
5382
sender = pre.fund_eoa()
5483
for salt in range(RECEIVER_CONTRACT_COUNT):
5584
txs.append(
5685
Transaction(
5786
to=DETERMINISTIC_FACTORY_ADDRESS,
5887
data=Hash(salt) + initcode,
59-
gas_limit=DEPLOYMENT_TX_GAS_LIMIT,
88+
gas_limit=gas_limit,
6089
sender=sender,
6190
)
6291
)

tests/benchmark/stateful/helpers.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ def build_existing_contract_initcode(
9393
return code
9494

9595

96+
# Runtime size of the jochemnet unique-code contract (EIP-170 limit).
97+
JOCHEMNET_RUNTIME_SIZE = 0x6000
98+
99+
96100
def build_unique_contract_initcode() -> bytes:
97101
"""
98102
Deployed runtime contract layout.
@@ -109,7 +113,7 @@ def build_unique_contract_initcode() -> bytes:
109113
Embedded ADDRESS makes runtime unique per contract;
110114
initcode and its CREATE2 hash is shared across all salts.
111115
"""
112-
max_code_size = 0x6000 # EIP-170 contract code size limit
116+
max_code_size = JOCHEMNET_RUNTIME_SIZE
113117

114118
# MCOPY fills MEM[0:0x8000] with JUMPDEST.
115119
# Runtime only uses MEM[0:0x6000].
@@ -149,6 +153,17 @@ def account_mode_initcode(fork: Fork, account_mode: AccountMode) -> bytes:
149153
return bytes(build_existing_contract_initcode(fork, account_mode))
150154

151155

156+
def account_mode_runtime_size(fork: Fork, account_mode: AccountMode) -> int:
157+
"""
158+
Return the deployed runtime size in bytes for the account mode.
159+
"""
160+
if account_mode == AccountMode.EXISTING_CONTRACT_MINIMAL:
161+
return 1
162+
if account_mode == AccountMode.EXISTING_CONTRACT_JUMPDEST:
163+
return JOCHEMNET_RUNTIME_SIZE
164+
return fork.max_code_size()
165+
166+
152167
def build_benchmark_txs(
153168
*,
154169
pre: Alloc,

0 commit comments

Comments
 (0)