ethereum.forks.bpo4.vm.instructions.stack

Ethereum Virtual Machine (EVM) Stack Instructions.

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

Introduction

Implementations of the EVM stack related instructions.

pop

Removes an item from the stack.

Parameters

evm : The current EVM frame.

def pop(evm: Evm) -> None:
28
    """
29
    Removes an item from the stack.
30
31
    Parameters
32
    ----------
33
    evm :
34
        The current EVM frame.
35
36
    """
37
    # STACK
38
    stack.pop(evm.stack)
39
40
    # GAS
41
    charge_gas(evm, GasCosts.OPCODE_POP)
42
43
    # OPERATION
44
    pass
45
46
    # PROGRAM COUNTER
47
    evm.pc += Uint(1)

push_n

Pushes an N-byte immediate onto the stack. Push zero if num_bytes is zero.

Parameters

evm : The current EVM frame.

num_bytes : The number of immediate bytes to be read from the code and pushed to the stack. Push zero if num_bytes is zero.

def push_n(evm: Evm, ​​num_bytes: int) -> None:
51
    """
52
    Pushes an N-byte immediate onto the stack. Push zero if num_bytes is zero.
53
54
    Parameters
55
    ----------
56
    evm :
57
        The current EVM frame.
58
59
    num_bytes :
60
        The number of immediate bytes to be read from the code and pushed to
61
        the stack. Push zero if num_bytes is zero.
62
63
    """
64
    # STACK
65
    pass
66
67
    # GAS
68
    if num_bytes == 0:
69
        charge_gas(evm, GasCosts.OPCODE_PUSH0)
70
    else:
71
        charge_gas(evm, GasCosts.OPCODE_PUSH)
72
73
    # OPERATION
74
    data_to_push = U256.from_be_bytes(
75
        buffer_read(evm.code, U256(evm.pc + Uint(1)), U256(num_bytes))
76
    )
77
    stack.push(evm.stack, data_to_push)
78
79
    # PROGRAM COUNTER
80
    evm.pc += Uint(1) + Uint(num_bytes)

dup_n

Duplicates the Nth stack item (from top of the stack) to the top of stack.

Parameters

evm : The current EVM frame.

item_number : The stack item number (0-indexed from top of stack) to be duplicated to the top of stack.

def dup_n(evm: Evm, ​​item_number: int) -> None:
84
    """
85
    Duplicates the Nth stack item (from top of the stack) to the top of stack.
86
87
    Parameters
88
    ----------
89
    evm :
90
        The current EVM frame.
91
92
    item_number :
93
        The stack item number (0-indexed from top of stack) to be duplicated
94
        to the top of stack.
95
96
    """
97
    # STACK
98
    pass
99
100
    # GAS
101
    charge_gas(evm, GasCosts.OPCODE_DUP)
102
    if item_number >= len(evm.stack):
103
        raise StackUnderflowError
104
    data_to_duplicate = evm.stack[len(evm.stack) - 1 - item_number]
105
    stack.push(evm.stack, data_to_duplicate)
106
107
    # PROGRAM COUNTER
108
    evm.pc += Uint(1)

swap_n

Swaps the top and the item_number element of the stack, where the top of the stack is position zero.

If item_number is zero, this function does nothing (which should not be possible, since there is no SWAP0 instruction).

Parameters

evm : The current EVM frame.

item_number : The stack item number (0-indexed from top of stack) to be swapped with the top of stack element.

def swap_n(evm: Evm, ​​item_number: int) -> None:
112
    """
113
    Swaps the top and the `item_number` element of the stack, where
114
    the top of the stack is position zero.
115
116
    If `item_number` is zero, this function does nothing (which should not be
117
    possible, since there is no `SWAP0` instruction).
118
119
    Parameters
120
    ----------
121
    evm :
122
        The current EVM frame.
123
124
    item_number :
125
        The stack item number (0-indexed from top of stack) to be swapped
126
        with the top of stack element.
127
128
    """
129
    # STACK
130
    pass
131
132
    # GAS
133
    charge_gas(evm, GasCosts.OPCODE_SWAP)
134
    if item_number >= len(evm.stack):
135
        raise StackUnderflowError
136
    evm.stack[-1], evm.stack[-1 - item_number] = (
137
        evm.stack[-1 - item_number],
138
        evm.stack[-1],
139
    )
