From b5c6043202aae1e7662ac7678cdf2572af4ed686 Mon Sep 17 00:00:00 2001 From: twoeths Date: Mon, 14 Oct 2024 20:05:40 +0700 Subject: [PATCH] fix: queued attestations metric (#7158) --- .../beacon-node/src/metrics/metrics/beacon.ts | 2 +- .../fork-choice/src/forkChoice/forkChoice.ts | 17 ++++++++++++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/packages/beacon-node/src/metrics/metrics/beacon.ts b/packages/beacon-node/src/metrics/metrics/beacon.ts index 1737a5a2468f..685fd56674ad 100644 --- a/packages/beacon-node/src/metrics/metrics/beacon.ts +++ b/packages/beacon-node/src/metrics/metrics/beacon.ts @@ -94,7 +94,7 @@ export function createBeaconMetrics(register: RegistryMetricCreator) { }), queuedAttestations: register.gauge({ name: "beacon_fork_choice_queued_attestations_count", - help: "Current count of queued_attestations in fork choice data structures", + help: "Count of queued_attestations in fork choice per slot", }), validatedAttestationDatas: register.gauge({ name: "beacon_fork_choice_validated_attestation_datas_count", diff --git a/packages/fork-choice/src/forkChoice/forkChoice.ts b/packages/fork-choice/src/forkChoice/forkChoice.ts index 6ca3fd3a183a..1084ec8b8e00 100644 --- a/packages/fork-choice/src/forkChoice/forkChoice.ts +++ b/packages/fork-choice/src/forkChoice/forkChoice.ts @@ -94,6 +94,12 @@ export class ForkChoice implements IForkChoice { () => new MapDef(() => new Set()) ); + /** + * It's inconsistent to count number of queued attestations at different intervals of slot. + * Instead of that, we count number of queued attestations at the previous slot. + */ + private queuedAttestationsPreviousSlot = 0; + // Note: as of Jun 2022 Lodestar metrics show that 100% of the times updateHead() is called, synced = false. // Because we are processing attestations from gossip, recomputing scores is always necessary // /** Avoid having to compute deltas all the times. */ @@ -129,13 +135,9 @@ export class ForkChoice implements IForkChoice { } getMetrics(): ForkChoiceMetrics { - let numAttestations = 0; - for (const indicesByRoot of this.queuedAttestations.values()) { - numAttestations += Array.from(indicesByRoot.values()).reduce((acc, indices) => acc + indices.size, 0); - } return { votes: this.votes.length, - queuedAttestations: numAttestations, + queuedAttestations: this.queuedAttestationsPreviousSlot, validatedAttestationDatas: this.validatedAttestationDatas.size, balancesLength: this.balances.length, nodes: this.protoArray.nodes.length, @@ -771,6 +773,7 @@ export class ForkChoice implements IForkChoice { this.onTick(previousSlot + 1); } + this.queuedAttestationsPreviousSlot = 0; // Process any attestations that might now be eligible. this.processAttestationQueue(); this.validatedAttestationDatas = new Set(); @@ -1373,6 +1376,10 @@ export class ForkChoice implements IForkChoice { // equivocatingIndices was checked in onAttestation this.addLatestMessage(validatorIndex, targetEpoch, blockRootHex); } + + if (slot === currentSlot - 1) { + this.queuedAttestationsPreviousSlot += validatorIndices.size; + } } } else { break;