Skip to content

Commit 74cb495

Browse files
twoethsdapplion
andauthored
fix: sendRpc in a for loop (#347)
* fix: no shared param passed to sendRpc in a for loop * Review PR * Remove export Co-authored-by: dapplion <[email protected]>
1 parent 4c73e81 commit 74cb495

File tree

3 files changed

+32
-58
lines changed

3 files changed

+32
-58
lines changed

src/index.ts

Lines changed: 32 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { CustomEvent, EventEmitter } from '@libp2p/interfaces/events'
1010
import { MessageCache } from './message-cache.js'
1111
import { RPC, IRPC } from './message/rpc.js'
1212
import * as constants from './constants.js'
13-
import { createGossipRpc, shuffle, messageIdToString } from './utils/index.js'
13+
import { shuffle, messageIdToString } from './utils/index.js'
1414
import {
1515
PeerScore,
1616
PeerScoreParams,
@@ -1150,8 +1150,7 @@ export class GossipSub extends EventEmitter<GossipsubEvents> implements Initiali
11501150
*/
11511151
private sendSubscriptions(toPeer: PeerIdStr, topics: string[], subscribe: boolean): void {
11521152
this.sendRpc(toPeer, {
1153-
subscriptions: topics.map((topic) => ({ topic, subscribe })),
1154-
messages: []
1153+
subscriptions: topics.map((topic) => ({ topic, subscribe }))
11551154
})
11561155
}
11571156

@@ -1172,7 +1171,7 @@ export class GossipSub extends EventEmitter<GossipsubEvents> implements Initiali
11721171
return
11731172
}
11741173

1175-
this.sendRpc(id, createGossipRpc(ihave, { iwant, prune }))
1174+
this.sendRpc(id, { messages: ihave, control: { iwant, prune } })
11761175
}
11771176

