Skip to content

Commit

Permalink
sdk: add reconcile method to Client
Browse files Browse the repository at this point in the history
Rename `reconcile` to  `reconcilie_with_items` in `RelayPool`
  • Loading branch information
yukibtc committed Oct 23, 2023
1 parent 78a9b48 commit 56fb935
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
5 changes: 5 additions & 0 deletions crates/nostr-sdk/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Duration>) -> Result<Vec<Event>, Error> {
self.get_events_of(vec![Filter::new().kind(Kind::ChannelCreation)], timeout)
Expand Down
4 changes: 2 additions & 2 deletions crates/nostr-sdk/src/relay/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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)?;
Expand Down
12 changes: 9 additions & 3 deletions crates/nostr-sdk/src/relay/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}");
Expand Down

0 comments on commit 56fb935

Please sign in to comment.