diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2026-02-23 15:41:32 +0000 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2026-02-23 15:41:32 +0000 |
| commit | c54ce061d6d278cce8362d5af085808ca60c239b (patch) | |
| tree | ec967d6195d9f7ec4f061449596611afe3a0950f /src/nostr/policy/announcement.rs | |
| parent | e0ad39a489b3398f8208713bf728db0cb11475b0 (diff) | |
| parent | 113928aa84894ea8f65c247d9987527e792b32a9 (diff) | |
feat: announcement purgatory
Extends purgatory to hold repository announcements until git data arrives,
preventing empty repositories from being served to clients.
When an announcement is received, a bare repo is created immediately and the
announcement is held in purgatory. It is only promoted and served once a git
push confirms real content exists. If no push arrives before expiry, the bare
repo is deleted and the announcement is silently discarded.
Key behaviours:
- Soft expiry: announcements are hidden from clients but kept alive while git
pushes are in progress, reviving on successful push
- Expiry is extended when a matching state event or git push is observed
- NIP-09 deletion events remove announcements from purgatory
- Purgatory state (announcements, state events, PR events, expired set) is
persisted to disk on graceful shutdown and restored on startup, with elapsed
downtime subtracted from expiry deadlines
- Purgatory announcements drive StateOnly sync in the sync system so state
events are fetched from listed relays before promotion
- SyncLevel added to RepoSyncIndex to distinguish purgatory repos (StateOnly)
from promoted repos (Full L2+L3 sync)
Diffstat (limited to 'src/nostr/policy/announcement.rs')
| -rw-r--r-- | src/nostr/policy/announcement.rs | 273 |
1 files changed, 263 insertions, 10 deletions
diff --git a/src/nostr/policy/announcement.rs b/src/nostr/policy/announcement.rs index 15a6e58..b366f0b 100644 --- a/src/nostr/policy/announcement.rs +++ b/src/nostr/policy/announcement.rs | |||
| @@ -3,6 +3,8 @@ | |||
| 3 | /// Handles validation of NIP-34 repository announcements (kind 30617) | 3 | /// Handles validation of NIP-34 repository announcements (kind 30617) |
| 4 | /// according to GRASP-01 specification. | 4 | /// according to GRASP-01 specification. |
| 5 | use nostr_relay_builder::prelude::{Alphabet, Event, Filter, Kind, PublicKey, SingleLetterTag}; | 5 | use nostr_relay_builder::prelude::{Alphabet, Event, Filter, Kind, PublicKey, SingleLetterTag}; |
| 6 | use std::collections::HashSet; | ||
| 7 | use std::time::Duration; | ||
| 6 | 8 | ||
| 7 | use super::PolicyContext; | 9 | use super::PolicyContext; |
| 8 | use crate::config::Config; | 10 | use crate::config::Config; |
| @@ -11,12 +13,14 @@ use crate::nostr::events::{validate_announcement, RepositoryAnnouncement}; | |||
| 11 | /// Result of announcement policy evaluation | 13 | /// Result of announcement policy evaluation |
| 12 | #[derive(Debug, Clone, PartialEq)] | 14 | #[derive(Debug, Clone, PartialEq)] |
| 13 | pub enum AnnouncementResult { | 15 | pub enum AnnouncementResult { |
| 14 | /// Accept: Event lists our service (GRASP-01 compliant) | 16 | /// Accept: Event lists our service (GRASP-01 compliant) - replacement announcement |
| 15 | Accept, | 17 | Accept, |
| 16 | /// Accept as maintainer: Event accepted via maintainer exception (multi-maintainer) | 18 | /// Accept as maintainer: Event accepted via maintainer exception (multi-maintainer) |
| 17 | AcceptMaintainer, | 19 | AcceptMaintainer, |
| 18 | /// Accept as archive: Event accepted via GRASP-05 archive whitelist (read-only) | 20 | /// Accept as archive: Event accepted via GRASP-05 archive whitelist (read-only) |
| 19 | AcceptArchive, | 21 | AcceptArchive, |
| 22 | /// Accept to purgatory: New announcement, waiting for git data | ||
| 23 | AcceptPurgatory, | ||
| 20 | /// Reject: Event fails validation with reason | 24 | /// Reject: Event fails validation with reason |
| 21 | Reject(String), | 25 | Reject(String), |
| 22 | } | 26 | } |
| @@ -35,10 +39,13 @@ impl AnnouncementPolicy { | |||
| 35 | 39 | ||
| 36 | /// Validate a repository announcement event | 40 | /// Validate a repository announcement event |
| 37 | /// | 41 | /// |
| 38 | /// Returns `Accept` if the announcement lists the service properly, | 42 | /// Returns: |
| 39 | /// `AcceptMaintainer` if accepted via maintainer exception, | 43 | /// - `Accept` if this is a replacement announcement (active announcement exists in DB or |
| 40 | /// `AcceptArchive` if accepted via GRASP-05 archive config, | 44 | /// purgatory) |
| 41 | /// or `Reject` with reason. | 45 | /// - `AcceptPurgatory` if this is a new announcement (no active announcement exists) |
| 46 | /// - `AcceptMaintainer` if accepted via maintainer exception | ||
| 47 | /// - `AcceptArchive` if accepted via GRASP-05 archive config | ||
| 48 | /// - `Reject` with reason if validation fails | ||
| 42 | pub async fn validate(&self, event: &Event) -> AnnouncementResult { | 49 | pub async fn validate(&self, event: &Event) -> AnnouncementResult { |
| 43 | // First, try validation (GRASP-01 + GRASP-05) | 50 | // First, try validation (GRASP-01 + GRASP-05) |
| 44 | let validation_result = validate_announcement(event, &self.config); | 51 | let validation_result = validate_announcement(event, &self.config); |
| @@ -49,6 +56,23 @@ impl AnnouncementPolicy { | |||
| 49 | // GRASP-01 Exception: Accept announcements from recursive maintainers | 56 | // GRASP-01 Exception: Accept announcements from recursive maintainers |
| 50 | match RepositoryAnnouncement::from_event(event.clone()) { | 57 | match RepositoryAnnouncement::from_event(event.clone()) { |
| 51 | Ok(announcement) => { | 58 | Ok(announcement) => { |
| 59 | // If this pubkey+identifier has a purgatory entry AND the incoming | ||
| 60 | // event is strictly newer, the owner is sending a replacement that | ||
| 61 | // removes our service. Clear the purgatory entry and its bare repo. | ||
| 62 | // | ||
| 63 | // If the incoming event is older than the purgatory entry (e.g. a | ||
| 64 | // relay replay of a superseded announcement), ignore it — the newer | ||
| 65 | // purgatory entry takes precedence and must not be evicted. | ||
| 66 | let should_evict = self | ||
| 67 | .ctx | ||
| 68 | .purgatory | ||
| 69 | .find_announcement(&event.pubkey, &announcement.identifier) | ||
| 70 | .is_some_and(|entry| event.created_at > entry.event.created_at); | ||
| 71 | |||
| 72 | if should_evict { | ||
| 73 | self.remove_purgatory_announcement(&event.pubkey, &announcement.identifier); | ||
| 74 | } | ||
| 75 | |||
| 52 | match self | 76 | match self |
| 53 | .is_maintainer_in_any_announcement( | 77 | .is_maintainer_in_any_announcement( |
| 54 | &announcement.identifier, | 78 | &announcement.identifier, |
| @@ -67,11 +91,221 @@ impl AnnouncementPolicy { | |||
| 67 | Err(_) => AnnouncementResult::Reject(reason), | 91 | Err(_) => AnnouncementResult::Reject(reason), |
| 68 | } | 92 | } |
| 69 | } | 93 | } |
| 70 | // Accept, AcceptArchive, or AcceptMaintainer - return as-is | 94 | AnnouncementResult::Accept | AnnouncementResult::AcceptArchive => { |
| 95 | // Parse announcement to check for existing active announcement | ||
| 96 | match RepositoryAnnouncement::from_event(event.clone()) { | ||
| 97 | Ok(announcement) => { | ||
| 98 | let in_db = match self | ||
| 99 | .has_db_announcement(&event.pubkey, &announcement.identifier) | ||
| 100 | .await | ||
| 101 | { | ||
| 102 | Ok(v) => v, | ||
| 103 | Err(e) => { | ||
| 104 | tracing::warn!( | ||
| 105 | error = %e, | ||
| 106 | "Failed to check for existing DB announcement - rejecting" | ||
| 107 | ); | ||
| 108 | return AnnouncementResult::Reject(format!( | ||
| 109 | "Database error checking existing announcement: {}", | ||
| 110 | e | ||
| 111 | )); | ||
| 112 | } | ||
| 113 | }; | ||
| 114 | |||
| 115 | if in_db { | ||
| 116 | // Replacement announcement with DB entry - accept immediately | ||
| 117 | tracing::debug!( | ||
| 118 | identifier = %announcement.identifier, | ||
| 119 | "Replacement announcement (DB) - accepting immediately" | ||
| 120 | ); | ||
| 121 | return validation_result; | ||
| 122 | } | ||
| 123 | |||
| 124 | let in_purgatory = self | ||
| 125 | .ctx | ||
| 126 | .purgatory | ||
| 127 | .has_purgatory_announcement(&event.pubkey, &announcement.identifier); | ||
| 128 | |||
| 129 | if in_purgatory { | ||
| 130 | // Replacement announcement with purgatory entry - replace it and | ||
| 131 | // extend expiry so the new announcement gets a fresh 30-minute window. | ||
| 132 | tracing::debug!( | ||
| 133 | identifier = %announcement.identifier, | ||
| 134 | "Replacement announcement (purgatory) - replacing purgatory entry" | ||
| 135 | ); | ||
| 136 | self.replace_purgatory_announcement(event, &announcement); | ||
| 137 | // Return Accept (not AcceptPurgatory) - this is a replacement, not new | ||
| 138 | return validation_result; | ||
| 139 | } | ||
| 140 | |||
| 141 | // No existing announcement - route to purgatory | ||
| 142 | tracing::debug!( | ||
| 143 | identifier = %announcement.identifier, | ||
| 144 | "New announcement - routing to purgatory" | ||
| 145 | ); | ||
| 146 | AnnouncementResult::AcceptPurgatory | ||
| 147 | } | ||
| 148 | Err(e) => AnnouncementResult::Reject(format!( | ||
| 149 | "Failed to parse announcement: {}", | ||
| 150 | e | ||
| 151 | )), | ||
| 152 | } | ||
| 153 | } | ||
| 154 | // AcceptPurgatory shouldn't come from validate_announcement, but handle it | ||
| 71 | result => result, | 155 | result => result, |
| 72 | } | 156 | } |
| 73 | } | 157 | } |
| 74 | 158 | ||
| 159 | /// Replace a purgatory announcement entry with a newer event. | ||
| 160 | /// | ||
| 161 | /// Called when a replacement announcement arrives for a (pubkey, identifier) pair | ||
| 162 | /// that is currently in purgatory. Updates the purgatory entry and extends the | ||
| 163 | /// expiry so the new announcement has a fresh waiting window. | ||
| 164 | fn replace_purgatory_announcement( | ||
| 165 | &self, | ||
| 166 | event: &Event, | ||
| 167 | announcement: &RepositoryAnnouncement, | ||
| 168 | ) { | ||
| 169 | let repo_path = self.ctx.git_data_path.join(announcement.repo_path()); | ||
| 170 | let relays: HashSet<String> = announcement.relays.iter().cloned().collect(); | ||
| 171 | |||
| 172 | // add_announcement uses the (owner, identifier) key so it overwrites the old entry | ||
| 173 | self.ctx.purgatory.add_announcement( | ||
| 174 | event.clone(), | ||
| 175 | announcement.identifier.clone(), | ||
| 176 | event.pubkey, | ||
| 177 | repo_path, | ||
| 178 | relays, | ||
| 179 | ); | ||
| 180 | |||
| 181 | // Extend the announcement's expiry (reset to full 30 min window) | ||
| 182 | self.ctx.purgatory.extend_announcement_expiry( | ||
| 183 | &event.pubkey, | ||
| 184 | &announcement.identifier, | ||
| 185 | Duration::from_secs(1800), | ||
| 186 | ); | ||
| 187 | |||
| 188 | // Also extend any state events waiting for this identifier | ||
| 189 | let state_entries = self.ctx.purgatory.find_state(&announcement.identifier); | ||
| 190 | if !state_entries.is_empty() { | ||
| 191 | let state_ids: Vec<_> = state_entries.iter().map(|e| e.event.id).collect(); | ||
| 192 | self.ctx.purgatory.extend_expiry( | ||
| 193 | &announcement.identifier, | ||
| 194 | &state_ids, | ||
| 195 | Duration::from_secs(1800), | ||
| 196 | ); | ||
| 197 | } | ||
| 198 | } | ||
| 199 | |||
| 200 | /// Remove a purgatory announcement and clean up associated resources. | ||
| 201 | /// | ||
| 202 | /// Called when a replacement announcement is rejected (owner removed our service). | ||
| 203 | /// Deletes the bare repository from disk and removes any state events waiting for | ||
| 204 | /// this identifier. | ||
| 205 | fn remove_purgatory_announcement(&self, pubkey: &PublicKey, identifier: &str) { | ||
| 206 | // Get the repo path before removing from purgatory | ||
| 207 | if let Some(entry) = self.ctx.purgatory.find_announcement(pubkey, identifier) { | ||
| 208 | // Delete the bare repository from disk | ||
| 209 | if entry.repo_path.exists() { | ||
| 210 | if let Err(e) = std::fs::remove_dir_all(&entry.repo_path) { | ||
| 211 | tracing::warn!( | ||
| 212 | path = %entry.repo_path.display(), | ||
| 213 | error = %e, | ||
| 214 | "Failed to delete bare repository during purgatory cleanup" | ||
| 215 | ); | ||
| 216 | } else { | ||
| 217 | tracing::info!( | ||
| 218 | path = %entry.repo_path.display(), | ||
| 219 | "Deleted bare repository for rejected purgatory announcement" | ||
| 220 | ); | ||
| 221 | } | ||
| 222 | } | ||
| 223 | } | ||
| 224 | |||
| 225 | // Remove the announcement from purgatory | ||
| 226 | self.ctx.purgatory.remove_announcement(pubkey, identifier); | ||
| 227 | |||
| 228 | // Only remove state events if no other owner still has an announcement in purgatory | ||
| 229 | // for this identifier. State events are keyed by identifier alone, so blindly removing | ||
| 230 | // them would also discard state events legitimately belonging to a different owner's | ||
| 231 | // repository that happens to share the same identifier string. | ||
| 232 | let other_owners_remain = !self | ||
| 233 | .ctx | ||
| 234 | .purgatory | ||
| 235 | .get_announcements_by_identifier(identifier) | ||
| 236 | .is_empty(); | ||
| 237 | |||
| 238 | if !other_owners_remain { | ||
| 239 | self.ctx.purgatory.remove_state(identifier); | ||
| 240 | } | ||
| 241 | |||
| 242 | tracing::info!( | ||
| 243 | identifier = %identifier, | ||
| 244 | other_owners_remain = %other_owners_remain, | ||
| 245 | "Cleared purgatory entry: owner removed our service from announcement" | ||
| 246 | ); | ||
| 247 | } | ||
| 248 | |||
| 249 | /// Check if there's an announcement in the database for this (pubkey, identifier). | ||
| 250 | /// | ||
| 251 | /// Only checks the database (promoted announcements). For purgatory checks use | ||
| 252 | /// `purgatory.has_purgatory_announcement()` directly. | ||
| 253 | async fn has_db_announcement( | ||
| 254 | &self, | ||
| 255 | pubkey: &PublicKey, | ||
| 256 | identifier: &str, | ||
| 257 | ) -> Result<bool, String> { | ||
| 258 | let filter = Filter::new() | ||
| 259 | .kind(Kind::GitRepoAnnouncement) | ||
| 260 | .author(*pubkey) | ||
| 261 | .custom_tag( | ||
| 262 | SingleLetterTag::lowercase(Alphabet::D), | ||
| 263 | identifier.to_string(), | ||
| 264 | ); | ||
| 265 | |||
| 266 | let events: Vec<Event> = match self.ctx.database.query(filter).await { | ||
| 267 | Ok(events) => events.into_iter().collect(), | ||
| 268 | Err(e) => return Err(format!("Database query failed: {}", e)), | ||
| 269 | }; | ||
| 270 | |||
| 271 | Ok(!events.is_empty()) | ||
| 272 | } | ||
| 273 | |||
| 274 | /// Add an announcement to purgatory | ||
| 275 | /// | ||
| 276 | /// Creates the bare repository and stores the announcement in purgatory | ||
| 277 | /// until git data arrives. | ||
| 278 | pub fn add_to_purgatory(&self, event: &Event) -> Result<(), String> { | ||
| 279 | let announcement = RepositoryAnnouncement::from_event(event.clone()) | ||
| 280 | .map_err(|e| format!("Failed to parse announcement: {}", e))?; | ||
| 281 | |||
| 282 | // Create bare repository | ||
| 283 | self.ensure_bare_repository(&announcement)?; | ||
| 284 | |||
| 285 | // Build repo path | ||
| 286 | let repo_path = self.ctx.git_data_path.join(announcement.repo_path()); | ||
| 287 | |||
| 288 | // Extract relays from announcement | ||
| 289 | let relays: HashSet<String> = announcement.relays.iter().cloned().collect(); | ||
| 290 | |||
| 291 | // Add to purgatory | ||
| 292 | self.ctx.purgatory.add_announcement( | ||
| 293 | event.clone(), | ||
| 294 | announcement.identifier.clone(), | ||
| 295 | event.pubkey, | ||
| 296 | repo_path, | ||
| 297 | relays, | ||
| 298 | ); | ||
| 299 | |||
| 300 | tracing::info!( | ||
| 301 | identifier = %announcement.identifier, | ||
| 302 | event_id = %event.id, | ||
| 303 | "Added announcement to purgatory" | ||
| 304 | ); | ||
| 305 | |||
| 306 | Ok(()) | ||
| 307 | } | ||
| 308 | |||
| 75 | /// Create a bare git repository if it doesn't exist | 309 | /// Create a bare git repository if it doesn't exist |
| 76 | /// Path format: <git_data_path>/<npub>/<identifier>.git | 310 | /// Path format: <git_data_path>/<npub>/<identifier>.git |
| 77 | pub fn ensure_bare_repository( | 311 | pub fn ensure_bare_repository( |
| @@ -117,6 +351,11 @@ impl AnnouncementPolicy { | |||
| 117 | /// | 351 | /// |
| 118 | /// This enables accepting announcements from maintainers even when they don't list | 352 | /// This enables accepting announcements from maintainers even when they don't list |
| 119 | /// this GRASP server, for maintainer chain discovery and GRASP-02 sync. | 353 | /// this GRASP server, for maintainer chain discovery and GRASP-02 sync. |
| 354 | /// | ||
| 355 | /// Checks both the database (promoted announcements) and purgatory (announcements | ||
| 356 | /// waiting for git data). This is necessary because a maintainer's announcement | ||
| 357 | /// (which lists the recursive maintainer) may still be in purgatory when the | ||
| 358 | /// recursive maintainer's announcement arrives. | ||
| 120 | async fn is_maintainer_in_any_announcement( | 359 | async fn is_maintainer_in_any_announcement( |
| 121 | &self, | 360 | &self, |
| 122 | identifier: &str, | 361 | identifier: &str, |
| @@ -128,12 +367,26 @@ impl AnnouncementPolicy { | |||
| 128 | identifier.to_string(), | 367 | identifier.to_string(), |
| 129 | ); | 368 | ); |
| 130 | 369 | ||
| 131 | let announcements: Vec<Event> = match self.ctx.database.query(filter).await { | 370 | let db_announcements: Vec<Event> = match self.ctx.database.query(filter).await { |
| 132 | Ok(events) => events.into_iter().collect(), | 371 | Ok(events) => events.into_iter().collect(), |
| 133 | Err(e) => return Err(format!("Database query failed: {}", e)), | 372 | Err(e) => return Err(format!("Database query failed: {}", e)), |
| 134 | }; | 373 | }; |
| 135 | 374 | ||
| 136 | if announcements.is_empty() { | 375 | // Also collect purgatory announcements for this identifier |
| 376 | let purgatory_announcements: Vec<Event> = self | ||
| 377 | .ctx | ||
| 378 | .purgatory | ||
| 379 | .get_announcements_by_identifier(identifier) | ||
| 380 | .into_iter() | ||
| 381 | .map(|entry| entry.event) | ||
| 382 | .collect(); | ||
| 383 | |||
| 384 | let all_announcements: Vec<&Event> = db_announcements | ||
| 385 | .iter() | ||
| 386 | .chain(purgatory_announcements.iter()) | ||
| 387 | .collect(); | ||
| 388 | |||
| 389 | if all_announcements.is_empty() { | ||
| 137 | // No existing announcements for this identifier - author cannot be a maintainer | 390 | // No existing announcements for this identifier - author cannot be a maintainer |
| 138 | return Ok(false); | 391 | return Ok(false); |
| 139 | } | 392 | } |
| @@ -141,14 +394,14 @@ impl AnnouncementPolicy { | |||
| 141 | let author_hex = author.to_hex(); | 394 | let author_hex = author.to_hex(); |
| 142 | 395 | ||
| 143 | // Check each announcement to see if author is listed as a maintainer | 396 | // Check each announcement to see if author is listed as a maintainer |
| 144 | for event in &announcements { | 397 | for event in &all_announcements { |
| 145 | // Check if author is the owner of this announcement | 398 | // Check if author is the owner of this announcement |
| 146 | if event.pubkey == *author { | 399 | if event.pubkey == *author { |
| 147 | return Ok(true); | 400 | return Ok(true); |
| 148 | } | 401 | } |
| 149 | 402 | ||
| 150 | // Check if author is listed in the maintainers tag | 403 | // Check if author is listed in the maintainers tag |
| 151 | if let Ok(announcement) = RepositoryAnnouncement::from_event(event.clone()) { | 404 | if let Ok(announcement) = RepositoryAnnouncement::from_event((*event).clone()) { |
| 152 | if announcement.maintainers.contains(&author_hex) { | 405 | if announcement.maintainers.contains(&author_hex) { |
| 153 | return Ok(true); | 406 | return Ok(true); |
| 154 | } | 407 | } |