2022-01-18 14:08:02 +00:00
|
|
|
# Nimbus - Portal Network
|
2024-02-28 17:31:45 +00:00
|
|
|
# Copyright (c) 2022-2024 Status Research & Development GmbH
|
2022-01-18 14:08:02 +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: [].}
|
2022-01-18 14:08:02 +00:00
|
|
|
|
|
|
|
import
|
2024-02-28 17:31:45 +00:00
|
|
|
json_serialization,
|
|
|
|
json_serialization/std/tables,
|
2024-05-30 12:54:03 +00:00
|
|
|
results,
|
|
|
|
stew/[byteutils, io2],
|
2024-02-28 17:31:45 +00:00
|
|
|
chronicles,
|
2024-10-04 21:21:26 +00:00
|
|
|
eth/common/[hashes, blocks, receipts, headers_rlp],
|
2022-12-02 04:39:12 +00:00
|
|
|
../../nimbus/common/[chain_config, genesis],
|
2024-09-23 16:56:28 +00:00
|
|
|
../network/history/[history_content, validation/historical_hashes_accumulator]
|
2022-01-18 14:08:02 +00:00
|
|
|
|
2024-10-04 21:21:26 +00:00
|
|
|
from eth/common/eth_types_rlp import rlpHash
|
|
|
|
|
2022-06-16 06:50:29 +00:00
|
|
|
export results, tables
|
2022-05-18 20:49:35 +00:00
|
|
|
|
2023-01-05 14:26:58 +00:00
|
|
|
# Helper calls to read/write history data from/to json files.
|
|
|
|
# Format is currently unspecified and likely to change.
|
|
|
|
|
|
|
|
# Reading JSON history data
|
2022-01-18 14:08:02 +00:00
|
|
|
|
|
|
|
type
|
2022-02-11 13:43:10 +00:00
|
|
|
BlockData* = object
|
2022-07-01 19:51:51 +00:00
|
|
|
header*: string
|
|
|
|
body*: string
|
|
|
|
receipts*: string
|
2022-02-11 13:43:10 +00:00
|
|
|
# TODO:
|
|
|
|
# uint64, but then it expects a string for some reason.
|
|
|
|
# Fix in nim-json-serialization or should I overload something here?
|
2022-07-01 19:51:51 +00:00
|
|
|
number*: int
|
2022-01-18 14:08:02 +00:00
|
|
|
|
2022-02-11 13:43:10 +00:00
|
|
|
BlockDataTable* = Table[string, BlockData]
|
2022-01-18 14:08:02 +00:00
|
|
|
|
2024-10-07 20:39:07 +00:00
|
|
|
iterator blockHashes*(blockData: BlockDataTable): Hash32 =
|
2022-10-17 18:38:51 +00:00
|
|
|
for k, v in blockData:
|
2024-10-07 20:39:07 +00:00
|
|
|
var blockHash: Hash32
|
2022-02-11 13:43:10 +00:00
|
|
|
try:
|
2024-10-07 20:39:07 +00:00
|
|
|
blockHash.data = hexToByteArray[sizeof(Hash32)](k)
|
2022-02-11 13:43:10 +00:00
|
|
|
except ValueError as e:
|
|
|
|
error "Invalid hex for block hash", error = e.msg, number = v.number
|
2022-02-22 10:52:44 +00:00
|
|
|
continue
|
2022-02-11 13:43:10 +00:00
|
|
|
|
2022-02-22 10:52:44 +00:00
|
|
|
yield blockHash
|
2022-01-18 14:08:02 +00:00
|
|
|
|
2022-09-09 19:21:48 +00:00
|
|
|
func readBlockData*(
|
2024-02-28 17:31:45 +00:00
|
|
|
hash: string, blockData: BlockData, verify = false
|
|
|
|
): Result[seq[(ContentKey, seq[byte])], string] =
|
2022-04-12 13:49:19 +00:00
|
|
|
var res: seq[(ContentKey, seq[byte])]
|
|
|
|
|
2024-10-07 20:39:07 +00:00
|
|
|
var blockHash: Hash32
|
2022-07-01 19:51:51 +00:00
|
|
|
try:
|
2024-10-07 20:39:07 +00:00
|
|
|
blockHash.data = hexToByteArray[sizeof(Hash32)](hash)
|
2022-07-01 19:51:51 +00:00
|
|
|
except ValueError as e:
|
2024-02-28 17:31:45 +00:00
|
|
|
return err("Invalid hex for blockhash, number " & $blockData.number & ": " & e.msg)
|
2022-04-12 13:49:19 +00:00
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
let contentKeyType = BlockKey(blockHash: blockHash)
|
2022-04-12 13:49:19 +00:00
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
try:
|
|
|
|
# If wanted the hash for the corresponding header can be verified
|
|
|
|
if verify:
|
2024-10-04 21:21:26 +00:00
|
|
|
if keccak256(blockData.header.hexToSeqByte()) != blockHash:
|
2022-07-01 19:51:51 +00:00
|
|
|
return err("Data is not matching hash, number " & $blockData.number)
|
2022-04-12 13:49:19 +00:00
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
block:
|
2024-02-28 17:31:45 +00:00
|
|
|
let contentKey =
|
|
|
|
ContentKey(contentType: blockHeader, blockHeaderKey: contentKeyType)
|
2022-04-12 13:49:19 +00:00
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
res.add((contentKey, blockData.header.hexToSeqByte()))
|
2022-04-12 13:49:19 +00:00
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
block:
|
2024-02-28 17:31:45 +00:00
|
|
|
let contentKey = ContentKey(contentType: blockBody, blockBodyKey: contentKeyType)
|
2022-04-12 13:49:19 +00:00
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
res.add((contentKey, blockData.body.hexToSeqByte()))
|
2022-04-12 13:49:19 +00:00
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
block:
|
2024-10-06 12:15:54 +00:00
|
|
|
let contentKey =
|
|
|
|
ContentKey(contentType: ContentType.receipts, receiptsKey: contentKeyType)
|
2022-04-12 13:49:19 +00:00
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
res.add((contentKey, blockData.receipts.hexToSeqByte()))
|
|
|
|
except ValueError as e:
|
|
|
|
return err("Invalid hex data, number " & $blockData.number & ": " & e.msg)
|
2022-04-12 13:49:19 +00:00
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
ok(res)
|
2022-04-12 13:49:19 +00:00
|
|
|
|
2022-02-22 10:52:44 +00:00
|
|
|
iterator blocks*(
|
2024-02-28 17:31:45 +00:00
|
|
|
blockData: BlockDataTable, verify = false
|
|
|
|
): seq[(ContentKey, seq[byte])] =
|
2022-10-17 18:38:51 +00:00
|
|
|
for k, v in blockData:
|
2022-04-12 13:49:19 +00:00
|
|
|
let res = readBlockData(k, v, verify)
|
2022-01-18 14:08:02 +00:00
|
|
|
|
2022-04-12 13:49:19 +00:00
|
|
|
if res.isOk():
|
|
|
|
yield res.get()
|
2022-02-22 10:52:44 +00:00
|
|
|
else:
|
2022-04-12 13:49:19 +00:00
|
|
|
error "Failed reading block from block data", error = res.error
|
2022-02-11 13:43:10 +00:00
|
|
|
|
2024-10-04 21:21:26 +00:00
|
|
|
func readBlockHeader*(blockData: BlockData): Result[Header, string] =
|
2022-05-18 20:49:35 +00:00
|
|
|
var rlp =
|
|
|
|
try:
|
2022-07-01 19:51:51 +00:00
|
|
|
rlpFromHex(blockData.header)
|
2022-05-18 20:49:35 +00:00
|
|
|
except ValueError as e:
|
2024-02-28 17:31:45 +00:00
|
|
|
return err(
|
|
|
|
"Invalid hex for rlp block data, number " & $blockData.number & ": " & e.msg
|
|
|
|
)
|
2022-05-18 20:49:35 +00:00
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
try:
|
2024-10-04 21:21:26 +00:00
|
|
|
return ok(rlp.read(Header))
|
2022-07-01 19:51:51 +00:00
|
|
|
except RlpError as e:
|
|
|
|
return err("Invalid header, number " & $blockData.number & ": " & e.msg)
|
2022-05-18 20:49:35 +00:00
|
|
|
|
2022-09-29 06:42:54 +00:00
|
|
|
func readHeaderData*(
|
2024-02-28 17:31:45 +00:00
|
|
|
hash: string, blockData: BlockData, verify = false
|
|
|
|
): Result[(ContentKey, seq[byte]), string] =
|
2024-10-07 20:39:07 +00:00
|
|
|
var blockHash: Hash32
|
2022-09-29 06:42:54 +00:00
|
|
|
try:
|
2024-10-07 20:39:07 +00:00
|
|
|
blockHash.data = hexToByteArray[sizeof(Hash32)](hash)
|
2022-09-29 06:42:54 +00:00
|
|
|
except ValueError as e:
|
2024-02-28 17:31:45 +00:00
|
|
|
return err("Invalid hex for blockhash, number " & $blockData.number & ": " & e.msg)
|
2022-09-29 06:42:54 +00:00
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
let contentKeyType = BlockKey(blockHash: blockHash)
|
2022-09-29 06:42:54 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
# If wanted the hash for the corresponding header can be verified
|
|
|
|
if verify:
|
2024-10-04 21:21:26 +00:00
|
|
|
if keccak256(blockData.header.hexToSeqByte()) != blockHash:
|
2022-09-29 06:42:54 +00:00
|
|
|
return err("Data is not matching hash, number " & $blockData.number)
|
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
let contentKey =
|
|
|
|
ContentKey(contentType: blockHeader, blockHeaderKey: contentKeyType)
|
2022-09-29 06:42:54 +00:00
|
|
|
|
|
|
|
let res = (contentKey, blockData.header.hexToSeqByte())
|
|
|
|
return ok(res)
|
|
|
|
except ValueError as e:
|
|
|
|
return err("Invalid hex data, number " & $blockData.number & ": " & e.msg)
|
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
iterator headers*(blockData: BlockDataTable, verify = false): (ContentKey, seq[byte]) =
|
2022-10-17 18:38:51 +00:00
|
|
|
for k, v in blockData:
|
2022-09-29 06:42:54 +00:00
|
|
|
let res = readHeaderData(k, v, verify)
|
|
|
|
|
|
|
|
if res.isOk():
|
|
|
|
yield res.get()
|
|
|
|
else:
|
|
|
|
error "Failed reading header from block data", error = res.error
|
|
|
|
|
2024-10-04 21:21:26 +00:00
|
|
|
proc getGenesisHeader*(id: NetworkId = MainNet): Header =
|
2022-06-01 13:21:22 +00:00
|
|
|
let params =
|
|
|
|
try:
|
|
|
|
networkParams(id)
|
|
|
|
except ValueError, RlpError:
|
2024-09-29 12:37:09 +00:00
|
|
|
debugEcho getCurrentException()[]
|
2022-06-01 13:21:22 +00:00
|
|
|
raise (ref Defect)(msg: "Network parameters should be valid")
|
|
|
|
|
2024-10-26 09:26:38 +00:00
|
|
|
toGenesisHeader(params)
|
2022-06-01 13:21:22 +00:00
|
|
|
|
2023-07-08 15:01:33 +00:00
|
|
|
# Reading JSON Portal content and content keys
|
|
|
|
|
|
|
|
type
|
|
|
|
JsonPortalContent* = object
|
|
|
|
content_key*: string
|
|
|
|
content_value*: string
|
|
|
|
|
|
|
|
JsonPortalContentTable* = OrderedTable[string, JsonPortalContent]
|
|
|
|
|
|
|
|
proc toString(v: IoErrorCode): string =
|
2024-02-28 17:31:45 +00:00
|
|
|
try:
|
|
|
|
ioErrorMsg(v)
|
|
|
|
except Exception as e:
|
|
|
|
raiseAssert e.msg
|
2023-07-08 15:01:33 +00:00
|
|
|
|
|
|
|
proc readJsonType*(dataFile: string, T: type): Result[T, string] =
|
2024-02-28 17:31:45 +00:00
|
|
|
let data = ?readAllFile(dataFile).mapErr(toString)
|
2023-07-08 15:01:33 +00:00
|
|
|
|
|
|
|
let decoded =
|
|
|
|
try:
|
|
|
|
Json.decode(data, T)
|
|
|
|
except SerializationError as e:
|
|
|
|
return err("Failed decoding json data-file: " & e.msg)
|
|
|
|
|
|
|
|
ok(decoded)
|
2022-08-01 19:00:21 +00:00
|
|
|
|
2023-01-05 14:26:58 +00:00
|
|
|
# Writing JSON history data
|
2022-11-04 08:27:01 +00:00
|
|
|
|
2023-01-05 14:26:58 +00:00
|
|
|
type
|
|
|
|
HeaderRecord* = object
|
|
|
|
header: string
|
|
|
|
number: uint64
|
|
|
|
|
|
|
|
BlockRecord* = object
|
|
|
|
header: string
|
|
|
|
body: string
|
|
|
|
receipts: string
|
|
|
|
number: uint64
|
|
|
|
|
2024-10-04 21:21:26 +00:00
|
|
|
proc writeHeaderRecord*(writer: var JsonWriter, header: Header) {.raises: [IOError].} =
|
2023-01-05 14:26:58 +00:00
|
|
|
let
|
2024-06-14 07:31:08 +00:00
|
|
|
dataRecord =
|
|
|
|
HeaderRecord(header: rlp.encode(header).to0xHex(), number: header.number)
|
2023-01-05 14:26:58 +00:00
|
|
|
|
|
|
|
headerHash = to0xHex(rlpHash(header).data)
|
|
|
|
|
|
|
|
writer.writeField(headerHash, dataRecord)
|
|
|
|
|
|
|
|
proc writeBlockRecord*(
|
2024-10-04 21:21:26 +00:00
|
|
|
writer: var JsonWriter, header: Header, body: BlockBody, receipts: seq[Receipt]
|
2024-02-28 17:31:45 +00:00
|
|
|
) {.raises: [IOError].} =
|
2023-01-05 14:26:58 +00:00
|
|
|
let
|
|
|
|
dataRecord = BlockRecord(
|
|
|
|
header: rlp.encode(header).to0xHex(),
|
|
|
|
body: encode(body).to0xHex(),
|
|
|
|
receipts: encode(receipts).to0xHex(),
|
2024-06-14 07:31:08 +00:00
|
|
|
number: header.number,
|
2024-02-28 17:31:45 +00:00
|
|
|
)
|
2023-01-05 14:26:58 +00:00
|
|
|
|
|
|
|
headerHash = to0xHex(rlpHash(header).data)
|
|
|
|
|
|
|
|
writer.writeField(headerHash, dataRecord)
|