ethereum.forks.bogota.vm.instructions.environment

Ethereum Virtual Machine (EVM) Environmental Instructions.

.. contents:: Table of Contents :backlinks: none :local:

Introduction

Implementations of the EVM environment related instructions.

address

Pushes the address of the current executing account to the stack.

Parameters

evm : The current EVM frame.

def address(​evm: Evm​) -> None:
36
    <snip>
45
    # STACK
46
    pass
47
48
    # GAS
49
    charge_gas(evm, GasCosts.OPCODE_ADDRESS)
50
51
    # OPERATION
52
    push(evm.stack, U256.from_be_bytes(evm.current_target))
53
54
    # PROGRAM COUNTER
55
    evm.pc += Uint(1)

balance

Pushes the balance of the given account onto the stack.

Parameters

evm : The current EVM frame.

def balance(​evm: Evm​) -> None:
59
    <snip>
68
    # STACK
69
    address = to_address_masked(pop(evm.stack))
70
71
    # GAS
72
    if address in evm.accessed_addresses:
73
        charge_gas(evm, GasCosts.WARM_ACCESS)
74
    else:
75
        evm.accessed_addresses.add(address)
76
        charge_gas(evm, GasCosts.COLD_ACCOUNT_ACCESS)
77
78
    # OPERATION
79
    # Non-existent accounts default to EMPTY_ACCOUNT, which has balance 0.
80
    tx_state = evm.tx_env.state
81
    balance = get_account(tx_state, address).balance
82
83
    push(evm.stack, balance)
84
85
    # PROGRAM COUNTER
86
    evm.pc += Uint(1)

origin

Pushes the caller of the transaction's top-level call to the stack, at every call depth. In a frame transaction this is the executing frame's caller: FRAME_ENTRY_POINT in DEFAULT and VERIFY frames, and the sender — possibly a contract — in SENDER frames.

Parameters

evm : The current EVM frame.

def origin(​evm: Evm​) -> None:
90
    <snip>
102
    # STACK
103
    pass
104
105
    # GAS
106
    charge_gas(evm, GasCosts.OPCODE_ORIGIN)
107
108
    # OPERATION
109
    push(evm.stack, U256.from_be_bytes(evm.tx_env.origin))
110
111
    # PROGRAM COUNTER
112
    evm.pc += Uint(1)

caller

Pushes the address of the caller onto the stack.

Parameters

evm : The current EVM frame.

def caller(​evm: Evm​) -> None:
116
    <snip>
125
    # STACK
126
    pass
127
128
    # GAS
129
    charge_gas(evm, GasCosts.OPCODE_CALLER)
130
131
    # OPERATION
132
    push(evm.stack, U256.from_be_bytes(evm.caller))
133
134
    # PROGRAM COUNTER
135
    evm.pc += Uint(1)

callvalue

Push the value (in wei) sent with the call onto the stack.

Parameters

evm : The current EVM frame.

def callvalue(​evm: Evm​) -> None:
139
    <snip>
148
    # STACK
149
    pass
150
151
    # GAS
152
    charge_gas(evm, GasCosts.OPCODE_CALLVALUE)
153
154
    # OPERATION
155
    push(evm.stack, evm.value)
156
157
    # PROGRAM COUNTER
158
    evm.pc += Uint(1)

calldataload

Push a word (32 bytes) of the input data belonging to the current environment onto the stack.

Parameters

evm : The current EVM frame.

def calldataload(​evm: Evm​) -> None:
162
    <snip>
172
    # STACK
173
    start_index = pop(evm.stack)
174
175
    # GAS
176
    charge_gas(evm, GasCosts.OPCODE_CALLDATALOAD)
177
178
    # OPERATION
179
    value = buffer_read(evm.call_data, start_index, U256(32))
180
181
    push(evm.stack, U256.from_be_bytes(value))
182
183
    # PROGRAM COUNTER
184
    evm.pc += Uint(1)

calldatasize

Push the size of input data in current environment onto the stack.

