From b892f9facc483082b4a56c8d57814b23cc78f139 Mon Sep 17 00:00:00 2001 From: Pablo Deymonnaz Date: Fri, 4 Dec 2026 18:49:12 -0300 Subject: [PATCH 1/3] Hold a Waggle peer to one aggregate per slot and aggregator EIP-8529's draft added a rule (b541c27f85): a message naming a second AggregateID with the slot and aggregator of one the sender already sent is dropped, and its sender penalized. The service now remembers the first aggregate each peer sent a message about, per slot and aggregator, by peer rather than by connection and for as long as the slot is tracked. A message of that peer about another one is dropped before the header of another aggregate we may hold is looked at, as the EIP orders it. The peer is penalized for the first such message only (waggle_second_aggregate_id): a peer on an earlier draft breaks the rule in every announcement when an aggregator signed two headers. The node keeps to the rule itself. Once it holds the header of an aggregate, by a peer's header passing or by publishing its own, it sends nothing about it to the peers that sent it another aggregate of the slot and aggregator: it may have requested chunks of theirs from them. A published aggregate is now held before it is pushed, for that reason. The rule replaces bound_headerless_aggregates, which let a peer count for the aggregate it named last: a peer can no longer name a second one. --- crates/common/telemetry/src/pq_events/mod.rs | 2 +- .../common/telemetry/src/pq_events/waggle.rs | 18 +- .../net/p2p/src/beacon/waggle_aggregates.rs | 314 ++++++++++++++---- docs/beacon_pq.md | 19 +- 4 files changed, 268 insertions(+), 85 deletions(-) diff --git a/crates/common/telemetry/src/pq_events/mod.rs b/crates/common/telemetry/src/pq_events/mod.rs index 2ad16f68e..52fbaee2d 100644 --- a/crates/common/telemetry/src/pq_events/mod.rs +++ b/crates/common/telemetry/src/pq_events/mod.rs @@ -807,7 +807,7 @@ fn waggle_transport(policy: WagglePolicy<'_>) -> Value { }, "incomplete_receive": { "coverage": "observed", - "code_location": "ethlambda_p2p::beacon::waggle_aggregates::AggregateDisseminationService: clean_up, when the tracking window closes on an aggregate that is not complete (expired/timeout); bound_headerless_aggregates, on_header_held and on_header_verified, where the service stops tracking an aggregate because its last peer moved on to another of its slot and aggregator, or because the header of another one is held (expired/evicted); AggregateDisseminationService::publish, when this node publishes an aggregate peers had sent part of, which is no receipt (cancelled/cancelled); Aggregate::on_chunk_valid, when the complete aggregate cannot be handed to the P2P actor, and the drop of a delivery that actor stopped before taking off its mailbox (failed/other); and ethlambda_p2p::beacon::waggle, Handler, when the retained chunks do not reassemble into a proof (failed/assembly_error). A service that stops (its topic is left, the node shuts down) ends its open trackers as cancelled/cancelled, which a node that is shutting down may no longer export. A tracker opens at the first message a peer sends about an AggregateID, before anything is validated, so one that ends having validated nothing (no header held, no chunk retained) is recorded within a budget of 256 at once and 32 a second for the process; past it the end is counted in pq_measurement_summary_evictions_total and not recorded, which is what keeps a peer naming a new aggregate in every message from filling the export queue. A tracker that held a header or retained a chunk always ends with its record", + "code_location": "ethlambda_p2p::beacon::waggle_aggregates::AggregateDisseminationService: clean_up, when the tracking window closes on an aggregate that is not complete (expired/timeout); on_header_held and on_header_verified, where the service stops tracking an aggregate because the header of another one of its slot and aggregator is held (expired/evicted); AggregateDisseminationService::publish, when this node publishes an aggregate peers had sent part of, which is no receipt (cancelled/cancelled); Aggregate::on_chunk_valid, when the complete aggregate cannot be handed to the P2P actor, and the drop of a delivery that actor stopped before taking off its mailbox (failed/other); and ethlambda_p2p::beacon::waggle, Handler, when the retained chunks do not reassemble into a proof (failed/assembly_error). A service that stops (its topic is left, the node shuts down) ends its open trackers as cancelled/cancelled, which a node that is shutting down may no longer export. A tracker opens at the first message a peer sends about an AggregateID, before anything is validated, so one that ends having validated nothing (no header held, no chunk retained) is recorded within a budget of 256 at once and 32 a second for the process; past it the end is counted in pq_measurement_summary_evictions_total and not recorded, which is what keeps a peer naming a new aggregate in every message from filling the export queue. A tracker that held a header or retained a chunk always ends with its record", }, "publish": { "code_location": "handoff: ethlambda_rpc::beacon::pool::post_pq_aggregate_and_proofs, after the header and the proof passed their checks and right before the aggregate is sent to the network actor (RpcToP2P::publish_pq_waggle_aggregate), so before that actor's mailbox and the proof's split into the chunks the service is given; handoff_admission: the topic service's mailbox, a tokio mpsc try_send in ethlambda_p2p::beacon::waggle::publish (accepted is read right before the non-blocking call that reports it; a full mailbox is rejected/queue_full and a closed one rejected/shutdown, each a local failed result); invoked: AggregateDisseminationService::publish, when the service takes the aggregate off its mailbox; result: that function's own disposition (tracked as its origin -> network accepted/none; every chunk already held -> duplicate_suppressed/duplicate; slot outside the tracking window, a no-op -> suppressed/stale; another aggregate of the slot and aggregator held -> failed/other; chunks that are not the 64 in order -> failed/invalid). An aggregate the seen cache already covers is not handed to the service: local duplicate_suppressed/duplicate, which settles its duty as a duplicate unless the aggregate's own publication still awaits the service's answer (a copy handed over twice: the answer settles it). A failure before the mailbox (no held fork digest, no service for it, a proof that does not split, a stopped network actor) emits no record", diff --git a/crates/common/telemetry/src/pq_events/waggle.rs b/crates/common/telemetry/src/pq_events/waggle.rs index 4ae99d29c..246afa44a 100644 --- a/crates/common/telemetry/src/pq_events/waggle.rs +++ b/crates/common/telemetry/src/pq_events/waggle.rs @@ -59,15 +59,15 @@ fn missing(reason: &str) -> Value { /// How many trackers that validated nothing may end with a record. /// /// A tracker opens at the first message a peer sends about an aggregate, -/// before anything of it is checked, so its ID is that peer's word alone: a -/// peer that names another aggregate of a slot and aggregator in every -/// message opens one tracker and ends another each time. A record for each -/// would let one peer fill the exporter's queue, at the cost of real -/// records. So those ends draw on a budget, and one past it is counted as a -/// summary eviction of the event, as this family counts every loss before a -/// record, and not recorded. A tracker that held a header or -/// retained a chunk cost its peers a signed header of an aggregator of the -/// slot, and always ends with its record. +/// before anything of it is checked, so its ID is that peer's word alone. A +/// peer is held to one aggregate per slot and aggregator, but it can name a +/// made-up one for every aggregator of every tracked slot, and again under +/// another identity. A record for each would let such peers fill the +/// exporter's queue, at the cost of real records. So those ends draw on a +/// budget, and one past it is counted as a summary eviction of the event, as +/// this family counts every loss before a record, and not recorded. A +/// tracker that held a header or retained a chunk cost its peers a signed +/// header of an aggregator of the slot, and always ends with its record. mod unvalidated { use std::sync::atomic::{AtomicU64, Ordering}; diff --git a/crates/net/p2p/src/beacon/waggle_aggregates.rs b/crates/net/p2p/src/beacon/waggle_aggregates.rs index 91c9b6a15..49963a665 100644 --- a/crates/net/p2p/src/beacon/waggle_aggregates.rs +++ b/crates/net/p2p/src/beacon/waggle_aggregates.rs @@ -252,6 +252,15 @@ mod metrics { ) .unwrap() }); + pub(super) static AGGREGATE_DISSEMINATION_SECOND_IDS_REJECTED: LazyLock = + LazyLock::new(|| { + register_int_counter!( + "lean_beacon_aggregate_dissemination_second_ids_rejected_total", + "Waggle messages dropped for naming a second aggregate of a slot and aggregator \ + from one peer" + ) + .unwrap() + }); pub(super) static AGGREGATE_DISSEMINATION_HEADERS: LazyLock = LazyLock::new(|| { register_int_counter_vec!( @@ -562,6 +571,19 @@ pub(crate) struct AggregateDisseminationService { /// header of at most one aggregate per slot and aggregator, the first valid one we received, /// and track no other aggregate of that slot and aggregator. held: HashMap<(Slot, u64), Root>, + /// The aggregate each peer sent us a message about first, by slot and aggregator. A peer + /// sends one aggregate per slot and aggregator, see `note_received_id`. Kept by peer rather + /// than by connection, so that connecting again does not let a peer name another one, and + /// for as long as the slot is tracked. + received_ids: HashMap<(Slot, u64), HashMap>, +} + +/// The aggregate of a slot and aggregator that a peer sent us a message about first. +struct ReceivedId { + header_root: Root, + /// Whether the peer was penalized for a message about another aggregate of the slot and + /// aggregator. It is penalized for the first such message only. + penalized: bool, } /// A peer on the topic, as of its newest connection. @@ -860,8 +882,12 @@ impl RequestCandidate<'_> { /// `AggregateDisseminationService::check_aggregate_id`. #[derive(Debug, PartialEq, Eq)] enum IdCheck { + /// The ID passed its checks, and we start tracking its aggregate. Track, - /// The message is dropped. + /// The ID passed its checks, so its sender is held to it, see `note_received_id`, but we + /// hold the header of another aggregate of its slot and aggregator. The message is dropped. + OtherHeld, + /// The ID did not pass its checks. The message is dropped. Ignore, } @@ -914,6 +940,7 @@ impl AggregateDisseminationService { peers: HashMap::new(), aggregates: HashMap::new(), held: HashMap::new(), + received_ids: HashMap::new(), } } @@ -1150,12 +1177,14 @@ impl AggregateDisseminationService { } aggregate.publication = publication; } - aggregate.push_chunks(new_chunks, &self.peers); - // Tell our peers what we have right away, so that they request what we don't push. The - // ones whose send queue is full are told with the next flush. - aggregate.announcement_pending = !aggregate.announce(&self.peers, Instant::now()); - + // Before anything is sent: it tells which peers are sent nothing of this aggregate. self.on_header_held(id); + if let Some(aggregate) = self.aggregates.get_mut(&id) { + aggregate.push_chunks(new_chunks, &self.peers); + // Tell our peers what we have right away, so that they request what we don't push. + // The ones whose send queue is full are told with the next flush. + aggregate.announcement_pending = !aggregate.announce(&self.peers, Instant::now()); + } self.mark_covered_by(id); Ok(()) } @@ -1261,11 +1290,58 @@ impl AggregateDisseminationService { } // We don't hold the header of another aggregate with the same slot and aggregator. if self.held.contains_key(&(id.slot, id.aggregator_index)) { - return IdCheck::Ignore; + return IdCheck::OtherHeld; } IdCheck::Track } + /// Notes that `peer_id` sent a message about the aggregate `id`, whose ID passed its checks. + /// Returns whether the message is to be handled. + /// + /// A peer sends us one aggregate per slot and aggregator: an honest aggregator signs one, + /// and a node holds the header of one. A message about another one is dropped, the EIP's + /// _[REJECT]_ of a second `AggregateID`. This also bounds what we remember about aggregates + /// whose header we don't hold, of which a peer could otherwise name one per message. + /// + /// The peer is penalized once per slot and aggregator, not per message. A peer on the + /// EIP's earlier drafts breaks the rule when an aggregator signed two headers: it requests + /// the one a peer announced, then announces the other one, whose header it got first, to + /// every peer, in a message per change of what it has. + fn note_received_id(&mut self, peer_id: PeerId, id: &AggregateId) -> bool { + let penalize = match self + .received_ids + .entry((id.slot, id.aggregator_index)) + .or_default() + .entry(peer_id) + { + Entry::Vacant(entry) => { + entry.insert(ReceivedId { + header_root: id.header_root, + penalized: false, + }); + return true; + } + Entry::Occupied(entry) if entry.get().header_root == id.header_root => return true, + Entry::Occupied(mut entry) => !std::mem::replace(&mut entry.get_mut().penalized, true), + }; + metrics::inc_counter(&metrics::AGGREGATE_DISSEMINATION_SECOND_IDS_REJECTED); + if penalize { + debug!( + slot = %id.slot, + aggregator_index = id.aggregator_index, + %peer_id, + "Peer sent a second aggregate of a slot and aggregator" + ); + self.penalize_peer( + peer_id, + None, + Penalty::MidTolerance, + "waggle_second_aggregate_id", + ); + } + false + } + /// Starts telemetry's record of our tracking the aggregate `id`, which a peer sent the /// first message about. `None` while telemetry's `pq` family is off. fn open_tracker(&self, id: &AggregateId) -> Option> { @@ -1284,22 +1360,37 @@ impl AggregateDisseminationService { piece, } = message; - if !self.aggregates.contains_key(&id) { - match self.check_aggregate_id(&id) { - IdCheck::Track => {} - IdCheck::Ignore => { - if piece.is_some() { - count_received_chunk("untracked"); - } - return Ok(()); + // The ID of an aggregate we track passed its checks when we started to. + let check = (!self.aggregates.contains_key(&id)).then(|| self.check_aggregate_id(&id)); + if check == Some(IdCheck::Ignore) { + if piece.is_some() { + count_received_chunk("untracked"); + } + return Ok(()); + } + // Before the header of another aggregate we may hold is looked at, as in the EIP: the + // sender is held to the ID though we drop the message. + if !self.note_received_id(peer_id, &id) { + if piece.is_some() { + count_received_chunk("second_id"); + } + return Ok(()); + } + match check { + None => {} + Some(IdCheck::Track) => { + metrics::inc_counter(&metrics::AGGREGATE_DISSEMINATION_AGGREGATES_TRACKED); + let mut aggregate = Aggregate::new(id); + aggregate.tracker = self.open_tracker(&id); + self.aggregates.insert(id, aggregate); + } + Some(IdCheck::OtherHeld | IdCheck::Ignore) => { + if piece.is_some() { + count_received_chunk("untracked"); } + return Ok(()); } - metrics::inc_counter(&metrics::AGGREGATE_DISSEMINATION_AGGREGATES_TRACKED); - let mut aggregate = Aggregate::new(id); - aggregate.tracker = self.open_tracker(&id); - self.aggregates.insert(id, aggregate); } - self.bound_headerless_aggregates(peer_id, id); let Some(aggregate) = self.aggregates.get_mut(&id) else { return Ok(()); @@ -1326,36 +1417,6 @@ impl AggregateDisseminationService { Ok(()) } - /// Keeps what we remember about aggregates whose header we don't hold bounded: of the - /// aggregates with the slot and aggregator of `id`, `peer_id` only counts as a peer of the - /// one it sent a message about last, which is `id`. Aggregates without a header that no - /// peer is left of are not tracked anymore. - /// - /// Until we hold a header, peers can announce several aggregates of a slot and aggregator, - /// of which an honest aggregator signed at most one, and we can't tell which. - fn bound_headerless_aggregates(&mut self, peer_id: PeerId, id: AggregateId) { - if self.held.contains_key(&(id.slot, id.aggregator_index)) { - // We track no other aggregate of this slot and aggregator. - return; - } - self.aggregates.retain(|other_id, other| { - if *other_id == id - || other_id.slot != id.slot - || other_id.aggregator_index != id.aggregator_index - || other.peers.remove(&peer_id).is_none() - { - return true; - } - other.forget_requests_to(peer_id); - // An aggregate whose header is being checked is kept: the result refers to it. - let keep = !other.peers.is_empty() || !matches!(other.header, HeaderState::Unknown); - if !keep { - other.end_tracking("expired", "evicted"); - } - keep - }); - } - /// Handles the piece `peer_id` sent of the aggregate `id`, which we track. Returns whether /// the rest of the message is to be handled, i.e. whether the message is not dropped. fn handle_piece(&mut self, peer_id: PeerId, id: AggregateId, piece: AggregatePiece) -> bool { @@ -1828,9 +1889,25 @@ impl AggregateDisseminationService { /// Notes that we hold the header of the aggregate `id` from now on, and stops tracking the /// other aggregates with its slot and aggregator. + /// + /// The peers that sent us another aggregate of the slot and aggregator are sent nothing + /// about this one. We may have requested chunks of theirs from them, and a peer drops and + /// penalizes a second aggregate of a slot and aggregator from us as we do from it, see + /// `note_received_id`. They have no use for this one: a peer sends messages about an + /// aggregate it holds the header of, or to the peers that announced it, which we did not. + /// So this is to be called before anything of the aggregate is pushed or announced. fn on_header_held(&mut self, id: AggregateId) { - self.held - .insert((id.slot, id.aggregator_index), id.header_root); + let slot_and_aggregator = (id.slot, id.aggregator_index); + self.held.insert(slot_and_aggregator, id.header_root); + if let Some(received) = self.received_ids.get(&slot_and_aggregator) + && let Some(aggregate) = self.aggregates.get_mut(&id) + { + for (peer_id, received) in received { + if received.header_root != id.header_root { + aggregate.peers.entry(*peer_id).or_default().banned = true; + } + } + } self.aggregates.retain(|other_id, other| { let keep = *other_id == id || other_id.slot != id.slot @@ -1970,6 +2047,7 @@ impl AggregateDisseminationService { tracked }); self.held.retain(|(slot, _), _| is_tracked(slot)); + self.received_ids.retain(|(slot, _), _| is_tracked(slot)); self.peers.retain(|_, peer| { // The subscription of the peer to us ended. It may subscribe again. @@ -4320,7 +4398,9 @@ mod tests { an_invalid_aggregate_is_no_longer_disseminated(rig()); covered_aggregates_are_no_longer_requested(rig()); one_header_is_held_per_slot_and_aggregator(rig()); - headerless_aggregates_are_bounded_per_peer(rig()); + a_peer_sends_one_aggregate_per_slot_and_aggregator(rig()); + a_peer_is_sent_one_aggregate_per_slot_and_aggregator(rig()); + a_published_aggregate_is_not_sent_to_a_peer_that_named_another(rig()); untracked_aggregates_are_only_tracked_if_their_id_passes(rig()); a_published_aggregate_is_pushed_and_announced(rig()); a_peer_that_comes_back_is_not_sent_its_chunks_again(rig()); @@ -4726,47 +4806,135 @@ mod tests { ); // Messages about the other one are ignored from now on, without penalty. - assert_eq!(rig.service.check_aggregate_id(&second.id), IdCheck::Ignore); + assert_eq!( + rig.service.check_aggregate_id(&second.id), + IdCheck::OtherHeld + ); rig.receive(&second, peer_2, None, Some(second.piece(4, true))); assert!(!rig.tracks(&second)); assert!(rig.work().is_empty()); assert!(rig.reports().is_empty()); + + // Also from a peer that sends its first message about the slot and aggregator. It is + // held to that aggregate though the message is dropped. + let (peer_3, _receiver_3) = rig.add_peer(); + rig.receive(&second, peer_3, Some(metadata(&[1], &[])), None); + assert!(rig.reports().is_empty()); + rig.receive(&first, peer_3, Some(metadata(&[1], &[])), None); + assert_eq!(rig.reports(), vec![(peer_3, "waggle_second_aggregate_id")]); } - fn headerless_aggregates_are_bounded_per_peer(mut rig: Rig) { - // Aggregates of the same slot and aggregator, of which we hold no header. + fn a_peer_sends_one_aggregate_per_slot_and_aggregator(mut rig: Rig) { + // Aggregates of the same slot and aggregator, of which we hold no header: an honest + // aggregator signed at most one, and we can't tell which. let made_up = (0..4) - .map(|seed| test_aggregate(7, &[0], seed)) + .map(|seed| test_aggregate(rig.aggregator, &[0], seed)) .collect::>(); let (peer_id, _receiver) = rig.add_peer(); let (other, _other_receiver) = rig.add_peer(); - // A peer only counts for the one it announced last, so it can't make us remember more - // than one per slot and aggregator. + // A peer is held to the first one it sends a message about. Messages about another are + // dropped, so it can't make us remember more than one, and it is penalized once, not + // per message. for test in &made_up { - rig.track(test); rig.receive(test, peer_id, Some(metadata(&[1], &[])), None); } assert_eq!(rig.service.aggregates.len(), 1); - assert!(rig.tracks(&made_up[3])); - - // An aggregate that another peer announced too stays tracked. - rig.track(&made_up[0]); - rig.receive(&made_up[0], other, Some(metadata(&[1], &[])), None); - rig.receive(&made_up[0], peer_id, Some(metadata(&[1], &[])), None); - assert!(!rig.tracks(&made_up[3])); - rig.track(&made_up[1]); - rig.receive(&made_up[1], peer_id, Some(metadata(&[1], &[])), None); assert!(rig.tracks(&made_up[0])); + assert_eq!(rig.reports(), vec![(peer_id, "waggle_second_aggregate_id")]); + + // Nothing of a dropped message is used: neither its chunk, nor its header. + rig.receive(&made_up[1], peer_id, None, Some(made_up[1].piece(3, true))); + assert!(!rig.tracks(&made_up[1])); + assert!(rig.work().is_empty()); + assert!(rig.reports().is_empty()); + + // Its messages about the first one are still handled. + rig.receive(&made_up[0], peer_id, Some(metadata(&[1, 2], &[])), None); + assert_eq!( + rig.aggregate(&made_up[0]).peers[&peer_id].has, + Some(bitmap(&[1, 2])) + ); + + // Another peer may send another one, also one we track already, and is held to it. + rig.receive(&made_up[1], other, Some(metadata(&[1], &[])), None); assert!(rig.tracks(&made_up[1])); - assert!(!rig.aggregate(&made_up[0]).peers.contains_key(&peer_id)); + assert!(rig.reports().is_empty()); + rig.receive(&made_up[0], other, Some(metadata(&[1], &[])), None); + assert!(!rig.aggregate(&made_up[0]).peers.contains_key(&other)); + assert_eq!(rig.reports(), vec![(other, "waggle_second_aggregate_id")]); + + // Connecting again does not let a peer name another one. + rig.service.peers.remove(&peer_id); + rig.receive(&made_up[2], peer_id, Some(metadata(&[1], &[])), None); + assert!(!rig.tracks(&made_up[2])); // Aggregates of other aggregators are not affected. - let another = test_aggregate(8, &[0], 0); - rig.track(&another); + let another = test_aggregate(7, &[0], 0); rig.receive(&another, peer_id, Some(metadata(&[1], &[])), None); - assert!(rig.tracks(&made_up[1])); + assert!(rig.tracks(&another)); assert!(rig.reports().is_empty()); + + // What a peer sent is forgotten with the tracking window of the slot. + let after_window = SLOT + TRACKED_SLOTS_AFTER_OWN + 1; + rig.set_now(SLOT_DURATION * after_window as u32 + DISPARITY); + rig.service.clean_up(); + assert!(rig.service.received_ids.is_empty()); + } + + fn a_peer_is_sent_one_aggregate_per_slot_and_aggregator(mut rig: Rig) { + // A faulty aggregator signed two headers for the slot. One peer announces the first, + // which we request from it, and another the second. + let first = test_aggregate(rig.aggregator, &[0], 0); + let second = test_aggregate(rig.aggregator, &[0], 1); + let (peer_1, receiver_1) = rig.add_peer(); + let (peer_2, _receiver_2) = rig.add_peer(); + let (peer_3, receiver_3) = rig.add_peer(); + rig.receive(&first, peer_1, Some(metadata(&[3], &[])), None); + rig.receive(&second, peer_2, Some(metadata(&[3], &[])), None); + let now = after_request_delay(rig.aggregate(&first)); + let peers = std::mem::take(&mut rig.service.peers); + rig.aggregate(&first).request_missing_chunks(&peers, now); + rig.service.peers = peers; + assert_eq!(requested_chunks(&receiver_1), vec![3]); + + // The second one's header is the one we come to hold. + rig.receive(&second, peer_2, None, Some(second.piece(3, true))); + rig.header_passes(&second, peer_2); + assert!(!rig.tracks(&first)); + rig.service.try_inform_new_peer(peer_1); + // As once the announcement interval has passed. + rig.aggregate(&second).last_announced = None; + rig.aggregate(&second).announcement_pending = true; + rig.service.flush_announcements(); + + // The peer we sent a message about the first one gets nothing about the second, which + // it would drop, and penalize us for. The other peers do. + assert!(sent(&receiver_1).is_empty()); + assert!(!sent(&receiver_3).is_empty()); + assert!(rig.aggregate(&second).peers[&peer_1].banned); + assert!(!rig.aggregate(&second).peers[&peer_3].banned); + assert!(rig.reports().is_empty()); + } + + fn a_published_aggregate_is_not_sent_to_a_peer_that_named_another(mut rig: Rig) { + // A peer named an aggregate of our aggregator before we publish its aggregate. + let ours = test_aggregate(rig.aggregator, &[0], 0); + let made_up = test_aggregate(rig.aggregator, &[0], 1); + let (peer_id, receiver) = rig.add_peer(); + let (_, other_receiver) = rig.add_peer(); + rig.receive(&made_up, peer_id, Some(metadata(&[1], &[])), None); + + rig.service + .publish(NewAggregate { + header: ours.verified.clone(), + chunks: ours.chunks.clone(), + publication: None, + }) + .unwrap(); + rig.service.flush_announcements(); + assert!(sent(&receiver).is_empty()); + assert!(!sent(&other_receiver).is_empty()); } fn untracked_aggregates_are_only_tracked_if_their_id_passes(mut rig: Rig) { diff --git a/docs/beacon_pq.md b/docs/beacon_pq.md index 1d04c5cf3..e46f59e00 100644 --- a/docs/beacon_pq.md +++ b/docs/beacon_pq.md @@ -160,6 +160,20 @@ Validation is split as the EIP splits it: chunk of it matched its signed header. One whose vote block is not imported yet is checked again, up to five times 500 ms apart. +A peer sends one aggregate per slot and aggregator, the EIP's _[REJECT]_ of a +second `AggregateID`: the service remembers the first one each peer sent a +message about (by peer, not by connection, for as long as the slot is +tracked), drops every message of that peer about another one, and penalizes +the peer for the first of them (`waggle_second_aggregate_id`), not for each. +The node keeps to the rule itself: once it holds the header of an aggregate, +it sends nothing about that aggregate to the peers that sent it another one +of the slot and aggregator, since it may have asked them for chunks of +theirs. Two deliberate departures from the EIP's penalties are this +once-per-seat penalty (a peer on an earlier draft breaks the rule in every +announcement when an aggregator signed two headers) and an aggregate whose +aggregator is not eligible, which is dropped without one (peers on another +fork see other aggregators). + An accepted aggregate goes to the pool block production packs from and to fork choice, as an accepted gossip aggregate did. @@ -214,8 +228,9 @@ Telemetry is wire 0.6 (`ethlambda_telemetry::pq_events::waggle`), and header held, no chunk retained) is recorded within a budget of 256 at once and 32 a second; past it the end is counted in `pq_measurement_summary_evictions_total` and not recorded. Without that - bound a peer naming another aggregate of a slot and aggregator in every - message would have one record made per message. + bound a peer naming a made-up aggregate for every aggregator of every + tracked slot, and again under another identity, would have a record made + for each. - A publication opens at the Beacon API's `handoff` and travels with the aggregate into the service's mailbox (`handoff_admission`). The service gives it its result: `accepted` once it tracks the aggregate as its origin From b77c5897d259498c4e53f9765f46d8e346e3793d Mon Sep 17 00:00:00 2001 From: Pablo Deymonnaz Date: Fri, 4 Dec 2026 18:53:46 -0300 Subject: [PATCH 2/3] Penalize every message that names a second aggregate, as EIP-8529 says The sender of a second AggregateID for a slot and aggregator was penalized for the first such message only. The EIP's REJECT penalizes the sender of each, so each is penalized now. --- .../net/p2p/src/beacon/waggle_aggregates.rs | 80 +++++++------------ docs/beacon_pq.md | 12 ++- 2 files changed, 36 insertions(+), 56 deletions(-) diff --git a/crates/net/p2p/src/beacon/waggle_aggregates.rs b/crates/net/p2p/src/beacon/waggle_aggregates.rs index 49963a665..4c6037990 100644 --- a/crates/net/p2p/src/beacon/waggle_aggregates.rs +++ b/crates/net/p2p/src/beacon/waggle_aggregates.rs @@ -575,15 +575,7 @@ pub(crate) struct AggregateDisseminationService { /// sends one aggregate per slot and aggregator, see `note_received_id`. Kept by peer rather /// than by connection, so that connecting again does not let a peer name another one, and /// for as long as the slot is tracked. - received_ids: HashMap<(Slot, u64), HashMap>, -} - -/// The aggregate of a slot and aggregator that a peer sent us a message about first. -struct ReceivedId { - header_root: Root, - /// Whether the peer was penalized for a message about another aggregate of the slot and - /// aggregator. It is penalized for the first such message only. - penalized: bool, + received_ids: HashMap<(Slot, u64), HashMap>, } /// A peer on the topic, as of its newest connection. @@ -1299,46 +1291,33 @@ impl AggregateDisseminationService { /// Returns whether the message is to be handled. /// /// A peer sends us one aggregate per slot and aggregator: an honest aggregator signs one, - /// and a node holds the header of one. A message about another one is dropped, the EIP's - /// _[REJECT]_ of a second `AggregateID`. This also bounds what we remember about aggregates - /// whose header we don't hold, of which a peer could otherwise name one per message. - /// - /// The peer is penalized once per slot and aggregator, not per message. A peer on the - /// EIP's earlier drafts breaks the rule when an aggregator signed two headers: it requests - /// the one a peer announced, then announces the other one, whose header it got first, to - /// every peer, in a message per change of what it has. + /// and a node holds the header of one. A message about another one is dropped and its + /// sender penalized, the EIP's _[REJECT]_ of a second `AggregateID`. This also bounds what + /// we remember about aggregates whose header we don't hold, of which a peer could otherwise + /// name one per message. fn note_received_id(&mut self, peer_id: PeerId, id: &AggregateId) -> bool { - let penalize = match self + let first = *self .received_ids .entry((id.slot, id.aggregator_index)) .or_default() .entry(peer_id) - { - Entry::Vacant(entry) => { - entry.insert(ReceivedId { - header_root: id.header_root, - penalized: false, - }); - return true; - } - Entry::Occupied(entry) if entry.get().header_root == id.header_root => return true, - Entry::Occupied(mut entry) => !std::mem::replace(&mut entry.get_mut().penalized, true), - }; - metrics::inc_counter(&metrics::AGGREGATE_DISSEMINATION_SECOND_IDS_REJECTED); - if penalize { - debug!( - slot = %id.slot, - aggregator_index = id.aggregator_index, - %peer_id, - "Peer sent a second aggregate of a slot and aggregator" - ); - self.penalize_peer( - peer_id, - None, - Penalty::MidTolerance, - "waggle_second_aggregate_id", - ); + .or_insert(id.header_root); + if first == id.header_root { + return true; } + debug!( + slot = %id.slot, + aggregator_index = id.aggregator_index, + %peer_id, + "Peer sent a second aggregate of a slot and aggregator" + ); + metrics::inc_counter(&metrics::AGGREGATE_DISSEMINATION_SECOND_IDS_REJECTED); + self.penalize_peer( + peer_id, + None, + Penalty::MidTolerance, + "waggle_second_aggregate_id", + ); false } @@ -1902,8 +1881,8 @@ impl AggregateDisseminationService { if let Some(received) = self.received_ids.get(&slot_and_aggregator) && let Some(aggregate) = self.aggregates.get_mut(&id) { - for (peer_id, received) in received { - if received.header_root != id.header_root { + for (peer_id, header_root) in received { + if *header_root != id.header_root { aggregate.peers.entry(*peer_id).or_default().banned = true; } } @@ -4834,20 +4813,22 @@ mod tests { let (other, _other_receiver) = rig.add_peer(); // A peer is held to the first one it sends a message about. Messages about another are - // dropped, so it can't make us remember more than one, and it is penalized once, not - // per message. + // dropped, so it can't make us remember more than one, and each is penalized. for test in &made_up { rig.receive(test, peer_id, Some(metadata(&[1], &[])), None); } assert_eq!(rig.service.aggregates.len(), 1); assert!(rig.tracks(&made_up[0])); - assert_eq!(rig.reports(), vec![(peer_id, "waggle_second_aggregate_id")]); + assert_eq!( + rig.reports(), + vec![(peer_id, "waggle_second_aggregate_id"); 3] + ); // Nothing of a dropped message is used: neither its chunk, nor its header. rig.receive(&made_up[1], peer_id, None, Some(made_up[1].piece(3, true))); assert!(!rig.tracks(&made_up[1])); assert!(rig.work().is_empty()); - assert!(rig.reports().is_empty()); + assert_eq!(rig.reports(), vec![(peer_id, "waggle_second_aggregate_id")]); // Its messages about the first one are still handled. rig.receive(&made_up[0], peer_id, Some(metadata(&[1, 2], &[])), None); @@ -4868,6 +4849,7 @@ mod tests { rig.service.peers.remove(&peer_id); rig.receive(&made_up[2], peer_id, Some(metadata(&[1], &[])), None); assert!(!rig.tracks(&made_up[2])); + assert_eq!(rig.reports(), vec![(peer_id, "waggle_second_aggregate_id")]); // Aggregates of other aggregators are not affected. let another = test_aggregate(7, &[0], 0); diff --git a/docs/beacon_pq.md b/docs/beacon_pq.md index e46f59e00..6cf3ebaf7 100644 --- a/docs/beacon_pq.md +++ b/docs/beacon_pq.md @@ -164,13 +164,11 @@ A peer sends one aggregate per slot and aggregator, the EIP's _[REJECT]_ of a second `AggregateID`: the service remembers the first one each peer sent a message about (by peer, not by connection, for as long as the slot is tracked), drops every message of that peer about another one, and penalizes -the peer for the first of them (`waggle_second_aggregate_id`), not for each. -The node keeps to the rule itself: once it holds the header of an aggregate, -it sends nothing about that aggregate to the peers that sent it another one -of the slot and aggregator, since it may have asked them for chunks of -theirs. Two deliberate departures from the EIP's penalties are this -once-per-seat penalty (a peer on an earlier draft breaks the rule in every -announcement when an aggregator signed two headers) and an aggregate whose +the peer for each (`waggle_second_aggregate_id`). The node keeps to the rule +itself: once it holds the header of an aggregate, it sends nothing about +that aggregate to the peers that sent it another one of the slot and +aggregator, since it may have asked them for chunks of theirs. The one +deliberate departure from the EIP's penalties is an aggregate whose aggregator is not eligible, which is dropped without one (peers on another fork see other aggregators). From 79088b77cf23a060ade055698028571ea3f71521 Mon Sep 17 00:00:00 2001 From: Pablo Deymonnaz Date: Fri, 4 Dec 2026 21:51:28 -0300 Subject: [PATCH 3/3] Close two gaps in the one-aggregate-per-peer rule, and fix the docs build Both are from the AI security review of this pull request. A peer whose first message of a slot and aggregator named another aggregate after we held a header was recorded but not kept out of the aggregate we hold: only on_header_held marked such peers, and it had already run. note_received_id now marks the peer too, so it is sent nothing more about the held aggregate. A message of a peer's previous connection that was delivered after we heard of its new one was dropped before its ID was recorded, so the peer could name another aggregate on the new connection. The message is still ignored, but its ID is received like any other. The docs build failed on `_[REJECT]_`, which the link checker reads as a link without a target. --- .../net/p2p/src/beacon/waggle_aggregates.rs | 117 +++++++++++++++++- docs/beacon_pq.md | 13 +- 2 files changed, 122 insertions(+), 8 deletions(-) diff --git a/crates/net/p2p/src/beacon/waggle_aggregates.rs b/crates/net/p2p/src/beacon/waggle_aggregates.rs index 4c6037990..5e57821bb 100644 --- a/crates/net/p2p/src/beacon/waggle_aggregates.rs +++ b/crates/net/p2p/src/beacon/waggle_aggregates.rs @@ -1211,8 +1211,12 @@ impl AggregateDisseminationService { .is_some_and(|peer| peer.connection_id > connection_id) { // Sent on a previous connection of the peer, and delivered late. The peer - // starts over on its new connection, so this is no longer of use. + // starts over on its new connection, so this is no longer of use, except + // for the aggregate it names, which the peer is held to on every connection. debug!(%peer_id, "Ignoring waggle message of a previous connection"); + if let Ok(message) = Message::try_from(pieces) { + self.note_id_of_previous_connection(peer_id, &message.id); + } return Ok(()); } let message = match Message::try_from(pieces) { @@ -1295,14 +1299,30 @@ impl AggregateDisseminationService { /// sender penalized, the EIP's _[REJECT]_ of a second `AggregateID`. This also bounds what /// we remember about aggregates whose header we don't hold, of which a peer could otherwise /// name one per message. + /// + /// If we hold the header of another aggregate of the slot and aggregator, the peer is sent + /// nothing more about that one, as `on_header_held` does for the peers that sent theirs + /// before we held it. fn note_received_id(&mut self, peer_id: PeerId, id: &AggregateId) -> bool { + let slot_and_aggregator = (id.slot, id.aggregator_index); let first = *self .received_ids - .entry((id.slot, id.aggregator_index)) + .entry(slot_and_aggregator) .or_default() .entry(peer_id) .or_insert(id.header_root); if first == id.header_root { + if let Some(held_root) = self.held.get(&slot_and_aggregator) + && *held_root != id.header_root + { + let held_id = AggregateId { + header_root: *held_root, + ..*id + }; + if let Some(held) = self.aggregates.get_mut(&held_id) { + held.peers.entry(peer_id).or_default().banned = true; + } + } return true; } debug!( @@ -1321,6 +1341,16 @@ impl AggregateDisseminationService { false } + /// Holds `peer_id` to the aggregate `id`, which a message of a previous connection of it + /// names. The message is otherwise ignored, but its ID is received like any other if it + /// passes its checks: a peer whose message is delivered after it connected again could + /// else name another aggregate of the slot and aggregator on its new connection. + fn note_id_of_previous_connection(&mut self, peer_id: PeerId, id: &AggregateId) { + if self.aggregates.contains_key(id) || self.check_aggregate_id(id) != IdCheck::Ignore { + self.note_received_id(peer_id, id); + } + } + /// Starts telemetry's record of our tracking the aggregate `id`, which a peer sent the /// first message about. `None` while telemetry's `pq` family is off. fn open_tracker(&self, id: &AggregateId) -> Option> { @@ -1874,7 +1904,8 @@ impl AggregateDisseminationService { /// penalizes a second aggregate of a slot and aggregator from us as we do from it, see /// `note_received_id`. They have no use for this one: a peer sends messages about an /// aggregate it holds the header of, or to the peers that announced it, which we did not. - /// So this is to be called before anything of the aggregate is pushed or announced. + /// So this is to be called before anything of the aggregate is pushed or announced. A peer + /// that sends us another one later is sent nothing from then on, see `note_received_id`. fn on_header_held(&mut self, id: AggregateId) { let slot_and_aggregator = (id.slot, id.aggregator_index); self.held.insert(slot_and_aggregator, id.header_root); @@ -4380,6 +4411,8 @@ mod tests { a_peer_sends_one_aggregate_per_slot_and_aggregator(rig()); a_peer_is_sent_one_aggregate_per_slot_and_aggregator(rig()); a_published_aggregate_is_not_sent_to_a_peer_that_named_another(rig()); + a_held_aggregate_is_not_sent_to_a_peer_that_names_another_later(rig()); + a_message_of_a_previous_connection_holds_its_sender_to_its_aggregate(rig()); untracked_aggregates_are_only_tracked_if_their_id_passes(rig()); a_published_aggregate_is_pushed_and_announced(rig()); a_peer_that_comes_back_is_not_sent_its_chunks_again(rig()); @@ -4919,6 +4952,84 @@ mod tests { assert!(!sent(&other_receiver).is_empty()); } + fn a_held_aggregate_is_not_sent_to_a_peer_that_names_another_later(mut rig: Rig) { + // We hold the header of an aggregate. A peer then sends its first message of the slot + // and aggregator, about another one. + let held = test_aggregate(rig.aggregator, &[0], 0); + let other = test_aggregate(rig.aggregator, &[0], 1); + rig.hold(&held, &[3]); + let (peer_id, receiver) = rig.add_peer(); + let (other_peer, other_receiver) = rig.add_peer(); + rig.receive(&other, peer_id, Some(metadata(&[1], &[])), None); + assert!(!rig.tracks(&other)); + assert!(rig.reports().is_empty()); + + // The peer is sent nothing about the one we hold from then on, as if it had named the + // other one before we held this one. + assert!(rig.aggregate(&held).peers[&peer_id].banned); + rig.service.try_inform_new_peer(peer_id); + rig.service.try_inform_new_peer(other_peer); + // As once the announcement interval has passed. + rig.aggregate(&held).last_announced = None; + rig.aggregate(&held).announcement_pending = true; + rig.service.flush_announcements(); + assert!(sent(&receiver).is_empty()); + assert!(!sent(&other_receiver).is_empty()); + + // A peer that names the one we hold is not affected. + rig.receive(&held, other_peer, Some(metadata(&[1], &[])), None); + assert!(!rig.aggregate(&held).peers[&other_peer].banned); + } + + fn a_message_of_a_previous_connection_holds_its_sender_to_its_aggregate(mut rig: Rig) { + // A faulty aggregator signed two headers for the slot. + let first = test_aggregate(rig.aggregator, &[0], 0); + let second = test_aggregate(rig.aggregator, &[0], 1); + let ineligible = test_aggregate(rig.no_aggregator, &[0], 0); + let (peer_id, _receiver) = rig.add_peer(); + let announcement = |test: &TestAggregate, connection_id| TopicEvent::Pieces { + peer_id, + connection_id, + pieces: ObjectPieces { + object_id: Bytes::from(test.id.to_ssz()), + metadata: Some(wire_metadata(&metadata(&[1], &[])).to_ssz().into()), + pieces: None, + }, + }; + + // The peer connected again, and what it sent on its previous connection is delivered + // after we heard of the new one. That is ignored, but for the aggregate it names. + peer_on_connection(&mut rig.service.peers, peer_id, connection(1)).unwrap(); + for test in [&first, &ineligible] { + let event = announcement(test, connection(0)); + rig.service.handle_waggle_event(event).unwrap(); + } + assert!(rig.service.aggregates.is_empty()); + assert!(rig.reports().is_empty()); + assert_eq!( + rig.service.received_ids[&(SLOT, rig.aggregator)][&peer_id], + first.id.header_root + ); + // One whose ID does not pass its checks is not received, as on the newest connection. + let ineligible_key = (SLOT, rig.no_aggregator); + assert!(!rig.service.received_ids.contains_key(&ineligible_key)); + + // So the peer can't name another one of the slot and aggregator on its new connection. + let event = announcement(&second, connection(1)); + rig.service.handle_waggle_event(event).unwrap(); + assert!(!rig.tracks(&second)); + assert_eq!(rig.reports(), vec![(peer_id, "waggle_second_aggregate_id")]); + let event = announcement(&first, connection(1)); + rig.service.handle_waggle_event(event).unwrap(); + assert!(rig.tracks(&first)); + assert!(rig.reports().is_empty()); + + // Nor does a second one of its previous connection go unnoticed. + let event = announcement(&second, connection(0)); + rig.service.handle_waggle_event(event).unwrap(); + assert_eq!(rig.reports(), vec![(peer_id, "waggle_second_aggregate_id")]); + } + fn untracked_aggregates_are_only_tracked_if_their_id_passes(mut rig: Rig) { let (peer_id, _receiver) = rig.add_peer(); diff --git a/docs/beacon_pq.md b/docs/beacon_pq.md index 6cf3ebaf7..cc08e7d6e 100644 --- a/docs/beacon_pq.md +++ b/docs/beacon_pq.md @@ -160,14 +160,17 @@ Validation is split as the EIP splits it: chunk of it matched its signed header. One whose vote block is not imported yet is checked again, up to five times 500 ms apart. -A peer sends one aggregate per slot and aggregator, the EIP's _[REJECT]_ of a +A peer sends one aggregate per slot and aggregator, the EIP's `[REJECT]` of a second `AggregateID`: the service remembers the first one each peer sent a message about (by peer, not by connection, for as long as the slot is tracked), drops every message of that peer about another one, and penalizes -the peer for each (`waggle_second_aggregate_id`). The node keeps to the rule -itself: once it holds the header of an aggregate, it sends nothing about -that aggregate to the peers that sent it another one of the slot and -aggregator, since it may have asked them for chunks of theirs. The one +the peer for each (`waggle_second_aggregate_id`). A message of a peer's +previous connection that is delivered after the peer connected again is +ignored but for that: the aggregate it names is remembered too. The node +keeps to the rule itself: once it holds the header of an aggregate, it sends +nothing about that aggregate to the peers that sent it another one of the +slot and aggregator, before or after it came to hold the header, since it +may have asked them for chunks of theirs. The one deliberate departure from the EIP's penalties is an aggregate whose aggregator is not eligible, which is dropped without one (peers on another fork see other aggregators).