From 1101895f9257ddf1dab2f7e56e74003834dafb5e Mon Sep 17 00:00:00 2001 From: andri lim Date: Wed, 4 Dec 2024 20:36:07 +0700 Subject: [PATCH] Move rlp block import into it's own subcommand (#2904) * Move rlp block import into it's own subcommand * Fix test_configuration --- nimbus/config.nim | 12 +++++---- nimbus/core/block_import.nim | 42 +++++++++++++++++++++++++----- nimbus/nimbus_execution_client.nim | 3 +++ nimbus/nimbus_import.nim | 4 +-- tests/test_configuration.nim | 6 ++--- 5 files changed, 50 insertions(+), 17 deletions(-) diff --git a/nimbus/config.nim b/nimbus/config.nim index f3fbd9768..be628dc56 100644 --- a/nimbus/config.nim +++ b/nimbus/config.nim @@ -104,6 +104,7 @@ type NimbusCmd* {.pure.} = enum noCommand `import` + `import-rlp` RpcFlag* {.pure.} = enum ## RPC flags @@ -485,11 +486,6 @@ type name: "jwt-secret" .}: Option[InputFile] of `import`: - blocksFile* {. - argument - desc: "One or more RLP encoded block(s) files" - name: "blocks-file" }: seq[InputFile] - maxBlocks* {. desc: "Maximum number of blocks to import" defaultValue: uint64.high() @@ -540,6 +536,12 @@ type defaultValue: false name: "debug-store-slot-hashes".}: bool + of `import-rlp`: + blocksFile* {. + argument + desc: "One or more RLP encoded block(s) files" + name: "blocks-file" }: seq[InputFile] + func parseCmdArg(T: type NetworkId, p: string): T {.gcsafe, raises: [ValueError].} = parseBiggestUInt(p).T diff --git a/nimbus/core/block_import.nim b/nimbus/core/block_import.nim index 54e98e3f8..70a0fe96a 100644 --- a/nimbus/core/block_import.nim +++ b/nimbus/core/block_import.nim @@ -11,9 +11,11 @@ import chronicles, - eth/rlp, stew/io2, + eth/rlp, + stew/io2, ./chain, - ../config + ../config, + ../utils/utils proc importRlpBlocks*(blocksRlp: openArray[byte], chain: ForkedChainRef, @@ -23,10 +25,9 @@ proc importRlpBlocks*(blocksRlp: openArray[byte], # the encoded rlp can contains one or more blocks rlp = rlpFromBytes(blocksRlp) blk: Block + printBanner = false + firstSkip = Opt.none(uint64) - # even though the new imported blocks have block number - # smaller than head, we keep importing it. - # it maybe a side chain. while rlp.hasData: blk = try: rlp.read(Block) @@ -34,7 +35,36 @@ proc importRlpBlocks*(blocksRlp: openArray[byte], # terminate if there was a decoding error return err($e.name & ": " & e.msg) - ? chain.importBlock(blk) + if blk.header.number <= chain.baseNumber: + if firstSkip.isNone: + firstSkip = Opt.some(blk.header.number) + continue + + if firstSkip.isSome: + if firstSkip.get == blk.header.number - 1: + info "Block number smaller than base", + skip=firstSkip.get + else: + info "Block number smaller than base", + startSkip=firstSkip.get, + skipTo=blk.header.number-1 + firstSkip.reset() + + if not printBanner: + info "Start importing block", + hash=blk.header.blockHash.short, + number=blk.header.number + printBanner = true + + let res = chain.importBlock(blk) + if res.isErr: + error "Error occured when importing block", + hash=blk.header.blockHash.short, + number=blk.header.number, + msg=res.error + if finalize: + ? chain.forkChoice(chain.latestHash, chain.latestHash) + return res if finalize: ? chain.forkChoice(chain.latestHash, chain.latestHash) diff --git a/nimbus/nimbus_execution_client.nim b/nimbus/nimbus_execution_client.nim index 342b9fbc5..5a975199b 100644 --- a/nimbus/nimbus_execution_client.nim +++ b/nimbus/nimbus_execution_client.nim @@ -23,6 +23,7 @@ import ./constants, ./nimbus_desc, ./nimbus_import, + ./core/block_import, ./core/eip4844, ./db/core_db/persistent, ./db/storage_types, @@ -235,6 +236,8 @@ proc run(nimbus: NimbusNode, conf: NimbusConf) = case conf.cmd of NimbusCmd.`import`: importBlocks(conf, com) + of NimbusCmd.`import-rlp`: + importRlpBlocks(conf, com) else: basicServices(nimbus, conf, com) manageAccounts(nimbus, conf) diff --git a/nimbus/nimbus_import.nim b/nimbus/nimbus_import.nim index 2b1a83429..eb15f4cdb 100644 --- a/nimbus/nimbus_import.nim +++ b/nimbus/nimbus_import.nim @@ -19,7 +19,7 @@ import beacon_chain/networking/network_metadata, ./config, ./common/common, - ./core/[block_import, chain], + ./core/chain, ./db/era1_db, ./utils/era_helpers @@ -341,5 +341,3 @@ proc importBlocks*(conf: NimbusConf, com: CommonRef) = if blocks.len > 0: process() - - importRlpBlocks(conf, com) diff --git a/tests/test_configuration.nim b/tests/test_configuration.nim index 3a6966bce..d558955c0 100644 --- a/tests/test_configuration.nim +++ b/tests/test_configuration.nim @@ -46,12 +46,12 @@ proc configurationMain*() = let ff = makeConfig(@["--chaindb:ariPrune"]) check ff.chainDbMode == ChainDbMode.AriPrune - test "import": + test "import-rlp": let aa = makeTestConfig() check aa.cmd == NimbusCmd.noCommand - let bb = makeConfig(@["import", genesisFile]) - check bb.cmd == NimbusCmd.`import` + let bb = makeConfig(@["import-rlp", genesisFile]) + check bb.cmd == NimbusCmd.`import-rlp` check bb.blocksFile[0].string == genesisFile test "custom-network loading config file with no genesis data":