mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-06-24 06:21:32 +00:00
fix: clean up peer discovery flow (#494)
* fix: clean up peer discovery flow * test(fix): let libp2p start after connecting * test(fix): dont auto dial in disco tests
This commit is contained in:
@ -216,7 +216,8 @@ describe('Dialing (direct, WebSockets)', () => {
|
||||
})
|
||||
|
||||
sinon.spy(libp2p.dialer.identifyService, 'identify')
|
||||
sinon.spy(libp2p.peerStore, 'update')
|
||||
sinon.spy(libp2p.peerStore, 'replace')
|
||||
sinon.spy(libp2p.upgrader, 'onConnection')
|
||||
|
||||
const connection = await libp2p.dialer.connectToMultiaddr(remoteAddr)
|
||||
expect(connection).to.exist()
|
||||
@ -225,7 +226,7 @@ describe('Dialing (direct, WebSockets)', () => {
|
||||
expect(libp2p.dialer.identifyService.identify.callCount).to.equal(1)
|
||||
await libp2p.dialer.identifyService.identify.firstCall.returnValue
|
||||
|
||||
expect(libp2p.peerStore.update.callCount).to.equal(1)
|
||||
expect(libp2p.peerStore.replace.callCount).to.equal(1)
|
||||
})
|
||||
|
||||
it('should be able to use hangup to close connections', async () => {
|
||||
|
@ -47,7 +47,7 @@ describe('Identify', () => {
|
||||
protocols,
|
||||
registrar: {
|
||||
peerStore: {
|
||||
update: () => {}
|
||||
replace: () => {}
|
||||
}
|
||||
}
|
||||
})
|
||||
@ -57,17 +57,17 @@ describe('Identify', () => {
|
||||
})
|
||||
|
||||
const observedAddr = multiaddr('/ip4/127.0.0.1/tcp/1234')
|
||||
const localConnectionMock = { newStream: () => {} }
|
||||
const localConnectionMock = { newStream: () => {}, remotePeer: remotePeer.id }
|
||||
const remoteConnectionMock = { remoteAddr: observedAddr }
|
||||
|
||||
const [local, remote] = duplexPair()
|
||||
sinon.stub(localConnectionMock, 'newStream').returns({ stream: local, protocol: multicodecs.IDENTIFY })
|
||||
|
||||
sinon.spy(localIdentify.registrar.peerStore, 'update')
|
||||
sinon.spy(localIdentify.registrar.peerStore, 'replace')
|
||||
|
||||
// Run identify
|
||||
await Promise.all([
|
||||
localIdentify.identify(localConnectionMock, remotePeer.id),
|
||||
localIdentify.identify(localConnectionMock),
|
||||
remoteIdentify.handleMessage({
|
||||
connection: remoteConnectionMock,
|
||||
stream: remote,
|
||||
@ -75,9 +75,9 @@ describe('Identify', () => {
|
||||
})
|
||||
])
|
||||
|
||||
expect(localIdentify.registrar.peerStore.update.callCount).to.equal(1)
|
||||
expect(localIdentify.registrar.peerStore.replace.callCount).to.equal(1)
|
||||
// Validate the remote peer gets updated in the peer store
|
||||
const call = localIdentify.registrar.peerStore.update.firstCall
|
||||
const call = localIdentify.registrar.peerStore.replace.firstCall
|
||||
expect(call.args[0].id.bytes).to.equal(remotePeer.id.bytes)
|
||||
})
|
||||
|
||||
@ -92,7 +92,7 @@ describe('Identify', () => {
|
||||
})
|
||||
|
||||
const observedAddr = multiaddr('/ip4/127.0.0.1/tcp/1234')
|
||||
const localConnectionMock = { newStream: () => {} }
|
||||
const localConnectionMock = { newStream: () => {}, remotePeer }
|
||||
const remoteConnectionMock = { remoteAddr: observedAddr }
|
||||
|
||||
const [local, remote] = duplexPair()
|
||||
@ -128,7 +128,7 @@ describe('Identify', () => {
|
||||
peerInfo: remotePeer,
|
||||
registrar: {
|
||||
peerStore: {
|
||||
update: () => {}
|
||||
replace: () => {}
|
||||
}
|
||||
}
|
||||
})
|
||||
@ -148,7 +148,7 @@ describe('Identify', () => {
|
||||
|
||||
sinon.spy(IdentifyService, 'updatePeerAddresses')
|
||||
sinon.spy(IdentifyService, 'updatePeerProtocols')
|
||||
sinon.spy(remoteIdentify.registrar.peerStore, 'update')
|
||||
sinon.spy(remoteIdentify.registrar.peerStore, 'replace')
|
||||
|
||||
// Run identify
|
||||
await Promise.all([
|
||||
@ -163,8 +163,8 @@ describe('Identify', () => {
|
||||
expect(IdentifyService.updatePeerAddresses.callCount).to.equal(1)
|
||||
expect(IdentifyService.updatePeerProtocols.callCount).to.equal(1)
|
||||
|
||||
expect(remoteIdentify.registrar.peerStore.update.callCount).to.equal(1)
|
||||
const [peerInfo] = remoteIdentify.registrar.peerStore.update.firstCall.args
|
||||
expect(remoteIdentify.registrar.peerStore.replace.callCount).to.equal(1)
|
||||
const [peerInfo] = remoteIdentify.registrar.peerStore.replace.firstCall.args
|
||||
expect(peerInfo.id.bytes).to.eql(localPeer.id.bytes)
|
||||
expect(peerInfo.multiaddrs.toArray()).to.eql([listeningAddr])
|
||||
expect(peerInfo.protocols).to.eql(localProtocols)
|
||||
@ -198,7 +198,7 @@ describe('Identify', () => {
|
||||
})
|
||||
|
||||
sinon.spy(libp2p.dialer.identifyService, 'identify')
|
||||
sinon.spy(libp2p.peerStore, 'update')
|
||||
sinon.spy(libp2p.peerStore, 'replace')
|
||||
|
||||
const connection = await libp2p.dialer.connectToMultiaddr(remoteAddr)
|
||||
expect(connection).to.exist()
|
||||
@ -207,7 +207,7 @@ describe('Identify', () => {
|
||||
expect(libp2p.dialer.identifyService.identify.callCount).to.equal(1)
|
||||
await libp2p.dialer.identifyService.identify.firstCall.returnValue
|
||||
|
||||
expect(libp2p.peerStore.update.callCount).to.equal(1)
|
||||
expect(libp2p.peerStore.replace.callCount).to.equal(1)
|
||||
await connection.close()
|
||||
})
|
||||
|
||||
|
@ -48,6 +48,7 @@ describe('peer discovery scenarios', () => {
|
||||
},
|
||||
config: {
|
||||
peerDiscovery: {
|
||||
autoDial: false,
|
||||
bootstrap: {
|
||||
enabled: true,
|
||||
list: bootstrappers
|
||||
@ -84,6 +85,7 @@ describe('peer discovery scenarios', () => {
|
||||
},
|
||||
config: {
|
||||
peerDiscovery: {
|
||||
autoDial: false,
|
||||
mdns: {
|
||||
enabled: true,
|
||||
interval: 200, // discover quickly
|
||||
@ -111,9 +113,11 @@ describe('peer discovery scenarios', () => {
|
||||
}
|
||||
})
|
||||
|
||||
await remoteLibp2p1.start()
|
||||
await remoteLibp2p2.start()
|
||||
await libp2p.start()
|
||||
await Promise.all([
|
||||
remoteLibp2p1.start(),
|
||||
remoteLibp2p2.start(),
|
||||
libp2p.start()
|
||||
])
|
||||
|
||||
await deferred.promise
|
||||
|
||||
@ -130,11 +134,14 @@ describe('peer discovery scenarios', () => {
|
||||
dht: KadDht
|
||||
},
|
||||
config: {
|
||||
peerDiscovery: {
|
||||
autoDial: false
|
||||
},
|
||||
dht: {
|
||||
randomWalk: {
|
||||
enabled: true,
|
||||
delay: 100,
|
||||
interval: 200, // start the query sooner
|
||||
delay: 100, // start the first query quickly
|
||||
interval: 1000,
|
||||
timeout: 3000
|
||||
},
|
||||
enabled: true
|
||||
@ -153,9 +160,10 @@ describe('peer discovery scenarios', () => {
|
||||
}
|
||||
})
|
||||
|
||||
await remoteLibp2p1.start()
|
||||
await remoteLibp2p2.start()
|
||||
await libp2p.start()
|
||||
await Promise.all([
|
||||
remoteLibp2p1.start(),
|
||||
remoteLibp2p2.start()
|
||||
])
|
||||
|
||||
// Topology:
|
||||
// A -> B
|
||||
@ -165,8 +173,12 @@ describe('peer discovery scenarios', () => {
|
||||
remoteLibp2p2.dial(remotePeerInfo1)
|
||||
])
|
||||
|
||||
libp2p.start()
|
||||
|
||||
await deferred.promise
|
||||
await remoteLibp2p1.stop()
|
||||
await remoteLibp2p2.stop()
|
||||
return Promise.all([
|
||||
remoteLibp2p1.stop(),
|
||||
remoteLibp2p2.stop()
|
||||
])
|
||||
})
|
||||
})
|
||||
|
@ -56,7 +56,6 @@ describe('peer-store', () => {
|
||||
// Put the peer in the store
|
||||
peerStore.put(peerInfo)
|
||||
|
||||
sinon.spy(peerStore, 'put')
|
||||
sinon.spy(peerStore, 'add')
|
||||
sinon.spy(peerStore, 'update')
|
||||
|
||||
@ -75,7 +74,6 @@ describe('peer-store', () => {
|
||||
|
||||
peerStore.put(peerInfo)
|
||||
|
||||
expect(peerStore.put.callCount).to.equal(1)
|
||||
expect(peerStore.add.callCount).to.equal(0)
|
||||
expect(peerStore.update.callCount).to.equal(1)
|
||||
})
|
||||
|
@ -166,7 +166,7 @@ describe('registrar', () => {
|
||||
|
||||
// Remove protocol to peer and update it
|
||||
peerInfo.protocols.delete(multicodec)
|
||||
peerStore.put(peerInfo)
|
||||
peerStore.replace(peerInfo)
|
||||
|
||||
await onDisconnectDefer.promise
|
||||
})
|
||||
|
Reference in New Issue
Block a user