2022-10-24 12:16:40 +00:00
|
|
|
# Nimbus - 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
|
2022-10-28 07:49:18 +00:00
|
|
|
stew/results, chronos, chronicles,
|
2022-10-24 12:16:40 +00:00
|
|
|
eth/p2p/discoveryv5/[protocol, enr],
|
|
|
|
beacon_chain/spec/forks,
|
|
|
|
beacon_chain/spec/datatypes/[phase0, altair, bellatrix],
|
2023-09-28 16:16:41 +00:00
|
|
|
beacon_chain/gossip_processing/light_client_processor,
|
2022-10-24 12:16:40 +00:00
|
|
|
../../../nimbus/constants,
|
|
|
|
../wire/[portal_protocol, portal_stream, portal_protocol_config],
|
2023-10-20 10:06:25 +00:00
|
|
|
"."/[beacon_content, beacon_db]
|
2023-03-13 20:30:57 +00:00
|
|
|
|
2023-10-20 10:06:25 +00:00
|
|
|
export beacon_content, beacon_db
|
2022-10-24 12:16:40 +00:00
|
|
|
|
|
|
|
logScope:
|
2023-10-20 10:06:25 +00:00
|
|
|
topics = "beacon_network"
|
2022-10-24 12:16:40 +00:00
|
|
|
|
|
|
|
const
|
|
|
|
lightClientProtocolId* = [byte 0x50, 0x1A]
|
|
|
|
|
|
|
|
type
|
2023-10-20 10:06:25 +00:00
|
|
|
BeaconNetwork* = ref object
|
2022-10-24 12:16:40 +00:00
|
|
|
portalProtocol*: PortalProtocol
|
2023-10-20 10:06:25 +00:00
|
|
|
beaconDb*: BeaconDb
|
2023-09-28 16:16:41 +00:00
|
|
|
processor*: ref LightClientProcessor
|
2023-09-04 10:21:01 +00:00
|
|
|
contentQueue*: AsyncQueue[(Opt[NodeId], ContentKeysList, seq[seq[byte]])]
|
2022-10-24 12:16:40 +00:00
|
|
|
forkDigests*: ForkDigests
|
|
|
|
processContentLoop: Future[void]
|
|
|
|
|
2022-11-08 17:31:45 +00:00
|
|
|
func toContentIdHandler(contentKey: ByteList): results.Opt[ContentId] =
|
|
|
|
ok(toContentId(contentKey))
|
2022-10-24 12:16:40 +00:00
|
|
|
|
2023-10-06 13:46:53 +00:00
|
|
|
proc getContent(
|
2023-10-20 10:06:25 +00:00
|
|
|
n: BeaconNetwork, contentKey: ContentKey):
|
2023-10-06 13:46:53 +00:00
|
|
|
Future[results.Opt[seq[byte]]] {.async.} =
|
|
|
|
let
|
|
|
|
contentKeyEncoded = encode(contentKey)
|
|
|
|
contentId = toContentId(contentKeyEncoded)
|
2023-10-18 14:59:44 +00:00
|
|
|
localContent = n.portalProtocol.dbGet(contentKeyEncoded, contentId)
|
|
|
|
|
|
|
|
if localContent.isSome():
|
|
|
|
return localContent
|
|
|
|
|
|
|
|
let contentRes = await n.portalProtocol.contentLookup(
|
2023-10-06 13:46:53 +00:00
|
|
|
contentKeyEncoded, contentId)
|
|
|
|
|
|
|
|
if contentRes.isNone():
|
|
|
|
warn "Failed fetching content from the beacon chain network",
|
|
|
|
contentKey = contentKeyEncoded
|
|
|
|
return Opt.none(seq[byte])
|
|
|
|
else:
|
|
|
|
return Opt.some(contentRes.value().content)
|
|
|
|
|
2022-10-24 12:16:40 +00:00
|
|
|
proc getLightClientBootstrap*(
|
2023-10-20 10:06:25 +00:00
|
|
|
n: BeaconNetwork,
|
2023-02-26 18:18:03 +00:00
|
|
|
trustedRoot: Digest):
|
|
|
|
Future[results.Opt[ForkedLightClientBootstrap]] {.async.} =
|
2022-10-24 12:16:40 +00:00
|
|
|
let
|
2023-10-06 13:46:53 +00:00
|
|
|
contentKey = bootstrapContentKey(trustedRoot)
|
|
|
|
contentResult = await n.getContent(contentKey)
|
|
|
|
|
|
|
|
if contentResult.isNone():
|
|
|
|
return Opt.none(ForkedLightClientBootstrap)
|
2022-10-24 12:16:40 +00:00
|
|
|
|
|
|
|
let
|
2023-10-06 13:46:53 +00:00
|
|
|
bootstrap = contentResult.value()
|
2023-09-22 16:42:02 +00:00
|
|
|
decodingResult = decodeLightClientBootstrapForked(
|
2023-10-06 13:46:53 +00:00
|
|
|
n.forkDigests, bootstrap)
|
2022-10-24 12:16:40 +00:00
|
|
|
|
2023-10-06 13:46:53 +00:00
|
|
|
if decodingResult.isErr():
|
2023-02-26 18:18:03 +00:00
|
|
|
return Opt.none(ForkedLightClientBootstrap)
|
2022-10-24 12:16:40 +00:00
|
|
|
else:
|
|
|
|
# TODO Not doing validation for now, as probably it should be done by layer
|
|
|
|
# above
|
2023-10-06 13:46:53 +00:00
|
|
|
return Opt.some(decodingResult.value())
|
2022-10-24 12:16:40 +00:00
|
|
|
|
2022-10-28 07:49:18 +00:00
|
|
|
proc getLightClientUpdatesByRange*(
|
2023-10-20 10:06:25 +00:00
|
|
|
n: BeaconNetwork,
|
2023-09-22 16:42:02 +00:00
|
|
|
startPeriod: SyncCommitteePeriod,
|
2023-02-26 18:18:03 +00:00
|
|
|
count: uint64):
|
|
|
|
Future[results.Opt[ForkedLightClientUpdateList]] {.async.} =
|
2022-11-18 09:00:06 +00:00
|
|
|
let
|
2023-10-06 13:46:53 +00:00
|
|
|
contentKey = updateContentKey(distinctBase(startPeriod), count)
|
|
|
|
contentResult = await n.getContent(contentKey)
|
|
|
|
|
|
|
|
if contentResult.isNone():
|
|
|
|
return Opt.none(ForkedLightClientUpdateList)
|
2022-11-03 08:12:32 +00:00
|
|
|
|
2022-11-18 09:00:06 +00:00
|
|
|
let
|
2023-10-06 13:46:53 +00:00
|
|
|
updates = contentResult.value()
|
2023-02-26 18:18:03 +00:00
|
|
|
decodingResult = decodeLightClientUpdatesByRange(
|
2023-10-06 13:46:53 +00:00
|
|
|
n.forkDigests, updates)
|
2022-11-18 09:00:06 +00:00
|
|
|
|
2023-10-06 13:46:53 +00:00
|
|
|
if decodingResult.isErr():
|
2023-02-26 18:18:03 +00:00
|
|
|
return Opt.none(ForkedLightClientUpdateList)
|
2022-11-18 09:00:06 +00:00
|
|
|
else:
|
|
|
|
# TODO Not doing validation for now, as probably it should be done by layer
|
|
|
|
# above
|
2023-10-06 13:46:53 +00:00
|
|
|
return Opt.some(decodingResult.value())
|
2022-11-03 08:12:32 +00:00
|
|
|
|
2022-10-28 07:49:18 +00:00
|
|
|
proc getLightClientFinalityUpdate*(
|
2023-10-20 10:06:25 +00:00
|
|
|
n: BeaconNetwork,
|
2023-10-06 13:46:53 +00:00
|
|
|
finalizedSlot: uint64
|
2023-02-26 18:18:03 +00:00
|
|
|
): Future[results.Opt[ForkedLightClientFinalityUpdate]] {.async.} =
|
2022-11-18 09:00:06 +00:00
|
|
|
let
|
2023-10-06 13:46:53 +00:00
|
|
|
contentKey = finalityUpdateContentKey(finalizedSlot)
|
|
|
|
contentResult = await n.getContent(contentKey)
|
2022-11-03 08:12:32 +00:00
|
|
|
|
2023-10-06 13:46:53 +00:00
|
|
|
if contentResult.isNone():
|
2023-02-26 18:18:03 +00:00
|
|
|
return Opt.none(ForkedLightClientFinalityUpdate)
|
2022-11-03 08:12:32 +00:00
|
|
|
|
|
|
|
let
|
2023-10-06 13:46:53 +00:00
|
|
|
finalityUpdate = contentResult.value()
|
2023-02-26 18:18:03 +00:00
|
|
|
decodingResult = decodeLightClientFinalityUpdateForked(
|
2023-09-28 16:16:41 +00:00
|
|
|
n.forkDigests, finalityUpdate)
|
2022-11-03 08:12:32 +00:00
|
|
|
|
2023-10-06 13:46:53 +00:00
|
|
|
if decodingResult.isErr():
|
2023-02-26 18:18:03 +00:00
|
|
|
return Opt.none(ForkedLightClientFinalityUpdate)
|
2022-11-03 08:12:32 +00:00
|
|
|
else:
|
2023-10-06 13:46:53 +00:00
|
|
|
return Opt.some(decodingResult.value())
|
2022-10-28 07:49:18 +00:00
|
|
|
|
|
|
|
proc getLightClientOptimisticUpdate*(
|
2023-10-20 10:06:25 +00:00
|
|
|
n: BeaconNetwork,
|
2023-10-06 13:46:53 +00:00
|
|
|
optimisticSlot: uint64
|
2023-02-26 18:18:03 +00:00
|
|
|
): Future[results.Opt[ForkedLightClientOptimisticUpdate]] {.async.} =
|
2022-11-03 08:12:32 +00:00
|
|
|
|
2022-11-18 09:00:06 +00:00
|
|
|
let
|
2023-10-06 13:46:53 +00:00
|
|
|
contentKey = optimisticUpdateContentKey(optimisticSlot)
|
|
|
|
contentResult = await n.getContent(contentKey)
|
2022-11-03 08:12:32 +00:00
|
|
|
|
2023-10-06 13:46:53 +00:00
|
|
|
if contentResult.isNone():
|
2023-02-26 18:18:03 +00:00
|
|
|
return Opt.none(ForkedLightClientOptimisticUpdate)
|
2022-11-03 08:12:32 +00:00
|
|
|
|
|
|
|
let
|
2023-10-06 13:46:53 +00:00
|
|
|
optimisticUpdate = contentResult.value()
|
2023-02-26 18:18:03 +00:00
|
|
|
decodingResult = decodeLightClientOptimisticUpdateForked(
|
2023-09-28 16:16:41 +00:00
|
|
|
n.forkDigests, optimisticUpdate)
|
2022-11-03 08:12:32 +00:00
|
|
|
|
2023-10-06 13:46:53 +00:00
|
|
|
if decodingResult.isErr():
|
2023-02-26 18:18:03 +00:00
|
|
|
return Opt.none(ForkedLightClientOptimisticUpdate)
|
2022-11-03 08:12:32 +00:00
|
|
|
else:
|
2023-10-06 13:46:53 +00:00
|
|
|
return Opt.some(decodingResult.value())
|
2022-10-28 07:49:18 +00:00
|
|
|
|
2022-10-24 12:16:40 +00:00
|
|
|
proc new*(
|
2023-10-20 10:06:25 +00:00
|
|
|
T: type BeaconNetwork,
|
2022-10-24 12:16:40 +00:00
|
|
|
baseProtocol: protocol.Protocol,
|
2023-10-20 10:06:25 +00:00
|
|
|
beaconDb: BeaconDb,
|
2022-10-24 12:16:40 +00:00
|
|
|
streamManager: StreamManager,
|
|
|
|
forkDigests: ForkDigests,
|
|
|
|
bootstrapRecords: openArray[Record] = [],
|
|
|
|
portalConfig: PortalProtocolConfig = defaultPortalProtocolConfig): T =
|
|
|
|
let
|
2023-10-05 17:29:39 +00:00
|
|
|
contentQueue = newAsyncQueue[(
|
|
|
|
Opt[NodeId], ContentKeysList, seq[seq[byte]])](50)
|
2022-10-24 12:16:40 +00:00
|
|
|
|
|
|
|
stream = streamManager.registerNewStream(contentQueue)
|
|
|
|
|
2023-10-05 17:29:39 +00:00
|
|
|
# Need to adjust the radius to a static max value as for the Beacon chain
|
|
|
|
# network all data must be accepted currently.
|
|
|
|
portalConfigAdjusted = PortalProtocolConfig(
|
|
|
|
tableIpLimits: portalConfig.tableIpLimits,
|
|
|
|
bitsPerHop: portalConfig.bitsPerHop,
|
|
|
|
radiusConfig: RadiusConfig(kind: Static, logRadius: 256),
|
|
|
|
disablePoke: portalConfig.disablePoke)
|
|
|
|
|
2022-10-24 12:16:40 +00:00
|
|
|
portalProtocol = PortalProtocol.new(
|
2022-11-08 17:31:45 +00:00
|
|
|
baseProtocol, lightClientProtocolId,
|
2023-02-26 18:18:03 +00:00
|
|
|
toContentIdHandler,
|
2023-10-20 10:06:25 +00:00
|
|
|
createGetHandler(beaconDb), stream, bootstrapRecords,
|
2023-10-05 17:29:39 +00:00
|
|
|
config = portalConfigAdjusted)
|
2022-10-24 12:16:40 +00:00
|
|
|
|
2023-10-20 10:06:25 +00:00
|
|
|
portalProtocol.dbPut = createStoreHandler(beaconDb)
|
2022-11-08 17:31:45 +00:00
|
|
|
|
2023-10-20 10:06:25 +00:00
|
|
|
BeaconNetwork(
|
2022-10-24 12:16:40 +00:00
|
|
|
portalProtocol: portalProtocol,
|
2023-10-20 10:06:25 +00:00
|
|
|
beaconDb: beaconDb,
|
2022-10-24 12:16:40 +00:00
|
|
|
contentQueue: contentQueue,
|
|
|
|
forkDigests: forkDigests
|
|
|
|
)
|
|
|
|
|
|
|
|
proc validateContent(
|
2023-10-20 10:06:25 +00:00
|
|
|
n: BeaconNetwork, content: seq[byte], contentKey: ByteList):
|
2023-10-18 21:29:20 +00:00
|
|
|
Result[void, string] =
|
2023-09-28 16:16:41 +00:00
|
|
|
let key = contentKey.decode().valueOr:
|
2023-10-18 21:29:20 +00:00
|
|
|
return err("Error decoding content key")
|
2023-09-28 16:16:41 +00:00
|
|
|
|
|
|
|
case key.contentType:
|
2023-12-19 18:59:38 +00:00
|
|
|
of unused:
|
|
|
|
raiseAssert "Should not be used and fail at decoding"
|
2023-09-28 16:16:41 +00:00
|
|
|
of lightClientBootstrap:
|
|
|
|
let decodingResult = decodeLightClientBootstrapForked(
|
|
|
|
n.forkDigests, content)
|
|
|
|
if decodingResult.isOk:
|
|
|
|
# TODO:
|
|
|
|
# Currently only verifying if the content can be decoded.
|
|
|
|
# Later on we need to either provide a list of acceptable bootstraps (not
|
|
|
|
# really scalable and requires quite some configuration) or find some
|
|
|
|
# way to proof these.
|
2023-10-18 14:59:44 +00:00
|
|
|
# They could be proven at moment of creation by checking finality update
|
|
|
|
# its finalized_header. And verifying the current_sync_committee with the
|
|
|
|
# header state root and current_sync_committee_branch?
|
|
|
|
# Perhaps can be expanded to being able to verify back fill by storing
|
|
|
|
# also the past beacon headers (This is sorta stored in a proof format
|
|
|
|
# for history network also)
|
2023-10-18 21:29:20 +00:00
|
|
|
ok()
|
2023-09-28 16:16:41 +00:00
|
|
|
else:
|
2023-10-18 21:29:20 +00:00
|
|
|
err("Error decoding content: " & decodingResult.error)
|
2023-09-28 16:16:41 +00:00
|
|
|
|
|
|
|
of lightClientUpdate:
|
|
|
|
let decodingResult = decodeLightClientUpdatesByRange(
|
|
|
|
n.forkDigests, content)
|
|
|
|
if decodingResult.isOk:
|
|
|
|
# TODO:
|
|
|
|
# Currently only verifying if the content can be decoded.
|
|
|
|
# Eventually only new updates that can be verified because the local
|
|
|
|
# node is synced should be accepted.
|
2023-10-18 21:29:20 +00:00
|
|
|
ok()
|
2023-09-28 16:16:41 +00:00
|
|
|
else:
|
2023-10-18 21:29:20 +00:00
|
|
|
err("Error decoding content: " & decodingResult.error)
|
2023-09-28 16:16:41 +00:00
|
|
|
|
|
|
|
of lightClientFinalityUpdate:
|
2023-10-18 21:29:20 +00:00
|
|
|
let update = decodeLightClientFinalityUpdateForked(
|
|
|
|
n.forkDigests, content).valueOr:
|
|
|
|
return err("Error decoding content: " & error)
|
|
|
|
|
|
|
|
let res = n.processor[].processLightClientFinalityUpdate(
|
|
|
|
MsgSource.gossip, update)
|
|
|
|
if res.isErr():
|
|
|
|
err("Error processing update: " & $res.error[1])
|
2023-09-28 16:16:41 +00:00
|
|
|
else:
|
2023-10-18 21:29:20 +00:00
|
|
|
ok()
|
2023-09-28 16:16:41 +00:00
|
|
|
|
|
|
|
of lightClientOptimisticUpdate:
|
2023-10-18 21:29:20 +00:00
|
|
|
let update = decodeLightClientOptimisticUpdateForked(
|
|
|
|
n.forkDigests, content).valueOr:
|
|
|
|
return err("Error decoding content: " & error)
|
|
|
|
|
|
|
|
let res = n.processor[].processLightClientOptimisticUpdate(
|
|
|
|
MsgSource.gossip, update)
|
|
|
|
if res.isErr():
|
|
|
|
err("Error processing update: " & $res.error[1])
|
2023-09-28 16:16:41 +00:00
|
|
|
else:
|
2023-10-18 21:29:20 +00:00
|
|
|
ok()
|
2022-10-24 12:16:40 +00:00
|
|
|
|
|
|
|
proc validateContent(
|
2023-10-20 10:06:25 +00:00
|
|
|
n: BeaconNetwork,
|
2022-10-24 12:16:40 +00:00
|
|
|
contentKeys: ContentKeysList,
|
|
|
|
contentItems: seq[seq[byte]]): Future[bool] {.async.} =
|
|
|
|
# content passed here can have less items then contentKeys, but not more.
|
|
|
|
for i, contentItem in contentItems:
|
2023-10-18 21:29:20 +00:00
|
|
|
let
|
|
|
|
contentKey = contentKeys[i]
|
|
|
|
validation = n.validateContent(contentItem, contentKey)
|
|
|
|
if validation.isOk():
|
2022-10-24 12:16:40 +00:00
|
|
|
let contentIdOpt = n.portalProtocol.toContentId(contentKey)
|
|
|
|
if contentIdOpt.isNone():
|
|
|
|
error "Received offered content with invalid content key", contentKey
|
|
|
|
return false
|
|
|
|
|
|
|
|
let contentId = contentIdOpt.get()
|
2022-11-08 17:31:45 +00:00
|
|
|
n.portalProtocol.storeContent(contentKey, contentId, contentItem)
|
2022-10-24 12:16:40 +00:00
|
|
|
|
|
|
|
info "Received offered content validated successfully", contentKey
|
|
|
|
|
|
|
|
else:
|
2023-10-18 21:29:20 +00:00
|
|
|
error "Received offered content failed validation",
|
|
|
|
contentKey, error = validation.error
|
2022-10-24 12:16:40 +00:00
|
|
|
return false
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
2023-10-20 10:06:25 +00:00
|
|
|
proc processContentLoop(n: BeaconNetwork) {.async.} =
|
2022-10-24 12:16:40 +00:00
|
|
|
try:
|
|
|
|
while true:
|
2023-09-04 10:21:01 +00:00
|
|
|
let (srcNodeId, contentKeys, contentItems) =
|
2022-10-24 12:16:40 +00:00
|
|
|
await n.contentQueue.popFirst()
|
|
|
|
|
|
|
|
# When there is one invalid content item, all other content items are
|
|
|
|
# dropped and not gossiped around.
|
|
|
|
# TODO: Differentiate between failures due to invalid data and failures
|
|
|
|
# due to missing network data for validation.
|
|
|
|
if await n.validateContent(contentKeys, contentItems):
|
2023-10-05 17:29:39 +00:00
|
|
|
asyncSpawn n.portalProtocol.randomGossipDiscardPeers(
|
2023-09-04 10:21:01 +00:00
|
|
|
srcNodeId, contentKeys, contentItems
|
2022-10-24 12:16:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
except CancelledError:
|
|
|
|
trace "processContentLoop canceled"
|
|
|
|
|
2023-10-20 10:06:25 +00:00
|
|
|
proc start*(n: BeaconNetwork) =
|
2023-10-06 13:46:53 +00:00
|
|
|
info "Starting portal beacon chain network"
|
2022-10-24 12:16:40 +00:00
|
|
|
n.portalProtocol.start()
|
|
|
|
n.processContentLoop = processContentLoop(n)
|
|
|
|
|
2023-10-20 10:06:25 +00:00
|
|
|
proc stop*(n: BeaconNetwork) =
|
2022-10-24 12:16:40 +00:00
|
|
|
n.portalProtocol.stop()
|
|
|
|
|
|
|
|
if not n.processContentLoop.isNil:
|
2023-10-17 12:19:50 +00:00
|
|
|
n.processContentLoop.cancelSoon()
|