43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164 | @pytest.mark.parametrize("transfer_amount", [0, 1])
@pytest.mark.parametrize("opcode", [Op.CALL, Op.CALLCODE])
@pytest.mark.parametrize("access_warm", [True, False])
def test_contract_calling_many_addresses(
benchmark_test: BenchmarkTestFiller,
pre: Alloc,
fork: Fork,
transfer_amount: int,
opcode: Op,
access_warm: bool,
gas_benchmark_value: int,
fixed_opcode_count: float | None,
) -> None:
"""Benchmark a contract that calls many distinct addresses."""
start_addr = 2**80 - 1
value_transfer = transfer_amount > 0
# Only CALL creates accounts on value transfer (CALLCODE doesn't).
account_creation = value_transfer and opcode == Op.CALL
setup = (
Op.ADD(1, Op.CALLDATALOAD(32)) # [end+1 = limit]
+ Op.CALLDATALOAD(0) # [start = index, limit]
)
iterating = While(
body=Op.POP(
opcode(
address=Op.ADD(start_addr, Op.DUP6),
value=transfer_amount,
# gas accounting
address_warm=access_warm,
value_transfer=value_transfer,
account_new=account_creation,
)
),
condition=Op.PUSH1(1) # [1, index, limit]
+ Op.ADD # [index+1, limit]
+ Op.DUP1 # [index+1, index+1, limit]
+ Op.DUP3 # [limit, index+1, index+1, limit]
+ Op.GT, # [limit > index+1, index+1, limit]
)
code = IteratingBytecode(
setup=setup,
iterating=iterating,
cleanup=Op.STOP,
)
contract_address = pre.deploy_contract(
code=code,
balance=10**9 if value_transfer else 0,
)
def calldata(iteration_count: int, start_iteration: int) -> bytes:
index_end = start_iteration + iteration_count - 1
return Hash(start_iteration) + Hash(index_end)
def access_list(
iteration_count: int, start_iteration: int
) -> list[AccessList]:
return [
AccessList(address=Address(start_addr + i), storage_keys=[])
for i in range(start_iteration, start_iteration + iteration_count)
]
tx_kwargs: dict = {
"calldata": calldata,
"access_list": access_list if access_warm else None,
}
total_iterations = (
sum(
code.tx_iterations_by_gas_limit(
fork=fork, gas_limit=gas_benchmark_value, **tx_kwargs
)
)
if fixed_opcode_count is None
else int(fixed_opcode_count * 1000)
)
if total_iterations == 0:
pytest.skip(
"Benchmark gas value cannot cover a single call to the contract."
)
with TestPhaseManager.execution():
sender = pre.fund_eoa()
if fixed_opcode_count is not None:
exec_txs = list(
code.transactions_by_total_iteration_count(
fork=fork,
total_iterations=total_iterations,
sender=sender,
to=contract_address,
**tx_kwargs,
)
)
else:
exec_txs = list(
code.transactions_by_gas_limit(
fork=fork,
gas_limit=gas_benchmark_value,
sender=sender,
to=contract_address,
**tx_kwargs,
)
)
total_gas_cost = sum(tx.gas_cost for tx in exec_txs)
if value_transfer:
total_gas_cost -= fork.gas_costs().CALL_STIPEND * total_iterations
post = {
Address(start_addr + i): Account(balance=transfer_amount)
for i in range(total_iterations)
if account_creation
}
benchmark_test(
post=post,
blocks=[Block(txs=exec_txs)],
expected_benchmark_gas_used=total_gas_cost,
)
|