Skip to content

Commit 721a53e

Browse files
authored
Merge pull request #41 from libp2p/chore/remove-peer-info-usage
chore: remove peer-info usage
2 parents 59d49dc + 071468f commit 721a53e

File tree

7 files changed

+87
-80
lines changed

7 files changed

+87
-80
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ TODO: add explanation for registrar!
5050
const Pubsub = require('libp2p-pubsub')
5151

5252
class PubsubImplementation extends Pubsub {
53-
constructor({ peerInfo, registrar, ...options })
53+
constructor({ peerId, registrar, ...options })
5454
super({
5555
debugName: 'libp2p:pubsub',
5656
multicodecs: '/pubsub-implementation/1.0.0',
57-
peerInfo: peerInfo,
57+
peerId: peerId,
5858
registrar: registrar,
5959
signMessages: options.signMessages,
6060
strictSigning: options.strictSigning

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": "^0.3.0",
63+
"peer-id": "~0.13.3",
6564
"protons": "^1.0.1"
6665
},
6766
"contributors": [

src/index.js

+33-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,12 @@ class PubsubBaseProtocol extends EventEmitter {
7979
this.log.err = debug(`${debugName}:error`)
8080

8181
this.multicodecs = utils.ensureArray(multicodecs)
82-
this.peerInfo = peerInfo
8382
this.registrar = registrar
8483

8584
this.started = false
8685

86+
this.peerId = peerId
87+
8788
/**
8889
* Map of topics to which peers are subscribed to
8990
*
@@ -99,9 +100,7 @@ class PubsubBaseProtocol extends EventEmitter {
99100
this.peers = new Map()
100101

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

106105
/**
107106
* If message signing should be required for incoming messages
@@ -170,13 +169,14 @@ class PubsubBaseProtocol extends EventEmitter {
170169
* @param {DuplexStream} props.strean
171170
* @param {Connection} props.connection connection
172171
*/
173-
async _onIncomingStream ({ protocol, stream, connection }) {
174-
const peerInfo = await PeerInfo.create(connection.remotePeer)
175-
peerInfo.protocols.add(protocol)
172+
_onIncomingStream ({ protocol, stream, connection }) {
173+
const peerId = connection.remotePeer
174+
const idB58Str = peerId.toB58String()
176175

177-
const idB58Str = peerInfo.id.toB58String()
178-
179-
const peer = this._addPeer(new Peer(peerInfo))
176+
const peer = this._addPeer(new Peer({
177+
id: peerId,
178+
protocols: [protocol]
179+
}))
180180

181181
peer.attachConnection(stream)
182182
this._processMessages(idB58Str, stream, peer)
@@ -185,14 +185,18 @@ class PubsubBaseProtocol extends EventEmitter {
185185
/**
186186
* Registrar notifies a connection successfully with pubsub protocol.
187187
* @private
188-
* @param {PeerInfo} peerInfo remote peer info
188+
* @param {PeerId} peerId remote peer-id
189189
* @param {Connection} conn connection to the peer
190190
*/
191-
async _onPeerConnected (peerInfo, conn) {
192-
const idB58Str = peerInfo.id.toB58String()
191+
async _onPeerConnected (peerId, conn) {
192+
const idB58Str = peerId.toB58String()
193193
this.log('connected', idB58Str)
194194

195-
const peer = this._addPeer(new Peer(peerInfo))
195+
const peer = this._addPeer(new Peer({
196+
id: peerId,
197+
protocols: this.multicodecs
198+
}))
199+
196200
try {
197201
const { stream } = await conn.newStream(this.multicodecs)
198202
peer.attachConnection(stream)
@@ -205,11 +209,11 @@ class PubsubBaseProtocol extends EventEmitter {
205209
/**
206210
* Registrar notifies a closing connection with pubsub protocol.
207211
* @private
208-
* @param {PeerInfo} peerInfo peer info
212+
* @param {PeerId} peerId peerId
209213
* @param {Error} err error for connection end
210214
*/
211-
_onPeerDisconnected (peerInfo, err) {
212-
const idB58Str = peerInfo.id.toB58String()
215+
_onPeerDisconnected (peerId, err) {
216+
const idB58Str = peerId.toB58String()
213217
const peer = this.peers.get(idB58Str)
214218

215219
this.log('connection ended', idB58Str, err ? err.message : '')
@@ -219,11 +223,11 @@ class PubsubBaseProtocol extends EventEmitter {
219223
/**
220224
* Add a new connected peer to the peers map.
221225
* @private
222-
* @param {PeerInfo} peer peer info
223-
* @returns {PeerInfo}
226+
* @param {Peer} peer internal peer
227+
* @returns {Peer}
224228
*/
225229
_addPeer (peer) {
226-
const id = peer.info.id.toB58String()
230+
const id = peer.id.toB58String()
227231
let existing = this.peers.get(id)
228232

229233
if (!existing) {
@@ -242,11 +246,11 @@ class PubsubBaseProtocol extends EventEmitter {
242246
* Remove a peer from the peers map.
243247
* @private
244248
* @param {Peer} peer peer state
245-
* @returns {PeerInfo}
249+
* @returns {Peer}
246250
*/
247251
_removePeer (peer) {
248252
if (!peer) return
249-
const id = peer.info.id.toB58String()
253+
const id = peer.id.toB58String()
250254

251255
this.log('remove', id, peer._references)
252256

@@ -287,7 +291,7 @@ class PubsubBaseProtocol extends EventEmitter {
287291
*/
288292
_buildMessage (message) {
289293
const msg = utils.normalizeOutRpcMessage(message)
290-
if (this.peerId) {
294+
if (this.signMessages) {
291295
return signMessage(this.peerId, msg)
292296
} else {
293297
return message
@@ -310,7 +314,7 @@ class PubsubBaseProtocol extends EventEmitter {
310314

311315
return Array.from(this.peers.values())
312316
.filter((peer) => peer.topics.has(topic))
313-
.map((peer) => peer.info.id.toB58String())
317+
.map((peer) => peer.id.toB58String())
314318
}
315319

316320
/**

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)