-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow only defined list of topics (#348)
* Allow only defined list of topics * Allow allowedTopics unit test
- Loading branch information
Showing
2 changed files
with
94 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { expect } from 'aegir/chai' | ||
import { GossipSub } from '../src/index.js' | ||
import { pEvent } from 'p-event' | ||
import { connectAllPubSubNodes, createComponentsArray } from './utils/create-pubsub.js' | ||
import { Components } from '@libp2p/components' | ||
import { stop } from '@libp2p/interfaces/startable' | ||
import { mockNetwork } from '@libp2p/interface-mocks' | ||
|
||
/* eslint-disable dot-notation */ | ||
describe('gossip / allowedTopics', () => { | ||
let nodes: Components[] | ||
|
||
const allowedTopic = 'topic_allowed' | ||
const notAllowedTopic = 'topic_not_allowed' | ||
const allowedTopics = [allowedTopic] | ||
const allTopics = [allowedTopic, notAllowedTopic] | ||
|
||
// Create pubsub nodes | ||
beforeEach(async () => { | ||
mockNetwork.reset() | ||
nodes = await createComponentsArray({ | ||
number: 2, | ||
connected: false, | ||
init: { | ||
allowedTopics | ||
} | ||
}) | ||
}) | ||
|
||
afterEach(async () => { | ||
await stop(...nodes) | ||
mockNetwork.reset() | ||
}) | ||
|
||
it('should send gossip to non-mesh peers in topic', async function () { | ||
this.timeout(10 * 1000) | ||
const [nodeA, nodeB] = nodes | ||
|
||
// add subscriptions to each node | ||
for (const topic of allTopics) { | ||
nodeA.getPubSub().subscribe(topic) | ||
} | ||
|
||
// every node connected to every other | ||
await Promise.all([ | ||
connectAllPubSubNodes(nodes), | ||
// nodeA should send nodeB all its subscriptions on connection | ||
pEvent(nodeB.getPubSub(), 'subscription-change') | ||
]) | ||
|
||
const nodeASubscriptions = Array.from((nodeA.getPubSub() as GossipSub)['subscriptions'].keys()) | ||
expect(nodeASubscriptions).deep.equals(allTopics, 'nodeA.subscriptions should be subcribed to all') | ||
|
||
const nodeBTopics = Array.from((nodeB.getPubSub() as GossipSub)['topics'].keys()) | ||
expect(nodeBTopics).deep.equals(allowedTopics, 'nodeB.topics should only contain allowedTopics') | ||
}) | ||
}) |