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: improve peer manager and re-integrate to light push #2191

Draft
wants to merge 13 commits into
base: master
Choose a base branch
from

Conversation

weboko
Copy link
Collaborator

@weboko weboko commented Oct 20, 2024

Copy link

github-actions bot commented Oct 20, 2024

size-limit report 📦

Path Size Loading time (3g) Running time (snapdragon) Total time
Waku node 84.53 KB (-2.17% 🔽) 1.7 s (-2.17% 🔽) 2.7 s (-21.9% 🔽) 4.4 s
Waku Simple Light Node 134.9 KB (-1.5% 🔽) 2.7 s (-1.5% 🔽) 3 s (-14.09% 🔽) 5.7 s
ECIES encryption 22.88 KB (0%) 458 ms (0%) 1.2 s (+1.39% 🔺) 1.7 s
Symmetric encryption 22.37 KB (0%) 448 ms (0%) 923 ms (-21.12% 🔽) 1.4 s
DNS discovery 70.49 KB (-0.17% 🔽) 1.5 s (-0.17% 🔽) 1.8 s (-35.1% 🔽) 3.2 s
Peer Exchange discovery 71.47 KB (-0.53% 🔽) 1.5 s (-0.53% 🔽) 1.8 s (-18.62% 🔽) 3.2 s
Local Peer Cache Discovery 65.08 KB (-0.13% 🔽) 1.4 s (-0.13% 🔽) 2 s (-11.12% 🔽) 3.3 s
Privacy preserving protocols 76.14 KB (+0.22% 🔺) 1.6 s (+0.22% 🔺) 2.7 s (+82.67% 🔺) 4.2 s
Waku Filter 78.46 KB (-3.08% 🔽) 1.6 s (-3.08% 🔽) 2.4 s (+2.51% 🔺) 3.9 s
Waku LightPush 75.15 KB (-0.7% 🔽) 1.6 s (-0.7% 🔽) 3 s (-1.79% 🔽) 4.5 s
History retrieval protocols 75.25 KB (-3.17% 🔽) 1.6 s (-3.17% 🔽) 2.3 s (-9.96% 🔽) 3.8 s
Deterministic Message Hashing 7.39 KB (0%) 148 ms (0%) 533 ms (+182.67% 🔺) 681 ms

Copy link
Collaborator

@danisharora099 danisharora099 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice PR! 🚀

Lots happening here.
A few pointers from my side:

  • Are we planning to address the TODOs part of this PR, or follow up?
  • Do we need to introduce newer tests as we delete the functionalities and respective spec tests?
  • How does the peer management work with this PR, considering the overhaul of BaseProtocol, PeerManager and ConnectionManager? Is it stable?


export { getHealthManager } from "./lib/health_manager.js";

export { KeepAliveManager } from "./lib/keep_alive_manager.js";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are we deleting the export for KeepAliveManager?

return getPeersForProtocol(this.components.peerStore, [this.multicodec]);
}

public async connectedPeers(): Promise<Peer[]> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still have access to this utility function from elsewhere?
Basically getting the connected peers for a particular protocol

relay?: IRelay,
options?: Partial<ConnectionManagerOptions>
) {
public constructor(options: ConnectionManagerConstructorOptions) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

confirming that we are shifting out of the singleton pattern?

Comment on lines +201 to +204
return peers
.filter((p) => !!p)
.filter((p) => (codec ? (p as Peer).protocols.includes(codec) : true))
.sort((left, right) => getPeerPing(left) - getPeerPing(right)) as Peer[];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should probably abstract this logic into a private helper to differentiate functionalities, something like:

return filterPeersByProtocols(peers).sortPeersByLatency

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reference:

public async getPeers(
    {
      numPeers,
      maxBootstrapPeers
    }: {
      numPeers: number;
      maxBootstrapPeers: number;
    } = {
      maxBootstrapPeers: 0,
      numPeers: 0
    }
  ): Promise<Peer[]> {
    // Retrieve all connected peers that support the protocol & shard (if configured)
    const allAvailableConnectedPeers = await this.connectedPeers();

    // Filter the peers based on discovery & number of peers requested
    const filteredPeers = filterPeersByDiscovery(
      allAvailableConnectedPeers,
      numPeers,
      maxBootstrapPeers
    );

    // Sort the peers by latency
    const sortedFilteredPeers = await sortPeersByLatency(
      this.components.peerStore,
      filteredPeers
    );

    if (sortedFilteredPeers.length === 0) {
      this.log.warn(
        "No peers found. Ensure you have a connection to the network."
      );
    }

    if (sortedFilteredPeers.length < numPeers) {
      this.log.warn(
        `Only ${sortedFilteredPeers.length} peers found. Requested ${numPeers}.`
      );
    }

    return sortedFilteredPeers;
  }

@@ -63,7 +85,8 @@ export interface IConnectionStateEvents {

export interface IConnectionManager
extends TypedEventEmitter<IPeersByDiscoveryEvents & IConnectionStateEvents> {
configuredPubsubTopics: PubsubTopic[];
pubsubTopics: PubsubTopic[];
getConnectedPeers(codec?: string): Promise<Peer[]>;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's use the Protocols enum instead of the codec string here -- easier way to get information from the function without expecting the knowledge of exact codec (we already export the Protocols enum)

Comment on lines +161 to +163
} as unknown as ConnectionManager,
{} as unknown as PeerManager,
options.libp2p
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why doesn't typescript comply here/why does it expect an unknown typecase? :(


import { PeerManager } from "./peer_manager.js";

describe("PeerManager", () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we add new tests for the PeerManager?

import { Logger } from "@waku/utils";
import { Mutex } from "async-mutex";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

were you able to validate the usage of manual locking instead of using the mutexes lib works well?

})
)[0];
const peers = await this.peerManager.getPeers();
const peer = peers[0];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: let's add a comment here that store only uses one peer

Comment on lines -42 to -48
this.addLibp2pEventListener("peer:disconnect", (evt) => {
const peerId = evt.detail;
if (this.getPeers().some((p) => p.id.equals(peerId))) {
void this.renewAndSubscribePeer(peerId);
}
});
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

apologies if i missed it, how would renewal happen on a peer disconnection now?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat: improve peer manager and re-integrate to light push
2 participants