@@ -16,27 +16,27 @@ import
1616 chronos,
1717 ./ chain,
1818 ../ conf,
19- ../ utils/ utils,
20- beacon_chain/ process_state,
21- ./ chain/ forked_chain/ chain_serialize
19+ beacon_chain/ process_state
2220
23- proc importRlpBlocks * (blocksRlp:seq [byte ],
24- chain: ForkedChainRef ,
25- finalize: bool ):
26- Future [Result [void , string ]] {.async : (raises: [CancelledError ]).} =
21+ # Only parse the RLP data and feed blocks into the ForkedChainRef.
22+ # Optionally finalize via fork-choice when requested.
23+ proc importRlpBlocks * (
24+ blocksRlp: seq [byte ], chain: ForkedChainRef , finalize: bool
25+ ): Future [Result [void , string ]] {.async : (raises: [CancelledError ]).} =
2726 var
28- # the encoded rlp can contains one or more blocks
27+ # the encoded rlp can contain one or more blocks
2928 rlp = rlpFromBytes (blocksRlp)
3029 blk: Block
3130 printBanner = false
3231 firstSkip = Opt .none (uint64 )
3332
3433 while not ProcessState .stopIt (notice (" Shutting down" , reason = it)) and rlp.hasData:
35- blk = try :
36- rlp.read (Block )
37- except RlpError as e:
38- # terminate if there was a decoding error
39- return err ($ e.name & " : " & e.msg)
34+ blk =
35+ try :
36+ rlp.read (Block )
37+ except RlpError as e:
38+ # terminate if there was a decoding error
39+ return err ($ e.name & " : " & e.msg)
4040
4141 if blk.header.number <= chain.baseNumber:
4242 if firstSkip.isNone:
@@ -45,60 +45,51 @@ proc importRlpBlocks*(blocksRlp:seq[byte],
4545
4646 if firstSkip.isSome:
4747 if firstSkip.get == blk.header.number - 1 :
48- info " Block number smaller than base" ,
49- skip= firstSkip.get
48+ info " Block number smaller than base" , skip = firstSkip.get
5049 else :
5150 info " Block number smaller than base" ,
52- startSkip= firstSkip.get,
53- skipTo= blk.header.number- 1
51+ startSkip = firstSkip.get, skipTo = blk.header.number - 1
5452 firstSkip.reset ()
5553
5654 if not printBanner:
5755 info " Start importing block" ,
58- hash= blk.header.computeBlockHash.short,
59- number= blk.header.number
56+ hash = blk.header.computeBlockHash.short, number = blk.header.number
6057 printBanner = true
6158
6259 let res = await chain.importBlock (blk, finalized = true )
6360 if res.isErr:
64- error " Error occured when importing block" ,
65- hash= blk.header.computeBlockHash.short,
66- number= blk.header.number,
67- msg= res.error
61+ error " Error occurred when importing block" ,
62+ hash = blk.header.computeBlockHash.short,
63+ number = blk.header.number,
64+ msg = res.error
6865 if finalize:
69- ? (await chain.forkChoice (chain.latestHash, chain.latestHash))
66+ ? (await chain.forkChoice (chain.latestHash, chain.latestHash))
7067 return res
7168
7269 if finalize:
73- ? (await chain.forkChoice (chain.latestHash, chain.latestHash))
70+ ? (await chain.forkChoice (chain.latestHash, chain.latestHash))
7471
7572 ok ()
7673
77- proc importRlpBlocks * (importFile: string ,
78- chain: ForkedChainRef ,
79- finalize: bool ): Future [Result [void , string ]] {.async : (raises: [CancelledError ]).} =
74+ proc importRlpBlocks * (
75+ importFile: string , chain: ForkedChainRef , finalize: bool
76+ ): Future [Result [void , string ]] {.async : (raises: [CancelledError ]).} =
8077 let bytes = io2.readAllBytes (importFile).valueOr:
8178 return err ($ error)
8279 await importRlpBlocks (bytes, chain, finalize)
8380
84- proc importRlpBlocks * (config: ExecutionClientConf , com: CommonRef ): Future [void ] {.async : (raises: [CancelledError ]).} =
85- # Both baseDistance and persistBatchSize are 0,
86- # we want changes persisted immediately
87- let chain = ForkedChainRef .init (com, baseDistance = 0 , persistBatchSize = 1 )
81+ proc importRlpBlocks * (
82+ config: ExecutionClientConf , com: CommonRef , chain: ForkedChainRef
83+ ): Future [void ] {.async : (raises: [CancelledError ]).} =
84+ if config.bootstrapBlocksFile.len == 0 :
85+ return
8886
89- # success or not, we quit after importing blocks
90- for i, blocksFile in config. blocksFile:
91- (await importRlpBlocks (string blocksFile , chain, false )).isOkOr:
92- warn " Error when importing blocks" , msg= error
93- # Finalize the existing chain in case of rlp read error
94- (await chain.forkChoice (chain.latestHash, chain.latestHash)).isOkOr:
95- error " Error when finalizing chain" , msg= error
87+ for blocksFile in config.bootstrapBlocksFile:
88+ let filePath = string ( blocksFile)
89+ (await importRlpBlocks (filePath , chain, config.bootstrapBlocksFinalized )).isOkOr:
90+ warn " Error when importing blocks" , msg = error
91+ if config.bootstrapBlocksFinalized:
92+ (await chain.forkChoice (chain.latestHash, chain.latestHash)).isOkOr:
93+ error " Error when finalizing chain" , msg = error
9694 quit (QuitFailure )
9795
98- let txFrame = chain.baseTxFrame
99- chain.serialize (txFrame).isOkOr:
100- error " FC.serialize error: " , msg = error
101- txFrame.checkpoint (chain.base.blk.header.number, skipSnapshot = true )
102- com.db.persist (txFrame)
103-
104- quit (QuitSuccess )
0 commit comments