2021-03-18 15:05:15 +00:00
|
|
|
# Nimbus
|
2024-02-04 14:28:20 +00:00
|
|
|
# Copyright (c) 2021-2024 Status Research & Development GmbH
|
2021-03-18 15:05:15 +00:00
|
|
|
# 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.
|
|
|
|
|
2024-05-31 07:13:56 +00:00
|
|
|
{.push raises: [].}
|
|
|
|
|
2021-03-18 15:05:15 +00:00
|
|
|
import
|
2022-12-02 04:35:41 +00:00
|
|
|
chronicles,
|
|
|
|
eth/rlp, stew/io2,
|
|
|
|
./chain,
|
2023-05-30 08:58:41 +00:00
|
|
|
../common/common,
|
|
|
|
../utils/utils
|
2021-03-18 15:05:15 +00:00
|
|
|
|
2022-12-02 04:35:41 +00:00
|
|
|
proc importRlpBlock*(blocksRlp: openArray[byte]; com: CommonRef; importFile: string = ""): bool =
|
2021-06-24 15:29:21 +00:00
|
|
|
var
|
|
|
|
# the encoded rlp can contains one or more blocks
|
2022-04-20 07:57:50 +00:00
|
|
|
rlp = rlpFromBytes(blocksRlp)
|
2022-12-02 04:35:41 +00:00
|
|
|
chain = newChain(com, extraValidation = true)
|
2021-06-24 15:29:21 +00:00
|
|
|
errorCount = 0
|
2023-05-30 08:58:41 +00:00
|
|
|
header: BlockHeader
|
|
|
|
body: BlockBody
|
2021-03-18 15:05:15 +00:00
|
|
|
|
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.
|
2023-08-06 03:42:28 +00:00
|
|
|
|
2021-06-24 15:29:21 +00:00
|
|
|
while rlp.hasData:
|
|
|
|
try:
|
2023-05-30 08:58:41 +00:00
|
|
|
rlp.decompose(header, body)
|
2021-06-24 15:29:21 +00:00
|
|
|
except RlpError as e:
|
|
|
|
# terminate if there was a decoding error
|
|
|
|
error "rlp error",
|
|
|
|
fileName = importFile,
|
|
|
|
msg = e.msg,
|
|
|
|
exception = e.name
|
|
|
|
return false
|
2024-05-31 07:13:56 +00:00
|
|
|
|
|
|
|
chain.persistBlocks([header], [body]).isOkOr():
|
|
|
|
# register one more error and continue
|
2021-06-24 15:29:21 +00:00
|
|
|
error "import error",
|
|
|
|
fileName = importFile,
|
2024-05-31 07:13:56 +00:00
|
|
|
error
|
2021-06-24 15:29:21 +00:00
|
|
|
errorCount.inc
|
2021-03-18 15:05:15 +00:00
|
|
|
|
2021-06-24 15:29:21 +00:00
|
|
|
return errorCount == 0
|
2022-04-20 07:57:50 +00:00
|
|
|
|
2022-12-02 04:35:41 +00:00
|
|
|
proc importRlpBlock*(importFile: string; com: CommonRef): bool =
|
2022-04-20 07:57:50 +00:00
|
|
|
let res = io2.readAllBytes(importFile)
|
|
|
|
if res.isErr:
|
|
|
|
error "failed to import",
|
|
|
|
fileName = importFile
|
|
|
|
return false
|
2022-12-02 04:35:41 +00:00
|
|
|
|
|
|
|
importRlpBlock(res.get, com, importFile)
|