Skip to content

Commit 8927f66

Browse files
committed
Merge "Gossip traits and in-memory storage"
Partially closes #570 Pull-Request: #1114 Signed-off-by: Yuki Kishimoto <[email protected]>
2 parents 4e08f80 + 593cf7a commit 8927f66

File tree

29 files changed

+1432
-765
lines changed

29 files changed

+1432
-765
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ jobs:
3535
- nostr-blossom
3636
- nostr-http-file-storage
3737
- nostr-database
38+
- nostr-gossip
39+
- nostr-gossip-memory
3840
- nostr-lmdb
3941
- nostr-indexeddb --target wasm32-unknown-unknown
4042
- nostr-ndb

Cargo.lock

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

Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ members = [
88
"database/nostr-lmdb",
99
"database/nostr-ndb",
1010

11+
# Gossip
12+
"gossip/nostr-gossip",
13+
"gossip/nostr-gossip-memory",
14+
1115
# Remote File Storage implementations
1216
"rfs/nostr-blossom",
1317
"rfs/nostr-http-file-storage",
@@ -39,6 +43,8 @@ negentropy = { version = "0.5", default-features = false }
3943
nostr = { version = "0.43", path = "./crates/nostr", default-features = false }
4044
nostr-connect = { version = "0.43", path = "./signer/nostr-connect", default-features = false }
4145
nostr-database = { version = "0.43", path = "./database/nostr-database", default-features = false }
46+
nostr-gossip = { version = "0.43", path = "./gossip/nostr-gossip", default-features = false }
47+
nostr-gossip-memory = { version = "0.43", path = "./gossip/nostr-gossip-memory", default-features = false }
4248
nostr-lmdb = { version = "0.43", path = "./database/nostr-lmdb", default-features = false }
4349
nostr-ndb = { version = "0.43", path = "./database/nostr-ndb", default-features = false }
4450
nostr-relay-builder = { version = "0.43", path = "./crates/nostr-relay-builder", default-features = false }

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ The project is split up into several crates:
1313
- [**nostr-lmdb**](./database/nostr-lmdb): LMDB storage backend
1414
- [**nostr-ndb**](./database/nostr-ndb): [nostrdb](https://github.com/damus-io/nostrdb) storage backend
1515
- [**nostr-indexeddb**](./database/nostr-indexeddb): IndexedDB storage backend
16+
- [**nostr-gossip**](./gossip/nostr-gossip): Gossip traits
17+
- [**nostr-gossip-memory**](./gossip/nostr-gossip-memory): In-memory gossip database
1618
- Remote File Storage implementations:
1719
- [**nostr-blossom**](./rfs/nostr-blossom): A library for interacting with the Blossom protocol
1820
- [**nostr-http-file-storage**](./rfs/nostr-http-file-storage): HTTP File Storage client (NIP-96)

contrib/scripts/check-crates.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ buildargs=(
3434
"-p nostr-blossom"
3535
"-p nostr-http-file-storage"
3636
"-p nostr-database"
37+
"-p nostr-gossip"
38+
"-p nostr-gossip-memory"
3739
"-p nostr-lmdb"
3840
"-p nostr-indexeddb --target wasm32-unknown-unknown"
3941
"-p nostr-ndb"

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/bot.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@
22
// Copyright (c) 2023-2025 Rust Nostr Developers
33
// Distributed under the MIT software license
44

5+
use nostr_gossip_memory::prelude::*;
56
use nostr_sdk::prelude::*;
67

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

1112
let keys = Keys::parse("nsec12kcgs78l06p30jz7z7h3n2x2cy99nw2z6zspjdp7qc206887mwvs95lnkx")?;
13+
let gossip = NostrGossipMemory::unbounded();
1214
let client = Client::builder()
1315
.signer(keys.clone())
14-
.opts(ClientOptions::new().gossip(true))
16+
.gossip(gossip)
1517
.build();
1618

1719
println!("Bot public key: {}", keys.public_key().to_bech32()?);

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::unbounded();
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)

0 commit comments

Comments
 (0)