Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve negentropy reconciliation #265

Merged
merged 4 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions bindings/nostr-sdk-ffi/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use nostr_ffi::{
};
use nostr_sdk::client::blocking::Client as ClientSdk;
use nostr_sdk::relay::RelayPoolNotification as RelayPoolNotificationSdk;
use nostr_sdk::NegentropyOptions;
use uniffi::Object;

mod builder;
Expand All @@ -25,6 +24,7 @@ pub use self::builder::ClientBuilder;
pub use self::options::Options;
pub use self::signer::ClientSigner;
use crate::error::Result;
use crate::relay::options::NegentropyOptions;
use crate::{NostrDatabase, Relay};

#[derive(Object)]
Expand Down Expand Up @@ -255,11 +255,10 @@ impl Client {
))
}

pub fn reconcile(&self, filter: Arc<Filter>) -> Result<()> {
Ok(self.inner.reconcile(
filter.as_ref().deref().clone(),
NegentropyOptions::default(),
)?)
pub fn reconcile(&self, filter: Arc<Filter>, opts: Arc<NegentropyOptions>) -> Result<()> {
Ok(self
.inner
.reconcile(filter.as_ref().deref().clone(), **opts)?)
}

pub fn handle_notifications(self: Arc<Self>, handler: Box<dyn HandleNotification>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use nostr_ffi::{ClientMessage, Event, Filter, RelayInformationDocument, Timestam
use nostr_sdk::{block_on, relay, FilterOptions};
use uniffi::{Enum, Object};

pub mod options;

use crate::error::Result;

#[derive(Object)]
Expand Down
65 changes: 65 additions & 0 deletions bindings/nostr-sdk-ffi/src/relay/options.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright (c) 2022-2023 Yuki Kishimoto
// Copyright (c) 2023-2024 Rust Nostr Developers
// Distributed under the MIT software license

use std::ops::Deref;
use std::sync::Arc;
use std::time::Duration;

use nostr_ffi::helper::unwrap_or_clone_arc;
use uniffi::{Enum, Object};

#[derive(Enum)]
pub enum NegentropyDirection {
Up,
Down,
Both,
}

impl From<NegentropyDirection> for nostr_sdk::NegentropyDirection {
fn from(value: NegentropyDirection) -> Self {
match value {
NegentropyDirection::Up => Self::Up,
NegentropyDirection::Down => Self::Down,
NegentropyDirection::Both => Self::Both,
}
}
}

#[derive(Clone, Object)]
pub struct NegentropyOptions {
inner: nostr_sdk::NegentropyOptions,
}

impl Deref for NegentropyOptions {
type Target = nostr_sdk::NegentropyOptions;

fn deref(&self) -> &Self::Target {
&self.inner
}
}

#[uniffi::export]
impl NegentropyOptions {
/// New default options
#[uniffi::constructor]
pub fn new() -> Self {
Self {
inner: nostr_sdk::NegentropyOptions::new(),
}
}

/// Timeout to check if negentropy it's supported (default: 10 secs)
pub fn initial_timeout(self: Arc<Self>, timeout: Duration) -> Self {
let mut builder = unwrap_or_clone_arc(self);
builder.inner = builder.inner.initial_timeout(timeout);
builder
}

/// Negentropy Sync direction (default: down)
pub fn direction(self: Arc<Self>, direction: NegentropyDirection) -> Self {
let mut builder = unwrap_or_clone_arc(self);
builder.inner = builder.inner.direction(direction.into());
builder
}
}
5 changes: 3 additions & 2 deletions bindings/nostr-sdk-js/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub use self::signer::JsClientSigner;
use self::zapper::{JsZapDetails, JsZapEntity};
use crate::abortable::JsAbortHandle;
use crate::database::JsNostrDatabase;
use crate::relay::options::JsNegentropyOptions;
use crate::relay::{JsRelay, JsRelayArray};

#[wasm_bindgen(js_name = Client)]
Expand Down Expand Up @@ -525,9 +526,9 @@ impl JsClient {
/// Negentropy reconciliation
///
/// <https://github.com/hoytech/negentropy>
pub async fn reconcile(&self, filter: &JsFilter) -> Result<()> {
pub async fn reconcile(&self, filter: &JsFilter, opts: &JsNegentropyOptions) -> Result<()> {
self.inner
.reconcile(filter.deref().clone(), NegentropyOptions::default())
.reconcile(filter.deref().clone(), **opts)
.await
.map_err(into_err)
}
Expand Down
2 changes: 2 additions & 0 deletions bindings/nostr-sdk-js/src/relay/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use nostr_sdk::prelude::*;
use nostr_sdk::relay::Relay;
use wasm_bindgen::prelude::*;

pub mod options;

#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(typescript_type = "JsRelay[]")]
Expand Down
68 changes: 68 additions & 0 deletions bindings/nostr-sdk-js/src/relay/options.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright (c) 2022-2023 Yuki Kishimoto
// Copyright (c) 2023-2024 Rust Nostr Developers
// Distributed under the MIT software license

use core::ops::Deref;

use nostr_sdk::{NegentropyDirection, NegentropyOptions};
use wasm_bindgen::prelude::*;

use crate::duration::JsDuration;

#[wasm_bindgen(js_name = NegentropyDirection)]
pub enum JsNegentropyDirection {
Up,
Down,
Both,
}

impl From<JsNegentropyDirection> for NegentropyDirection {
fn from(value: JsNegentropyDirection) -> Self {
match value {
JsNegentropyDirection::Up => Self::Up,
JsNegentropyDirection::Down => Self::Down,
JsNegentropyDirection::Both => Self::Both,
}
}
}

#[wasm_bindgen(js_name = NegentropyOptions)]
pub struct JsNegentropyOptions {
inner: NegentropyOptions,
}

impl Deref for JsNegentropyOptions {
type Target = NegentropyOptions;

fn deref(&self) -> &Self::Target {
&self.inner
}
}

impl From<NegentropyOptions> for JsNegentropyOptions {
fn from(inner: NegentropyOptions) -> Self {
Self { inner }
}
}

#[wasm_bindgen(js_class = NegentropyOptions)]
impl JsNegentropyOptions {
/// New default options
#[wasm_bindgen(constructor)]
pub fn new() -> Self {
Self {
inner: NegentropyOptions::new(),
}
}

/// Timeout to check if negentropy it's supported (default: 10 secs)
#[wasm_bindgen(js_name = initialTimeout)]
pub fn initial_timeout(self, timeout: JsDuration) -> Self {
self.inner.initial_timeout(*timeout).into()
}

/// Negentropy Sync direction (default: down)
pub fn direction(self, direction: JsNegentropyDirection) -> Self {
self.inner.direction(direction.into()).into()
}
}
6 changes: 3 additions & 3 deletions crates/nostr-sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ pub mod util;
pub use self::client::blocking;
pub use self::client::{Client, ClientBuilder, ClientSigner, Options};
pub use self::relay::{
ActiveSubscription, FilterOptions, InternalSubscriptionId, NegentropyOptions, Relay,
RelayConnectionStats, RelayOptions, RelayPoolNotification, RelayPoolOptions, RelaySendOptions,
RelayStatus,
ActiveSubscription, FilterOptions, InternalSubscriptionId, NegentropyDirection,
NegentropyOptions, Relay, RelayConnectionStats, RelayOptions, RelayPoolNotification,
RelayPoolOptions, RelaySendOptions, RelayStatus,
};

#[cfg(feature = "blocking")]
Expand Down
6 changes: 4 additions & 2 deletions crates/nostr-sdk/src/relay/limits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ pub struct Limits {
impl Default for Limits {
fn default() -> Self {
Self {
messages: MessagesLimits { max_size: 128_000 },
events: EventsLimits { max_size: 65_536 },
messages: MessagesLimits {
max_size: 5_250_000,
},
events: EventsLimits { max_size: 70_000 },
}
}
}
Expand Down
Loading
Loading