2023-12-01 16:20:52 +00:00
|
|
|
# Fluffy
|
2024-01-09 09:32:29 +00:00
|
|
|
# Copyright (c) 2021-2024 Status Research & Development GmbH
|
2021-10-09 11:22:03 +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.
|
|
|
|
|
2021-09-03 08:57:19 +00:00
|
|
|
import
|
2022-04-13 05:56:01 +00:00
|
|
|
stew/results, chronos, chronicles,
|
2022-01-06 08:06:05 +00:00
|
|
|
eth/p2p/discoveryv5/[protocol, enr],
|
2023-12-01 16:20:52 +00:00
|
|
|
../../database/content_db,
|
2022-01-18 08:01:22 +00:00
|
|
|
../wire/[portal_protocol, portal_stream, portal_protocol_config],
|
2024-01-19 17:18:57 +00:00
|
|
|
./state_content
|
2021-09-22 15:07:14 +00:00
|
|
|
|
2022-04-13 05:56:01 +00:00
|
|
|
logScope:
|
|
|
|
topics = "portal_state"
|
|
|
|
|
2021-09-22 15:07:14 +00:00
|
|
|
const
|
2021-12-08 10:54:22 +00:00
|
|
|
stateProtocolId* = [byte 0x50, 0x0A]
|
2021-09-03 08:57:19 +00:00
|
|
|
|
2021-09-22 15:07:14 +00:00
|
|
|
type StateNetwork* = ref object
|
2021-09-03 08:57:19 +00:00
|
|
|
portalProtocol*: PortalProtocol
|
2021-09-28 17:58:41 +00:00
|
|
|
contentDB*: ContentDB
|
2023-09-04 10:21:01 +00:00
|
|
|
contentQueue*: AsyncQueue[(Opt[NodeId], ContentKeysList, seq[seq[byte]])]
|
2022-07-11 14:29:16 +00:00
|
|
|
processContentLoop: Future[void]
|
2021-09-03 08:57:19 +00:00
|
|
|
|
2022-11-08 17:31:45 +00:00
|
|
|
func toContentIdHandler(contentKey: ByteList): results.Opt[ContentId] =
|
2024-01-19 17:18:57 +00:00
|
|
|
ok(toContentId(contentKey))
|
2021-09-03 08:57:19 +00:00
|
|
|
|
2021-10-09 11:22:03 +00:00
|
|
|
proc getContent*(n: StateNetwork, key: ContentKey):
|
2022-12-09 16:59:36 +00:00
|
|
|
Future[Opt[seq[byte]]] {.async.} =
|
2021-09-24 09:22:07 +00:00
|
|
|
let
|
|
|
|
keyEncoded = encode(key)
|
2021-11-17 16:11:17 +00:00
|
|
|
contentId = toContentId(key)
|
2022-01-06 08:06:05 +00:00
|
|
|
contentInRange = n.portalProtocol.inRange(contentId)
|
2021-10-09 11:22:03 +00:00
|
|
|
|
2022-01-06 08:06:05 +00:00
|
|
|
# When the content id is in the radius range, try to look it up in the db.
|
|
|
|
if contentInRange:
|
2021-10-09 11:22:03 +00:00
|
|
|
let contentFromDB = n.contentDB.get(contentId)
|
|
|
|
if contentFromDB.isSome():
|
|
|
|
return contentFromDB
|
|
|
|
|
|
|
|
let content = await n.portalProtocol.contentLookup(keyEncoded, contentId)
|
|
|
|
|
2022-04-06 11:47:23 +00:00
|
|
|
if content.isNone():
|
2022-12-09 16:59:36 +00:00
|
|
|
return Opt.none(seq[byte])
|
2022-04-06 11:47:23 +00:00
|
|
|
|
|
|
|
let contentResult = content.get()
|
|
|
|
|
2022-01-06 08:06:05 +00:00
|
|
|
# When content is found on the network and is in the radius range, store it.
|
|
|
|
if content.isSome() and contentInRange:
|
2022-04-06 11:47:23 +00:00
|
|
|
# TODO Add poke when working on state network
|
2022-05-12 16:04:37 +00:00
|
|
|
# TODO When working on state network, make it possible to pass different
|
|
|
|
# distance functions to store content
|
2022-11-08 17:31:45 +00:00
|
|
|
n.portalProtocol.storeContent(keyEncoded, contentId, contentResult.content)
|
2021-10-09 11:22:03 +00:00
|
|
|
|
|
|
|
# TODO: for now returning bytes, ultimately it would be nice to return proper
|
|
|
|
# domain types.
|
2022-12-09 16:59:36 +00:00
|
|
|
return Opt.some(contentResult.content)
|
2021-09-03 08:57:19 +00:00
|
|
|
|
2024-01-09 09:32:29 +00:00
|
|
|
proc validateContent(
|
|
|
|
n: StateNetwork,
|
|
|
|
contentKey: ByteList,
|
|
|
|
contentValue: seq[byte]): Future[bool] {.async.} =
|
|
|
|
let key = contentKey.decode().valueOr:
|
|
|
|
return false
|
|
|
|
|
|
|
|
case key.contentType:
|
|
|
|
of unused:
|
|
|
|
warn "Received content with unused content type"
|
|
|
|
false
|
|
|
|
of accountTrieNode:
|
|
|
|
true
|
2024-01-19 17:18:57 +00:00
|
|
|
of contractTrieNode:
|
2024-01-09 09:32:29 +00:00
|
|
|
true
|
2024-01-19 17:18:57 +00:00
|
|
|
of contractCode:
|
2024-01-09 09:32:29 +00:00
|
|
|
true
|
|
|
|
|
|
|
|
proc validateContent(
|
|
|
|
n: StateNetwork,
|
|
|
|
contentKeys: ContentKeysList,
|
|
|
|
contentValues: seq[seq[byte]]): Future[bool] {.async.} =
|
|
|
|
for i, contentValue in contentValues:
|
|
|
|
let contentKey = contentKeys[i]
|
|
|
|
if await n.validateContent(contentKey, contentValue):
|
|
|
|
let contentId = n.portalProtocol.toContentId(contentKey).valueOr:
|
|
|
|
error "Received offered content with invalid content key", contentKey
|
|
|
|
return false
|
|
|
|
|
|
|
|
n.portalProtocol.storeContent(contentKey, contentId, contentValue)
|
|
|
|
|
|
|
|
info "Received offered content validated successfully", contentKey
|
|
|
|
else:
|
|
|
|
error "Received offered content failed validation", contentKey
|
|
|
|
return false
|
2022-04-11 17:42:38 +00:00
|
|
|
|
2022-01-06 08:06:05 +00:00
|
|
|
proc new*(
|
|
|
|
T: type StateNetwork,
|
|
|
|
baseProtocol: protocol.Protocol,
|
|
|
|
contentDB: ContentDB,
|
2022-08-17 07:32:06 +00:00
|
|
|
streamManager: StreamManager,
|
2022-01-18 08:01:22 +00:00
|
|
|
bootstrapRecords: openArray[Record] = [],
|
|
|
|
portalConfig: PortalProtocolConfig = defaultPortalProtocolConfig): T =
|
2022-08-17 07:32:06 +00:00
|
|
|
|
2023-09-04 10:21:01 +00:00
|
|
|
let cq = newAsyncQueue[(Opt[NodeId], ContentKeysList, seq[seq[byte]])](50)
|
2022-08-17 07:32:06 +00:00
|
|
|
|
|
|
|
let s = streamManager.registerNewStream(cq)
|
|
|
|
|
2021-09-22 15:07:14 +00:00
|
|
|
let portalProtocol = PortalProtocol.new(
|
2022-11-08 17:31:45 +00:00
|
|
|
baseProtocol, stateProtocolId,
|
|
|
|
toContentIdHandler, createGetHandler(contentDB), s,
|
2024-01-19 17:18:57 +00:00
|
|
|
bootstrapRecords, config = portalConfig)
|
2021-09-13 13:56:44 +00:00
|
|
|
|
2022-11-08 17:31:45 +00:00
|
|
|
portalProtocol.dbPut = createStoreHandler(contentDB, portalConfig.radiusConfig, portalProtocol)
|
|
|
|
|
2022-08-17 07:32:06 +00:00
|
|
|
return StateNetwork(
|
|
|
|
portalProtocol: portalProtocol,
|
|
|
|
contentDB: contentDB,
|
|
|
|
contentQueue: cq
|
|
|
|
)
|
2021-09-03 08:57:19 +00:00
|
|
|
|
2022-07-11 14:29:16 +00:00
|
|
|
proc processContentLoop(n: StateNetwork) {.async.} =
|
|
|
|
try:
|
|
|
|
while true:
|
2024-01-09 09:32:29 +00:00
|
|
|
let (maybeContentId, contentKeys, contentValues) = await n.contentQueue.popFirst()
|
|
|
|
if await n.validateContent(contentKeys, contentValues):
|
|
|
|
asyncSpawn n.portalProtocol.neighborhoodGossipDiscardPeers(
|
|
|
|
maybeContentId, contentKeys, contentValues
|
|
|
|
)
|
2022-07-11 14:29:16 +00:00
|
|
|
except CancelledError:
|
|
|
|
trace "processContentLoop canceled"
|
|
|
|
|
2021-10-09 11:22:03 +00:00
|
|
|
proc start*(n: StateNetwork) =
|
2022-04-13 05:56:01 +00:00
|
|
|
info "Starting Portal execution state network",
|
2022-03-18 12:06:57 +00:00
|
|
|
protocolId = n.portalProtocol.protocolId
|
2021-10-09 11:22:03 +00:00
|
|
|
n.portalProtocol.start()
|
2021-09-03 08:57:19 +00:00
|
|
|
|
2022-07-11 14:29:16 +00:00
|
|
|
n.processContentLoop = processContentLoop(n)
|
|
|
|
|
2021-10-09 11:22:03 +00:00
|
|
|
proc stop*(n: StateNetwork) =
|
|
|
|
n.portalProtocol.stop()
|
2022-07-11 14:29:16 +00:00
|
|
|
|
|
|
|
if not n.processContentLoop.isNil:
|
2023-10-17 12:19:50 +00:00
|
|
|
n.processContentLoop.cancelSoon()
|