2021-10-09 11:22:03 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2021 Status Research & Development GmbH
|
|
|
|
# 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
|
|
|
|
std/[options, sugar],
|
2021-11-18 11:06:53 +00:00
|
|
|
stew/results, chronos,
|
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
|
|
|
|
|
|
|
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
|
2021-09-03 08:57:19 +00:00
|
|
|
|
2022-02-11 13:43:10 +00:00
|
|
|
func setStreamTransport*(n: StateNetwork, transport: UtpDiscv5Protocol) =
|
|
|
|
setTransport(n.portalProtocol.stream, transport)
|
|
|
|
|
2022-01-06 08:06:05 +00:00
|
|
|
proc toContentIdHandler(contentKey: ByteList): Option[ContentId] =
|
|
|
|
toContentId(contentKey)
|
2021-09-03 08:57:19 +00:00
|
|
|
|
2021-10-09 11:22:03 +00:00
|
|
|
proc getContent*(n: StateNetwork, key: ContentKey):
|
2021-09-13 13:56:44 +00:00
|
|
|
Future[Option[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():
|
|
|
|
return none[seq[byte]]()
|
|
|
|
|
|
|
|
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
|
|
|
|
n.contentDB.put(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-04-06 11:47:23 +00:00
|
|
|
return 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,
|
|
|
|
dataRadius = UInt256.high(),
|
2022-01-18 08:01:22 +00:00
|
|
|
bootstrapRecords: openArray[Record] = [],
|
|
|
|
portalConfig: PortalProtocolConfig = defaultPortalProtocolConfig): T =
|
2021-09-22 15:07:14 +00:00
|
|
|
let portalProtocol = PortalProtocol.new(
|
2022-04-11 17:42:38 +00:00
|
|
|
baseProtocol, stateProtocolId, contentDB,
|
|
|
|
toContentIdHandler, validateContent,
|
2022-02-11 13:43:10 +00:00
|
|
|
dataRadius, bootstrapRecords, stateDistanceCalculator,
|
2022-01-18 08:01:22 +00:00
|
|
|
config = portalConfig)
|
2021-09-13 13:56:44 +00:00
|
|
|
|
2021-09-28 17:58:41 +00:00
|
|
|
return StateNetwork(portalProtocol: portalProtocol, contentDB: contentDB)
|
2021-09-03 08:57:19 +00:00
|
|
|
|
2021-10-09 11:22:03 +00:00
|
|
|
proc start*(n: StateNetwork) =
|
2022-03-18 12:06:57 +00:00
|
|
|
info "Starting Portal state sub-network",
|
|
|
|
protocolId = n.portalProtocol.protocolId
|
2021-10-09 11:22:03 +00:00
|
|
|
n.portalProtocol.start()
|
2021-09-03 08:57:19 +00:00
|
|
|
|
2021-10-09 11:22:03 +00:00
|
|
|
proc stop*(n: StateNetwork) =
|
|
|
|
n.portalProtocol.stop()
|