ethereum.shanghai.vm.instructions.blockethereum.cancun.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.shanghai.vm.exceptions.StackUnderflowError~ethereum.cancun.vm.exceptions.StackUnderflowError If len(stack) is less than 1. :py:class:~ethereum.shanghai.vm.exceptions.OutOfGasError~ethereum.cancun.vm.exceptions.OutOfGasError If evm.gas_left is less than 20.

def block_hash(evm: Evm) -> None:
23
    """
24
    Push the hash of one of the 256 most recent complete blocks onto the
25
    stack. The block number to hash is present at the top of the stack.
26
27
    Parameters
28
    ----------
29
    evm :
30
        The current EVM frame.
31
32
    Raises
33
    ------
34
    :py:class:`~ethereum.shanghai.vm.exceptions.StackUnderflowError`
34
    :py:class:`~ethereum.cancun.vm.exceptions.StackUnderflowError`
35
        If `len(stack)` is less than `1`.
36
    :py:class:`~ethereum.shanghai.vm.exceptions.OutOfGasError`
36
    :py:class:`~ethereum.cancun.vm.exceptions.OutOfGasError`
37
        If `evm.gas_left` is less than `20`.
38
    """
39
    # STACK
40
    block_number = pop(evm.stack)
41
42
    # GAS
43
    charge_gas(evm, GAS_BLOCK_HASH)
44
45
    # OPERATION
46
    if evm.env.number <= block_number or evm.env.number > block_number + 256:
47
        # Default hash to 0, if the block of interest is not yet on the chain
48
        # (including the block which has the current executing transaction),
49
        # or if the block's age is more than 256.
50
        hash = b"\x00"
51
    else:
52
        hash = evm.env.block_hashes[-(evm.env.number - block_number)]
53
54
    push(evm.stack, U256.from_be_bytes(hash))
55
56
    # PROGRAM COUNTER
57
    evm.pc += 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.shanghai.vm.exceptions.StackOverflowError~ethereum.cancun.vm.exceptions.StackOverflowError If len(stack) is equal to 1024. :py:class:~ethereum.shanghai.vm.exceptions.OutOfGasError~ethereum.cancun.vm.exceptions.OutOfGasError If evm.gas_left is less than 2.

def coinbase(evm: Evm) -> None:
61
    """
62
    Push the current block's beneficiary address (address of the block miner)
63
    onto the stack.
64
65
    Here the current block refers to the block in which the currently
66
    executing transaction/call resides.
67
68
    Parameters
69
    ----------
70
    evm :
71
        The current EVM frame.
72
73
    Raises
74
    ------
75
    :py:class:`~ethereum.shanghai.vm.exceptions.StackOverflowError`
75
    :py:class:`~ethereum.cancun.vm.exceptions.StackOverflowError`
76
        If `len(stack)` is equal to `1024`.
77
    :py:class:`~ethereum.shanghai.vm.exceptions.OutOfGasError`
77
    :py:class:`~ethereum.cancun.vm.exceptions.OutOfGasError`
78
        If `evm.gas_left` is less than `2`.
79
    """
80
    # STACK
81
    pass
82
83
    # GAS
84
    charge_gas(evm, GAS_BASE)
85
86
    # OPERATION
87
    push(evm.stack, U256.from_be_bytes(evm.env.coinbase))
88
89
    # PROGRAM COUNTER
90
    evm.pc += 1

timestamp

Push the current block's timestamp onto the stack. Here the timestamp being referred 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.shanghai.vm.exceptions.StackOverflowError~ethereum.cancun.vm.exceptions.StackOverflowError If len(stack) is equal to 1024. :py:class:~ethereum.shanghai.vm.exceptions.OutOfGasError~ethereum.cancun.vm.exceptions.OutOfGasError If evm.gas_left is less than 2.

def timestamp(evm: Evm) -> None:
94
    """
95
    Push the current block's timestamp onto the stack. Here the timestamp
96
    being referred is actually the unix timestamp in seconds.
97
98
    Here the current block refers to the block in which the currently
99
    executing transaction/call resides.
100
101
    Parameters
102
    ----------
103
    evm :
104
        The current EVM frame.
105
106
    Raises
107
    ------
108
    :py:class:`~ethereum.shanghai.vm.exceptions.StackOverflowError`
108
    :py:class:`~ethereum.cancun.vm.exceptions.StackOverflowError`
109
        If `len(stack)` is equal to `1024`.
110
    :py:class:`~ethereum.shanghai.vm.exceptions.OutOfGasError`
110
    :py:class:`~ethereum.cancun.vm.exceptions.OutOfGasError`
111
        If `evm.gas_left` is less than `2`.
112
    """
113
    # STACK
114
    pass
115
116
    # GAS
117
    charge_gas(evm, GAS_BASE)
118
119
    # OPERATION
120
    push(evm.stack, evm.env.time)
121
122
    # PROGRAM COUNTER
123
    evm.pc += 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.shanghai.vm.exceptions.StackOverflowError~ethereum.cancun.vm.exceptions.StackOverflowError If len(stack) is equal to 1024. :py:class:~ethereum.shanghai.vm.exceptions.OutOfGasError~ethereum.cancun.vm.exceptions.OutOfGasError If evm.gas_left is less than 2.

