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,
|
2024-09-04 09:54:54 +00:00
|
|
|
../config
|
2021-03-18 15:05:15 +00:00
|
|
|
|
2024-09-04 09:54:54 +00:00
|
|
|
proc importRlpBlocks*(blocksRlp: openArray[byte],
|
|
|
|
chain: ForkedChainRef,
|
|
|
|
finalize: bool):
|
|
|
|
Result[void, string] =
|
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)
|
2024-10-16 01:34:12 +00:00
|
|
|
blk: Block
|
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.
|
2021-06-24 15:29:21 +00:00
|
|
|
while rlp.hasData:
|
2024-09-04 09:54:54 +00:00
|
|
|
blk = try:
|
2024-10-16 01:34:12 +00:00
|
|
|
rlp.read(Block)
|
2021-06-24 15:29:21 +00:00
|
|
|
except RlpError as e:
|
|
|
|
# terminate if there was a decoding error
|
2024-09-04 09:54:54 +00:00
|
|
|
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) =
|
2024-10-16 01:34:12 +00:00
|
|
|
var head: Header
|
2024-09-04 09:54:54 +00:00
|
|
|
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)
|