23
23
from eth .chains .mainnet import MAINNET_GENESIS_HEADER , MainnetChain
24
24
from eth .constants import BLANK_ROOT_HASH , EMPTY_SHA3
25
25
from eth .db .backends .level import LevelDB
26
+ from eth .db .trie import make_trie_root_and_nodes
26
27
from eth .rlp .headers import BlockHeader
27
28
from eth .rlp .transactions import BaseTransactionFields
28
29
from eth .rlp .accounts import Account
@@ -251,7 +252,7 @@ def get(self, node_hash):
251
252
252
253
253
254
def open_gethdb (location ):
254
- gethdb = GethDatabase (args . gethdb )
255
+ gethdb = GethDatabase (location )
255
256
256
257
last_block = gethdb .last_block_hash
257
258
last_block_num = gethdb .block_num_for_hash (last_block )
@@ -305,15 +306,12 @@ def main(args):
305
306
final_block_to_sync = min (args .syncuntil , final_block_to_sync )
306
307
307
308
for i in range (canonical_head .block_number , final_block_to_sync + 1 ):
308
- header_hash = gethdb .header_hash_for_block_number (i )
309
- header = gethdb .block_header (i , header_hash )
310
309
311
310
if not args .nobodies :
312
- body = gethdb .block_body (i )
313
- block_class = chain .get_vm_class (header ).get_block_class ()
314
- block = block_class (header , body .transactions , body .uncles )
315
- chain .chaindb .persist_block (block )
311
+ import_block_body (gethdb , chain , i )
316
312
else :
313
+ header_hash = gethdb .header_hash_for_block_number (i )
314
+ header = gethdb .block_header (i , header_hash )
317
315
headerdb .persist_header (header )
318
316
319
317
if i % 1000 == 0 :
@@ -403,20 +401,31 @@ def scan_state(gethdb: GethDatabase, trinitydb: LevelDB):
403
401
logger .info (f'scan_state: successfully imported { imported_entries } state entries' )
404
402
405
403
404
+ def import_block_body (gethdb , chain , block_number : int ):
405
+ header_hash = gethdb .header_hash_for_block_number (block_number )
406
+ header = gethdb .block_header (block_number , header_hash )
407
+
408
+ body = gethdb .block_body (block_number )
409
+ block_class = chain .get_vm_class (header ).get_block_class ()
410
+ block = block_class (header , body .transactions , body .uncles )
411
+ chain .chaindb .persist_block (block )
412
+
413
+ # persist_block saves the transactions into an index, but doesn't actually persist the
414
+ # transaction trie, meaning that without this next block attempts to read out the
415
+ # block will throw an exception
416
+ tx_root_hash , tx_kv_nodes = make_trie_root_and_nodes (body .transactions )
417
+ assert tx_root_hash == block .header .transaction_root
418
+ chain .chaindb .persist_trie_data_dict (tx_kv_nodes )
419
+
420
+
406
421
def import_body_range (gethdb , chain , start_block , end_block ):
407
422
logger .debug (
408
423
f'importing block bodies for blocks in range({ start_block } , { end_block + 1 } )'
409
424
)
410
425
previous_log_time = time .time ()
411
426
412
427
for i in range (start_block , end_block + 1 ):
413
- header_hash = gethdb .header_hash_for_block_number (i )
414
- header = gethdb .block_header (i , header_hash )
415
-
416
- body = gethdb .block_body (i )
417
- block_class = chain .get_vm_class (header ).get_block_class ()
418
- block = block_class (header , body .transactions , body .uncles )
419
- chain .chaindb .persist_block (block )
428
+ import_block_body (gethdb , chain , i )
420
429
421
430
if time .time () - previous_log_time > 5 :
422
431
logger .debug (f'importing bodies. block_number={ i } ' )
@@ -443,7 +452,7 @@ def process_blocks(gethdb, chain, end_block):
443
452
]
444
453
block = block_class (header , transactions , body .uncles )
445
454
imported_block , _ , _ = chain .import_block (block , perform_validation = True )
446
- logger .debug ('imported block: {imported_block}' )
455
+ logger .debug (f 'imported block: { imported_block } ' )
447
456
448
457
449
458
if __name__ == "__main__" :
0 commit comments