Skip to content

Commit

Permalink
sdk: move relay module to nostr-sdk-pool crate
Browse files Browse the repository at this point in the history
  • Loading branch information
yukibtc committed Feb 3, 2024
1 parent c52d099 commit 6931585
Show file tree
Hide file tree
Showing 27 changed files with 140 additions and 127 deletions.
15 changes: 14 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ async-utility = "0.1"
js-sys = "0.3"
nostr = { version = "0.27", path = "./crates/nostr", default-features = false }
nostr-database = { version = "0.27", path = "./crates/nostr-database", default-features = false }
nostr-sdk-pool = { version = "0.27", path = "./crates/nostr-sdk-pool" }
once_cell = "1.19"
serde_json = { version = "1.0", default-features = false }
thiserror = "1.0"
Expand Down
2 changes: 1 addition & 1 deletion bindings/nostr-sdk-ffi/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use nostr_ffi::{
RelayMessage,
};
use nostr_sdk::client::blocking::Client as ClientSdk;
use nostr_sdk::relay::RelayPoolNotification as RelayPoolNotificationSdk;
use nostr_sdk::pool::RelayPoolNotification as RelayPoolNotificationSdk;
use uniffi::Object;

mod builder;
Expand Down
4 changes: 2 additions & 2 deletions bindings/nostr-sdk-ffi/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ impl From<nostr_sdk::client::Error> for NostrSdkError {
}
}

