Skip to content
This repository was archived by the owner on May 23, 2023. It is now read-only.

Commit 6297068

Browse files
committed
test compatibility
1 parent 29c77b6 commit 6297068

File tree

4 files changed

+82
-44
lines changed

4 files changed

+82
-44
lines changed

ethereum/processblock.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ def _apply_msg(ext, msg, code):
214214
gas=msg.gas, value=msg.value,
215215
data=encode_hex(msg.data.extract_all()))
216216
if log_state.is_active:
217-
log_state.trace('MSG PRE STATE', account=msg.to, state=ext.log_storage(msg.to))
217+
log_state.trace('MSG PRE STATE', account=msg.to, bal=ext.get_balance(msg.to), state=ext.log_storage(msg.to))
218218
# Transfer value, instaquit if not enough
219219
snapshot = ext._block.snapshot()
220220
o = ext._block.transfer_value(msg.sender, msg.to, msg.value)
@@ -233,7 +233,7 @@ def _apply_msg(ext, msg, code):
233233
log_msg.debug('MSG APPLIED', result=o, gas_remained=gas,
234234
sender=msg.sender, to=msg.to, data=dat)
235235
if log_state.is_active:
236-
log_state.trace('MSG POST STATE', account=msg.to, state=ext.log_storage(msg.to))
236+
log_state.trace('MSG POST STATE', account=msg.to, bal=ext.get_balance(msg.to), state=ext.log_storage(msg.to))
237237

238238
if res == 0:
239239
log_msg.debug('REVERTING')
@@ -257,7 +257,9 @@ def create_contract(ext, msg):
257257
ext._block.reset_storage(msg.to)
258258
msg.is_create = True
259259
# assert not ext.get_code(msg.to)
260-
res, gas, dat = _apply_msg(ext, msg, msg.data.extract_all())
260+
code = msg.data.extract_all()
261+
msg.data = vm.CallData([], 0, 0)
262+
res, gas, dat = _apply_msg(ext, msg, code)
261263
assert utils.is_numeric(gas)
262264

263265
if res:

ethereum/tests/test_state.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
# hint: use 'py.test' with the '-s' option to dump logs to the console
1010
if '--trace' in sys.argv: # not default
1111
# configure_logging(':trace')
12-
sys.argv.remove('trace')
12+
sys.argv.remove('--trace')
1313

1414

1515
# SETUP TESTS IN GLOBAL NAME SPACE

ethereum/testutils.py

Lines changed: 75 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,50 @@
3939

4040
fixture_path = os.path.join(os.path.dirname(__file__), '..', 'fixtures')
4141

42+
4243
def parse_int_or_hex(s):
43-
if s[:2] == b'0x':
44+
if isinstance(s, int):
45+
return s
46+
elif s[:2] == b'0x':
4447
return utils.big_endian_to_int(decode_hex(s[2:]))
4548
else:
4649
return int(s)
4750

4851

52+
def acct_standard_form(a):
53+
return {
54+
"balance": parse_int_or_hex(a["balance"]),
55+
"nonce": parse_int_or_hex(a["nonce"]),
56+
"code": a["code"],
57+
"storage": a["storage"]
58+
}
59+
60+
61+
def compare_post_states(shouldbe, reallyis):
62+
if shouldbe is None and reallyis is None:
63+
return True
64+
if shouldbe is None or reallyis is None:
65+
raise Exception("Shouldbe: %r \n\nreallyis: %r" % (shouldbe, reallyis))
66+
for k in shouldbe:
67+
if k not in reallyis:
68+
r = {"nonce": 0, "balance": 0, "code": "0x", "storage": {}}
69+
else:
70+
r = acct_standard_form(reallyis[k])
71+
s = acct_standard_form(shouldbe[k])
72+
if s != r:
73+
raise Exception("Key %r\n\nshouldbe: %r \n\nreallyis: %r" %
74+
(k, s, r))
75+
return True
76+
77+
78+
def callcrate_standard_form(c):
79+
return {
80+
"gasLimit": parse_int_or_hex(c["gasLimit"]),
81+
"value": parse_int_or_hex(c["value"]),
82+
"data": c["data"]
83+
}
84+
85+
4986
def mktest(code, language, data=None, fun=None, args=None,
5087
gas=1000000, value=0, test_type=VM):
5188
s = t.state(1)
@@ -84,20 +121,20 @@ def run_vm_test(params, mode):
84121
# setup env
85122
header = blocks.BlockHeader(
86123
prevhash=decode_hex(env['previousHash']),
87-
number=int(env['currentNumber']),
124+
number=parse_int_or_hex(env['currentNumber']),
88125
coinbase=decode_hex(env['currentCoinbase']),
89-
difficulty=int(env['currentDifficulty']),
126+
difficulty=parse_int_or_hex(env['currentDifficulty']),
90127
gas_limit=parse_int_or_hex(env['currentGasLimit']),
91-
timestamp=int(env['currentTimestamp']))
128+
timestamp=parse_int_or_hex(env['currentTimestamp']))
92129
blk = blocks.Block(header, db=db)
93130

