nimbus-eth1/nimbus/core/block_import.nim

66 lines
1.9 KiB
Nim
Raw Normal View History

# Nimbus
# Copyright (c) 2021-2024 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
# at your option.
# This file may not be copied, modified, or distributed except according to
# those terms.
{.push raises: [].}
import
2022-12-02 04:35:41 +00:00
chronicles,
eth/rlp, stew/io2,
./chain,
../config
proc importRlpBlocks*(blocksRlp: openArray[byte],
chain: ForkedChainRef,
finalize: bool):
Result[void, string] =
var
# the encoded rlp can contains one or more blocks
2022-04-20 07:57:50 +00:00
rlp = rlpFromBytes(blocksRlp)
blk: Block
2023-06-28 23:24:37 +00:00
# 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 finalize:
? chain.forkChoice(chain.latestHash, chain.latestHash)
ok()
proc importRlpBlocks*(importFile: string,
chain: ForkedChainRef,
finalize: bool): Result[void, string] =
let bytes = io2.readAllBytes(importFile).valueOr:
return err($error)
importRlpBlocks(bytes, chain, finalize)
proc importRlpBlocks*(conf: NimbusConf, com: CommonRef) =
var head: Header
if not com.db.getCanonicalHead(head):
error "cannot get canonical head from db"
quit(QuitFailure)
let chain = newForkedChain(com, head, baseDistance = 0)
# success or not, we quit after importing blocks
for i, blocksFile in conf.blocksFile:
importRlpBlocks(string blocksFile, chain, i == conf.blocksFile.len-1).isOkOr:
warn "Error when importing blocks", msg=error
quit(QuitFailure)
quit(QuitSuccess)