diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 3ffad46..ab57df2 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -119,7 +119,7 @@ steps: nimble install -y --depsOnly # run tests - nimble test + nimble test_slim nimble examples_build displayName: 'build and test' diff --git a/libp2p.nimble b/libp2p.nimble index c21ebc7..f95e925 100644 --- a/libp2p.nimble +++ b/libp2p.nimble @@ -50,10 +50,18 @@ task testpubsub, "Runs pubsub tests": runTest("pubsub/testgossipinternal", sign = false, verify = false, moreoptions = "-d:pubsub_internal_testing") runTest("pubsub/testpubsub") runTest("pubsub/testpubsub", sign = false, verify = false) + runTest("pubsub/testpubsub", sign = false, verify = false, moreoptions = "-d:libp2p_pubsub_anonymize=true") runTest("pubsub/testgossipinternal10", sign = false, verify = false, moreoptions = "-d:pubsub_internal_testing") runTest("pubsub/testpubsub", moreoptions = "-d:fallback_gossipsub_10") runTest("pubsub/testpubsub", sign = false, verify = false, moreoptions = "-d:fallback_gossipsub_10") +task testpubsub_slim, "Runs pubsub tests": + runTest("pubsub/testgossipinternal", sign = false, verify = false, moreoptions = "-d:pubsub_internal_testing") + runTest("pubsub/testpubsub") + runTest("pubsub/testpubsub", sign = false, verify = false, moreoptions = "-d:libp2p_pubsub_anonymize=true") + runTest("pubsub/testgossipinternal10", sign = false, verify = false, moreoptions = "-d:pubsub_internal_testing") + runTest("pubsub/testpubsub", moreoptions = "-d:fallback_gossipsub_10") + task testfilter, "Run PKI filter test": runTest("testpkifilter", moreoptions = "-d:libp2p_pki_schemes=\"secp256k1\"") @@ -71,5 +79,12 @@ task test, "Runs the test suite": exec "nimble testinterop" exec "nimble testfilter" +task test_slim, "Runs the test suite": + exec "nimble testnative" + exec "nimble testpubsub_slim" + exec "nimble testdaemon" + exec "nimble testinterop" + exec "nimble testfilter" + task examples_build, "Build the samples": buildSample("directchat") diff --git a/libp2p/protocols/pubsub/pubsub.nim b/libp2p/protocols/pubsub/pubsub.nim index 88a2634..0cbca32 100644 --- a/libp2p/protocols/pubsub/pubsub.nim +++ b/libp2p/protocols/pubsub/pubsub.nim @@ -125,7 +125,8 @@ method onPubSubPeerEvent*(p: PubSub, peer: PubsubPeer, event: PubsubPeerEvent) { # Peer event is raised for the send connection in particular case event.kind of PubSubPeerEventKind.Connected: - p.sendSubs(peer, toSeq(p.topics.keys), true) + if p.topics.len > 0: + p.sendSubs(peer, toSeq(p.topics.keys), true) of PubSubPeerEventKind.Disconnected: discard @@ -357,7 +358,6 @@ proc init*[PubParams: object | bool]( topics: initTable[string, Topic](), msgIdProvider: msgIdProvider, parameters: parameters) - pubsub.initPubSub() proc peerEventHandler(peerId: PeerID, event: PeerEvent) {.async.} = if event == PeerEvent.Joined: @@ -369,6 +369,7 @@ proc init*[PubParams: object | bool]( switch.addPeerEventHandler(peerEventHandler, PeerEvent.Left) pubsub.initPubSub() + return pubsub diff --git a/libp2p/protocols/pubsub/rpc/message.nim b/libp2p/protocols/pubsub/rpc/message.nim index e5e4f28..7cc2d47 100644 --- a/libp2p/protocols/pubsub/rpc/message.nim +++ b/libp2p/protocols/pubsub/rpc/message.nim @@ -9,6 +9,7 @@ {.push raises: [Defect].} +import hashes import chronicles, metrics, stew/[byteutils, endians2] import ./messages, ./protobuf, @@ -28,7 +29,10 @@ declareCounter(libp2p_pubsub_sig_verify_success, "pubsub successfully validated declareCounter(libp2p_pubsub_sig_verify_failure, "pubsub failed validated messages") func defaultMsgIdProvider*(m: Message): string = - byteutils.toHex(m.seqno) & $m.fromPeer + if m.seqno.len > 0 and m.fromPeer.data.len > 0: + byteutils.toHex(m.seqno) & $m.fromPeer + else: + $m.data.hash & $m.topicIDs.hash proc sign*(msg: Message, privateKey: PrivateKey): CryptoResult[seq[byte]] = ok((? privateKey.sign(PubSubPrefix & encodeMessage(msg, false))).getBytes()) diff --git a/tests/pubsub/testfloodsub.nim b/tests/pubsub/testfloodsub.nim index 7299b47..dcd8f02 100644 --- a/tests/pubsub/testfloodsub.nim +++ b/tests/pubsub/testfloodsub.nim @@ -326,7 +326,7 @@ suite "FloodSub": var pubs: seq[Future[int]] for i in 0..= runs: seenFut.complete() - subs &= dialer.subscribe("foobar", handler) - - await allFuturesThrowing(subs).wait(30.seconds) - + await dialer.subscribe("foobar", handler) + await waitSub(nodes[0], dialer, "foobar") + tryPublish await wait(nodes[0].publish("foobar", toBytes("from node " & - $nodes[1].peerInfo.peerId)), - 1.minutes), runs, 5.seconds + $nodes[0].peerInfo.peerId)), + 1.minutes), 1, 5.seconds await wait(seenFut, 2.minutes) check: seen.len >= runs @@ -591,7 +589,6 @@ suite "GossipSub": await subscribeNodes(nodes) var seen: Table[PeerID, int] - var subs: seq[Future[void]] var seenFut = newFuture[void]() for dialer in nodes: var handler: TopicHandler @@ -606,14 +603,13 @@ suite "GossipSub": if not seenFut.finished() and seen.len >= runs: seenFut.complete() - subs &= dialer.subscribe("foobar", handler) - subs &= waitSub(nodes[0], dialer, "foobar") + await dialer.subscribe("foobar", handler) + await waitSub(nodes[0], dialer, "foobar") - await allFuturesThrowing(subs) tryPublish await wait(nodes[0].publish("foobar", toBytes("from node " & - $nodes[1].peerInfo.peerId)), - 1.minutes), 2, 5.seconds + $nodes[0].peerInfo.peerId)), + 1.minutes), 1, 5.seconds await wait(seenFut, 5.minutes) check: seen.len >= runs diff --git a/tests/pubsub/utils.nim b/tests/pubsub/utils.nim index 030af27..70e913a 100644 --- a/tests/pubsub/utils.nim +++ b/tests/pubsub/utils.nim @@ -2,6 +2,7 @@ const libp2p_pubsub_sign {.booldefine.} = true libp2p_pubsub_verify {.booldefine.} = true + libp2p_pubsub_anonymize {.booldefine.} = false import random import chronos @@ -28,6 +29,7 @@ proc generateNodes*( gossip: bool = false, triggerSelf: bool = false, verifySignature: bool = libp2p_pubsub_verify, + anonymize: bool = libp2p_pubsub_anonymize, sign: bool = libp2p_pubsub_sign): seq[PubSub] = for i in 0..