94131
# setup state
95132
for address, h in list(pre.items()):
96133
assert len(address) == 40
97134
address = decode_hex(address)
98135
assert set(h.keys()) == set(['code', 'nonce', 'balance', 'storage'])
99-
blk.set_nonce(address, int(h['nonce']))
100-
blk.set_balance(address, int(h['balance']))
136+
blk.set_nonce(address, parse_int_or_hex(h['nonce']))
137+
blk.set_balance(address, parse_int_or_hex(h['balance']))
101138
blk.set_code(address, decode_hex(h['code'][2:]))
102139
for k, v in h['storage'].items():
103140
blk.set_storage_data(address,
@@ -109,10 +146,10 @@ def run_vm_test(params, mode):
109146
recvaddr = decode_hex(exek['address'])
110147
tx = transactions.Transaction(
111148
nonce=blk._get_acct_item(decode_hex(exek['caller']), 'nonce'),
112-
gasprice=int(exek['gasPrice']),
113-
startgas=int(exek['gas']),
149+
gasprice=parse_int_or_hex(exek['gasPrice']),
150+
startgas=parse_int_or_hex(exek['gas']),
114151
to=recvaddr,
115-
value=int(exek['value']),
152+
value=parse_int_or_hex(exek['value']),
116153
data=decode_hex(exek['data'][2:]))
117154
tx.sender = sender
118155

@@ -183,18 +220,23 @@ def blkhash(n):
183220
return params2
184221
elif mode == VERIFY:
185222
params1 = copy.deepcopy(params)
186-
if 'post' in params1:
187-
for k, v in list(params1['post'].items()):
188-
if v == {'code': b'0x', 'nonce': '0', 'balance': '0', 'storage': {}}:
189-
del params1['post'][k]
190-
if 'post' in params2:
191-
for k, v in list(params2['post'].items()):
192-
if v == {'code': b'0x', 'nonce': '0', 'balance': '0', 'storage': {}}:
193-
del params2['post'][k]
223+
shouldbe, reallyis = params1.get('post', None), params2.get('post', None)
224+
compare_post_states(shouldbe, reallyis)
225+
226+
def normalize_value(k, p):
227+
if k in p:
228+
if k == 'gas':
229+
return parse_int_or_hex(p[k])
230+
elif k == 'callcreates':
231+
return map(callcrate_standard_form, p[k])
232+
else:
233+
return k
234+
return None
235+
194236
for k in ['pre', 'exec', 'env', 'callcreates',
195-
'out', 'gas', 'logs', 'post']:
196-
shouldbe = params1.get(k, None)
197-
reallyis = params2.get(k, None)
237+
'out', 'gas', 'logs']:
238+
shouldbe = normalize_value(k, params1)
239+
reallyis = normalize_value(k, params2)
198240
if shouldbe != reallyis:
199241
raise Exception("Mismatch: " + k + ':\n shouldbe %r\n reallyis %r' %
200242
(shouldbe, reallyis))
@@ -216,20 +258,20 @@ def run_state_test(params, mode):
216258
# setup env
217259
header = blocks.BlockHeader(
218260
prevhash=decode_hex(env['previousHash']),
219-
number=int(env['currentNumber']),
261+
number=parse_int_or_hex(env['currentNumber']),
220262
coinbase=decode_hex(env['currentCoinbase']),
221-
difficulty=int(env['currentDifficulty']),
263+
difficulty=parse_int_or_hex(env['currentDifficulty']),
222264
gas_limit=parse_int_or_hex(env['currentGasLimit']),
223-
timestamp=int(env['currentTimestamp']))
265+
timestamp=parse_int_or_hex(env['currentTimestamp']))
224266
blk = blocks.Block(header, db=db)
225267

