Skip to content

Commit 2d12efc

Browse files
committed
gossip: init
- Create `nostr-gossip` crate - Move gossip stuff from `nostr-sdk` to `nostr-gossip` - Add `gossip` feature to `nostr-sdk` - Add persistent and in-memory SQLite storage for gossip data - Add a middleware in the SDK to process events for gossip - Allows passing additional relays when breaking-down filters Closes #570 Signed-off-by: Yuki Kishimoto <yukikishimoto@protonmail.com>
1 parent 3e8e6a7 commit 2d12efc

File tree

27 files changed

+1540
-730
lines changed

27 files changed

+1540
-730
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ negentropy-deprecated = { package = "negentropy", version = "0.3", default-featu
2828
nostr = { version = "0.39", path = "./crates/nostr", default-features = false }
2929
nostr-connect = { version = "0.39", path = "./crates/nostr-connect", default-features = false }
3030
nostr-database = { version = "0.39", path = "./crates/nostr-database", default-features = false }
31+
nostr-gossip = { version = "0.39", path = "./crates/nostr-gossip", default-features = false }
3132
nostr-indexeddb = { version = "0.39", path = "./crates/nostr-indexeddb", default-features = false }
3233
nostr-lmdb = { version = "0.39", path = "./crates/nostr-lmdb", default-features = false }
3334
nostr-ndb = { version = "0.39", path = "./crates/nostr-ndb", default-features = false }

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ The project is split up into several crates in the `crates/` directory:
1111
* [**nostr-lmdb**](./crates/nostr-lmdb): LMDB storage backend
1212
* [**nostr-ndb**](./crates/nostr-ndb): [nostrdb](https://github.com/damus-io/nostrdb) storage backend
1313
* [**nostr-indexeddb**](./crates/nostr-indexeddb): IndexedDB storage backend
14+
* [**nostr-gossip**](./crates/nostr-gossip): Gossip tracker and storage
1415
* [**nostr-relay-pool**](./crates/nostr-relay-pool): Nostr Relay Pool
1516
* [**nostr-sdk**](./crates/nostr-sdk): High level client library
1617
* [**nwc**](./crates/nwc): Nostr Wallet Connect (NWC) client

contrib/scripts/check-crates.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@ buildargs=(
4545
"-p nostr-relay-builder"
4646
"-p nostr-connect"
4747
"-p nwc"
48+
"-p nostr-gossip"
4849
"-p nostr-sdk" # No default features
4950
"-p nostr-sdk --features all-nips" # Only NIPs features
51+
"-p nostr-sdk --features gossip" # Gossip stuff
5052
"-p nostr-sdk --features tor" # Embedded tor client
5153
"-p nostr-sdk --all-features" # All features
5254
"-p nostr-cli"

contrib/scripts/release.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ args=(
1212
"-p nostr-relay-pool"
1313
"-p nwc"
1414
"-p nostr-connect"
15+
"-p nostr-gossip"
1516
"-p nostr-sdk"
1617
"-p nostr-cli"
1718
)

crates/nostr-gossip/Cargo.toml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[package]
2+
name = "nostr-gossip"
3+
version = "0.39.0"
4+
edition = "2021"
5+
description = "Gossip storage for nostr apps"
6+
authors.workspace = true
7+
homepage.workspace = true
8+
repository.workspace = true
9+
license.workspace = true
10+
readme = "README.md"
11+
rust-version.workspace = true
12+
keywords = ["nostr", "gossip"]
13+
14+
[dependencies]
15+
async-utility.workspace = true
16+
lru.workspace = true
17+
nostr = { workspace = true, features = ["std"] }
18+
rusqlite = { version = "0.32", features = ["bundled"] }
19+
tracing = { workspace = true, features = ["std"] }
20+
21+
[dev-dependencies]
22+
tokio = { workspace = true, features = ["macros", "rt"] }

crates/nostr-gossip/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Nostr Gossip
2+
3+
## State
4+
5+
**This library is in an ALPHA state**, things that are implemented generally work but the API will change in breaking ways.
6+
7+
## Donations
8+
9+
`rust-nostr` is free and open-source. This means we do not earn any revenue by selling it. Instead, we rely on your financial support. If you actively use any of the `rust-nostr` libs/software/services, then please [donate](https://rust-nostr.org/donate).
10+
11+
## License
12+
13+
This project is distributed under the MIT software license - see the [LICENSE](../../LICENSE) file for details
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
-- Database settings
2+
PRAGMA journal_mode = WAL;
3+
PRAGMA synchronous = NORMAL;
4+
PRAGMA foreign_keys = ON;
5+
PRAGMA user_version = 1; -- Schema version
6+
7+
CREATE TABLE IF NOT EXISTS users (
8+
id INTEGER PRIMARY KEY AUTOINCREMENT,
9+
public_key BLOB NOT NULL UNIQUE
10+
);
11+
12+
CREATE TABLE IF NOT EXISTS relays (
13+
id INTEGER PRIMARY KEY AUTOINCREMENT,
14+
relay_url TEXT NOT NULL UNIQUE
15+
);
16+
17+
CREATE TABLE IF NOT EXISTS lists (
18+
id INTEGER PRIMARY KEY AUTOINCREMENT,
19+
pkid INTEGER NOT NULL, -- Public Key ID
20+
kind INTEGER NOT NULL, -- Kind of the list (i.e., 10002 for NIP65, 10050 for NIP17)
21+
created_at INTEGER NOT NULL DEFAULT 0, -- Event list created at (`created_at` field of event)
22+
last_check INTEGER NOT NULL DEFAULT 0, -- The timestamp of the last check
23+
FOREIGN KEY(pkid) REFERENCES users(id) ON DELETE CASCADE ON UPDATE NO ACTION
24+
);
25+
26+
CREATE UNIQUE INDEX IF NOT EXISTS pubkey_list_idx ON lists(pkid,kind);
27+
28+
CREATE TABLE IF NOT EXISTS relays_by_list (
29+
id INTEGER PRIMARY KEY AUTOINCREMENT,
30+
pkid INTEGER NOT NULL, -- Public Key ID
31+
listid INTEGER NOT NULL, -- List ID
32+
relayid INTEGER NOT NULL, -- Relay ID
33+
metadata TEXT DEFAULT NULL, -- NIP65 metadata: read, write or NULL
34+
FOREIGN KEY(pkid) REFERENCES users(id) ON DELETE CASCADE ON UPDATE NO ACTION,
35+
FOREIGN KEY(listid) REFERENCES lists(id) ON DELETE CASCADE ON UPDATE NO ACTION,
36+
FOREIGN KEY(relayid) REFERENCES relays(id)
37+
);
38+
39+
CREATE UNIQUE INDEX IF NOT EXISTS pubkey_list_relay_idx ON relays_by_list(pkid,listid,relayid);
40+
41+
-- CREATE TABLE IF NOT EXISTS tracker (
42+
-- id INTEGER PRIMARY KEY AUTOINCREMENT,
43+
-- pkid INTEGER NOT NULL, -- Public Key ID
44+
-- relayid INTEGER NOT NULL, -- Relay ID
45+
-- last_event INTEGER NOT NULL DEFAULT 0, -- Timestamp of the last event seen for the public key on the relay
46+
-- score INTEGER NOT NULL DEFAULT 5, -- Score
47+
-- FOREIGN KEY(pkid) REFERENCES users(id) ON DELETE CASCADE ON UPDATE NO ACTION,
48+
-- FOREIGN KEY(relayid) REFERENCES relays(id)
49+
-- );
50+
--
51+
-- CREATE UNIQUE INDEX IF NOT EXISTS pubkey_relay_idx ON tracker(pkid,relayid);

crates/nostr-gossip/src/constant.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright (c) 2022-2023 Yuki Kishimoto
2+
// Copyright (c) 2023-2025 Rust Nostr Developers
3+
// Distributed under the MIT software license
4+
5+
use std::num::NonZeroUsize;
6+
use std::time::Duration;
7+
8+
/// Max number of relays allowed in NIP17/NIP65 lists
9+
pub(crate) const MAX_RELAYS_LIST: usize = 5;
10+
pub(crate) const PUBKEY_METADATA_OUTDATED_AFTER: Duration = Duration::from_secs(60 * 60); // 60 min
11+
pub(crate) const CHECK_OUTDATED_INTERVAL: Duration = Duration::from_secs(60 * 5); // 5 min
12+
13+
pub(crate) const CACHE_SIZE: Option<NonZeroUsize> = NonZeroUsize::new(10_000);

0 commit comments

Comments
 (0)