2023-01-05 14:26:58 +00:00
|
|
|
# Nimbus - Portal Network
|
2024-02-09 10:13:12 +00:00
|
|
|
# Copyright (c) 2022-2024 Status Research & Development GmbH
|
2023-01-05 14:26:58 +00:00
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
2023-01-31 12:38:08 +00:00
|
|
|
{.push raises: [].}
|
2023-01-05 14:26:58 +00:00
|
|
|
|
|
|
|
import
|
2024-05-30 12:54:03 +00:00
|
|
|
stew/[byteutils, io2],
|
2024-02-28 17:31:45 +00:00
|
|
|
chronicles,
|
2024-05-30 12:54:03 +00:00
|
|
|
results,
|
2024-10-04 21:21:26 +00:00
|
|
|
eth/common/headers_rlp,
|
2023-01-05 14:26:58 +00:00
|
|
|
ncli/e2store,
|
2024-09-23 16:56:28 +00:00
|
|
|
../network/history/[history_content, validation/historical_hashes_accumulator]
|
2023-01-05 14:26:58 +00:00
|
|
|
|
|
|
|
export results
|
|
|
|
|
|
|
|
# Reading SSZ data from files
|
|
|
|
|
2024-09-27 20:25:26 +00:00
|
|
|
proc readAccumulator*(
|
|
|
|
file: string
|
|
|
|
): Result[FinishedHistoricalHashesAccumulator, string] =
|
2024-02-28 17:31:45 +00:00
|
|
|
let encodedAccumulator = ?readAllFile(file).mapErr(toString)
|
2023-01-05 14:26:58 +00:00
|
|
|
|
|
|
|
try:
|
2024-09-27 20:25:26 +00:00
|
|
|
ok(SSZ.decode(encodedAccumulator, FinishedHistoricalHashesAccumulator))
|
2023-10-17 12:19:50 +00:00
|
|
|
except SerializationError as e:
|
2023-01-05 14:26:58 +00:00
|
|
|
err("Failed decoding accumulator: " & e.msg)
|
|
|
|
|
2024-07-11 15:42:45 +00:00
|
|
|
proc readEpochRecord*(file: string): Result[EpochRecord, string] =
|
2024-02-28 17:31:45 +00:00
|
|
|
let encodedAccumulator = ?readAllFile(file).mapErr(toString)
|
2023-01-05 14:26:58 +00:00
|
|
|
|
|
|
|
try:
|
2024-07-11 15:42:45 +00:00
|
|
|
ok(SSZ.decode(encodedAccumulator, EpochRecord))
|
2023-10-17 12:19:50 +00:00
|
|
|
except SerializationError as e:
|
2023-01-05 14:26:58 +00:00
|
|
|
err("Decoding epoch accumulator failed: " & e.msg)
|
|
|
|
|
2024-07-11 15:42:45 +00:00
|
|
|
proc readEpochRecordCached*(file: string): Result[EpochRecordCached, string] =
|
2024-02-28 17:31:45 +00:00
|
|
|
let encodedAccumulator = ?readAllFile(file).mapErr(toString)
|
2023-01-05 14:26:58 +00:00
|
|
|
|
|
|
|
try:
|
2024-07-11 15:42:45 +00:00
|
|
|
ok(SSZ.decode(encodedAccumulator, EpochRecordCached))
|
2023-10-17 12:19:50 +00:00
|
|
|
except SerializationError as e:
|
2023-01-05 14:26:58 +00:00
|
|
|
err("Decoding epoch accumulator failed: " & e.msg)
|
|
|
|
|
|
|
|
# Reading data in e2s format
|
|
|
|
|
|
|
|
const
|
|
|
|
# Using the e2s format to store data, but without the specific structure
|
|
|
|
# like in an era file, as we currently don't really need that.
|
|
|
|
# See: https://github.com/status-im/nimbus-eth2/blob/stable/docs/e2store.md
|
|
|
|
# Added one type for now, with numbers not formally specified.
|
|
|
|
# Note:
|
|
|
|
# Snappy compression for `ExecutionBlockHeaderRecord` only helps for the
|
|
|
|
# first ~1M (?) block headers, after that there is no gain so we don't do it.
|
|
|
|
ExecutionBlockHeaderRecord* = [byte 0xFF, 0x00]
|
|
|
|
|
2024-10-04 21:21:26 +00:00
|
|
|
proc readBlockHeaders*(file: string): Result[seq[headers.Header], string] =
|
2024-02-28 17:31:45 +00:00
|
|
|
let fh = ?openFile(file, {OpenFlags.Read}).mapErr(toString)
|
|
|
|
defer:
|
|
|
|
discard closeFile(fh)
|
2023-01-05 14:26:58 +00:00
|
|
|
|
|
|
|
var data: seq[byte]
|
2024-10-04 21:21:26 +00:00
|
|
|
var blockHeaders: seq[headers.Header]
|
2023-01-05 14:26:58 +00:00
|
|
|
while true:
|
|
|
|
let header = readRecord(fh, data).valueOr:
|
|
|
|
break
|
|
|
|
|
|
|
|
if header.typ == ExecutionBlockHeaderRecord:
|
|
|
|
let blockHeader =
|
|
|
|
try:
|
2024-10-04 21:21:26 +00:00
|
|
|
rlp.decode(data, headers.Header)
|
2023-01-05 14:26:58 +00:00
|
|
|
except RlpError as e:
|
|
|
|
return err("Invalid block header in " & file & ": " & e.msg)
|
|
|
|
|
|
|
|
blockHeaders.add(blockHeader)
|
|
|
|
else:
|
|
|
|
warn "Skipping record, not a block header", typ = toHex(header.typ)
|
|
|
|
|
|
|
|
ok(blockHeaders)
|