Skip to content

Commit 55916fe

Browse files
Alan Shawvasco-santos
Alan Shaw
authored andcommitted
fix: ignore message false positive (#59)
* fix: ignore message false positive This PR changes the `toString` encoding of message `seqno` to 'hex' so that a unique key is generated by `utils.msgId` for caching messages. Converting the `seqno` to a utf8 string can sometimes result in the same string for different buffers. The following are two different seqno's that `js-ipfs` was sent from `go-ipfs` that demonstrates the problem: ```console $ node > const buf2 = Buffer.from('15603533e990dfe0', 'hex') > const buf1 = Buffer.from('15603533e990dfde', 'hex') > buf1.toString('utf8') '\u0015`53���' > buf2.toString('utf8') '\u0015`53���' > buf1.toString('utf8') === buf2.toString('utf8') true > ``` So sometimes we think we've seen the message when we haven't! 🤦‍♂️ License: MIT Signed-off-by: Alan Shaw <[email protected]> * fix: jsdoc for randomSeqno License: MIT Signed-off-by: Alan Shaw <[email protected]>
1 parent c4a108d commit 55916fe

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

src/index.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ const BaseProtocol = require('./base')
99
const utils = require('./utils')
1010
const pb = require('./message')
1111
const config = require('./config')
12-
const Buffer = require('safe-buffer').Buffer
1312

1413
const multicodec = config.multicodec
1514
const ensureArray = utils.ensureArray
@@ -88,7 +87,7 @@ class FloodSub extends BaseProtocol {
8887

8988
_processRpcMessages (msgs) {
9089
msgs.forEach((msg) => {
91-
const seqno = utils.msgId(msg.from, msg.seqno.toString())
90+
const seqno = utils.msgId(msg.from, msg.seqno)
9291
// 1. check if I've seen the message, if yes, ignore
9392
if (this.cache.has(seqno)) {
9493
return
@@ -168,7 +167,7 @@ class FloodSub extends BaseProtocol {
168167
return {
169168
from: from,
170169
data: msg,
171-
seqno: new Buffer(seqno),
170+
seqno: seqno,
172171
topicIDs: topics
173172
}
174173
}

src/utils.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,23 @@ exports = module.exports
88
/**
99
* Generatea random sequence number.
1010
*
11-
* @returns {string}
11+
* @returns {Buffer}
1212
* @private
1313
*/
1414
exports.randomSeqno = () => {
15-
return crypto.randomBytes(20).toString('hex')
15+
return crypto.randomBytes(20)
1616
}
1717

1818
/**
1919
* Generate a message id, based on the `from` and `seqno`.
2020
*
2121
* @param {string} from
22-
* @param {string} seqno
22+
* @param {Buffer} seqno
2323
* @returns {string}
2424
* @private
2525
*/
2626
exports.msgId = (from, seqno) => {
27-
return from + seqno
27+
return from + seqno.toString('hex')
2828
}
2929

3030
/**

test/utils.spec.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,20 @@ describe('utils', () => {
1010
const first = utils.randomSeqno()
1111
const second = utils.randomSeqno()
1212

13-
expect(first).to.have.length(40)
14-
expect(second).to.have.length(40)
13+
expect(first).to.have.length(20)
14+
expect(second).to.have.length(20)
1515
expect(first).to.not.eql(second)
1616
})
1717

1818
it('msgId', () => {
19-
expect(utils.msgId('hello', 'world')).to.be.eql('helloworld')
19+
expect(utils.msgId('hello', Buffer.from('world'))).to.be.eql('hello776f726c64')
20+
})
21+
22+
it('msgId should not generate same ID for two different buffers', () => {
23+
const peerId = 'QmPNdSYk5Rfpo5euNqwtyizzmKXMNHdXeLjTQhcN4yfX22'
24+
const msgId0 = utils.msgId(peerId, Buffer.from('15603533e990dfde', 'hex'))
25+
const msgId1 = utils.msgId(peerId, Buffer.from('15603533e990dfe0', 'hex'))
26+
expect(msgId0).to.not.eql(msgId1)
2027
})
2128

2229
it('anyMatch', () => {

0 commit comments

Comments
 (0)