ethereum.forks.bpo5.vm.stackethereum.forks.amsterdam.vm.stack

Ethereum Virtual Machine (EVM) Stack.

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

Introduction

Implementation of the stack operators for the EVM.

decode_single

Decode the immediate byte for DUPN/SWAPN to get the stack index.

Return n with 17 <= n <= 235.

Parameters

x : int The immediate byte value (0-90 or 128-255).

Returns

int The stack index n, where 17 <= n <= 235.

Raises

InvalidParameter If x is in the forbidden range (90 < x < 128 or x > 255).

def decode_single(x: U8) -> U8:
26
    <snip>
47
    if not (U8(0) <= x <= U8(90) or U8(128) <= x <= U8(255)):
48
        raise InvalidParameter(
49
            f"DUPN/SWAPN immediate byte {x} is out of range. "
50
            "Valid range: 0 <= x <= 90 or 128 <= x <= 255"
51
        )
52
53
    return U8((int(x) + 145) % 256)

decode_pair

Decode the immediate byte for EXCHANGE to get two stack indices.

Return (n, m) with 1 <= n <= 14 and n < m <= 30 - n.

Parameters

x : int The immediate byte value (0-81 or 128-255).

Returns

Tuple[int, int] The two stack indices (n, m), where 1 <= n <= 14 and n < m <= 30 - n.

Raises

InvalidParameter If x is in the forbidden range (81 < x < 128 or x > 255).

def decode_pair(x: U8) -> Tuple[U8, U8]:
57
    <snip>
79
    if not (U8(0) <= x <= U8(81) or U8(128) <= x <= U8(255)):
80
        raise InvalidParameter(
81
            f"EXCHANGE immediate byte {x} is in the forbidden "
82
            "range 82 <= x <= 127\n"
83
            "Valid range: 0 <= x <= 81 or 128 <= x <= 255"
84
        )
85
86
    k = U8(int(x) ^ 143)
87
    q, r = divmod(k, U8(16))
88
    if q < r:
89
        return q + U8(1), r + U8(1)
90
    else:
91
        return r + U8(1), U8(29) - q

pop

Pops the top item off of stack.

Parameters

stack : EVM stack.

Returns

value : U256 The top element on the stack.

def pop(stack: List[U256]) -> U256:
95
    <snip>
109
    if len(stack) == 0:
110
        raise StackUnderflowError
111
112
    return stack.pop()

push

Pushes value onto stack.

Parameters

stack : EVM stack.

value : Item to be pushed onto stack.

def push(stack: List[U256], ​​value: U256) -> None:
116
    <snip>
128
    if len(stack) == 1024:
129
        raise StackOverflowError
130
131
    return stack.append(value)