Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,15 @@ export interface MeshPeer {
direction: Direction
}

export interface GossipIHave {
peerId: string
message: RPC.ControlIHave[]
}

export interface GossipsubEvents extends PubSubEvents {
'gossipsub:heartbeat': CustomEvent
'gossipsub:message': CustomEvent<GossipsubMessage>
'gossipsub:ihave': CustomEvent<GossipIHave>
'gossipsub:graft': CustomEvent<MeshPeer>
'gossipsub:prune': CustomEvent<MeshPeer>
}
Expand Down Expand Up @@ -1483,6 +1489,10 @@ export class GossipSub extends TypedEventEmitter<GossipsubEvents> implements Pub
return []
}

this.safeDispatchEvent<GossipIHave>('gossipsub:ihave', {
detail: { peerId: id, message: ihave }
})

const iasked = this.iasked.get(id) ?? 0
if (iasked >= constants.GossipsubMaxIHaveLength) {
this.log('IHAVE: peer %s has already advertised too many messages (%d); ignoring', id, iasked)
Expand Down
23 changes: 23 additions & 0 deletions test/gossip.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,29 @@ describe('gossip', () => {
nodeASpy.pushGossip.restore()
})

it('should dispatch ihave event when gossiping', async function () {
this.timeout(10e4)
const nodeA = nodes[0]
const topic = 'Z'

const subscriptionPromises = nodes.map(async (n) => pEvent(n.pubsub, 'subscription-change'))
nodes.forEach((n) => { n.pubsub.subscribe(topic) })

await connectAllPubSubNodes(nodes)
await Promise.all(subscriptionPromises)
await Promise.all(nodes.map(async (n) => pEvent(n.pubsub, 'gossipsub:heartbeat')))

// Check that nodeA has received IHAVE event
const ihavePromise = pEvent(nodeA.pubsub, 'gossipsub:ihave')

await nodeA.pubsub.publish(topic, uint8ArrayFromString('test_ihave'))

await pEvent(nodeA.pubsub, 'gossipsub:heartbeat')

const ihaveEvent = await ihavePromise
expect(ihaveEvent.detail.message[0].topicID).to.equal(topic)
})

it('should send idontwant to peers in topic', async function () {
// This test checks that idontwants and idontwantsCounts are correctly incrmemented
// - idontwantCounts should track the number of idontwant messages received from a peer for a single heartbeat
Expand Down
Loading