Skip to content

Commit 16f2bc3

Browse files
authored
fix: ensure dials always use the latest PeerInfo from the PeerBook (libp2p#312)
* fix: ensure dials always use the latest PeerInfo from the PeerBook This fixes an issue where if dial is called with a new instance of PeerInfo, if it is the first dial to that peer, the queue was forever associated with that instance. This is currently the case when Circuit checks the HOP status of a potential relay. This ensures that whenever we dial, we are updating the peer book and using the latest PeerInfo in that dial request. * test: add test for get peer info * refactor: just use id with dialer queue
1 parent 45a86af commit 16f2bc3

File tree

4 files changed

+38
-8
lines changed

4 files changed

+38
-8
lines changed

src/dialer/queue.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,11 @@ function createConnectionWithProtocol ({ protocol, connection, callback }) {
6161
class Queue {
6262
/**
6363
* @constructor
64-
* @param {PeerInfo} peerInfo
64+
* @param {string} peerId
6565
* @param {Switch} _switch
6666
*/
67-
constructor (peerInfo, _switch) {
68-
this.peerInfo = peerInfo
69-
this.id = peerInfo.id.toB58String()
67+
constructor (peerId, _switch) {
68+
this.id = peerId
7069
this.switch = _switch
7170
this._queue = []
7271
this.isRunning = false
@@ -168,8 +167,9 @@ class Queue {
168167
this._run()
169168
})
170169

170+
const peerInfo = this.switch._peerBook.get(this.id)
171171
let queuedDial = this._queue.shift()
172-
let { connectionFSM, didCreate } = this._getOrCreateConnection(this.peerInfo)
172+
let { connectionFSM, didCreate } = this._getOrCreateConnection(peerInfo)
173173

174174
// If the dial expects a ConnectionFSM, we can provide that back now
175175
if (queuedDial.useFSM) {

src/dialer/queueManager.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class DialQueueManager {
4848
getQueue (peerInfo) {
4949
const id = peerInfo.id.toB58String()
5050

51-
this._queue[id] = this._queue[id] || new Queue(peerInfo, this.switch)
51+
this._queue[id] = this._queue[id] || new Queue(id, this.switch)
5252
return this._queue[id]
5353
}
5454
}

src/get-peer-info.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ const multiaddr = require('multiaddr')
1515
function getPeerInfo (peer, peerBook) {
1616
let peerInfo
1717

18-
// Already a PeerInfo instance
18+
// Already a PeerInfo instance,
19+
// add to the peer book and return the latest value
1920
if (PeerInfo.isPeerInfo(peer)) {
20-
return peer
21+
return peerBook.put(peer)
2122
}
2223

2324
// Attempt to convert from Multiaddr instance (not string)

test/get-peer-info.spec.js

+29
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,35 @@ describe('Get peer info', () => {
6363
})
6464
})
6565

66+
it('should add a peerInfo to the book', (done) => {
67+
PeerId.createFromJSON(TestPeerInfos[1].id, (err, id) => {
68+
const peerInfo = new PeerInfo(id)
69+
expect(peerBook.has(peerInfo.id.toB58String())).to.eql(false)
70+
71+
expect(getPeerInfo(peerInfo, peerBook)).to.exist()
72+
expect(peerBook.has(peerInfo.id.toB58String())).to.eql(true)
73+
done(err)
74+
})
75+
})
76+
77+
it('should return the most up to date version of the peer', (done) => {
78+
const ma1 = MultiAddr('/ip4/0.0.0.0/tcp/8080')
79+
const ma2 = MultiAddr('/ip6/::/tcp/8080')
80+
PeerId.createFromJSON(TestPeerInfos[1].id, (err, id) => {
81+
const peerInfo = new PeerInfo(id)
82+
peerInfo.multiaddrs.add(ma1)
83+
expect(getPeerInfo(peerInfo, peerBook)).to.exist()
84+
85+
const peerInfo2 = new PeerInfo(id)
86+
peerInfo2.multiaddrs.add(ma2)
87+
const returnedPeerInfo = getPeerInfo(peerInfo2, peerBook)
88+
expect(returnedPeerInfo.multiaddrs.toArray()).to.contain.members([
89+
ma1, ma2
90+
])
91+
done(err)
92+
})
93+
})
94+
6695
it('an invalid peer type should throw an error', () => {
6796
let func = () => {
6897
getPeerInfo('/ip4/127.0.0.1/tcp/1234', peerBook)

0 commit comments

Comments
 (0)