diff --git a/pkgs/id/crates/id-core/src/waiting.rs b/pkgs/id/crates/id-core/src/waiting.rs index 7e147351d6..03418284e4 100644 --- a/pkgs/id/crates/id-core/src/waiting.rs +++ b/pkgs/id/crates/id-core/src/waiting.rs @@ -299,7 +299,11 @@ where /// Unlike [`insert`](Self::insert) and [`park`](Self::park), nothing expires first, and an /// empty `missing` does not release the event: it is held for good, as a parked event is. /// The caller holds the events in the order they arrived, so they are released in that - /// order. An event whose key is already held is ignored. + /// order. An event whose key is already held, has arrived, or is ready is ignored, as for + /// [`insert`](Self::insert): restoring an already-applied or ready key must not hold a + /// second copy that a later arrival would hand out again (#3911). A proof header that + /// must stay held after it is marked arrived does not take this path: it takes + /// [`restore_held_for_good`](Self::restore_held_for_good). /// /// # Errors /// @@ -317,13 +321,46 @@ where event: E, arrived_at: u64, ) -> Result<(), WaitRefusal> { - if self.by_key.contains_key(&key) { + if self.arrived.contains(&key) + || self.by_key.contains_key(&key) + || self.ready_keys.contains(&key) + { return Ok(()); } let missing: HashSet = missing.into_iter().collect(); self.hold(author, key, missing, event, arrived_at) } + /// Holds an event for good even though its key has arrived: the explicit + /// held-for-good route for fork proof headers (#3911, design #4000 decision 3). + /// A proof header is marked arrived when its fork is decided, so the + /// ordinary [`restore`](Self::restore) would ignore it; this route holds it + /// anyway, waiting for nothing, so no arrival releases it and only the age + /// cap drops it. The caller is the store's proof park, which keeps the + /// row's first arrival time and its conflicts seen. + /// + /// An event whose key is already held is ignored, as for + /// [`restore`](Self::restore). The arrived and ready guards do not apply: + /// bypassing them is the point of the route. + /// + /// # Errors + /// + /// [`WaitRefusal::AuthorFull`] when the author already holds the per-author + /// cap, or [`WaitRefusal::PoolFull`] when the pool already holds the total + /// cap across all authors. + pub fn restore_held_for_good( + &mut self, + author: AuthorKey, + key: K, + event: E, + arrived_at: u64, + ) -> Result<(), WaitRefusal> { + if self.by_key.contains_key(&key) { + return Ok(()); + } + self.hold(author, key, HashSet::new(), event, arrived_at) + } + /// The held events of `author`, in sequence order of arrival. Parked events are /// included. pub fn held_by(&self, author: &AuthorKey) -> impl Iterator { @@ -811,4 +848,58 @@ mod tests { assert_eq!(pool.len(), 2); assert!(pool.contains(&1) && pool.contains(&2) && !pool.contains(&3)); } + + #[test] + fn test_restore_ignores_a_key_that_has_arrived() { + // #3911: an already-applied key restored again must not be held a + // second time: no held copy, no release when its dependency arrives, + // and nothing for take_ready. + let mut pool = pool(); + pool.arrived(1, 0); + pool.restore(author(1), 1, [10], "stale", 0).unwrap(); + assert!(!pool.contains(&1)); + assert!(pool.is_empty()); + assert!(pool.arrived(10, 0).is_empty()); + assert!(pool.take_ready().is_empty()); + assert!(pool.is_empty()); + } + + #[test] + fn test_restore_ignores_a_key_in_the_ready_queue() { + // #3911: a key sitting in the ready queue is not held, so restoring it + // must not create a held copy alongside the ready original: the ready + // event is handed out once, and no dependency arrival releases more. + let mut pool = pool(); + pool.arrived(10, 0); + pool.insert(author(1), 1, [10], "ready", 0).unwrap(); + pool.restore(author(1), 1, [10], "copy", 0).unwrap(); + assert!(!pool.contains(&1)); + assert_eq!(pool.take_ready(), vec!["ready"]); + assert!(pool.take_ready().is_empty(), "the ready event is taken once"); + assert!(pool.arrived(10, 0).is_empty()); + assert!(pool.is_empty()); + } + + #[test] + fn test_restore_held_for_good_holds_an_arrived_proof_key() { + // The explicit held-for-good route bypasses the arrived guard on + // purpose: a proof header marked arrived stays parked, counted under + // the caps, until the age cap drops it. + let mut pool = pool(); + pool.arrived(7, 0); + pool.restore_held_for_good(author(1), 7, "proof", 0) + .unwrap(); + assert!(pool.contains(&7)); + assert_eq!(pool.waiting_count(author(1)), 1); + assert!(pool.take_ready().is_empty()); + assert!(pool.arrived(9, 0).is_empty()); + assert!( + pool.contains(&7), + "no arrival releases a held-for-good copy" + ); + // A second park of the same key is a duplicate, not a second copy. + pool.restore_held_for_good(author(1), 7, "proof", 0) + .unwrap(); + assert_eq!(pool.waiting_count(author(1)), 1); + } } diff --git a/pkgs/id/src/event_store/ingest.rs b/pkgs/id/src/event_store/ingest.rs index 9853f0a959..74f9dfb49c 100644 --- a/pkgs/id/src/event_store/ingest.rs +++ b/pkgs/id/src/event_store/ingest.rs @@ -1035,7 +1035,7 @@ fn is_stored(txn: &WriteTransaction, id: &Hash) -> Result { /// Whether `id` is a fork proof header of `author`. A header stays one after a later proof drops /// it, so the check reads the presence index, not the current proof. -fn is_proof_header( +pub(super) fn is_proof_header( txn: &WriteTransaction, author: &AuthorKey, id: &Hash, @@ -3102,6 +3102,118 @@ mod tests { store.check_invariants().unwrap(); } + #[test] + fn test_a_hold_of_an_arrived_key_is_skipped_and_leaves_no_row() { + // #3911: the ordinary hold path refuses a key the pool has already + // arrived, without a duplicate release. The pool does not hold it, and + // no row survives the transaction. + let (_dir, mut store) = open(); + let signer = key(60); + let events = chain_of(&signer, 2); + store + .transact(0, |m| { + let held = m.hold(events[1].clone(), vec![events[0].id], false, 0)?; + assert_eq!(held, waiting_rows::Hold::Held); + let released = m.release(&events[1].id, 0)?; + assert!(released.is_empty()); + Ok(((), true)) + }) + .unwrap(); + + // Simulate a durable stale row for the key after it has arrived. The + // skipped hold must remove that row in its own transaction; release + // itself already deleted the original row above. + let stale = Row { + id: events[1].id, + author: author(&signer), + seq: 1, + arrived_at: 0, + arrival: 0, + deps: vec![events[0].id], + bytes: events[1].bytes.clone(), + }; + store.write(|txn| waiting_rows::put(txn, &stale)).unwrap(); + let again = store + .transact(0, |m| { + let again = m.hold(events[1].clone(), vec![events[0].id], false, 0)?; + Ok((again, true)) + }) + .unwrap(); + assert_eq!(again, waiting_rows::Hold::Skipped); + assert!(!store.is_waiting(&events[1].id)); + assert!(store.waiting_ids().unwrap().is_empty()); + store.check_invariants().unwrap(); + } + + #[test] + fn test_a_proof_header_parked_for_good_survives_a_reopen() { + // #3911, design #4000 phase 3a: a held event that a fork turns into a + // proof header stays parked through the explicit held-for-good route, + // even though the header is marked arrived -- and the park survives a + // reopen with its first arrival time, its proof, and its conflicts. + let (dir, mut store) = open(); + let signer = key(61); + let who = author(&signer); + let missing = Hash::digest(b"an event that is never delivered"); + let held = event(&signer, 1, Some(missing), Vec::new(), "message"); + let rival = event(&signer, 1, Some(missing), Vec::new(), "rival"); + assert_eq!(ingest(&mut store, &held), Ingest::Waiting { id: held.id }); + assert!(matches!( + ingest(&mut store, &rival), + Ingest::Fork { seq: 1, .. } + )); + assert!(store.is_waiting(&held.id)); + store.check_invariants().unwrap(); + drop(store); + let store = reopen(&dir, WaitingLimits::default(), 0); + store.check_invariants().unwrap(); + assert!( + store.is_waiting(&held.id), + "the proof park survives a reopen" + ); + let proof = store.fork_proof(&who).unwrap().expect("a proof is stored"); + assert_eq!( + [proof.first.id, proof.second.id], + sorted(&held.id, &rival.id) + ); + drop(store); + // The park keeps its first arrival time: due at WEEK_MS, not a reopen + // later, and gone once it is due. + let store = reopen(&dir, WaitingLimits::default(), WEEK_MS - 1); + assert!(store.is_waiting(&held.id)); + drop(store); + let store = reopen(&dir, WaitingLimits::default(), WEEK_MS); + assert!(!store.is_waiting(&held.id)); + assert!(store.waiting_ids().unwrap().is_empty()); + store.check_invariants().unwrap(); + } + + #[test] + fn test_a_row_for_an_already_stored_event_is_dropped_on_open() { + // #3911: a waiting row whose event is already stored is stale -- + // restoring it would hold an already-decided event again. The reopen + // drops the row in the same transaction and opens cleanly. + let (dir, mut store) = open(); + let signer = key(62); + let events = chain_of(&signer, 2); + ingest_all(&mut store, &events); + let stale = Row { + id: events[0].id, + author: author(&signer), + seq: 0, + arrived_at: 0, + arrival: 0, + deps: vec![Hash::digest(b"a dependency that never arrives")], + bytes: events[0].bytes.clone(), + }; + store.write(|txn| waiting_rows::put(txn, &stale)).unwrap(); + drop(store); + let store = reopen(&dir, WaitingLimits::default(), 0); + assert!(!store.is_waiting(&events[0].id)); + assert!(store.waiting_ids().unwrap().is_empty()); + store.check_invariants().unwrap(); + } + #[test] fn test_a_write_whose_rebuild_fails_makes_the_store_refuse_calls() { let (_dir, mut store) = open_with(WaitingLimits::new(256, 1_000)); diff --git a/pkgs/id/src/event_store/mod.rs b/pkgs/id/src/event_store/mod.rs index e185762e00..dea1c0839d 100644 --- a/pkgs/id/src/event_store/mod.rs +++ b/pkgs/id/src/event_store/mod.rs @@ -72,7 +72,7 @@ mod waiting_rows; use crate::waiting::{AuthorKey, WaitRefusal, WaitingLimits, WaitingPool}; pub use ingest::{Ingest, Refusal}; -use ingest::{is_present, present_in}; +use ingest::{is_present, is_proof_header, present_in}; use waiting_rows::{Mirror, Row}; /// The store's file name in its directory. @@ -1297,9 +1297,12 @@ impl EventStore { } /// Rebuilds the waiting pool from the rows, in arrival order. A row due at `now_ms` is - /// dropped, and so is a row past its author's cap. The drops commit with the rebuild. A row - /// that waits for a present event means the rows and the store disagree, so the rebuild - /// fails as corruption and the pool is left as it was. + /// dropped, and so is a row past its author's cap. A row whose event is already present -- + /// stored, applied, or voided -- is stale, so it is dropped too: restoring it would hold an + /// already-decided event again. A row for a fork proof header is instead re-held through + /// the explicit held-for-good route, even though the header is present. Every drop commits + /// with the rebuild. A row that waits for a present event means the rows and the store + /// disagree, so the rebuild fails as corruption and the pool is left as it was. fn reload(&mut self, now_ms: u64) -> Result<(), StoreError> { let txn = self.db.begin_write()?; let mut pool = WaitingPool::new(self.limits); @@ -1312,6 +1315,35 @@ impl EventStore { dropped = true; continue; } + let event = row_event(&row)?; + if is_proof_header(&txn, &row.author, &row.id)? { + // A proof header is present but never applied: it stays held for good through + // the explicit route, which the ordinary restore's arrived guard would refuse. + match pool.restore_held_for_good(row.author, row.id, event, row.arrived_at) { + Ok(()) => { + if !row.deps.is_empty() { + let parked = Row { + deps: Vec::new(), + ..row + }; + waiting_rows::put(&txn, &parked)?; + dropped = true; + } + } + Err(WaitRefusal::AuthorFull | WaitRefusal::PoolFull) => { + waiting_rows::delete(&txn, &row.id)?; + dropped = true; + } + } + continue; + } + if is_present(&txn, &row.id)? { + // The event is already stored, applied, or voided: the row is stale, so it + // goes rather than holding an already-decided event again. + waiting_rows::delete(&txn, &row.id)?; + dropped = true; + continue; + } for dep in &row.deps { if is_present(&txn, dep)? { return Err(corrupt( @@ -1319,9 +1351,14 @@ impl EventStore { )); } } - let event = row_event(&row)?; match pool.restore(row.author, row.id, row.deps, event, row.arrived_at) { - Ok(()) => {} + Ok(()) if pool.contains(&row.id) => {} + Ok(()) => { + // Skipped by an arrived, ready, or duplicate guard: the row names an + // event the rebuilt pool does not hold, so the row goes with the rebuild. + waiting_rows::delete(&txn, &row.id)?; + dropped = true; + } Err(WaitRefusal::AuthorFull | WaitRefusal::PoolFull) => { waiting_rows::delete(&txn, &row.id)?; dropped = true; diff --git a/pkgs/id/src/event_store/waiting_rows.rs b/pkgs/id/src/event_store/waiting_rows.rs index c8eec76d32..42370373a4 100644 --- a/pkgs/id/src/event_store/waiting_rows.rs +++ b/pkgs/id/src/event_store/waiting_rows.rs @@ -209,7 +209,9 @@ impl Mirror<'_> { } /// Holds `event` at clock time `now`, either parked or waiting for `deps`, and writes its - /// row. An event that is already held is left as it is. + /// row. An event that is already held is left as it is. An event whose key the pool has + /// already arrived or has ready is skipped: the ordinary restore ignores it, and its row + /// goes in the same transaction, so the rows never name an event the pool does not hold. pub fn hold( &mut self, event: Event, @@ -309,7 +311,10 @@ impl Mirror<'_> { } /// Holds `row` parked: it waits for nothing, so no arrival releases it, and only the age cap - /// drops it. It keeps its first arrival time and its arrival number. + /// drops it. It keeps its first arrival time and its arrival number. This is the store's + /// explicit held-for-good route for proof headers: the header is already marked arrived, + /// so the ordinary restore would ignore it, and + /// [`restore_held_for_good`](WaitingPool::restore_held_for_good) holds it anyway. fn park(&mut self, row: Row) -> Result<(), StoreError> { let event = row_event(&row)?; let parked = Row { @@ -317,7 +322,7 @@ impl Mirror<'_> { ..row }; self.pool - .restore(parked.author, parked.id, [], event, parked.arrived_at) + .restore_held_for_good(parked.author, parked.id, event, parked.arrived_at) .map_err(|_refused| corrupt("a held proof header cannot be held for good"))?; put(self.txn, &parked) }