154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242 | @pytest.mark.stub_parametrize("erc20_stub", "test_sstore_erc20_approve_")
def test_sstore_erc20_generic(
benchmark_test: BenchmarkTestFiller,
pre: Alloc,
fork: Fork,
gas_benchmark_value: int,
tx_gas_limit: int,
erc20_stub: str,
) -> None:
"""Benchmark SSTORE using ERC20 approve."""
sender = pre.fund_eoa()
threshold = 100_000
# Stub Account
erc20_address = pre.deploy_contract(
code=Bytecode(),
stub=erc20_stub,
)
# MEM[0] = function selector
# MEM[32] = starting address offset
setup = Op.MSTORE(
0,
APPROVE_SELECTOR,
) + Op.MSTORE(
32,
Op.SLOAD(0), # Address Offset
)
call_approve = Op.MSTORE(
64,
Op.ADD(1, Op.MLOAD(32)),
) + Op.POP(
Op.CALL(
address=erc20_address,
args_offset=28,
args_size=68,
)
)
loop = While(
body=call_approve + Op.MSTORE(32, Op.ADD(Op.MLOAD(32), 1)),
condition=Op.GT(Op.GAS, threshold),
)
teardown = Op.SSTORE(0, Op.MLOAD(32))
# Contract Deployment
code = setup + loop + teardown
attack_contract_address = pre.deploy_contract(code=code)
intrinsic_gas = fork.transaction_intrinsic_cost_calculator()()
# Transaction Loops
gas_remaining = gas_benchmark_value
# Collect tx params first, then build Transaction objects
# so that nonces are allocated contiguously per block.
tx_gas: list[int] = []
while gas_remaining > intrinsic_gas:
gas_available = min(gas_remaining, tx_gas_limit)
if gas_available < intrinsic_gas:
break
tx_gas.append(gas_available)
gas_remaining -= gas_available
txs = []
with TestPhaseManager.execution():
for gas_available in tx_gas:
txs.append(
Transaction(
gas_limit=gas_available,
to=attack_contract_address,
sender=sender,
)
)
blocks = [Block(txs=txs)]
benchmark_test(
pre=pre,
blocks=blocks,
skip_gas_used_validation=True,
expected_receipt_status=True,
)
|