Skip to content

Commit 26b31fd

Browse files
authoredAug 6, 2019
Merge pull request #34 from f0rki/reversetable_fix
Fixed wrong pushes/pops when building reverse table.
2 parents f609c22 + 89a8d5d commit 26b31fd

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed
 

‎pyevmasm/evmasm.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ def __init__(self, *args, **kwargs):
639639
def _name_to_opcode(self):
640640
if self.__name_to_opcode is None:
641641
self.__name_to_opcode = {}
642-
for opcode, (name, operand_size, pushes, pops, gas, description) in self._instruction_list.items():
642+
for opcode, (name, operand_size, pops, pushes, gas, description) in self._instruction_list.items():
643643
if name == 'PUSH':
644644
long_name = 'PUSH%d' % operand_size
645645
elif name == 'DUP':

‎tests/test_EVMAssembler.py

+39
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1+
import sys
12
import unittest
23

34
import pyevmasm as EVMAsm
45

56

7+
def int_to_bytes(i):
8+
if sys.version_info[0] >= 3:
9+
return i.to_bytes(1, 'little')
10+
else:
11+
return bytes(chr(i))
12+
13+
614
# noinspection PyPep8Naming
715
class EVMTest_Assembler(unittest.TestCase):
816
_multiprocess_can_split_ = True
@@ -83,5 +91,36 @@ def test_constantinople_fork(self):
8391
insn = EVMAsm.disassemble_one(b'\xf5', fork='constantinople')
8492
self.assertTrue(insn.mnemonic == 'CREATE2')
8593

94+
def test_assemble_DUP1_regression(self):
95+
insn = EVMAsm.assemble_one("DUP1")
96+
self.assertEqual(insn.mnemonic, "DUP1")
97+
self.assertEqual(insn.opcode, 0x80)
98+
99+
def test_assemble_LOGX_regression(self):
100+
inst_table = EVMAsm.instruction_tables[EVMAsm.DEFAULT_FORK]
101+
log0_opcode = 0xa0
102+
for n in range(5):
103+
opcode = log0_opcode + n
104+
self.assertTrue(opcode in inst_table, "{!r} not in instruction_table".format(opcode))
105+
asm = "LOG" + str(n)
106+
self.assertTrue(asm in inst_table, "{!r} not in instruction_table".format(asm))
107+
insn = EVMAsm.assemble_one(asm)
108+
self.assertEqual(insn.mnemonic, asm)
109+
self.assertEqual(insn.opcode, opcode)
110+
111+
def test_consistency_assembler_disassembler(self):
112+
"""
113+
Tests whether every opcode that can be disassembled, can also be
114+
assembled again.
115+
"""
116+
inst_table = EVMAsm.instruction_tables[EVMAsm.DEFAULT_FORK]
117+
for opcode in inst_table.keys():
118+
b = int_to_bytes(opcode) + b"\x00" * 32
119+
inst_dis = EVMAsm.disassemble_one(b)
120+
a = str(inst_dis)
121+
inst_as = EVMAsm.assemble_one(a)
122+
self.assertEqual(inst_dis, inst_as)
123+
124+
86125
if __name__ == '__main__':
87126
unittest.main()

0 commit comments

Comments
 (0)
Please sign in to comment.