2022-05-19 19:56:03 +00:00
|
|
|
## Nim-Codex
|
2022-04-13 16:32:35 +00:00
|
|
|
## Copyright (c) 2022 Status Research & Development GmbH
|
|
|
|
## Licensed under either of
|
|
|
|
## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
## * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
## at your option.
|
|
|
|
## This file may not be copied, modified, or distributed except according to
|
|
|
|
## those terms.
|
|
|
|
|
2022-05-26 02:29:31 +00:00
|
|
|
import std/algorithm
|
|
|
|
|
2022-04-13 16:32:35 +00:00
|
|
|
import pkg/chronos
|
2022-05-12 21:52:03 +00:00
|
|
|
import pkg/chronicles
|
2022-04-13 16:32:35 +00:00
|
|
|
import pkg/libp2p
|
2022-05-26 02:29:31 +00:00
|
|
|
import pkg/libp2p/routing_record
|
|
|
|
import pkg/libp2p/signed_envelope
|
2022-04-13 16:32:35 +00:00
|
|
|
import pkg/questionable
|
|
|
|
import pkg/questionable/results
|
|
|
|
import pkg/stew/shims/net
|
2022-05-26 02:29:31 +00:00
|
|
|
import pkg/contractabi/address as ca
|
2022-04-13 16:32:35 +00:00
|
|
|
import pkg/libp2pdht/discv5/protocol as discv5
|
|
|
|
|
2022-05-12 21:52:03 +00:00
|
|
|
import ./rng
|
|
|
|
import ./errors
|
2022-04-13 16:32:35 +00:00
|
|
|
|
|
|
|
export discv5
|
|
|
|
|
2022-05-26 02:29:31 +00:00
|
|
|
# TODO: If generics in methods had not been
|
|
|
|
# deprecated, this could have been implemented
|
|
|
|
# much more elegantly.
|
|
|
|
|
2022-04-13 16:32:35 +00:00
|
|
|
type
|
2022-05-12 21:52:03 +00:00
|
|
|
Discovery* = ref object of RootObj
|
2022-04-13 16:32:35 +00:00
|
|
|
protocol: discv5.Protocol
|
|
|
|
localInfo: PeerInfo
|
|
|
|
|
|
|
|
proc new*(
|
|
|
|
T: type Discovery,
|
|
|
|
localInfo: PeerInfo,
|
2022-05-19 02:29:15 +00:00
|
|
|
discoveryPort = 0.Port,
|
|
|
|
bootstrapNodes: seq[SignedPeerRecord] = @[],
|
2022-09-30 02:16:59 +00:00
|
|
|
store: Datastore = SQLiteDatastore.new(Memory)
|
|
|
|
.expect("Should not fail!")): T =
|
2022-04-13 16:32:35 +00:00
|
|
|
|
|
|
|
T(
|
|
|
|
protocol: newProtocol(
|
|
|
|
localInfo.privateKey,
|
|
|
|
bindPort = discoveryPort,
|
|
|
|
record = localInfo.signedPeerRecord,
|
|
|
|
bootstrapRecords = bootstrapNodes,
|
2022-09-30 02:16:59 +00:00
|
|
|
rng = Rng.instance(),
|
|
|
|
providers = ProvidersManager.new(store)),
|
2022-05-19 02:29:15 +00:00
|
|
|
localInfo: localInfo)
|
2022-04-13 16:32:35 +00:00
|
|
|
|
2022-05-26 02:29:31 +00:00
|
|
|
proc toNodeId*(cid: Cid): NodeId =
|
|
|
|
## Cid to discovery id
|
|
|
|
##
|
|
|
|
|
|
|
|
readUintBE[256](keccak256.digest(cid.data.buffer).data)
|
|
|
|
|
|
|
|
proc toNodeId*(host: ca.Address): NodeId =
|
|
|
|
## Eth address to discovery id
|
|
|
|
##
|
|
|
|
|
|
|
|
readUintBE[256](keccak256.digest(host.toArray).data)
|
|
|
|
|
2022-04-13 16:32:35 +00:00
|
|
|
proc findPeer*(
|
|
|
|
d: Discovery,
|
|
|
|
peerId: PeerID): Future[?PeerRecord] {.async.} =
|
2022-05-26 02:29:31 +00:00
|
|
|
let
|
|
|
|
node = await d.protocol.resolve(toNodeId(peerId))
|
|
|
|
|
2022-04-13 16:32:35 +00:00
|
|
|
return
|
|
|
|
if node.isSome():
|
|
|
|
some(node.get().record.data)
|
|
|
|
else:
|
|
|
|
none(PeerRecord)
|
|
|
|
|
2022-05-26 02:29:31 +00:00
|
|
|
method find*(
|
2022-04-13 16:32:35 +00:00
|
|
|
d: Discovery,
|
2022-05-12 21:52:03 +00:00
|
|
|
cid: Cid): Future[seq[SignedPeerRecord]] {.async, base.} =
|
|
|
|
## Find block providers
|
|
|
|
##
|
2022-04-13 16:32:35 +00:00
|
|
|
|
2022-05-12 21:52:03 +00:00
|
|
|
trace "Finding providers for block", cid = $cid
|
|
|
|
without providers =?
|
2022-05-26 02:29:31 +00:00
|
|
|
(await d.protocol.getProviders(cid.toNodeId())).mapFailure, error:
|
2022-05-12 21:52:03 +00:00
|
|
|
trace "Error finding providers for block", cid = $cid, error = error.msg
|
2022-04-13 16:32:35 +00:00
|
|
|
|
2022-05-12 21:52:03 +00:00
|
|
|
return providers
|
|
|
|
|
2022-05-26 02:29:31 +00:00
|
|
|
method provide*(d: Discovery, cid: Cid) {.async, base.} =
|
2022-05-12 21:52:03 +00:00
|
|
|
## Provide a bock Cid
|
|
|
|
##
|
|
|
|
|
|
|
|
trace "Providing block", cid = $cid
|
|
|
|
let
|
|
|
|
nodes = await d.protocol.addProvider(
|
2022-05-26 02:29:31 +00:00
|
|
|
cid.toNodeId(),
|
2022-05-12 21:52:03 +00:00
|
|
|
d.localInfo.signedPeerRecord)
|
|
|
|
|
|
|
|
if nodes.len <= 0:
|
|
|
|
trace "Couldn't provide to any nodes!"
|
|
|
|
|
|
|
|
trace "Provided to nodes", nodes = nodes.len
|
2022-04-13 16:32:35 +00:00
|
|
|
|
2022-05-26 02:29:31 +00:00
|
|
|
method find*(
|
|
|
|
d: Discovery,
|
|
|
|
host: ca.Address): Future[seq[SignedPeerRecord]] {.async, base.} =
|
|
|
|
## Find host providers
|
|
|
|
##
|
|
|
|
|
|
|
|
trace "Finding providers for host", host = $host
|
|
|
|
without var providers =?
|
|
|
|
(await d.protocol.getProviders(host.toNodeId())).mapFailure, error:
|
|
|
|
trace "Error finding providers for host", host = $host, exc = error.msg
|
|
|
|
return
|
|
|
|
|
|
|
|
if providers.len <= 0:
|
|
|
|
trace "No providers found", host = $host
|
|
|
|
return
|
|
|
|
|
|
|
|
providers.sort do(a, b: SignedPeerRecord) -> int:
|
|
|
|
system.cmp[uint64](a.data.seqNo, b.data.seqNo)
|
|
|
|
|
|
|
|
return providers
|
|
|
|
|
|
|
|
method provide*(d: Discovery, host: ca.Address) {.async, base.} =
|
|
|
|
## Provide hosts
|
|
|
|
##
|
|
|
|
|
|
|
|
trace "Providing host", host = $host
|
|
|
|
let
|
|
|
|
nodes = await d.protocol.addProvider(
|
|
|
|
host.toNodeId(),
|
|
|
|
d.localInfo.signedPeerRecord)
|
|
|
|
if nodes.len > 0:
|
|
|
|
trace "Provided to nodes", nodes = nodes.len
|
|
|
|
|
2022-10-05 16:01:21 +00:00
|
|
|
method removeProvider*(d: Discovery, peerId: PeerId): Future[void] =
|
|
|
|
## Remove provider from providers table
|
|
|
|
##
|
|
|
|
|
|
|
|
trace "Removing provider", peerId
|
|
|
|
d.protocol.removeProvidersLocal(peerId)
|
|
|
|
|
2022-04-13 16:32:35 +00:00
|
|
|
proc start*(d: Discovery) {.async.} =
|
2022-09-13 19:37:49 +00:00
|
|
|
d.protocol.updateRecord(
|
|
|
|
d.localInfo.signedPeerRecord.some)
|
|
|
|
.expect("updating SPR")
|
|
|
|
|
2022-04-13 16:32:35 +00:00
|
|
|
d.protocol.open()
|
2022-10-01 17:08:44 +00:00
|
|
|
await d.protocol.start()
|
2022-04-13 16:32:35 +00:00
|
|
|
|
|
|
|
proc stop*(d: Discovery) {.async.} =
|
|
|
|
await d.protocol.closeWait()
|