Skip to content

Commit f988a14

Browse files
committed
Add import_body_range command
1 parent f19c93e commit f988a14

File tree

1 file changed

+50
-11
lines changed

1 file changed

+50
-11
lines changed

scripts/gethimport.py

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import shutil
1313
import snappy
1414
import struct
15+
import time
1516

1617
import plyvel
1718

@@ -249,9 +250,7 @@ def get(self, node_hash):
249250
return geth_result
250251

251252

252-
def main(args):
253-
# 1. Open Geth database
254-
253+
def open_gethdb(location):
255254
gethdb = GethDatabase(args.gethdb)
256255

257256
last_block = gethdb.last_block_hash
@@ -262,22 +261,29 @@ def main(args):
262261
genesis_hash = gethdb.header_hash_for_block_number(0)
263262
genesis_header = gethdb.block_header(0, genesis_hash)
264263
assert genesis_header == MAINNET_GENESIS_HEADER
265-
logger.info(f'geth genesis header matches expected genesis')
266264

267-
# 2. Create trinity database
265+
return gethdb
266+
268267

268+
def open_trinitydb(location):
269269
db_already_existed = False
270-
if os.path.exists(args.destdb):
270+
if os.path.exists(location):
271271
db_already_existed = True
272272

273-
leveldb = LevelDB(db_path=Path(args.destdb), max_open_files=16)
273+
leveldb = LevelDB(db_path=Path(location), max_open_files=16)
274274

275275
if not db_already_existed:
276276
logger.info(f'Trinity database did not already exist, initializing it now')
277277
chain = MainnetChain.from_genesis_header(leveldb, MAINNET_GENESIS_HEADER)
278278
else:
279279
chain = MainnetChain(leveldb)
280280

281+
return chain
282+
283+
284+
def main(args):
285+
gethdb = open_gethdb(args.gethdb)
286+
chain = open_trinitydb(args.destdb)
281287
headerdb = chain.headerdb
282288

283289
# 3. Import headers + bodies
@@ -289,7 +295,10 @@ def main(args):
289295
geth_header = gethdb.block_header(canonical_head.block_number, canonical_head.hash)
290296
assert geth_header.hash == canonical_head.hash
291297

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
293302
if args.syncuntil:
294303
final_block_to_sync = min(args.syncuntil, final_block_to_sync)
295304

@@ -312,8 +321,6 @@ def main(args):
312321
if not args.syncuntil:
313322
# similar checks should be run if we added sync until!
314323
# 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)
317324
assert canonical_head.hash == geth_last_block_hash
318325
assert canonical_head.block_number == geth_last_block_num
319326

@@ -394,6 +401,26 @@ def scan_state(gethdb: GethDatabase, trinitydb: LevelDB):
394401
logger.info(f'scan_state: successfully imported {imported_entries} state entries')
395402

396403

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+
397424
if __name__ == "__main__":
398425
logging.basicConfig(
399426
level=logging.DEBUG,
@@ -407,9 +434,21 @@ def scan_state(gethdb: GethDatabase, trinitydb: LevelDB):
407434
parser.add_argument('-justblocks', action='store_true')
408435
parser.add_argument('-nobodies', action='store_true')
409436
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+
410444
args = parser.parse_args()
411445

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)
413452

414453
logger.warning('Some features are not yet implemented:')
415454
logger.warning('- Receipts were not imported')

0 commit comments

Comments
 (0)