Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

Commit

Permalink
chore: address review
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Nov 1, 2019
1 parent c670d50 commit a1410d6
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 111 deletions.
12 changes: 6 additions & 6 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License
The MIT License (MIT)

Copyright (c) 2016 libp2p
Copyright (c) 2019 Protocol Labs, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -9,13 +9,13 @@ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,14 @@ js-libp2p-floodsub
const FloodSub = require('libp2p-floodsub')

const registrar = {
register: (multicodec, handlers) => {
handle: (multicodecs, handle) => {
// register multicodec to libp2p
// handle function is called everytime a remote peer opens a stream to the peer.
},
register: (multicodecs, handlers) => {
// handlers will be used to notify pubsub of peer connection establishment or closing
},
unregister: (multicodec) => {
unregister: (id) => {

}
}
Expand Down Expand Up @@ -99,4 +102,4 @@ This repository falls under the IPFS [Code of Conduct](https://github.com/ipfs/c

## License

MIT © David Dias
Copyright (c) Protocol Labs, Inc. under the **MIT License**. See [LICENSE file](./LICENSE) for details.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,14 @@
"dirty-chai": "^2.0.1",
"it-pair": "^1.0.0",
"lodash": "^4.17.15",
"multiaddr": "^6.1.0",
"multiaddr": "^7.1.0",
"p-defer": "^3.0.0",
"peer-id": "~0.13.3",
"peer-info": "~0.17.0",
"sinon": "^7.5.0"
},
"dependencies": {
"async.nexttick": "^0.5.2",
"debug": "^4.1.1",
"it-length-prefixed": "^2.0.0",
"it-pipe": "^1.0.1",
Expand Down
18 changes: 12 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const pipe = require('it-pipe')
const lp = require('it-length-prefixed')
const pMap = require('p-map')
const TimeCache = require('time-cache')
const nextTick = require('async.nexttick')

const PeerInfo = require('peer-info')
const BaseProtocol = require('libp2p-pubsub')
Expand All @@ -27,6 +28,7 @@ class FloodSub extends BaseProtocol {
/**
* @param {PeerInfo} peerInfo instance of the peer's PeerInfo
* @param {Object} registrar
* @param {function} registrar.handle
* @param {function} registrar.register
* @param {function} registrar.unregister
* @param {Object} [options]
Expand All @@ -37,6 +39,7 @@ class FloodSub extends BaseProtocol {
assert(PeerInfo.isPeerInfo(peerInfo), 'peer info must be an instance of `peer-info`')

// registrar handling
assert(registrar && typeof registrar.handle === 'function', 'a handle function must be provided in registrar')
assert(registrar && typeof registrar.register === 'function', 'a register function must be provided in registrar')
assert(registrar && typeof registrar.unregister === 'function', 'a unregister function must be provided in registrar')

Expand Down Expand Up @@ -77,9 +80,10 @@ class FloodSub extends BaseProtocol {
* @override
* @param {PeerInfo} peerInfo peer info
* @param {Connection} conn connection to the peer
* @returns {Promise<void>}
*/
_onPeerConnected (peerInfo, conn) {
super._onPeerConnected(peerInfo, conn)
async _onPeerConnected (peerInfo, conn) {
await super._onPeerConnected(peerInfo, conn)
const idB58Str = peerInfo.id.toB58String()
const peer = this.peers.get(idB58Str)

Expand All @@ -105,7 +109,7 @@ class FloodSub extends BaseProtocol {
await pipe(
conn,
lp.decode(),
async function collect (source) {
async function (source) {
for await (const data of source) {
const rpc = Buffer.isBuffer(data) ? data : data.slice()

Expand Down Expand Up @@ -209,7 +213,7 @@ class FloodSub extends BaseProtocol {
/**
* Unmounts the floodsub protocol and shuts down every connection
* @override
* @returns {Promise}
* @returns {Promise<void>}
*/
async stop () {
await super.stop()
Expand All @@ -222,7 +226,7 @@ class FloodSub extends BaseProtocol {
* @override
* @param {Array<string>|string} topics
* @param {Array<any>|any} messages
* @returns {Promise}
* @returns {Promise<void>}
*/
async publish (topics, messages) {
assert(this.started, 'FloodSub is not started')
Expand Down Expand Up @@ -267,10 +271,10 @@ class FloodSub extends BaseProtocol {
assert(this.started, 'FloodSub is not started')

topics = ensureArray(topics)

topics.forEach((topic) => this.subscriptions.add(topic))

this.peers.forEach((peer) => sendSubscriptionsOnceReady(peer))

// make sure that FloodSub is already mounted
function sendSubscriptionsOnceReady (peer) {
if (peer && peer.isWritable) {
Expand Down Expand Up @@ -303,6 +307,8 @@ class FloodSub extends BaseProtocol {
function checkIfReady (peer) {
if (peer && peer.isWritable) {
peer.sendUnsubscriptions(topics)
} else {
nextTick(checkIfReady.bind(peer))
}
}
}
Expand Down
78 changes: 33 additions & 45 deletions test/2-nodes.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ const expect = chai.expect

const pDefer = require('p-defer')
const times = require('lodash/times')
const DuplexPair = require('it-pair/duplex')

const FloodSub = require('../src')
const { multicodec } = require('../src')
const { first, createPeerInfo, expectSet } = require('./utils')

const defOptions = {
emitSelf: true
}
const {
defOptions,
first,
createPeerInfo,
createMockRegistrar,
expectSet,
ConnectionPair
} = require('./utils')

function shouldNotHappen (_) {
expect.fail()
Expand All @@ -31,24 +33,15 @@ describe('basics between 2 nodes', () => {
const registrarRecordA = {}
const registrarRecordB = {}

const registrar = (registrarRecord) => ({
register: (multicodecs, handlers) => {
registrarRecord[multicodecs[0]] = handlers
},
unregister: (multicodecs) => {
delete registrarRecord[multicodecs[0]]
}
})

// Mount pubsub protocol
before(async () => {
[peerInfoA, peerInfoB] = await Promise.all([
createPeerInfo(),
createPeerInfo()
])

fsA = new FloodSub(peerInfoA, registrar(registrarRecordA), defOptions)
fsB = new FloodSub(peerInfoB, registrar(registrarRecordB), defOptions)
fsA = new FloodSub(peerInfoA, createMockRegistrar(registrarRecordA), defOptions)
fsB = new FloodSub(peerInfoB, createMockRegistrar(registrarRecordB), defOptions)

expect(fsA.peers.size).to.be.eql(0)
expect(fsA.subscriptions.size).to.eql(0)
Expand All @@ -63,14 +56,19 @@ describe('basics between 2 nodes', () => {
]))

// Connect floodsub nodes
before(() => {
before(async () => {
const onConnectA = registrarRecordA[multicodec].onConnect
const onConnectB = registrarRecordB[multicodec].onConnect
const handleB = registrarRecordB[multicodec].handler

// Notice peers of connection
const [d0, d1] = DuplexPair()
onConnectA(peerInfoB, d0)
onConnectB(peerInfoA, d1)
const [c0, c1] = ConnectionPair()
await onConnectA(peerInfoB, c0)

await handleB({
protocol: multicodec,
stream: c1.stream,
remotePeer: peerInfoA.id
})

expect(fsA.peers.size).to.be.eql(1)
expect(fsB.peers.size).to.be.eql(1)
Expand Down Expand Up @@ -236,24 +234,15 @@ describe('basics between 2 nodes', () => {
const registrarRecordA = {}
const registrarRecordB = {}

const registrar = (registrarRecord) => ({
register: (multicodec, handlers) => {
registrarRecord[multicodec] = handlers
},
unregister: (multicodec) => {
delete registrarRecord[multicodec]
}
})

// Mount pubsub protocol
before(async () => {
[peerInfoA, peerInfoB] = await Promise.all([
createPeerInfo(),
createPeerInfo()
])

fsA = new FloodSub(peerInfoA, registrar(registrarRecordA), defOptions)
fsB = new FloodSub(peerInfoB, registrar(registrarRecordB), defOptions)
fsA = new FloodSub(peerInfoA, createMockRegistrar(registrarRecordA), defOptions)
fsB = new FloodSub(peerInfoB, createMockRegistrar(registrarRecordB), defOptions)
})

// Start pubsub
Expand Down Expand Up @@ -281,19 +270,18 @@ describe('basics between 2 nodes', () => {
})

it('existing subscriptions are sent upon peer connection', async () => {
const dial = async () => {
const onConnectA = registrarRecordA[multicodec].onConnect
const onConnectB = registrarRecordB[multicodec].onConnect

// Notice peers of connection
const [c0, c1] = ConnectionPair()
await onConnectA(peerInfoB, c0)
await onConnectB(peerInfoA, c1)
}

await Promise.all([
// nodeA.dial(nodeB.peerInfo),
new Promise((resolve) => {
const onConnectA = registrarRecordA[multicodec].onConnect
const onConnectB = registrarRecordB[multicodec].onConnect

// Notice peers of connection
const [d0, d1] = DuplexPair()
onConnectA(peerInfoB, d0)
onConnectB(peerInfoA, d1)

resolve()
}),
dial(),
new Promise((resolve) => fsA.once('floodsub:subscription-change', resolve)),
new Promise((resolve) => fsB.once('floodsub:subscription-change', resolve))
])
Expand Down
Loading

0 comments on commit a1410d6

Please sign in to comment.