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.
|
|
|
|
|
|
|
|
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-10-09 11:22:03 +00:00
|
|
|
../../content_db,
|
2022-01-18 08:01:22 +00:00
|
|
|
../wire/[portal_protocol, portal_stream, portal_protocol_config],
|
2021-10-09 11:22:03 +00:00
|
|
|
./history_content
|
|
|
|
|
|
|
|
const
|
2021-12-08 10:54:22 +00:00
|
|
|
historyProtocolId* = [byte 0x50, 0x0B]
|
2021-10-09 11:22:03 +00:00
|
|
|
|
|
|
|
# TODO: Extract common parts from the different networks
|
|
|
|
type HistoryNetwork* = ref object
|
|
|
|
portalProtocol*: PortalProtocol
|
|
|
|
contentDB*: ContentDB
|
|
|
|
|
2022-02-11 13:43:10 +00:00
|
|
|
func setStreamTransport*(n: HistoryNetwork, transport: UtpDiscv5Protocol) =
|
|
|
|
setTransport(n.portalProtocol.stream, transport)
|
|
|
|
|
2022-01-06 08:06:05 +00:00
|
|
|
proc toContentIdHandler(contentKey: ByteList): Option[ContentId] =
|
|
|
|
some(toContentId(contentKey))
|
2021-10-09 11:22:03 +00:00
|
|
|
|
|
|
|
proc getContent*(n: HistoryNetwork, key: ContentKey):
|
|
|
|
Future[Option[seq[byte]]] {.async.} =
|
|
|
|
let
|
|
|
|
keyEncoded = encode(key)
|
|
|
|
contentId = toContentId(keyEncoded)
|
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-01-06 08:06:05 +00:00
|
|
|
# When content is found and is in the radius range, store it.
|
|
|
|
if content.isSome() and contentInRange:
|
2022-01-20 20:21:20 +00:00
|
|
|
n.contentDB.put(contentId, content.get())
|
2021-10-09 11:22:03 +00:00
|
|
|
|
|
|
|
# TODO: for now returning bytes, ultimately it would be nice to return proper
|
|
|
|
# domain types.
|
2022-01-20 20:21:20 +00:00
|
|
|
return content
|
2021-10-09 11:22:03 +00:00
|
|
|
|
2022-01-06 08:06:05 +00:00
|
|
|
proc new*(
|
|
|
|
T: type HistoryNetwork,
|
|
|
|
baseProtocol: protocol.Protocol,
|
|
|
|
contentDB: ContentDB,
|
|
|
|
dataRadius = UInt256.high(),
|
2022-01-18 08:01:22 +00:00
|
|
|
bootstrapRecords: openArray[Record] = [],
|
|
|
|
portalConfig: PortalProtocolConfig = defaultPortalProtocolConfig): T =
|
2021-10-09 11:22:03 +00:00
|
|
|
let portalProtocol = PortalProtocol.new(
|
2022-01-06 08:06:05 +00:00
|
|
|
baseProtocol, historyProtocolId, contentDB, toContentIdHandler,
|
2022-02-11 13:43:10 +00:00
|
|
|
dataRadius, bootstrapRecords,
|
2022-01-18 08:01:22 +00:00
|
|
|
config = portalConfig)
|
2021-10-09 11:22:03 +00:00
|
|
|
|
|
|
|
return HistoryNetwork(portalProtocol: portalProtocol, contentDB: contentDB)
|
|
|
|
|
|
|
|
proc start*(p: HistoryNetwork) =
|
|
|
|
p.portalProtocol.start()
|
|
|
|
|
|
|
|
proc stop*(p: HistoryNetwork) =
|
|
|
|
p.portalProtocol.stop()
|