7
7
import ethereum .transactions as transactions
8
8
import ethereum .state_transition as state_transition
9
9
import copy
10
+ import json
10
11
11
12
from ethereum .slogging import LogRecorder , configure_logging , set_level
12
13
config_string = ':info,eth.vm.log:trace,eth.vm.op:trace,eth.vm.stack:trace,eth.vm.exit:trace,eth.pb.msg:trace,eth.pb.tx:debug'
13
14
14
- configure_logging (config_string = config_string )
15
+ # configure_logging(config_string=config_string)
15
16
16
17
fake_headers = {}
17
18
@@ -57,11 +58,37 @@ def mk_fake_header(blknum):
57
58
58
59
configs = {
59
60
#"Homestead": konfig_homestead,
60
- "EIP150" : konfig_tangerine ,
61
+ # "EIP150": konfig_tangerine,
61
62
"EIP158" : konfig_spurious ,
62
63
"Metropolis" : konfig_metropolis
63
64
}
64
65
66
+ def mk_state_diff (prev , post ):
67
+ o = {}
68
+ for k in prev .keys ():
69
+ if k not in post :
70
+ o [k ] = ["-" , prev [k ]]
71
+ for k in post .keys ():
72
+ if k not in prev :
73
+ o [k ] = ["+" , post [k ]]
74
+ elif prev [k ] != post [k ]:
75
+ ok = {}
76
+ for key in ('nonce' , 'balance' , 'code' ):
77
+ if prev [k ][key ] != post [k ][key ]:
78
+ ok [key ] = [prev [k ][key ], "->" , post [k ][key ]]
79
+ if prev [k ]["storage" ] != post [k ]["storage" ]:
80
+ ok ["storage" ] = {}
81
+ for sk in prev [k ]["storage" ].keys ():
82
+ if sk not in post [k ]["storage" ]:
83
+ ok ["storage" ][sk ] = ["-" , prev [k ]["storage" ][sk ]]
84
+ for sk in post [k ]["storage" ].keys ():
85
+ if sk not in prev [k ]["storage" ]:
86
+ ok ["storage" ][sk ] = ["+" , post [k ]["storage" ][sk ]]
87
+ else :
88
+ ok ["storage" ][sk ] = [prev [k ]["storage" ][sk ], "->" , post [k ]["storage" ][sk ]]
89
+ o [k ] = ok
90
+ return o
91
+
65
92
def compute_state_test_unit (state , txdata , indices , konfig ):
66
93
state .env .config = konfig
67
94
s = state .snapshot ()
@@ -71,20 +98,24 @@ def compute_state_test_unit(state, txdata, indices, konfig):
71
98
nonce = parse_int_or_hex (txdata ['nonce' ] or b"0" ),
72
99
gasprice = parse_int_or_hex (txdata ['gasPrice' ] or b"0" ),
73
100
startgas = parse_int_or_hex (txdata ['gasLimit' ][indices ["gas" ]] or b"0" ),
74
- to = decode_hex (txdata ['to' ]),
101
+ to = decode_hex (remove_0x_head ( txdata ['to' ]) ),
75
102
value = parse_int_or_hex (txdata ['value' ][indices ["value" ]] or b"0" ),
76
103
data = decode_hex (remove_0x_head (txdata ['data' ][indices ["data" ]])))
77
- tx .sign (decode_hex (txdata ['secretKey' ]))
104
+ tx .sign (decode_hex (remove_0x_head ( txdata ['secretKey' ]) ))
78
105
# Run it
106
+ prev = state .to_dict ()
79
107
success , output = state_transition .apply_transaction (state , tx )
108
+ post = state .to_dict ()
80
109
print ("Applied tx" )
81
110
except InvalidTransaction as e :
82
111
print ("Exception: %r" % e )
112
+ post = state .to_dict ()
83
113
success , output = False , b''
84
114
state .commit ()
85
115
output_decl = {
86
- "hash" : encode_hex (state .trie .root_hash ),
87
- "indexes" : indices
116
+ "hash" : '0x' + encode_hex (state .trie .root_hash ),
117
+ "indexes" : indices ,
118
+ "diff" : mk_state_diff (prev , post )
88
119
}
89
120
state .revert (s )
90
121
return output_decl
@@ -94,23 +125,23 @@ def init_state(env, pre):
94
125
# Setup env
95
126
state = State (
96
127
env = Env (config = konfig ),
97
- block_prevhash = decode_hex (env ['previousHash' ]),
128
+ block_prevhash = decode_hex (remove_0x_head ( env ['previousHash' ]) ),
98
129
prev_headers = [mk_fake_header (i ) for i in range (parse_int_or_hex (env ['currentNumber' ]) - 1 ,
99
130
max (- 1 , parse_int_or_hex (env ['currentNumber' ]) - 257 ), - 1 )],
100
131
block_number = parse_int_or_hex (env ['currentNumber' ]),
101
- block_coinbase = decode_hex (env ['currentCoinbase' ]),
132
+ block_coinbase = decode_hex (remove_0x_head ( env ['currentCoinbase' ]) ),
102
133
block_difficulty = parse_int_or_hex (env ['currentDifficulty' ]),
103
134
gas_limit = parse_int_or_hex (env ['currentGasLimit' ]),
104
135
timestamp = parse_int_or_hex (env ['currentTimestamp' ]))
105
136
106
137
# Fill up pre
107
138
for address , h in list (pre .items ()):
108
- assert len (address ) == 40
109
- address = decode_hex (address )
139
+ assert len (address ) in ( 40 , 42 )
140
+ address = decode_hex (remove_0x_head ( address ) )
110
141
assert set (h .keys ()) == set (['code' , 'nonce' , 'balance' , 'storage' ])
111
142
state .set_nonce (address , parse_int_or_hex (h ['nonce' ]))
112
143
state .set_balance (address , parse_int_or_hex (h ['balance' ]))
113
- state .set_code (address , decode_hex (h ['code' ][ 2 :] ))
144
+ state .set_code (address , decode_hex (remove_0x_head ( h ['code' ]) ))
114
145
for k , v in h ['storage' ].items ():
115
146
state .set_storage_data (address ,
116
147
big_endian_to_int (decode_hex (k [2 :])),
@@ -128,12 +159,17 @@ def verify_state_test(test):
128
159
continue
129
160
print ("Testing for %s" % config_name )
130
161
for result in results :
162
+ data = test ["transaction" ]['data' ][result ["indexes" ]["data" ]]
163
+ if len (data ) > 2000 :
164
+ data = "data<%d>" % (len (data ) // 2 - 1 )
131
165
print ("Checking for values: g %d v %d d %s" % (
132
166
parse_int_or_hex (test ["transaction" ]['gasLimit' ][result ["indexes" ]["gas" ]]),
133
167
parse_int_or_hex (test ["transaction" ]['value' ][result ["indexes" ]["value" ]]),
134
- test [ "transaction" ][ ' data' ][ result [ "indexes" ][ "data" ]] ))
168
+ data ))
135
169
computed = compute_state_test_unit (_state , test ["transaction" ], result ["indexes" ], configs [config_name ])
136
170
if computed ["hash" ] != result ["hash" ]:
171
+ for k in computed ["diff" ]:
172
+ print (k , computed ["diff" ][k ])
137
173
raise Exception ("Hash mismatch, computed: %s, supplied: %s" % (computed ["hash" ], result ["hash" ]))
138
174
else :
139
175
print ("Hash matched!: %s" % computed ["hash" ])
0 commit comments