140
141
    # PROGRAM COUNTER
142
    evm.pc += Uint(1)

push0

145
push0 = partial(push_n, num_bytes=0)

push1

146
push1 = partial(push_n, num_bytes=1)

push2

147
push2 = partial(push_n, num_bytes=2)

push3

148
push3 = partial(push_n, num_bytes=3)

push4

149
push4 = partial(push_n, num_bytes=4)

push5

150
push5 = partial(push_n, num_bytes=5)

push6

151
push6 = partial(push_n, num_bytes=6)

push7

152
push7 = partial(push_n, num_bytes=7)

push8

153
push8 = partial(push_n, num_bytes=8)

push9

154
push9 = partial(push_n, num_bytes=9)

push10

155
push10 = partial(push_n, num_bytes=10)

push11

156
push11 = partial(push_n, num_bytes=11)

push12

157
push12 = partial(push_n, num_bytes=12)

push13

158
push13 = partial(push_n, num_bytes=13)

push14

159
push14 = partial(push_n, num_bytes=14)

push15

160
push15 = partial(push_n, num_bytes=15)

push16

161
push16 = partial(push_n, num_bytes=16)

push17

162
push17 = partial(push_n, num_bytes=17)

push18

163
push18 = partial(push_n, num_bytes=18)

push19

164
push19 = partial(push_n, num_bytes=19)

push20

165
push20 = partial(push_n, num_bytes=20)

push21

166
push21 = partial(push_n, num_bytes=21)

push22

167
push22 = partial(push_n, num_bytes=22)

push23

168
push23 = partial(push_n, num_bytes=23)

push24

169
push24 = partial(push_n, num_bytes=24)

push25

170
push25 = partial(push_n, num_bytes=25)

push26

171
push26 = partial(push_n, num_bytes=26)

push27

172
push27 = partial(push_n, num_bytes=27)

push28

173
push28 = partial(push_n, num_bytes=28)

push29

174
push29 = partial(push_n, num_bytes=29)

push30

175
push30 = partial(push_n, num_bytes=30)

push31

176
push31 = partial(push_n, num_bytes=31)

push32

177
push32 = partial(push_n, num_bytes=32)

dup1

179
dup1 = partial(dup_n, item_number=0)

dup2

180
dup2 = partial(dup_n, item_number=1)

dup3

181
dup3 = partial(dup_n, item_number=2)

dup4

182
dup4 = partial(dup_n, item_number=3)

dup5

183
dup5 = partial(dup_n, item_number=4)

dup6

184
dup6 = partial(dup_n, item_number=5)

dup7

185
dup7 = partial(dup_n, item_number=6)

dup8

186
dup8 = partial(dup_n, item_number=7)

dup9

187
dup9 = partial(dup_n, item_number=8)

dup10

188
dup10 = partial(dup_n, item_number=9)

dup11

189
dup11 = partial(dup_n, item_number=10)

dup12

190
dup12 = partial(dup_n, item_number=11)

dup13

191
dup13 = partial(dup_n, item_number=12)

dup14

192
dup14 = partial(dup_n, item_number=13)

dup15

193
dup15 = partial(dup_n, item_number=14)

dup16

194
dup16 = partial(dup_n, item_number=15)

swap1

196
swap1 = partial(swap_n, item_number=1)

swap2

197
swap2 = partial(swap_n, item_number=2)

swap3

198
swap3 = partial(swap_n, item_number=3)

swap4

199
swap4 = partial(swap_n, item_number=4)

swap5

200
swap5 = partial(swap_n, item_number=5)

swap6

201
swap6 = partial(swap_n, item_number=6)

swap7

202
swap7 = partial(swap_n, item_number=7)

swap8

203
swap8 = partial(swap_n, item_number=8)

swap9

204
swap9 = partial(swap_n, item_number=9)

swap10

205
swap10 = partial(swap_n, item_number=10)

swap11

206
swap11 = partial(swap_n, item_number=11)

swap12

207
swap12 = partial(swap_n, item_number=12)

swap13

208
swap13 = partial(swap_n, item_number=13)

swap14

209
swap14 = partial(swap_n, item_number=14)

swap15

210
swap15 = partial(swap_n, item_number=15)

swap16

211
swap16 = partial(swap_n, item_number=16)