Skip to content

Commit 21a63cb

Browse files
committed
chore: remove peer-info usage
BREAKING CHANGE: pubsub internal peer does not have info propery anymore and use the new topology api with peer-id instead of peer-info
1 parent 59d49dc commit 21a63cb

File tree

6 files changed

+79
-78
lines changed

6 files changed

+79
-78
lines changed

package.json

+2-3
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@
4949
"dirty-chai": "^2.0.1",
5050
"it-pair": "^1.0.0",
5151
"multiaddr": "^7.2.1",
52-
"peer-id": "~0.13.3",
53-
"peer-info": "~0.17.0",
5452
"sinon": "^9.0.0"
5553
},
5654
"dependencies": {
@@ -61,7 +59,8 @@
6159
"it-pipe": "^1.0.1",
6260
"it-pushable": "^1.3.2",
6361
"libp2p-crypto": "~0.17.0",
64-
"libp2p-interfaces": "^0.2.3",
62+
"libp2p-interfaces": "libp2p/js-interfaces#v0.3.x",
63+
"peer-id": "~0.13.3",
6564
"protons": "^1.0.1"
6665
},
6766
"contributors": [

src/index.js

+27-29
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const debug = require('debug')
44
const EventEmitter = require('events')
55
const errcode = require('err-code')
66

7-
const PeerInfo = require('peer-info')
7+
const PeerId = require('peer-id')
88
const MulticodecTopology = require('libp2p-interfaces/src/topology/multicodec-topology')
99

1010
const message = require('./message')
@@ -42,7 +42,7 @@ class PubsubBaseProtocol extends EventEmitter {
4242
* @param {Object} props
4343
* @param {String} props.debugName log namespace
4444
* @param {Array<string>|string} props.multicodecs protocol identificers to connect
45-
* @param {PeerInfo} props.peerInfo peer's peerInfo
45+
* @param {PeerId} props.peerId peer's peerId
4646
* @param {Object} props.registrar registrar for libp2p protocols
4747
* @param {function} props.registrar.handle
4848
* @param {function} props.registrar.register
@@ -54,7 +54,7 @@ class PubsubBaseProtocol extends EventEmitter {
5454
constructor ({
5555
debugName,
5656
multicodecs,
57-
peerInfo,
57+
peerId,
5858
registrar,
5959
signMessages = true,
6060
strictSigning = true
@@ -67,8 +67,8 @@ class PubsubBaseProtocol extends EventEmitter {
6767
throw new Error('multicodecs are required')
6868
}
6969

70-
if (!PeerInfo.isPeerInfo(peerInfo)) {
71-
throw new Error('peer info must be an instance of `peer-info`')
70+
if (!PeerId.isPeerId(peerId)) {
71+
throw new Error('peerId must be an instance of `peer-id`')
7272
}
7373

7474
validateRegistrar(registrar)
@@ -79,11 +79,13 @@ class PubsubBaseProtocol extends EventEmitter {
7979
this.log.err = debug(`${debugName}:error`)
8080

8181
this.multicodecs = utils.ensureArray(multicodecs)
82-
this.peerInfo = peerInfo
82+
this.signMessages = peerId
8383
this.registrar = registrar
8484

8585
this.started = false
8686

87+
this.peerId = peerId
88+
8789
/**
8890
* Map of topics to which peers are subscribed to
8991
*
@@ -99,9 +101,7 @@ class PubsubBaseProtocol extends EventEmitter {
99101
this.peers = new Map()
100102

101103
// Message signing
102-
if (signMessages) {
103-
this.peerId = this.peerInfo.id
104-
}
104+
this.signMessages = signMessages
105105

106106
/**
107107
* If message signing should be required for incoming messages
@@ -170,13 +170,11 @@ class PubsubBaseProtocol extends EventEmitter {
170170
* @param {DuplexStream} props.strean
171171
* @param {Connection} props.connection connection
172172
*/
173-
async _onIncomingStream ({ protocol, stream, connection }) {
174-
const peerInfo = await PeerInfo.create(connection.remotePeer)
175-
peerInfo.protocols.add(protocol)
176-
177-
const idB58Str = peerInfo.id.toB58String()
173+
_onIncomingStream ({ protocol, stream, connection }) {
174+
const peerId = connection.remotePeer
175+
const idB58Str = peerId.toB58String()
178176

179-
const peer = this._addPeer(new Peer(peerInfo))
177+
const peer = this._addPeer(new Peer(peerId, [protocol]))
180178

181179
peer.attachConnection(stream)
182180
this._processMessages(idB58Str, stream, peer)
@@ -185,14 +183,14 @@ class PubsubBaseProtocol extends EventEmitter {
185183
/**
186184
* Registrar notifies a connection successfully with pubsub protocol.
187185
* @private
188-
* @param {PeerInfo} peerInfo remote peer info
186+
* @param {PeerId} peerId remote peer-id
189187
* @param {Connection} conn connection to the peer
190188
*/
191-
async _onPeerConnected (peerInfo, conn) {
192-
const idB58Str = peerInfo.id.toB58String()
189+
async _onPeerConnected (peerId, conn) {
190+
const idB58Str = peerId.toB58String()
193191
this.log('connected', idB58Str)
194192

195-
const peer = this._addPeer(new Peer(peerInfo))
193+
const peer = this._addPeer(new Peer(peerId, this.multicodecs))
196194
try {
197195
const { stream } = await conn.newStream(this.multicodecs)
198196
peer.attachConnection(stream)
@@ -205,11 +203,11 @@ class PubsubBaseProtocol extends EventEmitter {
205203
/**
206204
* Registrar notifies a closing connection with pubsub protocol.
207205
* @private
208-
* @param {PeerInfo} peerInfo peer info
206+
* @param {PeerId} peerId peerId
209207
* @param {Error} err error for connection end
210208
*/
211-
_onPeerDisconnected (peerInfo, err) {
212-
const idB58Str = peerInfo.id.toB58String()
209+
_onPeerDisconnected (peerId, err) {
210+
const idB58Str = peerId.toB58String()
213211
const peer = this.peers.get(idB58Str)
214212

215213
this.log('connection ended', idB58Str, err ? err.message : '')
@@ -219,11 +217,11 @@ class PubsubBaseProtocol extends EventEmitter {
219217
/**
220218
* Add a new connected peer to the peers map.
221219
* @private
222-
* @param {PeerInfo} peer peer info
223-
* @returns {PeerInfo}
220+
* @param {Peer} peer internal peer
221+
* @returns {Peer}
224222
*/
225223
_addPeer (peer) {
226-
const id = peer.info.id.toB58String()
224+
const id = peer.id.toB58String()
227225
let existing = this.peers.get(id)
228226

229227
if (!existing) {
@@ -242,11 +240,11 @@ class PubsubBaseProtocol extends EventEmitter {
242240
* Remove a peer from the peers map.
243241
* @private
244242
* @param {Peer} peer peer state
245-
* @returns {PeerInfo}
243+
* @returns {Peer}
246244
*/
247245
_removePeer (peer) {
248246
if (!peer) return
249-
const id = peer.info.id.toB58String()
247+
const id = peer.id.toB58String()
250248

251249
this.log('remove', id, peer._references)
252250

@@ -287,7 +285,7 @@ class PubsubBaseProtocol extends EventEmitter {
287285
*/
288286
_buildMessage (message) {
289287
const msg = utils.normalizeOutRpcMessage(message)
290-
if (this.peerId) {
288+
if (this.signMessages) {
291289
return signMessage(this.peerId, msg)
292290
} else {
293291
return message
@@ -310,7 +308,7 @@ class PubsubBaseProtocol extends EventEmitter {
310308

311309
return Array.from(this.peers.values())
312310
.filter((peer) => peer.topics.has(topic))
313-
.map((peer) => peer.info.id.toB58String())
311+
.map((peer) => peer.id.toB58String())
314312
}
315313

316314
/**

src/peer.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,20 @@ const { RPC } = require('./message')
1313
*/
1414
class Peer extends EventEmitter {
1515
/**
16-
* @param {PeerInfo} info
16+
* @param {PeerId} id
17+
* @param {Array<string>} protocols
1718
*/
18-
constructor (info) {
19+
constructor (id, protocols) {
1920
super()
2021

2122
/**
22-
* @type {PeerInfo}
23+
* @type {PeerId}
2324
*/
24-
this.info = info
25+
this.id = id
26+
/**
27+
* @type {string}
28+
*/
29+
this.protocols = protocols
2530
/**
2631
* @type {Connection}
2732
*/
@@ -65,7 +70,7 @@ class Peer extends EventEmitter {
6570
*/
6671
write (msg) {
6772
if (!this.isWritable) {
68-
const id = this.info.id.toB58String()
73+
const id = this.id.toB58String()
6974
throw new Error('No writable connection to ' + id)
7075
}
7176

test/instance.spec.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ chai.use(require('chai-spies'))
77
const expect = chai.expect
88

99
const PubsubBaseProtocol = require('../src')
10-
const { createPeerInfo, mockRegistrar } = require('./utils')
10+
const { createPeerId, mockRegistrar } = require('./utils')
1111

1212
describe('should validate instance parameters', () => {
13-
let peerInfo
13+
let peerId
1414

1515
before(async () => {
16-
peerInfo = await createPeerInfo()
16+
peerId = await createPeerId()
1717
})
1818

1919
it('should throw if no debugName is provided', () => {
@@ -30,7 +30,7 @@ describe('should validate instance parameters', () => {
3030
}).to.throw()
3131
})
3232

33-
it('should throw if no peerInfo is provided', () => {
33+
it('should throw if no peerId is provided', () => {
3434
expect(() => {
3535
new PubsubBaseProtocol({ // eslint-disable-line no-new
3636
debugName: 'pubsub',
@@ -39,12 +39,12 @@ describe('should validate instance parameters', () => {
3939
}).to.throw()
4040
})
4141

42-
it('should throw if an invalid peerInfo is provided', () => {
42+
it('should throw if an invalid peerId is provided', () => {
4343
expect(() => {
4444
new PubsubBaseProtocol({ // eslint-disable-line no-new
4545
debugName: 'pubsub',
4646
multicodecs: '/pubsub/1.0.0',
47-
peerInfo: 'fake-peer-info'
47+
peerId: 'fake-peer-id'
4848
})
4949
}).to.throw()
5050
})
@@ -54,7 +54,7 @@ describe('should validate instance parameters', () => {
5454
new PubsubBaseProtocol({ // eslint-disable-line no-new
5555
debugName: 'pubsub',
5656
multicodecs: '/pubsub/1.0.0',
57-
peerInfo: peerInfo
57+
peerId: peerId
5858
})
5959
}).to.throw()
6060
})
@@ -64,7 +64,7 @@ describe('should validate instance parameters', () => {
6464
new PubsubBaseProtocol({ // eslint-disable-line no-new
6565
debugName: 'pubsub',
6666
multicodecs: '/pubsub/1.0.0',
67-
peerInfo: peerInfo,
67+
peerId: peerId,
6868
registrar: mockRegistrar
6969
})
7070
}).not.to.throw()

0 commit comments

Comments
 (0)