@@ -47,8 +47,35 @@ def mk_fake_header(blknum):
47
47
"Metropolis" : konfig_metropolis
48
48
}
49
49
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 ):
52
79
# Setup env
53
80
state = State (
54
81
env = Env (config = konfig ),
@@ -74,58 +101,20 @@ def compute_state_test_post(test, indices=None, _configs=None):
74
101
big_endian_to_int (decode_hex (k [2 :])),
75
102
decode_hex (v [2 :]))
76
103
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
119
106
120
107
def verify_state_test (test ):
121
108
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 ():
123
111
# Old protocol versions may not be supported
124
112
if config_name not in configs :
125
113
continue
126
114
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" ])
0 commit comments