Skip to content

Commit

Permalink
Merge pull request #41 from libp2p/chore/remove-peer-info-usage
Browse files Browse the repository at this point in the history
chore: remove peer-info usage
  • Loading branch information
vasco-santos authored Apr 22, 2020
2 parents 59d49dc + 071468f commit 721a53e
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 80 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ TODO: add explanation for registrar!
const Pubsub = require('libp2p-pubsub')

class PubsubImplementation extends Pubsub {
constructor({ peerInfo, registrar, ...options })
constructor({ peerId, registrar, ...options })
super({
debugName: 'libp2p:pubsub',
multicodecs: '/pubsub-implementation/1.0.0',
peerInfo: peerInfo,
peerId: peerId,
registrar: registrar,
signMessages: options.signMessages,
strictSigning: options.strictSigning
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
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": "^0.3.0",
"peer-id": "~0.13.3",
"protons": "^1.0.1"
},
"contributors": [
Expand Down
62 changes: 33 additions & 29 deletions src/index.js
Original file line number Diff line number Diff line change
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,12 @@ class PubsubBaseProtocol extends EventEmitter {
this.log.err = debug(`${debugName}:error`)

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

this.started = false

this.peerId = peerId

/**
* Map of topics to which peers are subscribed to
*
Expand All @@ -99,9 +100,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 +169,14 @@ 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)
_onIncomingStream ({ protocol, stream, connection }) {
const peerId = connection.remotePeer
const idB58Str = peerId.toB58String()

const idB58Str = peerInfo.id.toB58String()

const peer = this._addPeer(new Peer(peerInfo))
const peer = this._addPeer(new Peer({
id: peerId,
protocols: [protocol]
}))

peer.attachConnection(stream)
this._processMessages(idB58Str, stream, peer)
Expand All @@ -185,14 +185,18 @@ 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({
id: peerId,
protocols: this.multicodecs
}))

try {
const { stream } = await conn.newStream(this.multicodecs)
peer.attachConnection(stream)
Expand All @@ -205,11 +209,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 +223,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 +246,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 +291,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 +314,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading

0 comments on commit 721a53e

Please sign in to comment.