From 56fb9351127996755805ffb77778bf080756c2c8 Mon Sep 17 00:00:00 2001 From: Yuki Kishimoto Date: Mon, 23 Oct 2023 15:06:21 +0200 Subject: [PATCH] sdk: add `reconcile` method to `Client` Rename `reconcile` to `reconcilie_with_items` in `RelayPool` --- crates/nostr-sdk/src/client/mod.rs | 5 +++++ crates/nostr-sdk/src/relay/mod.rs | 4 ++-- crates/nostr-sdk/src/relay/pool.rs | 12 +++++++++--- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/crates/nostr-sdk/src/client/mod.rs b/crates/nostr-sdk/src/client/mod.rs index 7be7b6459..c24cfe6fa 100644 --- a/crates/nostr-sdk/src/client/mod.rs +++ b/crates/nostr-sdk/src/client/mod.rs @@ -1282,6 +1282,11 @@ impl Client { self.send_event_builder(builder).await } + /// Negentropy reconciliation + pub async fn reconcilie(&self, filter: Filter, timeout: Duration) -> Result<(), Error> { + Ok(self.pool.reconcilie(filter, timeout).await?) + } + /// Get a list of channels pub async fn get_channels(&self, timeout: Option) -> Result, Error> { self.get_events_of(vec![Filter::new().kind(Kind::ChannelCreation)], timeout) diff --git a/crates/nostr-sdk/src/relay/mod.rs b/crates/nostr-sdk/src/relay/mod.rs index a2e7a6fb0..51bb97677 100644 --- a/crates/nostr-sdk/src/relay/mod.rs +++ b/crates/nostr-sdk/src/relay/mod.rs @@ -1473,7 +1473,7 @@ impl Relay { pub async fn reconcilie( &self, filter: Filter, - my_items: Vec<(EventId, Timestamp)>, + items: Vec<(EventId, Timestamp)>, timeout: Duration, ) -> Result<(), Error> { if !self.opts.get_read() { @@ -1484,7 +1484,7 @@ impl Relay { let mut negentropy = Negentropy::new(id_size, Some(5_000))?; - for (id, timestamp) in my_items.into_iter() { + for (id, timestamp) in items.into_iter() { let cutted_id: &[u8] = &id.as_bytes()[..id_size]; let cutted_id = Bytes::from_slice(cutted_id); negentropy.add_item(timestamp.as_u64(), cutted_id)?; diff --git a/crates/nostr-sdk/src/relay/pool.rs b/crates/nostr-sdk/src/relay/pool.rs index 4b9c1d980..90b969e75 100644 --- a/crates/nostr-sdk/src/relay/pool.rs +++ b/crates/nostr-sdk/src/relay/pool.rs @@ -861,17 +861,23 @@ impl RelayPool { } /// Negentropy reconciliation - pub async fn reconcilie( + pub async fn reconcilie(&self, filter: Filter, timeout: Duration) -> Result<(), Error> { + let items: Vec<(EventId, Timestamp)> = self.database.negentropy_items(&filter).await?; + self.reconcilie_with_items(filter, items, timeout).await + } + + /// Negentropy reconciliation with custom items + pub async fn reconcilie_with_items( &self, filter: Filter, - my_items: Vec<(EventId, Timestamp)>, + items: Vec<(EventId, Timestamp)>, timeout: Duration, ) -> Result<(), Error> { let mut handles = Vec::new(); let relays = self.relays().await; for (url, relay) in relays.into_iter() { let filter = filter.clone(); - let my_items = my_items.clone(); + let my_items = items.clone(); let handle = thread::spawn(async move { if let Err(e) = relay.reconcilie(filter, my_items, timeout).await { tracing::error!("Failed to get reconcilie with {url}: {e}");