|
| 1 | +""" |
| 2 | +Test EXP opcode. |
| 3 | +""" |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from ethereum_test_base_types.base_types import ZeroPaddedHexNumber |
| 8 | +from ethereum_test_forks import Fork |
| 9 | +from ethereum_test_tools import ( |
| 10 | + Alloc, |
| 11 | + Environment, |
| 12 | + StateTestFiller, |
| 13 | +) |
| 14 | +from ethereum_test_vm import Opcodes as Op |
| 15 | +from tests.unscheduled.eip7692_eof_v1.gas_test import gas_test |
| 16 | + |
| 17 | +REFERENCE_SPEC_GIT_PATH = "N/A" |
| 18 | +REFERENCE_SPEC_VERSION = "N/A" |
| 19 | + |
| 20 | + |
| 21 | +def exp_gas(fork: Fork, exponent: int) -> int: |
| 22 | + """Calculate gas cost for EXP opcode given the exponent.""" |
| 23 | + byte_len = (exponent.bit_length() + 7) // 8 |
| 24 | + return fork.gas_costs().G_EXP + fork.gas_costs().G_EXP_BYTE * byte_len |
| 25 | + |
| 26 | + |
| 27 | +@pytest.mark.valid_from("Berlin") |
| 28 | +@pytest.mark.parametrize("a", [0, 1, pytest.param(2**256 - 1, id="a2to256minus1")]) |
| 29 | +@pytest.mark.parametrize( |
| 30 | + "exponent", |
| 31 | + [ |
| 32 | + 0, |
| 33 | + 1, |
| 34 | + 2, |
| 35 | + 1023, |
| 36 | + 1024, |
| 37 | + pytest.param(2**255, id="exponent2to255"), |
| 38 | + pytest.param(2**256 - 1, id="exponent2to256minus1"), |
| 39 | + ], |
| 40 | +) |
| 41 | +def test_gas( |
| 42 | + state_test: StateTestFiller, pre: Alloc, a: int, exponent: int, fork: Fork, env: Environment |
| 43 | +) -> None: |
| 44 | + """Test that EXP gas works as expected.""" |
| 45 | + warm_gas = exp_gas(fork, exponent) |
| 46 | + |
| 47 | + if cap := fork.transaction_gas_limit_cap(): |
| 48 | + env.gas_limit = ZeroPaddedHexNumber(cap) |
| 49 | + |
| 50 | + gas_test( |
| 51 | + fork, |
| 52 | + state_test, |
| 53 | + env, |
| 54 | + pre, |
| 55 | + setup_code=Op.PUSH32(exponent) + Op.PUSH32(a), |
| 56 | + subject_code=Op.EXP, |
| 57 | + tear_down_code=Op.STOP, |
| 58 | + cold_gas=warm_gas, |
| 59 | + warm_gas=warm_gas, |
| 60 | + eof=False, |
| 61 | + ) |
0 commit comments