2023-12-19 18:59:38 +00:00
|
|
|
# Fluffy - Portal Network
|
2024-01-26 22:38:12 +00:00
|
|
|
# Copyright (c) 2022-2024 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
|
2024-01-26 22:38:12 +00:00
|
|
|
historicalSummaries = 0x14
|
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
|
|
|
|
2024-05-13 16:49:21 +00:00
|
|
|
HistoricalSummariesKey* = object
|
|
|
|
epoch*: uint64
|
2024-01-26 22:38:12 +00:00
|
|
|
|
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
|
2024-01-26 22:38:12 +00:00
|
|
|
of historicalSummaries:
|
|
|
|
historicalSummariesKey*: HistoricalSummariesKey
|
2022-10-24 12:16:40 +00:00
|
|
|
|
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*(
|
2024-02-28 17:31:45 +00:00
|
|
|
forkDigests: ForkDigests, epoch: Epoch, cfg: RuntimeConfig
|
|
|
|
): ForkDigest =
|
2023-10-18 14:59:44 +00:00
|
|
|
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))
|
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
proc readSszBytes*(data: openArray[byte], val: var ContentKey) {.raises: [SszError].} =
|
2023-12-19 18:59:38 +00:00
|
|
|
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*(
|
2024-02-28 17:31:45 +00:00
|
|
|
obj: SomeForkedLightClientObject, forkDigest: ForkDigest
|
|
|
|
): seq[byte] =
|
2023-02-26 18:18:03 +00:00
|
|
|
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*(
|
2024-02-28 17:31:45 +00:00
|
|
|
forkDigest: ForkDigest, bootstrap: ForkedLightClientBootstrap
|
|
|
|
): seq[byte] =
|
2023-02-26 18:18:03 +00:00
|
|
|
encodeForkedLightClientObject(bootstrap, forkDigest)
|
|
|
|
|
|
|
|
func encodeFinalityUpdateForked*(
|
2024-02-28 17:31:45 +00:00
|
|
|
forkDigest: ForkDigest, finalityUpdate: ForkedLightClientFinalityUpdate
|
|
|
|
): seq[byte] =
|
2023-02-26 18:18:03 +00:00
|
|
|
encodeForkedLightClientObject(finalityUpdate, forkDigest)
|
|
|
|
|
|
|
|
func encodeOptimisticUpdateForked*(
|
2024-02-28 17:31:45 +00:00
|
|
|
forkDigest: ForkDigest, optimisticUpdate: ForkedLightClientOptimisticUpdate
|
|
|
|
): seq[byte] =
|
2023-02-26 18:18:03 +00:00
|
|
|
encodeForkedLightClientObject(optimisticUpdate, forkDigest)
|
|
|
|
|
|
|
|
func encodeLightClientUpdatesForked*(
|
2024-02-28 17:31:45 +00:00
|
|
|
forkDigest: ForkDigest, updates: openArray[ForkedLightClientUpdate]
|
|
|
|
): seq[byte] =
|
2023-02-26 18:18:03 +00:00
|
|
|
var list: ForkedLightClientUpdateBytesList
|
|
|
|
for update in updates:
|
|
|
|
discard list.add(
|
2024-02-28 17:31:45 +00:00
|
|
|
ForkedLightClientUpdateBytes(encodeForkedLightClientObject(update, forkDigest))
|
|
|
|
)
|
2023-02-26 18:18:03 +00:00
|
|
|
|
|
|
|
SSZ.encode(list)
|
|
|
|
|
|
|
|
func decodeForkedLightClientObject(
|
|
|
|
ObjType: type SomeForkedLightClientObject,
|
|
|
|
forkDigests: ForkDigests,
|
2024-02-28 17:31:45 +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:
|
2024-02-28 17:31:45 +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*(
|
2024-02-28 17:31:45 +00:00
|
|
|
forkDigests: ForkDigests, data: openArray[byte]
|
|
|
|
): Result[ForkedLightClientBootstrap, string] =
|
|
|
|
decodeForkedLightClientObject(ForkedLightClientBootstrap, forkDigests, data)
|
2022-11-03 08:12:32 +00:00
|
|
|
|
2023-02-26 18:18:03 +00:00
|
|
|
func decodeLightClientUpdateForked*(
|
2024-02-28 17:31:45 +00:00
|
|
|
forkDigests: ForkDigests, data: openArray[byte]
|
|
|
|
): Result[ForkedLightClientUpdate, string] =
|
|
|
|
decodeForkedLightClientObject(ForkedLightClientUpdate, forkDigests, data)
|
2022-11-03 08:12:32 +00:00
|
|
|
|
2023-02-26 18:18:03 +00:00
|
|
|
func decodeLightClientFinalityUpdateForked*(
|
2024-02-28 17:31:45 +00:00
|
|
|
forkDigests: ForkDigests, data: openArray[byte]
|
|
|
|
): Result[ForkedLightClientFinalityUpdate, string] =
|
|
|
|
decodeForkedLightClientObject(ForkedLightClientFinalityUpdate, forkDigests, data)
|
2022-11-03 08:12:32 +00:00
|
|
|
|
2023-02-26 18:18:03 +00:00
|
|
|
func decodeLightClientOptimisticUpdateForked*(
|
2024-02-28 17:31:45 +00:00
|
|
|
forkDigests: ForkDigests, data: openArray[byte]
|
|
|
|
): Result[ForkedLightClientOptimisticUpdate, string] =
|
|
|
|
decodeForkedLightClientObject(ForkedLightClientOptimisticUpdate, forkDigests, data)
|
2022-11-03 08:12:32 +00:00
|
|
|
|
2023-02-26 18:18:03 +00:00
|
|
|
func decodeLightClientUpdatesByRange*(
|
2024-02-28 17:31:45 +00:00
|
|
|
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:
|
2024-02-28 17:31:45 +00:00
|
|
|
let update = ?decodeLightClientUpdateForked(forkDigests, encodedUpdate.asSeq())
|
2023-02-26 18:18:03 +00:00
|
|
|
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,
|
2024-02-28 17:31:45 +00:00
|
|
|
lightClientBootstrapKey: LightClientBootstrapKey(blockHash: blockHash),
|
2022-12-27 14:25:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func updateContentKey*(startPeriod: uint64, count: uint64): ContentKey =
|
|
|
|
ContentKey(
|
|
|
|
contentType: lightClientUpdate,
|
2024-02-28 17:31:45 +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,
|
2024-02-28 17:31:45 +00:00
|
|
|
lightClientFinalityUpdateKey:
|
|
|
|
LightClientFinalityUpdateKey(finalizedSlot: finalizedSlot),
|
2022-11-18 09:00:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func optimisticUpdateContentKey*(optimisticSlot: uint64): ContentKey =
|
|
|
|
ContentKey(
|
|
|
|
contentType: lightClientOptimisticUpdate,
|
2024-02-28 17:31:45 +00:00
|
|
|
lightClientOptimisticUpdateKey:
|
|
|
|
LightClientOptimisticUpdateKey(optimisticSlot: optimisticSlot),
|
2022-11-18 09:00:06 +00:00
|
|
|
)
|
2024-01-26 22:38:12 +00:00
|
|
|
|
2024-05-13 16:49:21 +00:00
|
|
|
func historicalSummariesContentKey*(epoch: uint64): ContentKey =
|
|
|
|
ContentKey(
|
|
|
|
contentType: historicalSummaries,
|
|
|
|
historicalSummariesKey: HistoricalSummariesKey(epoch: epoch),
|
|
|
|
)
|