Skip to content

Commit

Permalink
Move rlp block import into it's own subcommand (#2904)
Browse files Browse the repository at this point in the history
* Move rlp block import into it's own subcommand

* Fix test_configuration
  • Loading branch information
jangko authored Dec 4, 2024
1 parent 56caa5f commit 1101895
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 17 deletions.
12 changes: 7 additions & 5 deletions nimbus/config.nim
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ type
NimbusCmd* {.pure.} = enum
noCommand
`import`
`import-rlp`

RpcFlag* {.pure.} = enum
## RPC flags
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down
42 changes: 36 additions & 6 deletions nimbus/core/block_import.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -23,18 +25,46 @@ 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)
except RlpError as e:
# 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)
Expand Down
3 changes: 3 additions & 0 deletions nimbus/nimbus_execution_client.nim
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import
./constants,
./nimbus_desc,
./nimbus_import,
./core/block_import,
./core/eip4844,
./db/core_db/persistent,
./db/storage_types,
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 1 addition & 3 deletions nimbus/nimbus_import.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -341,5 +341,3 @@ proc importBlocks*(conf: NimbusConf, com: CommonRef) =

if blocks.len > 0:
process()

importRlpBlocks(conf, com)
6 changes: 3 additions & 3 deletions tests/test_configuration.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down

0 comments on commit 1101895

Please sign in to comment.