Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: graft/prune events and mesh peer tagging #383

Merged
merged 17 commits into from
Feb 27, 2024
Merged
Changes from 5 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
49 changes: 45 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@
* Limits to bound protobuf decoding
*/
decodeRpcLimits?: DecodeRPCLimits

/**
* If true, will utilize the libp2p connection manager tagging system to prune/graft connections to peers, defaults to false
*/
taggingEnabled?: boolean
maschad marked this conversation as resolved.
Show resolved Hide resolved
}

export interface GossipsubMessage {
Expand Down Expand Up @@ -1466,7 +1471,7 @@
const now = Date.now()
let doPX = this.opts.doPX

graft.forEach(({ topicID }) => {
graft.forEach(async ({ topicID }) => {
maschad marked this conversation as resolved.
Show resolved Hide resolved
if (!topicID) {
return
}
Expand Down Expand Up @@ -1540,6 +1545,16 @@
peersInMesh.add(id)

this.metrics?.onAddToMesh(topicID, InclusionReason.Subscribed, 1)

if (this.opts?.taggingEnabled ?? false) {
await this.components.peerStore.merge(peerIdFromString(id), {
tags: {
[topicID]: {
value: 100 // value should be 0-100
}
}
})
}
})

if (!prune.length) {
Expand Down Expand Up @@ -1594,6 +1609,14 @@
}
await this.pxConnect(peers)
}

if (this.opts?.taggingEnabled ?? false) {
await this.components.peerStore.save(peerIdFromString(id), {
tags: {
[topicID]: {}
}
})
}
}
}

Expand Down Expand Up @@ -1832,9 +1855,9 @@

this.mesh.set(topic, toAdd)

toAdd.forEach((id) => {
toAdd.forEach(async (id) => {
this.log('JOIN: Add mesh link to %s in %s', id, topic)
this.sendGraft(id, topic)
await this.sendGraft(id, topic)

// rust-libp2p
// - peer_score.graft()
Expand Down Expand Up @@ -2190,14 +2213,24 @@
/**
* Sends a GRAFT message to a peer
*/
private sendGraft(id: PeerIdStr, topic: string): void {
private async sendGraft(id: PeerIdStr, topic: string): Promise<void> {
const graft = [
{
topicID: topic
}
]

this.sendRpc(id, { control: { graft } })

if (this.opts?.taggingEnabled ?? false) {
await this.components.peerStore.merge(peerIdFromString(id), {
tags: {
[topic]: {
value: 100 // value should be 0-100
}
}
})
}
}

/**
Expand All @@ -2209,6 +2242,14 @@
const prune = [await this.makePrune(id, topic, this.opts.doPX, onUnsubscribe)]

this.sendRpc(id, { control: { prune } })

if (this.opts.taggingEnabled ?? false) {
await this.components.peerStore.save(peerIdFromString(id), {
tags: {
[topic]: {}
}
})
}
}

/**
Expand Down Expand Up @@ -2459,7 +2500,7 @@

try {
peerInfo = await this.components.peerStore.get(id)
} catch (err: any) {

Check warning on line 2503 in src/index.ts

View workflow job for this annotation

GitHub Actions / check

Unexpected any. Specify a different type
if (err.code !== 'ERR_NOT_FOUND') {
throw err
}
Expand Down
Loading