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

Add redb backend #676

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ NIP35 support, better logs and docs, performance improvements, bugs fix and more
* nostr: don't use reply event as root `e` tag i no root is set in `EventBuilder::text_note_reply` ([Yuki Kishimoto])
* database: add manual trait implementations for `BTreeCappedSet` ([Yuki Kishimoto])
* database: replace LRU with custom memory cache for IDs tracking ([Yuki Kishimoto])
* database: remove `DatabaseHelper` from `MemoryDatabase` ([Yuki Kishimoto])
* lmdb: use `async-utility` to spawn blocking tasks ([Yuki Kishimoto])
* ndb: bump `nostr-ndb` to 0.5 ([Yuki Kishimoto])
* pool: add `PingTracker` and improve relay ping management ([Yuki Kishimoto])
Expand Down Expand Up @@ -210,6 +211,7 @@ NIP35 support, better logs and docs, performance improvements, bugs fix and more
* nostr: add `Kind::is_addressable` and `ADDRESSABLE_RANGE` ([Yuki Kishimoto])
* database: impl PartialEq and Eq for `Events` ([Yuki Kishimoto])
* database: add `SaveEventStatus` enum ([Yuki Kishimoto])
* redb: add `nostr-redb` ([Yuki Kishimoto])
* pool: add `ReceiverStream` ([Yuki Kishimoto])
* Add `SubscribeAutoCloseOptions::idle_timeout` ([Yuki Kishimoto])
* sdk: automatically resend event after NIP-42 authentication ([Yuki Kishimoto])
Expand Down
30 changes: 28 additions & 2 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ nostr-database = { version = "0.38", path = "./crates/nostr-database", default-f
nostr-indexeddb = { version = "0.38", path = "./crates/nostr-indexeddb", default-features = false }
nostr-lmdb = { version = "0.38", path = "./crates/nostr-lmdb", default-features = false }
nostr-ndb = { version = "0.38", path = "./crates/nostr-ndb", default-features = false }
nostr-redb = { version = "0.38", path = "./crates/nostr-redb", default-features = false }
nostr-relay-builder = { version = "0.38", path = "./crates/nostr-relay-builder", default-features = false }
nostr-relay-pool = { version = "0.38", path = "./crates/nostr-relay-pool", default-features = false }
nostr-sdk = { version = "0.38", path = "./crates/nostr-sdk", default-features = false }
Expand All @@ -44,6 +45,7 @@ wasm-bindgen-futures = "0.4"
web-sys = { version = "0.3", default-features = false }

[patch.crates-io]
async-utility = { git = "https://github.com/yukibtc/async-utility", rev = "575b6fb0b4e270ae44208d4b7c234366d33e32f6" }
# Patch needed to reduce bindings size
bip39 = { git = "https://github.com/yukibtc/rust-bip39", rev = "eade7c56eff5f320e8eb5beee23dd8fb46413938" } # Uses bitcoin_hashes v0.14

Expand Down
1 change: 1 addition & 0 deletions bindings/nostr-sdk-js/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ crate-type = ["cdylib"]
console_error_panic_hook = "0.1"
js-sys.workspace = true
nostr-connect.workspace = true
nostr-redb.workspace = true
nostr-sdk = { workspace = true, default-features = false, features = ["all-nips", "indexeddb"] }
nwc.workspace = true
tracing.workspace = true
Expand Down
14 changes: 9 additions & 5 deletions bindings/nostr-sdk-js/examples/negentropy.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
const { loadWasmAsync, initLogger, LogLevel, SyncOptions, SyncDirection, Filter, Client, NostrDatabase } = require("../");

// NOTE: this code work only on browser (due to indexeddb)!
const { loadWasmAsync, initLogger, LogLevel, SyncOptions, SyncDirection, Filter, Client, NostrDatabase, Kind } = require("../");

async function main() {
await loadWasmAsync();

initLogger(LogLevel.info());

let db = await NostrDatabase.indexeddb("js-test");
let db = await NostrDatabase.inMemory();
let client = Client.builder().database(db).build();

await client.addRelay("wss://relay.damus.io");

await client.connect();

let filter = new Filter().kind(1).limit(1000);
let filter = new Filter().kind(new Kind(1)).limit(1000);
let opts = new SyncOptions().direction(SyncDirection.Down);
await client.sync(filter, opts);

let f = new Filter().limit(2);
let events = await db.query([f]);
events.forEach((e) => {
console.log(e.asJson())
})
}

main();
2 changes: 1 addition & 1 deletion bindings/nostr-sdk-js/examples/webapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"@rust-nostr/nostr-sdk": "^0.33.0",
"@rust-nostr/nostr-sdk": "file:../../rust-nostr-nostr-sdk-0.37.0.tgz",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
Expand Down
17 changes: 11 additions & 6 deletions bindings/nostr-sdk-js/examples/webapp/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class App extends Component {

// Try to initialize log
try {
initLogger(LogLevel.info());
initLogger(LogLevel.debug());
console.log("Logger initialized");
} catch (error) {}
}

Expand All @@ -28,7 +29,11 @@ class App extends Component {
let nip07_signer = new BrowserSigner();
let signer = NostrSigner.nip07(nip07_signer);
let zapper = await NostrZapper.webln();
let db = await NostrDatabase.indexeddb("nostr-sdk-webapp-example");

console.log("Opening database...");
let db = await NostrDatabase.web("nostr-sdk-webapp-example-2");
console.log("Database opened.");

let client = new ClientBuilder().signer(signer).zapper(zapper).database(db).build();

let public_key = await nip07_signer.getPublicKey();
Expand All @@ -51,7 +56,7 @@ class App extends Component {
handleReconcile = async () => {
try {
let filter = new Filter().author(this.state.public_key);
let opts = new NegentropyOptions();
let opts = new SyncOptions();
await this.state.client.sync(filter, opts);
} catch (error) {
console.log(error)
Expand All @@ -65,15 +70,15 @@ class App extends Component {
console.time("query");
let events = await database.query([filter]);
console.timeEnd("query");
console.log("Got", events.length, "events");
console.log("Got", events.len(), "events");
} catch (error) {
console.log(error)
}
}

handlePublishTextNote = async () => {
try {
let builder = EventBuilder.textNote("Test from rust-nostr JavaScript bindings with NIP07 signer!", []);
let builder = EventBuilder.textNote("Test from rust-nostr JavaScript bindings with NIP07 signer!");
await this.state.client.sendEventBuilder(builder);
} catch (error) {
console.log(error)
Expand All @@ -82,7 +87,7 @@ class App extends Component {

handleZap = async () => {
try {
let pk = PublicKey.fromBech32("npub1drvpzev3syqt0kjrls50050uzf25gehpz9vgdw08hvex7e0vgfeq0eseet");
let pk = PublicKey.parse("npub1drvpzev3syqt0kjrls50050uzf25gehpz9vgdw08hvex7e0vgfeq0eseet");
let entity = ZapEntity.publicKey(pk);
let details = new ZapDetails(ZapType.Public).message("Zap for Rust Nostr!");
await this.state.client.zap(entity, 1000, details);
Expand Down
18 changes: 7 additions & 11 deletions bindings/nostr-sdk-js/src/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use std::ops::Deref;
use std::sync::Arc;

use nostr_redb::NostrRedb;
use nostr_sdk::prelude::*;
use wasm_bindgen::prelude::*;

Expand Down Expand Up @@ -74,22 +75,17 @@ impl From<Arc<dyn NostrDatabase>> for JsNostrDatabase {

#[wasm_bindgen(js_class = NostrDatabase)]
impl JsNostrDatabase {
/// Open/Create database with **unlimited** capacity
pub async fn indexeddb(name: &str) -> Result<JsNostrDatabase> {
let db = WebDatabase::open(name).await.map_err(into_err)?;
/// Open (or create) persistent web database
pub async fn web(name: &str) -> Result<JsNostrDatabase> {
let db = NostrRedb::web(name).await.map_err(into_err)?;
Ok(Self {
inner: db.into_nostr_database(),
})
}

/// Open/Create database with **limited** capacity
#[wasm_bindgen(js_name = indexeddbBounded)]
pub async fn indexeddb_bounded(name: &str, max_capacity: u64) -> Result<JsNostrDatabase> {
let db = Arc::new(
WebDatabase::open_bounded(name, max_capacity as usize)
.await
.map_err(into_err)?,
);
#[wasm_bindgen(js_name = inMemory)]
pub fn in_memory() -> Result<JsNostrDatabase> {
let db = NostrRedb::in_memory().map_err(into_err)?;
Ok(Self {
inner: db.into_nostr_database(),
})
Expand Down
Loading
Loading