Skip to content

Commit 4033b8b

Browse files
author
Tuyen
committedMar 18, 2020
Address comments
1 parent f174b5a commit 4033b8b

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed
 

‎README.md

+1-4
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,7 @@ Options is an optional object with the following key-value pairs:
6767

6868
* **`fallbackToFloodsub`**: boolean identifying whether the node should fallback to the floodsub protocol, if another connecting peer does not support gossipsub (defaults to **true**).
6969
* **`emitSelf`**: boolean identifying whether the node should emit to self on publish, in the event of the topic being subscribed (defaults to **false**).
70-
* **`msgIdFn`**: a function defining the message id given a message, this is optional. For example:
71-
```js
72-
const msgIdFn = (message) => Buffer.from(hash(message.data)).toString("base64")
73-
```
70+
* **`msgIdFn`**: a function with signature `(message) => string` defining the message id given a message, used internally to deduplicate gossip (defaults to `(message) => message.from + message.seqno.toString('hex')`)
7471

7572
For the remaining API, see https://github.com/libp2p/js-libp2p-pubsub
7673

‎src/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class GossipSub extends BasicPubsub {
2323
* @param {bool} [options.gossipIncoming] if incoming messages on a subscribed topic should be automatically gossiped, defaults to true
2424
* @param {bool} [options.fallbackToFloodsub] if dial should fallback to floodsub, defaults to true
2525
* @param {function} [options.msgIdFn] override the default message id function
26+
* @param {Object} [options.messageCache] override the default MessageCache
2627
* @constructor
2728
*/
2829
constructor (peerInfo, registrar, options = {}) {
@@ -82,7 +83,7 @@ class GossipSub extends BasicPubsub {
8283
* A message cache that contains the messages for last few hearbeat ticks
8384
*
8485
*/
85-
this.messageCache = new MessageCache(constants.GossipSubHistoryGossip, constants.GossipSubHistoryLength, this._msgIdFn)
86+
this.messageCache = options.messageCache || new MessageCache(constants.GossipSubHistoryGossip, constants.GossipSubHistoryLength, this._msgIdFn)
8687

8788
/**
8889
* A heartbeat timer that maintains the mesh

‎src/messageCache.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,20 @@ class MessageCache {
5353
* @returns {void}
5454
*/
5555
put (msg) {
56-
const msgID = this.msgIdFn(msg)
56+
const msgID = this.getMsgId(msg)
5757
this.msgs.set(msgID, msg)
5858
this.history[0].push(new CacheEntry(msgID, msg.topicIDs))
5959
}
6060

61+
/**
62+
* Get message id of message.
63+
* @param {rpc.RPC.Message} msg
64+
* @returns {string}
65+
*/
66+
getMsgId (msg) {
67+
return this.msgIdFn(msg)
68+
}
69+
6170
/**
6271
* Retrieves a message from the cache by its ID, if it is still present
6372
*

0 commit comments

Comments
 (0)
Please sign in to comment.