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

Commit 2542c28

Browse files
vubvub
authored andcommitted
Passing non-metropolis new-style state tests
1 parent c2ec1dc commit 2542c28

File tree

2 files changed

+44
-54
lines changed

2 files changed

+44
-54
lines changed

ethereum/new_statetest_utils.py

Lines changed: 39 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,35 @@ def mk_fake_header(blknum):
4747
"Metropolis": konfig_metropolis
4848
}
4949

50-
def compute_state_test_post(test, indices=None, _configs=None):
51-
env, pre, txdata = test["env"], test["pre"], test["transaction"]
50+
def compute_state_test_unit(state, txdata, indices, konfig):
51+
state.env.config = konfig
52+
s = state.snapshot()
53+
try:
54+
# Create the transaction
55+
tx = transactions.Transaction(
56+
nonce=parse_int_or_hex(txdata['nonce'] or b"0"),
57+
gasprice=parse_int_or_hex(txdata['gasPrice'] or b"0"),
58+
startgas=parse_int_or_hex(txdata['gasLimit'][indices["gas"]] or b"0"),
59+
to=decode_hex(txdata['to']),
60+
value=parse_int_or_hex(txdata['value'][indices["value"]] or b"0"),
61+
data=decode_hex(remove_0x_head(txdata['data'][indices["data"]])))
62+
tx.sign(decode_hex(txdata['secretKey']))
63+
# Run it
64+
success, output = state_transition.apply_transaction(state, tx)
65+
print("Applied tx")
66+
except InvalidTransaction as e:
67+
print("Exception: %r" % e)
68+
success, output = False, b''
69+
state.commit()
70+
output_decl = {
71+
"hash": encode_hex(state.trie.root_hash),
72+
"indexes": indices
73+
}
74+
state.revert(s)
75+
return output_decl
76+
77+
78+
def init_state(env, pre):
5279
# Setup env
5380
state = State(
5481
env=Env(config=konfig),
@@ -74,58 +101,20 @@ def compute_state_test_post(test, indices=None, _configs=None):
74101
big_endian_to_int(decode_hex(k[2:])),
75102
decode_hex(v[2:]))
76103

77-
78-
# We have an optional argument which is a list of JSONs specifying indices.
79-
# If this argument is set, we compute only those scenarios. If not, we
80-
# compute all of them.
81-
if indices is None:
82-
indices = []
83-
for data_index in range(len(txdata['data'])):
84-
for value_index in range(len(txdata['value'])):
85-
for gaslimit_index in range(len(txdata['gasLimit'])):
86-
indices.append({"data": data_index, "gas": gaslimit_index, "value": value_index})
87-
88-
o = {}
89-
for config_name in (configs.keys() if _configs is None else _configs):
90-
state.env.config = configs[config_name]
91-
output_decls = []
92-
for index_json in indices:
93-
print("Executing for indices %r" % index_json)
94-
data_index, value_index, gaslimit_index = index_json["data"], index_json["value"], index_json["gas"]
95-
try:
96-
# Create the transaction
97-
tx = transactions.Transaction(
98-
nonce=parse_int_or_hex(txdata['nonce'] or b"0"),
99-
gasprice=parse_int_or_hex(txdata['gasPrice'] or b"0"),
100-
startgas=parse_int_or_hex(txdata['gasLimit'][gaslimit_index] or b"0"),
101-
to=decode_hex(txdata['to']),
102-
value=parse_int_or_hex(txdata['value'][value_index] or b"0"),
103-
data=decode_hex(remove_0x_head(txdata['data'][data_index])))
104-
tx.sign(decode_hex(txdata['secretKey']))
105-
# Run it
106-
success, output = state_transition.apply_transaction(state, tx)
107-
print("Applied tx")
108-
except InvalidTransaction as e:
109-
print("Exception: %r" % e)
110-
success, output = False, b''
111-
state.commit()
112-
output_decl = {
113-
"hash": encode_hex(state.trie.root_hash),
114-
"indexes": index_json
115-
}
116-
output_decls.append(output_decl)
117-
o[config_name] = output_decls
118-
return o
104+
state.commit()
105+
return state
119106

120107
def verify_state_test(test):
121108
print("Verifying state test")
122-
for config_name, result in test["post"].items():
109+
_state = init_state(test["env"], test["pre"])
110+
for config_name, results in test["post"].items():
123111
# Old protocol versions may not be supported
124112
if config_name not in configs:
125113
continue
126114
print("Testing for %s" % config_name)
127-
computed = compute_state_test_post(test, [x["indexes"] for x in result], [config_name])[config_name]
128-
supplied = test["post"][config_name]
129-
assert len(computed) == len(supplied)
130-
for c, s in zip(computed, supplied):
131-
assert c["hash"] == s["hash"]
115+
for result in results:
116+
computed = compute_state_test_unit(_state, test["transaction"], result["indexes"], configs[config_name])
117+
if computed["hash"] != result["hash"]:
118+
raise Exception("Hash mismatch: %s computed % supplied" % (computed["hash"], result["hash"]))
119+
else:
120+
print("Hash matched!: %s" % computed["hash"])

ethereum/vm.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def __init__(self, parent_memory, offset=0, size=None):
3838

3939
def extract_all(self):
4040
d = self.data[self.offset: self.offset + self.size]
41-
d += [0] * (self.size - len(d))
41+
d.extend(bytearray(self.size - len(d)))
4242
o = bytearray(len(d))
4343
for i, x in enumerate(d):
4444
o[i] = x
@@ -48,7 +48,8 @@ def extract32(self, i):
4848
if i >= self.size:
4949
return 0
5050
o = self.data[self.offset + i: min(self.offset + i + 32, self.rlimit)]
51-
return utils.bytearray_to_int(o + [0] * (32 - len(o)))
51+
o.extend(bytearray(32 - len(o)))
52+
return utils.bytearray_to_int(o)
5253

5354
def extract_copy(self, mem, memstart, datastart, size):
5455
for i in range(size):
@@ -80,7 +81,7 @@ def __repr__(self):
8081
class Compustate():
8182

8283
def __init__(self, **kwargs):
83-
self.memory = []
84+
self.memory = bytearray()
8485
self.stack = []
8586
self.pc = 0
8687
self.gas = 0
@@ -126,7 +127,7 @@ def mem_extend(mem, compustate, op, start, sz):
126127
return False
127128
compustate.gas -= memfee
128129
m_extend = (newsize - oldsize) * 32
129-
mem.extend([0] * m_extend)
130+
mem.extend(bytearray(m_extend))
130131
return True
131132

132133

0 commit comments

Comments
 (0)