From e7c88acb83d6b7f25f38074f37668e17860dd518 Mon Sep 17 00:00:00 2001 From: Chad Nehemiah Date: Fri, 13 Jan 2023 15:32:14 -0500 Subject: [PATCH] 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 --- src/index.ts | 10 +++++++--- src/types.ts | 4 ++++ test/gossip.spec.ts | 19 ++++++++++++++++++- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index b2d81fbf..3a9bb99a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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' @@ -1995,7 +1996,7 @@ export class GossipSub extends EventEmitter implements PubSub { + async publish(topic: TopicStr, data: Uint8Array, opts?: PublishOpts): Promise { const transformedData = this.dataTransform ? this.dataTransform.outboundTransform(topic, data) : data if (this.publishConfig == null) { @@ -2018,7 +2019,10 @@ export class GossipSub extends EventEmitter implements PubSub { scoreParams: { IPColocationFactorThreshold: GossipsubDhi + 3 }, - maxInboundDataLength: 4000000 + maxInboundDataLength: 4000000, + allowPublishToZeroPeers: false } }) }) @@ -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]