Skip to content

test_invalid_gas_consumption()

Documentation for tests/byzantium/eip197_ec_pairing/test_gas.py::test_invalid_gas_consumption@b314d18e.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/byzantium/eip197_ec_pairing/test_gas.py::test_invalid_gas_consumption --fork Amsterdam

Test that invalid input to ecpairing consumes all forwarded gas.

Use CodeGasMeasure to verify the STATICCALL with invalid input consumes exactly the warm call cost plus all forwarded gas.

Source code in tests/byzantium/eip197_ec_pairing/test_gas.py
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
@pytest.mark.valid_from("Byzantium")
@pytest.mark.parametrize(
    "input_data",
    [
        pytest.param(
            PointG1(1, 3) + Spec.G2,
            id="invalid_g1_point",
        ),
    ],
)
@pytest.mark.parametrize(
    "extra_gas",
    [
        pytest.param(0, id="exact"),
        pytest.param(100_000, id="extra_100k"),
    ],
)
def test_invalid_gas_consumption(
    state_test: StateTestFiller,
    pre: Alloc,
    fork: Fork,
    precompile_gas: int,
    input_data: bytes,
    extra_gas: int,
) -> None:
    """
    Test that invalid input to ecpairing consumes all forwarded gas.

    Use CodeGasMeasure to verify the STATICCALL with invalid input
    consumes exactly the warm call cost plus all forwarded gas.
    """
    gas_forward = precompile_gas + extra_gas
    input_size = len(input_data)
    storage = Storage()

    staticcall_code = Op.STATICCALL(
        gas=gas_forward,
        address=Spec.ECPAIRING,
        args_size=input_size,
    )
    push_cost = staticcall_code.gas_cost(fork) - Op.STATICCALL(
        address_warm=False
    ).gas_cost(fork)

    # Pre-EIP-2929: fixed call = 700; Berlin+: warm access cost.
    gas_costs = fork.gas_costs()
    if fork >= Berlin:
        staticcall_base = gas_costs.WARM_ACCESS
    else:
        staticcall_base = 700

    account = pre.deploy_contract(
        code=(
            Om.MSTORE(input_data)
            # Warm the precompile address
            + Op.POP(Op.STATICCALL(gas=0, address=Spec.ECPAIRING))
            + CodeGasMeasure(
                code=staticcall_code,
                overhead_cost=push_cost,
                extra_stack_items=1,
                sstore_key=storage.store_next(staticcall_base + gas_forward),
            )
        ),
        storage=storage.canary(),
    )

    tx = Transaction(
        to=account,
        sender=pre.fund_eoa(),
        gas_limit=1_000_000,
        protected=True,
    )

    post = {account: Account(storage=storage)}

    state_test(pre=pre, post=post, tx=tx)

Parametrized Test Cases

This test generates 2 parametrized test cases across 12 forks.