-
Notifications
You must be signed in to change notification settings - Fork 446
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: discovery modules * chore: address review
- Loading branch information
1 parent
acbbc0f
commit 997ee16
Showing
4 changed files
with
206 additions
and
4 deletions.
There are no files selected for viewing
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,172 @@ | ||
'use strict' | ||
/* eslint-env mocha */ | ||
|
||
const chai = require('chai') | ||
chai.use(require('dirty-chai')) | ||
|
||
const defer = require('p-defer') | ||
const mergeOptions = require('merge-options') | ||
|
||
const Bootstrap = require('libp2p-bootstrap') | ||
const crypto = require('libp2p-crypto') | ||
const KadDht = require('libp2p-kad-dht') | ||
const MulticastDNS = require('libp2p-mdns') | ||
const multiaddr = require('multiaddr') | ||
|
||
const Libp2p = require('../../src') | ||
const baseOptions = require('../utils/base-options') | ||
const { createPeerInfoFromFixture } = require('../utils/creators/peer') | ||
|
||
describe('peer discovery scenarios', () => { | ||
let peerInfo, remotePeerInfo1, remotePeerInfo2 | ||
let libp2p | ||
|
||
before(async () => { | ||
[peerInfo, remotePeerInfo1, remotePeerInfo2] = await createPeerInfoFromFixture(3) | ||
|
||
peerInfo.multiaddrs.add(multiaddr('/ip4/127.0.0.1/tcp/0')) | ||
remotePeerInfo1.multiaddrs.add(multiaddr('/ip4/127.0.0.1/tcp/0')) | ||
remotePeerInfo2.multiaddrs.add(multiaddr('/ip4/127.0.0.1/tcp/0')) | ||
}) | ||
|
||
afterEach(async () => { | ||
libp2p && await libp2p.stop() | ||
}) | ||
|
||
it('bootstrap should discover all peers in the list', async () => { | ||
const deferred = defer() | ||
|
||
const bootstrappers = [ | ||
...remotePeerInfo1.multiaddrs.toArray().map((ma) => `${ma}/p2p/${remotePeerInfo1.id.toB58String()}`), | ||
...remotePeerInfo2.multiaddrs.toArray().map((ma) => `${ma}/p2p/${remotePeerInfo2.id.toB58String()}`) | ||
] | ||
|
||
libp2p = new Libp2p(mergeOptions(baseOptions, { | ||
peerInfo, | ||
modules: { | ||
peerDiscovery: [Bootstrap] | ||
}, | ||
config: { | ||
peerDiscovery: { | ||
bootstrap: { | ||
enabled: true, | ||
list: bootstrappers | ||
} | ||
} | ||
} | ||
})) | ||
|
||
const expectedPeers = new Set([ | ||
remotePeerInfo1.id.toB58String(), | ||
remotePeerInfo2.id.toB58String() | ||
]) | ||
|
||
libp2p.on('peer:discovery', (peerInfo) => { | ||
expectedPeers.delete(peerInfo.id.toB58String()) | ||
if (expectedPeers.size === 0) { | ||
libp2p.removeAllListeners('peer:discovery') | ||
deferred.resolve() | ||
} | ||
}) | ||
|
||
await libp2p.start() | ||
|
||
return deferred.promise | ||
}) | ||
|
||
it('MulticastDNS should discover all peers on the local network', async () => { | ||
const deferred = defer() | ||
|
||
const getConfig = (peerInfo) => mergeOptions(baseOptions, { | ||
peerInfo, | ||
modules: { | ||
peerDiscovery: [MulticastDNS] | ||
}, | ||
config: { | ||
peerDiscovery: { | ||
mdns: { | ||
enabled: true, | ||
interval: 200, // discover quickly | ||
// use a random tag to prevent CI collision | ||
serviceTag: crypto.randomBytes(10).toString('hex') | ||
} | ||
} | ||
} | ||
}) | ||
|
||
libp2p = new Libp2p(getConfig(peerInfo)) | ||
const remoteLibp2p1 = new Libp2p(getConfig(remotePeerInfo1)) | ||
const remoteLibp2p2 = new Libp2p(getConfig(remotePeerInfo2)) | ||
|
||
const expectedPeers = new Set([ | ||
remotePeerInfo1.id.toB58String(), | ||
remotePeerInfo2.id.toB58String() | ||
]) | ||
|
||
libp2p.on('peer:discovery', (peerInfo) => { | ||
expectedPeers.delete(peerInfo.id.toB58String()) | ||
if (expectedPeers.size === 0) { | ||
libp2p.removeAllListeners('peer:discovery') | ||
deferred.resolve() | ||
} | ||
}) | ||
|
||
await remoteLibp2p1.start() | ||
await remoteLibp2p2.start() | ||
await libp2p.start() | ||
|
||
await deferred.promise | ||
|
||
await remoteLibp2p1.stop() | ||
await remoteLibp2p2.stop() | ||
}) | ||
|
||
it('kad-dht should discover other peers', async () => { | ||
const deferred = defer() | ||
|
||
const getConfig = (peerInfo) => mergeOptions(baseOptions, { | ||
peerInfo, | ||
modules: { | ||
dht: KadDht | ||
}, | ||
config: { | ||
dht: { | ||
randomWalk: { | ||
enabled: true, | ||
delay: 100, | ||
interval: 200, // start the query sooner | ||
timeout: 3000 | ||
}, | ||
enabled: true | ||
} | ||
} | ||
}) | ||
|
||
libp2p = new Libp2p(getConfig(peerInfo)) | ||
const remoteLibp2p1 = new Libp2p(getConfig(remotePeerInfo1)) | ||
const remoteLibp2p2 = new Libp2p(getConfig(remotePeerInfo2)) | ||
|
||
libp2p.on('peer:discovery', (peerInfo) => { | ||
if (peerInfo.id.toB58String() === remotePeerInfo2.id.toB58String()) { | ||
libp2p.removeAllListeners('peer:discovery') | ||
deferred.resolve() | ||
} | ||
}) | ||
|
||
await remoteLibp2p1.start() | ||
await remoteLibp2p2.start() | ||
await libp2p.start() | ||
|
||
// Topology: | ||
// A -> B | ||
// C -> B | ||
await Promise.all([ | ||
libp2p.dial(remotePeerInfo1), | ||
remoteLibp2p2.dial(remotePeerInfo1) | ||
]) | ||
|
||
await deferred.promise | ||
await remoteLibp2p1.stop() | ||
await remoteLibp2p2.stop() | ||
}) | ||
}) |
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