1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
|
/// Announcement Policy - Repository announcement validation
///
/// Handles validation of NIP-34 repository announcements (kind 30617)
/// according to GRASP-01 specification.
use nostr_relay_builder::prelude::{Alphabet, Event, Filter, Kind, PublicKey, SingleLetterTag};
use std::collections::HashSet;
use std::time::Duration;
use super::PolicyContext;
use crate::config::Config;
use crate::nostr::events::{validate_announcement, RepositoryAnnouncement};
/// Result of announcement policy evaluation
#[derive(Debug, Clone, PartialEq)]
pub enum AnnouncementResult {
/// Accept: Event lists our service (GRASP-01 compliant) - replacement announcement
Accept,
/// Accept as maintainer: Event accepted via maintainer exception (multi-maintainer)
AcceptMaintainer,
/// Accept as archive: Event accepted via GRASP-05 archive whitelist (read-only)
AcceptArchive,
/// Accept to purgatory: New announcement, waiting for git data
AcceptPurgatory,
/// Reject: Event fails validation with reason
Reject(String),
}
/// Policy for validating repository announcements
#[derive(Clone)]
pub struct AnnouncementPolicy {
ctx: PolicyContext,
config: Config,
}
impl AnnouncementPolicy {
pub fn new(ctx: PolicyContext, config: Config) -> Self {
Self { ctx, config }
}
/// Validate a repository announcement event
///
/// Returns:
/// - `Accept` if this is a replacement announcement (active announcement exists in DB or
/// purgatory)
/// - `AcceptPurgatory` if this is a new announcement (no active announcement exists)
/// - `AcceptMaintainer` if accepted via maintainer exception
/// - `AcceptArchive` if accepted via GRASP-05 archive config
/// - `Reject` with reason if validation fails
pub async fn validate(&self, event: &Event) -> AnnouncementResult {
// First, try validation (GRASP-01 + GRASP-05)
let validation_result = validate_announcement(event, &self.config);
match validation_result {
AnnouncementResult::Reject(reason) => {
// Validation failed - check maintainer exception
// GRASP-01 Exception: Accept announcements from recursive maintainers
match RepositoryAnnouncement::from_event(event.clone()) {
Ok(announcement) => {
// If this pubkey+identifier has a purgatory entry AND the incoming
// event is strictly newer, the owner is sending a replacement that
// removes our service. Clear the purgatory entry and its bare repo.
//
// If the incoming event is older than the purgatory entry (e.g. a
// relay replay of a superseded announcement), ignore it — the newer
// purgatory entry takes precedence and must not be evicted.
let should_evict = self
.ctx
.purgatory
.find_announcement(&event.pubkey, &announcement.identifier)
.is_some_and(|entry| event.created_at > entry.event.created_at);
if should_evict {
self.remove_purgatory_announcement(&event.pubkey, &announcement.identifier);
}
match self
.is_maintainer_in_any_announcement(
&announcement.identifier,
&event.pubkey,
)
.await
{
Ok(true) => AnnouncementResult::AcceptMaintainer,
Ok(false) => AnnouncementResult::Reject(reason),
Err(_) => {
// Fail-secure: reject on database errors
AnnouncementResult::Reject(reason)
}
}
}
Err(_) => AnnouncementResult::Reject(reason),
}
}
AnnouncementResult::Accept | AnnouncementResult::AcceptArchive => {
// Parse announcement to check for existing active announcement
match RepositoryAnnouncement::from_event(event.clone()) {
Ok(announcement) => {
let in_db = match self
.has_db_announcement(&event.pubkey, &announcement.identifier)
.await
{
Ok(v) => v,
Err(e) => {
tracing::warn!(
error = %e,
"Failed to check for existing DB announcement - rejecting"
);
return AnnouncementResult::Reject(format!(
"Database error checking existing announcement: {}",
e
));
}
};
if in_db {
// Replacement announcement with DB entry - accept immediately
tracing::debug!(
identifier = %announcement.identifier,
"Replacement announcement (DB) - accepting immediately"
);
return validation_result;
}
let in_purgatory = self
.ctx
.purgatory
.has_purgatory_announcement(&event.pubkey, &announcement.identifier);
if in_purgatory {
// Replacement announcement with purgatory entry - replace it and
// extend expiry so the new announcement gets a fresh 30-minute window.
tracing::debug!(
identifier = %announcement.identifier,
"Replacement announcement (purgatory) - replacing purgatory entry"
);
self.replace_purgatory_announcement(event, &announcement);
// Return Accept (not AcceptPurgatory) - this is a replacement, not new
return validation_result;
}
// No existing announcement - route to purgatory
tracing::debug!(
identifier = %announcement.identifier,
"New announcement - routing to purgatory"
);
AnnouncementResult::AcceptPurgatory
}
Err(e) => AnnouncementResult::Reject(format!(
"Failed to parse announcement: {}",
e
)),
}
}
// AcceptPurgatory shouldn't come from validate_announcement, but handle it
result => result,
}
}
/// Replace a purgatory announcement entry with a newer event.
///
/// Called when a replacement announcement arrives for a (pubkey, identifier) pair
/// that is currently in purgatory. Updates the purgatory entry and extends the
/// expiry so the new announcement has a fresh waiting window.
fn replace_purgatory_announcement(
&self,
event: &Event,
announcement: &RepositoryAnnouncement,
) {
let repo_path = self.ctx.git_data_path.join(announcement.repo_path());
let relays: HashSet<String> = announcement.relays.iter().cloned().collect();
// add_announcement uses the (owner, identifier) key so it overwrites the old entry
self.ctx.purgatory.add_announcement(
event.clone(),
announcement.identifier.clone(),
event.pubkey,
repo_path,
relays,
);
// Extend the announcement's expiry (reset to full 30 min window)
self.ctx.purgatory.extend_announcement_expiry(
&event.pubkey,
&announcement.identifier,
Duration::from_secs(1800),
);
// Also extend any state events waiting for this identifier
let state_entries = self.ctx.purgatory.find_state(&announcement.identifier);
if !state_entries.is_empty() {
let state_ids: Vec<_> = state_entries.iter().map(|e| e.event.id).collect();
self.ctx.purgatory.extend_expiry(
&announcement.identifier,
&state_ids,
Duration::from_secs(1800),
);
}
}
/// Remove a purgatory announcement and clean up associated resources.
///
/// Called when a replacement announcement is rejected (owner removed our service).
/// Deletes the bare repository from disk and removes any state events waiting for
/// this identifier.
fn remove_purgatory_announcement(&self, pubkey: &PublicKey, identifier: &str) {
// Get the repo path before removing from purgatory
if let Some(entry) = self.ctx.purgatory.find_announcement(pubkey, identifier) {
// Delete the bare repository from disk
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 purgatory cleanup"
);
} else {
tracing::info!(
path = %entry.repo_path.display(),
"Deleted bare repository for rejected purgatory announcement"
);
}
}
}
// Remove the announcement from purgatory
self.ctx.purgatory.remove_announcement(pubkey, identifier);
// Remove any state events waiting for this identifier
self.ctx.purgatory.remove_state(identifier);
tracing::info!(
identifier = %identifier,
"Cleared purgatory entry: owner removed our service from announcement"
);
}
/// Check if there's an announcement in the database for this (pubkey, identifier).
///
/// Only checks the database (promoted announcements). For purgatory checks use
/// `purgatory.has_purgatory_announcement()` directly.
async fn has_db_announcement(
&self,
pubkey: &PublicKey,
identifier: &str,
) -> Result<bool, String> {
let filter = Filter::new()
.kind(Kind::GitRepoAnnouncement)
.author(*pubkey)
.custom_tag(
SingleLetterTag::lowercase(Alphabet::D),
identifier.to_string(),
);
let events: Vec<Event> = match self.ctx.database.query(filter).await {
Ok(events) => events.into_iter().collect(),
Err(e) => return Err(format!("Database query failed: {}", e)),
};
Ok(!events.is_empty())
}
/// Add an announcement to purgatory
///
/// Creates the bare repository and stores the announcement in purgatory
/// until git data arrives.
pub fn add_to_purgatory(&self, event: &Event) -> Result<(), String> {
let announcement = RepositoryAnnouncement::from_event(event.clone())
.map_err(|e| format!("Failed to parse announcement: {}", e))?;
// Create bare repository
self.ensure_bare_repository(&announcement)?;
// Build repo path
let repo_path = self.ctx.git_data_path.join(announcement.repo_path());
// Extract relays from announcement
let relays: HashSet<String> = announcement.relays.iter().cloned().collect();
// Add to purgatory
self.ctx.purgatory.add_announcement(
event.clone(),
announcement.identifier.clone(),
event.pubkey,
repo_path,
relays,
);
tracing::info!(
identifier = %announcement.identifier,
event_id = %event.id,
"Added announcement to purgatory"
);
Ok(())
}
/// Create a bare git repository if it doesn't exist
/// Path format: <git_data_path>/<npub>/<identifier>.git
pub fn ensure_bare_repository(
&self,
announcement: &RepositoryAnnouncement,
) -> Result<(), String> {
let repo_path = self.ctx.git_data_path.join(announcement.repo_path());
// Check if repository already exists
if repo_path.exists() {
tracing::debug!("Repository already exists at {}", repo_path.display());
return Ok(());
}
// Create parent directory (npub directory)
let parent = repo_path
.parent()
.ok_or_else(|| format!("Invalid repository path: {}", repo_path.display()))?;
std::fs::create_dir_all(parent)
.map_err(|e| format!("Failed to create directory {}: {}", parent.display(), e))?;
// Initialize bare repository using git command
let output = std::process::Command::new("git")
.args(["init", "--bare", repo_path.to_str().unwrap()])
.output()
.map_err(|e| format!("Failed to execute git init: {}", e))?;
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
return Err(format!("git init failed: {}", stderr));
}
tracing::info!("Created bare repository at {}", repo_path.display());
Ok(())
}
/// Check if a pubkey is listed as a maintainer in any announcement for this identifier
///
/// A pubkey is considered a maintainer if:
/// 1. They are the owner (pubkey) of an accepted announcement with this identifier, OR
/// 2. They are listed in the maintainers tag of ANY announcement with this identifier
///
/// This enables accepting announcements from maintainers even when they don't list
/// this GRASP server, for maintainer chain discovery and GRASP-02 sync.
///
/// Checks both the database (promoted announcements) and purgatory (announcements
/// waiting for git data). This is necessary because a maintainer's announcement
/// (which lists the recursive maintainer) may still be in purgatory when the
/// recursive maintainer's announcement arrives.
async fn is_maintainer_in_any_announcement(
&self,
identifier: &str,
author: &PublicKey,
) -> Result<bool, String> {
// Query all announcements with this identifier that are already in the database
let filter = Filter::new().kind(Kind::GitRepoAnnouncement).custom_tag(
SingleLetterTag::lowercase(Alphabet::D),
identifier.to_string(),
);
let db_announcements: Vec<Event> = match self.ctx.database.query(filter).await {
Ok(events) => events.into_iter().collect(),
Err(e) => return Err(format!("Database query failed: {}", e)),
};
// Also collect purgatory announcements for this identifier
let purgatory_announcements: Vec<Event> = self
.ctx
.purgatory
.get_announcements_by_identifier(identifier)
.into_iter()
.map(|entry| entry.event)
.collect();
let all_announcements: Vec<&Event> = db_announcements
.iter()
.chain(purgatory_announcements.iter())
.collect();
if all_announcements.is_empty() {
// No existing announcements for this identifier - author cannot be a maintainer
return Ok(false);
}
let author_hex = author.to_hex();
// Check each announcement to see if author is listed as a maintainer
for event in &all_announcements {
// Check if author is the owner of this announcement
if event.pubkey == *author {
return Ok(true);
}
// Check if author is listed in the maintainers tag
if let Ok(announcement) = RepositoryAnnouncement::from_event((*event).clone()) {
if announcement.maintainers.contains(&author_hex) {
return Ok(true);
}
}
}
Ok(false)
}
}
|