ethereum.forks.cancun.vm.instructions.blockethereum.forks.prague.vm.instructions.block

Ethereum Virtual Machine (EVM) Block Instructions.

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

Introduction

Implementations of the EVM block instructions.

block_hash

Push the hash of one of the 256 most recent complete blocks onto the stack. The block number to hash is present at the top of the stack.

Parameters

evm : The current EVM frame.

Raises

:py:class:~ethereum.forks.cancun.vm.exceptions.StackUnderflowError~ethereum.forks.prague.vm.exceptions.StackUnderflowError If len(stack) is less than 1. :py:class:~ethereum.forks.cancun.vm.exceptions.OutOfGasError~ethereum.forks.prague.vm.exceptions.OutOfGasError If evm.gas_left is less than 20.

def block_hash(evm: Evm) -> None:
22
    <snip>
39
    # STACK
40
    block_number = Uint(pop(evm.stack))
41
42
    # GAS
43
    charge_gas(evm, GasCosts.OPCODE_BLOCKHASH)
44
45
    # OPERATION
46
    max_block_number = block_number + Uint(256)
47
    current_block_number = evm.message.block_env.number
48
    if (
49
        current_block_number <= block_number
50
        or current_block_number > max_block_number
51
    ):
52
        # Default hash to 0, if the block of interest is not yet on the chain
53
        # (including the block which has the current executing transaction),
54
        # or if the block's age is more than 256.
55
        current_block_hash = b"\x00"
56
    else:
57
        current_block_hash = evm.message.block_env.block_hashes[
58
            -(current_block_number - block_number)
59
        ]
60
61
    push(evm.stack, U256.from_be_bytes(current_block_hash))
62
63
    # PROGRAM COUNTER
64
    evm.pc += Uint(1)

coinbase

Push the current block's beneficiary address (address of the block miner) onto the stack.

Here the current block refers to the block in which the currently executing transaction/call resides.

Parameters

evm : The current EVM frame.

Raises

:py:class:~ethereum.forks.cancun.vm.exceptions.StackOverflowError~ethereum.forks.prague.vm.exceptions.StackOverflowError If len(stack) is equal to 1024. :py:class:~ethereum.forks.cancun.vm.exceptions.OutOfGasError~ethereum.forks.prague.vm.exceptions.OutOfGasError If evm.gas_left is less than 2.

def coinbase(evm: Evm) -> None:
68
    <snip>
88
    # STACK
89
    pass
90
91
    # GAS
92
    charge_gas(evm, GasCosts.OPCODE_COINBASE)
93
94
    # OPERATION
95
    push(evm.stack, U256.from_be_bytes(evm.message.block_env.coinbase))
96
97
    # PROGRAM COUNTER
98
    evm.pc += Uint(1)

timestamp

Push the current block's timestamp onto the stack. Here the timestamp being referred to is actually the unix timestamp in seconds.

Here the current block refers to the block in which the currently executing transaction/call resides.

Parameters

evm : The current EVM frame.

Raises

:py:class:~ethereum.forks.cancun.vm.exceptions.StackOverflowError~ethereum.forks.prague.vm.exceptions.StackOverflowError If len(stack) is equal to 1024. :py:class:~ethereum.forks.cancun.vm.exceptions.OutOfGasError~ethereum.forks.prague.vm.exceptions.OutOfGasError If evm.gas_left is less than 2.

def timestamp(evm: Evm) -> None:
102
    <snip>
122
    # STACK
123
    pass
124
125
    # GAS
126
    charge_gas(evm, GasCosts.OPCODE_TIMESTAMP)
127
128
    # OPERATION
129
    push(evm.stack, evm.message.block_env.time)
130
131
    # PROGRAM COUNTER
132
    evm.pc += Uint(1)

number

Push the current block's number onto the stack.

Here the current block refers to the block in which the currently executing transaction/call resides.

Parameters

evm : The current EVM frame.

Raises

:py:class:~ethereum.forks.cancun.vm.exceptions.StackOverflowError~ethereum.forks.prague.vm.exceptions.StackOverflowError If len(stack) is equal to 1024. :py:class:~ethereum.forks.cancun.vm.exceptions.OutOfGasError~ethereum.forks.prague.vm.exceptions.OutOfGasError If evm.gas_left is less than 2.

def number(evm: Evm) -> None:
136
    <snip>
155
    # STACK
156
    pass
157
158
    # GAS
159
    charge_gas(evm, GasCosts.OPCODE_NUMBER)
160
161
    # OPERATION
162
    push(evm.stack, U256(evm.message.block_env.number))
163
164
    # PROGRAM COUNTER
165
    evm.pc += Uint(1)

prev_randao

Push the prev_randao value onto the stack.

The prev_randao value is the random output of the beacon chain's randomness oracle for the previous block.

Parameters

evm : The current EVM frame.

Raises

:py:class:~ethereum.forks.cancun.vm.exceptions.StackOverflowError~ethereum.forks.prague.vm.exceptions.StackOverflowError If len(stack) is equal to 1024. :py:class:~ethereum.forks.cancun.vm.exceptions.OutOfGasError~ethereum.forks.prague.vm.exceptions.OutOfGasError If evm.gas_left is less than 2.

def prev_randao(evm: Evm) -> None:
169
    <snip>
188
    # STACK
189
    pass
190
191
    # GAS
192
    charge_gas(evm, GasCosts.OPCODE_PREVRANDAO)
193
194
    # OPERATION
195
    push(evm.stack, U256.from_be_bytes(evm.message.block_env.prev_randao))
196
197
    # PROGRAM COUNTER
198
    evm.pc += Uint(1)

gas_limit

Push the current block's gas limit onto the stack.

Here the current block refers to the block in which the currently executing transaction/call resides.

Parameters

evm : The current EVM frame.

Raises

:py:class:~ethereum.forks.cancun.vm.exceptions.StackOverflowError~ethereum.forks.prague.vm.exceptions.StackOverflowError If len(stack) is equal to 1024. :py:class:~ethereum.forks.cancun.vm.exceptions.OutOfGasError~ethereum.forks.prague.vm.exceptions.OutOfGasError If evm.gas_left is less than 2.

def gas_limit(evm: Evm) -> None:
202
    <snip>
221
    # STACK
222
    pass
223
224
    # GAS
225
    charge_gas(evm, GasCosts.OPCODE_GASLIMIT)
226
227
    # OPERATION
228
    push(evm.stack, U256(evm.message.block_env.block_gas_limit))
229
230
    # PROGRAM COUNTER
231
    evm.pc += Uint(1)

chain_id

Push the chain id onto the stack.

Parameters

evm : The current EVM frame.

Raises

:py:class:~ethereum.forks.cancun.vm.exceptions.StackOverflowError~ethereum.forks.prague.vm.exceptions.StackOverflowError If len(stack) is equal to 1024. :py:class:~ethereum.forks.cancun.vm.exceptions.OutOfGasError~ethereum.forks.prague.vm.exceptions.OutOfGasError If evm.gas_left is less than 2.

def chain_id(evm: Evm) -> None:
235
    <snip>
251
    # STACK
252
    pass
253
254
    # GAS
255
    charge_gas(evm, GasCosts.OPCODE_CHAINID)
256
257
    # OPERATION
258
    push(evm.stack, U256(evm.message.block_env.chain_id))
259
260
    # PROGRAM COUNTER
261
    evm.pc += Uint(1)