Parameters

evm : The current EVM frame.

def calldatasize(​evm: Evm​) -> None:
188
    <snip>
197
    # STACK
198
    pass
199
200
    # GAS
201
    charge_gas(evm, GasCosts.OPCODE_CALLDATASIZE)
202
203
    # OPERATION
204
    push(evm.stack, U256(len(evm.call_data)))
205
206
    # PROGRAM COUNTER
207
    evm.pc += Uint(1)

calldatacopy

Copy a portion of the input data in current environment to memory.

This will also expand the memory, in case that the memory is insufficient to store the data.

Parameters

evm : The current EVM frame.

def calldatacopy(​evm: Evm​) -> None:
211
    <snip>
223
    # STACK
224
    memory_start_index = pop(evm.stack)
225
    data_start_index = pop(evm.stack)
226
    size = pop(evm.stack)
227
228
    # GAS
229
    words = ceil32(Uint(size)) // Uint(32)
230
    copy_gas_cost = ExecutionGas(GasCosts.OPCODE_COPY_PER_WORD * words)
231
    extend_memory = calculate_gas_extend_memory(
232
        evm.memory, [(memory_start_index, size)]
233
    )
234
    charge_gas(
235
        evm,
236
        GasCosts.OPCODE_CALLDATACOPY_BASE + copy_gas_cost + extend_memory.cost,
237
    )
238
239
    # OPERATION
240
    evm.memory += b"\x00" * extend_memory.expand_by
241
    value = buffer_read(evm.call_data, data_start_index, size)
242
    memory_write(evm.memory, memory_start_index, value)
243
244
    # PROGRAM COUNTER
245
    evm.pc += Uint(1)

codesize

Push the size of code running in current environment onto the stack.

Parameters

evm : The current EVM frame.

def codesize(​evm: Evm​) -> None:
249
    <snip>
258
    # STACK
259
    pass
260
261
    # GAS
262
    charge_gas(evm, GasCosts.OPCODE_CODESIZE)
263
264
    # OPERATION
265
    push(evm.stack, U256(len(evm.code)))
266
267
    # PROGRAM COUNTER
268
    evm.pc += Uint(1)

codecopy

Copy a portion of the code in current environment to memory.

This will also expand the memory, in case that the memory is insufficient to store the data.

Parameters

evm : The current EVM frame.

def codecopy(​evm: Evm​) -> None:
272
    <snip>
284
    # STACK
285
    memory_start_index = pop(evm.stack)
286
    code_start_index = pop(evm.stack)
287
    size = pop(evm.stack)
288
289
    # GAS
290
    words = ceil32(Uint(size)) // Uint(32)
291
    copy_gas_cost = ExecutionGas(GasCosts.OPCODE_COPY_PER_WORD * words)
292
    extend_memory = calculate_gas_extend_memory(
293
        evm.memory, [(memory_start_index, size)]
294
    )
295
    charge_gas(
296
        evm,
297
        GasCosts.OPCODE_CODECOPY_BASE + copy_gas_cost + extend_memory.cost,
298
    )
299
300
    # OPERATION
301
    evm.memory += b"\x00" * extend_memory.expand_by
302
    value = buffer_read(evm.code, code_start_index, size)
303
    memory_write(evm.memory, memory_start_index, value)
304
305
    # PROGRAM COUNTER
306
    evm.pc += Uint(1)

gasprice

Push the gas price used in current environment onto the stack.

Parameters

evm : The current EVM frame.

def gasprice(​evm: Evm​) -> None:
310
    <snip>
319
    # STACK
320
    pass
321
322
    # GAS
323
    charge_gas(evm, GasCosts.OPCODE_GASPRICE)
324
325
    # OPERATION
326
    push(evm.stack, U256(evm.tx_env.effective_gas_price))
327
328
    # PROGRAM COUNTER
329
    evm.pc += Uint(1)

extcodesize

Push the code size of a given account onto the stack.

Parameters

evm : The current EVM frame.

