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

Tag relevant peer #4997

Merged
merged 2 commits into from
Jan 15, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 5 additions & 2 deletions packages/beacon-node/src/network/nodejs/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,11 @@ export async function createNodejsLibp2p(options: ILibp2pOptions): Promise<Libp2
dialTimeout: 30_000,

autoDial: false,
// This is the default value, we reject connections based on tcp() option above so this is not important
maxConnections: Infinity,
// DOCS: the maximum number of connections libp2p is willing to have before it starts disconnecting.
// If ConnectionManager.size > maxConnections calls _maybeDisconnectOne() which will sort peers disconnect
// the one with the least `_peerValues`. That's a custom peer generalized score that's not used, so it always
// has the same value in current Lodestar usage.
maxConnections: options.maxConnections,
// DOCS: the minimum number of connections below which libp2p not activate preemptive disconnections.
// If ConnectionManager.size < minConnections, it won't prune peers in _maybeDisconnectOne(). If autoDial is
// off it doesn't have any effect in behaviour.
Expand Down
19 changes: 18 additions & 1 deletion packages/beacon-node/src/network/peers/peerManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ const STATUS_INBOUND_GRACE_PERIOD = 15 * 1000;
const CHECK_PING_STATUS_INTERVAL = 10 * 1000;
/** A peer is considered long connection if it's >= 1 day */
const LONG_PEER_CONNECTION_MS = 24 * 60 * 60 * 1000;
/**
* Tag peer when it's relevant and connecting to our node.
* When node has > maxPeer (55), libp2p randomly prune peers if we don't tag peers in use.
* See https://github.com/ChainSafe/lodestar/issues/4623#issuecomment-1374447934
**/
const PEER_RELEVANT_TAG = "relevant";
/** Tag value of PEER_RELEVANT_TAG */
const PEER_RELEVANT_TAG_VALUE = 100;

/**
* Relative factor of peers that are allowed to have a negative gossipsub score without penalizing them in lodestar.
Expand Down Expand Up @@ -329,7 +337,13 @@ export class PeerManager {
// Peer is usable, send it to the rangeSync
// NOTE: Peer may not be connected anymore at this point, potential race condition
// libp2p.connectionManager.get() returns not null if there's +1 open connections with `peer`
if (peerData) peerData.relevantStatus = RelevantPeerStatus.relevant;
if (peerData && peerData.relevantStatus !== RelevantPeerStatus.relevant) {
this.libp2p.peerStore
// ttl = undefined means it's never expired
.tagPeer(peer, PEER_RELEVANT_TAG, {ttl: undefined, value: PEER_RELEVANT_TAG_VALUE})
.catch((e) => this.logger.verbose("cannot tag peer", {peerId: peer.toString()}, e as Error));
peerData.relevantStatus = RelevantPeerStatus.relevant;
}
if (getConnection(this.libp2p.connectionManager, peer.toString())) {
this.networkEventBus.emit(NetworkEvent.peerConnected, peer, status);
}
Expand Down Expand Up @@ -580,6 +594,9 @@ export class PeerManager {
this.logger.verbose("peer disconnected", {peer: prettyPrintPeerId(peer), direction, status});
this.networkEventBus.emit(NetworkEvent.peerDisconnected, peer);
this.metrics?.peerDisconnectedEvent.inc({direction});
this.libp2p.peerStore
.unTagPeer(peer, PEER_RELEVANT_TAG)
.catch((e) => this.logger.verbose("cannot untag peer", {peerId: peer.toString()}, e as Error));
};

private async disconnect(peer: PeerId): Promise<void> {
Expand Down