/
test_virtual_machine.py
207 lines (169 loc) · 5.83 KB
/
test_virtual_machine.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import os
import pytest
from evm.db import (
get_db_backend,
)
from evm.db.chain import BaseChainDB
from eth_utils import (
keccak,
)
from evm.exceptions import (
VMError,
)
from evm.rlp.headers import (
BlockHeader,
)
from evm.vm.forks import (
HomesteadVM,
)
from evm.vm import (
Message,
Computation,
)
from evm.utils.fixture_tests import (
normalize_vmtest_fixture,
generate_fixture_tests,
load_fixture,
filter_fixtures,
setup_state_db,
verify_state_db,
hash_log_entries,
)
ROOT_PROJECT_DIR = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
BASE_FIXTURE_PATH = os.path.join(ROOT_PROJECT_DIR, 'fixtures', 'VMTests')
def vm_fixture_mark_fn(fixture_path, fixture_name):
if fixture_path.startswith('vmPerformance'):
return pytest.mark.skip('Performance tests are really slow')
elif fixture_path == 'vmSystemOperations/createNameRegistrator.json':
return pytest.mark.skip(
'Skipped in go-ethereum due to failure without parallel processing'
)
def pytest_generate_tests(metafunc):
generate_fixture_tests(
metafunc=metafunc,
base_fixture_path=BASE_FIXTURE_PATH,
filter_fn=filter_fixtures(
fixtures_base_dir=BASE_FIXTURE_PATH,
mark_fn=vm_fixture_mark_fn,
)
)
@pytest.fixture
def fixture(fixture_data):
fixture_path, fixture_key = fixture_data
fixture = load_fixture(
fixture_path,
fixture_key,
normalize_vmtest_fixture,
)
return fixture
#
# Testing Overrides
#
def apply_message_for_testing(self, message):
"""
For VM tests, we don't actually apply messages.
"""
computation = Computation(
vm=self,
message=message,
)
return computation
def apply_create_message_for_testing(self, message):
"""
For VM tests, we don't actually apply messages.
"""
computation = Computation(
vm=self,
message=message,
)
return computation
def get_block_hash_for_testing(self, block_number):
if block_number >= self.block.header.block_number:
return b''
elif block_number < self.block.header.block_number - 256:
return b''
else:
return keccak("{0}".format(block_number))
HomesteadVMForTesting = HomesteadVM.configure(
name='HomesteadVMForTesting',
apply_message=apply_create_message_for_testing,
apply_create_message=apply_create_message_for_testing,
get_ancestor_hash=get_block_hash_for_testing,
)
@pytest.fixture(params=['Frontier', 'Homestead', 'EIP150', 'SpuriousDragon'])
def vm_class(request):
if request.param == 'Frontier':
pytest.skip('Only the Homestead VM rules are currently supported')
elif request.param == 'Homestead':
return HomesteadVMForTesting
elif request.param == 'EIP150':
pytest.skip('Only the Homestead VM rules are currently supported')
elif request.param == 'SpuriousDragon':
pytest.skip('Only the Homestead VM rules are currently supported')
else:
assert False, "Unsupported VM: {0}".format(request.param)
def test_vm_fixtures(fixture, vm_class):
chaindb = BaseChainDB(get_db_backend())
header = BlockHeader(
coinbase=fixture['env']['currentCoinbase'],
difficulty=fixture['env']['currentDifficulty'],
block_number=fixture['env']['currentNumber'],
gas_limit=fixture['env']['currentGasLimit'],
timestamp=fixture['env']['currentTimestamp'],
)
vm = vm_class(header=header, chaindb=chaindb)
with vm.state_db() as state_db:
setup_state_db(fixture['pre'], state_db)
code = state_db.get_code(fixture['exec']['address'])
message = Message(
origin=fixture['exec']['origin'],
to=fixture['exec']['address'],
sender=fixture['exec']['caller'],
value=fixture['exec']['value'],
data=fixture['exec']['data'],
code=code,
gas=fixture['exec']['gas'],
gas_price=fixture['exec']['gasPrice'],
)
computation = vm.apply_computation(message)
if 'post' in fixture:
#
# Success checks
#
assert not computation.is_error
log_entries = computation.get_log_entries()
if 'logs' in fixture:
actual_logs_hash = hash_log_entries(log_entries)
expected_logs_hash = fixture['logs']
assert expected_logs_hash == actual_logs_hash
elif log_entries:
raise AssertionError("Got log entries: {0}".format(log_entries))
expected_output = fixture['out']
assert computation.output == expected_output
gas_meter = computation.gas_meter
expected_gas_remaining = fixture['gas']
actual_gas_remaining = gas_meter.gas_remaining
gas_delta = actual_gas_remaining - expected_gas_remaining
assert gas_delta == 0, "Gas difference: {0}".format(gas_delta)
call_creates = fixture.get('callcreates', [])
assert len(computation.children) == len(call_creates)
call_creates = fixture.get('callcreates', [])
for child_computation, created_call in zip(computation.children, call_creates):
to_address = created_call['destination']
data = created_call['data']
gas_limit = created_call['gasLimit']
value = created_call['value']
assert child_computation.msg.to == to_address
assert data == child_computation.msg.data or child_computation.msg.code
assert gas_limit == child_computation.msg.gas
assert value == child_computation.msg.value
post_state = fixture['post']
else:
#
# Error checks
#
assert computation.is_error
assert isinstance(computation._error, VMError)
post_state = fixture['pre']
with vm.state_db(read_only=True) as state_db:
verify_state_db(post_state, state_db)