Skip to content

Test Warm Coinbase

Documentation for test cases from tests/shanghai/eip3651_warm_coinbase/test_warm_coinbase.py.

Generate fixtures for these test cases with:

fill -v tests/shanghai/eip3651_warm_coinbase/test_warm_coinbase.py
Tests EIP-3651: Warm COINBASE

Tests for EIP-3651: Warm COINBASE.

Tests ported from:

test_warm_coinbase_call_out_of_gas(state_test, fork, opcode, contract_under_test_code, call_gas_exact, use_sufficient_gas)

Test that the coinbase is warm by accessing the COINBASE with each of the following opcodes:

  • CALL
  • CALLCODE
  • DELEGATECALL
  • STATICCALL
Source code in tests/shanghai/eip3651_warm_coinbase/test_warm_coinbase.py
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 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
141
142
143
144
@pytest.mark.valid_from("Shanghai")
@pytest.mark.parametrize(
    "use_sufficient_gas",
    [True, False],
    ids=["sufficient_gas", "insufficient_gas"],
)
@pytest.mark.parametrize(
    "opcode,contract_under_test_code,call_gas_exact",
    [
        (
            "call",
            Op.POP(Op.CALL(0, Op.COINBASE, 0, 0, 0, 0, 0)),
            # Extra gas: COINBASE + 4*PUSH1 + 2*DUP1 + POP
            GAS_REQUIRED_CALL_WARM_ACCOUNT + 22,
        ),
        (
            "callcode",
            Op.POP(Op.CALLCODE(0, Op.COINBASE, 0, 0, 0, 0, 0)),
            # Extra gas: COINBASE + 4*PUSH1 + 2*DUP1 + POP
            GAS_REQUIRED_CALL_WARM_ACCOUNT + 22,
        ),
        (
            "delegatecall",
            Op.POP(Op.DELEGATECALL(0, Op.COINBASE, 0, 0, 0, 0)),
            # Extra: COINBASE + 3*PUSH1 + 2*DUP1 + POP
            GAS_REQUIRED_CALL_WARM_ACCOUNT + 19,
        ),
        (
            "staticcall",
            Op.POP(Op.STATICCALL(0, Op.COINBASE, 0, 0, 0, 0)),
            # Extra: COINBASE + 3*PUSH1 + 2*DUP1 + POP
            GAS_REQUIRED_CALL_WARM_ACCOUNT + 19,
        ),
    ],
    ids=["CALL", "CALLCODE", "DELEGATECALL", "STATICCALL"],
)
def test_warm_coinbase_call_out_of_gas(
    state_test,
    fork,
    opcode,
    contract_under_test_code,
    call_gas_exact,
    use_sufficient_gas,
):
    """
    Test that the coinbase is warm by accessing the COINBASE with each
    of the following opcodes:

    - CALL
    - CALLCODE
    - DELEGATECALL
    - STATICCALL
    """
    env = Environment(
        coinbase="0x2adc25665018aa1fe0e6bc666dac8fc2697ff9ba",
        difficulty=0x20000,
        gas_limit=10000000000,
        number=1,
        timestamp=1000,
    )
    caller_address = "0xcccccccccccccccccccccccccccccccccccccccc"
    contract_under_test_address = 0x100

    if not use_sufficient_gas:
        call_gas_exact -= 1

    caller_code = Op.SSTORE(
        0,
        Op.CALL(call_gas_exact, contract_under_test_address, 0, 0, 0, 0, 0),
    )

    pre = {
        TestAddress: Account(balance=1000000000000000000000),
        caller_address: Account(code=caller_code),
        to_address(contract_under_test_address): Account(code=contract_under_test_code),
    }

    tx = Transaction(
        ty=0x0,
        chain_id=0x0,
        nonce=0,
        to=caller_address,
        gas_limit=100000000,
        gas_price=10,
        protected=False,
    )

    post = {}

    if use_sufficient_gas and is_fork(fork=fork, which=Shanghai):
        post[caller_address] = Account(
            storage={
                # On shanghai and beyond, calls with only 100 gas to
                # coinbase will succeed.
                0: 1,
            }
        )
    else:
        post[caller_address] = Account(
            storage={
                # Before shanghai, calls with only 100 gas to
                # coinbase will fail.
                0: 0,
            }
        )

    state_test(
        env=env,
        pre=pre,
        post=post,
        txs=[tx],
        tag="opcode_" + opcode,
    )

test_warm_coinbase_gas_usage(state_test, fork, opcode, code_gas_measure)

Test the gas usage of opcodes affected by assuming a warm coinbase:

  • EXTCODESIZE
  • EXTCODECOPY
  • EXTCODEHASH
  • BALANCE
  • CALL
  • CALLCODE
  • DELEGATECALL
  • STATICCALL
Source code in tests/shanghai/eip3651_warm_coinbase/test_warm_coinbase.py
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
@pytest.mark.valid_from("Merge")  # these tests fill for fork >= Berlin
@pytest.mark.parametrize(
    "opcode,code_gas_measure",
    gas_measured_opcodes,
    ids=[i[0] for i in gas_measured_opcodes],
)
def test_warm_coinbase_gas_usage(state_test, fork, opcode, code_gas_measure):
    """
    Test the gas usage of opcodes affected by assuming a warm coinbase:

    - EXTCODESIZE
    - EXTCODECOPY
    - EXTCODEHASH
    - BALANCE
    - CALL
    - CALLCODE
    - DELEGATECALL
    - STATICCALL
    """
    env = Environment(
        coinbase="0x2adc25665018aa1fe0e6bc666dac8fc2697ff9ba",
        difficulty=0x20000,
        gas_limit=10000000000,
        number=1,
        timestamp=1000,
    )

    measure_address = to_address(0x100)
    pre = {
        TestAddress: Account(balance=1000000000000000000000),
        measure_address: Account(
            code=code_gas_measure,
        ),
    }

    if is_fork(fork, Shanghai):
        expected_gas = GAS_REQUIRED_CALL_WARM_ACCOUNT  # Warm account access cost after EIP-3651
    else:
        expected_gas = 2600  # Cold account access cost before EIP-3651

    post = {
        measure_address: Account(
            storage={
                0x00: expected_gas,
            }
        )
    }
    tx = Transaction(
        ty=0x0,
        chain_id=0x0,
        nonce=0,
        to=measure_address,
        gas_limit=100000000,
        gas_price=10,
        protected=False,
    )

    state_test(
        env=env,
        pre=pre,
        post=post,
        txs=[tx],
        tag="opcode_" + opcode.lower(),
    )