From 5928a3f5db49b61a65b2fbc5914b22d8910d3497 Mon Sep 17 00:00:00 2001 From: Santiago Palladino Date: Thu, 5 Mar 2026 15:32:49 -0300 Subject: [PATCH] fix(p2p): report most severe failure in runValidations instead of first by insertion order Co-Authored-By: Claude Opus 4.6 --- yarn-project/p2p/src/services/libp2p/libp2p_service.ts | 8 ++++++-- yarn-project/stdlib/src/p2p/peer_error.ts | 7 +++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/yarn-project/p2p/src/services/libp2p/libp2p_service.ts b/yarn-project/p2p/src/services/libp2p/libp2p_service.ts index 2a81e7a1350d..d467d60ae734 100644 --- a/yarn-project/p2p/src/services/libp2p/libp2p_service.ts +++ b/yarn-project/p2p/src/services/libp2p/libp2p_service.ts @@ -1,5 +1,6 @@ import type { EpochCacheInterface } from '@aztec/epoch-cache'; import { BlockNumber, type SlotNumber } from '@aztec/foundation/branded-types'; +import { maxBy } from '@aztec/foundation/collection'; import { Fr } from '@aztec/foundation/curves/bn254'; import { type Logger, createLibp2pComponentLogger, createLogger } from '@aztec/foundation/log'; import { RunningPromise } from '@aztec/foundation/running-promise'; @@ -19,6 +20,7 @@ import { P2PMessage, type ValidationResult as P2PValidationResult, PeerErrorSeverity, + PeerErrorSeverityByHarshness, TopicType, createTopicString, getTopicsForConfig, @@ -1662,8 +1664,10 @@ export class LibP2PService extends WithTracer implements P2PService { // A promise that resolves when all validations have been run const allValidations = await Promise.all(validationPromises); - const failed = allValidations.find(x => !x.isValid); - if (failed) { + const failures = allValidations.filter(x => !x.isValid); + if (failures.length > 0) { + // Pick the most severe failure (lowest tolerance = harshest penalty) + const failed = maxBy(failures, f => PeerErrorSeverityByHarshness.indexOf(f.severity))!; return { allPassed: false, failure: { diff --git a/yarn-project/stdlib/src/p2p/peer_error.ts b/yarn-project/stdlib/src/p2p/peer_error.ts index 73be41ebada2..f3edb326fb55 100644 --- a/yarn-project/stdlib/src/p2p/peer_error.ts +++ b/yarn-project/stdlib/src/p2p/peer_error.ts @@ -15,3 +15,10 @@ export enum PeerErrorSeverity { */ HighToleranceError = 'HighToleranceError', } + +/** Severities ordered from mildest to harshest. */ +export const PeerErrorSeverityByHarshness = [ + PeerErrorSeverity.HighToleranceError, + PeerErrorSeverity.MidToleranceError, + PeerErrorSeverity.LowToleranceError, +] as const;