-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:waku-org/js-waku into chore/renam…
…e-local-peer-cache
- Loading branch information
Showing
8 changed files
with
481 additions
and
188 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import tests from "@libp2p/interface-compliance-tests/peer-discovery"; | ||
import type { LightNode } from "@waku/interfaces"; | ||
import { PeerExchangeCodec, PeerExchangeDiscovery } from "@waku/peer-exchange"; | ||
import { createLightNode } from "@waku/sdk"; | ||
import { singleShardInfoToPubsubTopic } from "@waku/utils"; | ||
|
||
import { | ||
beforeEachCustom, | ||
makeLogFileName, | ||
ServiceNode, | ||
tearDownNodes | ||
} from "../../src/index.js"; | ||
|
||
const pubsubTopic = [singleShardInfoToPubsubTopic({ clusterId: 0, shard: 2 })]; | ||
|
||
describe("Peer Exchange", function () { | ||
describe("Compliance Test", function () { | ||
this.timeout(100_000); | ||
|
||
let waku: LightNode; | ||
let nwaku1: ServiceNode; | ||
let nwaku2: ServiceNode; | ||
|
||
beforeEachCustom(this, async () => { | ||
nwaku1 = new ServiceNode(makeLogFileName(this.ctx) + "1"); | ||
nwaku2 = new ServiceNode(makeLogFileName(this.ctx) + "2"); | ||
}); | ||
|
||
tests({ | ||
async setup() { | ||
await nwaku1.start({ | ||
relay: true, | ||
discv5Discovery: true, | ||
peerExchange: true | ||
}); | ||
|
||
const enr = (await nwaku1.info()).enrUri; | ||
|
||
await nwaku2.start({ | ||
relay: true, | ||
discv5Discovery: true, | ||
peerExchange: true, | ||
discv5BootstrapNode: enr | ||
}); | ||
|
||
waku = await createLightNode(); | ||
await waku.start(); | ||
|
||
const nwaku2Ma = await nwaku2.getMultiaddrWithId(); | ||
|
||
// we do this because we want peer-exchange discovery to get initialised before we dial the peer which contains info about the other peer | ||
setTimeout(() => { | ||
void waku.libp2p.dialProtocol(nwaku2Ma, PeerExchangeCodec); | ||
}, 1000); | ||
|
||
return new PeerExchangeDiscovery(waku.libp2p.components, pubsubTopic); | ||
}, | ||
teardown: async () => { | ||
this.timeout(15000); | ||
await tearDownNodes([nwaku1, nwaku2], waku); | ||
} | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,236 @@ | ||
import { bootstrap } from "@libp2p/bootstrap"; | ||
import type { PeerId } from "@libp2p/interface"; | ||
import type { LightNode, PeersByDiscoveryResult } from "@waku/interfaces"; | ||
import { wakuPeerExchangeDiscovery } from "@waku/peer-exchange"; | ||
import { createLightNode, Tags } from "@waku/sdk"; | ||
import { Logger, singleShardInfoToPubsubTopic } from "@waku/utils"; | ||
import { expect } from "chai"; | ||
import Sinon, { SinonSpy } from "sinon"; | ||
|
||
import { | ||
afterEachCustom, | ||
beforeEachCustom, | ||
makeLogFileName, | ||
ServiceNode, | ||
tearDownNodes | ||
} from "../../src/index.js"; | ||
|
||
export const log = new Logger("test:pe"); | ||
const pubsubTopic = [singleShardInfoToPubsubTopic({ clusterId: 0, shard: 2 })]; | ||
|
||
describe("Peer Exchange", function () { | ||
this.timeout(150_000); | ||
let waku: LightNode; | ||
let nwaku1: ServiceNode; | ||
let nwaku2: ServiceNode; | ||
let nwaku3: ServiceNode; | ||
let dialPeerSpy: SinonSpy; | ||
let nwaku1PeerId: PeerId; | ||
|
||
beforeEachCustom(this, async () => { | ||
nwaku1 = new ServiceNode(makeLogFileName(this.ctx) + "1"); | ||
nwaku2 = new ServiceNode(makeLogFileName(this.ctx) + "2"); | ||
await nwaku1.start({ | ||
pubsubTopic: pubsubTopic, | ||
discv5Discovery: true, | ||
peerExchange: true, | ||
relay: true | ||
}); | ||
await nwaku2.start({ | ||
pubsubTopic: pubsubTopic, | ||
discv5Discovery: true, | ||
peerExchange: true, | ||
discv5BootstrapNode: (await nwaku1.info()).enrUri, | ||
relay: true | ||
}); | ||
nwaku1PeerId = await nwaku1.getPeerId(); | ||
}); | ||
|
||
afterEachCustom(this, async () => { | ||
await tearDownNodes([nwaku1, nwaku2, nwaku3], waku); | ||
}); | ||
|
||
it("getPeersByDiscovery", async function () { | ||
waku = await createLightNode({ | ||
libp2p: { | ||
peerDiscovery: [ | ||
bootstrap({ list: [(await nwaku2.getMultiaddrWithId()).toString()] }), | ||
wakuPeerExchangeDiscovery(pubsubTopic) | ||
] | ||
} | ||
}); | ||
await waku.start(); | ||
dialPeerSpy = Sinon.spy((waku as any).connectionManager, "dialPeer"); | ||
const pxPeersDiscovered = new Set<PeerId>(); | ||
await new Promise<void>((resolve) => { | ||
waku.libp2p.addEventListener("peer:discovery", (evt) => { | ||
return void (async () => { | ||
const peerId = evt.detail.id; | ||
const peer = await waku.libp2p.peerStore.get(peerId); | ||
const tags = Array.from(peer.tags.keys()); | ||
if (tags.includes(Tags.PEER_EXCHANGE)) { | ||
pxPeersDiscovered.add(peerId); | ||
if (pxPeersDiscovered.size === 1) { | ||
resolve(); | ||
} | ||
} | ||
})(); | ||
}); | ||
}); | ||
expect(dialPeerSpy.callCount).to.equal(1); | ||
|
||
const peers_after = <PeersByDiscoveryResult>( | ||
await waku.connectionManager.getPeersByDiscovery() | ||
); | ||
const discovered_peer_exchange = peers_after.DISCOVERED[Tags.PEER_EXCHANGE]; | ||
const discovered_bootstram = peers_after.DISCOVERED[Tags.BOOTSTRAP]; | ||
const connected_peer_exchange = peers_after.CONNECTED[Tags.PEER_EXCHANGE]; | ||
const connected_bootstram = peers_after.CONNECTED[Tags.BOOTSTRAP]; | ||
expect(discovered_peer_exchange.length).to.eq(1); | ||
expect(discovered_peer_exchange[0].id.toString()).to.eq( | ||
nwaku1PeerId.toString() | ||
); | ||
expect(discovered_peer_exchange[0].tags.has("peer-exchange")).to.be.true; | ||
expect(discovered_bootstram.length).to.eq(1); | ||
expect(connected_peer_exchange.length).to.eq(0); | ||
expect(connected_bootstram.length).to.eq(1); | ||
}); | ||
|
||
// will be skipped until https://github.com/waku-org/js-waku/issues/1860 is fixed | ||
it.skip("new peer added after a peer was already found", async function () { | ||
waku = await createLightNode({ | ||
libp2p: { | ||
peerDiscovery: [ | ||
bootstrap({ list: [(await nwaku2.getMultiaddrWithId()).toString()] }), | ||
wakuPeerExchangeDiscovery(pubsubTopic) | ||
] | ||
} | ||
}); | ||
await waku.start(); | ||
|
||
dialPeerSpy = Sinon.spy((waku as any).connectionManager, "dialPeer"); | ||
const pxPeersDiscovered = new Set<PeerId>(); | ||
await new Promise<void>((resolve) => { | ||
waku.libp2p.addEventListener("peer:discovery", (evt) => { | ||
return void (async () => { | ||
const peerId = evt.detail.id; | ||
const peer = await waku.libp2p.peerStore.get(peerId); | ||
const tags = Array.from(peer.tags.keys()); | ||
if (tags.includes(Tags.PEER_EXCHANGE)) { | ||
pxPeersDiscovered.add(peerId); | ||
if (pxPeersDiscovered.size === 1) { | ||
resolve(); | ||
} | ||
} | ||
})(); | ||
}); | ||
}); | ||
|
||
nwaku3 = new ServiceNode(makeLogFileName(this) + "3"); | ||
await nwaku3.start({ | ||
pubsubTopic: pubsubTopic, | ||
discv5Discovery: true, | ||
peerExchange: true, | ||
discv5BootstrapNode: (await nwaku1.info()).enrUri, | ||
relay: true, | ||
lightpush: true, | ||
filter: true | ||
}); | ||
|
||
await new Promise<void>((resolve) => { | ||
waku.libp2p.addEventListener("peer:discovery", (evt) => { | ||
return void (async () => { | ||
const peerId = evt.detail.id; | ||
const peer = await waku.libp2p.peerStore.get(peerId); | ||
const tags = Array.from(peer.tags.keys()); | ||
if (tags.includes(Tags.PEER_EXCHANGE)) { | ||
pxPeersDiscovered.add(peerId); | ||
if (pxPeersDiscovered.size === 2) { | ||
resolve(); | ||
} | ||
} | ||
})(); | ||
}); | ||
}); | ||
}); | ||
|
||
// will be skipped until https://github.com/waku-org/js-waku/issues/1858 is fixed | ||
it.skip("wrong wakuPeerExchangeDiscovery pubsub topic", async function () { | ||
waku = await createLightNode({ | ||
libp2p: { | ||
peerDiscovery: [ | ||
bootstrap({ list: [(await nwaku2.getMultiaddrWithId()).toString()] }), | ||
wakuPeerExchangeDiscovery(["wrong"]) | ||
] | ||
} | ||
}); | ||
await waku.start(); | ||
dialPeerSpy = Sinon.spy((waku as any).connectionManager, "dialPeer"); | ||
|
||
const pxPeersDiscovered = new Set<PeerId>(); | ||
await new Promise<void>((resolve) => { | ||
const timeoutId = setTimeout(() => { | ||
resolve(); | ||
}, 40000); | ||
|
||
waku.libp2p.addEventListener("peer:discovery", (evt) => { | ||
return void (async () => { | ||
const peerId = evt.detail.id; | ||
const peer = await waku.libp2p.peerStore.get(peerId); | ||
const tags = Array.from(peer.tags.keys()); | ||
if (tags.includes(Tags.PEER_EXCHANGE)) { | ||
pxPeersDiscovered.add(peerId); | ||
if (pxPeersDiscovered.size === 1) { | ||
clearTimeout(timeoutId); | ||
resolve(); | ||
} | ||
} | ||
})(); | ||
}); | ||
}); | ||
|
||
expect( | ||
pxPeersDiscovered.size, | ||
"No peer should have been discovered" | ||
).to.equal(0); | ||
}); | ||
|
||
it("peerDiscovery without wakuPeerExchangeDiscovery", async function () { | ||
waku = await createLightNode({ | ||
libp2p: { | ||
peerDiscovery: [ | ||
bootstrap({ list: [(await nwaku2.getMultiaddrWithId()).toString()] }) | ||
] | ||
} | ||
}); | ||
await waku.start(); | ||
dialPeerSpy = Sinon.spy((waku as any).connectionManager, "dialPeer"); | ||
|
||
const pxPeersDiscovered = new Set<PeerId>(); | ||
await new Promise<void>((resolve) => { | ||
const timeoutId = setTimeout(() => { | ||
resolve(); | ||
}, 40000); | ||
|
||
waku.libp2p.addEventListener("peer:discovery", (evt) => { | ||
return void (async () => { | ||
const peerId = evt.detail.id; | ||
const peer = await waku.libp2p.peerStore.get(peerId); | ||
const tags = Array.from(peer.tags.keys()); | ||
if (tags.includes(Tags.PEER_EXCHANGE)) { | ||
pxPeersDiscovered.add(peerId); | ||
if (pxPeersDiscovered.size === 1) { | ||
clearTimeout(timeoutId); | ||
resolve(); | ||
} | ||
} | ||
})(); | ||
}); | ||
}); | ||
|
||
expect( | ||
pxPeersDiscovered.size, | ||
"No peer should have been discovered" | ||
).to.equal(0); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.