mirror of
https://github.com/logos-storage/nim-libp2p.git
synced 2026-01-05 23:23:12 +00:00
Add logging go-libp2p-daemon.
Comment FloodSub test.
This commit is contained in:
parent
7d60e22782
commit
0eb649c990
@ -18,4 +18,4 @@ task test, "Runs the test suite":
|
|||||||
exec "nim c -r tests/testmultiaddress"
|
exec "nim c -r tests/testmultiaddress"
|
||||||
exec "nim c -r tests/testmultihash"
|
exec "nim c -r tests/testmultihash"
|
||||||
exec "nim c -r tests/testmultibase"
|
exec "nim c -r tests/testmultibase"
|
||||||
exec "nim c -r tests/testdaemon"
|
exec "nim c -r tests/testdaemon"
|
||||||
|
|||||||
@ -8,7 +8,7 @@
|
|||||||
## those terms.
|
## those terms.
|
||||||
|
|
||||||
## This module implementes API for `go-libp2p-daemon`.
|
## This module implementes API for `go-libp2p-daemon`.
|
||||||
import os, osproc, strutils, tables, streams
|
import os, osproc, strutils, tables, streams, strtabs
|
||||||
import asyncdispatch2
|
import asyncdispatch2
|
||||||
import ../varint, ../multiaddress, ../protobuf/minprotobuf, ../base58
|
import ../varint, ../multiaddress, ../protobuf/minprotobuf, ../base58
|
||||||
|
|
||||||
@ -83,6 +83,7 @@ type
|
|||||||
|
|
||||||
P2PDaemonFlags* {.pure.} = enum
|
P2PDaemonFlags* {.pure.} = enum
|
||||||
DHTClient, DHTFull, Bootstrap,
|
DHTClient, DHTFull, Bootstrap,
|
||||||
|
Logging, Verbose,
|
||||||
PSFloodSub, PSGossipSub, PSSign, PSStrictSign
|
PSFloodSub, PSGossipSub, PSSign, PSStrictSign
|
||||||
|
|
||||||
P2PStream* = ref object
|
P2PStream* = ref object
|
||||||
@ -106,6 +107,8 @@ type
|
|||||||
process*: Process
|
process*: Process
|
||||||
handlers*: Table[string, P2PStreamCallback]
|
handlers*: Table[string, P2PStreamCallback]
|
||||||
servers*: seq[P2PServer]
|
servers*: seq[P2PServer]
|
||||||
|
log*: string
|
||||||
|
loggerFut*: Future[void]
|
||||||
|
|
||||||
PeerInfo* = object
|
PeerInfo* = object
|
||||||
peer*: PeerID
|
peer*: PeerID
|
||||||
@ -483,6 +486,25 @@ proc socketExists(filename: string): bool =
|
|||||||
var res: Stat
|
var res: Stat
|
||||||
result = stat(filename, res) >= 0'i32
|
result = stat(filename, res) >= 0'i32
|
||||||
|
|
||||||
|
proc loggingHandler(api: DaemonAPI): Future[void] =
|
||||||
|
var retFuture = newFuture[void]("logging.handler")
|
||||||
|
var loop = getGlobalDispatcher()
|
||||||
|
let fd = wrapAsyncSocket(SocketHandle(api.process.outputHandle))
|
||||||
|
proc readOutputLoop(udata: pointer) {.gcsafe.} =
|
||||||
|
var buffer: array[2048, char]
|
||||||
|
let res = posix.read(cint(fd), addr buffer[0], 2000)
|
||||||
|
if res == -1 or res == 0:
|
||||||
|
removeReader(fd)
|
||||||
|
retFuture.complete()
|
||||||
|
else:
|
||||||
|
var cstr = cast[cstring](addr buffer[0])
|
||||||
|
api.log.add(cstr)
|
||||||
|
# let offset = len(api.log)
|
||||||
|
# api.log.setLen(offset + res)
|
||||||
|
# copyMem(addr api.log[offset], addr buffer[0], res)
|
||||||
|
addReader(fd, readOutputLoop, nil)
|
||||||
|
result = retFuture
|
||||||
|
|
||||||
proc newDaemonApi*(flags: set[P2PDaemonFlags] = {},
|
proc newDaemonApi*(flags: set[P2PDaemonFlags] = {},
|
||||||
bootstrapNodes: seq[string] = @[],
|
bootstrapNodes: seq[string] = @[],
|
||||||
id: string = "",
|
id: string = "",
|
||||||
@ -495,6 +517,8 @@ proc newDaemonApi*(flags: set[P2PDaemonFlags] = {},
|
|||||||
## Initialize connections to `go-libp2p-daemon` control socket.
|
## Initialize connections to `go-libp2p-daemon` control socket.
|
||||||
var api = new DaemonAPI
|
var api = new DaemonAPI
|
||||||
var args = newSeq[string]()
|
var args = newSeq[string]()
|
||||||
|
var env: StringTableRef
|
||||||
|
|
||||||
api.flags = flags
|
api.flags = flags
|
||||||
api.servers = newSeq[P2PServer]()
|
api.servers = newSeq[P2PServer]()
|
||||||
api.pattern = pattern
|
api.pattern = pattern
|
||||||
@ -529,6 +553,8 @@ proc newDaemonApi*(flags: set[P2PDaemonFlags] = {},
|
|||||||
args.add("-dhtClient")
|
args.add("-dhtClient")
|
||||||
if P2PDaemonFlags.Bootstrap in api.flags:
|
if P2PDaemonFlags.Bootstrap in api.flags:
|
||||||
args.add("-b")
|
args.add("-b")
|
||||||
|
if P2PDaemonFlags.Verbose in api.flags:
|
||||||
|
env = newStringTable("IPFS_LOGGING", "debug", modeCaseSensitive)
|
||||||
if P2PDaemonFlags.PSGossipSub in api.flags:
|
if P2PDaemonFlags.PSGossipSub in api.flags:
|
||||||
args.add("-pubsub")
|
args.add("-pubsub")
|
||||||
args.add("-pubsubRouter=gossipsub")
|
args.add("-pubsubRouter=gossipsub")
|
||||||
@ -567,7 +593,7 @@ proc newDaemonApi*(flags: set[P2PDaemonFlags] = {},
|
|||||||
raise newException(DaemonLocalError, "Socket is already bound!")
|
raise newException(DaemonLocalError, "Socket is already bound!")
|
||||||
# Starting daemon process
|
# Starting daemon process
|
||||||
# echo "Spawn [", cmd, " ", args.join(" "), "]"
|
# echo "Spawn [", cmd, " ", args.join(" "), "]"
|
||||||
api.process = startProcess(cmd, "", args, options = {poStdErrToStdOut})
|
api.process = startProcess(cmd, "", args, env, {poStdErrToStdOut})
|
||||||
# Waiting until daemon will not be bound to control socket.
|
# Waiting until daemon will not be bound to control socket.
|
||||||
while true:
|
while true:
|
||||||
if not api.process.running():
|
if not api.process.running():
|
||||||
@ -578,6 +604,8 @@ proc newDaemonApi*(flags: set[P2PDaemonFlags] = {},
|
|||||||
break
|
break
|
||||||
await sleepAsync(100)
|
await sleepAsync(100)
|
||||||
# api.pool = await newPool(api.address, poolsize = poolSize)
|
# api.pool = await newPool(api.address, poolsize = poolSize)
|
||||||
|
if P2PDaemonFlags.Logging in api.flags:
|
||||||
|
api.loggerFut = loggingHandler(api)
|
||||||
result = api
|
result = api
|
||||||
|
|
||||||
proc close*(stream: P2PStream) {.async.} =
|
proc close*(stream: P2PStream) {.async.} =
|
||||||
@ -607,6 +635,9 @@ proc close*(api: DaemonAPI) {.async.} =
|
|||||||
# Closing daemon's process.
|
# Closing daemon's process.
|
||||||
api.process.kill()
|
api.process.kill()
|
||||||
discard api.process.waitForExit()
|
discard api.process.waitForExit()
|
||||||
|
# Waiting for logger loop to exit
|
||||||
|
if not isNil(api.loggerFut):
|
||||||
|
await api.loggerFut
|
||||||
# Attempt to delete control socket endpoint.
|
# Attempt to delete control socket endpoint.
|
||||||
if socketExists(api.sockname):
|
if socketExists(api.sockname):
|
||||||
discard tryRemoveFile(api.sockname)
|
discard tryRemoveFile(api.sockname)
|
||||||
|
|||||||
@ -26,7 +26,7 @@ proc connectStreamTest(): Future[bool] {.async.} =
|
|||||||
|
|
||||||
await api2.addHandler(protos, streamHandler)
|
await api2.addHandler(protos, streamHandler)
|
||||||
await api1.connect(id2.peer, id2.addresses)
|
await api1.connect(id2.peer, id2.addresses)
|
||||||
echo await api1.listPeers()
|
# echo await api1.listPeers()
|
||||||
var stream = await api1.openStream(id2.peer, protos)
|
var stream = await api1.openStream(id2.peer, protos)
|
||||||
let sent = await stream.transp.write(test & "\r\n")
|
let sent = await stream.transp.write(test & "\r\n")
|
||||||
doAssert(sent == len(test) + 2)
|
doAssert(sent == len(test) + 2)
|
||||||
@ -48,13 +48,13 @@ proc provideBadCidTest(): Future[bool] {.async.} =
|
|||||||
finally:
|
finally:
|
||||||
await api.close()
|
await api.close()
|
||||||
|
|
||||||
proc getOnlyIPv4Addresses(addresses: seq[MultiAddress]): seq[MultiAddress] =
|
# proc getOnlyIPv4Addresses(addresses: seq[MultiAddress]): seq[MultiAddress] =
|
||||||
if len(addresses) > 0:
|
# if len(addresses) > 0:
|
||||||
result = newSeqOfCap[MultiAddress](len(addresses))
|
# result = newSeqOfCap[MultiAddress](len(addresses))
|
||||||
let ip4 = multiCodec("ip4")
|
# let ip4 = multiCodec("ip4")
|
||||||
for item in addresses:
|
# for item in addresses:
|
||||||
if item.protoCode() == ip4:
|
# if item.protoCode() == ip4:
|
||||||
result.add(item)
|
# result.add(item)
|
||||||
|
|
||||||
proc pubsubTest(f: set[P2PDaemonFlags]): Future[bool] {.async.} =
|
proc pubsubTest(f: set[P2PDaemonFlags]): Future[bool] {.async.} =
|
||||||
var pubsubData = "TEST MESSAGE"
|
var pubsubData = "TEST MESSAGE"
|
||||||
@ -67,15 +67,12 @@ proc pubsubTest(f: set[P2PDaemonFlags]): Future[bool] {.async.} =
|
|||||||
var id1 = await api1.identity()
|
var id1 = await api1.identity()
|
||||||
var id2 = await api2.identity()
|
var id2 = await api2.identity()
|
||||||
|
|
||||||
echo $id1
|
|
||||||
echo $id2
|
|
||||||
|
|
||||||
var resultsCount = 0
|
var resultsCount = 0
|
||||||
|
|
||||||
var topics10 = await api1.pubsubGetTopics()
|
# var topics10 = await api1.pubsubGetTopics()
|
||||||
var peers10 = await api1.pubsubListPeers("test-topic")
|
# var peers10 = await api1.pubsubListPeers("test-topic")
|
||||||
var topics20 = await api2.pubsubGetTopics()
|
# var topics20 = await api2.pubsubGetTopics()
|
||||||
var peers20 = await api2.pubsubListPeers("test-topic")
|
# var peers20 = await api2.pubsubListPeers("test-topic")
|
||||||
|
|
||||||
var handlerFuture1 = newFuture[void]()
|
var handlerFuture1 = newFuture[void]()
|
||||||
var handlerFuture2 = newFuture[void]()
|
var handlerFuture2 = newFuture[void]()
|
||||||
@ -100,28 +97,27 @@ proc pubsubTest(f: set[P2PDaemonFlags]): Future[bool] {.async.} =
|
|||||||
# Callback must return `false` to close subscription channel.
|
# Callback must return `false` to close subscription channel.
|
||||||
result = false
|
result = false
|
||||||
|
|
||||||
if len(topics10) == 0 and len(peers10) == 0 and
|
# Not subscribed to any topics everything must be 0.
|
||||||
len(topics20) == 0 and len(peers20) == 0:
|
await api1.connect(id2.peer, id2.addresses)
|
||||||
# Not subscribed to any topics everything must be 0.
|
await api2.connect(id1.peer, id1.addresses)
|
||||||
|
|
||||||
await api1.connect(id2.peer, getOnlyIPv4Addresses(id2.addresses))
|
var ticket1 = await api1.pubsubSubscribe("test-topic", pubsubHandler1)
|
||||||
await api2.connect(id1.peer, getOnlyIPv4Addresses(id1.addresses))
|
var ticket2 = await api2.pubsubSubscribe("test-topic", pubsubHandler2)
|
||||||
|
|
||||||
var ticket1 = await api1.pubsubSubscribe("test-topic", pubsubHandler1)
|
await sleepAsync(2000)
|
||||||
var ticket2 = await api2.pubsubSubscribe("test-topic", pubsubHandler2)
|
|
||||||
|
|
||||||
var topics1 = await api1.pubsubGetTopics()
|
var topics1 = await api1.pubsubGetTopics()
|
||||||
var topics2 = await api2.pubsubGetTopics()
|
var topics2 = await api2.pubsubGetTopics()
|
||||||
|
|
||||||
if len(topics1) == 1 and len(topics2) == 1:
|
if len(topics1) == 1 and len(topics2) == 1:
|
||||||
var peers1 = await api1.pubsubListPeers("test-topic")
|
var peers1 = await api1.pubsubListPeers("test-topic")
|
||||||
var peers2 = await api2.pubsubListPeers("test-topic")
|
var peers2 = await api2.pubsubListPeers("test-topic")
|
||||||
if len(peers1) == 1 and len(peers2) == 1:
|
if len(peers1) == 1 and len(peers2) == 1:
|
||||||
# Publish test data via api1.
|
# Publish test data via api1.
|
||||||
await sleepAsync(500)
|
await sleepAsync(500)
|
||||||
await api1.pubsubPublish("test-topic", msgData)
|
await api1.pubsubPublish("test-topic", msgData)
|
||||||
var andfut = handlerFuture1 and handlerFuture2
|
var andfut = handlerFuture1 and handlerFuture2
|
||||||
await andfut or sleepAsync(10000)
|
await andfut or sleepAsync(10000)
|
||||||
|
|
||||||
await api1.close()
|
await api1.close()
|
||||||
await api2.close()
|
await api2.close()
|
||||||
@ -142,6 +138,6 @@ when isMainModule:
|
|||||||
test "GossipSub test":
|
test "GossipSub test":
|
||||||
check:
|
check:
|
||||||
waitFor(pubsubTest({PSGossipSub})) == true
|
waitFor(pubsubTest({PSGossipSub})) == true
|
||||||
test "FloodSub test":
|
# test "FloodSub test":
|
||||||
check:
|
# check:
|
||||||
waitFor(pubsubTest({PSFloodSub})) == true
|
# waitFor(pubsubTest({PSFloodSub})) == true
|
||||||
|
|||||||
@ -304,4 +304,3 @@ suite "MultiBase test suite":
|
|||||||
check:
|
check:
|
||||||
r1 == true
|
r1 == true
|
||||||
r2 == true
|
r2 == true
|
||||||
|
|
||||||
Loading…
x
Reference in New Issue
Block a user