Skip to content

Commit 917b7f1

Browse files
jacobheunvasco-santos
authored andcommitted
fix: _emitMessages should not emit normalized messages (#79)
1 parent 44b11f4 commit 917b7f1

File tree

3 files changed

+98
-6
lines changed

3 files changed

+98
-6
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@
5353
"libp2p-tcp": "~0.13.0",
5454
"lodash": "^4.17.11",
5555
"peer-id": "~0.12.2",
56-
"peer-info": "~0.15.1"
56+
"peer-info": "~0.15.1",
57+
"sinon": "^7.3.2"
5758
},
5859
"dependencies": {
5960
"async": "^2.6.2",

src/index.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -179,20 +179,22 @@ class FloodSub extends BaseProtocol {
179179
const seqno = utils.randomSeqno()
180180
this.seenCache.put(utils.msgId(from, seqno))
181181

182-
this._buildMessage({
182+
const message = {
183183
from: from,
184184
data: msg,
185185
seqno: seqno,
186186
topicIDs: topics
187-
}, cb)
187+
}
188+
189+
// Emit to self if I'm interested
190+
this._emitMessages(topics, [message])
191+
192+
this._buildMessage(message, cb)
188193
}
189194

190195
asyncMap(messages, buildMessage, (err, msgObjects) => {
191196
if (err) return callback(err)
192197

193-
// Emit to self if I'm interested
194-
this._emitMessages(topics, msgObjects)
195-
196198
// send to all the other peers
197199
this._forwardMessages(topics, msgObjects)
198200

test/pubsub.spec.js

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/* eslint-env mocha */
2+
/* eslint max-nested-callbacks: ["error", 5] */
3+
'use strict'
4+
5+
const chai = require('chai')
6+
chai.use(require('dirty-chai'))
7+
const expect = chai.expect
8+
const sinon = require('sinon')
9+
10+
const Floodsub = require('../src')
11+
const { createNode } = require('./utils')
12+
const { utils } = require('libp2p-pubsub')
13+
14+
describe('pubsub', () => {
15+
let floodsub
16+
let libp2p
17+
18+
before((done) => {
19+
createNode('/ip4/127.0.0.1/tcp/0', (err, node) => {
20+
expect(err).to.not.exist()
21+
libp2p = node
22+
floodsub = new Floodsub(libp2p)
23+
done(err)
24+
})
25+
})
26+
27+
beforeEach(done => {
28+
floodsub.start(done)
29+
})
30+
31+
afterEach(done => {
32+
sinon.restore()
33+
floodsub.stop(done)
34+
})
35+
36+
describe('publish', () => {
37+
it('should emit non normalized messages', (done) => {
38+
sinon.spy(floodsub, '_emitMessages')
39+
sinon.spy(utils, 'randomSeqno')
40+
41+
const topic = 'my-topic'
42+
const message = Buffer.from('a neat message')
43+
44+
floodsub.publish(topic, message, (err) => {
45+
expect(err).to.not.exist()
46+
expect(floodsub._emitMessages.callCount).to.eql(1)
47+
48+
const [topics, messages] = floodsub._emitMessages.getCall(0).args
49+
expect(topics).to.eql([topic])
50+
expect(messages).to.eql([{
51+
from: libp2p.peerInfo.id.toB58String(),
52+
data: message,
53+
seqno: utils.randomSeqno.getCall(0).returnValue,
54+
topicIDs: topics
55+
}])
56+
done()
57+
})
58+
})
59+
60+
it('should forward normalized messages', (done) => {
61+
sinon.spy(floodsub, '_forwardMessages')
62+
sinon.spy(utils, 'randomSeqno')
63+
64+
const topic = 'my-topic'
65+
const message = Buffer.from('a neat message')
66+
67+
floodsub.publish(topic, message, (err) => {
68+
expect(err).to.not.exist()
69+
expect(floodsub._forwardMessages.callCount).to.eql(1)
70+
const [topics, messages] = floodsub._forwardMessages.getCall(0).args
71+
72+
floodsub._buildMessage({
73+
from: libp2p.peerInfo.id.toB58String(),
74+
data: message,
75+
seqno: utils.randomSeqno.getCall(0).returnValue,
76+
topicIDs: topics
77+
}, (err, expected) => {
78+
expect(err).to.not.exist()
79+
80+
expect(topics).to.eql([topic])
81+
expect(messages).to.eql([
82+
expected
83+
])
84+
done()
85+
})
86+
})
87+
})
88+
})
89+
})

0 commit comments

Comments
 (0)