Skip to content

Commit 1e778a2

Browse files
committed
sdk: update to use nostr-gossip crate
Signed-off-by: Yuki Kishimoto <[email protected]>
1 parent 62bb0ba commit 1e778a2

File tree

10 files changed

+375
-751
lines changed

10 files changed

+375
-751
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/nostr-sdk/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ nip98 = ["nostr/nip98"]
3535
async-utility.workspace = true
3636
nostr = { workspace = true, features = ["std"] }
3737
nostr-database.workspace = true
38+
nostr-gossip.workspace = true
3839
nostr-relay-pool.workspace = true
3940
tokio = { workspace = true, features = ["sync"] }
4041
tracing = { workspace = true, features = ["std"] }
@@ -43,6 +44,7 @@ tracing = { workspace = true, features = ["std"] }
4344
nostr-connect.workspace = true
4445
nostr-lmdb.workspace = true
4546
nostr-ndb.workspace = true
47+
nostr-gossip-memory.workspace = true
4648
tokio = { workspace = true, features = ["macros"] }
4749
tracing-subscriber = { workspace = true, features = ["env-filter"] }
4850

crates/nostr-sdk/examples/gossip.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@
44

55
use std::time::Duration;
66

7+
use nostr_gossip_memory::prelude::*;
78
use nostr_sdk::prelude::*;
89

910
#[tokio::main]
1011
async fn main() -> Result<()> {
1112
tracing_subscriber::fmt::init();
1213

1314
let keys = Keys::parse("nsec1ufnus6pju578ste3v90xd5m2decpuzpql2295m3sknqcjzyys9ls0qlc85")?;
14-
let opts = ClientOptions::new().gossip(true);
15-
let client = Client::builder().signer(keys).opts(opts).build();
15+
let gossip = NostrGossipMemory::new();
16+
let client = Client::builder().signer(keys).gossip(gossip).build();
1617

1718
client.add_discovery_relay("wss://relay.damus.io").await?;
1819
client.add_discovery_relay("wss://purplepag.es").await?;

crates/nostr-sdk/src/client/builder.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use std::sync::Arc;
99
use nostr::signer::{IntoNostrSigner, NostrSigner};
1010
use nostr_database::memory::MemoryDatabase;
1111
use nostr_database::{IntoNostrDatabase, NostrDatabase};
12+
use nostr_gossip::NostrGossip;
1213
use nostr_relay_pool::monitor::Monitor;
1314
use nostr_relay_pool::policy::AdmitPolicy;
1415
use nostr_relay_pool::transport::websocket::{
@@ -29,6 +30,8 @@ pub struct ClientBuilder {
2930
pub admit_policy: Option<Arc<dyn AdmitPolicy>>,
3031
/// Database
3132
pub database: Arc<dyn NostrDatabase>,
33+
/// Gossip
34+
pub gossip: Option<Arc<dyn NostrGossip>>,
3235
/// Relay monitor
3336
pub monitor: Option<Monitor>,
3437
/// Client options
@@ -42,6 +45,7 @@ impl Default for ClientBuilder {
4245
websocket_transport: Arc::new(DefaultWebsocketTransport),
4346
admit_policy: None,
4447
database: Arc::new(MemoryDatabase::default()),
48+
gossip: None,
4549
monitor: None,
4650
opts: ClientOptions::default(),
4751
}
@@ -106,6 +110,16 @@ impl ClientBuilder {
106110
self
107111
}
108112

113+
/// Set a gossip database
114+
#[inline]
115+
pub fn gossip<T>(mut self, gossip: T) -> Self
116+
where
117+
T: NostrGossip + 'static,
118+
{
119+
self.gossip = Some(Arc::new(gossip));
120+
self
121+
}
122+
109123
/// Set monitor
110124
#[inline]
111125
pub fn monitor(mut self, monitor: Monitor) -> Self {

crates/nostr-sdk/src/client/error.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::fmt;
77
use nostr::prelude::*;
88
use nostr::serde_json;
99
use nostr_database::prelude::*;
10+
use nostr_gossip::error::GossipError;
1011
use nostr_relay_pool::__private::SharedStateError;
1112
use nostr_relay_pool::prelude::*;
1213

@@ -21,6 +22,8 @@ pub enum Error {
2122
Database(DatabaseError),
2223
/// Signer error
2324
Signer(SignerError),
25+
/// Gossip error
26+
Gossip(GossipError),
2427
/// [`EventBuilder`] error
2528
EventBuilder(event::builder::Error),
2629
/// Json error
@@ -45,6 +48,7 @@ impl fmt::Display for Error {
4548
Self::RelayPool(e) => e.fmt(f),
4649
Self::Database(e) => e.fmt(f),
4750
Self::Signer(e) => e.fmt(f),
51+
Self::Gossip(e) => e.fmt(f),
4852
Self::EventBuilder(e) => e.fmt(f),
4953
Self::Json(e) => e.fmt(f),
5054
Self::SharedState(e) => e.fmt(f),
@@ -82,6 +86,12 @@ impl From<SignerError> for Error {
8286
}
8387
}
8488

89+
impl From<GossipError> for Error {
90+
fn from(e: GossipError) -> Self {
91+
Self::Gossip(e)
92+
}
93+
}
94+
8595
impl From<event::builder::Error> for Error {
8696
fn from(e: event::builder::Error) -> Self {
8797
Self::EventBuilder(e)

crates/nostr-sdk/src/client/middleware.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@ use std::sync::Arc;
22

33
use nostr::util::BoxedFuture;
44
use nostr::{Event, RelayUrl, SubscriptionId};
5+
use nostr_gossip::NostrGossip;
56
use nostr_relay_pool::policy::{AdmitPolicy, AdmitStatus, PolicyError};
67

7-
use crate::gossip::Gossip;
8-
98
#[derive(Debug)]
109
pub(crate) struct AdmissionPolicyMiddleware {
11-
pub(crate) gossip: Option<Gossip>,
10+
pub(crate) gossip: Option<Arc<dyn NostrGossip>>,
1211
pub(crate) external_policy: Option<Arc<dyn AdmitPolicy>>,
1312
}
1413

@@ -34,7 +33,10 @@ impl AdmitPolicy for AdmissionPolicyMiddleware {
3433
Box::pin(async move {
3534
// Process event in gossip
3635
if let Some(gossip) = &self.gossip {
37-
gossip.process_event(event).await;
36+
gossip
37+
.process(event, Some(relay_url))
38+
.await
39+
.map_err(PolicyError::backend)?;
3840
}
3941

4042
// Check if event is allowed by external policy

0 commit comments

Comments
 (0)