Skip to content

Commit

Permalink
feat: added allowPublishToZeroPeers as optional param to publish func…
Browse files Browse the repository at this point in the history
…tion (#395)

* feat: added Publish config as optional params (#367)

* test: added test for allowPublishZeroPeers param in publish function
  • Loading branch information
maschad authored Jan 13, 2023
1 parent 41740b5 commit e7c88ac
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
10 changes: 7 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ import {
DataTransform,
rejectReasonFromAcceptance,
MsgIdToStrFn,
MessageId
MessageId,
PublishOpts
} from './types.js'
import { buildRawMessage, validateToRawMessage } from './utils/buildRawMessage.js'
import { msgIdFnStrictNoSign, msgIdFnStrictSign } from './utils/msgIdFn.js'
Expand Down Expand Up @@ -1995,7 +1996,7 @@ export class GossipSub extends EventEmitter<GossipsubEvents> implements PubSub<G
*
* For messages not from us, this class uses `forwardMessage`.
*/
async publish(topic: TopicStr, data: Uint8Array): Promise<PublishResult> {
async publish(topic: TopicStr, data: Uint8Array, opts?: PublishOpts): Promise<PublishResult> {
const transformedData = this.dataTransform ? this.dataTransform.outboundTransform(topic, data) : data

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

if (tosend.size === 0 && !this.opts.allowPublishToZeroPeers && !willSendToSelf) {
// Current publish opt takes precedence global opts, while preserving false value
const allowPublishToZeroPeers = opts?.allowPublishToZeroPeers ?? this.opts.allowPublishToZeroPeers

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

Expand Down
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ export enum SignaturePolicy {
StrictNoSign = 'StrictNoSign'
}

export type PublishOpts = {
allowPublishToZeroPeers?: boolean
}

export enum PublishConfigType {
Signing,
Anonymous
Expand Down
19 changes: 18 additions & 1 deletion test/gossip.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ describe('gossip', () => {
scoreParams: {
IPColocationFactorThreshold: GossipsubDhi + 3
},
maxInboundDataLength: 4000000
maxInboundDataLength: 4000000,
allowPublishToZeroPeers: false
}
})
})
Expand Down Expand Up @@ -82,6 +83,22 @@ describe('gossip', () => {
nodeASpy.pushGossip.restore()
})

it('Should allow publishing to zero peers if flag is passed', async function () {
this.timeout(10e4)
const nodeA = nodes[0]
const topic = 'Z'

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

// gossip happens during the heartbeat
await pEvent(nodeA.pubsub, 'gossipsub:heartbeat')

// should have sent message to peerB
expect(publishResult.recipients).to.deep.equal([])
})

it('should reject incoming messages bigger than maxInboundDataLength limit', async function () {
this.timeout(10e4)
const nodeA = nodes[0]
Expand Down

0 comments on commit e7c88ac

Please sign in to comment.