Skip to content

Commit 7103cf2

Browse files
authored
chore: update dependencies (#116)
* chore: update dependencies BREAKING CHANGE: uses new multiaddr, libp2p-interfaces, etc
1 parent 9b62572 commit 7103cf2

File tree

6 files changed

+109
-74
lines changed

6 files changed

+109
-74
lines changed

.aegir.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ const esbuild = {
6060

6161
/** @type {import('aegir').PartialOptions} */
6262
module.exports = {
63+
build: {
64+
bundlesizeMax: '143KB',
65+
},
6366
test: {
6467
before,
6568
after,

.github/workflows/main.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,33 +39,33 @@ jobs:
3939
steps:
4040
- uses: actions/checkout@v2
4141
- run: npm install
42-
- run: npx aegir test -t browser -t webworker --bail
42+
- run: npx aegir test -t browser -t webworker --bail --timeout 60000
4343
test-firefox:
4444
needs: check
4545
runs-on: ubuntu-latest
4646
steps:
4747
- uses: actions/checkout@v2
4848
- run: npm install
49-
- run: npx aegir test -t browser -t webworker --bail -- --browser firefox
49+
- run: npx aegir test -t browser -t webworker --bail --timeout 60000 -- --browser firefox
5050
test-webkit:
5151
needs: check
5252
runs-on: ubuntu-latest
5353
steps:
5454
- uses: actions/checkout@v2
5555
- uses: microsoft/playwright-github-action@v1
5656
- run: npm install
57-
- run: npx aegir test -t browser -t webworker --bail -- --browser webkit
57+
- run: npx aegir test -t browser -t webworker --bail --timeout 60000 -- --browser webkit
5858
test-electron-main:
5959
needs: check
6060
runs-on: ubuntu-latest
6161
steps:
6262
- uses: actions/checkout@v2
6363
- run: npm install
64-
- run: npx xvfb-maybe aegir test -t electron-main --bail
64+
- run: npx xvfb-maybe aegir test -t electron-main --bail --timeout 60000
6565
test-electron-renderer:
6666
needs: check
6767
runs-on: ubuntu-latest
6868
steps:
6969
- uses: actions/checkout@v2
7070
- run: npm install
71-
- run: npx xvfb-maybe aegir test -t electron-renderer --bail
71+
- run: npx xvfb-maybe aegir test -t electron-renderer --bail --timeout 60000

package.json

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"main": "src/index.js",
77
"types": "dist/src/index.d.ts",
88
"scripts": {
9-
"lint": "aegir lint",
9+
"lint": "aegir ts -p check && aegir lint",
1010
"test": "aegir test",
1111
"test:node": "aegir test -t node",
1212
"test:browser": "aegir test -t browser",
@@ -50,20 +50,22 @@
5050
"chai": "^4.3.4",
5151
"ipfs-utils": "^8.1.0",
5252
"it-pair": "^1.0.0",
53-
"libp2p": "^0.31.6",
53+
"libp2p": "next",
54+
"libp2p-interfaces-compliance-tests": "^1.0.1",
5455
"libp2p-mplex": "^0.10.3",
55-
"libp2p-noise": "^3.0.0",
56-
"libp2p-websockets": "^0.15.7",
57-
"multiaddr": "^9.0.1",
56+
"libp2p-noise": "^4.0.0",
57+
"libp2p-websockets": "^0.16.0",
58+
"multiaddr": "^10.0.0",
59+
"multiformats": "^9.2.0",
5860
"os": "^0.1.1",
5961
"p-wait-for": "^3.1.0",
60-
"peer-id": "^0.14.8",
62+
"peer-id": "^0.15.0",
6163
"sinon": "^11.1.1",
6264
"util": "^0.12.3"
6365
},
6466
"dependencies": {
6567
"debug": "^4.2.0",
66-
"libp2p-interfaces": "^0.11.0",
68+
"libp2p-interfaces": "^1.0.1",
6769
"time-cache": "^0.3.0",
6870
"uint8arrays": "^2.1.5"
6971
},

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class FloodSub extends BaseProtocol {
5353
*/
5454
async _processRpcMessage (message) {
5555
// Check if I've seen the message, if yes, ignore
56-
const seqno = this.getMsgId(message)
56+
const seqno = await this.getMsgId(message)
5757
const msgIdStr = toString(seqno, 'base64')
5858

5959
if (this.seenCache.has(msgIdStr)) {
@@ -90,7 +90,7 @@ class FloodSub extends BaseProtocol {
9090
}
9191
peers.forEach((id) => {
9292
this.log('publish msgs on topics', message.topicIDs, id)
93-
if (id !== this.peerId.toB58String()) {
93+
if (id !== this.peerId.toB58String() && id !== message.receivedFrom) {
9494
this._sendRpc(id, { msgs: [utils.normalizeOutRpcMessage(message)] })
9595
}
9696
})

test/compliance.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-env mocha */
22
'use strict'
33

4-
const tests = require('libp2p-interfaces/src/pubsub/tests')
4+
const tests = require('libp2p-interfaces-compliance-tests/src/pubsub')
55

66
const Floodsub = require('../src')
77
const { createPeers } = require('./utils/create-peer')

test/floodsub.spec.js

Lines changed: 89 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -4,107 +4,137 @@
44
const { expect } = require('aegir/utils/chai')
55
const sinon = require('sinon')
66
const uint8ArrayFromString = require('uint8arrays/from-string')
7-
7+
const uint8ArrayToString = require('uint8arrays/to-string')
8+
const { sha256 } = require('multiformats/hashes/sha2')
89
const { utils } = require('libp2p-interfaces/src/pubsub')
9-
const pWaitFor = require('p-wait-for')
10+
const { SignaturePolicy } = require('libp2p-interfaces/src/pubsub/signature-policy')
11+
const PeerStreams = require('libp2p-interfaces/src/pubsub/peer-streams')
12+
const PeerId = require('peer-id')
1013

1114
const Floodsub = require('../src')
12-
const { createPeers } = require('./utils/create-peer')
1315

1416
const defOptions = {
15-
emitSelf: true
17+
emitSelf: true,
18+
globalSignaturePolicy: SignaturePolicy.StrictNoSign
1619
}
1720

1821
const topic = 'my-topic'
1922
const message = uint8ArrayFromString('a neat message')
2023

2124
describe('floodsub', () => {
22-
let floodsub1, floodsub2
23-
let peer1, peer2
25+
let floodsub
2426

2527
before(async () => {
2628
expect(Floodsub.multicodec).to.exist()
2729

28-
;[peer1, peer2] = await createPeers({ number: 2 })
29-
floodsub1 = new Floodsub(peer1, defOptions)
30-
floodsub2 = new Floodsub(peer2, defOptions)
30+
const libp2p = {
31+
peerId: await PeerId.create(),
32+
registrar: {
33+
handle: () => {},
34+
register: () => {},
35+
unregister: () => {}
36+
}
37+
}
38+
39+
floodsub = new Floodsub(libp2p, defOptions)
3140
})
3241

3342
beforeEach(() => {
34-
return Promise.all([
35-
floodsub1.start(),
36-
floodsub2.start()
37-
])
43+
floodsub.start()
3844
})
3945

40-
afterEach(async () => {
46+
afterEach(() => {
4147
sinon.restore()
42-
await floodsub1.stop()
43-
await floodsub2.stop()
44-
await peer1.stop()
45-
await peer2.stop()
48+
floodsub.stop()
4649
})
4750

48-
it('checks cache when processing incoming message', async () => {
49-
sinon.spy(floodsub2.seenCache, 'has')
50-
sinon.spy(floodsub2.seenCache, 'put')
51-
sinon.spy(floodsub2, '_processRpcMessage')
52-
sinon.spy(floodsub2, '_publish')
51+
it('checks cache when processing incoming message', async function () {
52+
const otherPeer = await PeerId.create()
53+
const sig = await sha256.encode(message)
54+
const key = uint8ArrayToString(sig, 'base64')
55+
let callCount = 0
5356

54-
let messageReceived = false
55-
function checkMessage (msg) {
56-
messageReceived = true
57+
const peerStream = new PeerStreams({
58+
id: otherPeer,
59+
protocol: 'test'
60+
})
61+
const rpc = {
62+
subscriptions: [],
63+
msgs: [{
64+
receivedFrom: peerStream.id.toB58String(),
65+
data: message,
66+
topicIDs: [topic]
67+
}]
5768
}
5869

59-
// connect peers
60-
await floodsub1._libp2p.dial(floodsub2._libp2p.peerId)
61-
62-
// subscribe and wait for subscription to be received in the other peer
63-
floodsub2.subscribe(topic)
64-
floodsub2.on(topic, checkMessage)
65-
await pWaitFor(() => {
66-
const subs = floodsub1.getSubscribers(topic)
67-
68-
return subs.length === 1
70+
floodsub.subscribe(topic)
71+
floodsub.on(topic, () => {
72+
callCount++
6973
})
7074

71-
await floodsub1.publish(topic, message)
72-
await pWaitFor(() => messageReceived === true)
75+
// the message should not be in the cache
76+
expect(floodsub.seenCache.has(key)).to.be.false()
77+
78+
// receive the message once
79+
await floodsub._processRpc(peerStream.id.toB58String(), peerStream, rpc)
7380

74-
expect(floodsub2.seenCache.has.callCount).to.eql(2) // Put also calls .has
75-
expect(floodsub2.seenCache.put.callCount).to.eql(1)
76-
expect(floodsub2._publish.callCount).to.eql(1) // Forward message
81+
// should have received the message
82+
expect(callCount).to.equal(1)
7783

78-
const [msgProcessed] = floodsub2._processRpcMessage.getCall(0).args
84+
// should be in the cache now
85+
expect(floodsub.seenCache.has(key)).to.be.true()
7986

80-
// Force a second process for the message
81-
await floodsub2._processRpcMessage(msgProcessed)
87+
// receive the message multiple times
88+
await floodsub._processRpc(peerStream.id.toB58String(), peerStream, rpc)
89+
await floodsub._processRpc(peerStream.id.toB58String(), peerStream, rpc)
90+
await floodsub._processRpc(peerStream.id.toB58String(), peerStream, rpc)
8291

83-
expect(floodsub2.seenCache.has.callCount).to.eql(3)
84-
expect(floodsub2.seenCache.put.callCount).to.eql(1) // No new put
85-
expect(floodsub2._publish.callCount).to.eql(1) // Not forwarded
92+
// should only have emitted the message once
93+
expect(callCount).to.equal(1)
8694
})
8795

8896
it('forwards normalized messages on publish', async () => {
89-
sinon.spy(floodsub1, '_forwardMessage')
90-
sinon.spy(utils, 'randomSeqno')
97+
sinon.spy(floodsub, '_forwardMessage')
9198

92-
await floodsub1.publish(topic, message)
93-
expect(floodsub1._forwardMessage.callCount).to.eql(1)
94-
const [messageToEmit] = floodsub1._forwardMessage.getCall(0).args
95-
96-
const computedSeqno = utils.randomSeqno.getCall(0).returnValue
97-
utils.randomSeqno.restore()
98-
sinon.stub(utils, 'randomSeqno').returns(computedSeqno)
99+
await floodsub.publish(topic, message)
100+
expect(floodsub._forwardMessage.callCount).to.equal(1)
101+
const [messageToEmit] = floodsub._forwardMessage.getCall(0).args
99102

100103
const expected = utils.normalizeInRpcMessage(
101-
await floodsub1._buildMessage({
102-
receivedFrom: peer1.peerId.toB58String(),
103-
from: peer1.peerId.toB58String(),
104+
await floodsub._buildMessage({
105+
receivedFrom: floodsub.peerId.toB58String(),
104106
data: message,
105107
topicIDs: [topic]
106108
}))
107109

108110
expect(messageToEmit).to.eql(expected)
109111
})
112+
113+
it('does not send received message back to original sender', async () => {
114+
sinon.spy(floodsub, '_sendRpc')
115+
116+
const sender = await PeerId.create()
117+
118+
const peerStream = new PeerStreams({
119+
id: sender,
120+
protocol: 'test'
121+
})
122+
const rpc = {
123+
subscriptions: [],
124+
msgs: [{
125+
receivedFrom: peerStream.id.toB58String(),
126+
data: message,
127+
topicIDs: [topic]
128+
}]
129+
}
130+
131+
// otherPeer is subscribed to the topic
132+
floodsub.topics.set(topic, new Set([sender.toB58String()]))
133+
134+
// receive the message
135+
await floodsub._processRpc(peerStream.id.toB58String(), peerStream, rpc)
136+
137+
// should not forward back to the sender
138+
expect(floodsub._sendRpc.called).to.be.false()
139+
})
110140
})

0 commit comments

Comments
 (0)