2022-05-18 20:49:35 +00:00
|
|
|
# Nimbus
|
2023-01-31 12:38:08 +00:00
|
|
|
# Copyright (c) 2022-2023 Status Research & Development GmbH
|
2022-05-18 20:49:35 +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/header-gossip-network.md
|
|
|
|
|
2023-01-31 12:38:08 +00:00
|
|
|
{.push raises: [].}
|
2022-05-18 20:49:35 +00:00
|
|
|
|
|
|
|
import
|
2022-06-16 06:50:29 +00:00
|
|
|
std/options,
|
|
|
|
nimcrypto/[sha2, hash], stint,
|
|
|
|
ssz_serialization,
|
2022-05-18 20:49:35 +00:00
|
|
|
../../common/common_types
|
|
|
|
|
2022-06-16 06:50:29 +00:00
|
|
|
export ssz_serialization, common_types, options, hash
|
2022-05-18 20:49:35 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
# Header Gossip Content Keys
|
|
|
|
# https://github.com/ethereum/portal-network-specs/blob/master/header-gossip-network.md#content-keys
|
2022-06-16 06:50:29 +00:00
|
|
|
# But with Accumulator removed as per
|
|
|
|
# https://github.com/ethereum/portal-network-specs/issues/153
|
2022-05-18 20:49:35 +00:00
|
|
|
|
|
|
|
ContentType* = enum
|
2022-06-16 06:50:29 +00:00
|
|
|
newBlockHeader = 0x00
|
2023-01-31 05:13:57 +00:00
|
|
|
# TODO: remove or fix this temporary
|
|
|
|
# dummySelector per latest spec.
|
|
|
|
# This is temporary workaround
|
|
|
|
# to fool SSZ.isUnion
|
|
|
|
dummySelector = 0x01
|
2022-05-18 20:49:35 +00:00
|
|
|
|
|
|
|
NewBlockHeaderKey* = object
|
|
|
|
blockHash*: BlockHash
|
|
|
|
blockNumber*: UInt256
|
|
|
|
|
|
|
|
ContentKey* = object
|
|
|
|
case contentType*: ContentType
|
|
|
|
of newBlockHeader:
|
|
|
|
newBlockHeaderKey*: NewBlockHeaderKey
|
2023-01-31 05:13:57 +00:00
|
|
|
of dummySelector:
|
|
|
|
dummyField: uint64
|
2022-05-18 20:49:35 +00:00
|
|
|
|
2022-06-16 06:50:29 +00:00
|
|
|
func encode*(contentKey: ContentKey): ByteList =
|
|
|
|
ByteList.init(SSZ.encode(contentKey))
|
2022-05-18 20:49:35 +00:00
|
|
|
|
2022-06-16 06:50:29 +00:00
|
|
|
func decode*(contentKey: ByteList): Option[ContentKey] =
|
|
|
|
try:
|
|
|
|
some(SSZ.decode(contentKey.asSeq(), ContentKey))
|
|
|
|
except SszError:
|
|
|
|
return none[ContentKey]()
|
2022-05-18 20:49:35 +00:00
|
|
|
|
2022-06-16 06:50:29 +00:00
|
|
|
func toContentId*(contentKey: ByteList): ContentId =
|
|
|
|
# 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())
|
2022-06-16 06:50:29 +00:00
|
|
|
readUintBE[256](idHash.data)
|
2022-05-18 20:49:35 +00:00
|
|
|
|
2022-06-16 06:50:29 +00:00
|
|
|
func toContentId*(contentKey: ContentKey): ContentId =
|
|
|
|
toContentId(encode(contentKey))
|