2021-10-09 11:22:03 +00:00
|
|
|
# Nimbus
|
2022-04-13 09:17:07 +00:00
|
|
|
# Copyright (c) 2021-2022 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
|
|
|
|
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
|
|
|
import
|
|
|
|
std/options,
|
2022-04-13 09:17:07 +00:00
|
|
|
nimcrypto/[sha2, hash], stew/byteutils, stint,
|
2021-10-23 12:28:12 +00:00
|
|
|
ssz_serialization,
|
2021-10-09 11:22:03 +00:00
|
|
|
../../common/common_types
|
|
|
|
|
2022-06-16 06:50:29 +00:00
|
|
|
export ssz_serialization, common_types, hash
|
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
|
|
|
|
masterAccumulator = 0x04
|
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
|
|
|
chainId*: uint16
|
|
|
|
blockHash*: BlockHash
|
|
|
|
|
2022-06-14 21:38:34 +00:00
|
|
|
EpochAccumulatorKey* = object
|
|
|
|
epochHash*: Digest
|
|
|
|
|
|
|
|
MasterAccumulatorKeyType* = enum
|
|
|
|
latest = 0x00 # An SSZ Union None
|
|
|
|
masterHash = 0x01
|
|
|
|
|
|
|
|
MasterAccumulatorKey* = object
|
|
|
|
case accumulaterKeyType*: MasterAccumulatorKeyType
|
|
|
|
of latest:
|
|
|
|
discard
|
|
|
|
of masterHash:
|
|
|
|
masterHashKey*: Digest
|
|
|
|
|
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
|
|
|
|
of masterAccumulator:
|
|
|
|
masterAccumulatorKey*: MasterAccumulatorKey
|
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
|
|
|
|
|
|
|
func decode*(contentKey: ByteList): Option[ContentKey] =
|
|
|
|
try:
|
|
|
|
some(SSZ.decode(contentKey.asSeq(), ContentKey))
|
|
|
|
except SszError:
|
|
|
|
return none[ContentKey]()
|
|
|
|
|
|
|
|
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?
|
2021-10-09 11:22:03 +00:00
|
|
|
let idHash = sha2.sha_256.digest(contentKey.asSeq())
|
|
|
|
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 =
|
|
|
|
"blockHash: " & $x.blockHash & ", chainId: " & $x.chainId
|
|
|
|
|
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 & ", "
|
|
|
|
|
|
|
|
case x.contentType:
|
|
|
|
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)
|
|
|
|
of masterAccumulator:
|
|
|
|
let key = x.masterAccumulatorKey
|
|
|
|
case key.accumulaterKeyType:
|
|
|
|
of latest:
|
|
|
|
res.add($key.accumulaterKeyType)
|
|
|
|
of masterHash:
|
|
|
|
res.add($key.accumulaterKeyType & ": " & $key.masterHashKey)
|
|
|
|
|
|
|
|
res.add(")")
|
2022-04-13 09:17:07 +00:00
|
|
|
|
2022-06-14 21:38:34 +00:00
|
|
|
res
|