Skip to content

Commit c749f3d

Browse files
committed
Add process_blocks command to simulate full sync
1 parent a4e39d1 commit c749f3d

File tree

1 file changed

+37
-5
lines changed

1 file changed

+37
-5
lines changed

scripts/gethimport.py

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -272,11 +272,13 @@ def open_trinitydb(location):
272272

273273
leveldb = LevelDB(db_path=Path(location), max_open_files=16)
274274

275-
if not db_already_existed:
276-
logger.info(f'Trinity database did not already exist, initializing it now')
277-
chain = MainnetChain.from_genesis_header(leveldb, MAINNET_GENESIS_HEADER)
278-
else:
279-
chain = MainnetChain(leveldb)
275+
if db_already_existed:
276+
return MainnetChain(leveldb)
277+
278+
logger.info(f'Trinity database did not already exist, initializing it now')
279+
chain = MainnetChain.from_genesis_header(leveldb, MAINNET_GENESIS_HEADER)
280+
281+
# from_genesis_header copied the header over to our trinity db but not the state
280282

281283
return chain
282284

@@ -421,6 +423,29 @@ def import_body_range(gethdb, chain, start_block, end_block):
421423
previous_log_time = time.time()
422424

423425

426+
def process_blocks(gethdb, chain, end_block):
427+
"Imports blocks read out of the gethdb. Simulates a full sync but w/o network traffic"
428+
429+
canonical_head = chain.headerdb.get_canonical_head()
430+
logger.info(f'starting block processing from chain tip: {canonical_head}')
431+
432+
start_block = max(canonical_head.block_number, 1)
433+
for i in range(start_block, end_block + 1):
434+
header_hash = gethdb.header_hash_for_block_number(i)
435+
header = gethdb.block_header(i, header_hash)
436+
vm_class = chain.get_vm_class(header)
437+
block_class = vm_class.get_block_class()
438+
transaction_class = vm_class.get_transaction_class()
439+
440+
body = gethdb.block_body(i)
441+
transactions = [
442+
transaction_class.from_base_transaction(txn) for txn in body.transactions
443+
]
444+
block = block_class(header, transactions, body.uncles)
445+
imported_block, _, _ = chain.import_block(block, perform_validation = True)
446+
logger.debug('imported block: {imported_block}')
447+
448+
424449
if __name__ == "__main__":
425450
logging.basicConfig(
426451
level=logging.DEBUG,
@@ -441,12 +466,19 @@ def import_body_range(gethdb, chain, start_block, end_block):
441466
import_body_range_parser.add_argument('-startblock', type=int, required=True)
442467
import_body_range_parser.add_argument('-endblock', type=int, required=True)
443468

469+
process_blocks_parser = subparsers.add_parser('process_blocks')
470+
process_blocks_parser.add_argument('-endblock', type=int, required=True)
471+
444472
args = parser.parse_args()
445473

446474
if args.command == 'import_body_range':
447475
gethdb = open_gethdb(args.gethdb)
448476
chain = open_trinitydb(args.destdb)
449477
import_body_range(gethdb, chain, args.startblock, args.endblock)
478+
elif args.command == 'process_blocks':
479+
gethdb = open_gethdb(args.gethdb)
480+
chain = open_trinitydb(args.destdb)
481+
process_blocks(gethdb, chain, args.endblock)
450482
else:
451483
main(args)
452484

0 commit comments

Comments
 (0)