Skip to content

Commit e7c88ac

Browse files
authored
feat: added allowPublishToZeroPeers as optional param to publish function (#395)
* feat: added Publish config as optional params (#367) * test: added test for allowPublishZeroPeers param in publish function
1 parent 41740b5 commit e7c88ac

3 files changed

Lines changed: 29 additions & 4 deletions

File tree

src/index.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ import {
5252
DataTransform,
5353
rejectReasonFromAcceptance,
5454
MsgIdToStrFn,
55-
MessageId
55+
MessageId,
56+
PublishOpts
5657
} from './types.js'
5758
import { buildRawMessage, validateToRawMessage } from './utils/buildRawMessage.js'
5859
import { msgIdFnStrictNoSign, msgIdFnStrictSign } from './utils/msgIdFn.js'
@@ -1995,7 +1996,7 @@ export class GossipSub extends EventEmitter<GossipsubEvents> implements PubSub<G
19951996
*
19961997
* For messages not from us, this class uses `forwardMessage`.
19971998
*/
1998-
async publish(topic: TopicStr, data: Uint8Array): Promise<PublishResult> {
1999+
async publish(topic: TopicStr, data: Uint8Array, opts?: PublishOpts): Promise<PublishResult> {
19992000
const transformedData = this.dataTransform ? this.dataTransform.outboundTransform(topic, data) : data
20002001

20012002
if (this.publishConfig == null) {
@@ -2018,7 +2019,10 @@ export class GossipSub extends EventEmitter<GossipsubEvents> implements PubSub<G
20182019
const { tosend, tosendCount } = this.selectPeersToPublish(topic)
20192020
const willSendToSelf = this.opts.emitSelf === true && this.subscriptions.has(topic)
20202021

2021-
if (tosend.size === 0 && !this.opts.allowPublishToZeroPeers && !willSendToSelf) {
2022+
// Current publish opt takes precedence global opts, while preserving false value
2023+
const allowPublishToZeroPeers = opts?.allowPublishToZeroPeers ?? this.opts.allowPublishToZeroPeers
2024+
2025+
if (tosend.size === 0 && !allowPublishToZeroPeers && !willSendToSelf) {
20222026
throw Error('PublishError.InsufficientPeers')
20232027
}
20242028

src/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ export enum SignaturePolicy {
7272
StrictNoSign = 'StrictNoSign'
7373
}
7474

75+
export type PublishOpts = {
76+
allowPublishToZeroPeers?: boolean
77+
}
78+
7579
export enum PublishConfigType {
7680
Signing,
7781
Anonymous

test/gossip.spec.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ describe('gossip', () => {
2626
scoreParams: {
2727
IPColocationFactorThreshold: GossipsubDhi + 3
2828
},
29-
maxInboundDataLength: 4000000
29+
maxInboundDataLength: 4000000,
30+
allowPublishToZeroPeers: false
3031
}
3132
})
3233
})
@@ -82,6 +83,22 @@ describe('gossip', () => {
8283
nodeASpy.pushGossip.restore()
8384
})
8485

86+
it('Should allow publishing to zero peers if flag is passed', async function () {
87+
this.timeout(10e4)
88+
const nodeA = nodes[0]
89+
const topic = 'Z'
90+
91+
const publishResult = await nodeA.pubsub.publish(topic, uint8ArrayFromString('hey'), {
92+
allowPublishToZeroPeers: true
93+
})
94+
95+
// gossip happens during the heartbeat
96+
await pEvent(nodeA.pubsub, 'gossipsub:heartbeat')
97+
98+
// should have sent message to peerB
99+
expect(publishResult.recipients).to.deep.equal([])
100+
})
101+
85102
it('should reject incoming messages bigger than maxInboundDataLength limit', async function () {
86103
this.timeout(10e4)
87104
const nodeA = nodes[0]

0 commit comments

Comments
 (0)