diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2025-12-18 10:12:11 +0000 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2025-12-18 10:12:11 +0000 |
| commit | 03f074d0d0840b946a356badde75551d61c0f84c (patch) | |
| tree | 97943bb692d40b3e572854bd30eec6bfdbcf8cb2 /src/sync/relay_connection.rs | |
| parent | 7821b107190cc116a30a4c339f935bc16a1d5197 (diff) | |
sync removing dead code
Diffstat (limited to 'src/sync/relay_connection.rs')
| -rw-r--r-- | src/sync/relay_connection.rs | 87 |
1 files changed, 42 insertions, 45 deletions
diff --git a/src/sync/relay_connection.rs b/src/sync/relay_connection.rs index 4167a0c..bc4b59e 100644 --- a/src/sync/relay_connection.rs +++ b/src/sync/relay_connection.rs | |||
| @@ -325,11 +325,25 @@ impl RelayConnection { | |||
| 325 | /// * `Ok(SubscriptionId)` - The subscription ID on success | 325 | /// * `Ok(SubscriptionId)` - The subscription ID on success |
| 326 | /// * `Err(String)` - Error description on failure | 326 | /// * `Err(String)` - Error description on failure |
| 327 | pub async fn subscribe_filter(&self, filter: Filter) -> Result<SubscriptionId, String> { | 327 | pub async fn subscribe_filter(&self, filter: Filter) -> Result<SubscriptionId, String> { |
| 328 | // DEBUG TRACING: Log the filter being subscribed to | ||
| 329 | tracing::debug!( | ||
| 330 | relay = %self.url, | ||
| 331 | filter = ?filter, | ||
| 332 | "subscribe_filter called with filter" | ||
| 333 | ); | ||
| 334 | |||
| 328 | let output = self | 335 | let output = self |
| 329 | .client | 336 | .client |
| 330 | .subscribe(filter, None) | 337 | .subscribe(filter, None) |
| 331 | .await | 338 | .await |
| 332 | .map_err(|e| format!("Failed to subscribe on {}: {}", self.url, e))?; | 339 | .map_err(|e| format!("Failed to subscribe on {}: {}", self.url, e))?; |
| 340 | |||
| 341 | tracing::debug!( | ||
| 342 | relay = %self.url, | ||
| 343 | subscription_id = %output.val, | ||
| 344 | "subscribe_filter succeeded" | ||
| 345 | ); | ||
| 346 | |||
| 333 | Ok(output.val) | 347 | Ok(output.val) |
| 334 | } | 348 | } |
| 335 | 349 | ||
| @@ -407,31 +421,36 @@ impl RelayConnection { | |||
| 407 | true | 421 | true |
| 408 | } | 422 | } |
| 409 | 423 | ||
| 410 | /// Perform negentropy synchronization for a filter | 424 | /// Perform a negentropy sync diff (dry run) to identify missing events |
| 411 | /// | 425 | /// |
| 412 | /// Uses NIP-77 negentropy protocol to efficiently reconcile events matching | 426 | /// This method performs NIP-77 negentropy reconciliation without downloading events. |
| 413 | /// the filter between local database and remote relay. This is much more | 427 | /// It returns the list of event IDs that need to be fetched. The caller should then |
| 414 | /// efficient than REQ+EOSE for relays with overlapping event sets. | 428 | /// manually fetch these events and pass them through the write policy for validation. |
| 415 | /// | 429 | /// |
| 416 | /// # Arguments | 430 | /// # Arguments |
| 417 | /// * `filter` - The filter defining which events to sync | 431 | /// * `filter` - The filter to sync |
| 418 | /// | 432 | /// |
| 419 | /// # Returns | 433 | /// # Returns |
| 420 | /// * `Ok(NegentropySyncResult)` - Sync completed successfully with reconciliation info | 434 | /// * `Ok(Reconciliation)` - Reconciliation result with remote/local/sent event IDs |
| 421 | /// * `Err(String)` - Sync failed (relay may not support NIP-77, or other error) | 435 | /// * `Err(String)` - Sync failed (relay may not support NIP-77, or other error) |
| 422 | /// | 436 | /// |
| 423 | /// # Fallback Behavior | 437 | /// # Usage Pattern |
| 424 | /// If this method fails, the caller should fall back to traditional REQ+EOSE sync. | 438 | /// ```ignore |
| 425 | /// Failure reasons include: | 439 | /// // 1. Get the diff |
| 426 | /// - Relay doesn't actually support NIP-77 (despite claiming to) | 440 | /// let reconciliation = conn.negentropy_sync_diff(filter).await?; |
| 427 | /// - Network errors during reconciliation | 441 | /// |
| 428 | /// - Timeout during sync | 442 | /// // 2. Fetch missing events by ID |
| 429 | pub async fn negentropy_sync_filter( | 443 | /// if !reconciliation.remote.is_empty() { |
| 430 | &self, | 444 | /// let ids: Vec<EventId> = reconciliation.remote.into_iter().collect(); |
| 431 | filter: Filter, | 445 | /// let filter = Filter::new().ids(ids); |
| 432 | ) -> Result<NegentropySyncResult, String> { | 446 | /// conn.subscribe_filter(filter, tx).await?; |
| 433 | // Use nostr-sdk's sync method which handles the NEG-OPEN/NEG-MSG exchange | 447 | /// } |
| 434 | let sync_opts = SyncOptions::default(); | 448 | /// |
| 449 | /// // 3. Events come through normal flow and get validated via process_event_static | ||
| 450 | /// ``` | ||
| 451 | pub async fn negentropy_sync_diff(&self, filter: Filter) -> Result<Reconciliation, String> { | ||
| 452 | // Use dry_run to only identify differences without downloading events | ||
| 453 | let sync_opts = SyncOptions::default().dry_run(); | ||
| 435 | 454 | ||
| 436 | match self.client.sync(filter.clone(), &sync_opts).await { | 455 | match self.client.sync(filter.clone(), &sync_opts).await { |
| 437 | Ok(output) => { | 456 | Ok(output) => { |
| @@ -441,9 +460,7 @@ impl RelayConnection { | |||
| 441 | relay = %self.url, | 460 | relay = %self.url, |
| 442 | local_count = reconciliation.local.len(), | 461 | local_count = reconciliation.local.len(), |
| 443 | remote_count = reconciliation.remote.len(), | 462 | remote_count = reconciliation.remote.len(), |
| 444 | sent_count = reconciliation.sent.len(), | 463 | "Negentropy diff completed (dry run)" |
| 445 | received_count = reconciliation.received.len(), | ||
| 446 | "Negentropy sync completed" | ||
| 447 | ); | 464 | ); |
| 448 | 465 | ||
| 449 | // Check for any failures | 466 | // Check for any failures |
| @@ -451,15 +468,11 @@ impl RelayConnection { | |||
| 451 | tracing::warn!( | 468 | tracing::warn!( |
| 452 | relay = %self.url, | 469 | relay = %self.url, |
| 453 | failures = ?output.failed, | 470 | failures = ?output.failed, |
| 454 | "Some relays failed during negentropy sync" | 471 | "Some relays failed during negentropy diff" |
| 455 | ); | 472 | ); |
| 456 | } | 473 | } |
| 457 | 474 | ||
| 458 | Ok(NegentropySyncResult { | 475 | Ok(reconciliation) |
| 459 | remote_only: reconciliation.remote.into_iter().collect(), | ||
| 460 | local_only: reconciliation.local.into_iter().collect(), | ||
| 461 | received: reconciliation.received.into_iter().collect(), | ||
| 462 | }) | ||
| 463 | } | 476 | } |
| 464 | Err(e) => { | 477 | Err(e) => { |
| 465 | // Log warning only once per relay to avoid spam | 478 | // Log warning only once per relay to avoid spam |
| @@ -470,30 +483,14 @@ impl RelayConnection { | |||
| 470 | tracing::warn!( | 483 | tracing::warn!( |
| 471 | relay = %self.url, | 484 | relay = %self.url, |
| 472 | error = %e, | 485 | error = %e, |
| 473 | "Negentropy sync failed, will fall back to REQ+EOSE" | 486 | "Negentropy diff failed, will fall back to REQ+EOSE" |
| 474 | ); | 487 | ); |
| 475 | } | 488 | } |
| 476 | Err(format!("Negentropy sync failed: {}", e)) | 489 | Err(format!("Negentropy diff failed: {}", e)) |
| 477 | } | 490 | } |
| 478 | } | 491 | } |
| 479 | } | 492 | } |
| 480 | 493 | ||
| 481 | /// Perform negentropy sync and return received event IDs | ||
| 482 | /// | ||
| 483 | /// Convenience method that performs negentropy sync and returns the event IDs | ||
| 484 | /// that were received (i.e., events that exist on remote but not locally). | ||
| 485 | /// | ||
| 486 | /// # Arguments | ||
| 487 | /// * `filter` - The filter defining which events to sync | ||
| 488 | /// | ||
| 489 | /// # Returns | ||
| 490 | /// * `Ok(Vec<EventId>)` - Event IDs received from remote relay | ||
| 491 | /// * `Err(String)` - Sync failed | ||
| 492 | pub async fn negentropy_sync_and_fetch(&self, filter: Filter) -> Result<Vec<EventId>, String> { | ||
| 493 | let result = self.negentropy_sync_filter(filter).await?; | ||
| 494 | Ok(result.received) | ||
| 495 | } | ||
| 496 | |||
| 497 | /// Check if this connection has a database configured for negentropy | 494 | /// Check if this connection has a database configured for negentropy |
| 498 | pub fn has_database(&self) -> bool { | 495 | pub fn has_database(&self) -> bool { |
| 499 | self.database.is_some() | 496 | self.database.is_some() |