Skip to content

Commit b7e832b

Browse files
committed
test: ensure the gossipsub-mesh-peer is added upon grafting, and removed at pruning
1 parent 814eebb commit b7e832b

File tree

1 file changed

+81
-1
lines changed

1 file changed

+81
-1
lines changed

test/gossip.spec.ts

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { mockNetwork } from '@libp2p/interface-mocks'
1010
import { stubInterface } from 'ts-sinon'
1111
import { Registrar } from '@libp2p/interface-registrar'
1212
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
13-
import { PeerStore } from '@libp2p/interface-peer-store'
13+
import { PeerStore, Tag } from '@libp2p/interface-peer-store'
1414
import { ConnectionManager } from '@libp2p/interface-connection-manager'
1515

1616
describe('gossip', () => {
@@ -80,6 +80,86 @@ describe('gossip', () => {
8080
nodeASpy.pushGossip.restore()
8181
})
8282

83+
it('should add the tag gossipsub-mesh-peer upon grafting', async function () {
84+
this.timeout(10e4)
85+
const topic = 'Z'
86+
// add subscriptions to each node
87+
nodes.forEach((n) => n.pubsub.subscribe(topic))
88+
89+
// every node connected to every other
90+
await connectAllPubSubNodes(nodes)
91+
92+
// wait for subscriptions to be transmitted
93+
await Promise.all(nodes.map(async (n) => await pEvent(n.pubsub, 'subscription-change')))
94+
95+
// await mesh rebalancing
96+
await Promise.all(nodes.map(async (n) => await pEvent(n.pubsub, 'gossipsub:heartbeat')))
97+
98+
// const allTags: Tag[][] = []
99+
100+
// nodes.forEach(async (subscribedPeer) => {
101+
// allTags.push(await subscribedPeer.components.peerStore.getTags(subscribedPeer.components.peerId))
102+
// })
103+
104+
// // gossip happens during the heartbeat
105+
// await pEvent(nodes[nodes.length - 1].pubsub, 'gossipsub:heartbeat')
106+
107+
// expect(
108+
// allTags.map((tags) => {
109+
// tags.map((tag) => {
110+
// tag.name.toString()
111+
// })
112+
// })
113+
// ).to.include('gossipsub-mesh-peer')
114+
115+
await pEvent(nodes[nodes.length - 1].pubsub, 'gossipsub:heartbeat')
116+
117+
expect(
118+
(await nodes[0].components.peerStore.getTags(nodes[0].components.peerId)).map((tag) => tag.name.toString())
119+
).to.include('gossipsub-mesh-peer')
120+
})
121+
122+
it('should remove the tag gossipsub-mesh-peer upon pruning', async function () {
123+
this.timeout(10e4)
124+
const topic = 'Z'
125+
// add subscriptions to each node
126+
nodes.forEach((n) => n.pubsub.subscribe(topic))
127+
128+
// every node connected to every other
129+
await connectAllPubSubNodes(nodes)
130+
131+
// wait for subscriptions to be transmitted
132+
await Promise.all(nodes.map(async (n) => await pEvent(n.pubsub, 'subscription-change')))
133+
134+
// disconnect some peers from the mesh to get some PRUNEs and the store the disconnected peers
135+
const unsubscribedPeers: GossipSubAndComponents[] = []
136+
137+
nodes.slice(0, 5).forEach((ps) => {
138+
ps.pubsub.unsubscribe(topic)
139+
unsubscribedPeers.push(ps)
140+
})
141+
142+
// await mesh rebalancing
143+
await Promise.all(nodes.map(async (n) => await pEvent(n.pubsub, 'gossipsub:heartbeat')))
144+
145+
const allTags: Tag[][] = []
146+
147+
unsubscribedPeers.forEach(async (unsubscribedPeer) => {
148+
allTags.push(await unsubscribedPeer.components.peerStore.getTags(unsubscribedPeer.components.peerId))
149+
})
150+
151+
// gossip happens during the heartbeat
152+
await pEvent(unsubscribedPeers[4].pubsub, 'gossipsub:heartbeat')
153+
154+
expect(
155+
allTags.map((tags) => {
156+
tags.map((tag) => {
157+
tag.name.toString()
158+
})
159+
})
160+
).to.not.include('gossipsub-mesh-peer')
161+
})
162+
83163
it('should reject incoming messages bigger than maxInboundDataLength limit', async function () {
84164
this.timeout(10e4)
85165
const nodeA = nodes[0]

0 commit comments

Comments
 (0)