Skip to content

Commit 6bc56df

Browse files
address Unify client starting process for rlp (#3909)
* add client booting pick e98da11 add era client booting * code clean up * simply and clean code * address reviews by removing era part for now * address reviews * reduce code * address reviews * address reviews * fix tests * Update scripts/check_revision.sh --------- Co-authored-by: Advaita Saha <adv11sphs@gmail.com>
1 parent a3e340b commit 6bc56df

File tree

5 files changed

+69
-67
lines changed

5 files changed

+69
-67
lines changed

execution_chain/conf.nim

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ type
6060
NimbusCmd* {.pure.} = enum
6161
executionClient
6262
`import`
63-
`import-rlp`
6463

6564
RpcFlag* {.pure.} = enum
6665
## RPC flags
@@ -512,6 +511,18 @@ type
512511
" CL which will result in a smaller memory footprint"
513512
name: "debug-beacon-sync-target-is-final".}: bool
514513

514+
bootstrapBlocksFile* {.
515+
desc: "Import RLP encoded block files before starting the client"
516+
defaultValue: @[]
517+
name: "bootstrap-blocks-file" .}: seq[InputFile]
518+
519+
bootstrapBlocksFinalized* {.
520+
hidden
521+
desc: "Treat bootstrap RLP imports as finalized chain segments"
522+
defaultValue: false
523+
name: "debug-bootstrap-finalized" .}: bool
524+
525+
# We now load all the import specific configurations directly into ExecutionClientConf
515526
of NimbusCmd.`import`:
516527
maxBlocks* {.
517528
desc: "Maximum number of blocks to import"
@@ -563,12 +574,6 @@ type
563574
defaultValue: false
564575
name: "debug-store-slot-hashes".}: bool
565576

566-
of NimbusCmd.`import-rlp`:
567-
blocksFile* {.
568-
argument
569-
desc: "One or more RLP encoded block(s) files"
570-
name: "blocks-file" .}: seq[InputFile]
571-
572577
func parseHexOrDec256(p: string): UInt256 {.raises: [ValueError].} =
573578
if startsWith(p, "0x"):
574579
parse(p, UInt256, 16)
@@ -827,4 +832,3 @@ proc makeConfig*(cmdLine = commandLineParams(), ignoreUnknown = false): Executio
827832
when isMainModule:
828833
# for testing purpose
829834
discard makeConfig()
830-

execution_chain/core/block_import.nim

Lines changed: 37 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -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)

execution_chain/nimbus_execution_client.nim

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,12 @@ proc runExeClient*(
324324
txFrame.checkpoint(fc.base.blk.header.number, skipSnapshot = true)
325325
com.db.persist(txFrame)
326326

327+
# Rlp import is there, first load the chain segment
328+
if config.bootstrapBlocksFile.len > 0:
329+
try:
330+
waitFor importRlpBlocks(config, com, nimbus.fc)
331+
except CancelledError:
332+
raiseAssert "Nothing cancels the future"
327333
# Be graceful about ctrl-c during init
328334
if ProcessState.stopping.isNone:
329335
ProcessState.notifyRunning()
@@ -390,11 +396,6 @@ proc main*(config = makeConfig(), nimbus = NimbusNode(nil)) {.noinline.} =
390396
case config.cmd
391397
of NimbusCmd.`import`:
392398
importBlocks(config, com)
393-
of NimbusCmd.`import - rlp`:
394-
try:
395-
waitFor importRlpBlocks(config, com)
396-
except CancelledError:
397-
raiseAssert "Nothing cancels the future"
398399
else:
399400
runExeClient(config, com, nil, nimbus=nimbus)
400401

scripts/check_revision.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ if [[ $COPYRIGHT_YEAR_START_GET != *$COPYRIGHT_YEAR_START_EXPECT* ]]; then
4242
fi
4343

4444
LAUNCHING_INFO_EXPECT="Launching execution client"
45-
LAUNCHING_INFO_GET=$($EL import-rlp | head -n 1)
45+
LAUNCHING_INFO_GET=$($EL import | head -n 1)
4646

4747
if [[ $LAUNCHING_INFO_GET != *$LAUNCHING_INFO_EXPECT* ]]; then
4848
echo "\"Launching execution client\" should appear at first line"

tests/test_configuration.nim

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,19 @@ proc configurationMain*() =
4444
check dd.dataDir() == "apple\\bin"
4545
check dd.keyStoreDir == "banana/bin"
4646

47-
test "import-rlp":
48-
let aa = makeTestConfig()
49-
check aa.cmd == NimbusCmd.executionClient
50-
51-
let bb = makeConfig(@["import-rlp", genesisFile])
52-
check bb.cmd == NimbusCmd.`import-rlp`
53-
check bb.blocksFile[0].string == genesisFile
47+
test "bootstrap-blocks-file parsing":
48+
let defaults = makeTestConfig()
49+
check defaults.bootstrapBlocksFile.len == 0
50+
check defaults.bootstrapBlocksFinalized == false
51+
52+
let cfg = makeConfig(@[
53+
"--bootstrap-blocks-file:" & genesisFile,
54+
"--debug-bootstrap-finalized:false",
55+
])
56+
check cfg.cmd == NimbusCmd.executionClient
57+
check cfg.bootstrapBlocksFile.len == 1
58+
check cfg.bootstrapBlocksFile[0].string == genesisFile
59+
check cfg.bootstrapBlocksFinalized == false
5460

5561
test "network loading config file with no genesis data":
5662
# no genesis will fallback to geth compatibility mode

0 commit comments

Comments
 (0)