Skip to content

Commit

Permalink
pool: get events only from remote relay when calling get_events_of
Browse files Browse the repository at this point in the history
…or `get_events_from`

When calling `get_events_of` or `get_events_from`, get events ONLY from remote relay/s instead of database + relay.

Signed-off-by: Yuki Kishimoto <[email protected]>
  • Loading branch information
yukibtc committed Aug 14, 2024
1 parent 348c64c commit 5432771
Show file tree
Hide file tree
Showing 12 changed files with 8 additions and 43 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
* pool: remove IDs collection from `InternalRelayPool::get_events_from` ([Yuki Kishimoto])
* pool: better checks before perform queries or send messages to relays ([Yuki Kishimoto])
* pool: bump `async-wsocket` to `v0.6` ([Yuki Kishimoto])
* pool: get events only from remote relay when calling `get_events_of` or `get_events_from` ([Yuki Kishimoto])
* database: not match event if `Filter::search` field is set ([Yuki Kishimoto])
* database: avoid to copy `EventId` in `Event::decode` ([Yuki Kishimoto])
* database: use `Vec` instead of `BTreeSet` as inner value for `TagIndexValues` ([Yuki Kishimoto])
Expand Down
2 changes: 0 additions & 2 deletions bindings/nostr-sdk-ffi/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,6 @@ impl Client {
}

/// Get events of filters from specific relays
///
/// Get events both from **local database** and **relays**
pub async fn get_events_from(
&self,
urls: Vec<String>,
Expand Down
4 changes: 0 additions & 4 deletions bindings/nostr-sdk-ffi/src/pool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,6 @@ impl RelayPool {
}

/// Get events of filters
///
/// Get events both from **local database** and **relays**
pub async fn get_events_of(
&self,
filters: Vec<Arc<Filter>>,
Expand All @@ -375,8 +373,6 @@ impl RelayPool {
}

/// Get events of filters from **specific relays**
///
/// Get events both from **local database** and **relays**
pub async fn get_events_from(
&self,
urls: Vec<String>,
Expand Down
2 changes: 0 additions & 2 deletions bindings/nostr-sdk-ffi/src/relay/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,6 @@ impl Relay {
}

/// Get events of filters
///
/// Get events from local database and relay
pub async fn get_events_of(
&self,
filters: Vec<Arc<Filter>>,
Expand Down
2 changes: 0 additions & 2 deletions bindings/nostr-sdk-js/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,8 +361,6 @@ impl JsClient {
}

/// Get events of filters from specific relays
///
/// Get events both from **local database** and **relays**
#[wasm_bindgen(js_name = getEventsFrom)]
pub async fn get_events_from(
&self,
Expand Down
4 changes: 0 additions & 4 deletions bindings/nostr-sdk-js/src/pool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,6 @@ impl JsRelayPool {
}

// /// Get events of filters
// ///
// /// Get events both from **local database** and **relays**
// #[wasm_bindgen(js_name = getEventsOf)]
// pub async fn get_events_of(
// &self,
Expand All @@ -410,8 +408,6 @@ impl JsRelayPool {
// }
//
// /// Get events of filters from **specific relays**
// ///
// /// Get events both from **local database** and **relays**
// #[wasm_bindgen(js_name = getEventsFrom)]
// pub async fn get_events_from(
// &self,
Expand Down
2 changes: 0 additions & 2 deletions bindings/nostr-sdk-js/src/relay/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,6 @@ impl JsRelay {
}

/// Get events of filters
///
/// Get events from local database and relay
#[wasm_bindgen(js_name = getEventsOf)]
pub async fn get_events_of(
&self,
Expand Down
11 changes: 2 additions & 9 deletions crates/nostr-relay-pool/src/pool/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use async_utility::thread::JoinHandle;
use async_utility::{thread, time};
use atomic_destructor::AtomicDestroyer;
use nostr::{ClientMessage, Event, EventId, Filter, SubscriptionId, Timestamp, TryIntoUrl, Url};
use nostr_database::{DynNostrDatabase, IntoNostrDatabase, Order};
use nostr_database::{DynNostrDatabase, IntoNostrDatabase};
use tokio::sync::{broadcast, mpsc, Mutex, RwLock};
use tokio_stream::wrappers::ReceiverStream;

Expand Down Expand Up @@ -632,15 +632,8 @@ impl InternalRelayPool {
return Err(Error::RelayNotFound);
}

let stored_events: Vec<Event> = self
.database
.query(filters.clone(), Order::Desc)
.await
.unwrap_or_default();

// Compose events collections
let events: Arc<Mutex<BTreeSet<Event>>> =
Arc::new(Mutex::new(stored_events.into_iter().collect()));
let events: Arc<Mutex<BTreeSet<Event>>> = Arc::new(Mutex::new(BTreeSet::new()));

// Filter relays and start query
let mut handles = Vec::with_capacity(urls.len());
Expand Down
4 changes: 0 additions & 4 deletions crates/nostr-relay-pool/src/pool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,8 +423,6 @@ impl RelayPool {
}

/// Get events of filters
///
/// Get events both from **local database** and **relays**
#[inline]
pub async fn get_events_of(
&self,
Expand All @@ -438,8 +436,6 @@ impl RelayPool {
}

/// Get events of filters from **specific relays**
///
/// Get events both from **local database** and **relays**
#[inline]
pub async fn get_events_from<I, U>(
&self,
Expand Down
15 changes: 5 additions & 10 deletions crates/nostr-relay-pool/src/relay/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//! Internal Relay
use std::cmp;
use std::collections::{BTreeSet, HashMap, HashSet};
use std::collections::{HashMap, HashSet};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::time::Duration;
Expand All @@ -24,7 +24,7 @@ use nostr::{
ClientMessage, Event, EventId, Filter, JsonUtil, Keys, Kind, MissingPartialEvent, PartialEvent,
RawRelayMessage, RelayMessage, SubscriptionId, Timestamp, Url,
};
use nostr_database::{DynNostrDatabase, Order};
use nostr_database::DynNostrDatabase;
use tokio::sync::mpsc::{self, Receiver, Sender};
use tokio::sync::{broadcast, oneshot, watch, Mutex, MutexGuard, RwLock};

Expand Down Expand Up @@ -1603,18 +1603,13 @@ impl InternalRelay {
timeout: Duration,
opts: FilterOptions,
) -> Result<Vec<Event>, Error> {
let stored_events: Vec<Event> = self
.database
.query(filters.clone(), Order::Desc)
.await
.unwrap_or_default();
let events: Mutex<BTreeSet<Event>> = Mutex::new(stored_events.into_iter().collect());
let events: Mutex<Vec<Event>> = Mutex::new(Vec::new());
self.get_events_of_with_callback(filters, timeout, opts, |event| async {
let mut events = events.lock().await;
events.insert(event);
events.push(event);
})
.await?;
Ok(events.into_inner().into_iter().rev().collect())
Ok(events.into_inner())
}

pub async fn count_events_of(
Expand Down
2 changes: 0 additions & 2 deletions crates/nostr-relay-pool/src/relay/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,6 @@ impl Relay {
}

/// Get events of filters
///
/// Get events from local database and relay
#[inline]
pub async fn get_events_of(
&self,
Expand Down
2 changes: 0 additions & 2 deletions crates/nostr-sdk/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -859,8 +859,6 @@ impl Client {
}

/// Get events of filters from specific relays
///
/// Get events both from **local database** and **relays**
#[inline]
pub async fn get_events_from<I, U>(
&self,
Expand Down

0 comments on commit 5432771

Please sign in to comment.