impl From<nostr_sdk::relay::Error> for NostrSdkError {
fn from(e: nostr_sdk::relay::Error) -> NostrSdkError {
impl From<nostr_sdk::pool::Error> for NostrSdkError {
fn from(e: nostr_sdk::pool::Error) -> NostrSdkError {
Self::Generic { err: e.to_string() }
}
}
Expand Down
26 changes: 13 additions & 13 deletions bindings/nostr-sdk-ffi/src/relay/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::sync::Arc;
use std::time::Duration;

use nostr_ffi::{ClientMessage, Event, Filter, RelayInformationDocument, Timestamp};
use nostr_sdk::{block_on, relay, FilterOptions};
use nostr_sdk::{block_on, pool, FilterOptions};
use uniffi::{Enum, Object};

pub mod options;
Expand All @@ -18,11 +18,11 @@ use crate::error::Result;

#[derive(Object)]
pub struct RelayConnectionStats {
inner: relay::RelayConnectionStats,
inner: pool::RelayConnectionStats,
}

impl From<relay::RelayConnectionStats> for RelayConnectionStats {
fn from(inner: relay::RelayConnectionStats) -> Self {
impl From<pool::RelayConnectionStats> for RelayConnectionStats {
fn from(inner: pool::RelayConnectionStats) -> Self {
Self { inner }
}
}
Expand Down Expand Up @@ -55,17 +55,17 @@ impl RelayConnectionStats {
}

pub fn latency(&self) -> Option<Duration> {
self.inner.latency_blocking()
block_on(async move { self.inner.latency().await })
}
}

#[derive(Object)]
pub struct ActiveSubscription {
inner: relay::ActiveSubscription,
inner: pool::ActiveSubscription,
}

impl From<relay::ActiveSubscription> for ActiveSubscription {
fn from(inner: relay::ActiveSubscription) -> Self {
impl From<pool::ActiveSubscription> for ActiveSubscription {
fn from(inner: pool::ActiveSubscription) -> Self {
Self { inner }
}
}
Expand Down Expand Up @@ -119,11 +119,11 @@ impl From<nostr_sdk::RelayStatus> for RelayStatus {

#[derive(Object)]
pub struct Relay {
inner: relay::Relay,
inner: pool::Relay,
}

impl From<relay::Relay> for Relay {
fn from(inner: relay::Relay) -> Self {
impl From<pool::Relay> for Relay {
fn from(inner: pool::Relay) -> Self {
Self { inner }
}
}
Expand All @@ -139,15 +139,15 @@ impl Relay {
}

pub fn status(&self) -> RelayStatus {
self.inner.status_blocking().into()
block_on(async move { self.inner.status().await.into() })
}

pub fn is_connected(&self) -> bool {
block_on(async move { self.inner.is_connected().await })
}

pub fn document(&self) -> Arc<RelayInformationDocument> {
Arc::new(self.inner.document_blocking().into())
block_on(async move { Arc::new(self.inner.document().await.into()) })
}

pub fn subscriptions(&self) -> HashMap<String, Arc<ActiveSubscription>> {
Expand Down
1 change: 0 additions & 1 deletion bindings/nostr-sdk-js/src/relay/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use nostr_js::nips::nip11::JsRelayInformationDocument;
use nostr_sdk::prelude::*;
use nostr_sdk::relay::Relay;
use wasm_bindgen::prelude::*;

pub mod flags;
Expand Down
1 change: 1 addition & 0 deletions contrib/scripts/check-docs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ set -e
buildargs=(
"-p nostr"
"-p nostr-database"
"-p nostr-sdk-pool"
"-p nostr-sdk"
)

Expand Down
1 change: 1 addition & 0 deletions contrib/scripts/release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ args=(
"-p nostr-database"
"-p nostr-sqlite"
"-p nostr-indexeddb"
"-p nostr-sdk-pool"
"-p nostr-sdk"
)

Expand Down
23 changes: 23 additions & 0 deletions crates/nostr-sdk-pool/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[package]
name = "nostr-sdk-pool"
version = "0.27.0"
edition = "2021"
authors.workspace = true
homepage.workspace = true
repository.workspace = true
license.workspace = true
rust-version.workspace = true

[features]
default = []
nip11 = ["nostr/nip11"]
blocking = ["async-utility/blocking", "nostr/blocking"]

[dependencies]
async-utility.workspace = true
async-wsocket = "0.2"
nostr = { workspace = true, features = ["std"] }
nostr-database.workspace = true
thiserror.workspace = true
tokio = { workspace = true, features = ["sync"] }
tracing = { workspace = true, features = ["std", "attributes"] }
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
// Copyright (c) 2023-2024 Rust Nostr Developers
// Distributed under the MIT software license

//! Relay
//! Nostr SDK Pool

#![forbid(unsafe_code)]
#![warn(missing_docs)]
#![warn(rustdoc::bare_urls)]
#![allow(unknown_lints)]
#![allow(clippy::arc_with_non_send_sync)]

use std::collections::{HashMap, HashSet};
#[cfg(not(target_arch = "wasm32"))]
Expand Down Expand Up @@ -34,7 +40,7 @@ use tokio::sync::{broadcast, oneshot, Mutex, RwLock};

mod flags;
pub mod limits;
mod options;
pub mod options;
pub mod pool;
mod stats;

Expand All @@ -50,8 +56,6 @@ use self::options::{
};
pub use self::pool::{RelayPoolMessage, RelayPoolNotification};
pub use self::stats::RelayConnectionStats;
#[cfg(feature = "blocking")]
use crate::RUNTIME;

type Message = (RelayEvent, Option<oneshot::Sender<bool>>);

Expand Down Expand Up @@ -365,12 +369,6 @@ impl Relay {
*status
}

/// Get [`RelayStatus`]
#[cfg(feature = "blocking")]
pub fn status_blocking(&self) -> RelayStatus {
RUNTIME.block_on(async { self.status().await })
}

async fn set_status(&self, status: RelayStatus) {
// Change status
let mut s = self.status.write().await;
Expand Down Expand Up @@ -402,12 +400,6 @@ impl Relay {
document.clone()
}

/// Get [`RelayInformationDocument`]
#[cfg(all(feature = "nip11", feature = "blocking"))]
pub fn document_blocking(&self) -> RelayInformationDocument {
RUNTIME.block_on(async { self.document().await })
}

#[cfg(feature = "nip11")]
async fn set_document(&self, document: RelayInformationDocument) {
let mut d = self.document.write().await;
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,24 @@
// Copyright (c) 2023-2024 Rust Nostr Developers
// Distributed under the MIT software license

//! Relay and Pool options

#[cfg(not(target_arch = "wasm32"))]
use std::net::SocketAddr;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::Arc;
use std::time::Duration;

use super::{AtomicRelayServiceFlags, RelayServiceFlags};
use crate::client::options::DEFAULT_SEND_TIMEOUT;

pub const DEFAULT_RETRY_SEC: u64 = 10;
pub const MIN_RETRY_SEC: u64 = 5;
pub const MAX_ADJ_RETRY_SEC: u64 = 60;
pub const NEGENTROPY_HIGH_WATER_UP: usize = 100;
pub const NEGENTROPY_LOW_WATER_UP: usize = 50;
pub const NEGENTROPY_BATCH_SIZE_DOWN: usize = 50;

/// Default send timeout
pub const DEFAULT_SEND_TIMEOUT: Duration = Duration::from_secs(20);
pub(super) const DEFAULT_RETRY_SEC: u64 = 10;
pub(super) const MIN_RETRY_SEC: u64 = 5;
pub(super) const MAX_ADJ_RETRY_SEC: u64 = 60;
pub(super) const NEGENTROPY_HIGH_WATER_UP: usize = 100;
pub(super) const NEGENTROPY_LOW_WATER_UP: usize = 50;
pub(super) const NEGENTROPY_BATCH_SIZE_DOWN: usize = 50;

/// [`Relay`](super::Relay) options
#[derive(Debug, Clone)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use nostr::message::MessageHandleError;
use nostr::nips::nip01::Coordinate;
use nostr::{
event, ClientMessage, Event, EventId, Filter, JsonUtil, MissingPartialEvent, PartialEvent,
RawRelayMessage, RelayMessage, SubscriptionId, Timestamp, Url,
RawRelayMessage, RelayMessage, SubscriptionId, Timestamp, TryIntoUrl, Url,
};
use nostr_database::{DatabaseError, DynNostrDatabase, IntoNostrDatabase, MemoryDatabase, Order};
use thiserror::Error;
Expand All @@ -26,7 +26,6 @@ use super::{
Error as RelayError, FilterOptions, InternalSubscriptionId, Limits, NegentropyOptions, Relay,
RelayOptions, RelaySendOptions, RelayStatus,
};
use crate::util::TryIntoUrl;

/// [`RelayPool`] error
#[derive(Debug, Error)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ use nostr::Timestamp;
#[cfg(not(target_arch = "wasm32"))]
use tokio::sync::RwLock;

#[cfg(feature = "blocking")]
use crate::RUNTIME;

/// Ping Stats
#[cfg(not(target_arch = "wasm32"))]
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -168,12 +165,6 @@ impl RelayConnectionStats {
sum.checked_div(latencies.len() as u32)
}

/// Calculate latency
#[cfg(all(not(target_arch = "wasm32"), feature = "blocking"))]
pub fn latency_blocking(&self) -> Option<Duration> {
RUNTIME.block_on(async { self.latency().await })
}

pub(crate) fn new_attempt(&self) {
self.attempts.fetch_add(1, Ordering::SeqCst);
}
Expand Down
8 changes: 4 additions & 4 deletions crates/nostr-sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ rustdoc-args = ["--cfg", "docsrs"]

[features]
default = ["all-nips"]
blocking = ["dep:once_cell", "async-utility/blocking", "nostr/blocking"]
blocking = ["dep:once_cell", "async-utility/blocking", "nostr/blocking", "nostr-sdk-pool/blocking"]
sqlite = ["dep:nostr-sqlite"]
indexeddb = ["dep:nostr-indexeddb"]
all-nips = ["nip04", "nip05", "nip06", "nip07", "nip11", "nip44", "nip46", "nip47", "nip57", "nip59"]
Expand All @@ -26,7 +26,7 @@ nip04 = ["nostr/nip04"]
nip05 = ["nostr/nip05"]
nip06 = ["nostr/nip06"]
nip07 = ["nostr/nip07"]
nip11 = ["nostr/nip11"]
nip11 = ["nostr/nip11", "nostr-sdk-pool/nip11"]
nip44 = ["nostr/nip44"]
nip46 = ["nostr/nip46"]
nip47 = ["nostr/nip47"]
Expand All @@ -35,17 +35,17 @@ nip59 = ["nostr/nip59"]

[dependencies]
async-utility.workspace = true
async-wsocket = "0.2"
lnurl-pay = { version = "0.3", features = ["api"], optional = true }
nostr = { workspace = true, features = ["std"] }
nostr-database.workspace = true
nostr-sdk-pool.workspace = true
once_cell = { workspace = true, optional = true }
thiserror.workspace = true
tracing = { workspace = true, features = ["std", "attributes"] }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
nostr-sqlite = { version = "0.27", path = "../nostr-sqlite", optional = true }
tokio = { workspace = true, features = ["rt-multi-thread", "time", "macros", "sync"] }
tokio = { workspace = true, features = ["rt-multi-thread", "macros", "sync"] }

[target.'cfg(target_arch = "wasm32")'.dependencies]
nostr-indexeddb = { version = "0.27", path = "../nostr-indexeddb", optional = true }
Expand Down
4 changes: 2 additions & 2 deletions crates/nostr-sdk/src/client/blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ use std::time::Duration;

use nostr::prelude::*;
use nostr_database::DynNostrDatabase;
use nostr_sdk_pool::{pool, NegentropyOptions, Relay, RelayOptions, RelayPoolNotification};
use tokio::sync::broadcast;

use super::signer::ClientSigner;
use super::{Error, Options, TryIntoUrl};
use crate::relay::{pool, Relay, RelayOptions, RelayPoolNotification};
use crate::{ClientBuilder, NegentropyOptions, RUNTIME};
use crate::{ClientBuilder, RUNTIME};

#[derive(Debug, Clone)]
pub struct Client {
Expand Down
11 changes: 5 additions & 6 deletions crates/nostr-sdk/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ use nostr::event::builder::Error as EventBuilderError;
use nostr::prelude::*;
use nostr::types::metadata::Error as MetadataError;
use nostr_database::DynNostrDatabase;
use nostr_sdk_pool::pool::{self, Error as RelayPoolError, RelayPool};
use nostr_sdk_pool::{
Error as RelayError, FilterOptions, NegentropyOptions, Relay, RelayOptions,
RelayPoolNotification, RelaySendOptions,
};
use tokio::sync::{broadcast, RwLock};

#[cfg(feature = "blocking")]
Expand All @@ -32,12 +37,6 @@ pub use self::signer::nip46::Nip46Signer;
pub use self::signer::{ClientSigner, ClientSignerType};
#[cfg(feature = "nip57")]
pub use self::zapper::{ClientZapper, ZapDetails, ZapEntity};
use crate::relay::pool::{self, Error as RelayPoolError, RelayPool};
use crate::relay::{
Error as RelayError, FilterOptions, NegentropyOptions, Relay, RelayOptions,
RelayPoolNotification, RelaySendOptions,
};
use crate::util::TryIntoUrl;

/// [`Client`] error
#[derive(Debug, thiserror::Error)]
Expand Down
Loading

0 comments on commit 6931585

Please sign in to comment.