11781177
/**
@@ -1915,10 +1914,9 @@ export class GossipSub extends EventEmitter<GossipsubEvents> implements Initiali
19151914
// Note: Don't throw if tosend is empty, we can have a mesh with a single peer
19161915

19171916
// forward the message to peers
1918-
const rpc = createGossipRpc([rawMsg])
19191917
tosend.forEach((id) => {
1920-
// self.send_message(*peer_id, event.clone())?;
1921-
this.sendRpc(id, rpc)
1918+
// sendRpc may mutate RPC message on piggyback, create a new message for each peer
1919+
this.sendRpc(id, { messages: [rawMsg] })
19221920
})
19231921

19241922
this.metrics?.onForwardMsg(rawMsg.topic, tosend.size)
@@ -1967,11 +1965,9 @@ export class GossipSub extends EventEmitter<GossipsubEvents> implements Initiali
19671965
this.publishedMessageIds.put(msgIdStr)
19681966

19691967
// Send to set of peers aggregated from direct, mesh, fanout
1970-
const rpc = createGossipRpc([rawMsg])
1971-
19721968
for (const id of tosend) {
1973-
// self.send_message(*peer_id, event.clone())?;
1974-
const sent = this.sendRpc(id, rpc)
1969+
// sendRpc may mutate RPC message on piggyback, create a new message for each peer
1970+
const sent = this.sendRpc(id, { messages: [rawMsg] })
19751971

19761972
// did not actually send the message
19771973
if (!sent) {
@@ -2072,8 +2068,7 @@ export class GossipSub extends EventEmitter<GossipsubEvents> implements Initiali
20722068
}
20732069
]
20742070

2075-
const out = createGossipRpc([], { graft })
2076-
this.sendRpc(id, out)
2071+
this.sendRpc(id, { control: { graft } })
20772072
}
20782073

20792074
/**
@@ -2082,8 +2077,7 @@ export class GossipSub extends EventEmitter<GossipsubEvents> implements Initiali
20822077
private async sendPrune(id: PeerIdStr, topic: string): Promise<void> {
20832078
const prune = [await this.makePrune(id, topic, this.opts.doPX)]
20842079

2085-
const out = createGossipRpc([], { prune })
2086-
this.sendRpc(id, out)
2080+
this.sendRpc(id, { control: { prune } })
20872081
}
20882082

20892083
/**
@@ -2132,30 +2126,32 @@ export class GossipSub extends EventEmitter<GossipsubEvents> implements Initiali
21322126
return true
21332127
}
21342128

2129+
/** Mutates `outRpc` adding graft and prune control messages */
21352130
public piggybackControl(id: PeerIdStr, outRpc: IRPC, ctrl: RPC.IControlMessage): void {
2136-
const tograft = (ctrl.graft || []).filter(({ topicID }) =>
2137-
((topicID && this.mesh.get(topicID)) || new Set()).has(id)
2138-
)
2139-
const toprune = (ctrl.prune || []).filter(
2140-
({ topicID }) => !((topicID && this.mesh.get(topicID)) || new Set()).has(id)
2141-
)
2142-
2143-
if (!tograft.length && !toprune.length) {
2144-
return
2131+
if (ctrl.graft) {
2132+
if (!outRpc.control) outRpc.control = {}
2133+
if (!outRpc.control.graft) outRpc.control.graft = []
2134+
for (const graft of ctrl.graft) {
2135+
if (graft.topicID && this.mesh.get(graft.topicID)?.has(id)) {
2136+
outRpc.control.graft.push(graft)
2137+
}
2138+
}
21452139
}
21462140

2147-
if (outRpc.control) {
2148-
outRpc.control.graft = outRpc.control.graft && outRpc.control.graft.concat(tograft)
2149-
outRpc.control.prune = outRpc.control.prune && outRpc.control.prune.concat(toprune)
2150-
} else {
2151-
outRpc.control = { graft: tograft, prune: toprune, ihave: [], iwant: [] }
2141+
if (ctrl.prune) {
2142+
if (!outRpc.control) outRpc.control = {}
2143+
if (!outRpc.control.prune) outRpc.control.prune = []
2144+
for (const prune of ctrl.prune) {
2145+
if (prune.topicID && this.mesh.get(prune.topicID)?.has(id)) {
2146+
outRpc.control.prune.push(prune)
2147+
}
2148+
}
21522149
}
21532150
}
21542151

2152+
/** Mutates `outRpc` adding ihave control messages */
21552153
private piggybackGossip(id: PeerIdStr, outRpc: IRPC, ihave: RPC.IControlIHave[]): void {
2156-
if (!outRpc.control) {
2157-
outRpc.control = { ihave: [], iwant: [], graft: [], prune: [] }
2158-
}
2154+
if (!outRpc.control) outRpc.control = {}
21592155
outRpc.control.ihave = ihave
21602156
}
21612157

@@ -2183,15 +2179,13 @@ export class GossipSub extends EventEmitter<GossipsubEvents> implements Initiali
21832179
toprune.delete(id)
21842180
}
21852181

2186-
const outRpc = createGossipRpc([], { graft, prune })
2187-
this.sendRpc(id, outRpc)
2182+
this.sendRpc(id, { control: { graft, prune } })
21882183
}
21892184
for (const [id, topics] of toprune) {
21902185
const prune = await Promise.all(
21912186
topics.map(async (topicID) => await this.makePrune(id, topicID, doPX && !(noPX.get(id) ?? false)))
21922187
)
2193-
const outRpc = createGossipRpc([], { prune })
2194-
this.sendRpc(id, outRpc)
2188+
this.sendRpc(id, { control: { prune } })
21952189
}
21962190
}
21972191

@@ -2264,12 +2258,12 @@ export class GossipSub extends EventEmitter<GossipsubEvents> implements Initiali
22642258
// send gossip first, which will also piggyback control
22652259
for (const [peer, ihave] of this.gossip.entries()) {
22662260
this.gossip.delete(peer)
2267-
this.sendRpc(peer, createGossipRpc([], { ihave }))
2261+
this.sendRpc(peer, { control: { ihave } })
22682262
}
22692263
// send the remaining control messages
22702264
for (const [peer, control] of this.control.entries()) {
22712265
this.control.delete(peer)
2272-
this.sendRpc(peer, createGossipRpc([], { graft: control.graft, prune: control.prune }))
2266+
this.sendRpc(peer, { control: { graft: control.graft, prune: control.prune } })
22732267
}
22742268
}
22752269

src/utils/create-gossip-rpc.ts

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/utils/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
export * from './create-gossip-rpc.js'
21
export * from './shuffle.js'
32
export * from './messageIdToString.js'
43
export { getPublishConfigFromPeerId } from './publishConfig.js'

0 commit comments

Comments
 (0)