-
Notifications
You must be signed in to change notification settings - Fork 42
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
base: master
Are you sure you want to change the base?
Conversation
size-limit report 📦
|
…s, add getPeers for IWaku
There was a problem hiding this 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"; |
There was a problem hiding this comment.
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[]> { |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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?
return peers | ||
.filter((p) => !!p) | ||
.filter((p) => (codec ? (p as Peer).protocols.includes(codec) : true)) | ||
.sort((left, right) => getPeerPing(left) - getPeerPing(right)) as Peer[]; |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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[]>; |
There was a problem hiding this comment.
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)
} as unknown as ConnectionManager, | ||
{} as unknown as PeerManager, | ||
options.libp2p |
There was a problem hiding this comment.
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", () => { |
There was a problem hiding this comment.
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"; |
There was a problem hiding this comment.
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]; |
There was a problem hiding this comment.
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
this.addLibp2pEventListener("peer:disconnect", (evt) => { | ||
const peerId = evt.detail; | ||
if (this.getPeers().some((p) => p.id.equals(peerId))) { | ||
void this.renewAndSubscribePeer(peerId); | ||
} | ||
}); | ||
} |
There was a problem hiding this comment.
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?
Problem
TBD
Solution
TBD
Notes