Skip to content

Commit

Permalink
Add default impl for getFastMsgIdStr()
Browse files Browse the repository at this point in the history
  • Loading branch information
twoeths committed Jan 11, 2022
1 parent 42b14e9 commit dfc2628
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ interface AcceptFromWhitelistEntry {
acceptUntil: number
}

type FastMsgIdFn = (msg: InMessage) => string;
type FastMsgIdFn = (msg: InMessage) => Promise<string>;

class Gossipsub extends Pubsub {
peers: Map<string, PeerStreams>
Expand All @@ -112,7 +112,6 @@ class Gossipsub extends Pubsub {
backoff: Map<string, Map<string, number>>
outbound: Map<string, boolean>
defaultMsgIdFn: MessageIdFunction
_msgIdFn: MessageIdFunction
getFastMsgIdStr: FastMsgIdFn
messageCache: MessageCache
score: PeerScore
Expand Down Expand Up @@ -152,7 +151,7 @@ class Gossipsub extends Pubsub {
*/
constructor (
libp2p: Libp2p,
options: Partial<GossipInputOptions> & Pick<GossipInputOptions, 'fastMsgIdFn'>
options: Partial<GossipInputOptions>
) {
const multicodecs = [constants.GossipsubIDv11, constants.GossipsubIDv10]
const opts = {
Expand Down Expand Up @@ -295,7 +294,11 @@ class Gossipsub extends Pubsub {
/**
* A fast message id function which return a string from InMessage
*/
this.getFastMsgIdStr = options.fastMsgIdFn
if (options.fastMsgIdFn) {
this.getFastMsgIdStr = options.fastMsgIdFn
} else {
this.getFastMsgIdStr = async (msg: InMessage): Promise<string> => messageIdToString(await this.getMsgId(msg))
}

/**
* A heartbeat timer that maintains the mesh
Expand Down Expand Up @@ -462,7 +465,7 @@ class Gossipsub extends Pubsub {
* @returns {Promise<void>}
*/
async _processRpcMessage (msg: InMessage): Promise<void> {
const fastMsgIdStr = this.getFastMsgIdStr(msg)
const fastMsgIdStr = await this.getFastMsgIdStr(msg)

// Ignore if we've already seen the message
let canonicalMsgIdStr = this.fastMsgIdCache.get(fastMsgIdStr)
Expand Down Expand Up @@ -1102,7 +1105,7 @@ class Gossipsub extends Pubsub {
* @param {InMessage} msg
*/
async getCanonicalMsgIdStr (msg: InMessage): Promise<string> {
return this.getCachedMsgIdStr(msg) ?? this.fastMsgIdCache.get(this.getFastMsgIdStr(msg)) ?? messageIdToString(await this.getMsgId(msg))
return this.getCachedMsgIdStr(msg) ?? this.fastMsgIdCache.get(await this.getFastMsgIdStr(msg)) ?? messageIdToString(await this.getMsgId(msg))
}

/**
Expand Down

0 comments on commit dfc2628

Please sign in to comment.