2323from eth .chains .mainnet import MAINNET_GENESIS_HEADER , MainnetChain
2424from eth .constants import BLANK_ROOT_HASH , EMPTY_SHA3
2525from eth .db .backends .level import LevelDB
26+ from eth .db .trie import make_trie_root_and_nodes
2627from eth .rlp .headers import BlockHeader
2728from eth .rlp .transactions import BaseTransactionFields
2829from eth .rlp .accounts import Account
@@ -251,7 +252,7 @@ def get(self, node_hash):
251252
252253
253254def open_gethdb (location ):
254- gethdb = GethDatabase (args . gethdb )
255+ gethdb = GethDatabase (location )
255256
256257 last_block = gethdb .last_block_hash
257258 last_block_num = gethdb .block_num_for_hash (last_block )
@@ -305,15 +306,12 @@ def main(args):
305306 final_block_to_sync = min (args .syncuntil , final_block_to_sync )
306307
307308 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 )
310309
311310 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 )
316312 else :
313+ header_hash = gethdb .header_hash_for_block_number (i )
314+ header = gethdb .block_header (i , header_hash )
317315 headerdb .persist_header (header )
318316
319317 if i % 1000 == 0 :
@@ -403,20 +401,31 @@ def scan_state(gethdb: GethDatabase, trinitydb: LevelDB):
403401 logger .info (f'scan_state: successfully imported { imported_entries } state entries' )
404402
405403
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+
406421def import_body_range (gethdb , chain , start_block , end_block ):
407422 logger .debug (
408423 f'importing block bodies for blocks in range({ start_block } , { end_block + 1 } )'
409424 )
410425 previous_log_time = time .time ()
411426
412427 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 )
420429
421430 if time .time () - previous_log_time > 5 :
422431 logger .debug (f'importing bodies. block_number={ i } ' )
@@ -443,7 +452,7 @@ def process_blocks(gethdb, chain, end_block):
443452 ]
444453 block = block_class (header , transactions , body .uncles )
445454 imported_block , _ , _ = chain .import_block (block , perform_validation = True )
446- logger .debug ('imported block: {imported_block}' )
455+ logger .debug (f 'imported block: { imported_block } ' )
447456
448457
449458if __name__ == "__main__" :
0 commit comments