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,
|
2021-09-23 12:26:41 +00:00
|
|
|
eth/p2p/discoveryv5/[protocol, node, enr],
|
2021-09-28 17:58:41 +00:00
|
|
|
../../content_db,
|
2021-09-22 15:07:14 +00:00
|
|
|
../wire/portal_protocol,
|
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
|
|
|
|
2021-09-28 17:58:41 +00:00
|
|
|
proc getHandler(contentDB: ContentDB): ContentHandler =
|
2021-09-22 15:07:14 +00:00
|
|
|
return (proc (contentKey: state_content.ByteList): ContentResult =
|
2021-09-28 17:58:41 +00:00
|
|
|
let contentId = toContentId(contentKey)
|
2021-11-17 16:11:17 +00:00
|
|
|
if contentId.isSome():
|
|
|
|
let maybeContent = contentDB.get(contentId.get())
|
|
|
|
if (maybeContent.isSome()):
|
|
|
|
ContentResult(kind: ContentFound, content: maybeContent.unsafeGet())
|
|
|
|
else:
|
|
|
|
ContentResult(kind: ContentMissing, contentId: contentId.get())
|
2021-09-03 08:57:19 +00:00
|
|
|
else:
|
2021-11-17 16:11:17 +00:00
|
|
|
ContentResult(kind: ContentKeyValidationFailure, error: ""))
|
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)
|
2021-10-09 11:22:03 +00:00
|
|
|
|
|
|
|
let nodeId = n.portalProtocol.localNode.id
|
|
|
|
|
|
|
|
let distance = n.portalProtocol.routingTable.distance(nodeId, contentId)
|
|
|
|
let inRange = distance <= n.portalProtocol.dataRadius
|
|
|
|
|
|
|
|
# When the content id is in our radius range, try to look it up in our db.
|
|
|
|
if inRange:
|
|
|
|
let contentFromDB = n.contentDB.get(contentId)
|
|
|
|
if contentFromDB.isSome():
|
|
|
|
return contentFromDB
|
|
|
|
|
|
|
|
let content = await n.portalProtocol.contentLookup(keyEncoded, contentId)
|
|
|
|
|
|
|
|
if content.isSome() and inRange:
|
|
|
|
n.contentDB.put(contentId, content.get().asSeq())
|
|
|
|
|
|
|
|
# TODO: for now returning bytes, ultimately it would be nice to return proper
|
|
|
|
# domain types.
|
2021-09-24 09:22:07 +00:00
|
|
|
return content.map(x => x.asSeq())
|
2021-09-03 08:57:19 +00:00
|
|
|
|
2021-09-22 15:07:14 +00:00
|
|
|
proc new*(T: type StateNetwork, baseProtocol: protocol.Protocol,
|
2021-09-28 17:58:41 +00:00
|
|
|
contentDB: ContentDB , dataRadius = UInt256.high(),
|
2021-12-13 08:06:29 +00:00
|
|
|
bootstrapRecords: openArray[Record] = []): T =
|
2021-09-22 15:07:14 +00:00
|
|
|
let portalProtocol = PortalProtocol.new(
|
2021-12-08 10:54:22 +00:00
|
|
|
baseProtocol, stateProtocolId, getHandler(contentDB), dataRadius,
|
2021-10-09 11:22:03 +00:00
|
|
|
bootstrapRecords, stateDistanceCalculator)
|
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) =
|
|
|
|
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()
|