2019-12-06 02:16:18 +00:00
|
|
|
import options, tables
|
|
|
|
import unittest
|
2020-05-23 17:14:22 +00:00
|
|
|
import chronos, chronicles, stew/byteutils
|
|
|
|
import helpers
|
2019-12-06 02:16:18 +00:00
|
|
|
import ../libp2p/[daemon/daemonapi,
|
|
|
|
protobuf/minprotobuf,
|
|
|
|
vbuffer,
|
|
|
|
multiaddress,
|
|
|
|
multicodec,
|
|
|
|
cid,
|
|
|
|
varint,
|
|
|
|
multihash,
|
2020-05-23 17:14:22 +00:00
|
|
|
standard_setup,
|
2020-07-01 06:25:09 +00:00
|
|
|
peerid,
|
2019-12-06 02:16:18 +00:00
|
|
|
peerinfo,
|
|
|
|
switch,
|
2020-06-19 17:29:43 +00:00
|
|
|
stream/connection,
|
2019-12-06 02:16:18 +00:00
|
|
|
muxers/muxer,
|
|
|
|
crypto/crypto,
|
|
|
|
muxers/mplex/mplex,
|
|
|
|
muxers/muxer,
|
|
|
|
protocols/protocol,
|
|
|
|
protocols/identify,
|
|
|
|
transports/transport,
|
|
|
|
transports/tcptransport,
|
|
|
|
protocols/secure/secure,
|
|
|
|
protocols/pubsub/pubsub,
|
2020-09-21 09:16:29 +00:00
|
|
|
protocols/pubsub/floodsub,
|
|
|
|
protocols/pubsub/gossipsub]
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
# TODO: Unify both PeerInfo structs
|
|
|
|
NativePeerInfo = peerinfo.PeerInfo
|
|
|
|
DaemonPeerInfo = daemonapi.PeerInfo
|
|
|
|
|
|
|
|
proc writeLp*(s: StreamTransport, msg: string | seq[byte]): Future[int] {.gcsafe.} =
|
|
|
|
## write lenght prefixed
|
|
|
|
var buf = initVBuffer()
|
|
|
|
buf.writeSeq(msg)
|
|
|
|
buf.finish()
|
|
|
|
result = s.write(buf.buffer)
|
|
|
|
|
|
|
|
proc readLp*(s: StreamTransport): Future[seq[byte]] {.async, gcsafe.} =
|
2020-05-29 16:25:25 +00:00
|
|
|
## read length prefixed msg
|
2019-12-06 02:16:18 +00:00
|
|
|
var
|
|
|
|
size: uint
|
|
|
|
length: int
|
2020-05-31 14:22:49 +00:00
|
|
|
res: VarintResult[void]
|
2019-12-06 02:16:18 +00:00
|
|
|
result = newSeq[byte](10)
|
2020-05-07 20:37:46 +00:00
|
|
|
|
|
|
|
for i in 0..<len(result):
|
|
|
|
await s.readExactly(addr result[i], 1)
|
|
|
|
res = LP.getUVarint(result.toOpenArray(0, i), length, size)
|
2020-05-31 14:22:49 +00:00
|
|
|
if res.isOk():
|
2020-05-07 20:37:46 +00:00
|
|
|
break
|
2020-05-31 14:22:49 +00:00
|
|
|
res.expect("Valid varint")
|
2020-05-07 20:37:46 +00:00
|
|
|
result.setLen(size)
|
|
|
|
if size > 0.uint:
|
|
|
|
await s.readExactly(addr result[0], int(size))
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-02-21 01:14:39 +00:00
|
|
|
proc testPubSubDaemonPublish(gossip: bool = false,
|
|
|
|
count: int = 1): Future[bool] {.async.} =
|
2019-12-06 02:16:18 +00:00
|
|
|
var pubsubData = "TEST MESSAGE"
|
|
|
|
var testTopic = "test-topic"
|
2020-06-03 02:21:11 +00:00
|
|
|
var msgData = pubsubData.toBytes()
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
var flags = {PSFloodSub}
|
|
|
|
if gossip:
|
|
|
|
flags = {PSGossipSub}
|
|
|
|
|
|
|
|
let daemonNode = await newDaemonApi(flags)
|
|
|
|
let daemonPeer = await daemonNode.identity()
|
2020-07-17 18:44:41 +00:00
|
|
|
let nativeNode = newStandardSwitch(
|
|
|
|
secureManagers = [SecureProtocol.Noise],
|
|
|
|
outTimeout = 5.minutes)
|
|
|
|
|
2020-08-12 00:05:49 +00:00
|
|
|
let pubsub = if gossip:
|
|
|
|
GossipSub.init(
|
|
|
|
switch = nativeNode).PubSub
|
|
|
|
else:
|
|
|
|
FloodSub.init(
|
|
|
|
switch = nativeNode).PubSub
|
|
|
|
|
|
|
|
nativeNode.mount(pubsub)
|
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
let awaiters = nativeNode.start()
|
2020-08-12 00:05:49 +00:00
|
|
|
await pubsub.start()
|
2019-12-06 02:16:18 +00:00
|
|
|
let nativePeer = nativeNode.peerInfo
|
|
|
|
|
2020-05-15 03:56:56 +00:00
|
|
|
var finished = false
|
2019-12-23 18:45:12 +00:00
|
|
|
var times = 0
|
2019-12-06 02:16:18 +00:00
|
|
|
proc nativeHandler(topic: string, data: seq[byte]) {.async.} =
|
2020-06-03 02:21:11 +00:00
|
|
|
let smsg = string.fromBytes(data)
|
2019-12-06 02:16:18 +00:00
|
|
|
check smsg == pubsubData
|
2019-12-23 18:45:12 +00:00
|
|
|
times.inc()
|
2020-05-15 03:56:56 +00:00
|
|
|
if times >= count and not finished:
|
|
|
|
finished = true
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-07-27 19:33:51 +00:00
|
|
|
let peer = NativePeerInfo.init(
|
|
|
|
daemonPeer.peer,
|
|
|
|
daemonPeer.addresses)
|
2020-08-12 00:05:49 +00:00
|
|
|
await nativeNode.connect(peer.peerId, peer.addrs)
|
2020-07-27 19:33:51 +00:00
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
await sleepAsync(1.seconds)
|
2019-12-07 16:36:39 +00:00
|
|
|
await daemonNode.connect(nativePeer.peerId, nativePeer.addrs)
|
2019-12-10 20:50:35 +00:00
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
proc pubsubHandler(api: DaemonAPI,
|
|
|
|
ticket: PubsubTicket,
|
2019-12-10 20:50:35 +00:00
|
|
|
message: PubSubMessage): Future[bool] {.async.} =
|
2019-12-06 02:16:18 +00:00
|
|
|
result = true # don't cancel subscription
|
|
|
|
|
|
|
|
asyncDiscard daemonNode.pubsubSubscribe(testTopic, pubsubHandler)
|
2020-08-12 00:05:49 +00:00
|
|
|
await pubsub.subscribe(testTopic, nativeHandler)
|
2020-05-23 17:14:22 +00:00
|
|
|
await sleepAsync(5.seconds)
|
2020-05-15 03:56:56 +00:00
|
|
|
|
|
|
|
proc publisher() {.async.} =
|
|
|
|
while not finished:
|
|
|
|
await daemonNode.pubsubPublish(testTopic, msgData)
|
2020-05-23 17:14:22 +00:00
|
|
|
await sleepAsync(500.millis)
|
2020-05-15 03:56:56 +00:00
|
|
|
|
2020-05-18 13:49:49 +00:00
|
|
|
await wait(publisher(), 5.minutes) # should be plenty of time
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-05-15 03:56:56 +00:00
|
|
|
result = true
|
2019-12-06 02:16:18 +00:00
|
|
|
await nativeNode.stop()
|
2020-08-12 00:05:49 +00:00
|
|
|
await pubsub.stop()
|
2019-12-06 02:16:18 +00:00
|
|
|
await allFutures(awaiters)
|
|
|
|
await daemonNode.close()
|
|
|
|
|
2020-02-21 01:14:39 +00:00
|
|
|
proc testPubSubNodePublish(gossip: bool = false,
|
|
|
|
count: int = 1): Future[bool] {.async.} =
|
2019-12-06 02:16:18 +00:00
|
|
|
var pubsubData = "TEST MESSAGE"
|
|
|
|
var testTopic = "test-topic"
|
2020-06-03 02:21:11 +00:00
|
|
|
var msgData = pubsubData.toBytes()
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
var flags = {PSFloodSub}
|
|
|
|
if gossip:
|
|
|
|
flags = {PSGossipSub}
|
|
|
|
|
|
|
|
let daemonNode = await newDaemonApi(flags)
|
|
|
|
let daemonPeer = await daemonNode.identity()
|
2020-07-17 18:44:41 +00:00
|
|
|
let nativeNode = newStandardSwitch(
|
2020-09-21 09:16:29 +00:00
|
|
|
secureManagers = [SecureProtocol.Noise],
|
2020-07-17 18:44:41 +00:00
|
|
|
outTimeout = 5.minutes)
|
|
|
|
|
2020-08-12 00:05:49 +00:00
|
|
|
let pubsub = if gossip:
|
|
|
|
GossipSub.init(
|
|
|
|
switch = nativeNode).PubSub
|
|
|
|
else:
|
|
|
|
FloodSub.init(
|
|
|
|
switch = nativeNode).PubSub
|
|
|
|
|
|
|
|
nativeNode.mount(pubsub)
|
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
let awaiters = nativeNode.start()
|
2020-08-12 00:05:49 +00:00
|
|
|
await pubsub.start()
|
2019-12-06 02:16:18 +00:00
|
|
|
let nativePeer = nativeNode.peerInfo
|
|
|
|
|
2020-07-27 19:33:51 +00:00
|
|
|
let peer = NativePeerInfo.init(
|
|
|
|
daemonPeer.peer,
|
|
|
|
daemonPeer.addresses)
|
|
|
|
await nativeNode.connect(peer)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
await sleepAsync(1.seconds)
|
2019-12-07 16:36:39 +00:00
|
|
|
await daemonNode.connect(nativePeer.peerId, nativePeer.addrs)
|
2019-12-10 20:50:35 +00:00
|
|
|
|
2019-12-23 18:45:12 +00:00
|
|
|
var times = 0
|
2020-05-15 03:56:56 +00:00
|
|
|
var finished = false
|
2019-12-06 02:16:18 +00:00
|
|
|
proc pubsubHandler(api: DaemonAPI,
|
|
|
|
ticket: PubsubTicket,
|
2019-12-10 20:50:35 +00:00
|
|
|
message: PubSubMessage): Future[bool] {.async.} =
|
2020-06-03 02:21:11 +00:00
|
|
|
let smsg = string.fromBytes(message.data)
|
2019-12-06 02:16:18 +00:00
|
|
|
check smsg == pubsubData
|
2019-12-23 18:45:12 +00:00
|
|
|
times.inc()
|
2020-05-15 03:56:56 +00:00
|
|
|
if times >= count and not finished:
|
|
|
|
finished = true
|
2019-12-06 02:16:18 +00:00
|
|
|
result = true # don't cancel subscription
|
|
|
|
|
2019-12-23 18:45:12 +00:00
|
|
|
discard await daemonNode.pubsubSubscribe(testTopic, pubsubHandler)
|
|
|
|
proc nativeHandler(topic: string, data: seq[byte]) {.async.} = discard
|
2020-08-12 00:05:49 +00:00
|
|
|
await pubsub.subscribe(testTopic, nativeHandler)
|
2020-05-23 17:14:22 +00:00
|
|
|
await sleepAsync(5.seconds)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-05-15 03:56:56 +00:00
|
|
|
proc publisher() {.async.} =
|
|
|
|
while not finished:
|
2020-08-12 00:05:49 +00:00
|
|
|
discard await pubsub.publish(testTopic, msgData)
|
2020-05-23 17:14:22 +00:00
|
|
|
await sleepAsync(500.millis)
|
2020-05-15 03:56:56 +00:00
|
|
|
|
2020-05-18 13:49:49 +00:00
|
|
|
await wait(publisher(), 5.minutes) # should be plenty of time
|
2020-05-15 03:56:56 +00:00
|
|
|
|
|
|
|
result = finished
|
2019-12-06 02:16:18 +00:00
|
|
|
await nativeNode.stop()
|
2020-08-12 00:05:49 +00:00
|
|
|
await pubsub.stop()
|
2019-12-06 02:16:18 +00:00
|
|
|
await allFutures(awaiters)
|
|
|
|
await daemonNode.close()
|
|
|
|
|
|
|
|
suite "Interop":
|
2020-05-23 17:15:47 +00:00
|
|
|
# TODO: chronos transports are leaking,
|
|
|
|
# but those are tracked for both the daemon
|
|
|
|
# and libp2p, so not sure which one it is,
|
|
|
|
# need to investigate more
|
|
|
|
# teardown:
|
|
|
|
# for tracker in testTrackers():
|
|
|
|
# # echo tracker.dump()
|
|
|
|
# # check tracker.isLeaked() == false
|
2020-05-23 17:14:22 +00:00
|
|
|
|
2020-09-21 09:16:29 +00:00
|
|
|
# TODO: this test is failing sometimes on windows
|
|
|
|
# For some reason we receive EOF before test 4 sometimes
|
2019-12-06 02:16:18 +00:00
|
|
|
test "native -> daemon multiple reads and writes":
|
|
|
|
proc runTests(): Future[bool] {.async.} =
|
|
|
|
var protos = @["/test-stream"]
|
|
|
|
|
2020-07-17 18:44:41 +00:00
|
|
|
let nativeNode = newStandardSwitch(
|
|
|
|
secureManagers = [SecureProtocol.Noise],
|
|
|
|
outTimeout = 5.minutes)
|
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
let awaiters = await nativeNode.start()
|
|
|
|
let daemonNode = await newDaemonApi()
|
|
|
|
let daemonPeer = await daemonNode.identity()
|
|
|
|
|
|
|
|
var testFuture = newFuture[void]("test.future")
|
|
|
|
proc daemonHandler(api: DaemonAPI, stream: P2PStream) {.async.} =
|
2020-06-03 02:21:11 +00:00
|
|
|
check string.fromBytes(await stream.transp.readLp()) == "test 1"
|
2020-10-15 01:37:20 +00:00
|
|
|
discard await stream.transp.writeLp("test 2")
|
2020-06-03 02:21:11 +00:00
|
|
|
check string.fromBytes(await stream.transp.readLp()) == "test 3"
|
2020-10-15 01:37:20 +00:00
|
|
|
discard await stream.transp.writeLp("test 4")
|
2019-12-06 02:16:18 +00:00
|
|
|
testFuture.complete()
|
|
|
|
|
|
|
|
await daemonNode.addHandler(protos, daemonHandler)
|
2019-12-07 16:36:39 +00:00
|
|
|
let conn = await nativeNode.dial(NativePeerInfo.init(daemonPeer.peer,
|
|
|
|
daemonPeer.addresses),
|
|
|
|
protos[0])
|
2019-12-06 02:16:18 +00:00
|
|
|
await conn.writeLp("test 1")
|
2020-06-03 02:21:11 +00:00
|
|
|
check "test 2" == string.fromBytes((await conn.readLp(1024)))
|
2020-10-15 01:37:20 +00:00
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
await conn.writeLp("test 3")
|
2020-06-03 02:21:11 +00:00
|
|
|
check "test 4" == string.fromBytes((await conn.readLp(1024)))
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
await wait(testFuture, 10.secs)
|
2020-05-23 17:14:22 +00:00
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
await nativeNode.stop()
|
2020-10-15 01:37:20 +00:00
|
|
|
await daemonNode.close()
|
2019-12-06 02:16:18 +00:00
|
|
|
await allFutures(awaiters)
|
2020-05-23 17:14:22 +00:00
|
|
|
|
|
|
|
await sleepAsync(1.seconds)
|
2019-12-06 02:16:18 +00:00
|
|
|
result = true
|
|
|
|
|
2020-10-15 01:37:20 +00:00
|
|
|
check:
|
|
|
|
waitFor(runTests()) == true
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
test "native -> daemon connection":
|
|
|
|
proc runTests(): Future[bool] {.async.} =
|
|
|
|
var protos = @["/test-stream"]
|
|
|
|
var test = "TEST STRING"
|
2020-03-06 19:19:43 +00:00
|
|
|
# We are preparing expect string, which should be prefixed with varint
|
|
|
|
# length and do not have `\r\n` suffix, because we going to use
|
|
|
|
# readLine().
|
|
|
|
var buffer = initVBuffer()
|
|
|
|
buffer.writeSeq(test & "\r\n")
|
|
|
|
buffer.finish()
|
|
|
|
var expect = newString(len(buffer) - 2)
|
|
|
|
copyMem(addr expect[0], addr buffer.buffer[0], len(expect))
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-07-17 18:44:41 +00:00
|
|
|
let nativeNode = newStandardSwitch(
|
2020-09-21 09:16:29 +00:00
|
|
|
secureManagers = [SecureProtocol.Noise],
|
2020-07-17 18:44:41 +00:00
|
|
|
outTimeout = 5.minutes)
|
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
let awaiters = await nativeNode.start()
|
|
|
|
|
|
|
|
let daemonNode = await newDaemonApi()
|
|
|
|
let daemonPeer = await daemonNode.identity()
|
|
|
|
|
|
|
|
var testFuture = newFuture[string]("test.future")
|
|
|
|
proc daemonHandler(api: DaemonAPI, stream: P2PStream) {.async.} =
|
2020-03-06 19:19:43 +00:00
|
|
|
# We should perform `readLp()` instead of `readLine()`. `readLine()`
|
|
|
|
# here reads actually length prefixed string.
|
2019-12-06 02:16:18 +00:00
|
|
|
var line = await stream.transp.readLine()
|
2020-03-06 19:19:43 +00:00
|
|
|
check line == expect
|
2019-12-06 02:16:18 +00:00
|
|
|
testFuture.complete(line)
|
2020-06-24 15:08:44 +00:00
|
|
|
await stream.close()
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
await daemonNode.addHandler(protos, daemonHandler)
|
2019-12-07 16:36:39 +00:00
|
|
|
let conn = await nativeNode.dial(NativePeerInfo.init(daemonPeer.peer,
|
|
|
|
daemonPeer.addresses),
|
|
|
|
protos[0])
|
2019-12-06 02:16:18 +00:00
|
|
|
await conn.writeLp(test & "\r\n")
|
2020-03-06 19:19:43 +00:00
|
|
|
result = expect == (await wait(testFuture, 10.secs))
|
2020-05-23 17:14:22 +00:00
|
|
|
|
|
|
|
await conn.close()
|
2019-12-06 02:16:18 +00:00
|
|
|
await nativeNode.stop()
|
|
|
|
await allFutures(awaiters)
|
|
|
|
await daemonNode.close()
|
|
|
|
|
|
|
|
check:
|
|
|
|
waitFor(runTests()) == true
|
|
|
|
|
|
|
|
test "daemon -> native connection":
|
|
|
|
proc runTests(): Future[bool] {.async.} =
|
|
|
|
var protos = @["/test-stream"]
|
|
|
|
var test = "TEST STRING"
|
|
|
|
|
|
|
|
var testFuture = newFuture[string]("test.future")
|
|
|
|
proc nativeHandler(conn: Connection, proto: string) {.async.} =
|
2020-06-03 02:21:11 +00:00
|
|
|
var line = string.fromBytes(await conn.readLp(1024))
|
2019-12-06 02:16:18 +00:00
|
|
|
check line == test
|
|
|
|
testFuture.complete(line)
|
|
|
|
await conn.close()
|
|
|
|
|
|
|
|
# custom proto
|
|
|
|
var proto = new LPProtocol
|
|
|
|
proto.handler = nativeHandler
|
|
|
|
proto.codec = protos[0] # codec
|
|
|
|
|
2020-07-17 18:44:41 +00:00
|
|
|
let nativeNode = newStandardSwitch(
|
|
|
|
secureManagers = [SecureProtocol.Noise], outTimeout = 5.minutes)
|
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
nativeNode.mount(proto)
|
|
|
|
|
|
|
|
let awaiters = await nativeNode.start()
|
|
|
|
let nativePeer = nativeNode.peerInfo
|
|
|
|
|
|
|
|
let daemonNode = await newDaemonApi()
|
2019-12-07 16:36:39 +00:00
|
|
|
await daemonNode.connect(nativePeer.peerId, nativePeer.addrs)
|
|
|
|
var stream = await daemonNode.openStream(nativePeer.peerId, protos)
|
2019-12-06 02:16:18 +00:00
|
|
|
discard await stream.transp.writeLp(test)
|
|
|
|
|
|
|
|
result = test == (await wait(testFuture, 10.secs))
|
2020-06-24 15:08:44 +00:00
|
|
|
|
|
|
|
await stream.close()
|
2019-12-06 02:16:18 +00:00
|
|
|
await nativeNode.stop()
|
|
|
|
await allFutures(awaiters)
|
|
|
|
await daemonNode.close()
|
2020-06-24 15:08:44 +00:00
|
|
|
await sleepAsync(1.seconds)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
waitFor(runTests()) == true
|
|
|
|
|
|
|
|
test "daemon -> multiple reads and writes":
|
|
|
|
proc runTests(): Future[bool] {.async.} =
|
|
|
|
var protos = @["/test-stream"]
|
|
|
|
|
|
|
|
var testFuture = newFuture[void]("test.future")
|
|
|
|
proc nativeHandler(conn: Connection, proto: string) {.async.} =
|
2020-06-03 02:21:11 +00:00
|
|
|
check "test 1" == string.fromBytes(await conn.readLp(1024))
|
|
|
|
await conn.writeLp("test 2".toBytes())
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-06-03 02:21:11 +00:00
|
|
|
check "test 3" == string.fromBytes(await conn.readLp(1024))
|
|
|
|
await conn.writeLp("test 4".toBytes())
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
testFuture.complete()
|
|
|
|
await conn.close()
|
|
|
|
|
|
|
|
# custom proto
|
|
|
|
var proto = new LPProtocol
|
|
|
|
proto.handler = nativeHandler
|
|
|
|
proto.codec = protos[0] # codec
|
|
|
|
|
2020-07-17 18:44:41 +00:00
|
|
|
let nativeNode = newStandardSwitch(
|
2020-09-21 09:16:29 +00:00
|
|
|
secureManagers = [SecureProtocol.Noise], outTimeout = 5.minutes)
|
2020-07-17 18:44:41 +00:00
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
nativeNode.mount(proto)
|
|
|
|
|
|
|
|
let awaiters = await nativeNode.start()
|
|
|
|
let nativePeer = nativeNode.peerInfo
|
|
|
|
|
|
|
|
let daemonNode = await newDaemonApi()
|
2019-12-07 16:36:39 +00:00
|
|
|
await daemonNode.connect(nativePeer.peerId, nativePeer.addrs)
|
|
|
|
var stream = await daemonNode.openStream(nativePeer.peerId, protos)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
asyncDiscard stream.transp.writeLp("test 1")
|
2020-06-03 02:21:11 +00:00
|
|
|
check "test 2" == string.fromBytes(await stream.transp.readLp())
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
asyncDiscard stream.transp.writeLp("test 3")
|
2020-06-03 02:21:11 +00:00
|
|
|
check "test 4" == string.fromBytes(await stream.transp.readLp())
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
await wait(testFuture, 10.secs)
|
|
|
|
|
|
|
|
result = true
|
2020-06-24 15:08:44 +00:00
|
|
|
await stream.close()
|
2019-12-06 02:16:18 +00:00
|
|
|
await nativeNode.stop()
|
|
|
|
await allFutures(awaiters)
|
|
|
|
await daemonNode.close()
|
|
|
|
|
|
|
|
check:
|
|
|
|
waitFor(runTests()) == true
|
|
|
|
|
|
|
|
test "read write multiple":
|
|
|
|
proc runTests(): Future[bool] {.async.} =
|
|
|
|
var protos = @["/test-stream"]
|
|
|
|
var test = "TEST STRING"
|
|
|
|
|
|
|
|
var count = 0
|
|
|
|
var testFuture = newFuture[int]("test.future")
|
|
|
|
proc nativeHandler(conn: Connection, proto: string) {.async.} =
|
|
|
|
while count < 10:
|
2020-06-03 02:21:11 +00:00
|
|
|
var line = string.fromBytes(await conn.readLp(1024))
|
2019-12-06 02:16:18 +00:00
|
|
|
check line == test
|
2020-06-03 02:21:11 +00:00
|
|
|
await conn.writeLp(test.toBytes())
|
2019-12-06 02:16:18 +00:00
|
|
|
count.inc()
|
|
|
|
|
|
|
|
testFuture.complete(count)
|
|
|
|
await conn.close()
|
|
|
|
|
|
|
|
# custom proto
|
|
|
|
var proto = new LPProtocol
|
|
|
|
proto.handler = nativeHandler
|
|
|
|
proto.codec = protos[0] # codec
|
|
|
|
|
2020-07-17 18:44:41 +00:00
|
|
|
let nativeNode = newStandardSwitch(
|
|
|
|
secureManagers = [SecureProtocol.Noise], outTimeout = 5.minutes)
|
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
nativeNode.mount(proto)
|
|
|
|
|
|
|
|
let awaiters = await nativeNode.start()
|
|
|
|
let nativePeer = nativeNode.peerInfo
|
|
|
|
|
|
|
|
let daemonNode = await newDaemonApi()
|
2019-12-07 16:36:39 +00:00
|
|
|
await daemonNode.connect(nativePeer.peerId, nativePeer.addrs)
|
|
|
|
var stream = await daemonNode.openStream(nativePeer.peerId, protos)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-03-27 16:02:14 +00:00
|
|
|
var count2 = 0
|
|
|
|
while count2 < 10:
|
2019-12-06 02:16:18 +00:00
|
|
|
discard await stream.transp.writeLp(test)
|
|
|
|
let line = await stream.transp.readLp()
|
2020-06-03 02:21:11 +00:00
|
|
|
check test == string.fromBytes(line)
|
2020-03-27 16:02:14 +00:00
|
|
|
inc(count2)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-05-15 03:56:56 +00:00
|
|
|
result = 10 == (await wait(testFuture, 1.minutes))
|
2020-03-29 14:28:48 +00:00
|
|
|
await stream.close()
|
2019-12-06 02:16:18 +00:00
|
|
|
await nativeNode.stop()
|
|
|
|
await allFutures(awaiters)
|
|
|
|
await daemonNode.close()
|
|
|
|
|
|
|
|
check:
|
|
|
|
waitFor(runTests()) == true
|
|
|
|
|
2019-12-23 18:45:12 +00:00
|
|
|
test "floodsub: daemon publish one":
|
2019-12-06 02:16:18 +00:00
|
|
|
check:
|
|
|
|
waitFor(testPubSubDaemonPublish()) == true
|
|
|
|
|
2019-12-23 18:45:12 +00:00
|
|
|
test "floodsub: daemon publish many":
|
2019-12-06 02:16:18 +00:00
|
|
|
check:
|
2019-12-23 18:45:12 +00:00
|
|
|
waitFor(testPubSubDaemonPublish(count = 10)) == true
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2019-12-23 18:45:12 +00:00
|
|
|
test "gossipsub: daemon publish one":
|
|
|
|
check:
|
|
|
|
waitFor(testPubSubDaemonPublish(gossip = true)) == true
|
|
|
|
|
|
|
|
test "gossipsub: daemon publish many":
|
|
|
|
check:
|
|
|
|
waitFor(testPubSubDaemonPublish(gossip = true, count = 10)) == true
|
|
|
|
|
|
|
|
test "floodsub: node publish one":
|
2019-12-06 02:16:18 +00:00
|
|
|
check:
|
|
|
|
waitFor(testPubSubNodePublish()) == true
|
|
|
|
|
2019-12-23 18:45:12 +00:00
|
|
|
test "floodsub: node publish many":
|
|
|
|
check:
|
|
|
|
waitFor(testPubSubNodePublish(count = 10)) == true
|
|
|
|
|
|
|
|
test "gossipsub: node publish one":
|
|
|
|
check:
|
|
|
|
waitFor(testPubSubNodePublish(gossip = true)) == true
|
|
|
|
|
|
|
|
test "gossipsub: node publish many":
|
2019-12-06 02:16:18 +00:00
|
|
|
check:
|
2019-12-23 18:45:12 +00:00
|
|
|
waitFor(testPubSubNodePublish(gossip = true, count = 10)) == true
|