From a34516628b0570a7bc844bf7b5a33257527f288f Mon Sep 17 00:00:00 2001 From: thesimplekid Date: Sat, 10 Jun 2023 23:08:48 -0400 Subject: [PATCH 1/8] docs: typo in `build-essential` package name --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 09a66f7..5fbd233 100644 --- a/README.md +++ b/README.md @@ -95,7 +95,7 @@ Building `nostr-rs-relay` requires an installation of Cargo & Rust: https://www. The following OS packages will be helpful; on Debian/Ubuntu: ```console -$ sudo apt-get install build-essentials cmake protobuf-compiler pkg-config libssl-dev +$ sudo apt-get install build-essential cmake protobuf-compiler pkg-config libssl-dev ``` Clone this repository, and then build a release version of the relay: From 2f102719034e2f5858598129b15de1fd9536e4cb Mon Sep 17 00:00:00 2001 From: rorp Date: Tue, 16 May 2023 07:21:21 -0700 Subject: [PATCH 2/8] improvement(NIP-42): use 'restricted:' prefix for auth error msgs --- src/notice.rs | 9 ++++++++- src/server.rs | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/notice.rs b/src/notice.rs index d243acc..6875de9 100644 --- a/src/notice.rs +++ b/src/notice.rs @@ -5,6 +5,7 @@ pub enum EventResultStatus { Blocked, RateLimited, Error, + Restricted, } pub struct EventResult { @@ -24,7 +25,7 @@ impl EventResultStatus { pub fn to_bool(&self) -> bool { match self { Self::Duplicate | Self::Saved => true, - Self::Invalid | Self::Blocked | Self::RateLimited | Self::Error => false, + Self::Invalid | Self::Blocked | Self::RateLimited | Self::Error | Self::Restricted => false, } } @@ -37,6 +38,7 @@ impl EventResultStatus { Self::Blocked => "blocked", Self::RateLimited => "rate-limited", Self::Error => "error", + Self::Restricted => "restricted", } } } @@ -81,6 +83,11 @@ impl Notice { Notice::prefixed(id, msg, EventResultStatus::Error) } + #[must_use] + pub fn restricted(id: String, msg: &str) -> Notice { + Notice::prefixed(id, msg, EventResultStatus::Restricted) + } + #[must_use] pub fn saved(id: String) -> Notice { Notice::EventResult(EventResult { diff --git a/src/server.rs b/src/server.rs index cfc5a5c..91625b1 100644 --- a/src/server.rs +++ b/src/server.rs @@ -1317,7 +1317,7 @@ async fn nostr_server( }, Err(e) => { info!("authentication error: {} (cid: {})", e, cid); - ws_stream.send(make_notice_message(&Notice::message(format!("Authentication error: {e}")))).await.ok(); + ws_stream.send(make_notice_message(&Notice::restricted(event.id, format!("authentication error: {e}").as_str()))).await.ok(); }, } } From 73285683a34fdca88b86338cf0fe30944d10c145 Mon Sep 17 00:00:00 2001 From: Yuval Adam <_@yuv.al> Date: Mon, 8 May 2023 14:36:33 +0300 Subject: [PATCH 3/8] docs: add database maintenance example queries --- docs/database-maintenance.md | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/docs/database-maintenance.md b/docs/database-maintenance.md index 82b39e3..e475918 100644 --- a/docs/database-maintenance.md +++ b/docs/database-maintenance.md @@ -78,18 +78,24 @@ PRAGMA foreign_keys = ON; delete from event where event_hash=x'00000000000c1271675dc86e3e1dd1336827bccabb90dc4c9d3b4465efefe00e'; ``` -### Deleting All Events for Pubkey +### Querying and Deleting All Events for Pubkey ```console PRAGMA foreign_keys = ON; + +select lower(hex(author)) as author, count(*) as c from event group by author order by c asc; + delete from event where author=x'000000000002c7831d9c5a99f183afc2813a6f69a16edda7f6fc0ed8110566e6'; ``` -### Deleting All Events of a Kind +### Querying and Deleting All Events of a Kind ```console PRAGMA foreign_keys = ON; + +select printf('%7d', kind), count(*) as c from event group by kind order by c; + delete from event where kind=70202; ``` From f4beb884b3f181b7544ee9980fb9ffdc17fe7218 Mon Sep 17 00:00:00 2001 From: Jamin M Date: Fri, 21 Apr 2023 08:32:53 -0700 Subject: [PATCH 4/8] feat: allow logging output to file --- Cargo.lock | 118 +++++++++++++++++++++++++++----------------------- Cargo.toml | 5 ++- config.toml | 3 ++ src/config.rs | 12 +++++ src/main.rs | 26 ++++++++++- 5 files changed, 106 insertions(+), 58 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9533514..e017cb9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -54,15 +54,6 @@ dependencies = [ "libc", ] -[[package]] -name = "ansi_term" -version = "0.12.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2" -dependencies = [ - "winapi", -] - [[package]] name = "anyhow" version = "1.0.69" @@ -420,7 +411,7 @@ dependencies = [ "js-sys", "num-integer", "num-traits", - "time", + "time 0.1.45", "wasm-bindgen", "winapi", ] @@ -565,7 +556,7 @@ dependencies = [ "tonic", "tracing", "tracing-core", - "tracing-subscriber 0.3.16", + "tracing-subscriber", ] [[package]] @@ -1511,15 +1502,6 @@ dependencies = [ "libc", ] -[[package]] -name = "matchers" -version = "0.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f099785f7595cc4b4553a174ce30dd7589ef93391ff414dbb67f62392b9e0ce1" -dependencies = [ - "regex-automata", -] - [[package]] name = "matchers" version = "0.1.0" @@ -1695,12 +1677,23 @@ dependencies = [ "tonic", "tonic-build", "tracing", - "tracing-subscriber 0.2.25", + "tracing-appender", + "tracing-subscriber", "tungstenite", "url", "uuid", ] +[[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 = "num" version = "0.2.1" @@ -1861,6 +1854,12 @@ version = "6.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b7820b9daea5457c9f21c69448905d723fbd21136ccf521748f23fd49e723ee" +[[package]] +name = "overload" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" + [[package]] name = "parking" version = "2.0.0" @@ -2990,6 +2989,33 @@ dependencies = [ "winapi", ] +[[package]] +name = "time" +version = "0.3.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd0cbfecb4d19b5ea75bb31ad904eb5b9fa13f21079c3b92017ebdf4999a5890" +dependencies = [ + "itoa", + "serde", + "time-core", + "time-macros", +] + +[[package]] +name = "time-core" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e153e1f1acaef8acc537e68b44906d2db6436e2b35ac2c6b42640fff91f00fd" + +[[package]] +name = "time-macros" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd80a657e71da814b8e5d60d3374fc6d35045062245d80224748ae522dd76f36" +dependencies = [ + "time-core", +] + [[package]] name = "tinyvec" version = "1.6.0" @@ -3223,6 +3249,17 @@ dependencies = [ "tracing-core", ] +[[package]] +name = "tracing-appender" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09d48f71a791638519505cefafe162606f706c25592e4bde4d97600c0195312e" +dependencies = [ + "crossbeam-channel", + "time 0.3.20", + "tracing-subscriber", +] + [[package]] name = "tracing-attributes" version = "0.1.23" @@ -3265,51 +3302,22 @@ dependencies = [ "tracing-core", ] -[[package]] -name = "tracing-serde" -version = "0.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bc6b213177105856957181934e4920de57730fc69bf42c37ee5bb664d406d9e1" -dependencies = [ - "serde", - "tracing-core", -] - [[package]] name = "tracing-subscriber" -version = "0.2.25" +version = "0.3.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0e0d2eaa99c3c2e41547cfa109e910a68ea03823cccad4a0525dcbc9b01e8c71" +checksum = "a6176eae26dd70d0c919749377897b54a9276bd7061339665dd68777926b5a70" dependencies = [ - "ansi_term", - "chrono", - "lazy_static", - "matchers 0.0.1", + "matchers", + "nu-ansi-term", + "once_cell", "regex", - "serde", - "serde_json", "sharded-slab", "smallvec", "thread_local", "tracing", "tracing-core", "tracing-log", - "tracing-serde", -] - -[[package]] -name = "tracing-subscriber" -version = "0.3.16" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a6176eae26dd70d0c919749377897b54a9276bd7061339665dd68777926b5a70" -dependencies = [ - "matchers 0.1.0", - "once_cell", - "regex", - "sharded-slab", - "thread_local", - "tracing", - "tracing-core", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index b04ef07..ce43d2b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,8 +13,9 @@ categories = ["network-programming", "web-programming"] [dependencies] clap = { version = "4.0.32", features = ["env", "default", "derive"]} -tracing = "0.1.36" -tracing-subscriber = "0.2.0" +tracing = "0.1.37" +tracing-appender = "0.2.2" +tracing-subscriber = "0.3.16" tokio = { version = "1", features = ["full", "tracing", "signal"] } prost = "0.11" tonic = "0.8.3" diff --git a/config.toml b/config.toml index 3dea8ee..8738064 100644 --- a/config.toml +++ b/config.toml @@ -57,6 +57,9 @@ description = "A newly created nostr-rs-relay.\n\nCustomize this with your own i # different nodes. Ignore for single-database instances. #connection_write = "postgresql://postgres:nostr@localhost:7500/nostr" +[logging] +folder_path = "./log" + [grpc] # gRPC interfaces for externalized decisions and other extensions to # functionality. diff --git a/src/config.rs b/src/config.rs index 32ac2d3..9d92d39 100644 --- a/src/config.rs +++ b/src/config.rs @@ -166,6 +166,13 @@ impl VerifiedUsers { } } +#[derive(Debug, Clone, Serialize, Deserialize)] +#[allow(unused)] +pub struct Logging { + pub folder_path: Option, + pub file_prefix: Option, +} + #[derive(Debug, Clone, Serialize, Deserialize)] #[allow(unused)] pub struct Settings { @@ -180,6 +187,7 @@ pub struct Settings { pub verified_users: VerifiedUsers, pub retention: Retention, pub options: Options, + pub logging: Logging, } impl Settings { @@ -320,6 +328,10 @@ impl Default for Settings { options: Options { reject_future_seconds: None, // Reject events in the future if defined }, + logging: Logging { + folder_path: None, + file_prefix: None, + }, } } } diff --git a/src/main.rs b/src/main.rs index 6a9116d..5e8a257 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,8 @@ use console_subscriber::ConsoleLayer; use nostr_rs_relay::cli::CLIArgs; use nostr_rs_relay::config; use nostr_rs_relay::server::start_server; +use tracing_appender::non_blocking::WorkerGuard; +use tracing_subscriber::EnvFilter; use std::sync::mpsc as syncmpsc; use std::sync::mpsc::{Receiver as MpscReceiver, Sender as MpscSender}; use std::thread; @@ -22,6 +24,8 @@ fn main() { // get config file name from args let config_file_arg = args.config; + let mut _log_guard: Option = None; + // configure settings from the config file (defaults to config.toml) // replace default settings with those read from the config file let mut settings = config::Settings::new(&config_file_arg); @@ -32,7 +36,27 @@ fn main() { ConsoleLayer::builder().with_default_env().init(); } else { // standard logging - tracing_subscriber::fmt::try_init().unwrap(); + if let Some(path) = &settings.logging.folder_path { + // write logs to a folder + let prefix = match &settings.logging.file_prefix { + Some(p) => p.as_str(), + None => "relay", + }; + let file_appender = tracing_appender::rolling::hourly(path, prefix); + let (non_blocking, guard) = tracing_appender::non_blocking(file_appender); + let filter = EnvFilter::from_default_env(); + // assign to a variable that is not dropped till the program ends + _log_guard = Some(guard); + + tracing_subscriber::fmt() + .with_env_filter(filter) + .with_writer(non_blocking) + .try_init() + .unwrap(); + } else { + // write to stdout + tracing_subscriber::fmt::try_init().unwrap(); + } } info!("Starting up from main"); From 9237eed73558c36d753e5e87238089379e3bec68 Mon Sep 17 00:00:00 2001 From: Jamin M Date: Mon, 1 May 2023 21:46:33 -0700 Subject: [PATCH 5/8] feat: roll over logs daily --- src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 5e8a257..98497a8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -42,7 +42,7 @@ fn main() { Some(p) => p.as_str(), None => "relay", }; - let file_appender = tracing_appender::rolling::hourly(path, prefix); + let file_appender = tracing_appender::rolling::daily(path, prefix); let (non_blocking, guard) = tracing_appender::non_blocking(file_appender); let filter = EnvFilter::from_default_env(); // assign to a variable that is not dropped till the program ends From 7a72e588ea0e58bc064b4b7a9312b6a96436c09c Mon Sep 17 00:00:00 2001 From: Greg Heartsfield Date: Fri, 23 Jun 2023 10:02:04 -0500 Subject: [PATCH 6/8] refactor: reorder imports --- src/main.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 98497a8..6e8994e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,14 +4,14 @@ use console_subscriber::ConsoleLayer; use nostr_rs_relay::cli::CLIArgs; use nostr_rs_relay::config; use nostr_rs_relay::server::start_server; -use tracing_appender::non_blocking::WorkerGuard; -use tracing_subscriber::EnvFilter; use std::sync::mpsc as syncmpsc; use std::sync::mpsc::{Receiver as MpscReceiver, Sender as MpscSender}; use std::thread; -use tracing::info; #[cfg(not(target_env = "msvc"))] use tikv_jemallocator::Jemalloc; +use tracing::info; +use tracing_appender::non_blocking::WorkerGuard; +use tracing_subscriber::EnvFilter; #[cfg(not(target_env = "msvc"))] #[global_allocator] @@ -47,7 +47,7 @@ fn main() { let filter = EnvFilter::from_default_env(); // assign to a variable that is not dropped till the program ends _log_guard = Some(guard); - + tracing_subscriber::fmt() .with_env_filter(filter) .with_writer(non_blocking) From 46474766222f016783364735439ef1cd0a91eefb Mon Sep 17 00:00:00 2001 From: Greg Heartsfield Date: Fri, 23 Jun 2023 10:34:25 -0500 Subject: [PATCH 7/8] improvement: default to logging on stdout --- config.toml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/config.toml b/config.toml index 8738064..e6db8d5 100644 --- a/config.toml +++ b/config.toml @@ -58,7 +58,9 @@ description = "A newly created nostr-rs-relay.\n\nCustomize this with your own i #connection_write = "postgresql://postgres:nostr@localhost:7500/nostr" [logging] -folder_path = "./log" +# Directory to store log files. Log files roll over daily. +#folder_path = "./log" +#file_prefix = "nostr-relay" [grpc] # gRPC interfaces for externalized decisions and other extensions to From 087b68128fb1b45161d17484aa16ebbc970159f6 Mon Sep 17 00:00:00 2001 From: Greg Heartsfield Date: Fri, 23 Jun 2023 10:38:06 -0500 Subject: [PATCH 8/8] fix: ensure startup SQL runs, even with zero min writers --- src/repo/sqlite.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/repo/sqlite.rs b/src/repo/sqlite.rs index b156f0c..3ad59bd 100644 --- a/src/repo/sqlite.rs +++ b/src/repo/sqlite.rs @@ -1203,6 +1203,11 @@ pub fn build_pool( .max_lifetime(Some(Duration::from_secs(30))) .build(manager) .unwrap(); + // retrieve a connection to ensure the startup statements run immediately + { + let _ = pool.get(); + } + info!( "Built a connection pool {:?} (min={}, max={})", name, min_size, max_size