Skip to content

Commit

Permalink
feat: allow for duplicate messages (ChainSafe#402)
Browse files Browse the repository at this point in the history
  • Loading branch information
maschad committed Feb 14, 2023
1 parent bae1492 commit f7f59cd
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ export interface GossipsubOpts extends GossipsubOptsSpec, PubSubInit {
asyncValidation: boolean
/** Do not throw `InsufficientPeers` error if publishing to zero peers */
allowPublishToZeroPeers: boolean
/** Do not throw `PublishError.Duplicate` if publishing duplicate messages */
allowPublishDuplicates: boolean
/** For a single stream, await processing each RPC before processing the next */
awaitRpcHandler: boolean
/** For a single RPC, await processing each message before processing the next */
Expand Down Expand Up @@ -2013,7 +2015,10 @@ export class GossipSub extends EventEmitter<GossipsubEvents> implements PubSub<G
const msgId = await this.msgIdFn(msg)
const msgIdStr = this.msgIdToStrFn(msgId)

if (this.seenCache.has(msgIdStr)) {
// Current publish opt takes precedence global opts, while preserving false value
const allowPublishDuplicates = opts?.allowPublishDuplicates ?? this.opts.allowPublishDuplicates

if (this.seenCache.has(msgIdStr) && !allowPublishDuplicates) {
// This message has already been seen. We don't re-publish messages that have already
// been published on the network.
throw Error('PublishError.Duplicate')
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export enum SignaturePolicy {

export type PublishOpts = {
allowPublishToZeroPeers?: boolean
allowPublishDuplicates?: boolean
}

export enum PublishConfigType {
Expand Down

0 comments on commit f7f59cd

Please sign in to comment.