|
| 1 | +""" |
| 2 | +abstract: BloatNet bench cases extracted from https://hackmd.io/9icZeLN7R0Sk5mIjKlZAHQ. |
| 3 | +
|
| 4 | + The idea of all these tests is to stress client implementations to find out |
| 5 | + where the limits of processing are focusing specifically on state-related |
| 6 | + operations. |
| 7 | +""" |
| 8 | + |
| 9 | +import pytest |
| 10 | + |
| 11 | +from ethereum_test_forks import Fork |
| 12 | +from ethereum_test_tools import ( |
| 13 | + Account, |
| 14 | + Alloc, |
| 15 | + Block, |
| 16 | + BlockchainTestFiller, |
| 17 | + Transaction, |
| 18 | + While, |
| 19 | +) |
| 20 | +from ethereum_test_vm import Bytecode |
| 21 | +from ethereum_test_vm import Opcodes as Op |
| 22 | + |
| 23 | +REFERENCE_SPEC_GIT_PATH = "DUMMY/bloatnet.md" |
| 24 | +REFERENCE_SPEC_VERSION = "1.0" |
| 25 | + |
| 26 | + |
| 27 | +# BLOATNET ARCHITECTURE: |
| 28 | +# |
| 29 | +# [Initcode Contract] [Factory Contract] [24KB Contracts] |
| 30 | +# (9.5KB) (116B) (N x 24KB each) |
| 31 | +# │ │ │ |
| 32 | +# │ EXTCODECOPY │ CREATE2(salt++) │ |
| 33 | +# └──────────────► ├──────────────────► Contract_0 |
| 34 | +# ├──────────────────► Contract_1 |
| 35 | +# ├──────────────────► Contract_2 |
| 36 | +# └──────────────────► Contract_N |
| 37 | +# |
| 38 | +# [Attack Contract] ──STATICCALL──► [Factory.getConfig()] |
| 39 | +# │ returns: (N, hash) |
| 40 | +# └─► Loop(i=0 to N): |
| 41 | +# 1. Generate CREATE2 addr: keccak256(0xFF|factory|i|hash)[12:] |
| 42 | +# 2. BALANCE(addr) → 2600 gas (cold access) |
| 43 | +# 3. EXTCODESIZE(addr) → 100 gas (warm access) |
| 44 | +# |
| 45 | +# HOW IT WORKS: |
| 46 | +# 1. Factory uses EXTCODECOPY to load initcode, avoiding PC-relative jumps |
| 47 | +# 2. Each CREATE2 deployment produces unique 24KB bytecode (via ADDRESS) |
| 48 | +# 3. All contracts share same initcode hash for deterministic addresses |
| 49 | +# 4. Attack rapidly accesses all contracts, stressing client's state handling |
| 50 | + |
| 51 | + |
| 52 | +@pytest.mark.valid_from("Prague") |
| 53 | +def test_bloatnet_balance_extcodesize( |
| 54 | + blockchain_test: BlockchainTestFiller, |
| 55 | + pre: Alloc, |
| 56 | + fork: Fork, |
| 57 | + gas_benchmark_value: int, |
| 58 | +): |
| 59 | + """ |
| 60 | + BloatNet test using BALANCE + EXTCODESIZE with "on-the-fly" CREATE2 |
| 61 | + address generation. |
| 62 | +
|
| 63 | + This test: |
| 64 | + 1. Assumes contracts are already deployed via the factory (salt 0 to N-1) |
| 65 | + 2. Generates CREATE2 addresses dynamically during execution |
| 66 | + 3. Calls BALANCE (cold) then EXTCODESIZE (warm) on each |
| 67 | + 4. Maximizes cache eviction by accessing many contracts |
| 68 | + """ |
| 69 | + gas_costs = fork.gas_costs() |
| 70 | + |
| 71 | + # Calculate gas costs |
| 72 | + intrinsic_gas = fork.transaction_intrinsic_cost_calculator()(calldata=b"") |
| 73 | + |
| 74 | + # Cost per contract access with CREATE2 address generation |
| 75 | + cost_per_contract = ( |
| 76 | + gas_costs.G_KECCAK_256 # SHA3 static cost for address generation (30) |
| 77 | + + gas_costs.G_KECCAK_256_WORD * 3 # SHA3 dynamic cost (85 bytes = 3 words * 6) |
| 78 | + + gas_costs.G_COLD_ACCOUNT_ACCESS # Cold BALANCE (2600) |
| 79 | + + gas_costs.G_BASE # POP balance (2) |
| 80 | + + gas_costs.G_WARM_ACCOUNT_ACCESS # Warm EXTCODESIZE (100) |
| 81 | + + gas_costs.G_BASE # POP code size (2) |
| 82 | + + gas_costs.G_BASE # DUP1 before BALANCE (3) |
| 83 | + + gas_costs.G_VERY_LOW * 4 # PUSH1 operations (4 * 3) |
| 84 | + + gas_costs.G_LOW # MLOAD for salt (3) |
| 85 | + + gas_costs.G_VERY_LOW # ADD for increment (3) |
| 86 | + + gas_costs.G_LOW # MSTORE salt back (3) |
| 87 | + + 10 # While loop overhead |
| 88 | + ) |
| 89 | + |
| 90 | + # Calculate how many contracts to access based on available gas |
| 91 | + available_gas = gas_benchmark_value - intrinsic_gas - 1000 # Reserve for cleanup |
| 92 | + contracts_needed = int(available_gas // cost_per_contract) |
| 93 | + |
| 94 | + # Deploy factory using stub contract - NO HARDCODED VALUES |
| 95 | + # The stub "bloatnet_factory" must be provided via --address-stubs flag |
| 96 | + # The factory at that address MUST have: |
| 97 | + # - Slot 0: Number of deployed contracts |
| 98 | + # - Slot 1: Init code hash for CREATE2 address calculation |
| 99 | + factory_address = pre.deploy_contract( |
| 100 | + code=Bytecode(), # Required parameter, but will be ignored for stubs |
| 101 | + stub="bloatnet_factory", |
| 102 | + ) |
| 103 | + |
| 104 | + # Log test requirements - deployed count read from factory storage |
| 105 | + print( |
| 106 | + f"Test needs {contracts_needed} contracts for " |
| 107 | + f"{gas_benchmark_value / 1_000_000:.1f}M gas. " |
| 108 | + f"Factory storage will be checked during execution." |
| 109 | + ) |
| 110 | + |
| 111 | + # Build attack contract that reads config from factory and performs attack |
| 112 | + attack_code = ( |
| 113 | + # Call getConfig() on factory to get num_deployed and init_code_hash |
| 114 | + Op.STATICCALL( |
| 115 | + gas=Op.GAS, |
| 116 | + address=factory_address, |
| 117 | + args_offset=0, |
| 118 | + args_size=0, |
| 119 | + ret_offset=96, |
| 120 | + ret_size=64, |
| 121 | + ) |
| 122 | + # Check if call succeeded |
| 123 | + + Op.ISZERO |
| 124 | + + Op.PUSH2(0x1000) # Jump to error handler if failed (far jump) |
| 125 | + + Op.JUMPI |
| 126 | + # Load results from memory |
| 127 | + # Memory[96:128] = num_deployed_contracts |
| 128 | + # Memory[128:160] = init_code_hash |
| 129 | + + Op.MLOAD(96) # Load num_deployed_contracts |
| 130 | + + Op.MLOAD(128) # Load init_code_hash |
| 131 | + # Setup memory for CREATE2 address generation |
| 132 | + # Memory layout at 0: 0xFF + factory_addr(20) + salt(32) + hash(32) |
| 133 | + + Op.MSTORE(0, factory_address) # Store factory address at memory position 0 |
| 134 | + + Op.MSTORE8(11, 0xFF) # Store 0xFF prefix at position (32 - 20 - 1) |
| 135 | + + Op.MSTORE(32, 0) # Store salt at position 32 |
| 136 | + # Stack now has: [num_contracts, init_code_hash] |
| 137 | + + Op.PUSH1(64) # Push memory position |
| 138 | + + Op.MSTORE # Store init_code_hash at memory[64] |
| 139 | + # Stack now has: [num_contracts] |
| 140 | + # Main attack loop - iterate through all deployed contracts |
| 141 | + + While( |
| 142 | + body=( |
| 143 | + # Generate CREATE2 addr: keccak256(0xFF+factory+salt+hash) |
| 144 | + Op.SHA3(11, 85) # Generate CREATE2 address from memory[11:96] |
| 145 | + # The address is now on the stack |
| 146 | + + Op.DUP1 # Duplicate for EXTCODESIZE |
| 147 | + + Op.POP(Op.BALANCE) # Cold access |
| 148 | + + Op.POP(Op.EXTCODESIZE) # Warm access |
| 149 | + # Increment salt for next iteration |
| 150 | + + Op.MSTORE(32, Op.ADD(Op.MLOAD(32), 1)) # Increment and store salt |
| 151 | + ), |
| 152 | + # Continue while we haven't reached the limit |
| 153 | + condition=Op.DUP1 + Op.PUSH1(1) + Op.SWAP1 + Op.SUB + Op.DUP1 + Op.ISZERO + Op.ISZERO, |
| 154 | + ) |
| 155 | + + Op.POP # Clean up counter |
| 156 | + ) |
| 157 | + |
| 158 | + # Deploy attack contract |
| 159 | + attack_address = pre.deploy_contract(code=attack_code) |
| 160 | + |
| 161 | + # Run the attack |
| 162 | + attack_tx = Transaction( |
| 163 | + to=attack_address, |
| 164 | + gas_limit=gas_benchmark_value, |
| 165 | + sender=pre.fund_eoa(), |
| 166 | + ) |
| 167 | + |
| 168 | + # Post-state: just verify attack contract exists |
| 169 | + post = { |
| 170 | + attack_address: Account(storage={}), |
| 171 | + } |
| 172 | + |
| 173 | + blockchain_test( |
| 174 | + pre=pre, |
| 175 | + blocks=[Block(txs=[attack_tx])], |
| 176 | + post=post, |
| 177 | + ) |
| 178 | + |
| 179 | + |
| 180 | +@pytest.mark.valid_from("Prague") |
| 181 | +def test_bloatnet_balance_extcodecopy( |
| 182 | + blockchain_test: BlockchainTestFiller, |
| 183 | + pre: Alloc, |
| 184 | + fork: Fork, |
| 185 | + gas_benchmark_value: int, |
| 186 | +): |
| 187 | + """ |
| 188 | + BloatNet test using BALANCE + EXTCODECOPY with on-the-fly CREATE2 |
| 189 | + address generation. |
| 190 | +
|
| 191 | + This test forces actual bytecode reads from disk by: |
| 192 | + 1. Assumes contracts are already deployed via the factory |
| 193 | + 2. Generating CREATE2 addresses dynamically during execution |
| 194 | + 3. Using BALANCE (cold) to warm the account |
| 195 | + 4. Using EXTCODECOPY (warm) to read 1 byte from the END of the bytecode |
| 196 | + """ |
| 197 | + gas_costs = fork.gas_costs() |
| 198 | + max_contract_size = fork.max_code_size() |
| 199 | + |
| 200 | + # Calculate costs |
| 201 | + intrinsic_gas = fork.transaction_intrinsic_cost_calculator()(calldata=b"") |
| 202 | + |
| 203 | + # Cost per contract with EXTCODECOPY and CREATE2 address generation |
| 204 | + cost_per_contract = ( |
| 205 | + gas_costs.G_KECCAK_256 # SHA3 static cost for address generation (30) |
| 206 | + + gas_costs.G_KECCAK_256_WORD * 3 # SHA3 dynamic cost (85 bytes = 3 words * 6) |
| 207 | + + gas_costs.G_COLD_ACCOUNT_ACCESS # Cold BALANCE (2600) |
| 208 | + + gas_costs.G_BASE # POP balance (2) |
| 209 | + + gas_costs.G_WARM_ACCOUNT_ACCESS # Warm EXTCODECOPY base (100) |
| 210 | + + gas_costs.G_COPY * 1 # Copy cost for 1 byte (3) |
| 211 | + + gas_costs.G_BASE * 2 # DUP1 before BALANCE, DUP4 for address (6) |
| 212 | + + gas_costs.G_VERY_LOW * 8 # PUSH operations (8 * 3 = 24) |
| 213 | + + gas_costs.G_LOW * 2 # MLOAD for salt twice (6) |
| 214 | + + gas_costs.G_VERY_LOW * 2 # ADD operations (6) |
| 215 | + + gas_costs.G_LOW # MSTORE salt back (3) |
| 216 | + + gas_costs.G_BASE # POP after EXTCODECOPY (2) |
| 217 | + + 10 # While loop overhead |
| 218 | + ) |
| 219 | + |
| 220 | + # Calculate how many contracts to access |
| 221 | + available_gas = gas_benchmark_value - intrinsic_gas - 1000 |
| 222 | + contracts_needed = int(available_gas // cost_per_contract) |
| 223 | + |
| 224 | + # Deploy factory using stub contract - NO HARDCODED VALUES |
| 225 | + # The stub "bloatnet_factory" must be provided via --address-stubs flag |
| 226 | + # The factory at that address MUST have: |
| 227 | + # - Slot 0: Number of deployed contracts |
| 228 | + # - Slot 1: Init code hash for CREATE2 address calculation |
| 229 | + factory_address = pre.deploy_contract( |
| 230 | + code=Bytecode(), # Required parameter, but will be ignored for stubs |
| 231 | + stub="bloatnet_factory", |
| 232 | + ) |
| 233 | + |
| 234 | + # Log test requirements - deployed count read from factory storage |
| 235 | + print( |
| 236 | + f"Test needs {contracts_needed} contracts for " |
| 237 | + f"{gas_benchmark_value / 1_000_000:.1f}M gas. " |
| 238 | + f"Factory storage will be checked during execution." |
| 239 | + ) |
| 240 | + |
| 241 | + # Build attack contract that reads config from factory and performs attack |
| 242 | + attack_code = ( |
| 243 | + # Call getConfig() on factory to get num_deployed and init_code_hash |
| 244 | + Op.STATICCALL( |
| 245 | + gas=Op.GAS, |
| 246 | + address=factory_address, |
| 247 | + args_offset=0, |
| 248 | + args_size=0, |
| 249 | + ret_offset=96, |
| 250 | + ret_size=64, |
| 251 | + ) |
| 252 | + # Check if call succeeded |
| 253 | + + Op.ISZERO |
| 254 | + + Op.PUSH2(0x1000) # Jump to error handler if failed (far jump) |
| 255 | + + Op.JUMPI |
| 256 | + # Load results from memory |
| 257 | + # Memory[96:128] = num_deployed_contracts |
| 258 | + # Memory[128:160] = init_code_hash |
| 259 | + + Op.MLOAD(96) # Load num_deployed_contracts |
| 260 | + + Op.MLOAD(128) # Load init_code_hash |
| 261 | + # Setup memory for CREATE2 address generation |
| 262 | + # Memory layout at 0: 0xFF + factory_addr(20) + salt(32) + hash(32) |
| 263 | + + Op.MSTORE(0, factory_address) # Store factory address at memory position 0 |
| 264 | + + Op.MSTORE8(11, 0xFF) # Store 0xFF prefix at position (32 - 20 - 1) |
| 265 | + + Op.MSTORE(32, 0) # Store salt at position 32 |
| 266 | + # Stack now has: [num_contracts, init_code_hash] |
| 267 | + + Op.PUSH1(64) # Push memory position |
| 268 | + + Op.MSTORE # Store init_code_hash at memory[64] |
| 269 | + # Stack now has: [num_contracts] |
| 270 | + # Main attack loop - iterate through all deployed contracts |
| 271 | + + While( |
| 272 | + body=( |
| 273 | + # Generate CREATE2 address |
| 274 | + Op.SHA3(11, 85) # Generate CREATE2 address from memory[11:96] |
| 275 | + # The address is now on the stack |
| 276 | + + Op.DUP1 # Duplicate for later operations |
| 277 | + + Op.POP(Op.BALANCE) # Cold access |
| 278 | + # EXTCODECOPY(addr, mem_offset, last_byte_offset, 1) |
| 279 | + # Read the LAST byte to force full contract load |
| 280 | + + Op.PUSH1(1) # size (1 byte) |
| 281 | + + Op.PUSH2(max_contract_size - 1) # code offset (last byte) |
| 282 | + # Use salt as memory offset to avoid overlap |
| 283 | + + Op.ADD(Op.MLOAD(32), 96) # Add base memory offset for unique position |
| 284 | + + Op.DUP4 # address (duplicated earlier) |
| 285 | + + Op.EXTCODECOPY |
| 286 | + + Op.POP # Clean up address |
| 287 | + # Increment salt for next iteration |
| 288 | + + Op.MSTORE(32, Op.ADD(Op.MLOAD(32), 1)) # Increment and store salt |
| 289 | + ), |
| 290 | + # Continue while counter > 0 |
| 291 | + condition=Op.DUP1 + Op.PUSH1(1) + Op.SWAP1 + Op.SUB + Op.DUP1 + Op.ISZERO + Op.ISZERO, |
| 292 | + ) |
| 293 | + + Op.POP # Clean up counter |
| 294 | + ) |
| 295 | + |
| 296 | + # Deploy attack contract |
| 297 | + attack_address = pre.deploy_contract(code=attack_code) |
| 298 | + |
| 299 | + # Run the attack |
| 300 | + attack_tx = Transaction( |
| 301 | + to=attack_address, |
| 302 | + gas_limit=gas_benchmark_value, |
| 303 | + sender=pre.fund_eoa(), |
| 304 | + ) |
| 305 | + |
| 306 | + # Post-state |
| 307 | + post = { |
| 308 | + attack_address: Account(storage={}), |
| 309 | + } |
| 310 | + |
| 311 | + blockchain_test( |
| 312 | + pre=pre, |
| 313 | + blocks=[Block(txs=[attack_tx])], |
| 314 | + post=post, |
| 315 | + ) |
0 commit comments