Skip to content

Commit

Permalink
fix: only emit event when there are topic peers (#129)
Browse files Browse the repository at this point in the history
To prevent gossipsub needing to be configured to publish messages
that will go no-where, check that there are topic peers before
publishing a message.
  • Loading branch information
achingbrain authored Dec 4, 2023
1 parent 1359e6e commit dfe4fed
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,12 @@
"@libp2p/interface-compliance-tests": "^5.0.5",
"@libp2p/logger": "^4.0.1",
"@libp2p/peer-id-factory": "^4.0.0",
"@types/sinon": "^17.0.2",
"aegir": "^41.0.5",
"p-defer": "^4.0.0",
"p-wait-for": "^5.0.0",
"protons": "^7.0.2",
"sinon": "^17.0.0",
"ts-sinon": "^2.0.2"
"sinon-ts": "^2.0.0"
}
}
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,11 @@ export class PubSubPeerDiscovery extends TypedEventEmitter<PeerDiscoveryEvents>
}

for (const topic of this.topics) {
if (pubsub.getSubscribers(topic).length === 0) {
this.log('skipping broadcasting our peer data on topic %s because there are no peers present', topic)
continue
}

this.log('broadcasting our peer data on topic %s', topic)
void pubsub.publish(topic, encodedPeer)
}
Expand Down
15 changes: 11 additions & 4 deletions test/compliance.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import tests from '@libp2p/interface-compliance-tests/peer-discovery'
import { defaultLogger } from '@libp2p/logger'
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
import { multiaddr } from '@multiformats/multiaddr'
import { stubInterface } from 'ts-sinon'
import { stubInterface } from 'sinon-ts'
import { pubsubPeerDiscovery, TOPIC } from '../src/index.js'
import { Peer as PBPeer } from '../src/peer.js'
import type { PubSub } from '@libp2p/interface'
Expand All @@ -17,6 +17,7 @@ describe('compliance tests', () => {
tests({
async setup () {
const peerId = await createEd25519PeerId()
const subscriber = await createEd25519PeerId()
await new Promise(resolve => setTimeout(resolve, 10))

const addressManager = stubInterface<AddressManager>()
Expand All @@ -25,15 +26,21 @@ describe('compliance tests', () => {
])

const pubsubDiscovery = pubsubPeerDiscovery()({
pubsub: stubInterface<PubSub>(),
peerId: await createEd25519PeerId(),
pubsub: stubInterface<PubSub>({
getSubscribers: () => {
return [
subscriber
]
}
}),
peerId,
addressManager,
logger: defaultLogger()
})

intervalId = setInterval(() => {
const peer = PBPeer.encode({
publicKey: peerId.publicKey,
publicKey: subscriber.publicKey,
addrs: [
multiaddr('/ip4/166.10.1.2/tcp/80').bytes
]
Expand Down
11 changes: 9 additions & 2 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { expect } from 'aegir/chai'
import defer from 'p-defer'
import pWaitFor from 'p-wait-for'
import sinon from 'sinon'
import { type StubbedInstance, stubInterface } from 'ts-sinon'
import { type StubbedInstance, stubInterface } from 'sinon-ts'
import { pubsubPeerDiscovery, type PubSubPeerDiscoveryComponents, TOPIC } from '../src/index.js'
import * as PB from '../src/peer.js'
import type { PeerDiscovery, PeerInfo, PubSub } from '@libp2p/interface'
Expand All @@ -24,8 +24,15 @@ describe('PubSub Peer Discovery', () => {

beforeEach(async () => {
const peerId = await createEd25519PeerId()
const subscriber = await createEd25519PeerId()

mockPubsub = stubInterface<PubSub>()
mockPubsub = stubInterface<PubSub>({
getSubscribers: () => {
return [
subscriber
]
}
})

const addressManager = stubInterface<AddressManager>()
addressManager.getAddresses.returns([
Expand Down

0 comments on commit dfe4fed

Please sign in to comment.