2019-03-04 18:22:38 +00:00
|
|
|
import chronos, nimcrypto, strutils
|
2020-03-16 07:43:08 +00:00
|
|
|
import ../../libp2p/daemon/daemonapi
|
|
|
|
import ../hexdump
|
2018-12-18 14:31:58 +00:00
|
|
|
|
|
|
|
const PubSubTopic = "test-net"
|
|
|
|
|
|
|
|
proc dumpSubscribedPeers(api: DaemonAPI) {.async.} =
|
|
|
|
var peers = await api.pubsubListPeers(PubSubTopic)
|
|
|
|
echo "= List of connected and subscribed peers:"
|
|
|
|
for item in peers:
|
2019-03-04 18:28:59 +00:00
|
|
|
echo item.pretty()
|
2018-12-18 14:31:58 +00:00
|
|
|
|
2019-03-04 23:57:18 +00:00
|
|
|
proc dumpAllPeers(api: DaemonAPI) {.async.} =
|
|
|
|
var peers = await api.listPeers()
|
|
|
|
echo "Current connected peers count = ", len(peers)
|
|
|
|
for item in peers:
|
|
|
|
echo item.peer.pretty()
|
|
|
|
|
|
|
|
proc monitor(api: DaemonAPI) {.async.} =
|
|
|
|
while true:
|
|
|
|
echo "Dumping all peers"
|
|
|
|
await dumpAllPeers(api)
|
|
|
|
await sleepAsync(5000)
|
|
|
|
|
2018-12-18 14:31:58 +00:00
|
|
|
proc main() {.async.} =
|
|
|
|
echo "= Starting P2P bootnode"
|
|
|
|
var api = await newDaemonApi({DHTFull, PSGossipSub})
|
|
|
|
var id = await api.identity()
|
2019-03-04 18:28:59 +00:00
|
|
|
echo "= P2P bootnode ", id.peer.pretty(), " started."
|
2018-12-18 14:31:58 +00:00
|
|
|
let mcip4 = multiCodec("ip4")
|
|
|
|
let mcip6 = multiCodec("ip6")
|
|
|
|
echo "= You can use one of this addresses to bootstrap your nodes:"
|
|
|
|
for item in id.addresses:
|
|
|
|
if item.protoCode() == mcip4 or item.protoCode() == mcip6:
|
2019-03-04 18:28:59 +00:00
|
|
|
echo $item & "/ipfs/" & id.peer.pretty()
|
2018-12-18 14:31:58 +00:00
|
|
|
|
2021-06-14 23:21:44 +00:00
|
|
|
asyncSpawn monitor(api)
|
2019-03-04 23:57:18 +00:00
|
|
|
|
2018-12-18 14:31:58 +00:00
|
|
|
proc pubsubLogger(
|
|
|
|
api: DaemonAPI, ticket: PubsubTicket, message: PubSubMessage
|
|
|
|
): Future[bool] {.async.} =
|
|
|
|
let msglen = len(message.data)
|
2019-03-04 18:51:10 +00:00
|
|
|
echo "= Recieved pubsub message with length ",
|
2019-03-04 18:28:59 +00:00
|
|
|
msglen, " bytes from peer ", message.peer.pretty()
|
2018-12-18 14:31:58 +00:00
|
|
|
echo dumpHex(message.data)
|
|
|
|
await api.dumpSubscribedPeers()
|
|
|
|
result = true
|
|
|
|
|
|
|
|
var ticket = await api.pubsubSubscribe(PubSubTopic, pubsubLogger)
|
|
|
|
|
|
|
|
when isMainModule:
|
|
|
|
waitFor(main())
|
|
|
|
while true:
|
|
|
|
poll()
|