def extcodesize(​evm: Evm​) -> None:
333
    <snip>
342
    # STACK
343
    address = to_address_masked(pop(evm.stack))
344
345
    # GAS
346
    if address in evm.accessed_addresses:
347
        access_gas_cost = GasCosts.WARM_ACCESS
348
    else:
349
        evm.accessed_addresses.add(address)
350
        access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS
351
    access_gas_cost += GasCosts.WARM_ACCESS  # Code reading cost (EIP-8038)
352
    charge_gas(evm, access_gas_cost)
353
354
    # OPERATION
355
    tx_state = evm.tx_env.state
356
    code_hash = get_account(tx_state, address).code_hash
357
    code = get_code(tx_state, code_hash)
358
359
    codesize = U256(len(code))
360
    push(evm.stack, codesize)
361
362
    # PROGRAM COUNTER
363
    evm.pc += Uint(1)

extcodecopy

Copy a portion of an account's code to memory.

Parameters

evm : The current EVM frame.

def extcodecopy(​evm: Evm​) -> None:
367
    <snip>
376
    # STACK
377
    address = to_address_masked(pop(evm.stack))
378
    memory_start_index = pop(evm.stack)
379
    code_start_index = pop(evm.stack)
380
    size = pop(evm.stack)
381
382
    # GAS
383
    words = ceil32(Uint(size)) // Uint(32)
384
    copy_gas_cost = ExecutionGas(GasCosts.OPCODE_COPY_PER_WORD * words)
385
    extend_memory = calculate_gas_extend_memory(
386
        evm.memory, [(memory_start_index, size)]
387
    )
388
389
    if address in evm.accessed_addresses:
390
        access_gas_cost = GasCosts.WARM_ACCESS
391
    else:
392
        evm.accessed_addresses.add(address)
393
        access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS
394
    access_gas_cost += GasCosts.WARM_ACCESS  # Code reading cost (EIP-8038)
395
396
    total_gas_cost = access_gas_cost + copy_gas_cost + extend_memory.cost
397
398
    charge_gas(evm, total_gas_cost)
399
400
    # OPERATION
401
    evm.memory += b"\x00" * extend_memory.expand_by
402
    tx_state = evm.tx_env.state
403
    code_hash = get_account(tx_state, address).code_hash
404
    code = get_code(tx_state, code_hash)
405
406
    value = buffer_read(code, code_start_index, size)
407
    memory_write(evm.memory, memory_start_index, value)
408
409
    # PROGRAM COUNTER
410
    evm.pc += Uint(1)

returndatasize

Pushes the size of the return data buffer onto the stack.

Parameters

evm : The current EVM frame.

def returndatasize(​evm: Evm​) -> None:
414
    <snip>
423
    # STACK
424
    pass
425
426
    # GAS
427
    charge_gas(evm, GasCosts.OPCODE_RETURNDATASIZE)
428
429
    # OPERATION
430
    push(evm.stack, U256(len(evm.return_data)))
431
432
    # PROGRAM COUNTER
433
    evm.pc += Uint(1)

returndatacopy

Copies data from the return data buffer to memory.

Parameters

evm : The current EVM frame.

def returndatacopy(​evm: Evm​) -> None:
437
    <snip>
446
    # STACK
447
    memory_start_index = pop(evm.stack)
448
    return_data_start_position = pop(evm.stack)
449
    size = pop(evm.stack)
450
451
    # GAS
452
    words = ceil32(Uint(size)) // Uint(32)
453
    copy_gas_cost = ExecutionGas(
454
        GasCosts.OPCODE_RETURNDATACOPY_PER_WORD * words
455
    )
456
    extend_memory = calculate_gas_extend_memory(
457
        evm.memory, [(memory_start_index, size)]
458
    )
459
    charge_gas(
460
        evm,
461
        GasCosts.OPCODE_RETURNDATACOPY_BASE
462
        + copy_gas_cost
463
        + extend_memory.cost,
464
    )
