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-14 15:07:14 +00:00
|
|
|
../wire/[portal_protocol, portal_stream],
|
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-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:
|
2021-10-09 11:22:03 +00:00
|
|
|
n.contentDB.put(contentId, content.get().asSeq())
|
|
|
|
|
|
|
|
# TODO: for now returning bytes, ultimately it would be nice to return proper
|
|
|
|
# domain types.
|
|
|
|
return content.map(x => x.asSeq())
|
|
|
|
|
2022-01-06 08:06:05 +00:00
|
|
|
proc new*(
|
|
|
|
T: type HistoryNetwork,
|
|
|
|
baseProtocol: protocol.Protocol,
|
|
|
|
contentDB: ContentDB,
|
2022-01-14 15:07:14 +00:00
|
|
|
portalStream: PortalStream,
|
2022-01-06 08:06:05 +00:00
|
|
|
dataRadius = UInt256.high(),
|
2021-12-13 08:06:29 +00:00
|
|
|
bootstrapRecords: openArray[Record] = []): 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-01-14 15:07:14 +00:00
|
|
|
portalStream, dataRadius, bootstrapRecords)
|
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()
|