798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886 | @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,
)
|