465
    if Uint(return_data_start_position) + Uint(size) > ulen(evm.return_data):
466
        raise OutOfBoundsRead
467
468
    evm.memory += b"\x00" * extend_memory.expand_by
469
    value = evm.return_data[
470
        return_data_start_position : return_data_start_position + size
471
    ]
472
    memory_write(evm.memory, memory_start_index, value)
473
474
    # PROGRAM COUNTER
475
    evm.pc += Uint(1)

extcodehash

Returns the keccak256 hash of a contract’s bytecode.

Parameters

evm : The current EVM frame.

def extcodehash(​evm: Evm​) -> None:
479
    <snip>
488
    # STACK
489
    address = to_address_masked(pop(evm.stack))
490
491
    # GAS
492
    if address in evm.accessed_addresses:
493
        access_gas_cost = GasCosts.WARM_ACCESS
494
    else:
495
        evm.accessed_addresses.add(address)
496
        access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS
497
498
    charge_gas(evm, access_gas_cost)
499
500
    # OPERATION
501
    tx_state = evm.tx_env.state
502
    account = get_account(tx_state, address)
503
504
    if account == EMPTY_ACCOUNT:
505
        codehash = U256(0)
506
    else:
507
        codehash = U256.from_be_bytes(account.code_hash)
508
509
    push(evm.stack, codehash)
510
511
    # PROGRAM COUNTER
512
    evm.pc += Uint(1)

self_balance

Pushes the balance of the current address to the stack.

Parameters

evm : The current EVM frame.

def self_balance(​evm: Evm​) -> None:
516
    <snip>
525
    # STACK
526
    pass
527
528
    # GAS
529
    charge_gas(evm, GasCosts.OPCODE_SELFBALANCE)
530
531
    # OPERATION
532
    # Non-existent accounts default to EMPTY_ACCOUNT, which has balance 0.
533
    balance = get_account(evm.tx_env.state, evm.current_target).balance
534
535
    push(evm.stack, balance)
536
537
    # PROGRAM COUNTER
538
    evm.pc += Uint(1)

base_fee

Pushes the base fee of the current block on to the stack.

Parameters

evm : The current EVM frame.

def base_fee(​evm: Evm​) -> None:
542
    <snip>
551
    # STACK
552
    pass
553
554
    # GAS
555
    charge_gas(evm, GasCosts.OPCODE_BASEFEE)
556
557
    # OPERATION
558
    push(evm.stack, U256(evm.block_env.base_fee_per_gas))
559
560
    # PROGRAM COUNTER
561
    evm.pc += Uint(1)

blob_hash

Pushes the versioned hash at a particular index on to the stack.

Parameters

evm : The current EVM frame.

def blob_hash(​evm: Evm​) -> None:
565
    <snip>
574
    # STACK
575
    index = pop(evm.stack)
576
577
    # GAS
578
    charge_gas(evm, GasCosts.OPCODE_BLOBHASH)
579
580
    # OPERATION
581
    if int(index) < len(evm.tx_env.blob_versioned_hashes):
582
        blob_hash = evm.tx_env.blob_versioned_hashes[index]
583
    else:
584
        blob_hash = Bytes32(b"\x00" * 32)
585
    push(evm.stack, U256.from_be_bytes(blob_hash))
586
587
    # PROGRAM COUNTER
588
    evm.pc += Uint(1)

blob_base_fee

Pushes the blob base fee on to the stack.

Parameters

evm : The current EVM frame.

def blob_base_fee(​evm: Evm​) -> None:
592
    <snip>
601
    # STACK
602
    pass
603
604
    # GAS
605
    charge_gas(evm, GasCosts.OPCODE_BLOBBASEFEE)
606
607
    # OPERATION
608
    blob_base_fee = calculate_blob_gas_price(evm.block_env.excess_blob_gas)
609
    push(evm.stack, U256(blob_base_fee))
610
611
    # PROGRAM COUNTER
612
    evm.pc += Uint(1)