-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathtest_swap.py
129 lines (104 loc) · 3.76 KB
/
test_swap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
"""
A State test for the set of `SWAP*` opcodes.
Ported from: https://github.com/ethereum/tests/
blob/develop/src/GeneralStateTestsFiller/VMTests/vmTests/swapFiller.yml.
"""
import pytest # noqa: I001
from ethereum_test_forks import Fork, Frontier, Homestead
from ethereum_test_tools import Account, Alloc, Bytecode, Environment
from ethereum_test_tools import Opcodes as Op
from ethereum_test_tools import StateTestFiller, Storage, Transaction
@pytest.mark.parametrize(
"swap_opcode",
[getattr(Op, f"SWAP{i}") for i in range(1, 17)],
ids=lambda op: str(op),
)
@pytest.mark.valid_from("Frontier")
def test_swap(
state_test: StateTestFiller,
fork: Fork,
pre: Alloc,
swap_opcode: Op
):
"""
The set of `SWAP*` opcodes swaps the top of the stack with a specific
element.
In this test, we ensure that the set of `SWAP*` opcodes correctly swaps
the top element with the nth element and stores the result in storage.
"""
env = Environment()
# Calculate which position we're swapping with (1-based index)
swap_pos = swap_opcode.int() - 0x90 + 1
# Generate stack values
stack_values = list(range(swap_pos + 1))
# Push the stack values onto the stack (in reverse order).
contract_code = Bytecode()
for value in reversed(stack_values):
contract_code += Op.PUSH1(value)
# Perform the SWAP operation.
contract_code += swap_opcode
# Store the top of the stack in storage slot 0.
contract_code += Op.PUSH1(0) + Op.SSTORE
# Deploy the contract with the generated bytecode.
contract = pre.deploy_contract(contract_code)
# Create a transaction to execute the contract.
tx = Transaction(
sender=pre.fund_eoa(),
to=contract,
gas_limit=500_000,
protected=False if fork in [Frontier, Homestead] else True,
)
# After SWAP, the top value will be the one at swap_pos
expected_value = swap_pos
# Define the expected post-state.
post = {}
storage = Storage()
storage.store_next(expected_value, f"SWAP{swap_pos} result")
post[contract] = Account(storage=storage)
# Run the state test.
state_test(env=env, pre=pre, post=post, tx=tx)
@pytest.mark.parametrize(
"swap_opcode",
[getattr(Op, f"SWAP{i}") for i in range(1, 17)],
ids=lambda op: str(op),
)
@pytest.mark.valid_from("Frontier")
def test_stack_underflow(
state_test: StateTestFiller,
fork: Fork,
pre: Alloc,
swap_opcode: Op,
):
"""
A test to ensure that the stack underflow when there are not enough
elements for the `SWAP*` opcode to operate.
For each SWAPn operation, we push exactly (n-1) elements to cause an
underflow when trying to swap with the nth element.
"""
env = Environment()
# Calculate which position we're swapping with (1-based index)
swap_pos = swap_opcode.int() - 0x90 + 1
# Push exactly (n-1) elements for SWAPn to cause underflow
contract_code = Bytecode()
for i in range(swap_pos - 1):
contract_code += Op.PUSH1(i % 256)
# Attempt to perform the SWAP operation
contract_code += swap_opcode
# Store the top of the stack in storage slot 0
contract_code += Op.PUSH1(0) + Op.SSTORE
# Deploy the contract with the generated bytecode.
contract = pre.deploy_contract(contract_code)
# Create a transaction to execute the contract.
tx = Transaction(
sender=pre.fund_eoa(),
to=contract,
gas_limit=500_000,
protected=False if fork in [Frontier, Homestead] else True,
)
# Define the expected post-state.
post = {}
storage = Storage()
storage.store_next(0, f"SWAP{swap_pos} failed due to stack underflow")
post[contract] = Account(storage=storage)
# Run the state test.
state_test(env=env, pre=pre, post=post, tx=tx)