2021-10-09 11:22:03 +00:00
|
|
|
# Nimbus
|
2024-02-28 17:31:45 +00:00
|
|
|
# Copyright (c) 2021-2024 Status Research & Development GmbH
|
2021-10-09 11:22:03 +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.
|
|
|
|
|
|
|
|
# https://github.com/ethereum/portal-network-specs/blob/master/history-network.md#content-keys-and-values
|
|
|
|
|
2023-01-31 12:38:08 +00:00
|
|
|
{.push raises: [].}
|
2021-10-09 11:22:03 +00:00
|
|
|
|
|
|
|
import
|
2022-12-09 16:59:36 +00:00
|
|
|
std/math,
|
2024-02-28 17:31:45 +00:00
|
|
|
nimcrypto/[sha2, hash],
|
|
|
|
stew/[byteutils, results],
|
|
|
|
stint,
|
2021-10-23 12:28:12 +00:00
|
|
|
ssz_serialization,
|
2021-10-09 11:22:03 +00:00
|
|
|
../../common/common_types
|
|
|
|
|
2023-05-30 04:56:54 +00:00
|
|
|
from beacon_chain/spec/datatypes/capella import Withdrawal
|
|
|
|
from beacon_chain/spec/presets/mainnet import MAX_WITHDRAWALS_PER_PAYLOAD
|
|
|
|
|
2022-12-09 16:59:36 +00:00
|
|
|
export ssz_serialization, common_types, hash, results
|
2021-10-09 11:22:03 +00:00
|
|
|
|
2022-07-01 19:51:51 +00:00
|
|
|
## Types and calls for history network content keys
|
2022-09-28 07:09:38 +00:00
|
|
|
|
2022-08-09 12:32:41 +00:00
|
|
|
const
|
2022-09-28 07:09:38 +00:00
|
|
|
# Maximum content key size:
|
|
|
|
# - 32 bytes for SSZ serialized `BlockKey`
|
|
|
|
# - 1 byte for `ContentType`
|
|
|
|
# TODO: calculate it somehow from the object definition (macro?)
|
|
|
|
maxContentKeySize* = 33
|
2022-07-01 19:51:51 +00:00
|
|
|
|
2021-10-09 11:22:03 +00:00
|
|
|
type
|
|
|
|
ContentType* = enum
|
2022-01-05 08:49:49 +00:00
|
|
|
blockHeader = 0x00
|
|
|
|
blockBody = 0x01
|
|
|
|
receipts = 0x02
|
2022-06-14 21:38:34 +00:00
|
|
|
epochAccumulator = 0x03
|
2021-10-09 11:22:03 +00:00
|
|
|
|
2022-06-14 21:38:34 +00:00
|
|
|
BlockKey* = object
|
2021-10-09 11:22:03 +00:00
|
|
|
blockHash*: BlockHash
|
|
|
|
|
2022-06-14 21:38:34 +00:00
|
|
|
EpochAccumulatorKey* = object
|
2024-02-28 17:31:45 +00:00
|
|
|
epochHash*: Digest
|
|
|
|
# TODO: Perhaps this should be called epochRoot in the spec instead
|
2022-06-14 21:38:34 +00:00
|
|
|
|
2022-01-05 08:49:49 +00:00
|
|
|
ContentKey* = object
|
|
|
|
case contentType*: ContentType
|
|
|
|
of blockHeader:
|
2022-06-14 21:38:34 +00:00
|
|
|
blockHeaderKey*: BlockKey
|
2022-01-05 08:49:49 +00:00
|
|
|
of blockBody:
|
2022-06-14 21:38:34 +00:00
|
|
|
blockBodyKey*: BlockKey
|
2022-01-05 08:49:49 +00:00
|
|
|
of receipts:
|
2022-06-14 21:38:34 +00:00
|
|
|
receiptsKey*: BlockKey
|
|
|
|
of epochAccumulator:
|
|
|
|
epochAccumulatorKey*: EpochAccumulatorKey
|
2021-10-09 11:22:03 +00:00
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
func init*(T: type ContentKey, contentType: ContentType, hash: BlockHash | Digest): T =
|
2022-12-09 16:59:36 +00:00
|
|
|
case contentType
|
|
|
|
of blockHeader:
|
2024-02-28 17:31:45 +00:00
|
|
|
ContentKey(contentType: contentType, blockHeaderKey: BlockKey(blockHash: hash))
|
2022-12-09 16:59:36 +00:00
|
|
|
of blockBody:
|
2024-02-28 17:31:45 +00:00
|
|
|
ContentKey(contentType: contentType, blockBodyKey: BlockKey(blockHash: hash))
|
2022-12-09 16:59:36 +00:00
|
|
|
of receipts:
|
2024-02-28 17:31:45 +00:00
|
|
|
ContentKey(contentType: contentType, receiptsKey: BlockKey(blockHash: hash))
|
2022-12-09 16:59:36 +00:00
|
|
|
of epochAccumulator:
|
|
|
|
ContentKey(
|
|
|
|
contentType: contentType,
|
2024-02-28 17:31:45 +00:00
|
|
|
epochAccumulatorKey: EpochAccumulatorKey(epochHash: hash),
|
|
|
|
)
|
2022-12-09 16:59:36 +00:00
|
|
|
|
2021-10-09 11:22:03 +00:00
|
|
|
func encode*(contentKey: ContentKey): ByteList =
|
2021-10-22 15:49:23 +00:00
|
|
|
ByteList.init(SSZ.encode(contentKey))
|
2021-10-09 11:22:03 +00:00
|
|
|
|
2022-12-09 16:59:36 +00:00
|
|
|
func decode*(contentKey: ByteList): Opt[ContentKey] =
|
2021-10-09 11:22:03 +00:00
|
|
|
try:
|
2022-12-09 16:59:36 +00:00
|
|
|
Opt.some(SSZ.decode(contentKey.asSeq(), ContentKey))
|
2023-10-17 12:19:50 +00:00
|
|
|
except SerializationError:
|
2022-12-09 16:59:36 +00:00
|
|
|
return Opt.none(ContentKey)
|
2021-10-09 11:22:03 +00:00
|
|
|
|
|
|
|
func toContentId*(contentKey: ByteList): ContentId =
|
2022-05-24 11:27:22 +00:00
|
|
|
# TODO: Should we try to parse the content key here for invalid ones?
|
2022-09-10 19:00:27 +00:00
|
|
|
let idHash = sha2.sha256.digest(contentKey.asSeq())
|
2021-10-09 11:22:03 +00:00
|
|
|
readUintBE[256](idHash.data)
|
|
|
|
|
|
|
|
func toContentId*(contentKey: ContentKey): ContentId =
|
|
|
|
toContentId(encode(contentKey))
|
2022-04-13 09:17:07 +00:00
|
|
|
|
|
|
|
func `$`*(x: BlockHash): string =
|
|
|
|
"0x" & x.data.toHex()
|
|
|
|
|
2022-06-14 21:38:34 +00:00
|
|
|
func `$`*(x: BlockKey): string =
|
2022-09-28 07:09:38 +00:00
|
|
|
"blockHash: " & $x.blockHash
|
2022-06-14 21:38:34 +00:00
|
|
|
|
2022-04-13 09:17:07 +00:00
|
|
|
func `$`*(x: ContentKey): string =
|
2022-06-14 21:38:34 +00:00
|
|
|
var res = "(type: " & $x.contentType & ", "
|
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
case x.contentType
|
2022-06-14 21:38:34 +00:00
|
|
|
of blockHeader:
|
|
|
|
res.add($x.blockHeaderKey)
|
|
|
|
of blockBody:
|
|
|
|
res.add($x.blockBodyKey)
|
|
|
|
of receipts:
|
|
|
|
res.add($x.receiptsKey)
|
|
|
|
of epochAccumulator:
|
|
|
|
let key = x.epochAccumulatorKey
|
|
|
|
res.add("epochHash: " & $key.epochHash)
|
|
|
|
|
|
|
|
res.add(")")
|
2022-04-13 09:17:07 +00:00
|
|
|
|
2022-06-14 21:38:34 +00:00
|
|
|
res
|
2022-07-01 19:51:51 +00:00
|
|
|
|
|
|
|
## Types for history network content
|
|
|
|
|
|
|
|
const
|
2024-02-28 17:31:45 +00:00
|
|
|
MAX_TRANSACTION_LENGTH* = 2 ^ 24 # ~= 16 million
|
|
|
|
MAX_TRANSACTION_COUNT* = 2 ^ 14 # ~= 16k
|
|
|
|
MAX_RECEIPT_LENGTH* = 2 ^ 27 # ~= 134 million
|
|
|
|
MAX_HEADER_LENGTH = 2 ^ 13 # = 8192
|
|
|
|
MAX_ENCODED_UNCLES_LENGTH* = MAX_HEADER_LENGTH * 2 ^ 4 # = 2**17 ~= 131k
|
2023-05-30 04:56:54 +00:00
|
|
|
MAX_WITHDRAWAL_LENGTH = 64
|
|
|
|
MAX_WITHDRAWALS_COUNT = MAX_WITHDRAWALS_PER_PAYLOAD
|
2022-07-01 19:51:51 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
## Types for content
|
|
|
|
# TODO: Using `init` on these lists appears to fail because of the constants
|
|
|
|
# that are used? Strange.
|
|
|
|
TransactionByteList* = List[byte, MAX_TRANSACTION_LENGTH] # RLP data
|
|
|
|
Transactions* = List[TransactionByteList, MAX_TRANSACTION_COUNT]
|
|
|
|
Uncles* = List[byte, MAX_ENCODED_UNCLES_LENGTH] # RLP data
|
|
|
|
|
2023-05-30 04:56:54 +00:00
|
|
|
WithdrawalByteList* = List[byte, MAX_WITHDRAWAL_LENGTH] # RLP data
|
|
|
|
Withdrawals* = List[WithdrawalByteList, MAX_WITHDRAWALS_COUNT]
|
|
|
|
|
|
|
|
# Pre-shanghai block body
|
|
|
|
# Post-merge this block body is required to have an empty list for uncles
|
|
|
|
PortalBlockBodyLegacy* = object
|
|
|
|
transactions*: Transactions
|
|
|
|
uncles*: Uncles
|
|
|
|
|
|
|
|
# Post-shanghai block body, added withdrawals
|
|
|
|
PortalBlockBodyShanghai* = object
|
2022-07-01 19:51:51 +00:00
|
|
|
transactions*: Transactions
|
|
|
|
uncles*: Uncles
|
2023-05-30 04:56:54 +00:00
|
|
|
withdrawals*: Withdrawals
|
2022-07-01 19:51:51 +00:00
|
|
|
|
|
|
|
ReceiptByteList* = List[byte, MAX_RECEIPT_LENGTH] # RLP data
|
2023-05-30 04:56:54 +00:00
|
|
|
PortalReceipts* = List[ReceiptByteList, MAX_TRANSACTION_COUNT]
|
2022-11-04 08:27:01 +00:00
|
|
|
|
|
|
|
AccumulatorProof* = array[15, Digest]
|
|
|
|
|
|
|
|
BlockHeaderProofType* = enum
|
|
|
|
none = 0x00 # An SSZ Union None
|
|
|
|
accumulatorProof = 0x01
|
|
|
|
|
|
|
|
BlockHeaderProof* = object
|
|
|
|
case proofType*: BlockHeaderProofType
|
|
|
|
of none:
|
|
|
|
discard
|
|
|
|
of accumulatorProof:
|
|
|
|
accumulatorProof*: AccumulatorProof
|
|
|
|
|
|
|
|
BlockHeaderWithProof* = object
|
|
|
|
header*: ByteList # RLP data
|
|
|
|
proof*: BlockHeaderProof
|
|
|
|
|
|
|
|
func init*(T: type BlockHeaderProof, proof: AccumulatorProof): T =
|
|
|
|
BlockHeaderProof(proofType: accumulatorProof, accumulatorProof: proof)
|
2023-01-19 08:33:11 +00:00
|
|
|
|
|
|
|
func init*(T: type BlockHeaderProof): T =
|
|
|
|
BlockHeaderProof(proofType: none)
|