Skip to content
This repository was archived by the owner on Jul 21, 2023. It is now read-only.

Commit fca175e

Browse files
vasco-santosjacobheun
andauthoredApr 23, 2020
chore: peer-discovery not using peer-info (#90)
* chore: peer-discovery not using peer-info BREAKING CHANGE: peer event emitted with id and multiaddrs properties instead of peer-info * chore: add tests for peer-discovery interface * chore: apply suggestions from code review Co-Authored-By: Jacob Heun <jacobheun@gmail.com> * chore: update readme with peerData and peerId Co-authored-by: Jacob Heun <jacobheun@gmail.com>
1 parent ecdda6e commit fca175e

12 files changed

+270
-205
lines changed
 

‎README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ const MDNS = require('libp2p-mdns')
2323

2424
const mdns = new MDNS(options)
2525

26-
mdns.on('peer', (peerInfo) => {
27-
console.log('Found a peer in the local network', peerInfo.id.toB58String())
26+
mdns.on('peer', (peerData) => {
27+
console.log('Found a peer in the local network', peerData.id.toB58String(), peerData.multiaddrs)
2828
})
2929

3030
// Broadcast for 20 seconds
@@ -33,7 +33,8 @@ setTimeout(() => mdns.stop(), 20 * 1000)
3333
```
3434

3535
- options
36-
- `peerInfo` - PeerInfo to announce
36+
- `peerId` - PeerId to announce
37+
- `multiaddrs` - multiaddrs to announce
3738
- `broadcast` - (true/false) announce our presence through mDNS, default `false`
3839
- `interval` - query interval, default 10 * 1000 (10 seconds)
3940
- `serviceTag` - name of the service announce , default 'ipfs.local`

‎package.json

+2-3
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,14 @@
3838
"chai": "^4.2.0",
3939
"delay": "^4.3.0",
4040
"dirty-chai": "^2.0.1",
41-
"interface-discovery": "^0.1.1",
41+
"libp2p-interfaces": "^0.3.0",
4242
"p-defer": "^3.0.0"
4343
},
4444
"dependencies": {
4545
"debug": "^4.1.1",
4646
"multiaddr": "^7.1.0",
4747
"multicast-dns": "^7.2.0",
48-
"peer-id": "~0.13.3",
49-
"peer-info": "~0.17.0"
48+
"peer-id": "~0.13.3"
5049
},
5150
"contributors": [
5251
"David Dias <daviddias.p@gmail.com>",

‎src/compat/index.js

+16-6
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ const Responder = require('./responder')
77
const Querier = require('./querier')
88

99
class GoMulticastDNS extends EE {
10-
constructor (peerInfo) {
10+
constructor ({ peerId, multiaddrs, queryPeriod, queryInterval }) {
1111
super()
1212
this._started = false
13-
this._peerInfo = peerInfo
13+
this._peerId = peerId
14+
this._multiaddrs = multiaddrs
15+
this._queryPeriod = queryPeriod
16+
this._queryInterval = queryInterval
1417
this._onPeer = this._onPeer.bind(this)
1518
}
1619

@@ -20,8 +23,15 @@ class GoMulticastDNS extends EE {
2023
}
2124

2225
this._started = true
23-
this._responder = new Responder(this._peerInfo)
24-
this._querier = new Querier(this._peerInfo.id)
26+
this._responder = new Responder({
27+
peerId: this._peerId,
28+
multiaddrs: this._multiaddrs
29+
})
30+
this._querier = new Querier({
31+
peerId: this._peerId,
32+
queryInterval: this._queryInterval,
33+
queryPeriod: this._queryPeriod
34+
})
2535

2636
this._querier.on('peer', this._onPeer)
2737

@@ -31,8 +41,8 @@ class GoMulticastDNS extends EE {
3141
])
3242
}
3343

34-
_onPeer (peerInfo) {
35-
this.emit('peer', peerInfo)
44+
_onPeer (peerData) {
45+
this.emit('peer', peerData)
3646
}
3747

3848
stop () {

‎src/compat/querier.js

+18-22
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,31 @@
33
const EE = require('events')
44
const MDNS = require('multicast-dns')
55
const Multiaddr = require('multiaddr')
6-
const PeerInfo = require('peer-info')
76
const PeerId = require('peer-id')
87
const debug = require('debug')
98
const log = debug('libp2p:mdns:compat:querier')
109
log.error = debug('libp2p:mdns:compat:querier:error')
1110
const { SERVICE_TAG_LOCAL, MULTICAST_IP, MULTICAST_PORT } = require('./constants')
1211

1312
class Querier extends EE {
14-
constructor (peerId, options) {
13+
constructor ({ peerId, queryInterval = 60000, queryPeriod }) {
1514
super()
15+
1616
if (!peerId) {
1717
throw new Error('missing peerId parameter')
1818
}
19-
options = options || {}
19+
2020
this._peerIdStr = peerId.toB58String()
21-
// Re-query every 60s, in leu of network change detection
22-
options.queryInterval = options.queryInterval || 60000
23-
// Time for which the MDNS server will stay alive waiting for responses
24-
// Must be less than options.queryInterval!
25-
options.queryPeriod = Math.min(
26-
options.queryInterval,
27-
options.queryPeriod == null ? 5000 : options.queryPeriod
28-
)
29-
this._options = options
21+
this._options = {
22+
// Re-query in leu of network change detection (every 60s by default)
23+
queryInterval: queryInterval,
24+
// Time for which the MDNS server will stay alive waiting for responses
25+
// Must be less than options.queryInterval!
26+
queryPeriod: Math.min(
27+
queryInterval,
28+
queryPeriod == null ? 5000 : queryPeriod
29+
)
30+
}
3031
this._onResponse = this._onResponse.bind(this)
3132
}
3233

@@ -57,7 +58,7 @@ class Querier extends EE {
5758
})
5859
}
5960

60-
async _onResponse (event, info) {
61+
_onResponse (event, info) {
6162
const answers = event.answers || []
6263
const ptrRecord = answers.find(a => a.type === 'PTR' && a.name === SERVICE_TAG_LOCAL)
6364

@@ -87,13 +88,6 @@ class Querier extends EE {
8788
return log('failed to create peer ID from TXT record data', peerIdStr, err)
8889
}
8990

90-
let peerInfo
91-
try {
92-
peerInfo = await PeerInfo.create(peerId)
93-
} catch (err) {
94-
return log.error('failed to create peer info from peer ID', peerId, err)
95-
}
96-
9791
const srvRecord = answers.find(a => a.type === 'SRV')
9892
if (!srvRecord) return log('missing SRV record in response')
9993

@@ -115,8 +109,10 @@ class Querier extends EE {
115109
return addrs
116110
}, [])
117111

118-
multiaddrs.forEach(addr => peerInfo.multiaddrs.add(addr))
119-
this.emit('peer', peerInfo)
112+
this.emit('peer', {
113+
id: peerId,
114+
multiaddrs
115+
})
120116
}
121117

122118
stop () {

‎src/compat/responder.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ const log = require('debug')('libp2p:mdns:compat:responder')
66
const { SERVICE_TAG_LOCAL } = require('./constants')
77

88
class Responder {
9-
constructor (peerInfo) {
10-
if (!peerInfo) {
11-
throw new Error('missing peerInfo parameter')
9+
constructor ({ peerId, multiaddrs }) {
10+
if (!peerId) {
11+
throw new Error('missing peerId parameter')
1212
}
1313

14-
this._peerInfo = peerInfo
15-
this._peerIdStr = peerInfo.id.toB58String()
14+
this._peerId = peerId
15+
this._peerIdStr = peerId.toB58String()
16+
this._multiaddrs = multiaddrs
1617
this._onQuery = this._onQuery.bind(this)
1718
}
1819

@@ -22,7 +23,7 @@ class Responder {
2223
}
2324

2425
_onQuery (event, info) {
25-
const addresses = this._peerInfo.multiaddrs.toArray().map(ma => ma.toOptions())
26+
const addresses = this._multiaddrs.map(ma => ma.toOptions())
2627
// Only announce TCP for now
2728
if (!addresses.length) return
2829

‎src/index.js

+14-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

33
const multicastDNS = require('multicast-dns')
4-
const EventEmitter = require('events').EventEmitter
4+
const { EventEmitter } = require('events')
55
const debug = require('debug')
66
const log = debug('libp2p:mdns')
77
const query = require('./query')
@@ -10,20 +10,24 @@ const GoMulticastDNS = require('./compat')
1010
class MulticastDNS extends EventEmitter {
1111
constructor (options = {}) {
1212
super()
13-
if (!options.peerInfo) {
14-
throw new Error('needs a PeerInfo to work')
13+
14+
if (!options.peerId) {
15+
throw new Error('needs own PeerId to work')
1516
}
1617

1718
this.broadcast = options.broadcast !== false
1819
this.interval = options.interval || (1e3 * 10)
1920
this.serviceTag = options.serviceTag || 'ipfs.local'
2021
this.port = options.port || 5353
21-
this.peerInfo = options.peerInfo
22+
this.peerId = options.peerId
23+
this.peerMultiaddrs = options.multiaddrs || []
2224
this._queryInterval = null
2325
this._onPeer = this._onPeer.bind(this)
2426

2527
if (options.compat !== false) {
26-
this._goMdns = new GoMulticastDNS(options.peerInfo, {
28+
this._goMdns = new GoMulticastDNS({
29+
multiaddrs: this.peerMultiaddrs,
30+
peerId: options.peerId,
2731
queryPeriod: options.compatQueryPeriod,
2832
queryInterval: options.compatQueryInterval
2933
})
@@ -51,12 +55,12 @@ class MulticastDNS extends EventEmitter {
5155
}
5256

5357
_onMdnsQuery (event) {
54-
query.gotQuery(event, this.mdns, this.peerInfo, this.serviceTag, this.broadcast)
58+
query.gotQuery(event, this.mdns, this.peerId, this.peerMultiaddrs, this.serviceTag, this.broadcast)
5559
}
5660

57-
async _onMdnsResponse (event) {
61+
_onMdnsResponse (event) {
5862
try {
59-
const foundPeer = await query.gotResponse(event, this.peerInfo, this.serviceTag)
63+
const foundPeer = query.gotResponse(event, this.peerId, this.serviceTag)
6064

6165
if (foundPeer) {
6266
this.emit('peer', foundPeer)
@@ -66,8 +70,8 @@ class MulticastDNS extends EventEmitter {
6670
}
6771
}
6872

69-
_onPeer (peerInfo) {
70-
this.emit('peer', peerInfo)
73+
_onPeer (peerData) {
74+
this.emit('peer', peerData)
7175
}
7276

7377
/**

‎src/query.js

+22-19
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict'
22

3-
const Peer = require('peer-info')
43
const os = require('os')
54
const debug = require('debug')
65
const log = debug('libp2p:mdns')
@@ -26,7 +25,7 @@ module.exports = {
2625
return setInterval(query, interval)
2726
},
2827

29-
gotResponse: async function (rsp, peerInfo, serviceTag) {
28+
gotResponse: function (rsp, localPeerId, serviceTag) {
3029
if (!rsp.answers) { return }
3130

3231
const answers = {
@@ -57,33 +56,37 @@ module.exports = {
5756
const multiaddrs = []
5857

5958
answers.a.forEach((a) => {
60-
multiaddrs.push(new Multiaddr('/ip4/' + a.data + '/tcp/' + port))
59+
const ma = new Multiaddr('/ip4/' + a.data + '/tcp/' + port)
60+
61+
if (!multiaddrs.some((m) => m.equals(ma))) {
62+
multiaddrs.push(ma)
63+
}
6164
})
65+
6266
answers.aaaa.forEach((a) => {
63-
multiaddrs.push(new Multiaddr('/ip6/' + a.data + '/tcp/' + port))
67+
const ma = new Multiaddr('/ip6/' + a.data + '/tcp/' + port)
68+
69+
if (!multiaddrs.some((m) => m.equals(ma))) {
70+
multiaddrs.push(ma)
71+
}
6472
})
6573

66-
if (peerInfo.id.toB58String() === b58Id) {
74+
if (localPeerId.toB58String() === b58Id) {
6775
return // replied to myself, ignore
6876
}
6977

7078
log('peer found -', b58Id)
7179

72-
const peerId = Id.createFromB58String(b58Id)
73-
74-
try {
75-
const peerFound = await Peer.create(peerId)
76-
multiaddrs.forEach((addr) => peerFound.multiaddrs.add(addr))
77-
return peerFound
78-
} catch (err) {
79-
log.error('Error creating PeerInfo from new found peer', err)
80+
return {
81+
id: Id.createFromB58String(b58Id),
82+
multiaddrs
8083
}
8184
},
8285

83-
gotQuery: function (qry, mdns, peerInfo, serviceTag, broadcast) {
86+
gotQuery: function (qry, mdns, peerId, multiaddrs, serviceTag, broadcast) {
8487
if (!broadcast) { return }
8588

86-
const addresses = peerInfo.multiaddrs.toArray().map(ma => ma.toOptions())
89+
const addresses = multiaddrs.map(ma => ma.toOptions())
8790
// Only announce TCP for now
8891
if (addresses.length === 0) { return }
8992

@@ -95,14 +98,14 @@ module.exports = {
9598
type: 'PTR',
9699
class: 'IN',
97100
ttl: 120,
98-
data: peerInfo.id.toB58String() + '.' + serviceTag
101+
data: peerId.toB58String() + '.' + serviceTag
99102
})
100103

101104
// Only announce TCP multiaddrs for now
102105
const port = addresses[0].port
103106

104107
answers.push({
105-
name: peerInfo.id.toB58String() + '.' + serviceTag,
108+
name: peerId.toB58String() + '.' + serviceTag,
106109
type: 'SRV',
107110
class: 'IN',
108111
ttl: 120,
@@ -115,11 +118,11 @@ module.exports = {
115118
})
116119

117120
answers.push({
118-
name: peerInfo.id.toB58String() + '.' + serviceTag,
121+
name: peerId.toB58String() + '.' + serviceTag,
119122
type: 'TXT',
120123
class: 'IN',
121124
ttl: 120,
122-
data: peerInfo.id.toB58String()
125+
data: peerId.toB58String()
123126
})
124127

125128
addresses.forEach((addr) => {

0 commit comments

Comments
 (0)
This repository has been archived.