2022-05-18 20:49:35 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2022 Status Research & Development GmbH
|
|
|
|
# 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
|
|
|
|
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
|
|
|
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
|
2022-05-18 20:49:35 +00:00
|
|
|
|
|
|
|
NewBlockHeaderKey* = object
|
|
|
|
blockHash*: BlockHash
|
|
|
|
blockNumber*: UInt256
|
|
|
|
|
|
|
|
ContentKey* = object
|
|
|
|
case contentType*: ContentType
|
|
|
|
of newBlockHeader:
|
|
|
|
newBlockHeaderKey*: NewBlockHeaderKey
|
|
|
|
|
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))
|