def number(evm: Evm) -> None:
127
    """
128
    Push the current block's number onto the stack.
129
130
    Here the current block refers to the block in which the currently
131
    executing transaction/call resides.
132
133
    Parameters
134
    ----------
135
    evm :
136
        The current EVM frame.
137
138
    Raises
139
    ------
140
    :py:class:`~ethereum.shanghai.vm.exceptions.StackOverflowError`
140
    :py:class:`~ethereum.cancun.vm.exceptions.StackOverflowError`
141
        If `len(stack)` is equal to `1024`.
142
    :py:class:`~ethereum.shanghai.vm.exceptions.OutOfGasError`
142
    :py:class:`~ethereum.cancun.vm.exceptions.OutOfGasError`
143
        If `evm.gas_left` is less than `2`.
144
    """
145
    # STACK
146
    pass
147
148
    # GAS
149
    charge_gas(evm, GAS_BASE)
150
151
    # OPERATION
152
    push(evm.stack, U256(evm.env.number))
153
154
    # PROGRAM COUNTER
155
    evm.pc += 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.shanghai.vm.exceptions.StackOverflowError~ethereum.cancun.vm.exceptions.StackOverflowError If len(stack) is equal to 1024. :py:class:~ethereum.shanghai.vm.exceptions.OutOfGasError~ethereum.cancun.vm.exceptions.OutOfGasError If evm.gas_left is less than 2.

def prev_randao(evm: Evm) -> None:
159
    """
160
    Push the `prev_randao` value onto the stack.
161
162
    The `prev_randao` value is the random output of the beacon chain's
163
    randomness oracle for the previous block.
164
165
    Parameters
166
    ----------
167
    evm :
168
        The current EVM frame.
169
170
    Raises
171
    ------
172
    :py:class:`~ethereum.shanghai.vm.exceptions.StackOverflowError`
172
    :py:class:`~ethereum.cancun.vm.exceptions.StackOverflowError`
173
        If `len(stack)` is equal to `1024`.
174
    :py:class:`~ethereum.shanghai.vm.exceptions.OutOfGasError`
174
    :py:class:`~ethereum.cancun.vm.exceptions.OutOfGasError`
175
        If `evm.gas_left` is less than `2`.
176
    """
177
    # STACK
178
    pass
179
180
    # GAS
181
    charge_gas(evm, GAS_BASE)
182
183
    # OPERATION
184
    push(evm.stack, U256.from_be_bytes(evm.env.prev_randao))
185
186
    # PROGRAM COUNTER
187
    evm.pc += 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.shanghai.vm.exceptions.StackOverflowError~ethereum.cancun.vm.exceptions.StackOverflowError If len(stack) is equal to 1024. :py:class:~ethereum.shanghai.vm.exceptions.OutOfGasError~ethereum.cancun.vm.exceptions.OutOfGasError If evm.gas_left is less than 2.

def gas_limit(evm: Evm) -> None:
191
    """
192
    Push the current block's gas limit onto the stack.
193
194
    Here the current block refers to the block in which the currently
195
    executing transaction/call resides.
196
197
    Parameters
198
    ----------
199
    evm :
200
        The current EVM frame.
201
202
    Raises
203
    ------
204
    :py:class:`~ethereum.shanghai.vm.exceptions.StackOverflowError`
204
    :py:class:`~ethereum.cancun.vm.exceptions.StackOverflowError`
205
        If `len(stack)` is equal to `1024`.
206
    :py:class:`~ethereum.shanghai.vm.exceptions.OutOfGasError`
206
    :py:class:`~ethereum.cancun.vm.exceptions.OutOfGasError`
207
        If `evm.gas_left` is less than `2`.
208
    """
209
    # STACK
210
    pass
211
212
    # GAS
213
    charge_gas(evm, GAS_BASE)
214
215
    # OPERATION
216
    push(evm.stack, U256(evm.env.gas_limit))
217
218
    # PROGRAM COUNTER
219
    evm.pc += 1

chain_id

Push the chain id onto the stack.

Parameters

evm : The current EVM frame.

Raises

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

def chain_id(evm: Evm) -> None:
223
    """
224
    Push the chain id onto the stack.
225
226
    Parameters
227
    ----------
228
    evm :
229
        The current EVM frame.
230
231
    Raises
232
    ------
233
    :py:class:`~ethereum.shanghai.vm.exceptions.StackOverflowError`
233
    :py:class:`~ethereum.cancun.vm.exceptions.StackOverflowError`
234
        If `len(stack)` is equal to `1024`.
235
    :py:class:`~ethereum.shanghai.vm.exceptions.OutOfGasError`
235
    :py:class:`~ethereum.cancun.vm.exceptions.OutOfGasError`
236
        If `evm.gas_left` is less than `2`.
237
    """
238
    # STACK
239
    pass
240
241
    # GAS
242
    charge_gas(evm, GAS_BASE)
243
244
    # OPERATION
245
    push(evm.stack, U256(evm.env.chain_id))
246
247
    # PROGRAM COUNTER
248
    evm.pc += 1