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