2021-10-09 11:22:03 +00:00
|
|
|
# Nimbus
|
2022-04-13 05:56:01 +00:00
|
|
|
# Copyright (c) 2021-2022 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],
|
2021-09-28 17:58:41 +00:00
|
|
|
../../content_db,
|
2022-01-18 08:01:22 +00:00
|
|
|
../wire/[portal_protocol, portal_stream, portal_protocol_config],
|
2021-10-05 19:16:33 +00:00
|
|
|
./state_content,
|
2021-10-09 11:22:03 +00:00
|
|
|
./state_distance
|
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
|
2022-08-17 07:32:06 +00:00
|
|
|
contentQueue*: AsyncQueue[(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] =
|
2022-01-06 08:06:05 +00:00
|
|
|
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
|
|
|
|
2022-04-11 17:42:38 +00:00
|
|
|
proc validateContent(content: openArray[byte], contentKey: ByteList): bool =
|
|
|
|
true
|
|
|
|
|
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
|
|
|
|
|
|
|
let cq = newAsyncQueue[(ContentKeysList, seq[seq[byte]])](50)
|
|
|
|
|
|
|
|
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,
|
2022-07-11 14:29:16 +00:00
|
|
|
bootstrapRecords, stateDistanceCalculator,
|
|
|
|
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:
|
|
|
|
# Just dropping state date for now
|
2022-08-22 10:23:26 +00:00
|
|
|
discard await n.contentQueue.popFirst()
|
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:
|
|
|
|
n.processContentLoop.cancel()
|