Skip to content

Commit 79c3272

Browse files
committed
chore: use libp2p utils sorter via addressBook getMultiaddrsForPeer
1 parent c242610 commit 79c3272

File tree

10 files changed

+61
-152
lines changed

10 files changed

+61
-152
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
"it-protocol-buffers": "^0.2.0",
6262
"libp2p-crypto": "^0.18.0",
6363
"libp2p-interfaces": "^0.7.2",
64-
"libp2p-utils": "^0.2.1",
64+
"libp2p-utils": "libp2p/js-libp2p-utils#feat/address-sorter",
6565
"mafmt": "^8.0.0",
6666
"merge-options": "^2.0.0",
6767
"moving-average": "^1.0.0",

src/circuit/auto-relay.js

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -129,27 +129,18 @@ class AutoRelay {
129129
}
130130

131131
// Get peer known addresses and sort them per public addresses first
132-
// Sorting addresses to public first
133-
const remoteAddrs = this._addressSorter(
134-
this._peerStore.addressBook.get(connection.remotePeer) || []
132+
const remoteAddrs = this._peerStore.addressBook.getMultiaddrsForPeer(
133+
connection.remotePeer, this._addressSorter
135134
)
136135

137136
if (!remoteAddrs || !remoteAddrs.length) {
138137
return
139138
}
140139

141-
const remoteMultiaddr = remoteAddrs[0].multiaddr
142-
143-
let listenAddr
144-
if (!remoteMultiaddr.protoNames().includes('p2p')) {
145-
listenAddr = `${remoteMultiaddr.toString()}/p2p/${connection.remotePeer.toB58String()}/p2p-circuit`
146-
} else {
147-
listenAddr = `${remoteMultiaddr.toString()}/p2p-circuit`
148-
}
149-
150-
// Attempt to listen on relay
140+
const listenAddr = `${remoteAddrs[0].toString()}/p2p-circuit`
151141
this._listenRelays.add(id)
152142

143+
// Attempt to listen on relay
153144
try {
154145
await this._transportManager.listen([multiaddr(listenAddr)])
155146
// Announce multiaddrs will update on listen success by TransportManager event being triggered

src/config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const { dnsaddrResolver } = require('multiaddr/src/resolvers')
66
const Constants = require('./constants')
77
const RelayConstants = require('./circuit/constants')
88

9-
const { sortPublicAddressesFirst } = require('./dialer/utils')
9+
const { publicAddressesFirst } = require('libp2p-utils/src/address-sort')
1010
const { FaultTolerance } = require('./transport-manager')
1111

1212
const DefaultConfig = {
@@ -28,7 +28,7 @@ const DefaultConfig = {
2828
resolvers: {
2929
dnsaddr: dnsaddrResolver
3030
},
31-
addressSorter: sortPublicAddressesFirst
31+
addressSorter: publicAddressesFirst
3232
},
3333
metrics: {
3434
enabled: false

src/dialer/index.js

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const log = debug('libp2p:dialer')
99
log.error = debug('libp2p:dialer:error')
1010

1111
const { DialRequest } = require('./dial-request')
12-
const { sortPublicAddressesFirst } = require('./utils')
12+
const { publicAddressesFirst } = require('libp2p-utils/src/address-sort')
1313
const getPeer = require('../get-peer')
1414

1515
const { codes } = require('../errors')
@@ -25,7 +25,7 @@ class Dialer {
2525
* @param {object} options
2626
* @param {TransportManager} options.transportManager
2727
* @param {Peerstore} options.peerStore
28-
* @param {(addresses: Array<Address) => Array<Address>} [options.addressSorter = sortPublicAddressesFirst] - Sort the known addresses of a peer before trying to dial.
28+
* @param {(addresses: Array<Address) => Array<Address>} [options.addressSorter = publicAddressesFirst] - Sort the known addresses of a peer before trying to dial.
2929
* @param {number} [options.concurrency = MAX_PARALLEL_DIALS] - Number of max concurrent dials.
3030
* @param {number} [options.perPeerLimit = MAX_PER_PEER_DIALS] - Number of max concurrent dials per peer.
3131
* @param {number} [options.timeout = DIAL_TIMEOUT] - How long a dial attempt is allowed to take.
@@ -34,7 +34,7 @@ class Dialer {
3434
constructor ({
3535
transportManager,
3636
peerStore,
37-
addressSorter = sortPublicAddressesFirst,
37+
addressSorter = publicAddressesFirst,
3838
concurrency = MAX_PARALLEL_DIALS,
3939
timeout = DIAL_TIMEOUT,
4040
perPeerLimit = MAX_PER_PEER_DIALS,
@@ -124,16 +124,7 @@ class Dialer {
124124
this.peerStore.addressBook.add(id, multiaddrs)
125125
}
126126

127-
let knownAddrs = this.addressSorter(
128-
this.peerStore.addressBook.get(id) || []
129-
).map((address) => {
130-
const multiaddr = address.multiaddr
131-
132-
const idString = multiaddr.getPeerId()
133-
if (idString && idString === id.toB58String()) return multiaddr
134-
135-
return multiaddr.encapsulate(`/p2p/${id.toB58String()}`)
136-
})
127+
let knownAddrs = this.peerStore.addressBook.getMultiaddrsForPeer(id, this.addressSorter) || []
137128

138129
// If received a multiaddr to dial, it should be the first to use
139130
// But, if we know other multiaddrs for the peer, we should try them too.

src/dialer/utils.js

Lines changed: 0 additions & 45 deletions
This file was deleted.

src/peer-store/address-book.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -319,20 +319,22 @@ class AddressBook extends Book {
319319
* Returns `undefined` if there are no known multiaddrs for the given peer.
320320
*
321321
* @param {PeerId} peerId
322+
* @param {(addresses: Array<Address) => Array<Address>} [addressSorter]
322323
* @returns {Array<Multiaddr>|undefined}
323324
*/
324-
getMultiaddrsForPeer (peerId) {
325+
getMultiaddrsForPeer (peerId, addressSorter = (ms) => ms) {
325326
if (!PeerId.isPeerId(peerId)) {
326327
throw errcode(new Error('peerId must be an instance of peer-id'), ERR_INVALID_PARAMETERS)
327328
}
328329

329330
const entry = this.data.get(peerId.toB58String())
330-
331331
if (!entry || !entry.addresses) {
332332
return undefined
333333
}
334334

335-
return entry.addresses.map((address) => {
335+
return addressSorter(
336+
entry.addresses || []
337+
).map((address) => {
336338
const multiaddr = address.multiaddr
337339

338340
const idString = multiaddr.getPeerId()

test/dialing/direct.node.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ describe('Dialing (direct, TCP)', () => {
132132
peerStore: {
133133
addressBook: {
134134
add: () => {},
135-
get: () => [{ multiaddr: unsupportedAddr }]
135+
getMultiaddrsForPeer: () => [unsupportedAddr]
136136
}
137137
}
138138
})
@@ -175,7 +175,7 @@ describe('Dialing (direct, TCP)', () => {
175175
peerStore: {
176176
addressBook: {
177177
add: () => {},
178-
get: () => addrs.map(a => ({ multiaddr: multiaddr(a) }))
178+
getMultiaddrsForPeer: () => addrs
179179
}
180180
}
181181
})

test/dialing/direct.spec.js

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const { AbortError } = require('libp2p-interfaces/src/transport/errors')
1616
const { codes: ErrorCodes } = require('../../src/errors')
1717
const Constants = require('../../src/constants')
1818
const Dialer = require('../../src/dialer')
19-
const dialerUtils = require('../../src/dialer/utils')
19+
const addressSort = require('libp2p-utils/src/address-sort')
2020
const PeerStore = require('../../src/peer-store')
2121
const TransportManager = require('../../src/transport-manager')
2222
const Libp2p = require('../../src')
@@ -45,6 +45,7 @@ describe('Dialing (direct, WebSockets)', () => {
4545
})
4646

4747
afterEach(() => {
48+
peerStore.delete(peerId)
4849
sinon.restore()
4950
})
5051

@@ -86,7 +87,7 @@ describe('Dialing (direct, WebSockets)', () => {
8687
peerStore: {
8788
addressBook: {
8889
add: () => {},
89-
get: () => [{ multiaddr: remoteAddr.decapsulate('/p2p') }]
90+
getMultiaddrsForPeer: () => [remoteAddr]
9091
}
9192
}
9293
})
@@ -102,7 +103,7 @@ describe('Dialing (direct, WebSockets)', () => {
102103
peerStore: {
103104
addressBook: {
104105
add: () => {},
105-
get: () => [{ multiaddr: remoteAddr.decapsulate('/p2p') }]
106+
getMultiaddrsForPeer: () => [remoteAddr]
106107
}
107108
}
108109
})
@@ -126,7 +127,7 @@ describe('Dialing (direct, WebSockets)', () => {
126127
peerStore: {
127128
addressBook: {
128129
add: () => {},
129-
get: () => [{ multiaddr: remoteAddr.decapsulate('/p2p') }]
130+
getMultiaddrsForPeer: () => [remoteAddr]
130131
}
131132
}
132133
})
@@ -142,7 +143,7 @@ describe('Dialing (direct, WebSockets)', () => {
142143
peerStore: {
143144
addressBook: {
144145
set: () => {},
145-
get: () => [{ multiaddr: unsupportedAddr.decapsulate('/p2p') }]
146+
getMultiaddrsForPeer: () => [unsupportedAddr]
146147
}
147148
}
148149
})
@@ -159,7 +160,7 @@ describe('Dialing (direct, WebSockets)', () => {
159160
peerStore: {
160161
addressBook: {
161162
add: () => {},
162-
get: () => [{ multiaddr: remoteAddr.decapsulate('/p2p') }]
163+
getMultiaddrsForPeer: () => [remoteAddr]
163164
}
164165
}
165166
})
@@ -178,25 +179,34 @@ describe('Dialing (direct, WebSockets)', () => {
178179
})
179180

180181
it('should sort addresses on dial', async () => {
181-
sinon.spy(dialerUtils, 'sortPublicAddressesFirst')
182+
const peerMultiaddrs = [
183+
multiaddr('/ip4/127.0.0.1/tcp/15001/ws'),
184+
multiaddr('/ip4/20.0.0.1/tcp/15001/ws'),
185+
multiaddr('/ip4/30.0.0.1/tcp/15001/ws')
186+
]
187+
188+
sinon.spy(addressSort, 'publicAddressesFirst')
189+
sinon.stub(localTM, 'dial').callsFake(createMockConnection)
190+
182191
const dialer = new Dialer({
183192
transportManager: localTM,
184-
addressSorter: dialerUtils.sortPublicAddressesFirst,
185-
concurrency: 2,
186-
peerStore: {
187-
addressBook: {
188-
set: () => { },
189-
get: () => [{ multiaddr: remoteAddr }, { multiaddr: remoteAddr }, { multiaddr: remoteAddr }]
190-
}
191-
}
193+
addressSorter: addressSort.publicAddressesFirst,
194+
concurrency: 3,
195+
peerStore
192196
})
193197

194-
sinon.stub(localTM, 'dial').callsFake(createMockConnection)
198+
// Inject data in the AddressBook
199+
peerStore.addressBook.add(peerId, peerMultiaddrs)
195200

196201
// Perform 3 multiaddr dials
197202
await dialer.connectToPeer(peerId)
198203

199-
expect(dialerUtils.sortPublicAddressesFirst.callCount).to.eql(1)
204+
expect(addressSort.publicAddressesFirst.callCount).to.eql(1)
205+
206+
const sortedAddresses = addressSort.publicAddressesFirst(peerMultiaddrs.map((m) => ({ multiaddr: m })))
207+
expect(localTM.dial.getCall(0).args[0].equals(sortedAddresses[0].multiaddr))
208+
expect(localTM.dial.getCall(1).args[0].equals(sortedAddresses[1].multiaddr))
209+
expect(localTM.dial.getCall(2).args[0].equals(sortedAddresses[2].multiaddr))
200210
})
201211

202212
it('should dial to the max concurrency', async () => {
@@ -206,7 +216,7 @@ describe('Dialing (direct, WebSockets)', () => {
206216
peerStore: {
207217
addressBook: {
208218
set: () => {},
209-
get: () => [{ multiaddr: remoteAddr }, { multiaddr: remoteAddr }, { multiaddr: remoteAddr }]
219+
getMultiaddrsForPeer: () => [remoteAddr, remoteAddr, remoteAddr]
210220
}
211221
}
212222
})
@@ -244,7 +254,7 @@ describe('Dialing (direct, WebSockets)', () => {
244254
peerStore: {
245255
addressBook: {
246256
set: () => {},
247-
get: () => [{ multiaddr: remoteAddr }, { multiaddr: remoteAddr }, { multiaddr: remoteAddr }]
257+
getMultiaddrsForPeer: () => [remoteAddr, remoteAddr, remoteAddr]
248258
}
249259
}
250260
})

test/dialing/utils.spec.js

Lines changed: 0 additions & 53 deletions
This file was deleted.

0 commit comments

Comments
 (0)