Skip to content

Commit

Permalink
chore: remove peer-info usage
Browse files Browse the repository at this point in the history
BREAKING CHANGE: pubsub internal peer does not have info propery anymore and use the new topology api with peer-id instead of peer-info
  • Loading branch information
vasco-santos committed Apr 17, 2020
1 parent 59d49dc commit 21a63cb
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 78 deletions.
5 changes: 2 additions & 3 deletions package.json
Expand Up @@ -49,8 +49,6 @@
"dirty-chai": "^2.0.1",
"it-pair": "^1.0.0",
"multiaddr": "^7.2.1",
"peer-id": "~0.13.3",
"peer-info": "~0.17.0",
"sinon": "^9.0.0"
},
"dependencies": {
Expand All @@ -61,7 +59,8 @@
"it-pipe": "^1.0.1",
"it-pushable": "^1.3.2",
"libp2p-crypto": "~0.17.0",
"libp2p-interfaces": "^0.2.3",
"libp2p-interfaces": "libp2p/js-interfaces#v0.3.x",
"peer-id": "~0.13.3",
"protons": "^1.0.1"
},
"contributors": [
Expand Down
56 changes: 27 additions & 29 deletions src/index.js
Expand Up @@ -4,7 +4,7 @@ const debug = require('debug')
const EventEmitter = require('events')
const errcode = require('err-code')

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

const message = require('./message')
Expand Down Expand Up @@ -42,7 +42,7 @@ class PubsubBaseProtocol extends EventEmitter {
* @param {Object} props
* @param {String} props.debugName log namespace
* @param {Array<string>|string} props.multicodecs protocol identificers to connect
* @param {PeerInfo} props.peerInfo peer's peerInfo
* @param {PeerId} props.peerId peer's peerId
* @param {Object} props.registrar registrar for libp2p protocols
* @param {function} props.registrar.handle
* @param {function} props.registrar.register
Expand All @@ -54,7 +54,7 @@ class PubsubBaseProtocol extends EventEmitter {
constructor ({
debugName,
multicodecs,
peerInfo,
peerId,
registrar,
signMessages = true,
strictSigning = true
Expand All @@ -67,8 +67,8 @@ class PubsubBaseProtocol extends EventEmitter {
throw new Error('multicodecs are required')
}

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

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

this.multicodecs = utils.ensureArray(multicodecs)
this.peerInfo = peerInfo
this.signMessages = peerId
this.registrar = registrar

this.started = false

this.peerId = peerId

/**
* Map of topics to which peers are subscribed to
*
Expand All @@ -99,9 +101,7 @@ class PubsubBaseProtocol extends EventEmitter {
this.peers = new Map()

// Message signing
if (signMessages) {
this.peerId = this.peerInfo.id
}
this.signMessages = signMessages

/**
* If message signing should be required for incoming messages
Expand Down Expand Up @@ -170,13 +170,11 @@ class PubsubBaseProtocol extends EventEmitter {
* @param {DuplexStream} props.strean
* @param {Connection} props.connection connection
*/
async _onIncomingStream ({ protocol, stream, connection }) {
const peerInfo = await PeerInfo.create(connection.remotePeer)
peerInfo.protocols.add(protocol)

const idB58Str = peerInfo.id.toB58String()
_onIncomingStream ({ protocol, stream, connection }) {
const peerId = connection.remotePeer
const idB58Str = peerId.toB58String()

const peer = this._addPeer(new Peer(peerInfo))
const peer = this._addPeer(new Peer(peerId, [protocol]))

peer.attachConnection(stream)
this._processMessages(idB58Str, stream, peer)
Expand All @@ -185,14 +183,14 @@ class PubsubBaseProtocol extends EventEmitter {
/**
* Registrar notifies a connection successfully with pubsub protocol.
* @private
* @param {PeerInfo} peerInfo remote peer info
* @param {PeerId} peerId remote peer-id
* @param {Connection} conn connection to the peer
*/
async _onPeerConnected (peerInfo, conn) {
const idB58Str = peerInfo.id.toB58String()
async _onPeerConnected (peerId, conn) {
const idB58Str = peerId.toB58String()
this.log('connected', idB58Str)

const peer = this._addPeer(new Peer(peerInfo))
const peer = this._addPeer(new Peer(peerId, this.multicodecs))
try {
const { stream } = await conn.newStream(this.multicodecs)
peer.attachConnection(stream)
Expand All @@ -205,11 +203,11 @@ class PubsubBaseProtocol extends EventEmitter {
/**
* Registrar notifies a closing connection with pubsub protocol.
* @private
* @param {PeerInfo} peerInfo peer info
* @param {PeerId} peerId peerId
* @param {Error} err error for connection end
*/
_onPeerDisconnected (peerInfo, err) {
const idB58Str = peerInfo.id.toB58String()
_onPeerDisconnected (peerId, err) {
const idB58Str = peerId.toB58String()
const peer = this.peers.get(idB58Str)

this.log('connection ended', idB58Str, err ? err.message : '')
Expand All @@ -219,11 +217,11 @@ class PubsubBaseProtocol extends EventEmitter {
/**
* Add a new connected peer to the peers map.
* @private
* @param {PeerInfo} peer peer info
* @returns {PeerInfo}
* @param {Peer} peer internal peer
* @returns {Peer}
*/
_addPeer (peer) {
const id = peer.info.id.toB58String()
const id = peer.id.toB58String()
let existing = this.peers.get(id)

if (!existing) {
Expand All @@ -242,11 +240,11 @@ class PubsubBaseProtocol extends EventEmitter {
* Remove a peer from the peers map.
* @private
* @param {Peer} peer peer state
* @returns {PeerInfo}
* @returns {Peer}
*/
_removePeer (peer) {
if (!peer) return
const id = peer.info.id.toB58String()
const id = peer.id.toB58String()

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

Expand Down Expand Up @@ -287,7 +285,7 @@ class PubsubBaseProtocol extends EventEmitter {
*/
_buildMessage (message) {
const msg = utils.normalizeOutRpcMessage(message)
if (this.peerId) {
if (this.signMessages) {
return signMessage(this.peerId, msg)
} else {
return message
Expand All @@ -310,7 +308,7 @@ class PubsubBaseProtocol extends EventEmitter {

return Array.from(this.peers.values())
.filter((peer) => peer.topics.has(topic))
.map((peer) => peer.info.id.toB58String())
.map((peer) => peer.id.toB58String())
}

/**
Expand Down
15 changes: 10 additions & 5 deletions src/peer.js
Expand Up @@ -13,15 +13,20 @@ const { RPC } = require('./message')
*/
class Peer extends EventEmitter {
/**
* @param {PeerInfo} info
* @param {PeerId} id
* @param {Array<string>} protocols
*/
constructor (info) {
constructor (id, protocols) {
super()

/**
* @type {PeerInfo}
* @type {PeerId}
*/
this.info = info
this.id = id
/**
* @type {string}
*/
this.protocols = protocols
/**
* @type {Connection}
*/
Expand Down Expand Up @@ -65,7 +70,7 @@ class Peer extends EventEmitter {
*/
write (msg) {
if (!this.isWritable) {
const id = this.info.id.toB58String()
const id = this.id.toB58String()
throw new Error('No writable connection to ' + id)
}

Expand Down
16 changes: 8 additions & 8 deletions test/instance.spec.js
Expand Up @@ -7,13 +7,13 @@ chai.use(require('chai-spies'))
const expect = chai.expect

const PubsubBaseProtocol = require('../src')
const { createPeerInfo, mockRegistrar } = require('./utils')
const { createPeerId, mockRegistrar } = require('./utils')

describe('should validate instance parameters', () => {
let peerInfo
let peerId

before(async () => {
peerInfo = await createPeerInfo()
peerId = await createPeerId()
})

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

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

it('should throw if an invalid peerInfo is provided', () => {
it('should throw if an invalid peerId is provided', () => {
expect(() => {
new PubsubBaseProtocol({ // eslint-disable-line no-new
debugName: 'pubsub',
multicodecs: '/pubsub/1.0.0',
peerInfo: 'fake-peer-info'
peerId: 'fake-peer-id'
})
}).to.throw()
})
Expand All @@ -54,7 +54,7 @@ describe('should validate instance parameters', () => {
new PubsubBaseProtocol({ // eslint-disable-line no-new
debugName: 'pubsub',
multicodecs: '/pubsub/1.0.0',
peerInfo: peerInfo
peerId: peerId
})
}).to.throw()
})
Expand All @@ -64,7 +64,7 @@ describe('should validate instance parameters', () => {
new PubsubBaseProtocol({ // eslint-disable-line no-new
debugName: 'pubsub',
multicodecs: '/pubsub/1.0.0',
peerInfo: peerInfo,
peerId: peerId,
registrar: mockRegistrar
})
}).not.to.throw()
Expand Down

0 comments on commit 21a63cb

Please sign in to comment.