Skip to content

Commit de60bdb

Browse files
committed
Add test for opcodes
Relates to #1111
1 parent 2d8b682 commit de60bdb

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

tests/core/opcodes/test_opcodes.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import pytest
2+
3+
from eth_utils import (
4+
to_canonical_address,
5+
)
6+
7+
from eth import (
8+
constants
9+
)
10+
from eth.vm import (
11+
opcode_values
12+
)
13+
from eth.vm.forks.byzantium.opcodes import (
14+
BYZANTIUM_OPCODES
15+
)
16+
from eth.vm.forks import (
17+
ByzantiumVM,
18+
SpuriousDragonVM,
19+
TangerineWhistleVM,
20+
HomesteadVM,
21+
FrontierVM,
22+
)
23+
24+
from eth.vm.message import (
25+
Message,
26+
)
27+
28+
29+
NORMALIZED_ADDRESS_A = "0x0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6"
30+
NORMALIZED_ADDRESS_B = "0xcd1722f3947def4cf144679da39c4c32bdc35681"
31+
CANONICAL_ADDRESS_A = to_canonical_address("0x0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6")
32+
CANONICAL_ADDRESS_B = to_canonical_address("0xcd1722f3947def4cf144679da39c4c32bdc35681")
33+
34+
35+
def prepare_computation(vm_class):
36+
37+
message = Message(
38+
to=CANONICAL_ADDRESS_A,
39+
sender=CANONICAL_ADDRESS_B,
40+
value=100,
41+
data=b'',
42+
code=b'',
43+
gas=800,
44+
)
45+
46+
tx_context = vm_class._state_class.transaction_context_class(
47+
gas_price=1,
48+
origin=CANONICAL_ADDRESS_B,
49+
)
50+
51+
computation = vm_class._state_class.computation_class(
52+
state=None,
53+
message=message,
54+
transaction_context=tx_context,
55+
)
56+
return computation
57+
58+
59+
@pytest.mark.parametrize(
60+
'vm_class, val1, val2, expected',
61+
(
62+
(ByzantiumVM, 2, 4, 6,),
63+
(SpuriousDragonVM, 2, 4, 6,),
64+
(TangerineWhistleVM, 2, 4, 6,),
65+
(HomesteadVM, 2, 4, 6,),
66+
(FrontierVM, 2, 4, 6,),
67+
)
68+
)
69+
def test_add(vm_class, val1, val2, expected):
70+
computation = prepare_computation(vm_class)
71+
computation.stack_push(val1)
72+
computation.stack_push(val2)
73+
computation.opcodes[opcode_values.ADD](computation)
74+
75+
result = computation.stack_pop(type_hint=constants.UINT256)
76+
77+
assert result == expected
78+
79+
80+
@pytest.mark.parametrize(
81+
'vm_class, val1, val2, expected',
82+
(
83+
(ByzantiumVM, 2, 2, 4,),
84+
(SpuriousDragonVM, 2, 2, 4,),
85+
(TangerineWhistleVM, 2, 2, 4,),
86+
(HomesteadVM, 2, 2, 4,),
87+
(FrontierVM, 2, 2, 4,),
88+
)
89+
)
90+
def test_mul(vm_class, val1, val2, expected):
91+
computation = prepare_computation(vm_class)
92+
computation.stack_push(val1)
93+
computation.stack_push(val2)
94+
computation.opcodes[opcode_values.MUL](computation)
95+
96+
result = computation.stack_pop(type_hint=constants.UINT256)
97+
98+
assert result == expected
99+
100+

0 commit comments

Comments
 (0)