-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathtest_EVMAssembler.py
191 lines (162 loc) · 7.63 KB
/
test_EVMAssembler.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import sys
import unittest
import pyevmasm as EVMAsm
def int_to_bytes(i):
if sys.version_info[0] >= 3:
return i.to_bytes(1, "little")
else:
return bytes(chr(i))
# noinspection PyPep8Naming
class EVMTest_Assembler(unittest.TestCase):
_multiprocess_can_split_ = True
maxDiff = None
def test_ADD_1(self):
instruction = EVMAsm.disassemble_one(b"\x60\x10")
self.assertEqual(
EVMAsm.Instruction(0x60, "PUSH", 1, 0, 1, 3, "Place 1 byte item on stack.", 16, 0), instruction
)
instruction = EVMAsm.assemble_one("PUSH1 0x10")
self.assertEqual(
instruction, EVMAsm.Instruction(0x60, "PUSH", 1, 0, 1, 3, "Place 1 byte item on stack.", 16, 0)
)
instructions1 = EVMAsm.disassemble_all(b"\x30\x31")
instructions2 = EVMAsm.assemble_all("ADDRESS\nBALANCE")
self.assertTrue(all(a == b for a, b in zip(instructions1, instructions2)))
# High level simple assembler/disassembler
bytecode = EVMAsm.assemble_hex(
"""PUSH1 0x80
BLOCKHASH
MSTORE
PUSH1 0x2
PUSH2 0x100
"""
)
self.assertEqual(bytecode, "0x608040526002610100")
asmcode = EVMAsm.disassemble_hex("0x608040526002610100")
self.assertEqual(asmcode, """PUSH1 0x80\nBLOCKHASH\nMSTORE\nPUSH1 0x2\nPUSH2 0x100""")
def test_STOP(self):
insn = EVMAsm.disassemble_one(b"\x00")
self.assertTrue(insn.mnemonic == "STOP")
def test_JUMPI(self):
insn = EVMAsm.disassemble_one(b"\x57")
self.assertTrue(insn.mnemonic == "JUMPI")
self.assertTrue(insn.is_branch)
def test_pre_byzantium(self):
insn = EVMAsm.disassemble_one(b"\x57", fork="frontier")
self.assertTrue(insn.mnemonic == "JUMPI")
self.assertTrue(insn.is_branch)
insn = EVMAsm.disassemble_one(b"\xfa", fork="frontier")
self.assertTrue(insn.mnemonic == "INVALID") # STATICCALL added in byzantium
insn = EVMAsm.disassemble_one(b"\xfd", fork="frontier")
self.assertTrue(insn.mnemonic == "INVALID") # REVERT added in byzantium
def test_byzantium_fork(self):
insn = EVMAsm.disassemble_one(b"\x57", fork="byzantium")
self.assertTrue(insn.mnemonic == "JUMPI")
self.assertTrue(insn.is_branch)
insn = EVMAsm.disassemble_one(b"\x1b", fork="byzantium")
self.assertTrue(insn.mnemonic == "INVALID") # SHL added in constantinople
insn = EVMAsm.disassemble_one(b"\x1c", fork="byzantium")
self.assertTrue(insn.mnemonic == "INVALID") # SHR added in constantinople
insn = EVMAsm.disassemble_one(b"\x1d", fork="byzantium")
self.assertTrue(insn.mnemonic == "INVALID") # SAR added in constantinople
insn = EVMAsm.disassemble_one(b"\x3f", fork="byzantium")
self.assertTrue(insn.mnemonic == "INVALID") # EXTCODEHASH added in constantinople
insn = EVMAsm.disassemble_one(b"\xf5", fork="byzantium")
self.assertTrue(insn.mnemonic == "INVALID") # CREATE2 added in constantinople
def test_constantinople_fork(self):
insn = EVMAsm.disassemble_one(b"\x1b", fork="constantinople")
self.assertTrue(insn.mnemonic == "SHL")
self.assertTrue(insn.is_arithmetic)
insn = EVMAsm.disassemble_one(b"\x1c", fork="constantinople")
self.assertTrue(insn.mnemonic == "SHR")
self.assertTrue(insn.is_arithmetic)
insn = EVMAsm.disassemble_one(b"\x1d", fork="constantinople")
self.assertTrue(insn.mnemonic == "SAR")
self.assertTrue(insn.is_arithmetic)
insn = EVMAsm.disassemble_one(b"\x3f", fork="constantinople")
self.assertTrue(insn.mnemonic == "EXTCODEHASH")
insn = EVMAsm.disassemble_one(b"\xf5", fork="constantinople")
self.assertTrue(insn.mnemonic == "CREATE2")
def test_istanbul_fork(self):
insn = EVMAsm.disassemble_one(b"\x31", fork="istanbul")
self.assertTrue(insn.mnemonic == "BALANCE")
self.assertTrue(insn.fee == 700)
self.assertTrue(insn.pops == 1)
self.assertTrue(insn.pushes == 1)
insn = EVMAsm.disassemble_one(b"\x3f", fork="istanbul")
self.assertTrue(insn.mnemonic == "EXTCODEHASH")
self.assertTrue(insn.fee == 700)
self.assertTrue(insn.pops == 1)
self.assertTrue(insn.pushes == 1)
insn = EVMAsm.disassemble_one(b"\x46", fork="istanbul")
self.assertTrue(insn.mnemonic == "CHAINID")
self.assertTrue(insn.fee == 2)
self.assertTrue(insn.pops == 0)
self.assertTrue(insn.pushes == 1)
insn = EVMAsm.disassemble_one(b"\x47", fork="istanbul")
self.assertTrue(insn.mnemonic == "SELFBALANCE")
self.assertTrue(insn.fee == 5)
self.assertTrue(insn.pops == 0)
self.assertTrue(insn.pushes == 1)
insn = EVMAsm.disassemble_one(b"\x54", fork="istanbul")
self.assertTrue(insn.mnemonic == "SLOAD")
self.assertTrue(insn.fee == 800)
self.assertTrue(insn.pops == 1)
self.assertTrue(insn.pushes == 1)
def test_london_fork(self):
insn = EVMAsm.disassemble_one(b"\x48", fork="london")
self.assertTrue(insn.mnemonic == "BASEFEE")
self.assertTrue(insn.fee == 2)
self.assertTrue(insn.pops == 0)
self.assertTrue(insn.pushes == 1)
def test_shanghai_fork(self):
insn = EVMAsm.disassemble_one(b"\x5f", fork="shanghai")
self.assertTrue(insn.mnemonic == "PUSH0")
self.assertTrue(insn.fee == 2)
self.assertTrue(insn.pops == 0)
self.assertTrue(insn.pushes == 1)
self.assertTrue(insn.operand_size == 0)
def test_dencun_fork(self):
insn = EVMAsm.disassemble_one(b"\x5e", fork="dencun")
self.assertTrue(insn.mnemonic == "MCOPY")
self.assertTrue(insn.fee == 3)
self.assertTrue(insn.pops == 3)
self.assertTrue(insn.pushes == 0)
self.assertTrue(insn.operand_size == 0)
insn = EVMAsm.disassemble_one(b"\x49", fork="dencun")
self.assertTrue(insn.mnemonic == "BLOBHASH")
insn = EVMAsm.disassemble_one(b"\x4a", fork="dencun")
self.assertTrue(insn.mnemonic == "BLOBBASEFEE")
insn = EVMAsm.disassemble_one(b"\x5c", fork="dencun")
self.assertTrue(insn.mnemonic=="TLOAD")
insn = EVMAsm.disassemble_one(b"\x5d", fork="dencun")
self.assertTrue(insn.mnemonic=="TSTORE")
def test_assemble_DUP1_regression(self):
insn = EVMAsm.assemble_one("DUP1")
self.assertEqual(insn.mnemonic, "DUP1")
self.assertEqual(insn.opcode, 0x80)
def test_assemble_LOGX_regression(self):
inst_table = EVMAsm.instruction_tables[EVMAsm.DEFAULT_FORK]
log0_opcode = 0xA0
for n in range(5):
opcode = log0_opcode + n
self.assertTrue(opcode in inst_table, "{!r} not in instruction_table".format(opcode))
asm = "LOG" + str(n)
self.assertTrue(asm in inst_table, "{!r} not in instruction_table".format(asm))
insn = EVMAsm.assemble_one(asm)
self.assertEqual(insn.mnemonic, asm)
self.assertEqual(insn.opcode, opcode)
def test_consistency_assembler_disassembler(self):
"""
Tests whether every opcode that can be disassembled, can also be
assembled again.
"""
inst_table = EVMAsm.instruction_tables[EVMAsm.DEFAULT_FORK]
for opcode in inst_table.keys():
b = int_to_bytes(opcode) + b"\x00" * 32
inst_dis = EVMAsm.disassemble_one(b)
a = str(inst_dis)
inst_as = EVMAsm.assemble_one(a)
self.assertEqual(inst_dis, inst_as)
if __name__ == "__main__":
unittest.main()