From b4da09689ee0bd6ac327a6ed7ffb01e2175e2596 Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Fri, 5 Dec 2025 16:37:09 +0000 Subject: remove stupid tests and methods --- src/sync/connection.rs | 41 +++++++++++++--------------- src/sync/subscription.rs | 71 ++++++++---------------------------------------- 2 files changed, 30 insertions(+), 82 deletions(-) (limited to 'src/sync') diff --git a/src/sync/connection.rs b/src/sync/connection.rs index e921185..61a33f8 100644 --- a/src/sync/connection.rs +++ b/src/sync/connection.rs @@ -70,10 +70,8 @@ impl SyncConnection { tracing::info!("Sync connection established to {}", url); // Create subscription manager for this connection - let subscription_manager = SubscriptionManager::new( - filter_service.clone(), - remote_domain.to_string(), - ); + let subscription_manager = + SubscriptionManager::new(filter_service.clone(), remote_domain.to_string()); Ok(Self { url: url.to_string(), @@ -208,10 +206,8 @@ impl SyncConnection { /// - kind 30617/30618: New announcement → add Layer 2 subscription /// - kind 1617/1618/1619/1621/1622: New PR/Issue → add Layer 3 subscription async fn handle_dynamic_subscription(&mut self, event: &Event) { - let kind = event.kind.as_u16(); - // Check if this is an announcement kind (triggers Layer 2 subscription) - if SubscriptionManager::is_announcement_kind(kind) { + if matches!(event.kind, Kind::GitRepoAnnouncement | Kind::RepoState) { if let Some(new_filters) = self.subscription_manager.add_announcement(event) { tracing::info!( "New announcement {} on {}, adding {} Layer 2 filter(s) (total filters: {})", @@ -224,8 +220,11 @@ impl SyncConnection { } } - // Check if this is a PR/Issue kind (triggers Layer 3 subscription) - if SubscriptionManager::is_pr_issue_kind(kind) { + // Check if this is a Patch/PR/Issue kind (triggers Layer 3 subscription) + if matches!( + event.kind, + Kind::GitPatch | Kind::GitIssue | Kind::Custom(1618) + ) { if let Some(new_filters) = self.subscription_manager.add_event(event) { tracing::info!( "New PR/Issue {} on {}, adding {} Layer 3 filter(s) (total filters: {})", @@ -366,11 +365,13 @@ pub async fn connect_with_retry( ); } - match SyncConnection::new(url, filter_service.clone(), &remote_domain, metrics.clone()).await { + match SyncConnection::new(url, filter_service.clone(), &remote_domain, metrics.clone()) + .await + { Ok(conn) => { // Record successful connection health_tracker.record_success(url); - + // Record metrics if let Some(ref m) = metrics { m.record_connection_attempt(url, true); @@ -379,7 +380,7 @@ pub async fn connect_with_retry( m.record_health_state(url, health_tracker.get_state(url)); m.record_failure_count(url, 0); } - + tracing::info!("Sync connection established to {}", url); // Run the connection (this blocks until disconnection) @@ -388,7 +389,7 @@ pub async fn connect_with_retry( // Connection ended - record as failure for reconnection backoff // (The connection ending is considered a failure even if it worked for a while) health_tracker.record_failure(url); - + // Update metrics for disconnection if let Some(ref m) = metrics { m.set_relay_connected(url, false); @@ -396,7 +397,7 @@ pub async fn connect_with_retry( m.record_health_state(url, health_tracker.get_state(url)); m.record_failure_count(url, health_tracker.get_failure_count(url)); } - + tracing::warn!("Sync connection to {} ended, will reconnect", url); } Err(e) => { @@ -405,14 +406,14 @@ pub async fn connect_with_retry( let failure_count = health_tracker.get_failure_count(url); let state = health_tracker.get_state(url); - + // Record metrics if let Some(ref m) = metrics { m.record_connection_attempt(url, false); m.set_relay_connected(url, false); m.record_health_state(url, state); m.record_failure_count(url, failure_count); - + // Track dead relays if state == super::health::HealthState::Dead { m.inc_dead_count(); @@ -435,11 +436,7 @@ pub async fn connect_with_retry( .get_remaining_backoff(url) .unwrap_or(Duration::from_secs(5)); - tracing::debug!( - "Waiting {:?} before reconnecting to {}", - wait_duration, - url - ); + tracing::debug!("Waiting {:?} before reconnecting to {}", wait_duration, url); tokio::time::sleep(wait_duration).await; } } @@ -473,4 +470,4 @@ mod tests { Some("relay.example.com".to_string()) ); } -} \ No newline at end of file +} diff --git a/src/sync/subscription.rs b/src/sync/subscription.rs index c37404f..bbeaa2a 100644 --- a/src/sync/subscription.rs +++ b/src/sync/subscription.rs @@ -26,12 +26,6 @@ use super::filter::FilterService; /// Maximum number of filters before consolidation is triggered const CONSOLIDATION_THRESHOLD: usize = 150; -/// Kind 30617 - Repository Announcement (NIP-34) -const KIND_REPOSITORY_ANNOUNCEMENT: u16 = 30617; - -/// Kind 30618 - Maintainer List (NIP-34) -const KIND_MAINTAINER_LIST: u16 = 30618; - /// Manages subscriptions for a single relay connection /// /// Tracks which announcements and events have been subscribed to, @@ -113,10 +107,7 @@ impl SubscriptionManager { // Build Layer 3 filter for this event // Layer 3 filters target events with 'e' tags pointing to this event - let filter = Filter::new().custom_tag( - SingleLetterTag::lowercase(Alphabet::E), - event_id, - ); + let filter = Filter::new().custom_tag(SingleLetterTag::lowercase(Alphabet::E), event_id); Some(vec![filter]) } @@ -212,67 +203,27 @@ impl SubscriptionManager { } }; - // Determine the kind for the coordinate - let kind = event.kind.as_u16(); - if kind != KIND_REPOSITORY_ANNOUNCEMENT && kind != KIND_MAINTAINER_LIST { + // Verify this is an announcement kind + if !matches!(event.kind, Kind::GitRepoAnnouncement | Kind::RepoState) { tracing::warn!( "Event {} is not an announcement (kind {}), cannot build Layer 2 filter", event.id.to_hex(), - kind + event.kind ); return Vec::new(); } // Build the addressable coordinate: kind:pubkey:identifier - let coord = format!("{}:{}:{}", kind, event.pubkey.to_hex(), identifier); + let coord = format!( + "{}:{}:{}", + event.kind.as_u16(), + event.pubkey.to_hex(), + identifier + ); // Create filter with 'a' tag for this coordinate - let filter = Filter::new().custom_tag( - SingleLetterTag::lowercase(Alphabet::A), - coord, - ); + let filter = Filter::new().custom_tag(SingleLetterTag::lowercase(Alphabet::A), coord); vec![filter] } - - /// Check if an event kind is an announcement kind - pub fn is_announcement_kind(kind: u16) -> bool { - kind == KIND_REPOSITORY_ANNOUNCEMENT || kind == KIND_MAINTAINER_LIST - } - - /// Check if an event kind is a PR/Issue/Patch kind that should trigger Layer 3 - pub fn is_pr_issue_kind(kind: u16) -> bool { - matches!( - kind, - 1617 | // Patch proposal (NIP-34) - 1618 | // PR - 1619 | // PR Update - 1621 | // Issue - 1622 // Reply - ) - } } - -#[cfg(test)] -mod tests { - use super::SubscriptionManager; - - #[test] - fn test_is_announcement_kind() { - assert!(SubscriptionManager::is_announcement_kind(30617)); - assert!(SubscriptionManager::is_announcement_kind(30618)); - assert!(!SubscriptionManager::is_announcement_kind(1)); - assert!(!SubscriptionManager::is_announcement_kind(1617)); - } - - #[test] - fn test_is_pr_issue_kind() { - assert!(SubscriptionManager::is_pr_issue_kind(1617)); - assert!(SubscriptionManager::is_pr_issue_kind(1618)); - assert!(SubscriptionManager::is_pr_issue_kind(1619)); - assert!(SubscriptionManager::is_pr_issue_kind(1621)); - assert!(SubscriptionManager::is_pr_issue_kind(1622)); - assert!(!SubscriptionManager::is_pr_issue_kind(30617)); - assert!(!SubscriptionManager::is_pr_issue_kind(1)); - } -} \ No newline at end of file -- cgit v1.2.3