|
| 1 | +"""Tests for the BenchmarkTest class and its transaction splitting functionality.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from ethereum_test_base_types import HexNumber |
| 6 | +from ethereum_test_specs.benchmark import BenchmarkTest |
| 7 | +from ethereum_test_types import Alloc, Environment, Transaction |
| 8 | + |
| 9 | + |
| 10 | +@pytest.mark.parametrize( |
| 11 | + "gas_benchmark_value_millions,expected_splits", |
| 12 | + [ |
| 13 | + (1, 1), # 1M / 16M = 1 transaction |
| 14 | + (10, 1), # 10M / 16M = 1 transaction |
| 15 | + (30, 2), # 30M / 16M = 2 transactions (16M + 14M) |
| 16 | + (45, 3), # 45M / 16M = 3 transactions (16M + 16M + 13M) |
| 17 | + (60, 4), # 60M / 16M = 4 transactions (16M + 16M + 16M + 12M) |
| 18 | + (100, 7), # 100M / 16M = 7 transactions (6x16M + 4M) |
| 19 | + (150, 10), # 150M / 16M = 10 transactions (9x16M + 6M) |
| 20 | + ], |
| 21 | +) |
| 22 | +def test_split_transaction(gas_benchmark_value_millions: int, expected_splits: int): |
| 23 | + """Test that transaction splitting works correctly for Osaka fork gas cap.""" |
| 24 | + gas_benchmark_value = gas_benchmark_value_millions * 1_000_000 |
| 25 | + gas_limit_cap = 16_000_000 # Osaka's transaction gas limit cap |
| 26 | + |
| 27 | + # Create a minimal BenchmarkTest instance |
| 28 | + benchmark_test = BenchmarkTest( |
| 29 | + pre=Alloc(), |
| 30 | + post=Alloc(), |
| 31 | + tx=Transaction(sender=HexNumber(0), to=HexNumber(0), nonce=0), |
| 32 | + env=Environment(), |
| 33 | + gas_benchmark_value=gas_benchmark_value, |
| 34 | + ) |
| 35 | + |
| 36 | + # Test the split_transaction method |
| 37 | + assert benchmark_test.tx is not None, "Transaction should not be None" |
| 38 | + split_txs = benchmark_test.split_transaction(benchmark_test.tx, gas_limit_cap) |
| 39 | + |
| 40 | + # Verify the number of transactions |
| 41 | + assert len(split_txs) == expected_splits, ( |
| 42 | + f"Expected {expected_splits} transactions for {gas_benchmark_value_millions}M gas, " |
| 43 | + f"got {len(split_txs)}" |
| 44 | + ) |
| 45 | + |
| 46 | + # Verify total gas equals the benchmark value |
| 47 | + total_gas = sum(tx.gas_limit for tx in split_txs) |
| 48 | + assert total_gas == gas_benchmark_value, ( |
| 49 | + f"Total gas {total_gas} doesn't match benchmark value {gas_benchmark_value}" |
| 50 | + ) |
| 51 | + |
| 52 | + # Verify no transaction exceeds the cap |
| 53 | + for i, tx in enumerate(split_txs): |
| 54 | + assert tx.gas_limit <= gas_limit_cap, ( |
| 55 | + f"Transaction {i} gas limit {tx.gas_limit} exceeds cap {gas_limit_cap}" |
| 56 | + ) |
| 57 | + |
| 58 | + # Verify nonces increment correctly |
| 59 | + for i, tx in enumerate(split_txs): |
| 60 | + assert tx.nonce == i, f"Transaction {i} has incorrect nonce {tx.nonce}" |
| 61 | + |
| 62 | + # Verify gas distribution |
| 63 | + for i, tx in enumerate(split_txs[:-1]): # All but last should be at cap |
| 64 | + assert tx.gas_limit == gas_limit_cap, ( |
| 65 | + f"Transaction {i} should have gas limit {gas_limit_cap}, got {tx.gas_limit}" |
| 66 | + ) |
| 67 | + |
| 68 | + # Last transaction should have the remainder |
| 69 | + if expected_splits > 1: |
| 70 | + expected_last_gas = gas_benchmark_value - (gas_limit_cap * (expected_splits - 1)) |
| 71 | + assert split_txs[-1].gas_limit == expected_last_gas, ( |
| 72 | + f"Last transaction should have {expected_last_gas} gas, got {split_txs[-1].gas_limit}" |
| 73 | + ) |
| 74 | + |
| 75 | + |
| 76 | +@pytest.mark.parametrize( |
| 77 | + "gas_benchmark_value,gas_limit_cap", |
| 78 | + [ |
| 79 | + (50_000_000, None), # No cap - should return single transaction |
| 80 | + (50_000_000, 100_000_000), # Cap higher than benchmark value |
| 81 | + ], |
| 82 | +) |
| 83 | +def test_split_transaction_edge_cases(gas_benchmark_value: int, gas_limit_cap: int | None): |
| 84 | + """Test edge cases for transaction splitting.""" |
| 85 | + benchmark_test = BenchmarkTest( |
| 86 | + pre=Alloc(), |
| 87 | + post=Alloc(), |
| 88 | + tx=Transaction(sender=HexNumber(0), to=HexNumber(0), nonce=0, gas_limit=1_000_000_000), |
| 89 | + env=Environment(), |
| 90 | + gas_benchmark_value=gas_benchmark_value, |
| 91 | + ) |
| 92 | + |
| 93 | + assert benchmark_test.tx is not None, "Transaction should not be None" |
| 94 | + split_txs = benchmark_test.split_transaction(benchmark_test.tx, gas_limit_cap) |
| 95 | + |
| 96 | + # Should return single transaction in both cases |
| 97 | + assert len(split_txs) == 1, f"Expected 1 transaction, got {len(split_txs)}" |
| 98 | + |
| 99 | + if gas_limit_cap is None: |
| 100 | + # When no cap, gas_limit should be benchmark value |
| 101 | + assert split_txs[0].gas_limit == gas_benchmark_value |
| 102 | + else: |
| 103 | + # When cap > benchmark, gas_limit should be min of tx.gas_limit and benchmark |
| 104 | + assert benchmark_test.tx is not None, "Transaction should not be None" |
| 105 | + assert split_txs[0].gas_limit == min(benchmark_test.tx.gas_limit, gas_benchmark_value) |
0 commit comments