226268
# setup state
227269
for address, h in list(pre.items()):
228270
assert len(address) == 40
229271
address = decode_hex(address)
230272
assert set(h.keys()) == set(['code', 'nonce', 'balance', 'storage'])
231-
blk.set_nonce(address, int(h['nonce']))
232-
blk.set_balance(address, int(h['balance']))
273+
blk.set_nonce(address, parse_int_or_hex(h['nonce']))
274+
blk.set_balance(address, parse_int_or_hex(h['balance']))
233275
blk.set_code(address, decode_hex(h['code'][2:]))
234276
for k, v in h['storage'].items():
235277
blk.set_storage_data(address,
@@ -238,20 +280,20 @@ def run_state_test(params, mode):
238280

239281
for address, h in list(pre.items()):
240282
address = decode_hex(address)
241-
assert blk.get_nonce(address) == int(h['nonce'])
242-
assert blk.get_balance(address) == int(h['balance'])
283+
assert blk.get_nonce(address) == parse_int_or_hex(h['nonce'])
284+
assert blk.get_balance(address) == parse_int_or_hex(h['balance'])
243285
assert blk.get_code(address) == decode_hex(h['code'][2:])
244286
for k, v in h['storage'].items():
245287
assert blk.get_storage_data(address, utils.big_endian_to_int(
246288
decode_hex(k[2:]))) == utils.big_endian_to_int(decode_hex(v[2:]))
247289

248290
# execute transactions
249291
tx = transactions.Transaction(
250-
nonce=int(exek['nonce'] or b"0"),
251-
gasprice=int(exek['gasPrice'] or b"0"),
292+
nonce=parse_int_or_hex(exek['nonce'] or b"0"),
293+
gasprice=parse_int_or_hex(exek['gasPrice'] or b"0"),
252294
startgas=parse_int_or_hex(exek['gasLimit'] or b"0"),
253295
to=decode_hex(exek['to'][2:] if exek['to'][:2] == b'0x' else exek['to']),
254-
value=int(exek['value'] or b"0"),
296+
value=parse_int_or_hex(exek['value'] or b"0"),
255297
data=decode_hex(exek['data'][2:])).sign(exek['secretKey'])
256298

257299
orig_apply_msg = pb.apply_msg
@@ -296,16 +338,10 @@ def blkhash(n):
296338
return params2
297339
elif mode == VERIFY:
298340
params1 = copy.deepcopy(params)
299-
if 'post' in params1:
300-
for k, v in list(params1['post'].items()):
301-
if v == {'code': b'0x', 'nonce': '0', 'balance': '0', 'storage': {}}:
302-
del params1['post'][k]
303-
if 'post' in params2:
304-
for k, v in list(params2['post'].items()):
305-
if v == {'code': b'0x', 'nonce': '0', 'balance': '0', 'storage': {}}:
306-
del params2['post'][k]
341+
shouldbe, reallyis = params1.get('post', None), params2.get('post', None)
342+
compare_post_states(shouldbe, reallyis)
307343
for k in ['pre', 'exec', 'env', 'callcreates',
308-
'out', 'gas', 'logs', 'post', 'postStateRoot']:
344+
'out', 'gas', 'logs', 'postStateRoot']:
309345
shouldbe = params1.get(k, None)
310346
reallyis = params2.get(k, None)
311347
if shouldbe != reallyis:

fixtures

Submodule fixtures updated 54 files

0 commit comments

Comments
 (0)