2023-12-19 18:59:38 +00:00
|
|
|
# Fluffy - Portal Network
|
2023-01-31 12:38:08 +00:00
|
|
|
# Copyright (c) 2022-2023 Status Research & Development GmbH
|
2022-10-24 12:16:40 +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-10-24 12:16:40 +00:00
|
|
|
|
|
|
|
import
|
2023-02-26 18:18:03 +00:00
|
|
|
std/typetraits,
|
2022-10-24 12:16:40 +00:00
|
|
|
stew/[arrayops, results],
|
|
|
|
beacon_chain/spec/forks,
|
|
|
|
beacon_chain/spec/datatypes/altair,
|
|
|
|
nimcrypto/[sha2, hash],
|
|
|
|
ssz_serialization,
|
2022-11-03 08:12:32 +00:00
|
|
|
ssz_serialization/codec,
|
2022-10-24 12:16:40 +00:00
|
|
|
../../common/common_types
|
|
|
|
|
|
|
|
export ssz_serialization, common_types, hash
|
|
|
|
|
2022-11-03 08:12:32 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.2.0/specs/altair/light-client/p2p-interface.md#configuration
|
|
|
|
const
|
|
|
|
MAX_REQUEST_LIGHT_CLIENT_UPDATES* = 128
|
|
|
|
|
|
|
|
# Needed to properly encode List[List[byte, XXX], MAX_REQUEST_LIGHT_CLIENT_UPDATES]
|
|
|
|
# based on eth2 MAX_CHUNK_SIZE, light client update should not be bigger than
|
|
|
|
# that
|
|
|
|
MAX_LIGHT_CLIENT_UPDATE_SIZE* = 1 * 1024 * 1024
|
|
|
|
|
2022-10-24 12:16:40 +00:00
|
|
|
type
|
|
|
|
ContentType* = enum
|
2023-12-19 18:59:38 +00:00
|
|
|
# Note: See same note as for state_content.nim
|
|
|
|
unused = 0x00
|
|
|
|
lightClientBootstrap = 0x10
|
|
|
|
lightClientUpdate = 0x11
|
|
|
|
lightClientFinalityUpdate = 0x12
|
|
|
|
lightClientOptimisticUpdate = 0x13
|
2022-10-24 12:16:40 +00:00
|
|
|
|
2023-02-26 18:18:03 +00:00
|
|
|
# TODO: Consider how we will gossip bootstraps?
|
|
|
|
# In consensus light client operation a node trusts only one bootstrap hash,
|
|
|
|
# therefore offers of other bootstraps would be rejected.
|
2022-10-24 12:16:40 +00:00
|
|
|
LightClientBootstrapKey* = object
|
|
|
|
blockHash*: Digest
|
|
|
|
|
|
|
|
LightClientUpdateKey* = object
|
2022-11-18 09:00:06 +00:00
|
|
|
startPeriod*: uint64
|
|
|
|
count*: uint64
|
2022-10-24 12:16:40 +00:00
|
|
|
|
2023-02-26 18:18:03 +00:00
|
|
|
# TODO:
|
|
|
|
# `optimisticSlot` and `finalizedSlot` are currently not in the spec. They are
|
|
|
|
# added to avoid accepting them in an offer based on the slot values. However,
|
|
|
|
# this causes them also to be included in a request, which makes perhaps less
|
|
|
|
# sense?
|
2022-10-24 12:16:40 +00:00
|
|
|
LightClientFinalityUpdateKey* = object
|
2023-07-08 15:01:33 +00:00
|
|
|
finalizedSlot*: uint64 ## slot of finalized header of the update
|
2022-10-24 12:16:40 +00:00
|
|
|
|
2023-02-26 18:18:03 +00:00
|
|
|
# TODO: Same remark as for `LightClientFinalityUpdateKey`
|
2022-10-24 12:16:40 +00:00
|
|
|
LightClientOptimisticUpdateKey* = object
|
2023-10-06 13:46:53 +00:00
|
|
|
optimisticSlot*: uint64 ## signature_slot of the update
|
2022-10-24 12:16:40 +00:00
|
|
|
|
|
|
|
ContentKey* = object
|
|
|
|
case contentType*: ContentType
|
2023-12-19 18:59:38 +00:00
|
|
|
of unused:
|
|
|
|
discard
|
2022-10-24 12:16:40 +00:00
|
|
|
of lightClientBootstrap:
|
|
|
|
lightClientBootstrapKey*: LightClientBootstrapKey
|
|
|
|
of lightClientUpdate:
|
|
|
|
lightClientUpdateKey*: LightClientUpdateKey
|
|
|
|
of lightClientFinalityUpdate:
|
|
|
|
lightClientFinalityUpdateKey*: LightClientFinalityUpdateKey
|
|
|
|
of lightClientOptimisticUpdate:
|
|
|
|
lightClientOptimisticUpdateKey*: LightClientOptimisticUpdateKey
|
|
|
|
|
2023-02-26 18:18:03 +00:00
|
|
|
# TODO:
|
|
|
|
# ForkedLightClientUpdateBytesList can get pretty big and is send in one go.
|
|
|
|
# We will need some chunking here but that is currently only possible in
|
|
|
|
# Portal wire protocol (and only for offer/accept).
|
2022-11-18 09:00:06 +00:00
|
|
|
ForkedLightClientUpdateBytes* = List[byte, MAX_LIGHT_CLIENT_UPDATE_SIZE]
|
2023-02-26 18:18:03 +00:00
|
|
|
ForkedLightClientUpdateBytesList* =
|
|
|
|
List[ForkedLightClientUpdateBytes, MAX_REQUEST_LIGHT_CLIENT_UPDATES]
|
|
|
|
# Note: Type not send over the wire, just used internally.
|
|
|
|
ForkedLightClientUpdateList* =
|
|
|
|
List[ForkedLightClientUpdate, MAX_REQUEST_LIGHT_CLIENT_UPDATES]
|
2022-10-24 12:16:40 +00:00
|
|
|
|
2023-10-18 14:59:44 +00:00
|
|
|
func forkDigestAtEpoch*(
|
|
|
|
forkDigests: ForkDigests, epoch: Epoch, cfg: RuntimeConfig): ForkDigest =
|
|
|
|
forkDigests.atEpoch(epoch, cfg)
|
|
|
|
|
2022-10-24 12:16:40 +00:00
|
|
|
func encode*(contentKey: ContentKey): ByteList =
|
2023-12-19 18:59:38 +00:00
|
|
|
doAssert(contentKey.contentType != unused)
|
2022-10-24 12:16:40 +00:00
|
|
|
ByteList.init(SSZ.encode(contentKey))
|
|
|
|
|
2023-12-19 18:59:38 +00:00
|
|
|
proc readSszBytes*(
|
|
|
|
data: openArray[byte], val: var ContentKey) {.raises: [SszError].} =
|
|
|
|
mixin readSszValue
|
|
|
|
if data.len() > 0 and data[0] == ord(unused):
|
|
|
|
raise newException(MalformedSszError, "SSZ selector unused value")
|
|
|
|
|
|
|
|
readSszValue(data, val)
|
|
|
|
|
2023-09-28 16:16:41 +00:00
|
|
|
func decode*(contentKey: ByteList): Opt[ContentKey] =
|
2022-10-24 12:16:40 +00:00
|
|
|
try:
|
2023-09-28 16:16:41 +00:00
|
|
|
Opt.some(SSZ.decode(contentKey.asSeq(), ContentKey))
|
2023-10-17 12:19:50 +00:00
|
|
|
except SerializationError:
|
2023-09-28 16:16:41 +00:00
|
|
|
return Opt.none(ContentKey)
|
2022-10-24 12:16:40 +00:00
|
|
|
|
|
|
|
func toContentId*(contentKey: ByteList): ContentId =
|
|
|
|
# TODO: Should we try to parse the content key here for invalid ones?
|
|
|
|
let idHash = sha2.sha256.digest(contentKey.asSeq())
|
|
|
|
readUintBE[256](idHash.data)
|
|
|
|
|
|
|
|
func toContentId*(contentKey: ContentKey): ContentId =
|
|
|
|
toContentId(encode(contentKey))
|
|
|
|
|
2023-03-17 09:19:17 +00:00
|
|
|
# Yes, this API is odd as you pass a SomeForkedLightClientObject yet still have
|
|
|
|
# to also pass the ForkDigest. This is because we can't just select the right
|
|
|
|
# digest through the LightClientDataFork here as LightClientDataFork and
|
|
|
|
# ConsensusFork are not mapped 1-to-1. There is loss of fork data.
|
|
|
|
# This means we need to get the ConsensusFork directly, which is possible by
|
|
|
|
# passing the epoch (slot) from the object through `forkDigestAtEpoch`. This
|
|
|
|
# however requires the runtime config which is part of the `Eth2Node` object.
|
|
|
|
# Not something we would like to include as a parameter here, so we stick with
|
|
|
|
# just passing the forkDigest and doing the work outside of this encode call.
|
2023-02-26 18:18:03 +00:00
|
|
|
func encodeForkedLightClientObject*(
|
|
|
|
obj: SomeForkedLightClientObject,
|
|
|
|
forkDigest: ForkDigest): seq[byte] =
|
|
|
|
withForkyObject(obj):
|
|
|
|
when lcDataFork > LightClientDataFork.None:
|
|
|
|
var res: seq[byte]
|
|
|
|
res.add(distinctBase(forkDigest))
|
|
|
|
res.add(SSZ.encode(forkyObject))
|
|
|
|
|
|
|
|
return res
|
|
|
|
else:
|
|
|
|
raiseAssert("No light client objects before Altair")
|
|
|
|
|
|
|
|
func encodeBootstrapForked*(
|
|
|
|
forkDigest: ForkDigest,
|
|
|
|
bootstrap: ForkedLightClientBootstrap): seq[byte] =
|
|
|
|
encodeForkedLightClientObject(bootstrap, forkDigest)
|
|
|
|
|
|
|
|
func encodeFinalityUpdateForked*(
|
|
|
|
forkDigest: ForkDigest,
|
|
|
|
finalityUpdate: ForkedLightClientFinalityUpdate): seq[byte] =
|
|
|
|
encodeForkedLightClientObject(finalityUpdate, forkDigest)
|
|
|
|
|
|
|
|
func encodeOptimisticUpdateForked*(
|
|
|
|
forkDigest: ForkDigest,
|
|
|
|
optimisticUpdate: ForkedLightClientOptimisticUpdate): seq[byte] =
|
|
|
|
encodeForkedLightClientObject(optimisticUpdate, forkDigest)
|
|
|
|
|
|
|
|
func encodeLightClientUpdatesForked*(
|
|
|
|
forkDigest: ForkDigest,
|
|
|
|
updates: openArray[ForkedLightClientUpdate]): seq[byte] =
|
|
|
|
var list: ForkedLightClientUpdateBytesList
|
|
|
|
for update in updates:
|
|
|
|
discard list.add(
|
|
|
|
ForkedLightClientUpdateBytes(
|
|
|
|
encodeForkedLightClientObject(update, forkDigest)))
|
|
|
|
|
|
|
|
SSZ.encode(list)
|
|
|
|
|
|
|
|
func decodeForkedLightClientObject(
|
|
|
|
ObjType: type SomeForkedLightClientObject,
|
|
|
|
forkDigests: ForkDigests,
|
2022-11-03 08:12:32 +00:00
|
|
|
data: openArray[byte]): Result[ObjType, string] =
|
2022-10-24 12:16:40 +00:00
|
|
|
if len(data) < 4:
|
2023-02-26 18:18:03 +00:00
|
|
|
return Result[ObjType, string].err("Not enough data for forkDigest")
|
2022-10-24 12:16:40 +00:00
|
|
|
|
|
|
|
let
|
2023-02-26 18:18:03 +00:00
|
|
|
forkDigest = ForkDigest(array[4, byte].initCopyFrom(data))
|
2023-04-13 16:40:01 +00:00
|
|
|
contextFork = forkDigests.consensusForkForDigest(forkDigest).valueOr:
|
2022-11-03 08:12:32 +00:00
|
|
|
return Result[ObjType, string].err("Unknown fork")
|
2022-10-24 12:16:40 +00:00
|
|
|
|
2023-04-13 16:40:01 +00:00
|
|
|
withLcDataFork(lcDataForkAtConsensusFork(contextFork)):
|
2023-02-26 18:18:03 +00:00
|
|
|
when lcDataFork > LightClientDataFork.None:
|
2023-10-06 13:46:53 +00:00
|
|
|
let res = decodeSsz(
|
|
|
|
data.toOpenArray(4, len(data) - 1), ObjType.Forky(lcDataFork))
|
2023-02-26 18:18:03 +00:00
|
|
|
if res.isOk:
|
|
|
|
# TODO:
|
|
|
|
# How can we verify the Epoch vs fork, e.g. with `consensusForkAtEpoch`?
|
|
|
|
# And should we?
|
|
|
|
var obj = ok ObjType(kind: lcDataFork)
|
|
|
|
obj.get.forky(lcDataFork) = res.get
|
|
|
|
obj
|
|
|
|
else:
|
|
|
|
Result[ObjType, string].err(res.error)
|
|
|
|
else:
|
|
|
|
Result[ObjType, string].err("Invalid Fork")
|
|
|
|
|
|
|
|
func decodeLightClientBootstrapForked*(
|
|
|
|
forkDigests: ForkDigests,
|
|
|
|
data: openArray[byte]): Result[ForkedLightClientBootstrap, string] =
|
|
|
|
decodeForkedLightClientObject(
|
|
|
|
ForkedLightClientBootstrap,
|
|
|
|
forkDigests,
|
2022-11-03 08:12:32 +00:00
|
|
|
data
|
|
|
|
)
|
|
|
|
|
2023-02-26 18:18:03 +00:00
|
|
|
func decodeLightClientUpdateForked*(
|
|
|
|
forkDigests: ForkDigests,
|
|
|
|
data: openArray[byte]): Result[ForkedLightClientUpdate, string] =
|
|
|
|
decodeForkedLightClientObject(
|
|
|
|
ForkedLightClientUpdate,
|
|
|
|
forkDigests,
|
2022-11-03 08:12:32 +00:00
|
|
|
data
|
|
|
|
)
|
|
|
|
|
2023-02-26 18:18:03 +00:00
|
|
|
func decodeLightClientFinalityUpdateForked*(
|
|
|
|
forkDigests: ForkDigests,
|
|
|
|
data: openArray[byte]): Result[ForkedLightClientFinalityUpdate, string] =
|
|
|
|
decodeForkedLightClientObject(
|
|
|
|
ForkedLightClientFinalityUpdate,
|
|
|
|
forkDigests,
|
2022-11-03 08:12:32 +00:00
|
|
|
data
|
|
|
|
)
|
|
|
|
|
2023-02-26 18:18:03 +00:00
|
|
|
func decodeLightClientOptimisticUpdateForked*(
|
|
|
|
forkDigests: ForkDigests,
|
|
|
|
data: openArray[byte]): Result[ForkedLightClientOptimisticUpdate, string] =
|
|
|
|
decodeForkedLightClientObject(
|
|
|
|
ForkedLightClientOptimisticUpdate,
|
|
|
|
forkDigests,
|
2022-11-03 08:12:32 +00:00
|
|
|
data
|
|
|
|
)
|
|
|
|
|
2023-02-26 18:18:03 +00:00
|
|
|
func decodeLightClientUpdatesByRange*(
|
|
|
|
forkDigests: ForkDigests,
|
|
|
|
data: openArray[byte]):
|
|
|
|
Result[ForkedLightClientUpdateList, string] =
|
|
|
|
let list = ? decodeSsz(data, ForkedLightClientUpdateBytesList)
|
2022-11-18 09:00:06 +00:00
|
|
|
|
2023-02-26 18:18:03 +00:00
|
|
|
var res: ForkedLightClientUpdateList
|
|
|
|
for encodedUpdate in list:
|
|
|
|
let update = ? decodeLightClientUpdateForked(
|
|
|
|
forkDigests, encodedUpdate.asSeq())
|
|
|
|
discard res.add(update)
|
2022-11-18 09:00:06 +00:00
|
|
|
|
2023-02-26 18:18:03 +00:00
|
|
|
ok(res)
|
2022-12-27 14:25:20 +00:00
|
|
|
|
2023-02-26 18:18:03 +00:00
|
|
|
func bootstrapContentKey*(blockHash: Digest): ContentKey =
|
2022-12-27 14:25:20 +00:00
|
|
|
ContentKey(
|
|
|
|
contentType: lightClientBootstrap,
|
2023-02-26 18:18:03 +00:00
|
|
|
lightClientBootstrapKey: LightClientBootstrapKey(blockHash: blockHash)
|
2022-12-27 14:25:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func updateContentKey*(startPeriod: uint64, count: uint64): ContentKey =
|
|
|
|
ContentKey(
|
|
|
|
contentType: lightClientUpdate,
|
2023-02-26 18:18:03 +00:00
|
|
|
lightClientUpdateKey: LightClientUpdateKey(
|
|
|
|
startPeriod: startPeriod, count: count)
|
2022-12-27 14:25:20 +00:00
|
|
|
)
|
|
|
|
|
2023-10-06 13:46:53 +00:00
|
|
|
func finalityUpdateContentKey*(finalizedSlot: uint64): ContentKey =
|
2022-11-18 09:00:06 +00:00
|
|
|
ContentKey(
|
|
|
|
contentType: lightClientFinalityUpdate,
|
|
|
|
lightClientFinalityUpdateKey: LightClientFinalityUpdateKey(
|
2023-02-26 18:18:03 +00:00
|
|
|
finalizedSlot: finalizedSlot
|
2022-11-18 09:00:06 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
func optimisticUpdateContentKey*(optimisticSlot: uint64): ContentKey =
|
|
|
|
ContentKey(
|
|
|
|
contentType: lightClientOptimisticUpdate,
|
|
|
|
lightClientOptimisticUpdateKey: LightClientOptimisticUpdateKey(
|
|
|
|
optimisticSlot: optimisticSlot
|
|
|
|
)
|
|
|
|
)
|