From d76003b629a4a03dba23a8a1c41da6e4ac4c30cf Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Wed, 18 Feb 2026 17:12:17 +0000 Subject: feat: upgrade repo to Full sync and trigger PR event subscription after announcement promotion When git data arrives for a purgatory announcement and promotes it to the database, the relay now: 1. Upgrades the announcement's sync level in RepoSyncIndex from StateOnly to Full (git/sync.rs: process_purgatory_announcements) 2. Sends AddFilters actions to SyncManager for all connected relays, using Full sync filters (Layer 2 #a/#A/#q) to subscribe to PR events (purgatory/sync/context.rs: RealSyncContext.process_newly_available_git_data) 3. For user-submitted purgatory announcements, registers the repo in RepoSyncIndex with StateOnly level and sends AddFilters to SyncManager so it discovers and connects to relays listed in the announcement tags (nostr/builder.rs: handle_announcement AcceptPurgatory path) The RealSyncContext now accepts optional repo_sync_index and sync_action_tx parameters. main.rs wires these up from SyncManager. PolicyContext gains repo_sync_index and sync_action_tx fields for the write policy path. --- src/nostr/policy/mod.rs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'src/nostr/policy/mod.rs') diff --git a/src/nostr/policy/mod.rs b/src/nostr/policy/mod.rs index 1566b6c..78a09fc 100644 --- a/src/nostr/policy/mod.rs +++ b/src/nostr/policy/mod.rs @@ -20,6 +20,7 @@ pub use crate::git::sync::AlignmentResult; use super::SharedDatabase; use crate::purgatory::Purgatory; +use crate::sync::{AddFilters, RepoSyncIndex}; use nostr_relay_builder::LocalRelay; use std::sync::Arc; @@ -34,6 +35,16 @@ pub struct PolicyContext { pub local_relay: Arc>>, /// Configuration reference for policy settings (includes blacklists) pub config: crate::config::Config, + /// Optional repo sync index for triggering relay discovery when announcements + /// go to purgatory via user submission (not via the sync path). + /// Wrapped in Arc for interior mutability (PolicyContext is Clone). + pub repo_sync_index: Arc>>, + /// Optional sender for AddFilters actions to SyncManager. + /// Used to trigger relay discovery when user-submitted purgatory announcements + /// are registered with StateOnly sync level. + /// Wrapped in Arc for interior mutability (PolicyContext is Clone). + pub sync_action_tx: + Arc>>>, } impl PolicyContext { @@ -51,6 +62,8 @@ impl PolicyContext { purgatory, local_relay: Arc::new(std::sync::RwLock::new(None)), config, + repo_sync_index: Arc::new(std::sync::RwLock::new(None)), + sync_action_tx: Arc::new(std::sync::RwLock::new(None)), } } @@ -68,4 +81,28 @@ impl PolicyContext { let guard = self.local_relay.read().unwrap(); guard.clone() } + + /// Set the repo sync index for relay discovery from user-submitted purgatory announcements. + pub fn set_repo_sync_index(&self, index: RepoSyncIndex) { + let mut guard = self.repo_sync_index.write().unwrap(); + *guard = Some(index); + } + + /// Get a clone of the repo sync index if it's been set. + pub fn get_repo_sync_index(&self) -> Option { + let guard = self.repo_sync_index.read().unwrap(); + guard.clone() + } + + /// Set the sync action sender for sending AddFilters actions to SyncManager. + pub fn set_sync_action_tx(&self, tx: tokio::sync::mpsc::Sender) { + let mut guard = self.sync_action_tx.write().unwrap(); + *guard = Some(tx); + } + + /// Get a clone of the sync action sender if it's been set. + pub fn get_sync_action_tx(&self) -> Option> { + let guard = self.sync_action_tx.read().unwrap(); + guard.clone() + } } -- cgit v1.2.3 From 3d9359d5ac0045fb93fd8732160e0de8413d6881 Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Wed, 18 Feb 2026 19:28:28 +0000 Subject: Revert "feat: upgrade repo to Full sync and trigger PR event subscription after announcement promotion" This reverts commit d76003b629a4a03dba23a8a1c41da6e4ac4c30cf. --- src/git/handlers.rs | 1 - src/git/sync.rs | 21 -------- src/main.rs | 20 ------- src/nostr/builder.rs | 118 ------------------------------------------ src/nostr/policy/mod.rs | 37 ------------- src/purgatory/sync/context.rs | 116 ----------------------------------------- 6 files changed, 313 deletions(-) (limited to 'src/nostr/policy/mod.rs') diff --git a/src/git/handlers.rs b/src/git/handlers.rs index 129ca2c..017eee4 100644 --- a/src/git/handlers.rs +++ b/src/git/handlers.rs @@ -307,7 +307,6 @@ pub async fn handle_receive_pack( Some(&relay), &purgatory, git_data_path_buf, - None, ) .await { diff --git a/src/git/sync.rs b/src/git/sync.rs index b3fa11a..4b35023 100644 --- a/src/git/sync.rs +++ b/src/git/sync.rs @@ -44,7 +44,6 @@ use crate::git::{self, oid_exists}; use crate::nostr::builder::SharedDatabase; use crate::nostr::events::RepositoryState; use crate::purgatory::{can_apply_state, Purgatory}; -use crate::sync::{RepoSyncIndex, SyncLevel}; /// Result of processing newly available git data. /// @@ -810,7 +809,6 @@ pub fn extract_identifier_from_pr_event(event: &Event) -> Option { /// * `local_relay` - Local relay for notifying WebSocket subscribers (optional) /// * `purgatory` - Purgatory instance to check for satisfiable events /// * `git_data_path` - Base path for git repositories -/// * `repo_sync_index` - Optional repo sync index for upgrading sync level on promotion /// /// # Returns /// A `ProcessResult` describing what was processed @@ -821,7 +819,6 @@ pub async fn process_newly_available_git_data( local_relay: Option<&nostr_relay_builder::LocalRelay>, purgatory: &Purgatory, git_data_path: &Path, - repo_sync_index: Option, ) -> anyhow::Result { let mut result = ProcessResult::default(); @@ -851,7 +848,6 @@ pub async fn process_newly_available_git_data( local_relay, purgatory, git_data_path, - repo_sync_index.as_ref(), ) .await; result.merge(announcement_result); @@ -1288,7 +1284,6 @@ async fn process_purgatory_announcements( local_relay: Option<&nostr_relay_builder::LocalRelay>, purgatory: &Purgatory, git_data_path: &Path, - repo_sync_index: Option<&RepoSyncIndex>, ) -> ProcessResult { let mut result = ProcessResult::default(); @@ -1343,22 +1338,6 @@ async fn process_purgatory_announcements( } } - // Upgrade sync level to Full in repo_sync_index - if let Some(index) = repo_sync_index { - let mut index = index.write().await; - // Use hex pubkey format to match how repo_sync_index keys are built - // (sync/mod.rs uses event.pubkey which is hex, not bech32) - let repo_id = format!("30617:{}:{}", owner.to_hex(), identifier); - if let Some(entry) = index.get_mut(&repo_id) { - entry.sync_level = SyncLevel::Full; - debug!( - identifier = %identifier, - repo_id = %repo_id, - "Upgraded sync level to Full after announcement promotion" - ); - } - } - result.announcements_released += 1; } Err(e) => { diff --git a/src/main.rs b/src/main.rs index 3ff30fb..ab6ede7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -132,24 +132,6 @@ async fn main() -> Result<()> { // Get a reference to the rejected events index for shutdown persistence let shutdown_rejected_index = sync_manager.rejected_events_index(); - // Get a reference to the repo sync index for upgrading sync levels on promotion - let repo_sync_index = sync_manager.repo_sync_index(); - - // Set the repo sync index on the write policy so user-submitted purgatory - // announcements can trigger relay discovery (connect to relays in announcement tags) - relay_with_db - .write_policy - .set_repo_sync_index(repo_sync_index.clone()); - - // Get the action sender BEFORE consuming sync_manager with spawn - let action_tx = sync_manager.action_tx(); - - // Set the sync action sender so the write policy can trigger relay connections - // when user-submitted purgatory announcements are registered with StateOnly level - if let Some(tx) = action_tx.clone() { - relay_with_db.write_policy.set_sync_action_tx(tx); - } - tokio::spawn(async move { sync_manager.run().await; }); @@ -202,8 +184,6 @@ async fn main() -> Result<()> { Some(config.domain.clone()), Some(relay_with_db.relay.clone()), git_naughty_list.clone(), - Some(repo_sync_index), - action_tx, )); // Create throttle manager for rate limiting remote git servers diff --git a/src/nostr/builder.rs b/src/nostr/builder.rs index 4c66f6d..aff12a6 100644 --- a/src/nostr/builder.rs +++ b/src/nostr/builder.rs @@ -98,24 +98,6 @@ impl Nip34WritePolicy { self.ctx.set_local_relay(relay); } - /// Set the repo sync index for relay discovery from user-submitted purgatory announcements. - /// - /// When a user submits an announcement that goes to purgatory (no git data yet), - /// the relay needs to discover and connect to relays listed in the announcement's - /// `relays` and `clone` tags. This index is updated when the announcement is accepted - /// into purgatory, triggering the sync system to connect and sync state events. - pub fn set_repo_sync_index(&self, index: crate::sync::RepoSyncIndex) { - self.ctx.set_repo_sync_index(index); - } - - /// Set the sync action sender for sending AddFilters actions to SyncManager. - /// - /// This allows the write policy to notify the SyncManager when user-submitted - /// purgatory announcements need relay discovery (triggering new connections). - pub fn set_sync_action_tx(&self, tx: tokio::sync::mpsc::Sender) { - self.ctx.set_sync_action_tx(tx); - } - /// Handle repository announcement event async fn handle_announcement(&self, event: &Event) -> WritePolicyResult { let event_id_str = event.id.to_bech32().unwrap_or_else(|_| event.id.to_hex()); @@ -164,106 +146,6 @@ impl Nip34WritePolicy { "Accepted announcement to purgatory: {} (waiting for git data)", event_id_str ); - - // Register in repo_sync_index with StateOnly level so the sync - // system discovers and connects to relays listed in this announcement. - // This is needed for user-submitted announcements (not via sync path) - // to trigger relay discovery and state event sync. - if let Some(repo_sync_index) = self.ctx.get_repo_sync_index() { - if let Some(identifier) = event.tags.iter().find_map(|tag| { - let tag_vec = tag.as_slice(); - if tag_vec.len() >= 2 && tag_vec[0] == "d" { - Some(tag_vec[1].to_string()) - } else { - None - } - }) { - let repo_id = - format!("30617:{}:{}", event.pubkey, identifier); - - // Get relay URLs stored in purgatory for this announcement - let relays = self - .ctx - .purgatory - .find_announcement(&event.pubkey, &identifier) - .map(|entry| entry.relays) - .unwrap_or_default(); - - if !relays.is_empty() { - use crate::sync::{ - AddFilters, PendingItems, RepoSyncNeeds, SyncLevel, - }; - - // Update repo_sync_index with StateOnly for this repo - let new_repos = { - let mut index = repo_sync_index.write().await; - let entry = - index.entry(repo_id.clone()).or_insert_with(|| { - RepoSyncNeeds { - relays: std::collections::HashSet::new(), - root_events: std::collections::HashSet::new(), - sync_level: SyncLevel::StateOnly, - } - }); - entry.relays.extend(relays.iter().cloned()); - // Don't upgrade if already Full - tracing::info!( - repo_id = %repo_id, - relay_count = entry.relays.len(), - "Registered user-submitted purgatory announcement in \ - RepoSyncIndex with StateOnly level for relay discovery" - ); - // Return cloned relays for AddFilters - relays.clone() - }; - - // Send AddFilters to SyncManager so it connects to these relays - if let Some(tx) = self.ctx.get_sync_action_tx() { - // Build state-only filters for this repo - let state_only_repos: std::collections::HashSet = - std::iter::once(repo_id.clone()).collect(); - let filters = - crate::sync::filters::build_sync_level_aware_filters( - &std::collections::HashSet::new(), - &state_only_repos, - &std::collections::HashSet::new(), - None, - ); - - for relay_url in new_repos { - // Skip our own domain - if relay_url.contains(&self.ctx.domain) { - continue; - } - let action = AddFilters { - relay_url: relay_url.clone(), - items: PendingItems { - repos: state_only_repos.clone(), - root_events: std::collections::HashSet::new(), - }, - filters: filters.clone(), - }; - if let Err(e) = tx.send(action).await { - tracing::warn!( - relay = %relay_url, - error = %e, - "Failed to send AddFilters action for \ - user-submitted purgatory announcement" - ); - } else { - tracing::info!( - relay = %relay_url, - repo_id = %repo_id, - "Sent AddFilters to SyncManager for \ - user-submitted purgatory announcement relay" - ); - } - } - } - } - } - } - WritePolicyResult::Reject { status: true, // Client sees OK message: "purgatory: won't be served until git data arrives".into(), diff --git a/src/nostr/policy/mod.rs b/src/nostr/policy/mod.rs index 78a09fc..1566b6c 100644 --- a/src/nostr/policy/mod.rs +++ b/src/nostr/policy/mod.rs @@ -20,7 +20,6 @@ pub use crate::git::sync::AlignmentResult; use super::SharedDatabase; use crate::purgatory::Purgatory; -use crate::sync::{AddFilters, RepoSyncIndex}; use nostr_relay_builder::LocalRelay; use std::sync::Arc; @@ -35,16 +34,6 @@ pub struct PolicyContext { pub local_relay: Arc>>, /// Configuration reference for policy settings (includes blacklists) pub config: crate::config::Config, - /// Optional repo sync index for triggering relay discovery when announcements - /// go to purgatory via user submission (not via the sync path). - /// Wrapped in Arc for interior mutability (PolicyContext is Clone). - pub repo_sync_index: Arc>>, - /// Optional sender for AddFilters actions to SyncManager. - /// Used to trigger relay discovery when user-submitted purgatory announcements - /// are registered with StateOnly sync level. - /// Wrapped in Arc for interior mutability (PolicyContext is Clone). - pub sync_action_tx: - Arc>>>, } impl PolicyContext { @@ -62,8 +51,6 @@ impl PolicyContext { purgatory, local_relay: Arc::new(std::sync::RwLock::new(None)), config, - repo_sync_index: Arc::new(std::sync::RwLock::new(None)), - sync_action_tx: Arc::new(std::sync::RwLock::new(None)), } } @@ -81,28 +68,4 @@ impl PolicyContext { let guard = self.local_relay.read().unwrap(); guard.clone() } - - /// Set the repo sync index for relay discovery from user-submitted purgatory announcements. - pub fn set_repo_sync_index(&self, index: RepoSyncIndex) { - let mut guard = self.repo_sync_index.write().unwrap(); - *guard = Some(index); - } - - /// Get a clone of the repo sync index if it's been set. - pub fn get_repo_sync_index(&self) -> Option { - let guard = self.repo_sync_index.read().unwrap(); - guard.clone() - } - - /// Set the sync action sender for sending AddFilters actions to SyncManager. - pub fn set_sync_action_tx(&self, tx: tokio::sync::mpsc::Sender) { - let mut guard = self.sync_action_tx.write().unwrap(); - *guard = Some(tx); - } - - /// Get a clone of the sync action sender if it's been set. - pub fn get_sync_action_tx(&self) -> Option> { - let guard = self.sync_action_tx.read().unwrap(); - guard.clone() - } } diff --git a/src/purgatory/sync/context.rs b/src/purgatory/sync/context.rs index 4dbb402..3568e89 100644 --- a/src/purgatory/sync/context.rs +++ b/src/purgatory/sync/context.rs @@ -193,7 +193,6 @@ use crate::nostr::builder::SharedDatabase; use crate::nostr::events::RepositoryState; use crate::purgatory::Purgatory; use crate::sync::naughty_list::NaughtyListTracker; -use crate::sync::RepoSyncIndex; use super::functions::extract_domain; @@ -222,13 +221,6 @@ pub struct RealSyncContext { /// Naughty list tracker for git remote domains with persistent errors git_naughty_list: Arc, - - /// Optional repo sync index for upgrading sync level on promotion - repo_sync_index: Option, - - /// Optional sender for AddFilters actions to SyncManager. - /// Used after announcement promotion to trigger PR event subscription on connected relays. - sync_action_tx: Option>, } impl RealSyncContext { @@ -241,9 +233,6 @@ impl RealSyncContext { /// * `our_domain` - Our domain to exclude from clone URLs /// * `local_relay` - Local relay for WebSocket notifications /// * `git_naughty_list` - Naughty list tracker for git remote domains - /// * `repo_sync_index` - Optional repo sync index for upgrading sync level on promotion - /// * `sync_action_tx` - Optional sender for triggering filter recomputation after promotion - #[allow(clippy::too_many_arguments)] pub fn new( purgatory: Arc, database: SharedDatabase, @@ -251,8 +240,6 @@ impl RealSyncContext { our_domain: Option, local_relay: Option, git_naughty_list: Arc, - repo_sync_index: Option, - sync_action_tx: Option>, ) -> Self { Self { purgatory, @@ -261,23 +248,9 @@ impl RealSyncContext { our_domain_value: our_domain, local_relay, git_naughty_list, - repo_sync_index, - sync_action_tx, } } - /// Set the sync action sender for triggering filter recomputation after announcement promotion. - /// - /// When an announcement is promoted from purgatory to Full sync level, the SyncManager - /// needs to subscribe to PR events for that repo on all connected relays. This sender - /// is used to trigger that subscription. - pub fn set_sync_action_tx( - &mut self, - tx: tokio::sync::mpsc::Sender, - ) { - self.sync_action_tx = Some(tx); - } - /// Get reference to the git naughty list tracker pub fn git_naughty_list(&self) -> &Arc { &self.git_naughty_list @@ -509,98 +482,9 @@ impl SyncContext for RealSyncContext { self.local_relay.as_ref(), &self.purgatory, &self.git_data_path, - self.repo_sync_index.clone(), ) .await?; - // If announcements were promoted (now Full sync level), notify SyncManager to - // recompute filters so PR event subscriptions are created on connected relays. - if result.announcements_released > 0 { - if let (Some(ref tx), Some(ref repo_sync_index)) = - (&self.sync_action_tx, &self.repo_sync_index) - { - let index = repo_sync_index.read().await; - for (repo_id, needs) in index.iter() { - if needs.sync_level == crate::sync::SyncLevel::Full - && !needs.root_events.is_empty() - { - // Send AddFilters for Full repos with root events - for relay_url in &needs.relays { - if let Some(ref domain) = self.our_domain_value { - if relay_url.contains(domain.as_str()) { - continue; - } - } - let full_repos: std::collections::HashSet = - std::iter::once(repo_id.clone()).collect(); - let filters = - crate::sync::filters::build_sync_level_aware_filters( - &full_repos, - &std::collections::HashSet::new(), - &needs.root_events, - None, - ); - let action = crate::sync::AddFilters { - relay_url: relay_url.clone(), - items: crate::sync::PendingItems { - repos: full_repos.clone(), - root_events: needs.root_events.clone(), - }, - filters, - }; - if let Err(e) = tx.send(action).await { - debug!( - relay = %relay_url, - error = %e, - "Failed to send AddFilters after announcement promotion" - ); - } else { - debug!( - relay = %relay_url, - repo_id = %repo_id, - "Sent AddFilters to SyncManager after announcement promotion" - ); - } - } - } else if needs.sync_level == crate::sync::SyncLevel::Full { - // Even without root_events, send empty repo filter to ensure - // Layer 2 subscriptions (PR events) are set up - for relay_url in &needs.relays { - if let Some(ref domain) = self.our_domain_value { - if relay_url.contains(domain.as_str()) { - continue; - } - } - let full_repos: std::collections::HashSet = - std::iter::once(repo_id.clone()).collect(); - let filters = - crate::sync::filters::build_sync_level_aware_filters( - &full_repos, - &std::collections::HashSet::new(), - &std::collections::HashSet::new(), - None, - ); - let action = crate::sync::AddFilters { - relay_url: relay_url.clone(), - items: crate::sync::PendingItems { - repos: full_repos.clone(), - root_events: std::collections::HashSet::new(), - }, - filters, - }; - if let Err(e) = tx.send(action).await { - debug!( - relay = %relay_url, - error = %e, - "Failed to send AddFilters (no root_events) after announcement promotion" - ); - } - } - } - } - } - } - // Convert from git::sync::ProcessResult to our ProcessResult Ok(ProcessResult { states_released: result.states_released, -- cgit v1.2.3 From e22021f0b248ebcf3bd09210d59b2cdb4701032f Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Wed, 18 Feb 2026 19:41:29 +0000 Subject: fix: simplify purgatory sync - fix SelfSubscriber sync_level upgrade and negentropy fallback Three targeted fixes for purgatory announcement sync: 1. SelfSubscriber sync_level upgrade: After or_insert_with in process_batch, always set entry.sync_level = SyncLevel::Full so that when a promoted announcement is broadcast via notify_event and SelfSubscriber receives it, an existing StateOnly entry gets upgraded to Full and PR event subscriptions are triggered immediately (not delayed up to 24h). 2. Negentropy fallback filter split: In handle_eose, when falling back from negentropy to REQ+EOSE, split batch_repos by SyncLevel and call build_sync_level_aware_filters instead of build_layer2_and_layer3_filters. Prevents StateOnly (purgatory) repos from getting Layer 2 #a/#A/#q filters prematurely, which caused nostr-sdk client deduplication to permanently drop PR events after orphan rejection. 3. Recompute sync filters after announcement batch EOSE: Add recompute_new_sync_filters_for_relay calls at all three batch-completion paths in handle_eose for generic filter (announcement) batches. This triggers state-only subscriptions for any purgatory repos registered during that batch, fixing the 24h delay before state event sync starts. 4. User-submitted purgatory announcements: Add repo_sync_index field to PolicyContext with setter/getter, wire in main.rs after SyncManager creation, and register in AcceptPurgatory handler so user-submitted announcements get StateOnly sync started immediately. 5. Update archive tests: test_archive_without_state_events_does_not_sync_git updated to reflect that StateOnly subscription now proactively fetches state events from source relays. test_archive_read_only_creates_bare_repo un-ignored as it now works end-to-end. --- src/main.rs | 7 +++++ src/nostr/builder.rs | 54 +++++++++++++++++++++++++++++++++++++ src/nostr/policy/mod.rs | 19 +++++++++++++ src/sync/mod.rs | 66 ++++++++++++++++++++++++++++++++++++++++++--- src/sync/self_subscriber.rs | 4 +++ tests/archive_read_only.rs | 63 +++++++++++++++++++++++++++---------------- 6 files changed, 187 insertions(+), 26 deletions(-) (limited to 'src/nostr/policy/mod.rs') diff --git a/src/main.rs b/src/main.rs index ab6ede7..ebe05a3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -132,6 +132,13 @@ async fn main() -> Result<()> { // Get a reference to the rejected events index for shutdown persistence let shutdown_rejected_index = sync_manager.rejected_events_index(); + // Wire repo_sync_index into write policy so user-submitted purgatory announcements + // get registered for state event sync immediately (Fix 3). + let repo_sync_index = sync_manager.repo_sync_index(); + relay_with_db + .write_policy + .set_repo_sync_index(repo_sync_index); + tokio::spawn(async move { sync_manager.run().await; }); diff --git a/src/nostr/builder.rs b/src/nostr/builder.rs index aff12a6..8d1e461 100644 --- a/src/nostr/builder.rs +++ b/src/nostr/builder.rs @@ -17,6 +17,7 @@ use crate::nostr::policy::{ AnnouncementPolicy, AnnouncementResult, PolicyContext, PrEventPolicy, ReferenceResult, RelatedEventPolicy, StatePolicy, StateResult, }; +use crate::sync::{RepoSyncIndex, RepoSyncNeeds, SyncLevel}; /// Type alias for the shared database used by the relay pub type SharedDatabase = Arc; @@ -98,6 +99,14 @@ impl Nip34WritePolicy { self.ctx.set_local_relay(relay); } + /// Set the repo sync index so that user-submitted purgatory announcements can + /// be registered for state event sync immediately. + /// + /// This must be called after SyncManager is created. + pub fn set_repo_sync_index(&self, index: RepoSyncIndex) { + self.ctx.set_repo_sync_index(index); + } + /// Handle repository announcement event async fn handle_announcement(&self, event: &Event) -> WritePolicyResult { let event_id_str = event.id.to_bech32().unwrap_or_else(|_| event.id.to_hex()); @@ -146,6 +155,51 @@ impl Nip34WritePolicy { "Accepted announcement to purgatory: {} (waiting for git data)", event_id_str ); + + // Register repo in repo_sync_index with StateOnly level so that + // state event sync starts promptly via the next batch EOSE recompute. + // This handles user-submitted purgatory announcements - the SelfSubscriber + // only sees DB events, so it won't pick these up automatically. + if let Some(repo_sync_index) = self.ctx.get_repo_sync_index() { + if let Ok(announcement) = + RepositoryAnnouncement::from_event(event.clone()) + { + use std::collections::HashSet; + let repo_id = format!( + "30617:{}:{}", + event.pubkey, + announcement.identifier + ); + + // Extract relay URLs from the announcement event tags + let relays: HashSet = event + .tags + .iter() + .flat_map(|tag| { + let tag_vec = tag.as_slice(); + if !tag_vec.is_empty() && tag_vec[0] == "relays" { + tag_vec[1..].iter().map(|s| s.to_string()).collect::>() + } else { + vec![] + } + }) + .collect(); + + let mut index = repo_sync_index.write().await; + index.entry(repo_id.clone()).or_insert_with(|| RepoSyncNeeds { + relays, + root_events: HashSet::new(), + sync_level: SyncLevel::StateOnly, + }); + drop(index); + + tracing::debug!( + repo_id = %repo_id, + "Registered purgatory announcement in repo_sync_index as StateOnly" + ); + } + } + WritePolicyResult::Reject { status: true, // Client sees OK message: "purgatory: won't be served until git data arrives".into(), diff --git a/src/nostr/policy/mod.rs b/src/nostr/policy/mod.rs index 1566b6c..c958586 100644 --- a/src/nostr/policy/mod.rs +++ b/src/nostr/policy/mod.rs @@ -20,6 +20,7 @@ pub use crate::git::sync::AlignmentResult; use super::SharedDatabase; use crate::purgatory::Purgatory; +use crate::sync::RepoSyncIndex; use nostr_relay_builder::LocalRelay; use std::sync::Arc; @@ -34,6 +35,8 @@ pub struct PolicyContext { pub local_relay: Arc>>, /// Configuration reference for policy settings (includes blacklists) pub config: crate::config::Config, + /// Repo sync index for registering purgatory announcements (set after SyncManager creation) + pub repo_sync_index: Arc>>, } impl PolicyContext { @@ -51,6 +54,7 @@ impl PolicyContext { purgatory, local_relay: Arc::new(std::sync::RwLock::new(None)), config, + repo_sync_index: Arc::new(std::sync::RwLock::new(None)), } } @@ -68,4 +72,19 @@ impl PolicyContext { let guard = self.local_relay.read().unwrap(); guard.clone() } + + /// Set the repo sync index after SyncManager has been created. + /// + /// This allows purgatory announcements submitted by users to be registered + /// in the sync index so state event sync starts promptly. + pub fn set_repo_sync_index(&self, index: RepoSyncIndex) { + let mut guard = self.repo_sync_index.write().unwrap(); + *guard = Some(index); + } + + /// Get a clone of the repo sync index if it has been set. + pub fn get_repo_sync_index(&self) -> Option { + let guard = self.repo_sync_index.read().unwrap(); + guard.clone() + } } diff --git a/src/sync/mod.rs b/src/sync/mod.rs index 519017b..916e2b0 100644 --- a/src/sync/mod.rs +++ b/src/sync/mod.rs @@ -700,6 +700,14 @@ impl SyncManager { self.rejected_events_index.save_to_disk(path) } + /// Get a clone of the repo sync index Arc. + /// + /// This allows the write policy to register user-submitted purgatory announcements + /// in the sync index so that state event sync starts promptly. + pub fn repo_sync_index(&self) -> RepoSyncIndex { + self.repo_sync_index.clone() + } + /// Handle EOSE (End Of Stored Events) for a subscription /// /// This method: @@ -951,9 +959,29 @@ impl SyncManager { // Create REQ+EOSE subscriptions using original semantic filters // This queries by kind/author/tags instead of by ID, which may - // succeed even when ID-based queries fail - let fallback_filters = filters::build_layer2_and_layer3_filters( - &batch_repos, + // succeed even when ID-based queries fail. + // Split batch_repos by SyncLevel to avoid sending Layer 2 filters + // (#a/#A/#q) for StateOnly (purgatory) repos - those PRs would be + // rejected as orphan and then silently dropped by nostr-sdk deduplication. + let (full_repos, state_only_repos) = { + let repo_index = self.repo_sync_index.read().await; + let mut full = HashSet::new(); + let mut state_only = HashSet::new(); + for repo_ref in &batch_repos { + match repo_index.get(repo_ref).map(|n| n.sync_level) { + Some(SyncLevel::StateOnly) => { + state_only.insert(repo_ref.clone()); + } + _ => { + full.insert(repo_ref.clone()); + } + } + } + (full, state_only) + }; + let fallback_filters = filters::build_sync_level_aware_filters( + &full_repos, + &state_only_repos, &batch_root_events, None, ); @@ -1033,12 +1061,24 @@ impl SyncManager { { let mut completed_batch = batches.remove(idx); completed_batch.failed = true; // Mark as failed + let is_generic = + completed_batch.items.repos.is_empty() + && completed_batch.items.root_events.is_empty(); if batches.is_empty() { pending.remove(&relay_url_for_fallback); } drop(pending); self.confirm_batch(&relay_url_for_fallback, completed_batch) .await; + // For generic filter (announcement) batches, recompute filters + // so any purgatory repos registered during this batch get + // state-only subscriptions triggered. + if is_generic { + self.recompute_new_sync_filters_for_relay( + &relay_url_for_fallback, + ) + .await; + } } } return; @@ -1132,12 +1172,24 @@ impl SyncManager { if let Some(batches) = pending.get_mut(&relay_url_for_retry) { if let Some(idx) = batches.iter().position(|b| b.batch_id == batch_id) { let completed_batch = batches.remove(idx); + let is_generic = + completed_batch.items.repos.is_empty() + && completed_batch.items.root_events.is_empty(); if batches.is_empty() { pending.remove(&relay_url_for_retry); } drop(pending); self.confirm_batch(&relay_url_for_retry, completed_batch) .await; + // For generic filter (announcement) batches, recompute filters + // so any purgatory repos registered during this batch get + // state-only subscriptions triggered. + if is_generic { + self.recompute_new_sync_filters_for_relay( + &relay_url_for_retry, + ) + .await; + } } } return; @@ -1148,6 +1200,8 @@ impl SyncManager { // 3. Batch complete - extract and remove let completed_batch = batches.remove(batch_idx); + let is_generic = completed_batch.items.repos.is_empty() + && completed_batch.items.root_events.is_empty(); // Clean up empty relay entry if batches.is_empty() { @@ -1159,6 +1213,12 @@ impl SyncManager { // 4. Confirm the batch (moves items to RelayState) self.confirm_batch(relay_url, completed_batch).await; + + // 5. For generic filter (announcement) batches, recompute sync filters so any + // purgatory repos registered during this batch get state-only subscriptions triggered. + if is_generic { + self.recompute_new_sync_filters_for_relay(relay_url).await; + } } /// Confirm a completed batch by moving items to RelayState diff --git a/src/sync/self_subscriber.rs b/src/sync/self_subscriber.rs index db16c62..70c3dbf 100644 --- a/src/sync/self_subscriber.rs +++ b/src/sync/self_subscriber.rs @@ -478,6 +478,10 @@ impl SelfSubscriber { root_events: HashSet::new(), sync_level: SyncLevel::Full, }); + // Upgrade sync_level to Full - this handles the case where the entry + // already exists as StateOnly (purgatory announcement) and is now being + // promoted (git data arrived and the event was broadcast via notify_event). + entry.sync_level = SyncLevel::Full; entry.relays.extend(needs.relays); entry.root_events.extend(needs.root_events); diff --git a/tests/archive_read_only.rs b/tests/archive_read_only.rs index e388ae5..069b3b7 100644 --- a/tests/archive_read_only.rs +++ b/tests/archive_read_only.rs @@ -55,7 +55,6 @@ use std::time::Duration; /// 5. Verify bare repository is created and git data is synced /// 6. Verify git pushes are rejected (read-only mode) #[tokio::test] -#[ignore] // Requires SyncLevel implementation (Phase 3) - purgatory announcements don't trigger per-repo sync yet async fn test_archive_read_only_creates_bare_repo() { // 1. Start source relay let source_relay = TestRelay::start().await; @@ -264,24 +263,24 @@ async fn test_archive_read_only_creates_bare_repo() { source_relay.stop().await; } -/// Test that archive mode without state events does NOT sync git data. +/// Test that archive mode proactively syncs state events and git data +/// when the source relay has state events available. /// -/// This verifies the security model: archive mode only syncs git data -/// when there are state events to validate against. +/// With StateOnly sync now implemented, purgatory announcements subscribe +/// to state events from the relays listed in the announcement. This means +/// the archive relay will: +/// 1. Sync the announcement → purgatory → register as StateOnly in repo_sync_index +/// 2. Subscribe to state events (kind 30618) on source relay +/// 3. Receive the state event → purgatory sync triggered +/// 4. Fetch git data from source relay's clone URL /// -/// With announcement purgatory, the flow is: -/// 1. Send announcement to source relay (goes to purgatory) -/// 2. Send state event to source relay (goes to purgatory) -/// 3. Push git data to source relay (promotes announcement and state event) -/// 4. Start archive relay with sync from source -/// 5. Archive relay syncs the promoted announcement -/// 6. Verify git data is NOT synced (archive has no state event to authorize git fetch) +/// This test verifies the full sync chain works end-to-end for archive mode. #[tokio::test] -async fn test_archive_without_state_events_does_not_sync_git() { +async fn test_archive_syncs_state_events_and_git_data_via_state_only_subscription() { // 1. Start source relay let source_relay = TestRelay::start().await; let keys = Keys::generate(); - let identifier = "archive-no-state-repo"; + let identifier = "archive-state-only-sync-repo"; // Pre-allocate archive relay port let archive_port = TestRelay::find_free_port(); @@ -295,6 +294,7 @@ async fn test_archive_without_state_events_does_not_sync_git() { let npub = keys.public_key().to_bech32().expect("Failed to get npub"); // 3. Create and send announcement listing BOTH relays + // The archive relay will subscribe to state events on BOTH listed relays let announcement = create_repo_announcement( &keys, &[&source_relay.domain(), &archive_domain], @@ -337,6 +337,8 @@ async fn test_archive_without_state_events_does_not_sync_git() { ) .expect("Failed to create state event"); + let state_event_id = state_event.id; + source_client .send_event(&state_event) .await @@ -348,9 +350,12 @@ async fn test_archive_without_state_events_does_not_sync_git() { push_to_relay(temp_dir.path(), &source_relay.domain(), &npub, identifier) .expect("Push to source should succeed"); - tokio::time::sleep(Duration::from_millis(500)).await; + // Wait for state event to be promoted on source relay + wait_for_event_served(source_relay.url(), &state_event_id, Duration::from_secs(5)) + .await + .expect("State event should be served on source relay after push"); - // 6. Start archive relay (without state event - we don't send state event to archive) + // 6. Start archive relay - StateOnly subscription will proactively fetch state events let archive_relay = TestRelay::start_with_archive_and_sync( archive_port, Some(source_relay.url().to_string()), @@ -360,15 +365,28 @@ async fn test_archive_without_state_events_does_not_sync_git() { ) .await; - // Wait for sync + // Wait for sync connection wait_for_sync_connection(archive_relay.url(), 1, Duration::from_secs(5)) .await .expect("Sync connection should establish"); - // Give time for sync to fetch announcement - tokio::time::sleep(Duration::from_secs(3)).await; + // 7. Wait for state event to be served on archive relay + // The StateOnly subscription fetches the state event from source relay, + // which then triggers purgatory sync and git data fetch. + let found = wait_for_event_served( + archive_relay.url(), + &state_event_id, + Duration::from_secs(30), // Allow time for sync + git fetch + ) + .await; + + assert!( + found.is_ok(), + "State event should be served on archive after StateOnly subscription fetches it: {:?}", + found.err() + ); - // 7. Verify bare repository was created (announcement was synced and accepted to purgatory) + // 8. Verify bare repository was created let repo_path = archive_relay .git_data_path() .join(format!("{}/{}.git", npub, identifier)); @@ -378,8 +396,7 @@ async fn test_archive_without_state_events_does_not_sync_git() { "Bare repository should be created for archive announcement" ); - // 8. Verify git data was NOT synced (no state events on archive to trigger git fetch) - // Check that the commit does NOT exist in the archive relay's repo + // 9. Verify git data was synced via the state event chain let output = tokio::process::Command::new("git") .args(["cat-file", "-t", &commit_hash]) .current_dir(&repo_path) @@ -389,8 +406,8 @@ async fn test_archive_without_state_events_does_not_sync_git() { let commit_exists = output.map(|o| o.status.success()).unwrap_or(false); assert!( - !commit_exists, - "Git data should NOT be synced without state events (security: validates against Nostr state)" + commit_exists, + "Git data should be synced via StateOnly subscription → state event → git fetch chain" ); // Cleanup -- cgit v1.2.3 From ee113a654e2971a6ebdb07398cc5638dbe59b48c Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Wed, 18 Feb 2026 20:32:13 +0000 Subject: fix: replace repo_sync_index wiring with purgatory announcement sync timer Instead of threading repo_sync_index through PolicyContext/builder.rs/main.rs to handle user-submitted purgatory announcements, add a simple background timer (run_purgatory_announcement_sync, every 5s) that scans the purgatory for announcement entries and registers them in repo_sync_index as StateOnly. This is simpler and covers both flows: - Sync-path announcements: inline registration still happens during event processing (sync/mod.rs:1839+), timer provides a safety net - User-submitted announcements: SelfSubscriber never sees them (rejected from DB), timer is the primary registration path The timer calls sync_purgatory_announcements_to_index() which: 1. Snapshots purgatory via new announcements_for_sync() public method 2. Or_inserts StateOnly entries (never downgrades Full entries) 3. Detects newly added relay URLs and calls handle_new_sync_filters to connect and subscribe - fixing the failing test that expected relay discovery from a user-submitted purgatory announcement Removes: repo_sync_index field from PolicyContext, set/get_repo_sync_index methods, set_repo_sync_index on Nip34WritePolicy, wiring in main.rs, and the inline AcceptPurgatory registration block in builder.rs. --- src/main.rs | 7 --- src/nostr/builder.rs | 54 +-------------------- src/nostr/policy/mod.rs | 19 -------- src/purgatory/mod.rs | 17 +++++++ src/sync/mod.rs | 125 ++++++++++++++++++++++++++++++++++++++++++++---- 5 files changed, 134 insertions(+), 88 deletions(-) (limited to 'src/nostr/policy/mod.rs') diff --git a/src/main.rs b/src/main.rs index ebe05a3..ab6ede7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -132,13 +132,6 @@ async fn main() -> Result<()> { // Get a reference to the rejected events index for shutdown persistence let shutdown_rejected_index = sync_manager.rejected_events_index(); - // Wire repo_sync_index into write policy so user-submitted purgatory announcements - // get registered for state event sync immediately (Fix 3). - let repo_sync_index = sync_manager.repo_sync_index(); - relay_with_db - .write_policy - .set_repo_sync_index(repo_sync_index); - tokio::spawn(async move { sync_manager.run().await; }); diff --git a/src/nostr/builder.rs b/src/nostr/builder.rs index 8d1e461..c2d4939 100644 --- a/src/nostr/builder.rs +++ b/src/nostr/builder.rs @@ -17,7 +17,7 @@ use crate::nostr::policy::{ AnnouncementPolicy, AnnouncementResult, PolicyContext, PrEventPolicy, ReferenceResult, RelatedEventPolicy, StatePolicy, StateResult, }; -use crate::sync::{RepoSyncIndex, RepoSyncNeeds, SyncLevel}; + /// Type alias for the shared database used by the relay pub type SharedDatabase = Arc; @@ -99,14 +99,6 @@ impl Nip34WritePolicy { self.ctx.set_local_relay(relay); } - /// Set the repo sync index so that user-submitted purgatory announcements can - /// be registered for state event sync immediately. - /// - /// This must be called after SyncManager is created. - pub fn set_repo_sync_index(&self, index: RepoSyncIndex) { - self.ctx.set_repo_sync_index(index); - } - /// Handle repository announcement event async fn handle_announcement(&self, event: &Event) -> WritePolicyResult { let event_id_str = event.id.to_bech32().unwrap_or_else(|_| event.id.to_hex()); @@ -156,50 +148,6 @@ impl Nip34WritePolicy { event_id_str ); - // Register repo in repo_sync_index with StateOnly level so that - // state event sync starts promptly via the next batch EOSE recompute. - // This handles user-submitted purgatory announcements - the SelfSubscriber - // only sees DB events, so it won't pick these up automatically. - if let Some(repo_sync_index) = self.ctx.get_repo_sync_index() { - if let Ok(announcement) = - RepositoryAnnouncement::from_event(event.clone()) - { - use std::collections::HashSet; - let repo_id = format!( - "30617:{}:{}", - event.pubkey, - announcement.identifier - ); - - // Extract relay URLs from the announcement event tags - let relays: HashSet = event - .tags - .iter() - .flat_map(|tag| { - let tag_vec = tag.as_slice(); - if !tag_vec.is_empty() && tag_vec[0] == "relays" { - tag_vec[1..].iter().map(|s| s.to_string()).collect::>() - } else { - vec![] - } - }) - .collect(); - - let mut index = repo_sync_index.write().await; - index.entry(repo_id.clone()).or_insert_with(|| RepoSyncNeeds { - relays, - root_events: HashSet::new(), - sync_level: SyncLevel::StateOnly, - }); - drop(index); - - tracing::debug!( - repo_id = %repo_id, - "Registered purgatory announcement in repo_sync_index as StateOnly" - ); - } - } - WritePolicyResult::Reject { status: true, // Client sees OK message: "purgatory: won't be served until git data arrives".into(), diff --git a/src/nostr/policy/mod.rs b/src/nostr/policy/mod.rs index c958586..1566b6c 100644 --- a/src/nostr/policy/mod.rs +++ b/src/nostr/policy/mod.rs @@ -20,7 +20,6 @@ pub use crate::git::sync::AlignmentResult; use super::SharedDatabase; use crate::purgatory::Purgatory; -use crate::sync::RepoSyncIndex; use nostr_relay_builder::LocalRelay; use std::sync::Arc; @@ -35,8 +34,6 @@ pub struct PolicyContext { pub local_relay: Arc>>, /// Configuration reference for policy settings (includes blacklists) pub config: crate::config::Config, - /// Repo sync index for registering purgatory announcements (set after SyncManager creation) - pub repo_sync_index: Arc>>, } impl PolicyContext { @@ -54,7 +51,6 @@ impl PolicyContext { purgatory, local_relay: Arc::new(std::sync::RwLock::new(None)), config, - repo_sync_index: Arc::new(std::sync::RwLock::new(None)), } } @@ -72,19 +68,4 @@ impl PolicyContext { let guard = self.local_relay.read().unwrap(); guard.clone() } - - /// Set the repo sync index after SyncManager has been created. - /// - /// This allows purgatory announcements submitted by users to be registered - /// in the sync index so state event sync starts promptly. - pub fn set_repo_sync_index(&self, index: RepoSyncIndex) { - let mut guard = self.repo_sync_index.write().unwrap(); - *guard = Some(index); - } - - /// Get a clone of the repo sync index if it has been set. - pub fn get_repo_sync_index(&self) -> Option { - let guard = self.repo_sync_index.read().unwrap(); - guard.clone() - } } diff --git a/src/purgatory/mod.rs b/src/purgatory/mod.rs index 3b5514b..1894738 100644 --- a/src/purgatory/mod.rs +++ b/src/purgatory/mod.rs @@ -680,6 +680,23 @@ impl Purgatory { self.announcement_purgatory.len() } + /// Collect (repo_id, relay_urls) for all announcements currently in purgatory. + /// + /// Returns a vec of `(repo_id, relay_urls)` where `repo_id` is the addressable + /// coordinate string `"30617:{pubkey_hex}:{identifier}"`. Used by the purgatory + /// announcement sync timer to register StateOnly entries in `repo_sync_index`. + pub fn announcements_for_sync(&self) -> Vec<(String, HashSet)> { + self.announcement_purgatory + .iter() + .map(|entry| { + let (owner, identifier) = entry.key(); + let repo_id = format!("30617:{}:{}", owner.to_hex(), identifier); + let relays = entry.value().relays.clone(); + (repo_id, relays) + }) + .collect() + } + /// Get all event IDs currently stored in purgatory AND previously expired events. /// /// Returns a HashSet of all event IDs for: diff --git a/src/sync/mod.rs b/src/sync/mod.rs index 916e2b0..ed5b6e7 100644 --- a/src/sync/mod.rs +++ b/src/sync/mod.rs @@ -397,6 +397,37 @@ async fn run_daily_timer( } } +/// Background task that periodically syncs purgatory announcements into repo_sync_index. +/// +/// Runs every 5 seconds. For each announcement currently in purgatory, ensures there +/// is a `StateOnly` entry in `repo_sync_index`. New entries trigger `handle_new_sync_filters` +/// which connects to the relay URLs listed in the announcement and subscribes to state +/// events (kind 30618). +/// +/// This covers two cases: +/// - Sync-path announcements: registered inline during event processing, but this +/// provides a safety net in case the inline registration was missed. +/// - User-submitted purgatory announcements: the SelfSubscriber never sees them +/// (they're rejected from DB), so this timer is the primary registration path. +async fn run_purgatory_announcement_sync( + sync_manager: Arc>, + mut shutdown_rx: broadcast::Receiver<()>, +) { + let interval = Duration::from_secs(5); + loop { + tokio::select! { + _ = tokio::time::sleep(interval) => { + let mut manager = sync_manager.lock().await; + manager.sync_purgatory_announcements_to_index().await; + } + _ = shutdown_rx.recv() => { + tracing::debug!("Purgatory announcement sync timer received shutdown signal"); + break; + } + } + } +} + // Combined Health and Metrics Checker /// Background task for cleaning up expired entries from the rejected events index @@ -700,14 +731,6 @@ impl SyncManager { self.rejected_events_index.save_to_disk(path) } - /// Get a clone of the repo sync index Arc. - /// - /// This allows the write policy to register user-submitted purgatory announcements - /// in the sync index so that state event sync starts promptly. - pub fn repo_sync_index(&self) -> RepoSyncIndex { - self.repo_sync_index.clone() - } - /// Handle EOSE (End Of Stored Events) for a subscription /// /// This method: @@ -1560,7 +1583,17 @@ impl SyncManager { run_rejected_index_cleanup(cleanup_manager, cleanup_shutdown).await; }); - // 11. Main loop - handle actions from self-subscriber, disconnect, EOSE, and connect notifications + // 11. Spawn purgatory announcement sync timer (every 5s) + // Ensures purgatory announcements (including user-submitted ones that never + // touch the DB) are registered in repo_sync_index as StateOnly so that + // state event subscriptions are established on their listed relay URLs. + let purgatory_sync_manager = Arc::clone(&sync_manager); + let purgatory_sync_shutdown = shutdown_tx.subscribe(); + tokio::spawn(async move { + run_purgatory_announcement_sync(purgatory_sync_manager, purgatory_sync_shutdown).await; + }); + + // 12. Main loop - handle actions from self-subscriber, disconnect, EOSE, and connect notifications loop { // Wait for an event without holding the lock tokio::select! { @@ -2419,6 +2452,80 @@ impl SyncManager { } } + /// Sync purgatory announcements into repo_sync_index as StateOnly entries. + /// + /// Called periodically by the purgatory announcement sync timer (every 5s). + /// For each announcement currently in purgatory, ensures a `StateOnly` entry + /// exists in `repo_sync_index`. New entries are then picked up by + /// `handle_new_sync_filters` which connects to listed relay URLs and subscribes + /// to state events for that repo. + /// + /// Idempotent: existing entries are not downgraded (a promoted Full entry stays Full). + async fn sync_purgatory_announcements_to_index(&mut self) { + use crate::sync::algorithms::{compute_actions, derive_relay_targets}; + + // Collect all purgatory announcements (snapshot - no async holds) + let announcements = self.purgatory.announcements_for_sync(); + + if announcements.is_empty() { + return; + } + + // Register any new entries in repo_sync_index as StateOnly + let mut new_relay_urls: std::collections::HashSet = std::collections::HashSet::new(); + { + let mut index = self.repo_sync_index.write().await; + for (repo_id, relays) in &announcements { + let entry = index.entry(repo_id.clone()).or_insert_with(|| { + tracing::debug!( + repo_id = %repo_id, + "Registering purgatory announcement in repo_sync_index as StateOnly" + ); + RepoSyncNeeds { + relays: std::collections::HashSet::new(), + root_events: std::collections::HashSet::new(), + sync_level: SyncLevel::StateOnly, + } + }); + // Don't downgrade an already-Full entry + // Add any new relay URLs + for relay in relays { + if entry.relays.insert(relay.clone()) { + new_relay_urls.insert(relay.clone()); + } + } + } + } + + if new_relay_urls.is_empty() { + return; + } + + // For any relay URLs that are new, compute and send AddFilters actions + let all_targets = { + let repo_index = self.repo_sync_index.read().await; + derive_relay_targets(&repo_index) + }; + + let actions = { + let pending_index = self.pending_sync_index.read().await; + let relay_index = self.relay_sync_index.read().await; + compute_actions(&all_targets, &pending_index, &relay_index) + }; + + for action in actions { + // Only act on relays that have new URLs (avoids redundant work) + if new_relay_urls.contains(&action.relay_url) { + tracing::info!( + relay = %action.relay_url, + repos = action.items.repos.len(), + "Purgatory sync timer: connecting to new relay from purgatory announcement" + ); + self.handle_new_sync_filters(action).await; + } + } + } + /// Handle a relay disconnection /// /// This method is called when the event loop terminates and sends a disconnect notification. -- cgit v1.2.3 From 65ac6ef83205c41653e6ffe2acd664f968926fb2 Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Mon, 23 Feb 2026 13:29:47 +0000 Subject: feat: remove purgatory announcements on NIP-09 deletion events Kind 5 deletion events signed by the announcement author now evict the corresponding purgatory entry and delete the bare repository from disk. Both NIP-09 reference styles are supported: - e tag (event ID): matches the purgatory entry whose event ID equals the tag value - a tag (coordinate 30617::): matches by coordinate, only removes entries with created_at <= deletion event created_at per NIP-09 spec Author-only enforcement: coordinate pubkey and e-tag owner must match the deletion event pubkey; third-party deletion attempts are silently ignored. Includes 6 unit tests and 2 integration tests (event ID and coordinate paths). --- grasp-audit/src/specs/grasp01/purgatory.rs | 199 +++++++++++++ src/nostr/builder.rs | 8 +- src/nostr/policy/deletion.rs | 438 +++++++++++++++++++++++++++++ src/nostr/policy/mod.rs | 2 + tests/purgatory.rs | 7 + 5 files changed, 652 insertions(+), 2 deletions(-) create mode 100644 src/nostr/policy/deletion.rs (limited to 'src/nostr/policy/mod.rs') diff --git a/grasp-audit/src/specs/grasp01/purgatory.rs b/grasp-audit/src/specs/grasp01/purgatory.rs index 9c4b401..9d97d3b 100644 --- a/grasp-audit/src/specs/grasp01/purgatory.rs +++ b/grasp-audit/src/specs/grasp01/purgatory.rs @@ -46,6 +46,12 @@ impl PurgatoryTests { results.add(Self::test_bare_repo_exists_for_purgatory_announcement(client).await); results.add(Self::test_state_event_accepted_for_purgatory_announcement(client).await); + // Deletion event tests (NIP-09) + results.add(Self::test_deletion_by_event_id_removes_purgatory_announcement(client).await); + results.add( + Self::test_deletion_by_coordinate_removes_purgatory_announcement(client).await, + ); + // State event purgatory tests (already implemented) results.add(Self::test_state_event_not_served_before_git_data(client).await); results.add(Self::test_state_event_served_after_git_push(client).await); @@ -646,6 +652,199 @@ impl PurgatoryTests { }) .await } + // ============================================================ + // Deletion Event Tests (NIP-09) + // ============================================================ + + /// Test: Kind 5 deletion event by event ID removes purgatory announcement + /// + /// Spec: NIP-09 + /// "A special event with kind 5... having a list of one or more `e` or `a` tags, + /// each referencing an event the author is requesting to be deleted." + /// + /// This test verifies: + /// 1. Send a valid repository announcement (enters purgatory) + /// 2. Send a kind 5 deletion event referencing the announcement by event ID + /// 3. The announcement is no longer in purgatory (git push would fail) + /// 4. The deletion event itself is accepted by the relay + pub async fn test_deletion_by_event_id_removes_purgatory_announcement( + client: &AuditClient, + ) -> TestResult { + TestResult::new( + "deletion_by_event_id_removes_purgatory_announcement", + SpecRef::PurgatoryAcceptUntilGitData, + "Kind 5 deletion by event ID SHOULD remove a purgatory announcement", + ) + .run(|| async { + let ctx = TestContext::new(client); + + // Send announcement to purgatory + let repo = ctx + .get_fixture(FixtureKind::ValidRepoSent) + .await + .map_err(|e| format!("Failed to create repo announcement: {}", e))?; + + let repo_id = repo + .tags + .iter() + .find(|t| t.kind() == TagKind::d()) + .and_then(|t| t.content()) + .ok_or("Missing d tag in repo announcement")? + .to_string(); + + // Verify it's in purgatory (not served) + tokio::time::sleep(Duration::from_millis(300)).await; + if client.is_event_on_relay(repo.id).await.map_err(|e| e.to_string())? { + return Err( + "Announcement was served immediately - purgatory not working".to_string(), + ); + } + + // Build and send kind 5 deletion event referencing the announcement by event ID + let deletion = client + .event_builder(Kind::EventDeletion, "") + .tag(Tag::event(repo.id)) + .tag(Tag::custom( + TagKind::custom("k"), + vec!["30617"], + )) + .build(client.keys()) + .map_err(|e| format!("Failed to build deletion event: {}", e))?; + + client + .send_event(deletion) + .await + .map_err(|e| format!("Relay rejected deletion event: {}", e))?; + + tokio::time::sleep(Duration::from_millis(300)).await; + + // Verify the announcement can no longer be promoted by attempting a git push. + // We check this indirectly: if the purgatory entry was removed, a subsequent + // git push to the repo path should fail (no bare repo). + // For the integration test we verify the announcement is still not served + // (it was never promoted) and that the deletion event was accepted. + // The bare-repo deletion is verified by attempting a git clone. + let http_url = AuditClient::ws_to_http_url(&client.relay_url().await.map_err(|e| e.to_string())?) + .map_err(|e| e.to_string())?; + let clone_url = format!( + "{}/{}/{}.git", + http_url, + client.public_key().to_bech32().map_err(|e| e.to_string())?, + repo_id + ); + + // git ls-remote should fail (bare repo deleted) + let output = std::process::Command::new("git") + .args(["ls-remote", &clone_url]) + .output() + .map_err(|e| format!("Failed to run git ls-remote: {}", e))?; + + if output.status.success() { + return Err(format!( + "Bare repo still exists after deletion event. \ + Expected git ls-remote to fail for {}", + clone_url + )); + } + + Ok(()) + }) + .await + } + + /// Test: Kind 5 deletion event by `a` tag coordinate removes purgatory announcement + /// + /// Spec: NIP-09 + /// "When an `a` tag is used, relays SHOULD delete all versions of the replaceable + /// event up to the `created_at` timestamp of the deletion request event." + /// + /// This test verifies: + /// 1. Send a valid repository announcement (enters purgatory) + /// 2. Send a kind 5 deletion event referencing the announcement by coordinate + /// (`30617::`) + /// 3. The announcement is no longer in purgatory + pub async fn test_deletion_by_coordinate_removes_purgatory_announcement( + client: &AuditClient, + ) -> TestResult { + TestResult::new( + "deletion_by_coordinate_removes_purgatory_announcement", + SpecRef::PurgatoryAcceptUntilGitData, + "Kind 5 deletion by `a` coordinate SHOULD remove a purgatory announcement", + ) + .run(|| async { + let ctx = TestContext::new(client); + + // Send announcement to purgatory + let repo = ctx + .get_fixture(FixtureKind::ValidRepoSent) + .await + .map_err(|e| format!("Failed to create repo announcement: {}", e))?; + + let repo_id = repo + .tags + .iter() + .find(|t| t.kind() == TagKind::d()) + .and_then(|t| t.content()) + .ok_or("Missing d tag in repo announcement")? + .to_string(); + + // Verify it's in purgatory (not served) + tokio::time::sleep(Duration::from_millis(300)).await; + if client.is_event_on_relay(repo.id).await.map_err(|e| e.to_string())? { + return Err( + "Announcement was served immediately - purgatory not working".to_string(), + ); + } + + // Build coordinate: `30617::` + let coord = format!( + "30617:{}:{}", + client.public_key().to_hex(), + repo_id + ); + + // Build and send kind 5 deletion event referencing by coordinate + let deletion = client + .event_builder(Kind::EventDeletion, "") + .tag(Tag::custom(TagKind::custom("a"), vec![coord])) + .tag(Tag::custom(TagKind::custom("k"), vec!["30617"])) + .build(client.keys()) + .map_err(|e| format!("Failed to build deletion event: {}", e))?; + + client + .send_event(deletion) + .await + .map_err(|e| format!("Relay rejected deletion event: {}", e))?; + + tokio::time::sleep(Duration::from_millis(300)).await; + + // Verify bare repo was deleted + let http_url = AuditClient::ws_to_http_url(&client.relay_url().await.map_err(|e| e.to_string())?) + .map_err(|e| e.to_string())?; + let clone_url = format!( + "{}/{}/{}.git", + http_url, + client.public_key().to_bech32().map_err(|e| e.to_string())?, + repo_id + ); + + let output = std::process::Command::new("git") + .args(["ls-remote", &clone_url]) + .output() + .map_err(|e| format!("Failed to run git ls-remote: {}", e))?; + + if output.status.success() { + return Err(format!( + "Bare repo still exists after deletion event. \ + Expected git ls-remote to fail for {}", + clone_url + )); + } + + Ok(()) + }) + .await + } } #[cfg(test)] diff --git a/src/nostr/builder.rs b/src/nostr/builder.rs index c2d4939..d056e46 100644 --- a/src/nostr/builder.rs +++ b/src/nostr/builder.rs @@ -14,8 +14,8 @@ use nostr_relay_builder::prelude::*; use crate::config::{Config, DatabaseBackend}; use crate::nostr::events::RepositoryAnnouncement; use crate::nostr::policy::{ - AnnouncementPolicy, AnnouncementResult, PolicyContext, PrEventPolicy, ReferenceResult, - RelatedEventPolicy, StatePolicy, StateResult, + AnnouncementPolicy, AnnouncementResult, DeletionPolicy, PolicyContext, PrEventPolicy, + ReferenceResult, RelatedEventPolicy, StatePolicy, StateResult, }; @@ -29,6 +29,7 @@ pub type SharedDatabase = Arc; /// - `StatePolicy` - State event validation + ref alignment /// - `PrEventPolicy` - PR/PR Update validation /// - `RelatedEventPolicy` - Forward/backward reference checking +/// - `DeletionPolicy` - NIP-09 event deletion request handling /// /// Uses stateful database queries to check event relationships. #[derive(Clone)] @@ -38,6 +39,7 @@ pub struct Nip34WritePolicy { state_policy: StatePolicy, pr_event_policy: PrEventPolicy, related_event_policy: RelatedEventPolicy, + deletion_policy: DeletionPolicy, } impl std::fmt::Debug for Nip34WritePolicy { @@ -69,6 +71,7 @@ impl Nip34WritePolicy { state_policy: StatePolicy::new(ctx.clone()), pr_event_policy: PrEventPolicy::new(ctx.clone()), related_event_policy: RelatedEventPolicy::new(ctx.clone()), + deletion_policy: DeletionPolicy::new(ctx.clone()), ctx, } } @@ -521,6 +524,7 @@ impl WritePolicy for Nip34WritePolicy { ); WritePolicyResult::Accept } + Kind::EventDeletion => self.deletion_policy.handle(event).await, _ => self.handle_related_event(event, "Event").await, } }) diff --git a/src/nostr/policy/deletion.rs b/src/nostr/policy/deletion.rs new file mode 100644 index 0000000..69a5758 --- /dev/null +++ b/src/nostr/policy/deletion.rs @@ -0,0 +1,438 @@ +/// Deletion Policy - NIP-09 event deletion request handling +/// +/// Handles kind 5 (EventDeletion) events that request removal of repository +/// announcements (kind 30617) from purgatory. +/// +/// ## NIP-09 Rules Enforced +/// +/// - Only the event author can delete their own events (pubkey must match) +/// - `e` tags reference specific event IDs to delete +/// - `a` tags reference addressable events by coordinate (`::`) +/// - When an `a` tag is used, all versions up to `created_at` of the deletion request +/// are considered deleted +/// +/// ## Purgatory Interaction +/// +/// When a valid deletion request targets a kind 30617 announcement that is currently +/// in purgatory (not yet promoted to the database), the purgatory entry is removed +/// and the bare repository is deleted from disk. +use nostr_relay_builder::prelude::{Event, WritePolicyResult}; + +use super::PolicyContext; + +/// Policy for handling NIP-09 event deletion requests +#[derive(Clone)] +pub struct DeletionPolicy { + ctx: PolicyContext, +} + +impl DeletionPolicy { + pub fn new(ctx: PolicyContext) -> Self { + Self { ctx } + } + + /// Process a kind 5 (EventDeletion) event. + /// + /// Checks whether the deletion request targets any purgatory announcements + /// and removes them if so. The deletion event itself is always accepted + /// (relays should store deletion requests per NIP-09). + /// + /// Only the event author can delete their own events — this is enforced by + /// checking that the purgatory entry's owner matches `event.pubkey`. + pub async fn handle(&self, event: &Event) -> WritePolicyResult { + // Process purgatory removals synchronously (no async needed) + self.remove_purgatory_targets(event); + + // Always accept the deletion event itself so it is stored and + // can prevent re-acceptance of the deleted event in the future. + WritePolicyResult::Accept + } + + /// Remove any purgatory announcements targeted by this deletion event. + /// + /// Handles both reference styles from NIP-09: + /// - `e` tags: event ID references — match against purgatory entry event IDs + /// - `a` tags: addressable coordinate references — `30617::` + /// + /// Only removes entries where the purgatory entry's owner matches the deletion + /// event's pubkey (enforces author-only deletion). + fn remove_purgatory_targets(&self, event: &Event) { + let author = &event.pubkey; + + for tag in event.tags.iter() { + let tag_vec = tag.as_slice(); + if tag_vec.len() < 2 { + continue; + } + + match tag_vec[0].as_str() { + "e" => { + // Event ID reference: find purgatory announcement with this event ID + let target_id = &tag_vec[1]; + self.remove_by_event_id(author, target_id, event.created_at.as_secs()); + } + "a" => { + // Addressable coordinate reference: `::` + let coord = &tag_vec[1]; + self.remove_by_coordinate(author, coord, event.created_at.as_secs()); + } + _ => {} + } + } + } + + /// Remove a purgatory announcement matched by event ID. + /// + /// Scans all purgatory announcements owned by `author` and removes the one + /// whose event ID hex matches `target_id_hex`. + fn remove_by_event_id(&self, author: &nostr_relay_builder::prelude::PublicKey, target_id_hex: &str, _deletion_created_at: u64) { + // Scan announcements owned by this author for a matching event ID + // We use get_announcements_by_identifier would require knowing the identifier, + // so instead we iterate via find_announcement after collecting all entries. + // The DashMap doesn't expose a direct "find by event ID" method, so we use + // the announcements_for_sync snapshot to get all (repo_id, _) pairs and then + // look up each one. + let all = self.ctx.purgatory.announcements_for_sync(); + for (repo_id, _) in all { + // repo_id format: "30617:{pubkey_hex}:{identifier}" + let parts: Vec<&str> = repo_id.splitn(3, ':').collect(); + if parts.len() != 3 { + continue; + } + let entry_pubkey_hex = parts[1]; + let identifier = parts[2]; + + // Only check entries owned by the deletion event author + if entry_pubkey_hex != author.to_hex() { + continue; + } + + if let Some(entry) = self.ctx.purgatory.find_announcement(author, identifier) { + if entry.event.id.to_hex() == target_id_hex { + tracing::info!( + event_id = %target_id_hex, + identifier = %identifier, + author = %author.to_hex(), + "Deletion request: removing purgatory announcement by event ID" + ); + self.evict_purgatory_entry(author, identifier); + return; // event IDs are unique, no need to continue + } + } + } + } + + /// Remove a purgatory announcement matched by addressable coordinate. + /// + /// The coordinate format is `::`. Only kind 30617 + /// coordinates are relevant here. Per NIP-09, all versions up to `deletion_created_at` + /// are considered deleted — since purgatory entries are always a single event per + /// (owner, identifier), we delete if the entry's `created_at` ≤ `deletion_created_at`. + fn remove_by_coordinate( + &self, + author: &nostr_relay_builder::prelude::PublicKey, + coordinate: &str, + deletion_created_at: u64, + ) { + // Parse coordinate: `::` + let parts: Vec<&str> = coordinate.splitn(3, ':').collect(); + if parts.len() != 3 { + return; + } + + let kind_str = parts[0]; + let coord_pubkey_hex = parts[1]; + let identifier = parts[2]; + + // Only handle kind 30617 (GitRepoAnnouncement) + if kind_str != "30617" { + return; + } + + // The coordinate pubkey must match the deletion event author + if coord_pubkey_hex != author.to_hex() { + tracing::debug!( + coord_pubkey = %coord_pubkey_hex, + deletion_author = %author.to_hex(), + "Ignoring deletion: coordinate pubkey does not match deletion author" + ); + return; + } + + if let Some(entry) = self.ctx.purgatory.find_announcement(author, identifier) { + // Per NIP-09: delete all versions up to deletion_created_at + if entry.event.created_at.as_secs() <= deletion_created_at { + tracing::info!( + identifier = %identifier, + author = %author.to_hex(), + entry_created_at = entry.event.created_at.as_secs(), + deletion_created_at = %deletion_created_at, + "Deletion request: removing purgatory announcement by coordinate" + ); + self.evict_purgatory_entry(author, identifier); + } else { + tracing::debug!( + identifier = %identifier, + author = %author.to_hex(), + entry_created_at = entry.event.created_at.as_secs(), + deletion_created_at = %deletion_created_at, + "Ignoring deletion: purgatory entry is newer than deletion request" + ); + } + } + } + + /// Remove a purgatory announcement and delete its bare repository from disk. + fn evict_purgatory_entry( + &self, + author: &nostr_relay_builder::prelude::PublicKey, + identifier: &str, + ) { + // Get repo path before removing + if let Some(entry) = self.ctx.purgatory.find_announcement(author, identifier) { + if entry.repo_path.exists() { + if let Err(e) = std::fs::remove_dir_all(&entry.repo_path) { + tracing::warn!( + path = %entry.repo_path.display(), + error = %e, + "Failed to delete bare repository during deletion request processing" + ); + } else { + tracing::info!( + path = %entry.repo_path.display(), + "Deleted bare repository for deletion-requested purgatory announcement" + ); + } + } + } + + self.ctx.purgatory.remove_announcement(author, identifier); + + // Remove state events for this identifier only if no other owner's + // announcement remains in purgatory (state events are keyed by identifier alone) + let other_owners_remain = !self + .ctx + .purgatory + .get_announcements_by_identifier(identifier) + .is_empty(); + + if !other_owners_remain { + self.ctx.purgatory.remove_state(identifier); + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::nostr::policy::PolicyContext; + use crate::purgatory::Purgatory; + use nostr_relay_builder::prelude::*; + use std::collections::HashSet; + use std::path::PathBuf; + use std::sync::Arc; + + fn make_context() -> PolicyContext { + let db = Arc::new(MemoryDatabase::with_opts(MemoryDatabaseOptions { + events: true, + max_events: None, + })); + let purgatory = Arc::new(Purgatory::new(PathBuf::new())); + let config = crate::config::Config::for_testing(); + PolicyContext::new("test.example.com", db, PathBuf::new(), purgatory, config) + } + + fn make_announcement_event(keys: &Keys, identifier: &str) -> Event { + EventBuilder::new(Kind::GitRepoAnnouncement, "") + .tags(vec![ + Tag::identifier(identifier), + Tag::custom(TagKind::custom("clone"), vec!["https://example.com/repo.git"]), + ]) + .sign_with_keys(keys) + .unwrap() + } + + fn add_to_purgatory(ctx: &PolicyContext, event: &Event, identifier: &str) { + ctx.purgatory.add_announcement( + event.clone(), + identifier.to_string(), + event.pubkey, + PathBuf::new(), + HashSet::new(), + ); + } + + #[tokio::test] + async fn test_deletion_by_event_id_removes_purgatory_entry() { + let ctx = make_context(); + let keys = Keys::generate(); + let identifier = "my-repo"; + + let announcement = make_announcement_event(&keys, identifier); + add_to_purgatory(&ctx, &announcement, identifier); + + assert!(ctx.purgatory.has_purgatory_announcement(&keys.public_key(), identifier)); + + // Build kind 5 deletion event referencing the announcement by event ID + let deletion = EventBuilder::new(Kind::EventDeletion, "") + .tags(vec![ + Tag::event(announcement.id), + Tag::custom(TagKind::custom("k"), vec!["30617"]), + ]) + .sign_with_keys(&keys) + .unwrap(); + + let policy = DeletionPolicy::new(ctx.clone()); + let result = policy.handle(&deletion).await; + + assert!(matches!(result, WritePolicyResult::Accept)); + assert!( + !ctx.purgatory.has_purgatory_announcement(&keys.public_key(), identifier), + "Purgatory entry should have been removed" + ); + } + + #[tokio::test] + async fn test_deletion_by_coordinate_removes_purgatory_entry() { + let ctx = make_context(); + let keys = Keys::generate(); + let identifier = "my-repo"; + + let announcement = make_announcement_event(&keys, identifier); + add_to_purgatory(&ctx, &announcement, identifier); + + assert!(ctx.purgatory.has_purgatory_announcement(&keys.public_key(), identifier)); + + // Build kind 5 deletion event referencing the announcement by coordinate + let coord = format!("30617:{}:{}", keys.public_key().to_hex(), identifier); + let deletion = EventBuilder::new(Kind::EventDeletion, "") + .tags(vec![ + Tag::custom(TagKind::custom("a"), vec![coord]), + Tag::custom(TagKind::custom("k"), vec!["30617"]), + ]) + .sign_with_keys(&keys) + .unwrap(); + + let policy = DeletionPolicy::new(ctx.clone()); + let result = policy.handle(&deletion).await; + + assert!(matches!(result, WritePolicyResult::Accept)); + assert!( + !ctx.purgatory.has_purgatory_announcement(&keys.public_key(), identifier), + "Purgatory entry should have been removed" + ); + } + + #[tokio::test] + async fn test_deletion_by_wrong_author_does_not_remove() { + let ctx = make_context(); + let owner_keys = Keys::generate(); + let attacker_keys = Keys::generate(); + let identifier = "my-repo"; + + let announcement = make_announcement_event(&owner_keys, identifier); + add_to_purgatory(&ctx, &announcement, identifier); + + // Attacker tries to delete by event ID + let deletion = EventBuilder::new(Kind::EventDeletion, "") + .tags(vec![ + Tag::event(announcement.id), + Tag::custom(TagKind::custom("k"), vec!["30617"]), + ]) + .sign_with_keys(&attacker_keys) + .unwrap(); + + let policy = DeletionPolicy::new(ctx.clone()); + let result = policy.handle(&deletion).await; + + assert!(matches!(result, WritePolicyResult::Accept)); + assert!( + ctx.purgatory.has_purgatory_announcement(&owner_keys.public_key(), identifier), + "Purgatory entry should NOT have been removed by wrong author" + ); + } + + #[tokio::test] + async fn test_deletion_by_coordinate_wrong_author_does_not_remove() { + let ctx = make_context(); + let owner_keys = Keys::generate(); + let attacker_keys = Keys::generate(); + let identifier = "my-repo"; + + let announcement = make_announcement_event(&owner_keys, identifier); + add_to_purgatory(&ctx, &announcement, identifier); + + // Attacker tries to delete by coordinate using owner's pubkey in coord + // but signs with their own key — coord pubkey != deletion author + let coord = format!("30617:{}:{}", owner_keys.public_key().to_hex(), identifier); + let deletion = EventBuilder::new(Kind::EventDeletion, "") + .tags(vec![ + Tag::custom(TagKind::custom("a"), vec![coord]), + Tag::custom(TagKind::custom("k"), vec!["30617"]), + ]) + .sign_with_keys(&attacker_keys) + .unwrap(); + + let policy = DeletionPolicy::new(ctx.clone()); + let result = policy.handle(&deletion).await; + + assert!(matches!(result, WritePolicyResult::Accept)); + assert!( + ctx.purgatory.has_purgatory_announcement(&owner_keys.public_key(), identifier), + "Purgatory entry should NOT have been removed by wrong author" + ); + } + + #[tokio::test] + async fn test_deletion_of_nonexistent_entry_is_accepted() { + let ctx = make_context(); + let keys = Keys::generate(); + + // No purgatory entry exists — deletion should still be accepted + let deletion = EventBuilder::new(Kind::EventDeletion, "") + .tags(vec![ + Tag::custom(TagKind::custom("a"), vec![ + format!("30617:{}:nonexistent", keys.public_key().to_hex()) + ]), + ]) + .sign_with_keys(&keys) + .unwrap(); + + let policy = DeletionPolicy::new(ctx.clone()); + let result = policy.handle(&deletion).await; + + assert!(matches!(result, WritePolicyResult::Accept)); + } + + #[tokio::test] + async fn test_deletion_by_coordinate_respects_created_at() { + let ctx = make_context(); + let keys = Keys::generate(); + let identifier = "my-repo"; + + // Create announcement with a future timestamp + let future_ts = Timestamp::now().as_secs() + 3600; // 1 hour in the future + let announcement = EventBuilder::new(Kind::GitRepoAnnouncement, "") + .tags(vec![Tag::identifier(identifier)]) + .custom_created_at(Timestamp::from(future_ts)) + .sign_with_keys(&keys) + .unwrap(); + add_to_purgatory(&ctx, &announcement, identifier); + + // Deletion event with current timestamp (older than announcement) + let coord = format!("30617:{}:{}", keys.public_key().to_hex(), identifier); + let deletion = EventBuilder::new(Kind::EventDeletion, "") + .tags(vec![Tag::custom(TagKind::custom("a"), vec![coord])]) + .sign_with_keys(&keys) + .unwrap(); + + let policy = DeletionPolicy::new(ctx.clone()); + let result = policy.handle(&deletion).await; + + assert!(matches!(result, WritePolicyResult::Accept)); + assert!( + ctx.purgatory.has_purgatory_announcement(&keys.public_key(), identifier), + "Purgatory entry should NOT be removed: entry is newer than deletion request" + ); + } +} diff --git a/src/nostr/policy/mod.rs b/src/nostr/policy/mod.rs index 1566b6c..f5b981a 100644 --- a/src/nostr/policy/mod.rs +++ b/src/nostr/policy/mod.rs @@ -6,11 +6,13 @@ /// - `PrEventPolicy` - PR/PR Update validation /// - `RelatedEventPolicy` - Forward/backward reference checking mod announcement; +mod deletion; mod pr_event; mod related; mod state; pub use announcement::{AnnouncementPolicy, AnnouncementResult}; +pub use deletion::DeletionPolicy; pub use pr_event::PrEventPolicy; pub use related::{ReferenceResult, RelatedEventPolicy}; pub use state::{StatePolicy, StateResult}; diff --git a/tests/purgatory.rs b/tests/purgatory.rs index efc28c9..553271f 100644 --- a/tests/purgatory.rs +++ b/tests/purgatory.rs @@ -66,6 +66,13 @@ isolated_purgatory_test!(test_announcement_served_after_git_push); isolated_purgatory_test!(test_bare_repo_exists_for_purgatory_announcement); isolated_purgatory_test!(test_state_event_accepted_for_purgatory_announcement); +// ============================================================ +// Deletion Event Tests (NIP-09) +// ============================================================ + +isolated_purgatory_test!(test_deletion_by_event_id_removes_purgatory_announcement); +isolated_purgatory_test!(test_deletion_by_coordinate_removes_purgatory_announcement); + // ============================================================ // State Event Purgatory Tests (already implemented) // ============================================================ -- cgit v1.2.3