Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix!: make PublishError.InsufficientPeers more self-explanatory #487

Merged
merged 1 commit into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,15 @@ export interface GossipsubOpts extends GossipsubOptsSpec, PubSubInit {
* reportMessageValidationResult() after the message is dropped from mcache won't forward the message.
*/
asyncValidation: boolean
/** Do not throw `InsufficientPeers` error if publishing to zero peers */
allowPublishToZeroPeers: boolean
/**
* Do not throw `PublishError.NoPeersSubscribedToTopic` error if there are no
* peers listening on the topic.
*
* N.B. if you sent this option to true, and you publish a message on a topic
* with no peers listening on that topic, no other network node will ever
* receive the message.
*/
allowPublishToZeroTopicPeers: boolean
/** Do not throw `PublishError.Duplicate` if publishing duplicate messages */
ignoreDuplicatePublishError: boolean
/** For a single stream, await processing each RPC before processing the next */
Expand Down Expand Up @@ -2095,10 +2102,10 @@ export class GossipSub extends TypedEventEmitter<GossipsubEvents> implements Pub
const willSendToSelf = this.opts.emitSelf && this.subscriptions.has(topic)

// Current publish opt takes precedence global opts, while preserving false value
const allowPublishToZeroPeers = opts?.allowPublishToZeroPeers ?? this.opts.allowPublishToZeroPeers
const allowPublishToZeroTopicPeers = opts?.allowPublishToZeroTopicPeers ?? this.opts.allowPublishToZeroTopicPeers

if (tosend.size === 0 && !allowPublishToZeroPeers && !willSendToSelf) {
throw Error('PublishError.InsufficientPeers')
if (tosend.size === 0 && !allowPublishToZeroTopicPeers && !willSendToSelf) {
throw Error('PublishError.NoPeersSubscribedToTopic')
}

// If the message isn't a duplicate and we have sent it to some peers add it to the
Expand Down
10 changes: 9 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,15 @@ export enum SignaturePolicy {
}

export interface PublishOpts {
allowPublishToZeroPeers?: boolean
/**
* Do not throw `PublishError.NoPeersSubscribedToTopic` error if there are no
* peers listening on the topic.
*
* N.B. if you sent this option to true, and you publish a message on a topic
* with no peers listening on that topic, no other network node will ever
* receive the message.
*/
allowPublishToZeroTopicPeers?: boolean
ignoreDuplicatePublishError?: boolean
/** serialize message once and send to all peers without control messages */
batchPublish?: boolean
Expand Down
2 changes: 1 addition & 1 deletion test/2-nodes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ describe('2 nodes', () => {
// Create pubsub nodes
beforeEach(async () => {
mockNetwork.reset()
nodes = await createComponentsArray({ number: 2, init: { allowPublishToZeroPeers: true } })
nodes = await createComponentsArray({ number: 2, init: { allowPublishToZeroTopicPeers: true } })
await connectAllPubSubNodes(nodes)

// Create subscriptions
Expand Down
2 changes: 1 addition & 1 deletion test/compliance.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe.skip('interface compliance', function () {
...args.init,
// libp2p-interfaces-compliance-tests in test 'can subscribe and unsubscribe correctly' publishes to no peers
// Disable check to allow passing tests
allowPublishToZeroPeers: true
allowPublishToZeroTopicPeers: true
}
)

Expand Down
4 changes: 2 additions & 2 deletions test/gossip.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('gossip', () => {
IPColocationFactorThreshold: GossipsubDhi + 3
},
maxInboundDataLength: 4000000,
allowPublishToZeroPeers: false
allowPublishToZeroTopicPeers: false
}
})
})
Expand Down Expand Up @@ -89,7 +89,7 @@ describe('gossip', () => {
const topic = 'Z'

const publishResult = await nodeA.pubsub.publish(topic, uint8ArrayFromString('hey'), {
allowPublishToZeroPeers: true
allowPublishToZeroTopicPeers: true
})

// gossip happens during the heartbeat
Expand Down