ethereum.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
    """
27
    Decode the immediate byte for DUPN/SWAPN to get the stack index.
28
29
    Return n with 17 <= n <= 235.
30
31
    Parameters
32
    ----------
33
    x : int
34
        The immediate byte value (0-90 or 128-255).
35
36
    Returns
37
    -------
38
    int
39
        The stack index n, where 17 <= n <= 235.
40
41
    Raises
42
    ------
43
    InvalidParameter
44
        If x is in the forbidden range (90 < x < 128 or x > 255).
45
46
    """
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
    """
58
    Decode the immediate byte for EXCHANGE to get two stack indices.
59
60
    Return (n, m) with 1 <= n <= 14 and n < m <= 30 - n.
61
62
    Parameters
63
    ----------
64
    x : int
65
        The immediate byte value (0-81 or 128-255).
66
67
    Returns
68
    -------
69
    Tuple[int, int]
70
        The two stack indices (n, m), where
71
        1 <= n <= 14 and n < m <= 30 - n.
72
73
    Raises
74
    ------
75
    InvalidParameter
76
        If x is in the forbidden range (81 < x < 128 or x > 255).
77
78
    """
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
    """
96
    Pops the top item off of `stack`.
97
98
    Parameters
99
    ----------
100
    stack :
101
        EVM stack.
102
103
    Returns
104
    -------
105
    value : `U256`
106
        The top element on the stack.
107
108
    """
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
    """
117
    Pushes `value` onto `stack`.
118
119
    Parameters
120
    ----------
121
    stack :
122
        EVM stack.
123
124
    value :
125
        Item to be pushed onto `stack`.
126
127
    """
128
    if len(stack) == 1024:
129
        raise StackOverflowError
130
131
    return stack.append(value)