From 3c8eb05565689073c3139c088ecd6453997f8817 Mon Sep 17 00:00:00 2001 From: kieran Date: Thu, 26 Dec 2024 15:51:42 +0000 Subject: [PATCH] feat: startup sync/subscribe --- Cargo.lock | 414 +++++++++++++++++++++++++++++++++++----------------- Cargo.toml | 4 +- config.yml | 1 + src/main.rs | 87 +++++++++-- 4 files changed, 354 insertions(+), 152 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f1e93bb..9687787 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -217,6 +217,29 @@ version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d965446196e3b7decd44aa7ee49e31d630118f90ef12f97900f262eb915c951d" +[[package]] +name = "bindgen" +version = "0.69.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "271383c67ccabffb7381723dea0672a673f292304fcb45c01cc648c7a8d58088" +dependencies = [ + "bitflags 2.6.0", + "cexpr", + "clang-sys", + "itertools", + "lazy_static", + "lazycell", + "log", + "prettyplease", + "proc-macro2", + "quote", + "regex", + "rustc-hash", + "shlex", + "syn", + "which", +] + [[package]] name = "bip39" version = "2.1.0" @@ -367,6 +390,15 @@ dependencies = [ "shlex", ] +[[package]] +name = "cexpr" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6fac387a98bb7c37292057cffc56d62ecb629900026402633ae9160df93a8766" +dependencies = [ + "nom", +] + [[package]] name = "cfg-if" version = "1.0.0" @@ -408,6 +440,17 @@ dependencies = [ "zeroize", ] +[[package]] +name = "clang-sys" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b023947811758c97c59bf9d1c188fd619ad4718dcaa767947df1cadb14f39f4" +dependencies = [ + "glob", + "libc", + "libloading", +] + [[package]] name = "clap" version = "4.5.23" @@ -511,21 +554,6 @@ dependencies = [ "libc", ] -[[package]] -name = "crossbeam-queue" -version = "0.3.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0f58bbc28f91df819d0aa2a2c00cd19754769c2fad90579b3592b1c9ba7a3115" -dependencies = [ - "crossbeam-utils", -] - -[[package]] -name = "crossbeam-utils" -version = "0.8.21" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28" - [[package]] name = "crunchy" version = "0.2.2" @@ -580,15 +608,6 @@ dependencies = [ "const-random", ] -[[package]] -name = "doxygen-rs" -version = "0.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "415b6ec780d34dcf624666747194393603d0373b7141eef01d12ee58881507d9" -dependencies = [ - "phf", -] - [[package]] name = "dtan_relay" version = "0.1.0" @@ -638,6 +657,16 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" +[[package]] +name = "errno" +version = "0.3.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33d852cb9b869c2a9b3df2f71a3074817f01e1844f839a144f5fcef059a4eb5d" +dependencies = [ + "libc", + "windows-sys 0.52.0", +] + [[package]] name = "flatbuffers" version = "23.5.26" @@ -671,6 +700,7 @@ checksum = "65bc07b1a8bc7c85c5f2e110c476c7389b4554ba72af57d8445ea63a576b0876" dependencies = [ "futures-channel", "futures-core", + "futures-executor", "futures-io", "futures-sink", "futures-task", @@ -693,12 +723,34 @@ version = "0.3.31" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e" +[[package]] +name = "futures-executor" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e28d1d997f585e54aebc3f97d39e72338912123a67330d723fdbb564d646c9f" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + [[package]] name = "futures-io" version = "0.3.31" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9e5c1b78ca4aae1ac06c48a526a655760685149f0d465d21f37abfe57ce075c6" +[[package]] +name = "futures-macro" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "162ee34ebcb7c64a8abebc059ce0fee27c2262618d7b60ed8faf72fef13c3650" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "futures-sink" version = "0.3.31" @@ -720,6 +772,7 @@ dependencies = [ "futures-channel", "futures-core", "futures-io", + "futures-macro", "futures-sink", "futures-task", "memchr", @@ -757,6 +810,12 @@ version = "0.31.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f" +[[package]] +name = "glob" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" + [[package]] name = "gloo-timers" version = "0.3.0" @@ -799,40 +858,6 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" -[[package]] -name = "heed" -version = "0.20.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7d4f449bab7320c56003d37732a917e18798e2f1709d80263face2b4f9436ddb" -dependencies = [ - "bitflags 2.6.0", - "byteorder", - "heed-traits", - "heed-types", - "libc", - "lmdb-master-sys", - "once_cell", - "page_size", - "synchronoise", - "url", -] - -[[package]] -name = "heed-traits" -version = "0.20.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eb3130048d404c57ce5a1ac61a903696e8fcde7e8c2991e9fcfc1f27c3ef74ff" - -[[package]] -name = "heed-types" -version = "0.20.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d3f528b053a6d700b2734eabcd0fd49cb8230647aa72958467527b0b7917114" -dependencies = [ - "byteorder", - "heed-traits", -] - [[package]] name = "hermit-abi" version = "0.4.0" @@ -869,6 +894,15 @@ dependencies = [ "digest", ] +[[package]] +name = "home" +version = "0.5.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589533453244b0995c858700322199b2becb13b627df2851f64a2775d024abcf" +dependencies = [ + "windows-sys 0.59.0", +] + [[package]] name = "http" version = "1.2.0" @@ -1080,6 +1114,15 @@ version = "1.70.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" +[[package]] +name = "itertools" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba291022dbbd398a455acf126c1e341954079855bc60dfdda641363bd6922569" +dependencies = [ + "either", +] + [[package]] name = "itoa" version = "1.0.14" @@ -1107,29 +1150,46 @@ dependencies = [ "serde", ] +[[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + +[[package]] +name = "lazycell" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55" + [[package]] name = "libc" version = "0.2.169" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a" +[[package]] +name = "libloading" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc2f4eb4bc735547cfed7c0a4922cbd04a4655978c09b54f1f7b228750664c34" +dependencies = [ + "cfg-if", + "windows-targets", +] + +[[package]] +name = "linux-raw-sys" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89" + [[package]] name = "litemap" version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4ee93343901ab17bd981295f2cf0026d4ad018c7c31ba84549a4ddbb47a45104" -[[package]] -name = "lmdb-master-sys" -version = "0.2.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "472c3760e2a8d0f61f322fb36788021bb36d573c502b50fa3e2bcaac3ec326c9" -dependencies = [ - "cc", - "doxygen-rs", - "libc", -] - [[package]] name = "log" version = "0.4.22" @@ -1142,6 +1202,12 @@ version = "2.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" +[[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + [[package]] name = "miniz_oxide" version = "0.8.2" @@ -1174,6 +1240,16 @@ version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "43a88da9dd148bbcdce323dd6ac47d369b4769d4a3b78c6c52389b9269f77932" +[[package]] +name = "nom" +version = "7.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" +dependencies = [ + "memchr", + "minimal-lexical", +] + [[package]] name = "nostr" version = "0.37.0" @@ -1205,21 +1281,19 @@ version = "0.37.0" source = "git+https://github.com/rust-nostr/nostr.git?rev=7351b86c17cb06869a69cb49645d2e9809a9c1a9#7351b86c17cb06869a69cb49645d2e9809a9c1a9" dependencies = [ "async-trait", - "flatbuffers", "nostr", "tokio", ] [[package]] -name = "nostr-lmdb" +name = "nostr-ndb" version = "0.37.0" source = "git+https://github.com/rust-nostr/nostr.git?rev=7351b86c17cb06869a69cb49645d2e9809a9c1a9#7351b86c17cb06869a69cb49645d2e9809a9c1a9" dependencies = [ - "async-utility", - "heed", + "async-trait", "nostr", "nostr-database", - "tokio", + "nostrdb", ] [[package]] @@ -1262,12 +1336,39 @@ dependencies = [ "async-utility", "nostr", "nostr-database", - "nostr-lmdb", + "nostr-ndb", "nostr-relay-pool", "tokio", "tracing", ] +[[package]] +name = "nostrdb" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33673e11ea05bd0b4e601bd85695c03936d02e147044bb06821ada4c2c7b9255" +dependencies = [ + "bindgen", + "cc", + "flatbuffers", + "futures", + "libc", + "thiserror 2.0.9", + "tokio", + "tracing", + "tracing-subscriber", +] + +[[package]] +name = "nu-ansi-term" +version = "0.46.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77a8165726e8236064dbb45459242600304b42a5ea24ee2948e18e023bf7ba84" +dependencies = [ + "overload", + "winapi", +] + [[package]] name = "object" version = "0.36.7" @@ -1300,14 +1401,10 @@ dependencies = [ ] [[package]] -name = "page_size" -version = "0.6.0" +name = "overload" +version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "30d5b2194ed13191c1999ae0704b7839fb18384fa22e49b57eeaa97d79ce40da" -dependencies = [ - "libc", - "winapi", -] +checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" [[package]] name = "password-hash" @@ -1387,48 +1484,6 @@ dependencies = [ "sha2", ] -[[package]] -name = "phf" -version = "0.11.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ade2d8b8f33c7333b51bcf0428d37e217e9f32192ae4772156f65063b8ce03dc" -dependencies = [ - "phf_macros", - "phf_shared", -] - -[[package]] -name = "phf_generator" -version = "0.11.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "48e4cc64c2ad9ebe670cb8fd69dd50ae301650392e81c05f9bfcb2d5bdbc24b0" -dependencies = [ - "phf_shared", - "rand", -] - -[[package]] -name = "phf_macros" -version = "0.11.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3444646e286606587e49f3bcf1679b8cef1dc2c5ecc29ddacaffc305180d464b" -dependencies = [ - "phf_generator", - "phf_shared", - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "phf_shared" -version = "0.11.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90fcb95eef784c2ac79119d1dd819e162b5da872ce6f3c3abe1e8ca1c082f72b" -dependencies = [ - "siphasher", -] - [[package]] name = "pin-project-lite" version = "0.2.15" @@ -1471,6 +1526,16 @@ dependencies = [ "log", ] +[[package]] +name = "prettyplease" +version = "0.2.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64d1ec885c64d0457d564db4ec299b2dae3f9c02808b8ad9c3a089c591b18033" +dependencies = [ + "proc-macro2", + "syn", +] + [[package]] name = "proc-macro2" version = "1.0.92" @@ -1592,6 +1657,12 @@ version = "0.1.24" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" +[[package]] +name = "rustc-hash" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" + [[package]] name = "rustc_version" version = "0.4.1" @@ -1601,6 +1672,19 @@ dependencies = [ "semver", ] +[[package]] +name = "rustix" +version = "0.38.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f93dc38ecbab2eb790ff964bb77fa94faf256fd3e73285fd7ba0903b76bedb85" +dependencies = [ + "bitflags 2.6.0", + "errno", + "libc", + "linux-raw-sys", + "windows-sys 0.52.0", +] + [[package]] name = "rustls" version = "0.23.20" @@ -1750,6 +1834,15 @@ dependencies = [ "digest", ] +[[package]] +name = "sharded-slab" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6" +dependencies = [ + "lazy_static", +] + [[package]] name = "shlex" version = "1.3.0" @@ -1757,10 +1850,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" [[package]] -name = "siphasher" -version = "0.3.11" +name = "signal-hook-registry" +version = "1.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "38b58827f4464d87d377d175e90bf58eb00fd8716ff0a62f80356b5e61555d0d" +checksum = "a9e9e0b4211b72e7b8b6e85c807d36c212bdb33ea8587f7569562a84df5465b1" +dependencies = [ + "libc", +] [[package]] name = "slab" @@ -1822,15 +1918,6 @@ dependencies = [ "unicode-ident", ] -[[package]] -name = "synchronoise" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3dbc01390fc626ce8d1cffe3376ded2b72a11bb70e1c75f404a210e4daa4def2" -dependencies = [ - "crossbeam-queue", -] - [[package]] name = "synstructure" version = "0.13.1" @@ -1891,6 +1978,16 @@ dependencies = [ "syn", ] +[[package]] +name = "thread_local" +version = "1.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b9ef9bad013ada3808854ceac7b46812a6465ba368859a37e2100283d2d719c" +dependencies = [ + "cfg-if", + "once_cell", +] + [[package]] name = "tiny-keccak" version = "2.0.2" @@ -1936,6 +2033,7 @@ dependencies = [ "libc", "mio", "pin-project-lite", + "signal-hook-registry", "socket2", "tokio-macros", "windows-sys 0.52.0", @@ -2053,6 +2151,32 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e672c95779cf947c5311f83787af4fa8fffd12fb27e4993211a84bdfd9610f9c" dependencies = [ "once_cell", + "valuable", +] + +[[package]] +name = "tracing-log" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3" +dependencies = [ + "log", + "once_cell", + "tracing-core", +] + +[[package]] +name = "tracing-subscriber" +version = "0.3.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8189decb5ac0fa7bc8b96b7cb9b2701d60d48805aca84a238004d665fcc4008" +dependencies = [ + "nu-ansi-term", + "sharded-slab", + "smallvec", + "thread_local", + "tracing-core", + "tracing-log", ] [[package]] @@ -2166,6 +2290,12 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" +[[package]] +name = "valuable" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" + [[package]] name = "version_check" version = "0.9.5" @@ -2264,6 +2394,18 @@ dependencies = [ "rustls-pki-types", ] +[[package]] +name = "which" +version = "4.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87ba24419a2078cd2b0f2ede2691b6c66d8e47836da3b6db8265ebad47afbfc7" +dependencies = [ + "either", + "home", + "once_cell", + "rustix", +] + [[package]] name = "winapi" version = "0.3.9" diff --git a/Cargo.toml b/Cargo.toml index 4ee85fb..b14a36e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,12 +4,12 @@ version = "0.1.0" edition = "2021" [dependencies] -nostr-sdk = { git = "https://github.com/rust-nostr/nostr.git", rev = "7351b86c17cb06869a69cb49645d2e9809a9c1a9", package = "nostr-sdk", features = ["lmdb"] } +nostr-sdk = { git = "https://github.com/rust-nostr/nostr.git", rev = "7351b86c17cb06869a69cb49645d2e9809a9c1a9", package = "nostr-sdk", features = ["ndb"] } nostr-relay-builder = { git = "https://github.com/rust-nostr/nostr.git", rev = "7351b86c17cb06869a69cb49645d2e9809a9c1a9", package = "nostr-relay-builder" } log = "0.4.22" config = { version = "0.15.4", features = ["yaml"] } pretty_env_logger = "0.5.0" -tokio = { version = "1.42.0", features = ["rt", "rt-multi-thread", "macros"] } +tokio = { version = "1.42.0", features = ["rt", "rt-multi-thread", "macros", "fs", "signal"] } clap = { version = "4.5.23", features = ["derive"] } anyhow = "1.0.95" serde = { version = "1.0.216", features = ["derive"] } diff --git a/config.yml b/config.yml index a20fc66..91c4da6 100644 --- a/config.yml +++ b/config.yml @@ -6,6 +6,7 @@ relays: - "wss://relay.damus.io" - "wss://nos.lol" - "wss://relay.nostr.band" + - "wss://relay.snort.social" # Listen address of dtan relay relay_listen: "0.0.0.0:8000" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 8134ee5..cde10a4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,16 +1,16 @@ use anyhow::Result; use clap::Parser; use config::Config; -use nostr_relay_builder::builder::{PolicyResult, WritePolicy}; +use log::error; +use nostr_relay_builder::builder::{PolicyResult, QueryPolicy, WritePolicy}; use nostr_relay_builder::prelude::Event; use nostr_relay_builder::{LocalRelay, RelayBuilder}; use nostr_sdk::prelude::async_trait; -use nostr_sdk::{Client, NostrLMDB}; +use nostr_sdk::{Client, Filter, Kind, NdbDatabase, Options, SubscribeOptions, SyncOptions}; use serde::Deserialize; use std::collections::HashSet; use std::net::SocketAddr; use std::path::PathBuf; -use std::sync::Arc; #[derive(Parser, Debug)] #[command(about, version)] @@ -31,13 +31,13 @@ pub struct Settings { pub relay_listen: Option, } -#[derive(Debug)] -struct AcceptKinds(HashSet); +#[derive(Debug, Clone)] +struct AcceptKinds(HashSet); impl AcceptKinds { pub fn from(input: I) -> Self where - I: Into>, + I: Into>, { Self(input.into()) } @@ -46,7 +46,7 @@ impl AcceptKinds { #[async_trait] impl WritePolicy for AcceptKinds { async fn admit_event(&self, event: &Event, _addr: &SocketAddr) -> PolicyResult { - if !self.0.contains(&event.kind.as_u16()) { + if !self.0.contains(&event.kind) { PolicyResult::Reject("kind not accepted".to_string()) } else { PolicyResult::Accept @@ -54,6 +54,22 @@ impl WritePolicy for AcceptKinds { } } +#[async_trait] +impl QueryPolicy for AcceptKinds { + async fn admit_query(&self, query: &[Filter], _addr: &SocketAddr) -> PolicyResult { + if !query.iter().all(|q| { + q.kinds + .as_ref() + .map(|k| k.iter().all(|v| self.0.contains(v))) + .unwrap_or(false) + }) { + PolicyResult::Reject("kind not supported".to_string()) + } else { + PolicyResult::Accept + } + } +} + #[tokio::main] async fn main() -> Result<()> { pretty_env_logger::init(); @@ -67,26 +83,69 @@ async fn main() -> Result<()> { .build()? .try_deserialize()?; - let db = NostrLMDB::open(config.database_dir.unwrap_or(PathBuf::from("./data")))?; - let db = Arc::new(db); + let db = NdbDatabase::open( + config + .database_dir + .unwrap_or(PathBuf::from("./data")) + .to_str() + .unwrap(), + )?; + + const KINDS: [Kind; 4] = [ + Kind::Metadata, + Kind::RelayList, + Kind::Torrent, + Kind::TorrentComment, + ]; + + let client = Client::builder() + .database(db.clone()) + .opts(Options::default()) + .build(); - let client = Client::builder().database(db.clone()).build(); for relay in &config.relays { client.add_relay(relay).await?; } client.connect().await; + client + .pool() + .subscribe( + vec![Filter::new().kinds(KINDS.to_vec())], + SubscribeOptions::default().close_on(None), + ) + .await?; + // re-sync with bootstrap relays + let client_sync = client.clone(); + let sync_kinds = KINDS[1..].to_vec(); + tokio::spawn(async move { + if let Err(e) = client_sync + .sync(Filter::new().kinds(sync_kinds), &SyncOptions::default()) + .await + { + error!("{}", e); + } + }); + + let policy = AcceptKinds::from(KINDS); let addr = config .relay_listen - .unwrap_or(SocketAddr::new([0, 0, 0, 0].into(), 8000)); + .unwrap_or(SocketAddr::new([127, 0, 0, 1].into(), 8000)); let relay = RelayBuilder::default() .database(db.clone()) .addr(addr.ip()) .port(addr.port()) - .write_policy(AcceptKinds::from([0, 2003, 2004])); + .write_policy(policy.clone()) + .query_policy(policy); - LocalRelay::run(relay).await?; + let relay = LocalRelay::run(relay).await?; + + tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())? + .recv() + .await + .expect("failed to listen to shutdown signal"); + + relay.shutdown(); - // start scraper Ok(()) }