2022-07-01 18:20:42 +00:00
|
|
|
import chronos, unittest2, helpers, stublogger
|
2018-12-16 13:51:12 +00:00
|
|
|
import ../libp2p/daemon/daemonapi, ../libp2p/multiaddress, ../libp2p/multicodec,
|
2020-07-01 06:25:09 +00:00
|
|
|
../libp2p/cid, ../libp2p/multihash, ../libp2p/peerid
|
2018-11-19 04:34:05 +00:00
|
|
|
|
2019-10-29 18:51:48 +00:00
|
|
|
when defined(nimHasUsed): {.used.}
|
|
|
|
|
2018-11-19 04:34:05 +00:00
|
|
|
proc identitySpawnTest(): Future[bool] {.async.} =
|
2018-12-10 20:55:06 +00:00
|
|
|
var api = await newDaemonApi()
|
2018-11-19 04:34:05 +00:00
|
|
|
var data = await api.identity()
|
|
|
|
await api.close()
|
|
|
|
result = true
|
|
|
|
|
2018-11-19 17:30:40 +00:00
|
|
|
proc connectStreamTest(): Future[bool] {.async.} =
|
2018-12-10 20:55:06 +00:00
|
|
|
var api1 = await newDaemonApi()
|
|
|
|
var api2 = await newDaemonApi()
|
2018-11-19 17:30:40 +00:00
|
|
|
|
|
|
|
var id1 = await api1.identity()
|
|
|
|
var id2 = await api2.identity()
|
|
|
|
|
|
|
|
var protos = @["/test-stream"]
|
|
|
|
var test = "TEST STRING"
|
|
|
|
|
|
|
|
var testFuture = newFuture[string]("test.future")
|
|
|
|
|
|
|
|
proc streamHandler(api: DaemonAPI, stream: P2PStream) {.async.} =
|
|
|
|
var line = await stream.transp.readLine()
|
|
|
|
testFuture.complete(line)
|
|
|
|
|
|
|
|
await api2.addHandler(protos, streamHandler)
|
|
|
|
await api1.connect(id2.peer, id2.addresses)
|
2018-12-12 14:08:55 +00:00
|
|
|
# echo await api1.listPeers()
|
2018-11-19 17:30:40 +00:00
|
|
|
var stream = await api1.openStream(id2.peer, protos)
|
|
|
|
let sent = await stream.transp.write(test & "\r\n")
|
|
|
|
doAssert(sent == len(test) + 2)
|
2019-03-30 23:32:04 +00:00
|
|
|
var check = await wait(testFuture, 10.seconds)
|
2018-11-19 17:30:40 +00:00
|
|
|
doAssert(check == test)
|
2018-12-10 10:38:12 +00:00
|
|
|
await stream.close()
|
2018-11-19 20:53:20 +00:00
|
|
|
await api1.close()
|
|
|
|
await api2.close()
|
2018-11-19 17:30:40 +00:00
|
|
|
result = true
|
|
|
|
|
2020-03-24 08:05:11 +00:00
|
|
|
# proc provideCidTest(): Future[bool] {.async.} =
|
|
|
|
# var api1 = await newDaemonApi({DHTFull})
|
|
|
|
# var api2 = await newDaemonApi({DHTFull})
|
|
|
|
# var msg = "ethereum2-beacon-chain"
|
|
|
|
# var bmsg = cast[seq[byte]](msg)
|
|
|
|
# var mh = MultiHash.digest("sha2-256", bmsg)
|
|
|
|
# var cid = Cid.init(CIDv1, multiCodec("dag-pb"), mh)
|
2018-12-16 13:51:12 +00:00
|
|
|
|
2020-03-24 08:05:11 +00:00
|
|
|
# var id1 = await api1.identity()
|
|
|
|
# var id2 = await api2.identity()
|
2018-12-16 13:51:12 +00:00
|
|
|
|
2020-03-24 08:05:11 +00:00
|
|
|
# await api1.connect(id2.peer, id2.addresses)
|
2019-02-28 12:17:39 +00:00
|
|
|
|
2020-03-24 08:05:11 +00:00
|
|
|
# while true:
|
|
|
|
# var peers = await api1.listPeers()
|
|
|
|
# if len(peers) > 0:
|
|
|
|
# break
|
2018-12-16 13:51:12 +00:00
|
|
|
|
2020-03-24 08:05:11 +00:00
|
|
|
# await api1.dhtProvide(cid)
|
|
|
|
# var peers = await api2.dhtFindProviders(cid, 10)
|
2018-12-16 13:51:12 +00:00
|
|
|
|
2020-03-24 08:05:11 +00:00
|
|
|
# if len(peers) == 1:
|
|
|
|
# if peers[0].peer == id1.peer:
|
|
|
|
# result = true
|
2018-12-16 13:51:12 +00:00
|
|
|
|
2020-03-24 08:05:11 +00:00
|
|
|
# await api1.close()
|
|
|
|
# await api2.close()
|
2018-12-09 16:44:20 +00:00
|
|
|
|
|
|
|
proc pubsubTest(f: set[P2PDaemonFlags]): Future[bool] {.async.} =
|
|
|
|
var pubsubData = "TEST MESSAGE"
|
|
|
|
var msgData = cast[seq[byte]](pubsubData)
|
2018-12-10 20:55:06 +00:00
|
|
|
var api1, api2: DaemonAPI
|
2018-12-11 02:17:36 +00:00
|
|
|
|
2019-08-21 21:42:44 +00:00
|
|
|
api1 = await newDaemonApi(f)
|
|
|
|
api2 = await newDaemonApi(f)
|
2018-12-09 16:44:20 +00:00
|
|
|
|
|
|
|
var id1 = await api1.identity()
|
|
|
|
var id2 = await api2.identity()
|
|
|
|
|
|
|
|
var resultsCount = 0
|
|
|
|
|
2018-12-10 20:55:06 +00:00
|
|
|
var handlerFuture1 = newFuture[void]()
|
|
|
|
var handlerFuture2 = newFuture[void]()
|
2018-12-09 16:44:20 +00:00
|
|
|
|
|
|
|
proc pubsubHandler1(api: DaemonAPI,
|
|
|
|
ticket: PubsubTicket,
|
|
|
|
message: PubSubMessage): Future[bool] {.async.} =
|
|
|
|
let smsg = cast[string](message.data)
|
|
|
|
if smsg == pubsubData:
|
|
|
|
inc(resultsCount)
|
2018-12-10 20:55:06 +00:00
|
|
|
handlerFuture1.complete()
|
2018-12-09 16:44:20 +00:00
|
|
|
# Callback must return `false` to close subscription channel.
|
2018-12-10 20:55:06 +00:00
|
|
|
result = false
|
2018-12-09 16:44:20 +00:00
|
|
|
|
|
|
|
proc pubsubHandler2(api: DaemonAPI,
|
|
|
|
ticket: PubsubTicket,
|
|
|
|
message: PubSubMessage): Future[bool] {.async.} =
|
|
|
|
let smsg = cast[string](message.data)
|
|
|
|
if smsg == pubsubData:
|
|
|
|
inc(resultsCount)
|
2018-12-10 20:55:06 +00:00
|
|
|
handlerFuture2.complete()
|
2018-12-09 16:44:20 +00:00
|
|
|
# Callback must return `false` to close subscription channel.
|
|
|
|
result = false
|
|
|
|
|
2018-12-14 09:00:49 +00:00
|
|
|
await api1.connect(id2.peer, id2.addresses)
|
|
|
|
await api2.connect(id1.peer, id1.addresses)
|
2018-12-09 16:44:20 +00:00
|
|
|
|
2018-12-12 14:08:55 +00:00
|
|
|
var ticket1 = await api1.pubsubSubscribe("test-topic", pubsubHandler1)
|
|
|
|
var ticket2 = await api2.pubsubSubscribe("test-topic", pubsubHandler2)
|
2018-12-10 20:55:06 +00:00
|
|
|
|
2019-03-30 23:32:04 +00:00
|
|
|
await sleepAsync(2.seconds)
|
2018-12-09 16:44:20 +00:00
|
|
|
|
2018-12-12 14:08:55 +00:00
|
|
|
var topics1 = await api1.pubsubGetTopics()
|
|
|
|
var topics2 = await api2.pubsubGetTopics()
|
2018-12-10 20:55:06 +00:00
|
|
|
|
2018-12-12 14:08:55 +00:00
|
|
|
if len(topics1) == 1 and len(topics2) == 1:
|
|
|
|
var peers1 = await api1.pubsubListPeers("test-topic")
|
|
|
|
var peers2 = await api2.pubsubListPeers("test-topic")
|
|
|
|
if len(peers1) == 1 and len(peers2) == 1:
|
|
|
|
# Publish test data via api1.
|
2019-03-30 23:32:04 +00:00
|
|
|
await sleepAsync(500.milliseconds)
|
2018-12-12 14:08:55 +00:00
|
|
|
await api1.pubsubPublish("test-topic", msgData)
|
2019-08-01 05:56:59 +00:00
|
|
|
var res = await one(allFutures(handlerFuture1, handlerFuture2),
|
|
|
|
sleepAsync(10.seconds))
|
2018-12-11 02:17:36 +00:00
|
|
|
|
2018-12-09 16:44:20 +00:00
|
|
|
await api1.close()
|
|
|
|
await api2.close()
|
|
|
|
if resultsCount == 2:
|
|
|
|
result = true
|
2018-11-22 15:35:44 +00:00
|
|
|
|
2018-11-19 04:34:05 +00:00
|
|
|
when isMainModule:
|
|
|
|
suite "libp2p-daemon test suite":
|
|
|
|
test "Simple spawn and get identity test":
|
2018-11-19 04:42:50 +00:00
|
|
|
check:
|
|
|
|
waitFor(identitySpawnTest()) == true
|
2018-11-19 17:30:40 +00:00
|
|
|
test "Connect/Accept peer/stream test":
|
|
|
|
check:
|
2018-11-19 20:53:20 +00:00
|
|
|
waitFor(connectStreamTest()) == true
|
2020-03-24 08:05:11 +00:00
|
|
|
# test "Provide CID test":
|
|
|
|
# check:
|
|
|
|
# waitFor(provideCidTest()) == true
|
2021-12-27 10:17:00 +00:00
|
|
|
flakyAsyncTest "GossipSub test", attempts=4:
|
|
|
|
check (await pubsubTest({PSGossipSub})) == true
|
|
|
|
flakyAsyncTest "FloodSub test", attempts=4:
|
|
|
|
check (await pubsubTest({PSFloodSub})) == true
|