diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2025-12-22 21:03:49 +0000 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2025-12-22 21:03:49 +0000 |
| commit | 6b55efedba7c069eff7a3e335845a961d40274db (patch) | |
| tree | 7e036869007cb503fda5a81d959ecd49a9b3e5a3 | |
| parent | 1df90c609399c675e629b97294aee81a0b1e66dd (diff) | |
chore: cargo fmt and clippy
| -rw-r--r-- | src/sync/health.rs | 12 | ||||
| -rw-r--r-- | src/sync/mod.rs | 2 | ||||
| -rw-r--r-- | src/sync/relay_connection.rs | 4 | ||||
| -rw-r--r-- | tests/common/sync_helpers.rs | 35 | ||||
| -rw-r--r-- | tests/sync/tag_variations.rs | 2 |
5 files changed, 24 insertions, 31 deletions
diff --git a/src/sync/health.rs b/src/sync/health.rs index a10427f..2948707 100644 --- a/src/sync/health.rs +++ b/src/sync/health.rs | |||
| @@ -136,12 +136,14 @@ impl RelayHealth { | |||
| 136 | 136 | ||
| 137 | // Check if we're in stability period after recovery | 137 | // Check if we're in stability period after recovery |
| 138 | // (recovered from failures but not yet proven stable) | 138 | // (recovered from failures but not yet proven stable) |
| 139 | if let (Some(last_success), Some(last_failure)) = (self.last_success_time, self.last_failure_time) { | 139 | if let (Some(last_success), Some(last_failure)) = |
| 140 | (self.last_success_time, self.last_failure_time) | ||
| 141 | { | ||
| 140 | // Only consider stability period if recovery happened after the last failure | 142 | // Only consider stability period if recovery happened after the last failure |
| 141 | if last_success > last_failure { | 143 | if last_success > last_failure { |
| 142 | let time_since_recovery = now.duration_since(last_success); | 144 | let time_since_recovery = now.duration_since(last_success); |
| 143 | let stability_period = Duration::from_secs(STABILITY_PERIOD_SECS); | 145 | let stability_period = Duration::from_secs(STABILITY_PERIOD_SECS); |
| 144 | 146 | ||
| 145 | if time_since_recovery < stability_period { | 147 | if time_since_recovery < stability_period { |
| 146 | // Still in stability period - remain degraded to prove stability | 148 | // Still in stability period - remain degraded to prove stability |
| 147 | return HealthState::Degraded; | 149 | return HealthState::Degraded; |
| @@ -339,9 +341,10 @@ impl RelayHealthTracker { | |||
| 339 | // Respect existing next_retry_at if it's later (e.g., from rate limiting) | 341 | // Respect existing next_retry_at if it's later (e.g., from rate limiting) |
| 340 | let new_retry_at = now + backoff; | 342 | let new_retry_at = now + backoff; |
| 341 | health.next_retry_at = Some( | 343 | health.next_retry_at = Some( |
| 342 | health.next_retry_at | 344 | health |
| 345 | .next_retry_at | ||
| 343 | .unwrap_or(new_retry_at) | 346 | .unwrap_or(new_retry_at) |
| 344 | .max(new_retry_at) | 347 | .max(new_retry_at), |
| 345 | ); | 348 | ); |
| 346 | 349 | ||
| 347 | let new_state = health.state(); | 350 | let new_state = health.state(); |
| @@ -393,7 +396,6 @@ impl RelayHealthTracker { | |||
| 393 | } | 396 | } |
| 394 | } | 397 | } |
| 395 | 398 | ||
| 396 | |||
| 397 | /// Check if relay is currently rate limited | 399 | /// Check if relay is currently rate limited |
| 398 | /// | 400 | /// |
| 399 | /// Returns true if the relay is in RateLimited state and the cooldown period | 401 | /// Returns true if the relay is in RateLimited state and the cooldown period |
diff --git a/src/sync/mod.rs b/src/sync/mod.rs index 6ec39e8..65affc6 100644 --- a/src/sync/mod.rs +++ b/src/sync/mod.rs | |||
| @@ -1599,7 +1599,7 @@ impl SyncManager { | |||
| 1599 | write_policy: &Nip34WritePolicy, | 1599 | write_policy: &Nip34WritePolicy, |
| 1600 | local_relay: &LocalRelay, | 1600 | local_relay: &LocalRelay, |
| 1601 | ) -> ProcessResult { | 1601 | ) -> ProcessResult { |
| 1602 | use nostr_relay_builder::prelude::{WritePolicyResult, WritePolicy}; | 1602 | use nostr_relay_builder::prelude::{WritePolicy, WritePolicyResult}; |
| 1603 | use std::net::{IpAddr, Ipv4Addr, SocketAddr}; | 1603 | use std::net::{IpAddr, Ipv4Addr, SocketAddr}; |
| 1604 | // Check if event already exists | 1604 | // Check if event already exists |
| 1605 | match database.event_by_id(&event.id).await { | 1605 | match database.event_by_id(&event.id).await { |
diff --git a/src/sync/relay_connection.rs b/src/sync/relay_connection.rs index de20e0f..bd9ab80 100644 --- a/src/sync/relay_connection.rs +++ b/src/sync/relay_connection.rs | |||
| @@ -24,7 +24,7 @@ use crate::nostr::builder::SharedDatabase; | |||
| 24 | #[derive(Debug)] | 24 | #[derive(Debug)] |
| 25 | pub enum RelayEvent { | 25 | pub enum RelayEvent { |
| 26 | /// A new event was received (event, subscription_id) | 26 | /// A new event was received (event, subscription_id) |
| 27 | Event(Event, SubscriptionId), | 27 | Event(Box<Event>, SubscriptionId), |
| 28 | /// End of stored events for a subscription | 28 | /// End of stored events for a subscription |
| 29 | EndOfStoredEvents(SubscriptionId), | 29 | EndOfStoredEvents(SubscriptionId), |
| 30 | /// NOTICE message from relay | 30 | /// NOTICE message from relay |
| @@ -219,7 +219,7 @@ impl RelayConnection { | |||
| 219 | "Received event" | 219 | "Received event" |
| 220 | ); | 220 | ); |
| 221 | if event_sender | 221 | if event_sender |
| 222 | .send(RelayEvent::Event(*event, subscription_id.clone())) | 222 | .send(RelayEvent::Event(Box::new(*event), subscription_id.clone())) |
| 223 | .await | 223 | .await |
| 224 | .is_err() | 224 | .is_err() |
| 225 | { | 225 | { |
diff --git a/tests/common/sync_helpers.rs b/tests/common/sync_helpers.rs index cccfbdd..8279a04 100644 --- a/tests/common/sync_helpers.rs +++ b/tests/common/sync_helpers.rs | |||
| @@ -795,9 +795,8 @@ impl MetricsTestHarness { | |||
| 795 | /// Start syncing relay on a specific port pointing to source[idx] | 795 | /// Start syncing relay on a specific port pointing to source[idx] |
| 796 | pub async fn start_syncing_relay_on_port(&mut self, source_idx: usize, port: u16) { | 796 | pub async fn start_syncing_relay_on_port(&mut self, source_idx: usize, port: u16) { |
| 797 | let source_url = self.source_relays[source_idx].url().to_string(); | 797 | let source_url = self.source_relays[source_idx].url().to_string(); |
| 798 | self.syncing_relay = Some( | 798 | self.syncing_relay = |
| 799 | TestRelay::start_on_port_with_options(port, Some(source_url), false).await, | 799 | Some(TestRelay::start_on_port_with_options(port, Some(source_url), false).await); |
| 800 | ); | ||
| 801 | } | 800 | } |
| 802 | 801 | ||
| 803 | /// Start syncing relay pointing to random unused port (for failure tests) | 802 | /// Start syncing relay pointing to random unused port (for failure tests) |
| @@ -1122,16 +1121,15 @@ async fn send_to_relay(relay: &TestRelay, event: &Event) -> Result<(), String> { | |||
| 1122 | /// let result = run_sync_test(&[], &[comment]).await; | 1121 | /// let result = run_sync_test(&[], &[comment]).await; |
| 1123 | /// // Assert comment synced to result.syncing_relay | 1122 | /// // Assert comment synced to result.syncing_relay |
| 1124 | /// ``` | 1123 | /// ``` |
| 1125 | pub async fn run_sync_test( | 1124 | pub async fn run_sync_test(historic_events: &[Event], live_events: &[Event]) -> SyncTestResult { |
| 1126 | historic_events: &[Event], | ||
| 1127 | live_events: &[Event], | ||
| 1128 | ) -> SyncTestResult { | ||
| 1129 | // Validate usage - cannot provide events in both slices | 1125 | // Validate usage - cannot provide events in both slices |
| 1130 | let historic_mode = !historic_events.is_empty(); | 1126 | let historic_mode = !historic_events.is_empty(); |
| 1131 | let live_mode = !live_events.is_empty(); | 1127 | let live_mode = !live_events.is_empty(); |
| 1132 | 1128 | ||
| 1133 | if historic_mode && live_mode { | 1129 | if historic_mode && live_mode { |
| 1134 | panic!("Invalid usage: both historic_events and live_events provided. Use one or the other."); | 1130 | panic!( |
| 1131 | "Invalid usage: both historic_events and live_events provided. Use one or the other." | ||
| 1132 | ); | ||
| 1135 | } | 1133 | } |
| 1136 | // Note: Both slices can be empty - this tests just the announcement sync | 1134 | // Note: Both slices can be empty - this tests just the announcement sync |
| 1137 | 1135 | ||
| @@ -1144,11 +1142,8 @@ pub async fn run_sync_test( | |||
| 1144 | 1142 | ||
| 1145 | // 3. Create keys and announcement listing both relays | 1143 | // 3. Create keys and announcement listing both relays |
| 1146 | let keys = Keys::generate(); | 1144 | let keys = Keys::generate(); |
| 1147 | let announcement = create_repo_announcement( | 1145 | let announcement = |
| 1148 | &keys, | 1146 | create_repo_announcement(&keys, &[&source.domain(), &syncing_domain], "test-repo"); |
| 1149 | &[&source.domain(), &syncing_domain], | ||
| 1150 | "test-repo", | ||
| 1151 | ); | ||
| 1152 | 1147 | ||
| 1153 | // 4. Send announcement + historic events to source BEFORE syncing relay starts | 1148 | // 4. Send announcement + historic events to source BEFORE syncing relay starts |
| 1154 | send_to_relay(&source, &announcement) | 1149 | send_to_relay(&source, &announcement) |
| @@ -1161,12 +1156,8 @@ pub async fn run_sync_test( | |||
| 1161 | } | 1156 | } |
| 1162 | 1157 | ||
| 1163 | // 5. Start syncing relay (connects to source) | 1158 | // 5. Start syncing relay (connects to source) |
| 1164 | let syncing = TestRelay::start_on_port_with_options( | 1159 | let syncing = |
| 1165 | syncing_port, | 1160 | TestRelay::start_on_port_with_options(syncing_port, Some(source.url().into()), false).await; |
| 1166 | Some(source.url().into()), | ||
| 1167 | false, | ||
| 1168 | ) | ||
| 1169 | .await; | ||
| 1170 | 1161 | ||
| 1171 | // 6. Wait for sync connection to establish | 1162 | // 6. Wait for sync connection to establish |
| 1172 | let _ = wait_for_sync_connection(syncing.url(), 1, Duration::from_secs(5)).await; | 1163 | let _ = wait_for_sync_connection(syncing.url(), 1, Duration::from_secs(5)).await; |
| @@ -1208,8 +1199,8 @@ mod sync_helper_tests { | |||
| 1208 | async fn test_run_sync_test_panics_with_both_slices() { | 1199 | async fn test_run_sync_test_panics_with_both_slices() { |
| 1209 | let keys = Keys::generate(); | 1200 | let keys = Keys::generate(); |
| 1210 | let coord = repo_coord(&keys, "test"); | 1201 | let coord = repo_coord(&keys, "test"); |
| 1211 | let historic = build_layer2_issue_event(&keys, &coord, "Historic") | 1202 | let historic = |
| 1212 | .expect("Should create event"); | 1203 | build_layer2_issue_event(&keys, &coord, "Historic").expect("Should create event"); |
| 1213 | let live = build_layer3_reply_with_e_tag(&keys, &EventId::all_zeros(), "Live") | 1204 | let live = build_layer3_reply_with_e_tag(&keys, &EventId::all_zeros(), "Live") |
| 1214 | .expect("Should create event"); | 1205 | .expect("Should create event"); |
| 1215 | 1206 | ||
diff --git a/tests/sync/tag_variations.rs b/tests/sync/tag_variations.rs index 5b3ec0b..7153104 100644 --- a/tests/sync/tag_variations.rs +++ b/tests/sync/tag_variations.rs | |||
| @@ -344,7 +344,7 @@ async fn test_layer3_sync_with_lowercase_e_tag() { | |||
| 344 | .with_max_level(tracing::Level::DEBUG) | 344 | .with_max_level(tracing::Level::DEBUG) |
| 345 | .with_test_writer() | 345 | .with_test_writer() |
| 346 | .try_init(); | 346 | .try_init(); |
| 347 | 347 | ||
| 348 | // 1. Start relays | 348 | // 1. Start relays |
| 349 | let relay_a = TestRelay::start().await; | 349 | let relay_a = TestRelay::start().await; |
| 350 | println!( | 350 | println!( |