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

Commit 0b37104

Browse files
committed
Fixed a bug with a few block tests
1 parent bd8e1d6 commit 0b37104

File tree

3 files changed

+68
-18
lines changed

3 files changed

+68
-18
lines changed

ethereum/abi.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,25 +117,29 @@ def __init__(self, contract_interface):
117117
for description in contract_interface:
118118
encode_types = [
119119
element['type']
120-
for element in description['inputs']
120+
for element in description.get('inputs', [])
121121
]
122122

123123
signature = [
124124
(element['type'], element['name'])
125-
for element in description['inputs']
125+
for element in description.get('inputs', [])
126126
]
127127

128128
# type can be omitted, defaulting to function
129-
if description.get('type', 'function') == 'function':
130-
normalized_name = _normalize_name(description['name'])
129+
if description.get('type', 'function') in ('function', 'fallback'):
130+
if description.get('type', 'function') == 'function':
131+
normalized_name = _normalize_name(description['name'])
132+
prefix = method_id(normalized_name, encode_types)
133+
else:
134+
prefix = 0
131135

132136
decode_types = [
133137
element['type']
134-
for element in description['outputs']
138+
for element in description.get('outputs', [])
135139
]
136140

137141
self.function_data[normalized_name] = {
138-
'prefix': method_id(normalized_name, encode_types),
142+
'prefix': prefix,
139143
'encode_types': encode_types,
140144
'decode_types': decode_types,
141145
'is_constant': description.get('constant', False),

ethereum/new_state.py

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,6 @@ def __init__(self, nonce, balance, storage, code_hash, env):
7070

7171
def commit(self):
7272
for k, v in self.storage_cache.items():
73-
assert is_numeric(k)
74-
assert is_numeric(v)
7573
if v:
7674
self.storage_trie.update(utils.encode_int32(k), rlp.encode(v))
7775
else:
@@ -85,7 +83,6 @@ def code(self):
8583

8684
@code.setter
8785
def code(self, value):
88-
assert is_string(value)
8986
self.code_hash = utils.sha3(value)
9087
# Technically a db storage leak, but doesn't really matter; the only
9188
# thing that fails to get garbage collected is when code disappears due
@@ -94,15 +91,12 @@ def code(self, value):
9491
# self.env.db.inc_refcount(self.code_hash, value)
9592

9693
def get_storage_data(self, key):
97-
assert is_numeric(key)
9894
if key not in self.storage_cache:
9995
v = self.storage_trie.get(utils.encode_int32(key))
10096
self.storage_cache[key] = utils.big_endian_to_int(rlp.decode(v) if v else b'')
10197
return self.storage_cache[key]
10298

10399
def set_storage_data(self, key, value):
104-
assert is_numeric(key)
105-
assert is_numeric(value)
106100
self.storage_cache[key] = value
107101

108102
@classmethod
@@ -186,7 +180,6 @@ def set_and_journal(self, acct, param, val):
186180
# self.journal.append((acct, param, getattr(acct, param)))
187181
preval = getattr(acct, param)
188182
self.journal.append(lambda: setattr(acct, param, preval))
189-
assert acct._mutable
190183
setattr(acct, param, val)
191184

192185
def set_balance(self, address, value):
@@ -368,6 +361,57 @@ def to_snapshot(self, root_only=False, no_prevblocks=False):
368361
snapshot[k] = {str(n): ['0x'+encode_hex(h) for h in headers] for n, headers in v.items()}
369362
return snapshot
370363

364+
# Creates a state from a snapshot
365+
@classmethod
366+
def from_snapshot(cls, snapshot_data, env):
367+
state = State(env = env)
368+
if "alloc" in snapshot_data:
369+
for addr, data in snapshot_data["alloc"].items():
370+
if len(addr) == 40:
371+
addr = decode_hex(addr)
372+
assert len(addr) == 20
373+
if 'wei' in data:
374+
state.set_balance(addr, parse_as_int(data['wei']))
375+
if 'balance' in data:
376+
state.set_balance(addr, parse_as_int(data['balance']))
377+
if 'code' in data:
378+
state.set_code(addr, parse_as_bin(data['code']))
379+
if 'nonce' in data:
380+
state.set_nonce(addr, parse_as_int(data['nonce']))
381+
if 'storage' in data:
382+
for k, v in data['storage'].items():
383+
state.set_storage_data(addr, parse_as_bin(k), parse_as_bin(v))
384+
elif "state_root" in snapshot_data:
385+
state.trie.root_hash = parse_as_bin(snapshot_data["state_root"])
386+
else:
387+
raise Exception("Must specify either alloc or state root parameter")
388+
for k, default in STATE_DEFAULTS.items():
389+
default = copy.copy(default)
390+
v = snapshot_data[k] if k in snapshot_data else None
391+
if is_numeric(default):
392+
setattr(state, k, parse_as_int(v) if k in snapshot_data else default)
393+
elif is_string(default):
394+
setattr(state, k, parse_as_bin(v) if k in snapshot_data else default)
395+
elif k == 'prev_headers':
396+
if k in snapshot_data:
397+
headers = [dict_to_prev_header(h) for h in v]
398+
else:
399+
headers = default
400+
setattr(state, k, headers)
401+
elif k == 'recent_uncles':
402+
if k in snapshot_data:
403+
uncles = {}
404+
for height, _uncles in v.items():
405+
uncles[int(height)] = []
406+
for uncle in _uncles:
407+
uncles[int(height)].append(parse_as_bin(uncle))
408+
else:
409+
uncles = default
410+
setattr(state, k, uncles)
411+
state.commit()
412+
return state
413+
414+
371415
def ephemeral_clone(self):
372416
snapshot = self.to_snapshot(root_only=True, no_prevblocks=True)
373417
env2 = Env(OverlayDB(self.env.db), self.env.config)
@@ -376,8 +420,9 @@ def ephemeral_clone(self):
376420
setattr(s, param, getattr(self, param))
377421
s.recent_uncles = self.recent_uncles
378422
s.prev_headers = self.prev_headers
379-
assert len(self.journal) == len(self.cache) == 0
380-
s.journal = []
423+
for acct in self.cache.values():
424+
assert not acct.touched
425+
s.journal = copy.copy(self.journal)
381426
s.cache = {}
382427
return s
383428

ethereum/tests/test_blocks.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,17 +106,17 @@ def run_block_test(params, config_overrides=None):
106106

107107
def get_config_overrides(filename, testname):
108108
o = {}
109-
if 'TestNetwork' in filename:
109+
if '/TestNetwork/' in filename:
110110
o['HOMESTEAD_FORK_BLKNUM'] = 5
111111
# o['DAO_FORK_BLKNUM'] = 8
112112
o['ANTI_DOS_FORK_BLKNUM'] = 10
113113
o['CLEARING_FORK_BLKNUM'] = 14
114114
o['METROPOLIS_FORK_BLKNUM'] = 16
115-
elif 'EIP150' in filename or 'EIP150' in testname:
115+
elif '/EIP150/' in filename or 'EIP150' in testname:
116116
o['HOMESTEAD_FORK_BLKNUM'] = 0
117117
o['DAO_FORK_BLKNUM'] = 2**99
118118
o['ANTI_DOS_FORK_BLKNUM'] = 0
119-
elif 'Homestead' in filename or 'Homestead' in testname:
119+
elif '/Homestead/' in filename or 'Homestead' in testname:
120120
o['HOMESTEAD_FORK_BLKNUM'] = 0
121121
elif 'EIP158' in testname:
122122
o['HOMESTEAD_FORK_BLKNUM'] = 0
@@ -143,6 +143,7 @@ def test_block(filename, testname, testdata):
143143
('bl10251623GO.json', u'randomBlockTest'),
144144
('bl201507071825GO.json', u'randomBlockTest'),
145145
('call_OOG_additionalGasCosts2.json', 'call_OOG_additionalGasCosts2_d0g0v0_EIP158'),
146+
('MLOAD_Bounds.json', 'MLOAD_Bounds_d0g0v0_EIP158'),
146147
}
147148

148149
def exclude(filename, testname, _):

0 commit comments

Comments
 (0)