2022-07-01 18:19:57 +00:00
|
|
|
# Nim-LibP2P
|
2023-01-20 14:47:40 +00:00
|
|
|
# Copyright (c) 2023 Status Research & Development GmbH
|
2022-07-01 18:19:57 +00:00
|
|
|
# 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.
|
2018-11-19 02:52:11 +00:00
|
|
|
|
2023-06-07 11:12:49 +00:00
|
|
|
{.push raises: [].}
|
2021-05-21 16:27:01 +00:00
|
|
|
|
2018-11-19 02:52:11 +00:00
|
|
|
## This module implementes API for `go-libp2p-daemon`.
|
2021-10-25 08:26:32 +00:00
|
|
|
import std/[os, osproc, strutils, tables, strtabs, sequtils]
|
2021-05-22 18:27:30 +00:00
|
|
|
import pkg/[chronos, chronicles]
|
2020-07-01 06:25:09 +00:00
|
|
|
import ../varint, ../multiaddress, ../multicodec, ../cid, ../peerid
|
2021-01-28 22:12:54 +00:00
|
|
|
import ../wire, ../multihash, ../protobuf/minprotobuf, ../errors
|
2023-05-18 08:24:17 +00:00
|
|
|
import ../crypto/crypto, ../utility
|
2018-11-19 02:52:11 +00:00
|
|
|
|
2021-06-02 13:39:10 +00:00
|
|
|
export peerid, multiaddress, multicodec, multihash, cid, crypto, wire, errors
|
2019-03-04 23:57:18 +00:00
|
|
|
|
2018-11-19 02:52:11 +00:00
|
|
|
when not defined(windows):
|
|
|
|
import posix
|
|
|
|
|
|
|
|
const
|
2019-01-09 17:12:15 +00:00
|
|
|
DefaultSocketPath* = "/unix/tmp/p2pd.sock"
|
|
|
|
DefaultUnixSocketPattern* = "/unix/tmp/nim-p2pd-$1-$2.sock"
|
|
|
|
DefaultIpSocketPattern* = "/ip4/127.0.0.1/tcp/$2"
|
|
|
|
DefaultUnixChildPattern* = "/unix/tmp/nim-p2pd-handle-$1-$2.sock"
|
|
|
|
DefaultIpChildPattern* = "/ip4/127.0.0.1/tcp/$2"
|
2018-11-19 02:52:11 +00:00
|
|
|
DefaultDaemonFile* = "p2pd"
|
|
|
|
|
|
|
|
type
|
2019-08-21 21:42:44 +00:00
|
|
|
IpfsLogLevel* {.pure.} = enum
|
|
|
|
Critical
|
|
|
|
Error
|
|
|
|
Warning
|
|
|
|
Notice
|
|
|
|
Info
|
|
|
|
Debug
|
|
|
|
Trace
|
|
|
|
|
2018-11-19 02:52:11 +00:00
|
|
|
RequestType* {.pure.} = enum
|
2021-10-25 08:26:32 +00:00
|
|
|
IDENTIFY = 0
|
2018-11-19 02:52:11 +00:00
|
|
|
CONNECT = 1
|
|
|
|
STREAM_OPEN = 2
|
|
|
|
STREAM_HANDLER = 3
|
|
|
|
DHT = 4
|
|
|
|
LIST_PEERS = 5
|
|
|
|
CONNMANAGER = 6
|
|
|
|
DISCONNECT = 7
|
2018-12-04 17:53:36 +00:00
|
|
|
PUBSUB = 8
|
2018-11-19 02:52:11 +00:00
|
|
|
|
|
|
|
DHTRequestType* {.pure.} = enum
|
|
|
|
FIND_PEER = 0
|
|
|
|
FIND_PEERS_CONNECTED_TO_PEER = 1
|
|
|
|
FIND_PROVIDERS = 2
|
|
|
|
GET_CLOSEST_PEERS = 3
|
|
|
|
GET_PUBLIC_KEY = 4
|
|
|
|
GET_VALUE = 5
|
|
|
|
SEARCH_VALUE = 6
|
|
|
|
PUT_VALUE = 7
|
|
|
|
PROVIDE = 8
|
|
|
|
|
|
|
|
ConnManagerRequestType* {.pure.} = enum
|
|
|
|
TAG_PEER = 0
|
|
|
|
UNTAG_PEER = 1
|
|
|
|
TRIM = 2
|
|
|
|
|
2018-12-09 16:44:20 +00:00
|
|
|
PSRequestType* {.pure.} = enum
|
|
|
|
GET_TOPICS = 0
|
|
|
|
LIST_PEERS = 1
|
|
|
|
PUBLISH = 2
|
|
|
|
SUBSCRIBE = 3
|
|
|
|
|
2018-11-19 02:52:11 +00:00
|
|
|
ResponseKind* = enum
|
|
|
|
Malformed
|
|
|
|
Error
|
|
|
|
Success
|
|
|
|
|
|
|
|
ResponseType* {.pure.} = enum
|
|
|
|
ERROR = 2
|
|
|
|
STREAMINFO = 3
|
|
|
|
IDENTITY = 4
|
|
|
|
DHT = 5
|
|
|
|
PEERINFO = 6
|
2018-12-04 17:53:36 +00:00
|
|
|
PUBSUB = 7
|
2018-11-19 02:52:11 +00:00
|
|
|
|
|
|
|
DHTResponseType* {.pure.} = enum
|
|
|
|
BEGIN = 0
|
|
|
|
VALUE = 1
|
|
|
|
END = 2
|
|
|
|
|
|
|
|
MultiProtocol* = string
|
|
|
|
DHTValue* = seq[byte]
|
|
|
|
|
|
|
|
P2PStreamFlags* {.pure.} = enum
|
|
|
|
None
|
|
|
|
Closed
|
|
|
|
Inbound
|
|
|
|
Outbound
|
|
|
|
|
2018-12-16 13:51:12 +00:00
|
|
|
P2PDaemonFlags* = enum
|
|
|
|
DHTClient ## Start daemon in DHT client mode
|
|
|
|
DHTFull ## Start daemon with full DHT support
|
|
|
|
Bootstrap ## Start daemon with bootstrap
|
|
|
|
WaitBootstrap
|
|
|
|
## Start daemon with bootstrap and wait until daemon
|
|
|
|
## establish connection to at least 2 peers
|
|
|
|
PSFloodSub ## Enable `FloodSub` protocol in daemon
|
|
|
|
PSGossipSub ## Enable `GossipSub` protocol in daemon
|
|
|
|
PSNoSign ## Disable pubsub message signing (default true)
|
2018-12-17 20:39:25 +00:00
|
|
|
PSStrictSign ## Force strict checking pubsub message signature
|
2019-08-21 21:42:44 +00:00
|
|
|
NATPortMap ## Force daemon to use NAT-PMP.
|
2019-08-28 06:19:16 +00:00
|
|
|
AutoNAT ## Force daemon to use AutoNAT.
|
|
|
|
AutoRelay ## Enables autorelay mode.
|
|
|
|
RelayActive ## Enables active mode for relay.
|
|
|
|
RelayDiscovery ## Enables passive discovery for relay.
|
|
|
|
RelayHop ## Enables hop for relay.
|
2021-12-16 10:05:20 +00:00
|
|
|
NoInlinePeerId ## Disable inlining of peer ID (not yet in #master).
|
2019-08-21 21:42:44 +00:00
|
|
|
NoProcessCtrl ## Process was not spawned.
|
2018-11-19 02:52:11 +00:00
|
|
|
|
|
|
|
P2PStream* = ref object
|
|
|
|
flags*: set[P2PStreamFlags]
|
2021-12-16 10:05:20 +00:00
|
|
|
peer*: PeerId
|
2018-11-19 02:52:11 +00:00
|
|
|
raddress*: MultiAddress
|
|
|
|
protocol*: string
|
|
|
|
transp*: StreamTransport
|
|
|
|
|
2018-12-10 10:38:12 +00:00
|
|
|
P2PServer = object
|
|
|
|
server*: StreamServer
|
2019-01-09 17:12:15 +00:00
|
|
|
address*: MultiAddress
|
2018-12-10 10:38:12 +00:00
|
|
|
|
2018-11-19 02:52:11 +00:00
|
|
|
DaemonAPI* = ref object # pool*: TransportPool
|
|
|
|
flags*: set[P2PDaemonFlags]
|
2019-01-09 17:12:15 +00:00
|
|
|
address*: MultiAddress
|
2018-11-19 02:52:11 +00:00
|
|
|
pattern*: string
|
|
|
|
ucounter*: int
|
|
|
|
process*: Process
|
|
|
|
handlers*: Table[string, P2PStreamCallback]
|
2018-12-10 10:38:12 +00:00
|
|
|
servers*: seq[P2PServer]
|
2019-03-11 09:31:02 +00:00
|
|
|
userData*: RootRef
|
2018-11-19 02:52:11 +00:00
|
|
|
|
|
|
|
PeerInfo* = object
|
2021-12-16 10:05:20 +00:00
|
|
|
peer*: PeerId
|
2018-11-19 17:30:40 +00:00
|
|
|
addresses*: seq[MultiAddress]
|
2018-11-19 02:52:11 +00:00
|
|
|
|
2018-12-09 16:44:20 +00:00
|
|
|
PubsubTicket* = ref object
|
|
|
|
topic*: string
|
|
|
|
handler*: P2PPubSubCallback
|
|
|
|
transp*: StreamTransport
|
|
|
|
|
|
|
|
PubSubMessage* = object
|
2021-12-16 10:05:20 +00:00
|
|
|
peer*: PeerId
|
2018-12-09 16:44:20 +00:00
|
|
|
data*: seq[byte]
|
|
|
|
seqno*: seq[byte]
|
|
|
|
topics*: seq[string]
|
2019-03-06 11:23:12 +00:00
|
|
|
signature*: Signature
|
|
|
|
key*: PublicKey
|
2018-12-09 16:44:20 +00:00
|
|
|
|
2018-11-19 02:52:11 +00:00
|
|
|
P2PStreamCallback* = proc(api: DaemonAPI, stream: P2PStream): Future[void] {.
|
2023-06-07 11:12:49 +00:00
|
|
|
gcsafe, raises: [CatchableError]
|
|
|
|
.}
|
2018-12-09 16:44:20 +00:00
|
|
|
P2PPubSubCallback* = proc(
|
|
|
|
api: DaemonAPI, ticket: PubsubTicket, message: PubSubMessage
|
2023-06-07 11:12:49 +00:00
|
|
|
): Future[bool] {.gcsafe, raises: [CatchableError].}
|
2018-11-19 02:52:11 +00:00
|
|
|
|
2021-01-28 22:12:54 +00:00
|
|
|
DaemonError* = object of LPError
|
|
|
|
DaemonRemoteError* = object of DaemonError
|
|
|
|
DaemonLocalError* = object of DaemonError
|
2018-11-19 02:52:11 +00:00
|
|
|
|
2018-12-09 16:44:20 +00:00
|
|
|
var daemonsCount {.threadvar.}: int
|
|
|
|
|
2020-11-19 02:06:42 +00:00
|
|
|
chronicles.formatIt(PeerInfo):
|
|
|
|
shortLog(it)
|
|
|
|
|
2018-11-19 02:52:11 +00:00
|
|
|
proc requestIdentity(): ProtoBuffer =
|
|
|
|
## https://github.com/libp2p/go-libp2p-daemon/blob/master/conn.go
|
|
|
|
## Processing function `doIdentify(req *pb.Request)`.
|
|
|
|
result = initProtoBuffer({WithVarintLength})
|
2023-05-18 08:24:17 +00:00
|
|
|
result.write(1, safeConvert[uint](RequestType.IDENTIFY))
|
2018-11-19 02:52:11 +00:00
|
|
|
result.finish()
|
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc requestConnect(
|
|
|
|
peerid: PeerId, addresses: openArray[MultiAddress], timeout = 0
|
2018-11-23 10:16:35 +00:00
|
|
|
): ProtoBuffer =
|
2018-11-19 02:52:11 +00:00
|
|
|
## https://github.com/libp2p/go-libp2p-daemon/blob/master/conn.go
|
|
|
|
## Processing function `doConnect(req *pb.Request)`.
|
|
|
|
result = initProtoBuffer({WithVarintLength})
|
|
|
|
var msg = initProtoBuffer()
|
2021-10-25 08:26:32 +00:00
|
|
|
msg.write(1, peerid)
|
2018-11-19 02:52:11 +00:00
|
|
|
for item in addresses:
|
2021-10-25 08:26:32 +00:00
|
|
|
msg.write(2, item.data.buffer)
|
2018-11-23 10:16:35 +00:00
|
|
|
if timeout > 0:
|
2021-10-25 08:26:32 +00:00
|
|
|
msg.write(3, hint64(timeout))
|
2023-05-18 08:24:17 +00:00
|
|
|
result.write(1, safeConvert[uint](RequestType.CONNECT))
|
2021-10-25 08:26:32 +00:00
|
|
|
result.write(2, msg)
|
2018-11-19 02:52:11 +00:00
|
|
|
result.finish()
|
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc requestDisconnect(peerid: PeerId): ProtoBuffer =
|
2018-11-19 02:52:11 +00:00
|
|
|
## https://github.com/libp2p/go-libp2p-daemon/blob/master/conn.go
|
|
|
|
## Processing function `doDisconnect(req *pb.Request)`.
|
|
|
|
result = initProtoBuffer({WithVarintLength})
|
|
|
|
var msg = initProtoBuffer()
|
2021-10-25 08:26:32 +00:00
|
|
|
msg.write(1, peerid)
|
2023-05-18 08:24:17 +00:00
|
|
|
result.write(1, safeConvert[uint](RequestType.DISCONNECT))
|
2021-10-25 08:26:32 +00:00
|
|
|
result.write(7, msg)
|
2018-11-19 02:52:11 +00:00
|
|
|
result.finish()
|
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc requestStreamOpen(
|
|
|
|
peerid: PeerId, protocols: openArray[string], timeout = 0
|
2018-11-23 10:16:35 +00:00
|
|
|
): ProtoBuffer =
|
2018-11-19 02:52:11 +00:00
|
|
|
## https://github.com/libp2p/go-libp2p-daemon/blob/master/conn.go
|
|
|
|
## Processing function `doStreamOpen(req *pb.Request)`.
|
|
|
|
result = initProtoBuffer({WithVarintLength})
|
|
|
|
var msg = initProtoBuffer()
|
2021-10-25 08:26:32 +00:00
|
|
|
msg.write(1, peerid)
|
2018-11-19 02:52:11 +00:00
|
|
|
for item in protocols:
|
2021-10-25 08:26:32 +00:00
|
|
|
msg.write(2, item)
|
2018-11-23 10:16:35 +00:00
|
|
|
if timeout > 0:
|
2021-10-25 08:26:32 +00:00
|
|
|
msg.write(3, hint64(timeout))
|
2023-05-18 08:24:17 +00:00
|
|
|
result.write(1, safeConvert[uint](RequestType.STREAM_OPEN))
|
2021-10-25 08:26:32 +00:00
|
|
|
result.write(3, msg)
|
2018-11-19 02:52:11 +00:00
|
|
|
result.finish()
|
|
|
|
|
2019-01-09 17:12:15 +00:00
|
|
|
proc requestStreamHandler(
|
2021-12-16 10:05:20 +00:00
|
|
|
address: MultiAddress, protocols: openArray[MultiProtocol]
|
|
|
|
): ProtoBuffer =
|
2018-11-19 02:52:11 +00:00
|
|
|
## https://github.com/libp2p/go-libp2p-daemon/blob/master/conn.go
|
|
|
|
## Processing function `doStreamHandler(req *pb.Request)`.
|
|
|
|
result = initProtoBuffer({WithVarintLength})
|
|
|
|
var msg = initProtoBuffer()
|
2021-10-25 08:26:32 +00:00
|
|
|
msg.write(1, address.data.buffer)
|
2018-11-19 02:52:11 +00:00
|
|
|
for item in protocols:
|
2021-10-25 08:26:32 +00:00
|
|
|
msg.write(2, item)
|
2023-05-18 08:24:17 +00:00
|
|
|
result.write(1, safeConvert[uint](RequestType.STREAM_HANDLER))
|
2021-10-25 08:26:32 +00:00
|
|
|
result.write(4, msg)
|
2018-11-19 02:52:11 +00:00
|
|
|
result.finish()
|
|
|
|
|
|
|
|
proc requestListPeers(): ProtoBuffer =
|
|
|
|
## https://github.com/libp2p/go-libp2p-daemon/blob/master/conn.go
|
|
|
|
## Processing function `doListPeers(req *pb.Request)`
|
|
|
|
result = initProtoBuffer({WithVarintLength})
|
2023-05-18 08:24:17 +00:00
|
|
|
result.write(1, safeConvert[uint](RequestType.LIST_PEERS))
|
2018-11-19 02:52:11 +00:00
|
|
|
result.finish()
|
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc requestDHTFindPeer(peer: PeerId, timeout = 0): ProtoBuffer =
|
2018-11-19 02:52:11 +00:00
|
|
|
## https://github.com/libp2p/go-libp2p-daemon/blob/master/dht.go
|
|
|
|
## Processing function `doDHTFindPeer(req *pb.DHTRequest)`.
|
2023-05-18 08:24:17 +00:00
|
|
|
let msgid = safeConvert[uint](DHTRequestType.FIND_PEER)
|
2018-11-19 02:52:11 +00:00
|
|
|
result = initProtoBuffer({WithVarintLength})
|
|
|
|
var msg = initProtoBuffer()
|
2021-10-25 08:26:32 +00:00
|
|
|
msg.write(1, msgid)
|
|
|
|
msg.write(2, peer)
|
2018-11-19 02:52:11 +00:00
|
|
|
if timeout > 0:
|
2021-10-25 08:26:32 +00:00
|
|
|
msg.write(7, hint64(timeout))
|
2018-11-19 02:52:11 +00:00
|
|
|
msg.finish()
|
2023-05-18 08:24:17 +00:00
|
|
|
result.write(1, safeConvert[uint](RequestType.DHT))
|
2021-10-25 08:26:32 +00:00
|
|
|
result.write(5, msg)
|
2018-11-19 02:52:11 +00:00
|
|
|
result.finish()
|
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc requestDHTFindPeersConnectedToPeer(peer: PeerId, timeout = 0): ProtoBuffer =
|
2018-11-19 02:52:11 +00:00
|
|
|
## https://github.com/libp2p/go-libp2p-daemon/blob/master/dht.go
|
|
|
|
## Processing function `doDHTFindPeersConnectedToPeer(req *pb.DHTRequest)`.
|
2023-05-18 08:24:17 +00:00
|
|
|
let msgid = safeConvert[uint](DHTRequestType.FIND_PEERS_CONNECTED_TO_PEER)
|
2018-11-19 02:52:11 +00:00
|
|
|
result = initProtoBuffer({WithVarintLength})
|
|
|
|
var msg = initProtoBuffer()
|
2021-10-25 08:26:32 +00:00
|
|
|
msg.write(1, msgid)
|
|
|
|
msg.write(2, peer)
|
2018-11-19 02:52:11 +00:00
|
|
|
if timeout > 0:
|
2021-10-25 08:26:32 +00:00
|
|
|
msg.write(7, hint64(timeout))
|
2018-11-19 02:52:11 +00:00
|
|
|
msg.finish()
|
2023-05-18 08:24:17 +00:00
|
|
|
result.write(1, safeConvert[uint](RequestType.DHT))
|
2021-10-25 08:26:32 +00:00
|
|
|
result.write(5, msg)
|
2018-11-19 02:52:11 +00:00
|
|
|
result.finish()
|
|
|
|
|
|
|
|
proc requestDHTFindProviders(cid: Cid, count: uint32, timeout = 0): ProtoBuffer =
|
|
|
|
## https://github.com/libp2p/go-libp2p-daemon/blob/master/dht.go
|
|
|
|
## Processing function `doDHTFindProviders(req *pb.DHTRequest)`.
|
2023-05-18 08:24:17 +00:00
|
|
|
let msgid = safeConvert[uint](DHTRequestType.FIND_PROVIDERS)
|
2018-11-19 02:52:11 +00:00
|
|
|
result = initProtoBuffer({WithVarintLength})
|
|
|
|
var msg = initProtoBuffer()
|
2021-10-25 08:26:32 +00:00
|
|
|
msg.write(1, msgid)
|
|
|
|
msg.write(3, cid.data.buffer)
|
|
|
|
msg.write(6, count)
|
2018-11-19 02:52:11 +00:00
|
|
|
if timeout > 0:
|
2021-10-25 08:26:32 +00:00
|
|
|
msg.write(7, hint64(timeout))
|
2018-11-19 02:52:11 +00:00
|
|
|
msg.finish()
|
2023-05-18 08:24:17 +00:00
|
|
|
result.write(1, safeConvert[uint](RequestType.DHT))
|
2021-10-25 08:26:32 +00:00
|
|
|
result.write(5, msg)
|
2018-11-19 02:52:11 +00:00
|
|
|
result.finish()
|
|
|
|
|
|
|
|
proc requestDHTGetClosestPeers(key: string, timeout = 0): ProtoBuffer =
|
|
|
|
## https://github.com/libp2p/go-libp2p-daemon/blob/master/dht.go
|
|
|
|
## Processing function `doDHTGetClosestPeers(req *pb.DHTRequest)`.
|
2023-05-18 08:24:17 +00:00
|
|
|
let msgid = safeConvert[uint](DHTRequestType.GET_CLOSEST_PEERS)
|
2018-11-19 02:52:11 +00:00
|
|
|
result = initProtoBuffer({WithVarintLength})
|
|
|
|
var msg = initProtoBuffer()
|
2021-10-25 08:26:32 +00:00
|
|
|
msg.write(1, msgid)
|
|
|
|
msg.write(4, key)
|
2018-11-19 02:52:11 +00:00
|
|
|
if timeout > 0:
|
2021-10-25 08:26:32 +00:00
|
|
|
msg.write(7, hint64(timeout))
|
2018-11-19 02:52:11 +00:00
|
|
|
msg.finish()
|
2023-05-18 08:24:17 +00:00
|
|
|
result.write(1, safeConvert[uint](RequestType.DHT))
|
2021-10-25 08:26:32 +00:00
|
|
|
result.write(5, msg)
|
2018-11-19 02:52:11 +00:00
|
|
|
result.finish()
|
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc requestDHTGetPublicKey(peer: PeerId, timeout = 0): ProtoBuffer =
|
2018-11-19 02:52:11 +00:00
|
|
|
## https://github.com/libp2p/go-libp2p-daemon/blob/master/dht.go
|
|
|
|
## Processing function `doDHTGetPublicKey(req *pb.DHTRequest)`.
|
2023-05-18 08:24:17 +00:00
|
|
|
let msgid = safeConvert[uint](DHTRequestType.GET_PUBLIC_KEY)
|
2018-11-19 02:52:11 +00:00
|
|
|
result = initProtoBuffer({WithVarintLength})
|
|
|
|
var msg = initProtoBuffer()
|
2021-10-25 08:26:32 +00:00
|
|
|
msg.write(1, msgid)
|
|
|
|
msg.write(2, peer)
|
2018-11-19 02:52:11 +00:00
|
|
|
if timeout > 0:
|
2021-10-25 08:26:32 +00:00
|
|
|
msg.write(7, hint64(timeout))
|
2018-11-19 02:52:11 +00:00
|
|
|
msg.finish()
|
2023-05-18 08:24:17 +00:00
|
|
|
result.write(1, safeConvert[uint](RequestType.DHT))
|
2021-10-25 08:26:32 +00:00
|
|
|
result.write(5, msg)
|
2018-11-19 02:52:11 +00:00
|
|
|
result.finish()
|
|
|
|
|
|
|
|
proc requestDHTGetValue(key: string, timeout = 0): ProtoBuffer =
|
|
|
|
## https://github.com/libp2p/go-libp2p-daemon/blob/master/dht.go
|
|
|
|
## Processing function `doDHTGetValue(req *pb.DHTRequest)`.
|
2023-05-18 08:24:17 +00:00
|
|
|
let msgid = safeConvert[uint](DHTRequestType.GET_VALUE)
|
2018-11-19 02:52:11 +00:00
|
|
|
result = initProtoBuffer({WithVarintLength})
|
|
|
|
var msg = initProtoBuffer()
|
2021-10-25 08:26:32 +00:00
|
|
|
msg.write(1, msgid)
|
|
|
|
msg.write(4, key)
|
2018-11-19 02:52:11 +00:00
|
|
|
if timeout > 0:
|
2021-10-25 08:26:32 +00:00
|
|
|
msg.write(7, hint64(timeout))
|
2018-11-19 02:52:11 +00:00
|
|
|
msg.finish()
|
2023-05-18 08:24:17 +00:00
|
|
|
result.write(1, safeConvert[uint](RequestType.DHT))
|
2021-10-25 08:26:32 +00:00
|
|
|
result.write(5, msg)
|
2018-11-19 02:52:11 +00:00
|
|
|
result.finish()
|
|
|
|
|
|
|
|
proc requestDHTSearchValue(key: string, timeout = 0): ProtoBuffer =
|
|
|
|
## https://github.com/libp2p/go-libp2p-daemon/blob/master/dht.go
|
|
|
|
## Processing function `doDHTSearchValue(req *pb.DHTRequest)`.
|
2023-05-18 08:24:17 +00:00
|
|
|
let msgid = safeConvert[uint](DHTRequestType.SEARCH_VALUE)
|
2018-11-19 02:52:11 +00:00
|
|
|
result = initProtoBuffer({WithVarintLength})
|
|
|
|
var msg = initProtoBuffer()
|
2021-10-25 08:26:32 +00:00
|
|
|
msg.write(1, msgid)
|
|
|
|
msg.write(4, key)
|
2018-11-19 02:52:11 +00:00
|
|
|
if timeout > 0:
|
2021-10-25 08:26:32 +00:00
|
|
|
msg.write(7, hint64(timeout))
|
2018-11-19 02:52:11 +00:00
|
|
|
msg.finish()
|
2023-05-18 08:24:17 +00:00
|
|
|
result.write(1, safeConvert[uint](RequestType.DHT))
|
2021-10-25 08:26:32 +00:00
|
|
|
result.write(5, msg)
|
2018-11-19 02:52:11 +00:00
|
|
|
result.finish()
|
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc requestDHTPutValue(key: string, value: openArray[byte], timeout = 0): ProtoBuffer =
|
2018-11-19 02:52:11 +00:00
|
|
|
## https://github.com/libp2p/go-libp2p-daemon/blob/master/dht.go
|
|
|
|
## Processing function `doDHTPutValue(req *pb.DHTRequest)`.
|
2023-05-18 08:24:17 +00:00
|
|
|
let msgid = safeConvert[uint](DHTRequestType.PUT_VALUE)
|
2018-11-19 02:52:11 +00:00
|
|
|
result = initProtoBuffer({WithVarintLength})
|
|
|
|
var msg = initProtoBuffer()
|
2021-10-25 08:26:32 +00:00
|
|
|
msg.write(1, msgid)
|
|
|
|
msg.write(4, key)
|
|
|
|
msg.write(5, value)
|
2018-11-19 02:52:11 +00:00
|
|
|
if timeout > 0:
|
2021-10-25 08:26:32 +00:00
|
|
|
msg.write(7, hint64(timeout))
|
2018-11-19 02:52:11 +00:00
|
|
|
msg.finish()
|
2023-05-18 08:24:17 +00:00
|
|
|
result.write(1, uint(RequestType.DHT))
|
2021-10-25 08:26:32 +00:00
|
|
|
result.write(5, msg)
|
2018-11-19 02:52:11 +00:00
|
|
|
result.finish()
|
|
|
|
|
2018-12-16 13:51:12 +00:00
|
|
|
proc requestDHTProvide(cid: Cid, timeout = 0): ProtoBuffer =
|
2018-11-19 02:52:11 +00:00
|
|
|
## https://github.com/libp2p/go-libp2p-daemon/blob/master/dht.go
|
|
|
|
## Processing function `doDHTProvide(req *pb.DHTRequest)`.
|
2023-05-18 08:24:17 +00:00
|
|
|
let msgid = safeConvert[uint](DHTRequestType.PROVIDE)
|
2018-11-19 02:52:11 +00:00
|
|
|
result = initProtoBuffer({WithVarintLength})
|
|
|
|
var msg = initProtoBuffer()
|
2021-10-25 08:26:32 +00:00
|
|
|
msg.write(1, msgid)
|
|
|
|
msg.write(3, cid.data.buffer)
|
2018-11-19 02:52:11 +00:00
|
|
|
if timeout > 0:
|
2021-10-25 08:26:32 +00:00
|
|
|
msg.write(7, hint64(timeout))
|
2018-11-19 02:52:11 +00:00
|
|
|
msg.finish()
|
2023-05-18 08:24:17 +00:00
|
|
|
result.write(1, safeConvert[uint](RequestType.DHT))
|
2021-10-25 08:26:32 +00:00
|
|
|
result.write(5, msg)
|
2018-11-19 02:52:11 +00:00
|
|
|
result.finish()
|
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc requestCMTagPeer(peer: PeerId, tag: string, weight: int): ProtoBuffer =
|
2018-11-19 02:52:11 +00:00
|
|
|
## https://github.com/libp2p/go-libp2p-daemon/blob/master/connmgr.go#L18
|
2023-05-18 08:24:17 +00:00
|
|
|
let msgid = safeConvert[uint](ConnManagerRequestType.TAG_PEER)
|
2018-11-19 02:52:11 +00:00
|
|
|
result = initProtoBuffer({WithVarintLength})
|
|
|
|
var msg = initProtoBuffer()
|
2021-10-25 08:26:32 +00:00
|
|
|
msg.write(1, msgid)
|
|
|
|
msg.write(2, peer)
|
|
|
|
msg.write(3, tag)
|
|
|
|
msg.write(4, hint64(weight))
|
2018-11-19 02:52:11 +00:00
|
|
|
msg.finish()
|
2023-05-18 08:24:17 +00:00
|
|
|
result.write(1, safeConvert[uint](RequestType.CONNMANAGER))
|
2021-10-25 08:26:32 +00:00
|
|
|
result.write(6, msg)
|
2018-11-19 02:52:11 +00:00
|
|
|
result.finish()
|
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc requestCMUntagPeer(peer: PeerId, tag: string): ProtoBuffer =
|
2018-11-19 02:52:11 +00:00
|
|
|
## https://github.com/libp2p/go-libp2p-daemon/blob/master/connmgr.go#L33
|
2023-05-18 08:24:17 +00:00
|
|
|
let msgid = safeConvert[uint](ConnManagerRequestType.UNTAG_PEER)
|
2018-11-19 02:52:11 +00:00
|
|
|
result = initProtoBuffer({WithVarintLength})
|
|
|
|
var msg = initProtoBuffer()
|
2021-10-25 08:26:32 +00:00
|
|
|
msg.write(1, msgid)
|
|
|
|
msg.write(2, peer)
|
|
|
|
msg.write(3, tag)
|
2018-11-19 02:52:11 +00:00
|
|
|
msg.finish()
|
2023-05-18 08:24:17 +00:00
|
|
|
result.write(1, safeConvert[uint](RequestType.CONNMANAGER))
|
2021-10-25 08:26:32 +00:00
|
|
|
result.write(6, msg)
|
2018-11-19 02:52:11 +00:00
|
|
|
result.finish()
|
|
|
|
|
|
|
|
proc requestCMTrim(): ProtoBuffer =
|
|
|
|
## https://github.com/libp2p/go-libp2p-daemon/blob/master/connmgr.go#L47
|
2023-05-18 08:24:17 +00:00
|
|
|
let msgid = safeConvert[uint](ConnManagerRequestType.TRIM)
|
2018-11-19 02:52:11 +00:00
|
|
|
result = initProtoBuffer({WithVarintLength})
|
|
|
|
var msg = initProtoBuffer()
|
2021-10-25 08:26:32 +00:00
|
|
|
msg.write(1, msgid)
|
2018-11-19 02:52:11 +00:00
|
|
|
msg.finish()
|
2023-05-18 08:24:17 +00:00
|
|
|
result.write(1, safeConvert[uint](RequestType.CONNMANAGER))
|
2021-10-25 08:26:32 +00:00
|
|
|
result.write(6, msg)
|
2018-11-19 02:52:11 +00:00
|
|
|
result.finish()
|
|
|
|
|
2018-12-09 16:44:20 +00:00
|
|
|
proc requestPSGetTopics(): ProtoBuffer =
|
|
|
|
## https://github.com/libp2p/go-libp2p-daemon/blob/master/pubsub.go
|
|
|
|
## Processing function `doPubsubGetTopics(req *pb.PSRequest)`.
|
2023-05-18 08:24:17 +00:00
|
|
|
let msgid = safeConvert[uint](PSRequestType.GET_TOPICS)
|
2018-12-09 16:44:20 +00:00
|
|
|
result = initProtoBuffer({WithVarintLength})
|
|
|
|
var msg = initProtoBuffer()
|
2021-10-25 08:26:32 +00:00
|
|
|
msg.write(1, msgid)
|
2018-12-09 16:44:20 +00:00
|
|
|
msg.finish()
|
2023-05-18 08:24:17 +00:00
|
|
|
result.write(1, safeConvert[uint](RequestType.PUBSUB))
|
2021-10-25 08:26:32 +00:00
|
|
|
result.write(8, msg)
|
2018-12-09 16:44:20 +00:00
|
|
|
result.finish()
|
|
|
|
|
|
|
|
proc requestPSListPeers(topic: string): ProtoBuffer =
|
|
|
|
## https://github.com/libp2p/go-libp2p-daemon/blob/master/pubsub.go
|
|
|
|
## Processing function `doPubsubListPeers(req *pb.PSRequest)`.
|
2023-05-18 08:24:17 +00:00
|
|
|
let msgid = safeConvert[uint](PSRequestType.LIST_PEERS)
|
2018-12-09 16:44:20 +00:00
|
|
|
result = initProtoBuffer({WithVarintLength})
|
|
|
|
var msg = initProtoBuffer()
|
2021-10-25 08:26:32 +00:00
|
|
|
msg.write(1, msgid)
|
|
|
|
msg.write(2, topic)
|
2018-12-09 16:44:20 +00:00
|
|
|
msg.finish()
|
2023-05-18 08:24:17 +00:00
|
|
|
result.write(1, safeConvert[uint](RequestType.PUBSUB))
|
2021-10-25 08:26:32 +00:00
|
|
|
result.write(8, msg)
|
2018-12-09 16:44:20 +00:00
|
|
|
result.finish()
|
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc requestPSPublish(topic: string, data: openArray[byte]): ProtoBuffer =
|
2018-12-09 16:44:20 +00:00
|
|
|
## https://github.com/libp2p/go-libp2p-daemon/blob/master/pubsub.go
|
|
|
|
## Processing function `doPubsubPublish(req *pb.PSRequest)`.
|
2023-05-18 08:24:17 +00:00
|
|
|
let msgid = safeConvert[uint](PSRequestType.PUBLISH)
|
2018-12-09 16:44:20 +00:00
|
|
|
result = initProtoBuffer({WithVarintLength})
|
|
|
|
var msg = initProtoBuffer()
|
2021-10-25 08:26:32 +00:00
|
|
|
msg.write(1, msgid)
|
|
|
|
msg.write(2, topic)
|
|
|
|
msg.write(3, data)
|
2018-12-09 16:44:20 +00:00
|
|
|
msg.finish()
|
2023-05-18 08:24:17 +00:00
|
|
|
result.write(1, safeConvert[uint](RequestType.PUBSUB))
|
2021-10-25 08:26:32 +00:00
|
|
|
result.write(8, msg)
|
2018-12-09 16:44:20 +00:00
|
|
|
result.finish()
|
|
|
|
|
|
|
|
proc requestPSSubscribe(topic: string): ProtoBuffer =
|
|
|
|
## https://github.com/libp2p/go-libp2p-daemon/blob/master/pubsub.go
|
|
|
|
## Processing function `doPubsubSubscribe(req *pb.PSRequest)`.
|
2023-05-18 08:24:17 +00:00
|
|
|
let msgid = safeConvert[uint](PSRequestType.SUBSCRIBE)
|
2018-12-09 16:44:20 +00:00
|
|
|
result = initProtoBuffer({WithVarintLength})
|
|
|
|
var msg = initProtoBuffer()
|
2021-10-25 08:26:32 +00:00
|
|
|
msg.write(1, msgid)
|
|
|
|
msg.write(2, topic)
|
2018-12-09 16:44:20 +00:00
|
|
|
msg.finish()
|
2023-05-18 08:24:17 +00:00
|
|
|
result.write(1, safeConvert[uint](RequestType.PUBSUB))
|
2021-10-25 08:26:32 +00:00
|
|
|
result.write(8, msg)
|
2018-12-09 16:44:20 +00:00
|
|
|
result.finish()
|
|
|
|
|
2021-10-25 08:26:32 +00:00
|
|
|
proc checkResponse(pb: ProtoBuffer): ResponseKind {.inline.} =
|
2018-11-19 02:52:11 +00:00
|
|
|
result = ResponseKind.Malformed
|
|
|
|
var value: uint64
|
2021-10-25 08:26:32 +00:00
|
|
|
if getRequiredField(pb, 1, value).isOk():
|
2018-11-19 02:52:11 +00:00
|
|
|
if value == 0:
|
|
|
|
result = ResponseKind.Success
|
|
|
|
else:
|
|
|
|
result = ResponseKind.Error
|
|
|
|
|
2023-06-07 11:12:49 +00:00
|
|
|
proc getErrorMessage(pb: ProtoBuffer): string {.inline, raises: [DaemonLocalError].} =
|
2021-10-25 08:26:32 +00:00
|
|
|
var error: seq[byte]
|
|
|
|
if pb.getRequiredField(ResponseType.ERROR.int, error).isOk():
|
|
|
|
if initProtoBuffer(error).getRequiredField(1, result).isErr():
|
2018-11-19 02:52:11 +00:00
|
|
|
raise newException(DaemonLocalError, "Error message is missing!")
|
|
|
|
|
|
|
|
proc recvMessage(conn: StreamTransport): Future[seq[byte]] {.async.} =
|
|
|
|
var
|
|
|
|
size: uint
|
|
|
|
length: int
|
2020-05-31 14:22:49 +00:00
|
|
|
res: VarintResult[void]
|
2018-11-19 02:52:11 +00:00
|
|
|
var buffer = newSeq[byte](10)
|
2018-12-09 16:44:20 +00:00
|
|
|
try:
|
|
|
|
for i in 0 ..< len(buffer):
|
|
|
|
await conn.readExactly(addr buffer[i], 1)
|
|
|
|
res = PB.getUVarint(buffer.toOpenArray(0, i), length, size)
|
2020-05-31 14:22:49 +00:00
|
|
|
if res.isOk():
|
2018-12-09 16:44:20 +00:00
|
|
|
break
|
2022-11-15 13:01:14 +00:00
|
|
|
if res.isErr() or size > 1'u shl 22:
|
2018-12-09 16:44:20 +00:00
|
|
|
buffer.setLen(0)
|
2019-08-25 07:09:54 +00:00
|
|
|
result = buffer
|
|
|
|
return
|
2018-12-09 16:44:20 +00:00
|
|
|
buffer.setLen(size)
|
|
|
|
await conn.readExactly(addr buffer[0], int(size))
|
|
|
|
except TransportIncompleteError:
|
|
|
|
buffer.setLen(0)
|
|
|
|
|
2018-11-19 02:52:11 +00:00
|
|
|
result = buffer
|
|
|
|
|
2021-06-02 13:39:10 +00:00
|
|
|
proc newConnection*(api: DaemonAPI): Future[StreamTransport] {.raises: [LPError].} =
|
2018-12-10 20:55:06 +00:00
|
|
|
result = connect(api.address)
|
|
|
|
|
2019-08-22 10:01:28 +00:00
|
|
|
proc closeConnection*(api: DaemonAPI, transp: StreamTransport): Future[void] =
|
|
|
|
result = transp.closeWait()
|
2018-12-10 20:55:06 +00:00
|
|
|
|
2019-01-09 17:12:15 +00:00
|
|
|
proc socketExists(address: MultiAddress): Future[bool] {.async.} =
|
|
|
|
try:
|
|
|
|
var transp = await connect(address)
|
2019-08-01 05:56:59 +00:00
|
|
|
await transp.closeWait()
|
2019-01-09 17:12:15 +00:00
|
|
|
result = true
|
2023-06-07 11:12:49 +00:00
|
|
|
except CatchableError:
|
2019-01-09 17:12:15 +00:00
|
|
|
result = false
|
2018-12-13 09:17:02 +00:00
|
|
|
|
2019-08-21 21:42:44 +00:00
|
|
|
when defined(windows):
|
2019-06-25 08:27:38 +00:00
|
|
|
proc getCurrentProcessId(): uint32 {.
|
|
|
|
stdcall, dynlib: "kernel32", importc: "GetCurrentProcessId"
|
|
|
|
.}
|
2024-06-11 15:18:06 +00:00
|
|
|
|
2018-12-18 14:31:58 +00:00
|
|
|
proc getProcessId(): int =
|
2019-01-09 17:12:15 +00:00
|
|
|
result = cast[int](getCurrentProcessId())
|
2024-06-11 15:18:06 +00:00
|
|
|
|
2019-08-21 21:42:44 +00:00
|
|
|
else:
|
|
|
|
proc getProcessId(): int =
|
2023-05-18 08:24:17 +00:00
|
|
|
result = int(posix.getpid())
|
2018-12-18 14:31:58 +00:00
|
|
|
|
2019-08-01 05:56:59 +00:00
|
|
|
proc getSocket(pattern: string, count: ptr int): Future[MultiAddress] {.async.} =
|
2018-12-18 14:31:58 +00:00
|
|
|
var sockname = ""
|
|
|
|
var pid = $getProcessId()
|
2019-08-01 05:56:59 +00:00
|
|
|
sockname = pattern % [pid, $(count[])]
|
2020-05-31 14:22:49 +00:00
|
|
|
let tmpma = MultiAddress.init(sockname).tryGet()
|
2019-08-01 05:56:59 +00:00
|
|
|
|
|
|
|
if UNIX.match(tmpma):
|
|
|
|
while true:
|
|
|
|
count[] = count[] + 1
|
|
|
|
sockname = pattern % [pid, $(count[])]
|
2020-05-31 14:22:49 +00:00
|
|
|
var ma = MultiAddress.init(sockname).tryGet()
|
2019-08-01 05:56:59 +00:00
|
|
|
let res = await socketExists(ma)
|
|
|
|
if not res:
|
|
|
|
result = ma
|
|
|
|
break
|
|
|
|
elif TCP.match(tmpma):
|
|
|
|
sockname = pattern % [pid, "0"]
|
2020-05-31 14:22:49 +00:00
|
|
|
var ma = MultiAddress.init(sockname).tryGet()
|
2019-08-01 05:56:59 +00:00
|
|
|
var sock = createAsyncSocket(ma)
|
|
|
|
if sock.bindAsyncSocket(ma):
|
|
|
|
# Socket was successfully bound, then its free to use
|
|
|
|
count[] = count[] + 1
|
|
|
|
var ta = sock.getLocalAddress()
|
|
|
|
sockname = pattern % [pid, $ta.port]
|
2020-05-31 14:22:49 +00:00
|
|
|
result = MultiAddress.init(sockname).tryGet()
|
2019-08-01 05:56:59 +00:00
|
|
|
closeSocket(sock)
|
2018-12-18 14:31:58 +00:00
|
|
|
|
2018-12-16 13:51:12 +00:00
|
|
|
# This is forward declaration needed for newDaemonApi()
|
2023-12-05 07:05:32 +00:00
|
|
|
proc listPeers*(api: DaemonAPI): Future[seq[PeerInfo]] {.async.}
|
2018-12-16 13:51:12 +00:00
|
|
|
|
2019-08-21 21:42:44 +00:00
|
|
|
proc copyEnv(): StringTableRef =
|
|
|
|
## This procedure copy all environment variables into StringTable.
|
|
|
|
result = newStringTable(modeStyleInsensitive)
|
|
|
|
for key, val in envPairs():
|
|
|
|
result[key] = val
|
2019-08-01 05:56:59 +00:00
|
|
|
|
2018-11-19 02:52:11 +00:00
|
|
|
proc newDaemonApi*(
|
|
|
|
flags: set[P2PDaemonFlags] = {},
|
|
|
|
bootstrapNodes: seq[string] = @[],
|
|
|
|
id: string = "",
|
2019-06-12 18:18:08 +00:00
|
|
|
hostAddresses: seq[MultiAddress] = @[],
|
|
|
|
announcedAddresses: seq[MultiAddress] = @[],
|
2018-11-19 02:52:11 +00:00
|
|
|
daemon = DefaultDaemonFile,
|
2019-01-09 17:12:15 +00:00
|
|
|
sockpath = "",
|
2019-08-01 05:56:59 +00:00
|
|
|
patternSock = "",
|
|
|
|
patternHandler = "",
|
2018-12-09 16:44:20 +00:00
|
|
|
poolSize = 10,
|
|
|
|
gossipsubHeartbeatInterval = 0,
|
2018-12-16 13:51:12 +00:00
|
|
|
gossipsubHeartbeatDelay = 0,
|
2019-08-21 21:42:44 +00:00
|
|
|
peersRequired = 2,
|
|
|
|
logFile = "",
|
|
|
|
logLevel = IpfsLogLevel.Debug,
|
|
|
|
): Future[DaemonAPI] {.async.} =
|
2018-12-16 13:51:12 +00:00
|
|
|
## Initialize connection to `go-libp2p-daemon` control socket.
|
2019-01-09 17:12:15 +00:00
|
|
|
##
|
|
|
|
## ``flags`` - set of P2PDaemonFlags.
|
|
|
|
##
|
|
|
|
## ``bootstrapNodes`` - list of bootnode's addresses in MultiAddress format.
|
|
|
|
## (default: @[], which means usage of default nodes inside of
|
|
|
|
## `go-libp2p-daemon`).
|
|
|
|
##
|
|
|
|
## ``id`` - path to file with identification information (default: "" which
|
|
|
|
## means - generate new random identity).
|
|
|
|
##
|
2019-06-12 18:18:08 +00:00
|
|
|
## ``hostAddresses`` - list of multiaddrs the host should listen on.
|
|
|
|
## (default: @[], the daemon will pick a listening port at random).
|
|
|
|
##
|
|
|
|
## ``announcedAddresses`` - list of multiaddrs the host should announce to
|
|
|
|
## the network (default: @[], the daemon will announce its own listening
|
|
|
|
## address).
|
|
|
|
##
|
2019-01-09 17:12:15 +00:00
|
|
|
## ``daemon`` - name of ``go-libp2p-daemon`` executable (default: "p2pd").
|
|
|
|
##
|
|
|
|
## ``sockpath`` - default control socket MultiAddress
|
|
|
|
## (default: "/unix/tmp/p2pd.sock").
|
|
|
|
##
|
|
|
|
## ``patternSock`` - MultiAddress pattern string, used to start multiple
|
2019-08-01 05:56:59 +00:00
|
|
|
## daemons (default on Unix: "/unix/tmp/nim-p2pd-$1-$2.sock", on Windows:
|
|
|
|
## "/ip4/127.0.0.1/tcp/$2").
|
2019-01-09 17:12:15 +00:00
|
|
|
##
|
|
|
|
## ``patternHandler`` - MultiAddress pattern string, used to establish
|
2019-08-01 05:56:59 +00:00
|
|
|
## incoming channels (default on Unix: "/unix/tmp/nim-p2pd-handle-$1-$2.sock",
|
|
|
|
## on Windows: "/ip4/127.0.0.1/tcp/$2").
|
2019-01-09 17:12:15 +00:00
|
|
|
##
|
|
|
|
## ``poolSize`` - size of connections pool (default: 10).
|
|
|
|
##
|
|
|
|
## ``gossipsubHeartbeatInterval`` - GossipSub protocol heartbeat interval in
|
|
|
|
## milliseconds (default: 0, use default `go-libp2p-daemon` values).
|
|
|
|
##
|
|
|
|
## ``gossipsubHeartbeatDelay`` - GossipSub protocol heartbeat delay in
|
|
|
|
## millseconds (default: 0, use default `go-libp2p-daemon` values).
|
|
|
|
##
|
|
|
|
## ``peersRequired`` - Wait until `go-libp2p-daemon` will connect to at least
|
|
|
|
## ``peersRequired`` peers before return from `newDaemonApi()` procedure
|
|
|
|
## (default: 2).
|
2019-08-21 21:42:44 +00:00
|
|
|
##
|
|
|
|
## ``logFile`` - Enable ``go-libp2p-daemon`` logging and store it to file
|
|
|
|
## ``logFile`` (default: "", no logging)
|
|
|
|
##
|
|
|
|
## ``logLevel`` - Set ``go-libp2p-daemon`` logging verbosity level to
|
2019-08-22 10:29:54 +00:00
|
|
|
## ``logLevel`` (default: Debug)
|
2018-12-09 16:44:20 +00:00
|
|
|
var api = new DaemonAPI
|
2018-12-10 20:55:06 +00:00
|
|
|
var args = newSeq[string]()
|
2018-12-12 14:08:55 +00:00
|
|
|
var env: StringTableRef
|
|
|
|
|
2019-08-01 05:56:59 +00:00
|
|
|
when defined(windows):
|
|
|
|
var patternForSocket =
|
|
|
|
if len(patternSock) > 0: patternSock else: DefaultIpSocketPattern
|
|
|
|
var patternForChild =
|
|
|
|
if len(patternHandler) > 0: patternHandler else: DefaultIpChildPattern
|
|
|
|
else:
|
|
|
|
var patternForSocket =
|
|
|
|
if len(patternSock) > 0: patternSock else: DefaultUnixSocketPattern
|
|
|
|
var patternForChild =
|
|
|
|
if len(patternHandler) > 0: patternHandler else: DefaultUnixChildPattern
|
|
|
|
|
2018-12-09 16:44:20 +00:00
|
|
|
api.flags = flags
|
2018-12-10 10:38:12 +00:00
|
|
|
api.servers = newSeq[P2PServer]()
|
2019-08-01 05:56:59 +00:00
|
|
|
api.pattern = patternForChild
|
2018-12-09 16:44:20 +00:00
|
|
|
api.ucounter = 1
|
|
|
|
|
2019-01-09 17:12:15 +00:00
|
|
|
if len(sockpath) == 0:
|
2019-08-21 21:42:44 +00:00
|
|
|
api.flags.excl(NoProcessCtrl)
|
2019-08-01 05:56:59 +00:00
|
|
|
api.address = await getSocket(patternForSocket, addr daemonsCount)
|
2018-12-18 14:31:58 +00:00
|
|
|
else:
|
2020-05-31 14:22:49 +00:00
|
|
|
api.address = MultiAddress.init(sockpath).tryGet()
|
2019-08-21 21:42:44 +00:00
|
|
|
api.flags.incl(NoProcessCtrl)
|
2019-01-09 17:12:15 +00:00
|
|
|
let res = await socketExists(api.address)
|
|
|
|
if not res:
|
|
|
|
raise newException(DaemonLocalError, "Could not connect to remote daemon")
|
2019-08-21 21:42:44 +00:00
|
|
|
result = api
|
|
|
|
return
|
2018-12-09 16:44:20 +00:00
|
|
|
|
2018-12-18 14:31:58 +00:00
|
|
|
# DHTFull and DHTClient could not be present at the same time
|
|
|
|
if DHTFull in flags and DHTClient in flags:
|
|
|
|
api.flags.excl(DHTClient)
|
|
|
|
# PSGossipSub and PSFloodSub could not be present at the same time
|
|
|
|
if PSGossipSub in flags and PSFloodSub in flags:
|
|
|
|
api.flags.excl(PSFloodSub)
|
|
|
|
if DHTFull in api.flags:
|
|
|
|
args.add("-dht")
|
|
|
|
if DHTClient in api.flags:
|
|
|
|
args.add("-dhtClient")
|
|
|
|
if {Bootstrap, WaitBootstrap} * api.flags != {}:
|
|
|
|
args.add("-b")
|
2019-08-21 21:42:44 +00:00
|
|
|
if len(logFile) != 0:
|
|
|
|
env = copyEnv()
|
|
|
|
env["IPFS_LOGGING_FMT"] = "nocolor"
|
|
|
|
env["GOLOG_FILE"] = logFile
|
|
|
|
case logLevel
|
|
|
|
of IpfsLogLevel.Critical:
|
|
|
|
env["IPFS_LOGGING"] = "CRITICAL"
|
|
|
|
of IpfsLogLevel.Error:
|
|
|
|
env["IPFS_LOGGING"] = "ERROR"
|
|
|
|
of IpfsLogLevel.Warning:
|
|
|
|
env["IPFS_LOGGING"] = "WARNING"
|
|
|
|
of IpfsLogLevel.Notice:
|
|
|
|
env["IPFS_LOGGING"] = "NOTICE"
|
|
|
|
of IpfsLogLevel.Info:
|
|
|
|
env["IPFS_LOGGING"] = "INFO"
|
|
|
|
of IpfsLogLevel.Debug:
|
|
|
|
env["IPFS_LOGGING"] = "DEBUG"
|
|
|
|
of IpfsLogLevel.Trace:
|
|
|
|
env["IPFS_LOGGING"] = "DEBUG"
|
|
|
|
env["GOLOG_TRACING_FILE"] = logFile
|
2018-12-18 14:31:58 +00:00
|
|
|
if PSGossipSub in api.flags:
|
|
|
|
args.add("-pubsub")
|
|
|
|
args.add("-pubsubRouter=gossipsub")
|
|
|
|
if gossipsubHeartbeatInterval != 0:
|
|
|
|
let param = $gossipsubHeartbeatInterval & "ms"
|
|
|
|
args.add("-gossipsubHeartbeatInterval=" & param)
|
|
|
|
if gossipsubHeartbeatDelay != 0:
|
|
|
|
let param = $gossipsubHeartbeatDelay & "ms"
|
|
|
|
args.add("-gossipsubHeartbeatInitialDelay=" & param)
|
|
|
|
if PSFloodSub in api.flags:
|
|
|
|
args.add("-pubsub")
|
|
|
|
args.add("-pubsubRouter=floodsub")
|
|
|
|
if api.flags * {PSFloodSub, PSGossipSub} != {}:
|
|
|
|
if PSNoSign in api.flags:
|
|
|
|
args.add("-pubsubSign=false")
|
|
|
|
if PSStrictSign in api.flags:
|
|
|
|
args.add("-pubsubSignStrict=true")
|
|
|
|
if NATPortMap in api.flags:
|
|
|
|
args.add("-natPortMap=true")
|
2019-08-28 06:19:16 +00:00
|
|
|
if AutoNAT in api.flags:
|
|
|
|
args.add("-autonat=true")
|
|
|
|
if AutoRelay in api.flags:
|
|
|
|
args.add("-autoRelay=true")
|
|
|
|
if RelayActive in api.flags:
|
|
|
|
args.add("-relayActive=true")
|
|
|
|
if RelayDiscovery in api.flags:
|
|
|
|
args.add("-relayDiscovery=true")
|
|
|
|
if RelayHop in api.flags:
|
|
|
|
args.add("-relayHop=true")
|
2021-12-16 10:05:20 +00:00
|
|
|
if NoInlinePeerId in api.flags:
|
|
|
|
args.add("-noInlinePeerId=true")
|
2018-12-18 14:31:58 +00:00
|
|
|
if len(bootstrapNodes) > 0:
|
|
|
|
args.add("-bootstrapPeers=" & bootstrapNodes.join(","))
|
|
|
|
if len(id) != 0:
|
|
|
|
args.add("-id=" & id)
|
2019-06-12 18:18:08 +00:00
|
|
|
if len(hostAddresses) > 0:
|
|
|
|
var opt = "-hostAddrs="
|
|
|
|
for i, address in hostAddresses:
|
|
|
|
if i > 0:
|
|
|
|
opt.add ","
|
|
|
|
opt.add $address
|
|
|
|
args.add(opt)
|
|
|
|
if len(announcedAddresses) > 0:
|
|
|
|
var opt = "-announceAddrs="
|
|
|
|
for i, address in announcedAddresses:
|
|
|
|
if i > 0:
|
|
|
|
opt.add ","
|
|
|
|
opt.add $address
|
|
|
|
args.add(opt)
|
2020-06-20 10:56:55 +00:00
|
|
|
args.add("-noise=true")
|
2020-09-21 09:16:29 +00:00
|
|
|
args.add("-quic=false")
|
2019-01-09 17:12:15 +00:00
|
|
|
args.add("-listen=" & $api.address)
|
2018-12-09 16:44:20 +00:00
|
|
|
|
2018-12-10 20:55:06 +00:00
|
|
|
# We are trying to get absolute daemon path.
|
|
|
|
let cmd = findExe(daemon)
|
2020-09-21 09:16:29 +00:00
|
|
|
trace "p2pd cmd", cmd, args
|
2018-12-10 20:55:06 +00:00
|
|
|
if len(cmd) == 0:
|
|
|
|
raise newException(DaemonLocalError, "Could not find daemon executable!")
|
2019-01-09 17:12:15 +00:00
|
|
|
|
2018-12-10 20:55:06 +00:00
|
|
|
# Starting daemon process
|
2019-03-06 11:23:12 +00:00
|
|
|
# echo "Starting ", cmd, " ", args.join(" ")
|
2023-05-18 08:24:17 +00:00
|
|
|
api.process = exceptionToAssert:
|
2022-01-05 15:27:33 +00:00
|
|
|
startProcess(cmd, "", args, env, {poParentStreams})
|
2018-12-10 20:55:06 +00:00
|
|
|
# Waiting until daemon will not be bound to control socket.
|
|
|
|
while true:
|
|
|
|
if not api.process.running():
|
|
|
|
raise newException(DaemonLocalError, "Daemon executable could not be started!")
|
2019-01-09 17:12:15 +00:00
|
|
|
let res = await socketExists(api.address)
|
|
|
|
if res:
|
2018-12-10 20:55:06 +00:00
|
|
|
break
|
2019-03-30 23:32:04 +00:00
|
|
|
await sleepAsync(500.milliseconds)
|
2019-01-09 17:12:15 +00:00
|
|
|
|
2018-12-16 13:51:12 +00:00
|
|
|
if WaitBootstrap in api.flags:
|
|
|
|
while true:
|
|
|
|
var peers = await listPeers(api)
|
|
|
|
if len(peers) >= peersRequired:
|
|
|
|
break
|
2019-03-30 23:32:04 +00:00
|
|
|
await sleepAsync(1.seconds)
|
2018-12-16 13:51:12 +00:00
|
|
|
|
2018-12-09 16:44:20 +00:00
|
|
|
result = api
|
2018-11-19 02:52:11 +00:00
|
|
|
|
2018-12-10 10:38:12 +00:00
|
|
|
proc close*(stream: P2PStream) {.async.} =
|
2018-11-19 02:52:11 +00:00
|
|
|
## Close ``stream``.
|
|
|
|
if P2PStreamFlags.Closed notin stream.flags:
|
2019-08-22 10:01:28 +00:00
|
|
|
await stream.transp.closeWait()
|
2018-11-19 02:52:11 +00:00
|
|
|
stream.transp = nil
|
|
|
|
stream.flags.incl(P2PStreamFlags.Closed)
|
|
|
|
else:
|
|
|
|
raise newException(DaemonLocalError, "Stream is already closed!")
|
|
|
|
|
|
|
|
proc close*(api: DaemonAPI) {.async.} =
|
|
|
|
## Shutdown connections to `go-libp2p-daemon` control socket.
|
2018-12-10 20:55:06 +00:00
|
|
|
# await api.pool.close()
|
2018-11-19 02:52:11 +00:00
|
|
|
# Closing all pending servers.
|
|
|
|
if len(api.servers) > 0:
|
|
|
|
var pending = newSeq[Future[void]]()
|
|
|
|
for server in api.servers:
|
2018-12-10 10:38:12 +00:00
|
|
|
server.server.stop()
|
|
|
|
server.server.close()
|
|
|
|
pending.add(server.server.join())
|
2019-08-01 05:56:59 +00:00
|
|
|
await allFutures(pending)
|
2018-12-10 10:38:12 +00:00
|
|
|
for server in api.servers:
|
2020-11-19 02:06:42 +00:00
|
|
|
let address = initTAddress(server.address).tryGet()
|
2019-01-09 17:12:15 +00:00
|
|
|
discard tryRemoveFile($address)
|
2018-12-10 10:38:12 +00:00
|
|
|
api.servers.setLen(0)
|
2018-11-19 02:52:11 +00:00
|
|
|
# Closing daemon's process.
|
2019-08-22 10:01:28 +00:00
|
|
|
if NoProcessCtrl notin api.flags:
|
2019-08-22 05:56:36 +00:00
|
|
|
when defined(windows):
|
|
|
|
api.process.kill()
|
|
|
|
else:
|
|
|
|
api.process.terminate()
|
2019-08-21 21:42:44 +00:00
|
|
|
discard api.process.waitForExit()
|
2019-01-09 17:12:15 +00:00
|
|
|
# Attempt to delete unix socket endpoint.
|
2020-11-19 02:06:42 +00:00
|
|
|
let address = initTAddress(api.address).tryGet()
|
2019-01-09 17:12:15 +00:00
|
|
|
if address.family == AddressFamily.Unix:
|
|
|
|
discard tryRemoveFile($address)
|
2018-11-19 02:52:11 +00:00
|
|
|
|
|
|
|
template withMessage(m, body: untyped): untyped =
|
|
|
|
let kind = m.checkResponse()
|
|
|
|
if kind == ResponseKind.Error:
|
|
|
|
raise newException(DaemonRemoteError, m.getErrorMessage())
|
|
|
|
elif kind == ResponseKind.Malformed:
|
|
|
|
raise newException(DaemonLocalError, "Malformed message received!")
|
|
|
|
else:
|
|
|
|
body
|
|
|
|
|
|
|
|
proc transactMessage(
|
|
|
|
transp: StreamTransport, pb: ProtoBuffer
|
|
|
|
): Future[ProtoBuffer] {.async.} =
|
|
|
|
let length = pb.getLen()
|
|
|
|
let res = await transp.write(pb.getPtr(), length)
|
|
|
|
if res != length:
|
|
|
|
raise newException(DaemonLocalError, "Could not send message to daemon!")
|
|
|
|
var message = await transp.recvMessage()
|
2018-12-09 16:44:20 +00:00
|
|
|
if len(message) == 0:
|
|
|
|
raise newException(DaemonLocalError, "Incorrect or empty message received!")
|
2018-11-19 02:52:11 +00:00
|
|
|
result = initProtoBuffer(message)
|
|
|
|
|
2021-10-25 08:26:32 +00:00
|
|
|
proc getPeerInfo(pb: ProtoBuffer): PeerInfo {.raises: [DaemonLocalError].} =
|
2018-11-19 02:52:11 +00:00
|
|
|
## Get PeerInfo object from ``pb``.
|
|
|
|
result.addresses = newSeq[MultiAddress]()
|
2021-10-25 08:26:32 +00:00
|
|
|
if pb.getRequiredField(1, result.peer).isErr():
|
|
|
|
raise newException(DaemonLocalError, "Incorrect or empty message received!")
|
2021-05-21 16:27:01 +00:00
|
|
|
|
2021-10-25 08:26:32 +00:00
|
|
|
discard pb.getRepeatedField(2, result.addresses)
|
2018-11-19 02:52:11 +00:00
|
|
|
|
|
|
|
proc identity*(api: DaemonAPI): Future[PeerInfo] {.async.} =
|
|
|
|
## Get Node identity information
|
2018-12-10 20:55:06 +00:00
|
|
|
var transp = await api.newConnection()
|
2018-11-19 02:52:11 +00:00
|
|
|
try:
|
|
|
|
var pb = await transactMessage(transp, requestIdentity())
|
|
|
|
pb.withMessage:
|
2021-10-25 08:26:32 +00:00
|
|
|
var res: seq[byte]
|
|
|
|
if pb.getRequiredField(ResponseType.IDENTITY.int, res).isOk():
|
|
|
|
var resPb = initProtoBuffer(res)
|
|
|
|
result = getPeerInfo(resPb)
|
2018-11-19 02:52:11 +00:00
|
|
|
finally:
|
2018-12-10 20:55:06 +00:00
|
|
|
await api.closeConnection(transp)
|
2018-11-19 02:52:11 +00:00
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc connect*(
|
2018-11-27 12:33:41 +00:00
|
|
|
api: DaemonAPI, peer: PeerId, addresses: seq[MultiAddress], timeout = 0
|
|
|
|
) {.async.} =
|
2018-11-19 02:52:11 +00:00
|
|
|
## Connect to remote peer with id ``peer`` and addresses ``addresses``.
|
2018-12-10 20:55:06 +00:00
|
|
|
var transp = await api.newConnection()
|
2018-11-19 02:52:11 +00:00
|
|
|
try:
|
2018-11-27 12:33:41 +00:00
|
|
|
var pb = await transp.transactMessage(requestConnect(peer, addresses, timeout))
|
2018-11-19 02:52:11 +00:00
|
|
|
pb.withMessage:
|
|
|
|
discard
|
2023-06-07 11:12:49 +00:00
|
|
|
except CatchableError:
|
2018-12-10 20:55:06 +00:00
|
|
|
await api.closeConnection(transp)
|
2018-11-19 02:52:11 +00:00
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc disconnect*(api: DaemonAPI, peer: PeerId) {.async.} =
|
2018-11-19 02:52:11 +00:00
|
|
|
## Disconnect from remote peer with id ``peer``.
|
2018-12-10 20:55:06 +00:00
|
|
|
var transp = await api.newConnection()
|
2018-11-19 02:52:11 +00:00
|
|
|
try:
|
|
|
|
var pb = await transp.transactMessage(requestDisconnect(peer))
|
|
|
|
pb.withMessage:
|
|
|
|
discard
|
|
|
|
finally:
|
2018-12-10 20:55:06 +00:00
|
|
|
await api.closeConnection(transp)
|
2018-11-19 02:52:11 +00:00
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc openStream*(
|
|
|
|
api: DaemonAPI, peer: PeerId, protocols: seq[string], timeout = 0
|
2018-11-27 12:33:41 +00:00
|
|
|
): Future[P2PStream] {.async.} =
|
2018-11-19 02:52:11 +00:00
|
|
|
## Open new stream to peer ``peer`` using one of the protocols in
|
|
|
|
## ``protocols``. Returns ``StreamTransport`` for the stream.
|
2018-12-10 20:55:06 +00:00
|
|
|
var transp = await api.newConnection()
|
2018-11-19 02:52:11 +00:00
|
|
|
var stream = new P2PStream
|
|
|
|
try:
|
2018-11-27 12:33:41 +00:00
|
|
|
var pb = await transp.transactMessage(requestStreamOpen(peer, protocols, timeout))
|
2018-11-19 02:52:11 +00:00
|
|
|
pb.withMessage:
|
2021-10-25 08:26:32 +00:00
|
|
|
var res: seq[byte]
|
|
|
|
if pb.getRequiredField(ResponseType.STREAMINFO.int, res).isOk():
|
|
|
|
let resPb = initProtoBuffer(res)
|
2019-03-04 18:22:38 +00:00
|
|
|
# stream.peer = newSeq[byte]()
|
2018-11-27 12:16:04 +00:00
|
|
|
var raddress = newSeq[byte]()
|
2018-11-19 02:52:11 +00:00
|
|
|
stream.protocol = ""
|
2021-10-25 08:26:32 +00:00
|
|
|
resPb.getRequiredField(1, stream.peer).tryGet()
|
|
|
|
resPb.getRequiredField(2, raddress).tryGet()
|
2020-05-31 14:22:49 +00:00
|
|
|
stream.raddress = MultiAddress.init(raddress).tryGet()
|
2021-10-25 08:26:32 +00:00
|
|
|
resPb.getRequiredField(3, stream.protocol).tryGet()
|
2018-11-19 02:52:11 +00:00
|
|
|
stream.flags.incl(Outbound)
|
|
|
|
stream.transp = transp
|
|
|
|
result = stream
|
2022-01-05 15:27:33 +00:00
|
|
|
except CatchableError as exc:
|
2018-12-10 20:55:06 +00:00
|
|
|
await api.closeConnection(transp)
|
2019-11-29 19:44:37 +00:00
|
|
|
raise exc
|
2018-11-19 02:52:11 +00:00
|
|
|
|
|
|
|
proc streamHandler(server: StreamServer, transp: StreamTransport) {.async.} =
|
|
|
|
var api = getUserData[DaemonAPI](server)
|
|
|
|
var message = await transp.recvMessage()
|
|
|
|
var pb = initProtoBuffer(message)
|
|
|
|
var stream = new P2PStream
|
2018-11-27 12:16:04 +00:00
|
|
|
var raddress = newSeq[byte]()
|
2018-11-19 02:52:11 +00:00
|
|
|
stream.protocol = ""
|
2021-10-25 08:26:32 +00:00
|
|
|
pb.getRequiredField(1, stream.peer).tryGet()
|
|
|
|
pb.getRequiredField(2, raddress).tryGet()
|
2020-05-31 14:22:49 +00:00
|
|
|
stream.raddress = MultiAddress.init(raddress).tryGet()
|
2021-10-25 08:26:32 +00:00
|
|
|
pb.getRequiredField(3, stream.protocol).tryGet()
|
2018-11-19 02:52:11 +00:00
|
|
|
stream.flags.incl(Inbound)
|
|
|
|
stream.transp = transp
|
|
|
|
if len(stream.protocol) > 0:
|
|
|
|
var handler = api.handlers.getOrDefault(stream.protocol)
|
|
|
|
if not isNil(handler):
|
2021-06-02 13:39:10 +00:00
|
|
|
asyncSpawn handler(api, stream)
|
2018-11-19 02:52:11 +00:00
|
|
|
|
|
|
|
proc addHandler*(
|
|
|
|
api: DaemonAPI, protocols: seq[string], handler: P2PStreamCallback
|
2023-06-07 11:12:49 +00:00
|
|
|
) {.async, raises: [LPError].} =
|
2018-11-19 02:52:11 +00:00
|
|
|
## Add stream handler ``handler`` for set of protocols ``protocols``.
|
2018-12-10 20:55:06 +00:00
|
|
|
var transp = await api.newConnection()
|
2019-08-01 05:56:59 +00:00
|
|
|
let maddress = await getSocket(api.pattern, addr api.ucounter)
|
2019-01-09 17:12:15 +00:00
|
|
|
var server = createStreamServer(maddress, streamHandler, udata = api)
|
2018-11-19 02:52:11 +00:00
|
|
|
try:
|
|
|
|
for item in protocols:
|
|
|
|
api.handlers[item] = handler
|
|
|
|
server.start()
|
2019-01-09 17:12:15 +00:00
|
|
|
var pb = await transp.transactMessage(requestStreamHandler(maddress, protocols))
|
2018-11-19 02:52:11 +00:00
|
|
|
pb.withMessage:
|
2019-01-09 17:12:15 +00:00
|
|
|
api.servers.add(P2PServer(server: server, address: maddress))
|
2022-01-05 15:27:33 +00:00
|
|
|
except CatchableError as exc:
|
2018-11-19 02:52:11 +00:00
|
|
|
for item in protocols:
|
|
|
|
api.handlers.del(item)
|
|
|
|
server.stop()
|
|
|
|
server.close()
|
|
|
|
await server.join()
|
2019-11-29 19:44:37 +00:00
|
|
|
raise exc
|
|
|
|
finally:
|
2018-12-10 20:55:06 +00:00
|
|
|
await api.closeConnection(transp)
|
2018-11-19 02:52:11 +00:00
|
|
|
|
|
|
|
proc listPeers*(api: DaemonAPI): Future[seq[PeerInfo]] {.async.} =
|
|
|
|
## Get list of remote peers to which we are currently connected.
|
2018-12-10 20:55:06 +00:00
|
|
|
var transp = await api.newConnection()
|
2018-11-19 02:52:11 +00:00
|
|
|
try:
|
|
|
|
var pb = await transp.transactMessage(requestListPeers())
|
|
|
|
pb.withMessage:
|
|
|
|
result = newSeq[PeerInfo]()
|
2021-10-25 08:26:32 +00:00
|
|
|
var ress: seq[seq[byte]]
|
|
|
|
if pb.getRequiredRepeatedField(ResponseType.PEERINFO.int, ress).isOk():
|
|
|
|
for p in ress:
|
|
|
|
let peer = initProtoBuffer(p).getPeerInfo()
|
2018-11-19 02:52:11 +00:00
|
|
|
result.add(peer)
|
|
|
|
finally:
|
2018-12-10 20:55:06 +00:00
|
|
|
await api.closeConnection(transp)
|
2018-11-19 02:52:11 +00:00
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc cmTagPeer*(api: DaemonAPI, peer: PeerId, tag: string, weight: int) {.async.} =
|
2018-11-19 02:52:11 +00:00
|
|
|
## Tag peer with id ``peer`` using ``tag`` and ``weight``.
|
2018-12-10 20:55:06 +00:00
|
|
|
var transp = await api.newConnection()
|
2018-11-19 02:52:11 +00:00
|
|
|
try:
|
|
|
|
var pb = await transp.transactMessage(requestCMTagPeer(peer, tag, weight))
|
|
|
|
withMessage(pb):
|
|
|
|
discard
|
|
|
|
finally:
|
2018-12-10 20:55:06 +00:00
|
|
|
await api.closeConnection(transp)
|
2018-11-19 02:52:11 +00:00
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc cmUntagPeer*(api: DaemonAPI, peer: PeerId, tag: string) {.async.} =
|
2018-11-19 02:52:11 +00:00
|
|
|
## Remove tag ``tag`` from peer with id ``peer``.
|
2018-12-10 20:55:06 +00:00
|
|
|
var transp = await api.newConnection()
|
2018-11-19 02:52:11 +00:00
|
|
|
try:
|
|
|
|
var pb = await transp.transactMessage(requestCMUntagPeer(peer, tag))
|
|
|
|
withMessage(pb):
|
|
|
|
discard
|
|
|
|
finally:
|
2018-12-10 20:55:06 +00:00
|
|
|
await api.closeConnection(transp)
|
2018-11-19 02:52:11 +00:00
|
|
|
|
|
|
|
proc cmTrimPeers*(api: DaemonAPI) {.async.} =
|
|
|
|
## Trim all connections.
|
2018-12-10 20:55:06 +00:00
|
|
|
var transp = await api.newConnection()
|
2018-11-19 02:52:11 +00:00
|
|
|
try:
|
|
|
|
var pb = await transp.transactMessage(requestCMTrim())
|
|
|
|
withMessage(pb):
|
|
|
|
discard
|
|
|
|
finally:
|
2018-12-10 20:55:06 +00:00
|
|
|
await api.closeConnection(transp)
|
2018-11-19 02:52:11 +00:00
|
|
|
|
2021-10-25 08:26:32 +00:00
|
|
|
proc dhtGetSinglePeerInfo(pb: ProtoBuffer): PeerInfo {.raises: [DaemonLocalError].} =
|
|
|
|
var res: seq[byte]
|
|
|
|
if pb.getRequiredField(2, res).isOk():
|
|
|
|
result = initProtoBuffer(res).getPeerInfo()
|
2018-11-19 02:52:11 +00:00
|
|
|
else:
|
|
|
|
raise newException(DaemonLocalError, "Missing required field `peer`!")
|
|
|
|
|
2021-10-25 08:26:32 +00:00
|
|
|
proc dhtGetSingleValue(pb: ProtoBuffer): seq[byte] {.raises: [DaemonLocalError].} =
|
2018-11-19 02:52:11 +00:00
|
|
|
result = newSeq[byte]()
|
2021-10-25 08:26:32 +00:00
|
|
|
if pb.getRequiredField(3, result).isErr():
|
2018-11-19 02:52:11 +00:00
|
|
|
raise newException(DaemonLocalError, "Missing field `value`!")
|
|
|
|
|
2021-10-25 08:26:32 +00:00
|
|
|
proc dhtGetSinglePublicKey(pb: ProtoBuffer): PublicKey {.raises: [DaemonLocalError].} =
|
|
|
|
if pb.getRequiredField(3, result).isErr():
|
2019-03-06 11:23:12 +00:00
|
|
|
raise newException(DaemonLocalError, "Missing field `value`!")
|
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc dhtGetSinglePeerId(pb: ProtoBuffer): PeerId {.raises: [DaemonLocalError].} =
|
2021-10-25 08:26:32 +00:00
|
|
|
if pb.getRequiredField(3, result).isErr():
|
2019-03-04 18:22:38 +00:00
|
|
|
raise newException(DaemonLocalError, "Missing field `value`!")
|
|
|
|
|
2022-07-27 11:47:50 +00:00
|
|
|
proc enterDhtMessage(
|
|
|
|
pb: ProtoBuffer, rt: DHTResponseType
|
2023-06-07 11:12:49 +00:00
|
|
|
): ProtoBuffer {.inline, raises: [DaemonLocalError].} =
|
2021-10-25 08:26:32 +00:00
|
|
|
var dhtResponse: seq[byte]
|
|
|
|
if pb.getRequiredField(ResponseType.DHT.int, dhtResponse).isOk():
|
|
|
|
var pbDhtResponse = initProtoBuffer(dhtResponse)
|
|
|
|
var dtype: uint
|
|
|
|
if pbDhtResponse.getRequiredField(1, dtype).isErr():
|
2018-11-19 02:52:11 +00:00
|
|
|
raise newException(DaemonLocalError, "Missing required DHT field `type`!")
|
2023-05-18 08:24:17 +00:00
|
|
|
if dtype != safeConvert[uint](rt):
|
2018-11-19 02:52:11 +00:00
|
|
|
raise newException(DaemonLocalError, "Wrong DHT answer type! ")
|
2021-10-25 08:26:32 +00:00
|
|
|
|
|
|
|
var value: seq[byte]
|
|
|
|
if pbDhtResponse.getRequiredField(3, value).isErr():
|
|
|
|
raise newException(DaemonLocalError, "Missing required DHT field `value`!")
|
2023-12-05 07:05:32 +00:00
|
|
|
|
2021-10-25 08:26:32 +00:00
|
|
|
return initProtoBuffer(value)
|
2018-11-19 02:52:11 +00:00
|
|
|
else:
|
|
|
|
raise newException(DaemonLocalError, "Wrong message type!")
|
|
|
|
|
2021-10-25 08:26:32 +00:00
|
|
|
proc enterPsMessage(
|
|
|
|
pb: ProtoBuffer
|
2023-06-07 11:12:49 +00:00
|
|
|
): ProtoBuffer {.inline, raises: [DaemonLocalError].} =
|
2021-10-25 08:26:32 +00:00
|
|
|
var res: seq[byte]
|
|
|
|
if pb.getRequiredField(ResponseType.PUBSUB.int, res).isErr():
|
2018-12-09 16:44:20 +00:00
|
|
|
raise newException(DaemonLocalError, "Wrong message type!")
|
|
|
|
|
2021-10-25 08:26:32 +00:00
|
|
|
initProtoBuffer(res)
|
|
|
|
|
|
|
|
proc getDhtMessageType(
|
|
|
|
pb: ProtoBuffer
|
2023-06-07 11:12:49 +00:00
|
|
|
): DHTResponseType {.inline, raises: [DaemonLocalError].} =
|
2018-11-19 02:52:11 +00:00
|
|
|
var dtype: uint
|
2021-10-25 08:26:32 +00:00
|
|
|
if pb.getRequiredField(1, dtype).isErr():
|
2018-11-19 02:52:11 +00:00
|
|
|
raise newException(DaemonLocalError, "Missing required DHT field `type`!")
|
2023-05-18 08:24:17 +00:00
|
|
|
if dtype == safeConvert[uint](DHTResponseType.VALUE):
|
2018-11-19 02:52:11 +00:00
|
|
|
result = DHTResponseType.VALUE
|
2023-05-18 08:24:17 +00:00
|
|
|
elif dtype == safeConvert[uint](DHTResponseType.END):
|
2018-11-19 02:52:11 +00:00
|
|
|
result = DHTResponseType.END
|
|
|
|
else:
|
|
|
|
raise newException(DaemonLocalError, "Wrong DHT answer type!")
|
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc dhtFindPeer*(
|
|
|
|
api: DaemonAPI, peer: PeerId, timeout = 0
|
2018-11-19 02:52:11 +00:00
|
|
|
): Future[PeerInfo] {.async.} =
|
|
|
|
## Find peer with id ``peer`` and return peer information ``PeerInfo``.
|
|
|
|
##
|
|
|
|
## You can specify timeout for DHT request with ``timeout`` value. ``0`` value
|
|
|
|
## means no timeout.
|
2018-12-10 20:55:06 +00:00
|
|
|
var transp = await api.newConnection()
|
2018-11-19 02:52:11 +00:00
|
|
|
try:
|
|
|
|
var pb = await transp.transactMessage(requestDHTFindPeer(peer, timeout))
|
|
|
|
withMessage(pb):
|
2021-10-25 08:26:32 +00:00
|
|
|
result = pb.enterDhtMessage(DHTResponseType.VALUE).dhtGetSinglePeerInfo()
|
2018-11-19 02:52:11 +00:00
|
|
|
finally:
|
2018-12-10 20:55:06 +00:00
|
|
|
await api.closeConnection(transp)
|
2018-11-19 02:52:11 +00:00
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc dhtGetPublicKey*(
|
|
|
|
api: DaemonAPI, peer: PeerId, timeout = 0
|
2019-03-06 11:23:12 +00:00
|
|
|
): Future[PublicKey] {.async.} =
|
2018-11-19 02:52:11 +00:00
|
|
|
## Get peer's public key from peer with id ``peer``.
|
|
|
|
##
|
|
|
|
## You can specify timeout for DHT request with ``timeout`` value. ``0`` value
|
|
|
|
## means no timeout.
|
2018-12-10 20:55:06 +00:00
|
|
|
var transp = await api.newConnection()
|
2018-11-19 02:52:11 +00:00
|
|
|
try:
|
|
|
|
var pb = await transp.transactMessage(requestDHTGetPublicKey(peer, timeout))
|
|
|
|
withMessage(pb):
|
2021-10-25 08:26:32 +00:00
|
|
|
result = pb.enterDhtMessage(DHTResponseType.VALUE).dhtGetSinglePublicKey()
|
2018-11-19 02:52:11 +00:00
|
|
|
finally:
|
2018-12-10 20:55:06 +00:00
|
|
|
await api.closeConnection(transp)
|
2018-11-19 02:52:11 +00:00
|
|
|
|
|
|
|
proc dhtGetValue*(
|
|
|
|
api: DaemonAPI, key: string, timeout = 0
|
|
|
|
): Future[seq[byte]] {.async.} =
|
|
|
|
## Get value associated with ``key``.
|
|
|
|
##
|
|
|
|
## You can specify timeout for DHT request with ``timeout`` value. ``0`` value
|
|
|
|
## means no timeout.
|
2018-12-10 20:55:06 +00:00
|
|
|
var transp = await api.newConnection()
|
2018-11-19 02:52:11 +00:00
|
|
|
try:
|
|
|
|
var pb = await transp.transactMessage(requestDHTGetValue(key, timeout))
|
|
|
|
withMessage(pb):
|
2021-10-25 08:26:32 +00:00
|
|
|
result = pb.enterDhtMessage(DHTResponseType.VALUE).dhtGetSingleValue()
|
2018-11-19 02:52:11 +00:00
|
|
|
finally:
|
2018-12-10 20:55:06 +00:00
|
|
|
await api.closeConnection(transp)
|
2018-11-19 02:52:11 +00:00
|
|
|
|
|
|
|
proc dhtPutValue*(
|
|
|
|
api: DaemonAPI, key: string, value: seq[byte], timeout = 0
|
|
|
|
) {.async.} =
|
|
|
|
## Associate ``value`` with ``key``.
|
|
|
|
##
|
|
|
|
## You can specify timeout for DHT request with ``timeout`` value. ``0`` value
|
|
|
|
## means no timeout.
|
2018-12-10 20:55:06 +00:00
|
|
|
var transp = await api.newConnection()
|
2018-11-19 02:52:11 +00:00
|
|
|
try:
|
|
|
|
var pb = await transp.transactMessage(requestDHTPutValue(key, value, timeout))
|
|
|
|
withMessage(pb):
|
|
|
|
discard
|
|
|
|
finally:
|
2018-12-10 20:55:06 +00:00
|
|
|
await api.closeConnection(transp)
|
2018-11-19 02:52:11 +00:00
|
|
|
|
2018-12-16 13:51:12 +00:00
|
|
|
proc dhtProvide*(api: DaemonAPI, cid: Cid, timeout = 0) {.async.} =
|
2018-11-19 02:52:11 +00:00
|
|
|
## Provide content with id ``cid``.
|
|
|
|
##
|
|
|
|
## You can specify timeout for DHT request with ``timeout`` value. ``0`` value
|
|
|
|
## means no timeout.
|
2018-12-10 20:55:06 +00:00
|
|
|
var transp = await api.newConnection()
|
2018-11-19 02:52:11 +00:00
|
|
|
try:
|
|
|
|
var pb = await transp.transactMessage(requestDHTProvide(cid, timeout))
|
|
|
|
withMessage(pb):
|
|
|
|
discard
|
|
|
|
finally:
|
2018-12-10 20:55:06 +00:00
|
|
|
await api.closeConnection(transp)
|
2018-11-19 02:52:11 +00:00
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc dhtFindPeersConnectedToPeer*(
|
|
|
|
api: DaemonAPI, peer: PeerId, timeout = 0
|
2018-11-19 02:52:11 +00:00
|
|
|
): Future[seq[PeerInfo]] {.async.} =
|
|
|
|
## Find peers which are connected to peer with id ``peer``.
|
|
|
|
##
|
|
|
|
## You can specify timeout for DHT request with ``timeout`` value. ``0`` value
|
|
|
|
## means no timeout.
|
2018-12-10 20:55:06 +00:00
|
|
|
var transp = await api.newConnection()
|
2018-11-19 02:52:11 +00:00
|
|
|
var list = newSeq[PeerInfo]()
|
|
|
|
try:
|
|
|
|
let spb = requestDHTFindPeersConnectedToPeer(peer, timeout)
|
|
|
|
var pb = await transp.transactMessage(spb)
|
|
|
|
withMessage(pb):
|
2021-10-25 08:26:32 +00:00
|
|
|
discard pb.enterDhtMessage(DHTResponseType.BEGIN)
|
2018-11-19 02:52:11 +00:00
|
|
|
while true:
|
|
|
|
var message = await transp.recvMessage()
|
2018-12-09 16:44:20 +00:00
|
|
|
if len(message) == 0:
|
|
|
|
break
|
2018-11-19 02:52:11 +00:00
|
|
|
var cpb = initProtoBuffer(message)
|
|
|
|
if cpb.getDhtMessageType() == DHTResponseType.END:
|
|
|
|
break
|
|
|
|
list.add(cpb.dhtGetSinglePeerInfo())
|
|
|
|
result = list
|
|
|
|
finally:
|
2018-12-10 20:55:06 +00:00
|
|
|
await api.closeConnection(transp)
|
2018-11-19 02:52:11 +00:00
|
|
|
|
|
|
|
proc dhtGetClosestPeers*(
|
|
|
|
api: DaemonAPI, key: string, timeout = 0
|
2021-12-16 10:05:20 +00:00
|
|
|
): Future[seq[PeerId]] {.async.} =
|
2018-11-19 02:52:11 +00:00
|
|
|
## Get closest peers for ``key``.
|
|
|
|
##
|
|
|
|
## You can specify timeout for DHT request with ``timeout`` value. ``0`` value
|
|
|
|
## means no timeout.
|
2018-12-10 20:55:06 +00:00
|
|
|
var transp = await api.newConnection()
|
2021-12-16 10:05:20 +00:00
|
|
|
var list = newSeq[PeerId]()
|
2018-11-19 02:52:11 +00:00
|
|
|
try:
|
|
|
|
let spb = requestDHTGetClosestPeers(key, timeout)
|
|
|
|
var pb = await transp.transactMessage(spb)
|
|
|
|
withMessage(pb):
|
2021-10-25 08:26:32 +00:00
|
|
|
discard pb.enterDhtMessage(DHTResponseType.BEGIN)
|
2018-11-19 02:52:11 +00:00
|
|
|
while true:
|
|
|
|
var message = await transp.recvMessage()
|
2018-12-09 16:44:20 +00:00
|
|
|
if len(message) == 0:
|
|
|
|
break
|
2018-11-19 02:52:11 +00:00
|
|
|
var cpb = initProtoBuffer(message)
|
|
|
|
if cpb.getDhtMessageType() == DHTResponseType.END:
|
|
|
|
break
|
2021-12-16 10:05:20 +00:00
|
|
|
list.add(cpb.dhtGetSinglePeerId())
|
2018-11-19 02:52:11 +00:00
|
|
|
result = list
|
|
|
|
finally:
|
2018-12-10 20:55:06 +00:00
|
|
|
await api.closeConnection(transp)
|
2018-11-19 02:52:11 +00:00
|
|
|
|
2018-12-16 13:51:12 +00:00
|
|
|
proc dhtFindProviders*(
|
|
|
|
api: DaemonAPI, cid: Cid, count: uint32, timeout = 0
|
2018-12-09 16:44:20 +00:00
|
|
|
): Future[seq[PeerInfo]] {.async.} =
|
2018-11-19 02:52:11 +00:00
|
|
|
## Get ``count`` providers for content with id ``cid``.
|
|
|
|
##
|
|
|
|
## You can specify timeout for DHT request with ``timeout`` value. ``0`` value
|
|
|
|
## means no timeout.
|
2018-12-10 20:55:06 +00:00
|
|
|
var transp = await api.newConnection()
|
2018-11-19 02:52:11 +00:00
|
|
|
var list = newSeq[PeerInfo]()
|
|
|
|
try:
|
|
|
|
let spb = requestDHTFindProviders(cid, count, timeout)
|
|
|
|
var pb = await transp.transactMessage(spb)
|
|
|
|
withMessage(pb):
|
2021-10-25 08:26:32 +00:00
|
|
|
discard pb.enterDhtMessage(DHTResponseType.BEGIN)
|
2018-11-19 02:52:11 +00:00
|
|
|
while true:
|
|
|
|
var message = await transp.recvMessage()
|
2018-12-09 16:44:20 +00:00
|
|
|
if len(message) == 0:
|
|
|
|
break
|
2018-11-19 02:52:11 +00:00
|
|
|
var cpb = initProtoBuffer(message)
|
|
|
|
if cpb.getDhtMessageType() == DHTResponseType.END:
|
|
|
|
break
|
|
|
|
list.add(cpb.dhtGetSinglePeerInfo())
|
|
|
|
result = list
|
|
|
|
finally:
|
2018-12-10 20:55:06 +00:00
|
|
|
await api.closeConnection(transp)
|
2018-11-19 02:52:11 +00:00
|
|
|
|
|
|
|
proc dhtSearchValue*(
|
|
|
|
api: DaemonAPI, key: string, timeout = 0
|
|
|
|
): Future[seq[seq[byte]]] {.async.} =
|
|
|
|
## Search for value with ``key``, return list of values found.
|
|
|
|
##
|
|
|
|
## You can specify timeout for DHT request with ``timeout`` value. ``0`` value
|
|
|
|
## means no timeout.
|
2018-12-10 20:55:06 +00:00
|
|
|
var transp = await api.newConnection()
|
2018-11-19 02:52:11 +00:00
|
|
|
var list = newSeq[seq[byte]]()
|
|
|
|
try:
|
|
|
|
var pb = await transp.transactMessage(requestDHTSearchValue(key, timeout))
|
|
|
|
withMessage(pb):
|
2021-10-25 08:26:32 +00:00
|
|
|
discard pb.enterDhtMessage(DHTResponseType.BEGIN)
|
2018-11-19 02:52:11 +00:00
|
|
|
while true:
|
|
|
|
var message = await transp.recvMessage()
|
2018-12-09 16:44:20 +00:00
|
|
|
if len(message) == 0:
|
|
|
|
break
|
2018-11-19 02:52:11 +00:00
|
|
|
var cpb = initProtoBuffer(message)
|
|
|
|
if cpb.getDhtMessageType() == DHTResponseType.END:
|
|
|
|
break
|
|
|
|
list.add(cpb.dhtGetSingleValue())
|
|
|
|
result = list
|
|
|
|
finally:
|
2018-12-10 20:55:06 +00:00
|
|
|
await api.closeConnection(transp)
|
2018-12-09 16:44:20 +00:00
|
|
|
|
|
|
|
proc pubsubGetTopics*(api: DaemonAPI): Future[seq[string]] {.async.} =
|
|
|
|
## Get list of topics this node is subscribed to.
|
2018-12-10 20:55:06 +00:00
|
|
|
var transp = await api.newConnection()
|
2018-12-09 16:44:20 +00:00
|
|
|
try:
|
|
|
|
var pb = await transp.transactMessage(requestPSGetTopics())
|
|
|
|
withMessage(pb):
|
2021-10-25 08:26:32 +00:00
|
|
|
let innerPb = pb.enterPsMessage()
|
2018-12-09 16:44:20 +00:00
|
|
|
var topics = newSeq[string]()
|
2021-10-25 08:26:32 +00:00
|
|
|
discard innerPb.getRepeatedField(1, topics)
|
2018-12-09 16:44:20 +00:00
|
|
|
result = topics
|
|
|
|
finally:
|
2018-12-10 20:55:06 +00:00
|
|
|
await api.closeConnection(transp)
|
2018-12-09 16:44:20 +00:00
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc pubsubListPeers*(api: DaemonAPI, topic: string): Future[seq[PeerId]] {.async.} =
|
2018-12-09 16:44:20 +00:00
|
|
|
## Get list of peers we are connected to and which also subscribed to topic
|
|
|
|
## ``topic``.
|
2018-12-10 20:55:06 +00:00
|
|
|
var transp = await api.newConnection()
|
2018-12-09 16:44:20 +00:00
|
|
|
try:
|
|
|
|
var pb = await transp.transactMessage(requestPSListPeers(topic))
|
|
|
|
withMessage(pb):
|
2021-12-16 10:05:20 +00:00
|
|
|
var peer: PeerId
|
2021-10-25 08:26:32 +00:00
|
|
|
let innerPb = pb.enterPsMessage()
|
|
|
|
var peers = newSeq[seq[byte]]()
|
|
|
|
discard innerPb.getRepeatedField(2, peers)
|
|
|
|
result = peers.mapIt(PeerId.init(it).get())
|
2018-12-09 16:44:20 +00:00
|
|
|
finally:
|
2018-12-10 20:55:06 +00:00
|
|
|
await api.closeConnection(transp)
|
2018-12-09 16:44:20 +00:00
|
|
|
|
|
|
|
proc pubsubPublish*(api: DaemonAPI, topic: string, value: seq[byte]) {.async.} =
|
|
|
|
## Get list of peer identifiers which are subscribed to topic ``topic``.
|
2018-12-10 20:55:06 +00:00
|
|
|
var transp = await api.newConnection()
|
2018-12-09 16:44:20 +00:00
|
|
|
try:
|
|
|
|
var pb = await transp.transactMessage(requestPSPublish(topic, value))
|
|
|
|
withMessage(pb):
|
|
|
|
discard
|
|
|
|
finally:
|
2018-12-10 20:55:06 +00:00
|
|
|
await api.closeConnection(transp)
|
2018-12-09 16:44:20 +00:00
|
|
|
|
2021-10-25 08:26:32 +00:00
|
|
|
proc getPubsubMessage*(pb: ProtoBuffer): PubSubMessage =
|
2019-03-04 18:22:38 +00:00
|
|
|
result.data = newSeq[byte]()
|
|
|
|
result.seqno = newSeq[byte]()
|
2021-10-25 08:26:32 +00:00
|
|
|
discard pb.getField(1, result.peer)
|
|
|
|
discard pb.getField(2, result.data)
|
|
|
|
discard pb.getField(3, result.seqno)
|
|
|
|
discard pb.getRepeatedField(4, result.topics)
|
|
|
|
discard pb.getField(5, result.signature)
|
|
|
|
discard pb.getField(6, result.key)
|
2018-12-09 16:44:20 +00:00
|
|
|
|
|
|
|
proc pubsubLoop(api: DaemonAPI, ticket: PubsubTicket) {.async.} =
|
|
|
|
while true:
|
|
|
|
var pbmessage = await ticket.transp.recvMessage()
|
|
|
|
if len(pbmessage) == 0:
|
|
|
|
break
|
|
|
|
var pb = initProtoBuffer(pbmessage)
|
|
|
|
var message = pb.getPubsubMessage()
|
|
|
|
## We can do here `await` too
|
|
|
|
let res = await ticket.handler(api, ticket, message)
|
|
|
|
if not res:
|
|
|
|
ticket.transp.close()
|
|
|
|
await ticket.transp.join()
|
|
|
|
break
|
|
|
|
|
|
|
|
proc pubsubSubscribe*(
|
|
|
|
api: DaemonAPI, topic: string, handler: P2PPubSubCallback
|
|
|
|
): Future[PubsubTicket] {.async.} =
|
|
|
|
## Subscribe to topic ``topic``.
|
2018-12-10 20:55:06 +00:00
|
|
|
var transp = await api.newConnection()
|
2018-12-09 16:44:20 +00:00
|
|
|
try:
|
|
|
|
var pb = await transp.transactMessage(requestPSSubscribe(topic))
|
|
|
|
pb.withMessage:
|
|
|
|
var ticket = new PubsubTicket
|
|
|
|
ticket.topic = topic
|
|
|
|
ticket.handler = handler
|
|
|
|
ticket.transp = transp
|
2021-06-02 13:39:10 +00:00
|
|
|
asyncSpawn pubsubLoop(api, ticket)
|
2018-12-09 16:44:20 +00:00
|
|
|
result = ticket
|
2022-01-05 15:27:33 +00:00
|
|
|
except CatchableError as exc:
|
2018-12-11 02:17:36 +00:00
|
|
|
await api.closeConnection(transp)
|
2019-11-29 19:44:37 +00:00
|
|
|
raise exc
|
2020-11-19 02:06:42 +00:00
|
|
|
|
|
|
|
proc shortLog*(pinfo: PeerInfo): string =
|
|
|
|
## Get string representation of ``PeerInfo`` object.
|
|
|
|
result = newStringOfCap(128)
|
2021-12-16 10:05:20 +00:00
|
|
|
result.add("{PeerId: '")
|
2020-11-19 02:06:42 +00:00
|
|
|
result.add($pinfo.peer.shortLog())
|
|
|
|
result.add("' Addresses: [")
|
|
|
|
let length = len(pinfo.addresses)
|
|
|
|
for i in 0 ..< length:
|
|
|
|
result.add("'")
|
|
|
|
result.add($pinfo.addresses[i])
|
|
|
|
result.add("'")
|
|
|
|
if i < length - 1:
|
|
|
|
result.add(", ")
|
|
|
|
result.add("]}")
|
|
|
|
if len(pinfo.addresses) > 0:
|
|
|
|
result = result
|