@@ -59,11 +59,11 @@ def __init__(self, genesis=None, env=None, coinbase=b'\x00' * 20, \
59
59
60
60
self .head_hash = self .state .prev_headers [0 ].hash
61
61
self .genesis = Block (self .state .prev_headers [0 ], [], [])
62
- self .db .put ('state:' + self .head_hash , self .state .trie .root_hash )
62
+ self .db .put ('state:' + encode_hex ( self .head_hash ) , self .state .trie .root_hash )
63
63
self .db .put ('GENESIS_NUMBER' , str (self .state .block_number ))
64
- self .db .put ('GENESIS_HASH' , str ( self .state .prev_headers [0 ].hash ) )
64
+ self .db .put ('GENESIS_HASH' , self .state .prev_headers [0 ].hash )
65
65
assert self .state .block_number == self .state .prev_headers [0 ].number
66
- self .db .put ('score:' + self .state .prev_headers [0 ].hash , "0" )
66
+ self .db .put ('score:' + encode_hex ( self .state .prev_headers [0 ].hash ) , "0" )
67
67
self .db .put ('GENESIS_STATE' , json .dumps (self .state .to_snapshot ()))
68
68
self .db .put (self .head_hash , 'GENESIS' )
69
69
self .min_gasprice = kwargs .get ('min_gasprice' , 5 * 10 ** 9 )
@@ -90,7 +90,7 @@ def mk_poststate_of_blockhash(self, blockhash):
90
90
if self .db .get (blockhash ) == 'GENESIS' :
91
91
return State .from_snapshot (json .loads (self .db .get ('GENESIS_STATE' )), self .env )
92
92
state = State (env = self .env )
93
- state .trie .root_hash = self .db .get ('state:' + blockhash )
93
+ state .trie .root_hash = self .db .get ('state:' + encode_hex ( blockhash ) )
94
94
block = rlp .decode (self .db .get (blockhash ), Block )
95
95
update_block_env_variables (state , block )
96
96
state .gas_used = block .header .gas_used
@@ -141,14 +141,14 @@ def get_block(self, blockhash):
141
141
# parent hash and see that it is one of its children
142
142
def add_child (self , child ):
143
143
try :
144
- existing = self .db .get ('child:' + child .header .prevhash )
144
+ existing = self .db .get ('child:' + encode_hex ( child .header .prevhash ) )
145
145
except :
146
- existing = ''
146
+ existing = b ''
147
147
existing_hashes = []
148
148
for i in range (0 , len (existing ), 32 ):
149
149
existing_hashes .append (existing [i : i + 32 ])
150
150
if child .header .hash not in existing_hashes :
151
- self .db .put ('child:' + child .header .prevhash , existing + child .header .hash )
151
+ self .db .put ('child:' + encode_hex ( child .header .prevhash ) , existing + child .header .hash )
152
152
153
153
def get_blockhash_by_number (self , number ):
154
154
try :
@@ -163,7 +163,7 @@ def get_block_by_number(self, number):
163
163
def get_child_hashes (self , blockhash ):
164
164
o = []
165
165
try :
166
- data = self .db .get ('child:' + blockhash )
166
+ data = self .db .get ('child:' + encode_hex ( blockhash ) )
167
167
for i in range (0 , len (data ), 32 ):
168
168
o .append (data [i :i + 32 ])
169
169
return o
@@ -181,14 +181,14 @@ def get_children(self, block):
181
181
def get_score (self , block ):
182
182
if not block :
183
183
return 0
184
- key = 'score:' + block .header .hash
184
+ key = 'score:' + encode_hex ( block .header .hash )
185
185
if key not in self .db :
186
186
try :
187
187
parent_score = self .get_score (self .get_parent (block ))
188
188
self .db .put (key , str (parent_score + block .difficulty +
189
189
random .randrange (block .difficulty // 10 ** 6 + 1 )))
190
190
except :
191
- return int (self .db .get ('score:' + block .prevhash ))
191
+ return int (self .db .get ('score:' + encode_hex ( block .prevhash ) ))
192
192
return int (self .db .get (key ))
193
193
194
194
# These two functions should be called periodically so as to
@@ -234,20 +234,20 @@ def add_block(self, block):
234
234
log .info ('Block %s with parent %s invalid, reason: %s' % (encode_hex (block .header .hash ), encode_hex (block .header .prevhash ), e ))
235
235
return False
236
236
self .db .put ('block:' + str (block .header .number ), block .header .hash )
237
- self .db .put ('state:' + block .header .hash , self .state .trie .root_hash )
237
+ self .db .put ('state:' + encode_hex ( block .header .hash ) , self .state .trie .root_hash )
238
238
self .head_hash = block .header .hash
239
239
for i , tx in enumerate (block .transactions ):
240
- self .db .put ('txindex:' + tx .hash , rlp .encode ([block .number , i ]))
240
+ self .db .put ('txindex:' + encode_hex ( tx .hash ) , rlp .encode ([block .number , i ]))
241
241
elif block .header .prevhash in self .env .db :
242
242
log .info ('Receiving block not on head, adding to secondary post state' ,
243
243
prevhash = encode_hex (block .header .prevhash ))
244
244
temp_state = self .mk_poststate_of_blockhash (block .header .prevhash )
245
245
try :
246
246
apply_block (temp_state , block )
247
- except (KeyError , ValueError ), e : # FIXME add relevant exceptions here
247
+ except (KeyError , ValueError ) as e : # FIXME add relevant exceptions here
248
248
log .info ('Block %s with parent %s invalid, reason: %s' % (encode_hex (block .header .hash ), encode_hex (block .header .prevhash ), e ))
249
249
return False
250
- self .db .put ('state:' + block .header .hash , temp_state .trie .root_hash )
250
+ self .db .put ('state:' + encode_hex ( block .header .hash ) , temp_state .trie .root_hash )
251
251
block_score = self .get_score (block )
252
252
# Replace the head
253
253
if block_score > self .get_score (self .head ):
@@ -263,21 +263,21 @@ def add_block(self, block):
263
263
break
264
264
b = self .get_parent (b )
265
265
replace_from = b .header .number
266
- for i in xrange (replace_from , 2 ** 63 - 1 ):
266
+ for i in range (replace_from , 2 ** 63 - 1 ):
267
267
log .info ('Rewriting height %d' % i )
268
268
key = 'block:' + str (i )
269
269
orig_at_height = self .db .get (key ) if key in self .db else None
270
270
if orig_at_height :
271
271
self .db .delete (key )
272
272
orig_block_at_height = self .get_block (orig_at_height )
273
273
for tx in orig_block_at_height .transactions :
274
- if 'txindex:' + tx .hash in self .db :
275
- self .db .delete ('txindex:' + tx .hash )
274
+ if 'txindex:' + encode_hex ( tx .hash ) in self .db :
275
+ self .db .delete ('txindex:' + encode_hex ( tx .hash ) )
276
276
if i in new_chain :
277
277
new_block_at_height = new_chain [i ]
278
278
self .db .put (key , new_block_at_height .header .hash )
279
279
for i , tx in enumerate (new_block_at_height .transactions ):
280
- self .db .put ('txindex:' + tx .hash ,
280
+ self .db .put ('txindex:' + encode_hex ( tx .hash ) ,
281
281
rlp .encode ([new_block_at_height .number , i ]))
282
282
if i not in new_chain and not orig_at_height :
283
283
break
@@ -324,7 +324,7 @@ def get_chain(self, frm=None, to=2**63 - 1):
324
324
if frm is None :
325
325
frm = int (self .db .get ('GENESIS_NUMBER' )) + 1
326
326
chain = []
327
- for i in xrange (frm , to ):
327
+ for i in range (frm , to ):
328
328
h = self .get_blockhash_by_number (i )
329
329
if not h :
330
330
return chain
@@ -334,8 +334,8 @@ def get_chain(self, frm=None, to=2**63 - 1):
334
334
def get_transaction (self , tx ):
335
335
if not isinstance (tx , (str , bytes )):
336
336
tx = tx .hash
337
- if 'txindex:' + tx in self .db :
338
- data = rlp .decode (self .db .get ('txindex:' + tx ))
337
+ if 'txindex:' + encode_hex ( tx ) in self .db :
338
+ data = rlp .decode (self .db .get ('txindex:' + encode_hex ( tx ) ))
339
339
blk , index = self .get_block_by_number (
340
340
big_endian_to_int (data [0 ])), big_endian_to_int (data [1 ])
341
341
tx = blk .transactions [index ]
@@ -359,3 +359,4 @@ def db(self):
359
359
@property
360
360
def config (self ):
361
361
return self .env .config
362
+
0 commit comments