12
12
import shutil
13
13
import snappy
14
14
import struct
15
+ import time
15
16
16
17
import plyvel
17
18
@@ -249,9 +250,7 @@ def get(self, node_hash):
249
250
return geth_result
250
251
251
252
252
- def main (args ):
253
- # 1. Open Geth database
254
-
253
+ def open_gethdb (location ):
255
254
gethdb = GethDatabase (args .gethdb )
256
255
257
256
last_block = gethdb .last_block_hash
@@ -262,22 +261,29 @@ def main(args):
262
261
genesis_hash = gethdb .header_hash_for_block_number (0 )
263
262
genesis_header = gethdb .block_header (0 , genesis_hash )
264
263
assert genesis_header == MAINNET_GENESIS_HEADER
265
- logger .info (f'geth genesis header matches expected genesis' )
266
264
267
- # 2. Create trinity database
265
+ return gethdb
266
+
268
267
268
+ def open_trinitydb (location ):
269
269
db_already_existed = False
270
- if os .path .exists (args . destdb ):
270
+ if os .path .exists (location ):
271
271
db_already_existed = True
272
272
273
- leveldb = LevelDB (db_path = Path (args . destdb ), max_open_files = 16 )
273
+ leveldb = LevelDB (db_path = Path (location ), max_open_files = 16 )
274
274
275
275
if not db_already_existed :
276
276
logger .info (f'Trinity database did not already exist, initializing it now' )
277
277
chain = MainnetChain .from_genesis_header (leveldb , MAINNET_GENESIS_HEADER )
278
278
else :
279
279
chain = MainnetChain (leveldb )
280
280
281
+ return chain
282
+
283
+
284
+ def main (args ):
285
+ gethdb = open_gethdb (args .gethdb )
286
+ chain = open_trinitydb (args .destdb )
281
287
headerdb = chain .headerdb
282
288
283
289
# 3. Import headers + bodies
@@ -289,7 +295,10 @@ def main(args):
289
295
geth_header = gethdb .block_header (canonical_head .block_number , canonical_head .hash )
290
296
assert geth_header .hash == canonical_head .hash
291
297
292
- final_block_to_sync = last_block_num
298
+ geth_last_block_hash = gethdb .last_block_hash
299
+ geth_last_block_num = gethdb .block_num_for_hash (geth_last_block_hash )
300
+
301
+ final_block_to_sync = geth_last_block_num
293
302
if args .syncuntil :
294
303
final_block_to_sync = min (args .syncuntil , final_block_to_sync )
295
304
@@ -312,8 +321,6 @@ def main(args):
312
321
if not args .syncuntil :
313
322
# similar checks should be run if we added sync until!
314
323
# some final checks, these should never fail
315
- geth_last_block_hash = gethdb .last_block_hash
316
- geth_last_block_num = gethdb .block_num_for_hash (geth_last_block_hash )
317
324
assert canonical_head .hash == geth_last_block_hash
318
325
assert canonical_head .block_number == geth_last_block_num
319
326
@@ -394,6 +401,26 @@ def scan_state(gethdb: GethDatabase, trinitydb: LevelDB):
394
401
logger .info (f'scan_state: successfully imported { imported_entries } state entries' )
395
402
396
403
404
+ def import_body_range (gethdb , chain , start_block , end_block ):
405
+ logger .debug (
406
+ f'importing block bodies for blocks in range({ start_block } , { end_block + 1 } )'
407
+ )
408
+ previous_log_time = time .time ()
409
+
410
+ for i in range (start_block , end_block + 1 ):
411
+ header_hash = gethdb .header_hash_for_block_number (i )
412
+ header = gethdb .block_header (i , header_hash )
413
+
414
+ body = gethdb .block_body (i )
415
+ block_class = chain .get_vm_class (header ).get_block_class ()
416
+ block = block_class (header , body .transactions , body .uncles )
417
+ chain .chaindb .persist_block (block )
418
+
419
+ if time .time () - previous_log_time > 5 :
420
+ logger .debug (f'importing bodies. block_number={ i } ' )
421
+ previous_log_time = time .time ()
422
+
423
+
397
424
if __name__ == "__main__" :
398
425
logging .basicConfig (
399
426
level = logging .DEBUG ,
@@ -407,9 +434,21 @@ def scan_state(gethdb: GethDatabase, trinitydb: LevelDB):
407
434
parser .add_argument ('-justblocks' , action = 'store_true' )
408
435
parser .add_argument ('-nobodies' , action = 'store_true' )
409
436
parser .add_argument ('-syncuntil' , type = int , action = 'store' )
437
+
438
+ subparsers = parser .add_subparsers (dest = "command" )
439
+
440
+ import_body_range_parser = subparsers .add_parser ('import_body_range' )
441
+ import_body_range_parser .add_argument ('-startblock' , type = int , required = True )
442
+ import_body_range_parser .add_argument ('-endblock' , type = int , required = True )
443
+
410
444
args = parser .parse_args ()
411
445
412
- main (args )
446
+ if args .command == 'import_body_range' :
447
+ gethdb = open_gethdb (args .gethdb )
448
+ chain = open_trinitydb (args .destdb )
449
+ import_body_range (gethdb , chain , args .startblock , args .endblock )
450
+ else :
451
+ main (args )
413
452
414
453
logger .warning ('Some features are not yet implemented:' )
415
454
logger .warning ('- Receipts were not imported' )
0 commit comments