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..5e57821bb 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,11 @@ 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>, } /// A peer on the topic, as of its newest connection. @@ -860,8 +874,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 +932,7 @@ impl AggregateDisseminationService { peers: HashMap::new(), aggregates: HashMap::new(), held: HashMap::new(), + received_ids: HashMap::new(), } } @@ -1150,12 +1169,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(()) } @@ -1190,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) { @@ -1261,11 +1286,71 @@ 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 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. + /// + /// 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(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!( + 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 + } + + /// 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> { @@ -1284,22 +1369,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 +1426,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 +1898,26 @@ 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. 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) { - 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, header_root) in received { + if *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 +2057,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 +4408,11 @@ 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()); + 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()); @@ -4726,47 +4818,216 @@ 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 each is penalized. 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"); 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_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); + 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])); + assert_eq!(rig.reports(), vec![(peer_id, "waggle_second_aggregate_id")]); // 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 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) { diff --git a/docs/beacon_pq.md b/docs/beacon_pq.md index 1d04c5cf3..cc08e7d6e 100644 --- a/docs/beacon_pq.md +++ b/docs/beacon_pq.md @@ -160,6 +160,21 @@ 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 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). + An accepted aggregate goes to the pool block production packs from and to fork choice, as an accepted gossip aggregate did. @@ -214,8 +229,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