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-11-02 17:40:28 +00:00
|
|
|
import ./formats
|
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-11-02 00:58:41 +00:00
|
|
|
logScope:
|
|
|
|
topics = "codex discovery"
|
|
|
|
|
2022-04-13 16:32:35 +00:00
|
|
|
type
|
2022-05-12 21:52:03 +00:00
|
|
|
Discovery* = ref object of RootObj
|
2023-06-19 06:21:03 +00:00
|
|
|
protocol*: discv5.Protocol # dht protocol
|
2022-11-02 00:58:41 +00:00
|
|
|
key: PrivateKey # private key
|
|
|
|
peerId: PeerId # the peer id of the local node
|
|
|
|
announceAddrs: seq[MultiAddress] # addresses announced as part of the provider records
|
|
|
|
providerRecord*: ?SignedPeerRecord # record to advertice node connection information, this carry any
|
|
|
|
# address that the node can be connected on
|
|
|
|
dhtRecord*: ?SignedPeerRecord # record to advertice DHT connection information
|
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*(
|
2023-06-22 15:11:18 +00:00
|
|
|
d: Discovery,
|
|
|
|
peerId: PeerId
|
|
|
|
): Future[?PeerRecord] {.async.} =
|
2023-06-19 06:21:03 +00:00
|
|
|
trace "protocol.resolve..."
|
2023-06-22 15:11:18 +00:00
|
|
|
## Find peer using the given Discovery object
|
|
|
|
##
|
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():
|
2022-11-02 00:58:41 +00:00
|
|
|
node.get().record.data.some
|
2022-04-13 16:32:35 +00:00
|
|
|
else:
|
2022-11-02 00:58:41 +00:00
|
|
|
PeerRecord.none
|
2022-04-13 16:32:35 +00:00
|
|
|
|
2022-05-26 02:29:31 +00:00
|
|
|
method find*(
|
2023-06-22 15:11:18 +00:00
|
|
|
d: Discovery,
|
|
|
|
cid: Cid
|
|
|
|
): Future[seq[SignedPeerRecord]] {.async, base.} =
|
2022-05-12 21:52:03 +00:00
|
|
|
## Find block providers
|
2023-06-22 15:11:18 +00:00
|
|
|
##
|
2022-04-13 16:32:35 +00:00
|
|
|
|
2022-10-27 13:41:34 +00:00
|
|
|
trace "Finding providers for block", cid
|
2022-05-12 21:52:03 +00:00
|
|
|
without providers =?
|
2022-05-26 02:29:31 +00:00
|
|
|
(await d.protocol.getProviders(cid.toNodeId())).mapFailure, error:
|
2022-10-27 13:41:34 +00:00
|
|
|
trace "Error finding providers for block", 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
|
|
|
|
##
|
|
|
|
|
2022-10-27 13:41:34 +00:00
|
|
|
trace "Providing block", cid
|
2022-05-12 21:52:03 +00:00
|
|
|
let
|
|
|
|
nodes = await d.protocol.addProvider(
|
2022-11-02 00:58:41 +00:00
|
|
|
cid.toNodeId(), d.providerRecord.get)
|
2022-05-12 21:52:03 +00:00
|
|
|
|
|
|
|
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*(
|
2023-06-22 15:11:18 +00:00
|
|
|
d: Discovery,
|
|
|
|
host: ca.Address
|
|
|
|
): Future[seq[SignedPeerRecord]] {.async, base.} =
|
2022-05-26 02:29:31 +00:00
|
|
|
## 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(
|
2022-11-02 00:58:41 +00:00
|
|
|
host.toNodeId(), d.providerRecord.get)
|
2022-05-26 02:29:31 +00:00
|
|
|
if nodes.len > 0:
|
|
|
|
trace "Provided to nodes", nodes = nodes.len
|
|
|
|
|
2023-06-22 15:11:18 +00:00
|
|
|
method removeProvider*(
|
|
|
|
d: Discovery,
|
|
|
|
peerId: PeerId
|
|
|
|
): Future[void] {.base.} =
|
|
|
|
## Remove provider from providers table
|
|
|
|
##
|
|
|
|
|
2022-10-05 16:01:21 +00:00
|
|
|
trace "Removing provider", peerId
|
|
|
|
d.protocol.removeProvidersLocal(peerId)
|
|
|
|
|
2022-11-02 00:58:41 +00:00
|
|
|
proc updateAnnounceRecord*(d: Discovery, addrs: openArray[MultiAddress]) =
|
2023-06-22 15:11:18 +00:00
|
|
|
## Update providers record
|
|
|
|
##
|
|
|
|
|
2022-10-27 13:44:56 +00:00
|
|
|
d.announceAddrs = @addrs
|
2022-11-02 00:58:41 +00:00
|
|
|
|
|
|
|
trace "Updating announce record", addrs = d.announceAddrs
|
|
|
|
d.providerRecord = SignedPeerRecord.init(
|
|
|
|
d.key, PeerRecord.init(d.peerId, d.announceAddrs))
|
|
|
|
.expect("Should construct signed record").some
|
2022-09-13 19:37:49 +00:00
|
|
|
|
2022-10-27 13:44:56 +00:00
|
|
|
if not d.protocol.isNil:
|
2022-11-02 00:58:41 +00:00
|
|
|
d.protocol.updateRecord(d.providerRecord)
|
|
|
|
.expect("Should update SPR")
|
|
|
|
|
|
|
|
proc updateDhtRecord*(d: Discovery, ip: ValidIpAddress, port: Port) =
|
2023-06-22 15:11:18 +00:00
|
|
|
## Update providers record
|
|
|
|
##
|
|
|
|
|
2022-11-02 00:58:41 +00:00
|
|
|
trace "Updating Dht record", ip, port = $port
|
|
|
|
d.dhtRecord = SignedPeerRecord.init(
|
|
|
|
d.key, PeerRecord.init(d.peerId, @[
|
|
|
|
MultiAddress.init(
|
|
|
|
ip,
|
|
|
|
IpTransportProtocol.udpProtocol,
|
|
|
|
port)])).expect("Should construct signed record").some
|
2022-10-27 13:44:56 +00:00
|
|
|
|
2023-07-18 05:51:20 +00:00
|
|
|
if not d.protocol.isNil:
|
|
|
|
d.protocol.updateRecord(d.dhtRecord)
|
|
|
|
.expect("Should update SPR")
|
|
|
|
|
2022-10-27 13:44:56 +00:00
|
|
|
proc start*(d: Discovery) {.async.} =
|
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()
|
2022-10-27 13:44:56 +00:00
|
|
|
|
|
|
|
proc new*(
|
2023-06-22 15:11:18 +00:00
|
|
|
T: type Discovery,
|
|
|
|
key: PrivateKey,
|
|
|
|
bindIp = ValidIpAddress.init(IPv4_any()),
|
|
|
|
bindPort = 0.Port,
|
|
|
|
announceAddrs: openArray[MultiAddress],
|
|
|
|
bootstrapNodes: openArray[SignedPeerRecord] = [],
|
|
|
|
store: Datastore = SQLiteDatastore.new(Memory).expect("Should not fail!")
|
|
|
|
): Discovery =
|
|
|
|
## Create a new Discovery node instance for the given key and datastore
|
|
|
|
##
|
2022-10-27 13:44:56 +00:00
|
|
|
|
|
|
|
var
|
2023-06-22 15:11:18 +00:00
|
|
|
self = Discovery(
|
2022-11-02 00:58:41 +00:00
|
|
|
key: key,
|
|
|
|
peerId: PeerId.init(key).expect("Should construct PeerId"))
|
2022-10-27 13:44:56 +00:00
|
|
|
|
2022-11-02 00:58:41 +00:00
|
|
|
self.updateAnnounceRecord(announceAddrs)
|
2022-10-27 13:44:56 +00:00
|
|
|
|
|
|
|
self.protocol = newProtocol(
|
|
|
|
key,
|
2022-11-02 00:58:41 +00:00
|
|
|
bindIp = bindIp.toNormalIp,
|
|
|
|
bindPort = bindPort,
|
|
|
|
record = self.providerRecord.get,
|
2022-10-27 13:44:56 +00:00
|
|
|
bootstrapRecords = bootstrapNodes,
|
|
|
|
rng = Rng.instance(),
|
|
|
|
providers = ProvidersManager.new(store))
|
|
|
|
|
|
|
|
self
|