Skip to content

Commit 1958aab

Browse files
authored
fix!: make PublishError.InsufficientPeers more self-explanatory (#487)
People continually mistake the `InsufficientPeers` error for something that can be "worked around" by setting `allowPublishToZeroPeers` to true, and they then expect other network nodes to still recieve their messages. This PR renames the option to `allowPublishToZeroTopicPeers` - this makes it clear(er) that it's not that you don't have any peers, it's that no peers you have are listening on the topic. It also improves the JSDoc comment on the option and changes the `Error` message from `PublishError.InsufficientPeers` to `PublishError.NoPeersSubscribedToTopic` which (I hope) more accurately describes what's wrong. BREAKING CHANGE: The `allowPublishToZeroPeers` option has been renamed to `allowPublishToZeroTopicPeers` Fixes #472
1 parent 42b5b92 commit 1958aab

File tree

5 files changed

+25
-10
lines changed

5 files changed

+25
-10
lines changed

src/index.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,15 @@ export interface GossipsubOpts extends GossipsubOptsSpec, PubSubInit {
105105
* reportMessageValidationResult() after the message is dropped from mcache won't forward the message.
106106
*/
107107
asyncValidation: boolean
108-
/** Do not throw `InsufficientPeers` error if publishing to zero peers */
109-
allowPublishToZeroPeers: boolean
108+
/**
109+
* Do not throw `PublishError.NoPeersSubscribedToTopic` error if there are no
110+
* peers listening on the topic.
111+
*
112+
* N.B. if you sent this option to true, and you publish a message on a topic
113+
* with no peers listening on that topic, no other network node will ever
114+
* receive the message.
115+
*/
116+
allowPublishToZeroTopicPeers: boolean
110117
/** Do not throw `PublishError.Duplicate` if publishing duplicate messages */
111118
ignoreDuplicatePublishError: boolean
112119
/** For a single stream, await processing each RPC before processing the next */
@@ -2137,10 +2144,10 @@ export class GossipSub extends TypedEventEmitter<GossipsubEvents> implements Pub
21372144
const willSendToSelf = this.opts.emitSelf && this.subscriptions.has(topic)
21382145

21392146
// Current publish opt takes precedence global opts, while preserving false value
2140-
const allowPublishToZeroPeers = opts?.allowPublishToZeroPeers ?? this.opts.allowPublishToZeroPeers
2147+
const allowPublishToZeroTopicPeers = opts?.allowPublishToZeroTopicPeers ?? this.opts.allowPublishToZeroTopicPeers
21412148

2142-
if (tosend.size === 0 && !allowPublishToZeroPeers && !willSendToSelf) {
2143-
throw Error('PublishError.InsufficientPeers')
2149+
if (tosend.size === 0 && !allowPublishToZeroTopicPeers && !willSendToSelf) {
2150+
throw Error('PublishError.NoPeersSubscribedToTopic')
21442151
}
21452152

21462153
// If the message isn't a duplicate and we have sent it to some peers add it to the

src/types.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,15 @@ export enum SignaturePolicy {
7171
}
7272

7373
export interface PublishOpts {
74-
allowPublishToZeroPeers?: boolean
74+
/**
75+
* Do not throw `PublishError.NoPeersSubscribedToTopic` error if there are no
76+
* peers listening on the topic.
77+
*
78+
* N.B. if you sent this option to true, and you publish a message on a topic
79+
* with no peers listening on that topic, no other network node will ever
80+
* receive the message.
81+
*/
82+
allowPublishToZeroTopicPeers?: boolean
7583
ignoreDuplicatePublishError?: boolean
7684
/** serialize message once and send to all peers without control messages */
7785
batchPublish?: boolean

test/2-nodes.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ describe('2 nodes', () => {
243243
// Create pubsub nodes
244244
beforeEach(async () => {
245245
mockNetwork.reset()
246-
nodes = await createComponentsArray({ number: 2, init: { allowPublishToZeroPeers: true } })
246+
nodes = await createComponentsArray({ number: 2, init: { allowPublishToZeroTopicPeers: true } })
247247
await connectAllPubSubNodes(nodes)
248248

249249
// Create subscriptions

test/compliance.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ describe.skip('interface compliance', function () {
2929
...args.init,
3030
// libp2p-interfaces-compliance-tests in test 'can subscribe and unsubscribe correctly' publishes to no peers
3131
// Disable check to allow passing tests
32-
allowPublishToZeroPeers: true
32+
allowPublishToZeroTopicPeers: true
3333
}
3434
)
3535

test/gossip.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ describe('gossip', () => {
2727
IPColocationFactorThreshold: GossipsubDhi + 3
2828
},
2929
maxInboundDataLength: 4000000,
30-
allowPublishToZeroPeers: false
30+
allowPublishToZeroTopicPeers: false
3131
}
3232
})
3333
})
@@ -89,7 +89,7 @@ describe('gossip', () => {
8989
const topic = 'Z'
9090

9191
const publishResult = await nodeA.pubsub.publish(topic, uint8ArrayFromString('hey'), {
92-
allowPublishToZeroPeers: true
92+
allowPublishToZeroTopicPeers: true
9393
})
9494

9595
// gossip happens during the heartbeat

0 commit comments

Comments
 (0)