From c9f23b7075f45563394ea575f4203388c9eff99d Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Wed, 28 Oct 2026 12:29:34 -0500 Subject: [PATCH] =?UTF-8?q?id(local=5Fevents):=20bounded=20dependency=20fr?= =?UTF-8?q?ontier=20for=20record()=20=E2=80=94=20fix=20#1736?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkgs/id/src/local_events.rs | 176 ++++++++++++++++++++++++++++++++++-- 1 file changed, 166 insertions(+), 10 deletions(-) diff --git a/pkgs/id/src/local_events.rs b/pkgs/id/src/local_events.rs index 39c344df31..33d3a27e05 100644 --- a/pkgs/id/src/local_events.rs +++ b/pkgs/id/src/local_events.rs @@ -19,11 +19,11 @@ use std::time::{SystemTime, UNIX_EPOCH}; use anyhow::{Context, Result, bail}; use p2panda_core::{Hash, SigningKey}; -use crate::chain::{ChainExt, Event, sign}; +use crate::chain::{ChainExt, Event, MAX_AFTER, sign}; use crate::documents::{DOCUMENT_KIND, DocumentRef, REMOVAL_KIND}; use crate::event_store::{EventStore, Ingest}; use crate::search::{Hit, ValueMatch, search_store}; -use crate::tag_claims::{CLEAR_KIND, Claim, SET_KIND, TagOp, check_op}; +use crate::tag_claims::{CLEAR_KIND, Claim, MAX_CLEAR_CLAIMS, SET_KIND, TagOp, check_op}; use crate::waiting::{AuthorKey, WaitingLimits}; /// The directory the event store lives in, under the data directory, unless `--event-store` names @@ -178,7 +178,7 @@ impl LocalEvents { for (name, value) in tags { check_tag(name, value.as_deref())?; } - let id = self.record(DOCUMENT_KIND, None, Some(document.clone()), None)?; + let id = self.record(DOCUMENT_KIND, None, Some(document.clone()), None, &[])?; let mut claims = Vec::with_capacity(tags.len()); for (name, value) in tags { claims.push(self.set_tag(id, name, value.as_deref())?); @@ -201,28 +201,86 @@ impl LocalEvents { name: name.to_owned(), value: value.map(str::to_owned), }; - self.record(SET_KIND, Some(op), None, None) + self.record(SET_KIND, Some(op), None, None, &[]) } /// Clears every live claim named `name` on `subject`, whoever set it. It returns how many it /// cleared, and the clear's event ID. Nothing to clear is an error. /// + /// A claim is removed only when it sits in the clear's causal past, so each sub-clear + /// names the current head of every author whose claims it names as a required head of + /// its `after` frontier (see [`LocalEvents::bounded_after`]). When the claims span more + /// authors than the frontier cap ([`MAX_AFTER`]) or more claims than one clear may name + /// ([`MAX_CLEAR_CLAIMS`]), the clear is split: several `tag.clear` events are recorded, + /// partitioning the claims by author into groups that fit both caps, in author order. + /// The returned count is the total across all sub-clears, and the returned event ID is + /// the last sub-clear's -- the one after which every named claim is cleared. + /// /// # Errors /// /// Fails if no live claim matches, or the store cannot record the event. pub fn clear_tag(&mut self, subject: Hash, name: &str) -> Result<(usize, Hash)> { - let claims: Vec = self + let claims: Vec = self .store .tag_claims()? .into_iter() .filter(|claim| claim.subject == subject && claim.name == name) - .map(|claim| claim.id) .collect(); if claims.is_empty() { bail!("no live claim on {} is named {name}", subject.to_hex()); } let count = claims.len(); - let event = self.record(CLEAR_KIND, Some(TagOp::Clear { claims }), None, None)?; + // The current head of each author, for the required tier of each sub-clear. + let heads: HashMap = self + .frontier()? + .into_iter() + .map(|head| (head.author, head.event)) + .collect(); + // Partition the claims by author into groups fitting both caps, in author order. + let mut by_author: HashMap> = HashMap::new(); + for claim in claims { + by_author.entry(claim.author).or_default().push(claim); + } + let mut authors: Vec = by_author.keys().copied().collect(); + authors.sort_unstable(); + let mut groups: Vec> = Vec::new(); + let mut group: Vec = Vec::new(); + let mut group_authors = 0usize; + for author in authors { + let authored = by_author.remove(&author).unwrap_or_default(); + if !group.is_empty() + && (group_authors == MAX_AFTER || group.len() + authored.len() > MAX_CLEAR_CLAIMS) + { + groups.push(std::mem::take(&mut group)); + group_authors = 0; + } + group_authors += 1; + group.extend(authored); + } + if !group.is_empty() { + groups.push(group); + } + let mut last = None; + for group in groups { + let mut required: Vec = Vec::new(); + let mut seen: HashSet = HashSet::new(); + for claim in &group { + if seen.insert(claim.author) { + if let Some(head) = heads.get(&claim.author) { + required.push(*head); + } + } + } + let ids: Vec = group.iter().map(|claim| claim.id).collect(); + last = Some(self.record( + CLEAR_KIND, + Some(TagOp::Clear { claims: ids }), + None, + None, + &required, + )?); + } + let event = last.context("a clear with claims records at least one event")?; Ok((count, event)) } @@ -350,17 +408,61 @@ impl LocalEvents { .collect()) } - /// Signs one event with the node's key, after every other author's head, and ingests it. The - /// store must apply it, or the call fails. + /// The `after` frontier for a new event: a dependency frontier, not an author census + /// (#1736). `after` is capped at [`MAX_AFTER`] hashes by the wire format, and events naming + /// more are refused by every existing node, so the frontier must fit the cap no matter how + /// many authors the store holds. + /// + /// When every other author's head fits (the common case), all of them are named, in + /// frontier order, exactly as before. Past the cap, the frontier is built in two tiers: + /// first the `required` heads -- for a `tag.clear`, the current head of each author of a + /// named claim, because a claim is removed only when it sits in the clear's causal past, + /// which the author's head in `after` guarantees through that author's backlink chain -- + /// then the remaining slots fill with the other authors' heads in frontier order. + /// Heads past the cap are simply not named: the events they head stay in the store, and + /// naming a head is about causal past, not about membership, so a later event loses + /// nothing but the claim of having seen them. + fn bounded_after(&self, required: &[Hash]) -> Result> { + let heads = self.other_heads()?; + if heads.len() <= MAX_AFTER { + return Ok(heads); + } + let current: HashSet = heads.iter().copied().collect(); + let mut after: Vec = Vec::with_capacity(MAX_AFTER); + let mut named: HashSet = HashSet::new(); + for head in required { + if after.len() == MAX_AFTER { + break; + } + if current.contains(head) && named.insert(*head) { + after.push(*head); + } + } + for head in heads { + if after.len() == MAX_AFTER { + break; + } + if named.insert(head) { + after.push(head); + } + } + Ok(after) + } + + /// Signs one event with the node's key, after a bounded frontier of the other authors' + /// heads (see [`LocalEvents::bounded_after`]), and ingests it. `required` names heads the + /// frontier must include when it is capped; other kinds than `tag.clear` require none. + /// The store must apply it, or the call fails. fn record( &mut self, kind: &str, tag: Option, document: Option, removes: Option, + required: &[Hash], ) -> Result { let author = self.author(); - let after = self.other_heads()?; + let after = self.bounded_after(required)?; let chain = self.store.chain(&author)?.unwrap_or_default(); let ext = ChainExt { kind: kind.to_owned(), @@ -487,6 +589,60 @@ mod tests { Hash::from_bytes([byte; 32]) } + fn key(byte: u8) -> SigningKey { + SigningKey::from_bytes(&[byte; 32]) + } + + /// Opens a one-shot node for foreign author `byte` in the shared store + /// and has it set one claim named `name` on `subject`. + fn foreign_claim(dir: &Path, byte: u8, subject: Hash, name: &str) { + let mut events = LocalEvents::open(dir, key(byte)).unwrap(); + events.set_tag(subject, name, Some("x")).unwrap(); + } + + #[test] + fn test_record_still_works_with_more_authors_than_the_after_cap() { + // Regression for #1736: with 33+ other authors in the store, the + // uncapped frontier made every record() fail validation, so the + // node could never write again. + for foreign in [33u8, 100] { + let dir = tempfile::TempDir::new().unwrap(); + let subject = id(200); + for byte in 1..=foreign { + foreign_claim(dir.path(), byte, subject, "topic"); + } + let mut events = LocalEvents::open(dir.path(), key(0)).unwrap(); + let published = events.publish("text/plain", b"hello", &[]).unwrap(); + events.set_tag(published.id, "topic", Some("mine")).unwrap(); + let (cleared, _) = events.clear_tag(published.id, "topic").unwrap(); + assert_eq!(cleared, 1); + } + } + + #[test] + fn test_clear_spanning_more_authors_than_the_cap_clears_exactly_its_claims() { + // Regression for #1736: 40 authors hold a claim named "topic" on the + // subject -- more distinct authors than MAX_AFTER, so the clear must + // split by author. Exactly those claims are removed: a claim with a + // different name on the same subject, and a same-name claim on a + // different subject, both survive. + let dir = tempfile::TempDir::new().unwrap(); + let subject = id(201); + let other_subject = id(202); + for byte in 1..=40u8 { + foreign_claim(dir.path(), byte, subject, "topic"); + } + foreign_claim(dir.path(), 1, subject, "other"); + foreign_claim(dir.path(), 2, other_subject, "topic"); + let mut events = LocalEvents::open(dir.path(), key(0)).unwrap(); + let (cleared, _) = events.clear_tag(subject, "topic").unwrap(); + assert_eq!(cleared, 40); + let remaining = events.tags_on(subject).unwrap(); + assert_eq!(remaining.len(), 1); + assert_eq!(remaining[0].name, "other"); + assert_eq!(events.tags_on(other_subject).unwrap().len(), 1); + } + #[test] fn test_causal_order_puts_every_event_after_its_parents() { // 1 and 2 are roots. 3 names 1, and 4 names 3 and 2. 5 names nothing it did not see.