Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7c4bf5cc8f | ||
|
|
e2de162931 | ||
|
|
4f606615eb | ||
|
|
84a58ebbcd | ||
|
|
c48e45686d | ||
|
|
bbe359364a | ||
|
|
9e9c494367 | ||
|
|
5fa24bc9f1 | ||
|
|
4de7490d97 | ||
|
|
d0f63dc66e |
Generated
+1
-1
@@ -1095,7 +1095,7 @@ checksum = "38bf9645c8b145698bb0b18a4637dcacbc421ea49bef2317e4fd8065a387cf21"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "nostr-rs-relay"
|
name = "nostr-rs-relay"
|
||||||
version = "0.7.2"
|
version = "0.7.3"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"bitcoin_hashes",
|
"bitcoin_hashes",
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "nostr-rs-relay"
|
name = "nostr-rs-relay"
|
||||||
version = "0.7.2"
|
version = "0.7.3"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
authors = ["Greg Heartsfield <scsibug@imap.cc>"]
|
authors = ["Greg Heartsfield <scsibug@imap.cc>"]
|
||||||
description = "A relay implementation for the Nostr protocol"
|
description = "A relay implementation for the Nostr protocol"
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
# [nostr-rs-relay](https://git.sr.ht/~gheartsfield/nostr-rs-relay)
|
# [nostr-rs-relay](https://git.sr.ht/~gheartsfield/nostr-rs-relay)
|
||||||
|
|
||||||
This is a [nostr](https://github.com/nostr-protocol/nostr) relay, written in
|
This is a [nostr](https://github.com/nostr-protocol/nostr) relay,
|
||||||
Rust. It currently supports the entire relay protocol, and has a
|
written in Rust. It currently supports the entire relay protocol, and
|
||||||
SQLite persistence layer.
|
persists data with SQLite.
|
||||||
|
|
||||||
The project master repository is available on
|
The project master repository is available on
|
||||||
[sourcehut](https://sr.ht/~gheartsfield/nostr-rs-relay/), and is
|
[sourcehut](https://sr.ht/~gheartsfield/nostr-rs-relay/), and is
|
||||||
@@ -37,15 +37,32 @@ application. Use a bind mount to store the SQLite database outside of
|
|||||||
the container image, and map the container's 8080 port to a host port
|
the container image, and map the container's 8080 port to a host port
|
||||||
(7000 in the example below).
|
(7000 in the example below).
|
||||||
|
|
||||||
|
The examples below start a rootless podman container, mapping a local
|
||||||
|
data directory and config file.
|
||||||
|
|
||||||
```console
|
```console
|
||||||
$ docker build -t nostr-rs-relay .
|
$ podman build -t nostr-rs-relay .
|
||||||
|
|
||||||
$ docker run -it -p 7000:8080 \
|
$ mkdir data
|
||||||
--mount src=$(pwd)/data,target=/usr/src/app/db,type=bind nostr-rs-relay
|
|
||||||
|
|
||||||
[2021-12-31T19:58:31Z INFO nostr_rs_relay] listening on: 0.0.0.0:8080
|
$ podman unshare chown 100:100 data
|
||||||
[2021-12-31T19:58:31Z INFO nostr_rs_relay::db] opened database "/usr/src/app/db/nostr.db" for writing
|
|
||||||
[2021-12-31T19:58:31Z INFO nostr_rs_relay::db] DB version = 2
|
$ podman run -it --rm -p 7000:8080 \
|
||||||
|
--user=100:100 \
|
||||||
|
-v $(pwd)/data:/usr/src/app/db:Z \
|
||||||
|
-v $(pwd)/config.toml:/usr/src/app/config.toml:ro,Z \
|
||||||
|
--name nostr-relay nostr-rs-relay:latest
|
||||||
|
|
||||||
|
Nov 19 15:31:15.013 INFO nostr_rs_relay: Starting up from main
|
||||||
|
Nov 19 15:31:15.017 INFO nostr_rs_relay::server: listening on: 0.0.0.0:8080
|
||||||
|
Nov 19 15:31:15.019 INFO nostr_rs_relay::server: db writer created
|
||||||
|
Nov 19 15:31:15.019 INFO nostr_rs_relay::server: control message listener started
|
||||||
|
Nov 19 15:31:15.019 INFO nostr_rs_relay::db: Built a connection pool "event writer" (min=1, max=4)
|
||||||
|
Nov 19 15:31:15.019 INFO nostr_rs_relay::db: opened database "/usr/src/app/db/nostr.db" for writing
|
||||||
|
Nov 19 15:31:15.019 INFO nostr_rs_relay::schema: DB version = 0
|
||||||
|
Nov 19 15:31:15.054 INFO nostr_rs_relay::schema: database pragma/schema initialized to v7, and ready
|
||||||
|
Nov 19 15:31:15.054 INFO nostr_rs_relay::schema: All migration scripts completed successfully. Welcome to v7.
|
||||||
|
Nov 19 15:31:15.521 INFO nostr_rs_relay::db: Built a connection pool "client query" (min=4, max=128)
|
||||||
```
|
```
|
||||||
|
|
||||||
Use a `nostr` client such as
|
Use a `nostr` client such as
|
||||||
|
|||||||
@@ -435,7 +435,7 @@ fn query_from_filter(f: &ReqFilter) -> (String, Vec<Box<dyn ToSql>>) {
|
|||||||
return (empty_query, empty_params);
|
return (empty_query, empty_params);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut query = "SELECT DISTINCT(e.content), e.created_at FROM event e ".to_owned();
|
let mut query = "SELECT DISTINCT(e.content), e.created_at FROM event e".to_owned();
|
||||||
// query parameters for SQLite
|
// query parameters for SQLite
|
||||||
let mut params: Vec<Box<dyn ToSql>> = vec![];
|
let mut params: Vec<Box<dyn ToSql>> = vec![];
|
||||||
|
|
||||||
@@ -471,8 +471,14 @@ fn query_from_filter(f: &ReqFilter) -> (String, Vec<Box<dyn ToSql>>) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if !authvec.is_empty() {
|
||||||
let authors_clause = format!("({})", auth_searches.join(" OR "));
|
let authors_clause = format!("({})", auth_searches.join(" OR "));
|
||||||
filter_components.push(authors_clause);
|
filter_components.push(authors_clause);
|
||||||
|
} else {
|
||||||
|
// if the authors list was empty, we should never return
|
||||||
|
// any results.
|
||||||
|
filter_components.push("false".to_owned());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// Query for Kind
|
// Query for Kind
|
||||||
if let Some(ks) = &f.kinds {
|
if let Some(ks) = &f.kinds {
|
||||||
@@ -505,8 +511,14 @@ fn query_from_filter(f: &ReqFilter) -> (String, Vec<Box<dyn ToSql>>) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if !idvec.is_empty() {
|
||||||
let id_clause = format!("({})", id_searches.join(" OR "));
|
let id_clause = format!("({})", id_searches.join(" OR "));
|
||||||
filter_components.push(id_clause);
|
filter_components.push(id_clause);
|
||||||
|
} else {
|
||||||
|
// if the ids list was empty, we should never return
|
||||||
|
// any results.
|
||||||
|
filter_components.push("false".to_owned());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// Query for tags
|
// Query for tags
|
||||||
if let Some(map) = &f.tags {
|
if let Some(map) = &f.tags {
|
||||||
@@ -582,7 +594,6 @@ fn query_from_sub(sub: &Subscription) -> (String, Vec<Box<dyn ToSql>>) {
|
|||||||
.map(|s| format!("SELECT content, created_at FROM ({})", s))
|
.map(|s| format!("SELECT content, created_at FROM ({})", s))
|
||||||
.collect();
|
.collect();
|
||||||
let query: String = subqueries_selects.join(" UNION ");
|
let query: String = subqueries_selects.join(" UNION ");
|
||||||
trace!("final query string: {}", query);
|
|
||||||
(query, params)
|
(query, params)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -608,6 +619,8 @@ pub async fn db_query(
|
|||||||
trace!("SQL generated in {:?}", start.elapsed());
|
trace!("SQL generated in {:?}", start.elapsed());
|
||||||
// show pool stats
|
// show pool stats
|
||||||
debug!("DB pool stats: {:?}", pool.state());
|
debug!("DB pool stats: {:?}", pool.state());
|
||||||
|
// cutoff for displaying slow queries
|
||||||
|
let slow_cutoff = Duration::from_millis(200);
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
if let Ok(conn) = pool.get() {
|
if let Ok(conn) = pool.get() {
|
||||||
// execute the query. Don't cache, since queries vary so much.
|
// execute the query. Don't cache, since queries vary so much.
|
||||||
@@ -616,11 +629,16 @@ pub async fn db_query(
|
|||||||
let mut first_result = true;
|
let mut first_result = true;
|
||||||
while let Some(row) = event_rows.next()? {
|
while let Some(row) = event_rows.next()? {
|
||||||
if first_result {
|
if first_result {
|
||||||
|
let first_result_elapsed = start.elapsed();
|
||||||
|
// logging for slow queries
|
||||||
|
if first_result_elapsed >= slow_cutoff {
|
||||||
|
info!("final query string (slow): {}", q);
|
||||||
|
} else {
|
||||||
|
trace!("final query string: {}", q);
|
||||||
|
}
|
||||||
debug!(
|
debug!(
|
||||||
"time to first result: {:?} (cid={}, sub={:?})",
|
"time to first result: {:?} (cid={}, sub={:?})",
|
||||||
start.elapsed(),
|
first_result_elapsed, client_id, sub.id
|
||||||
client_id,
|
|
||||||
sub.id
|
|
||||||
);
|
);
|
||||||
first_result = false;
|
first_result = false;
|
||||||
}
|
}
|
||||||
|
|||||||
+51
-3
@@ -20,7 +20,7 @@ pragma mmap_size = 536870912; -- 512MB of mmap
|
|||||||
"##;
|
"##;
|
||||||
|
|
||||||
/// Latest database version
|
/// Latest database version
|
||||||
pub const DB_VERSION: usize = 7;
|
pub const DB_VERSION: usize = 9;
|
||||||
|
|
||||||
/// Schema definition
|
/// Schema definition
|
||||||
const INIT_SQL: &str = formatcp!(
|
const INIT_SQL: &str = formatcp!(
|
||||||
@@ -48,10 +48,10 @@ content TEXT NOT NULL -- serialized json of event object
|
|||||||
|
|
||||||
-- Event Indexes
|
-- Event Indexes
|
||||||
CREATE UNIQUE INDEX IF NOT EXISTS event_hash_index ON event(event_hash);
|
CREATE UNIQUE INDEX IF NOT EXISTS event_hash_index ON event(event_hash);
|
||||||
CREATE INDEX IF NOT EXISTS created_at_index ON event(created_at);
|
|
||||||
CREATE INDEX IF NOT EXISTS author_index ON event(author);
|
CREATE INDEX IF NOT EXISTS author_index ON event(author);
|
||||||
|
CREATE INDEX IF NOT EXISTS created_at_index ON event(created_at);
|
||||||
CREATE INDEX IF NOT EXISTS delegated_by_index ON event(delegated_by);
|
CREATE INDEX IF NOT EXISTS delegated_by_index ON event(delegated_by);
|
||||||
CREATE INDEX IF NOT EXISTS kind_index ON event(kind);
|
CREATE INDEX IF NOT EXISTS event_composite_index ON event(kind,created_at);
|
||||||
|
|
||||||
-- Tag Table
|
-- Tag Table
|
||||||
-- Tag values are stored as either a BLOB (if they come in as a
|
-- Tag values are stored as either a BLOB (if they come in as a
|
||||||
@@ -157,6 +157,13 @@ pub fn upgrade_db(conn: &mut PooledConnection) -> Result<()> {
|
|||||||
if curr_version == 6 {
|
if curr_version == 6 {
|
||||||
curr_version = mig_6_to_7(conn)?;
|
curr_version = mig_6_to_7(conn)?;
|
||||||
}
|
}
|
||||||
|
if curr_version == 7 {
|
||||||
|
curr_version = mig_7_to_8(conn)?;
|
||||||
|
}
|
||||||
|
if curr_version == 8 {
|
||||||
|
curr_version = mig_8_to_9(conn)?;
|
||||||
|
}
|
||||||
|
|
||||||
if curr_version == DB_VERSION {
|
if curr_version == DB_VERSION {
|
||||||
info!(
|
info!(
|
||||||
"All migration scripts completed successfully. Welcome to v{}.",
|
"All migration scripts completed successfully. Welcome to v{}.",
|
||||||
@@ -373,3 +380,44 @@ PRAGMA user_version = 7;
|
|||||||
}
|
}
|
||||||
Ok(7)
|
Ok(7)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn mig_7_to_8(conn: &mut PooledConnection) -> Result<usize> {
|
||||||
|
info!("database schema needs update from 7->8");
|
||||||
|
// Remove redundant indexes, and add a better multi-column index.
|
||||||
|
let upgrade_sql = r##"
|
||||||
|
DROP INDEX IF EXISTS created_at_index;
|
||||||
|
DROP INDEX IF EXISTS kind_index;
|
||||||
|
CREATE INDEX IF NOT EXISTS event_composite_index ON event(kind,created_at);
|
||||||
|
PRAGMA user_version = 8;
|
||||||
|
"##;
|
||||||
|
match conn.execute_batch(upgrade_sql) {
|
||||||
|
Ok(()) => {
|
||||||
|
info!("database schema upgraded v7 -> v8");
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
error!("update failed: {}", err);
|
||||||
|
panic!("database could not be upgraded");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(8)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn mig_8_to_9(conn: &mut PooledConnection) -> Result<usize> {
|
||||||
|
info!("database schema needs update from 8->9");
|
||||||
|
// Those old indexes were actually helpful...
|
||||||
|
let upgrade_sql = r##"
|
||||||
|
CREATE INDEX IF NOT EXISTS created_at_index ON event(created_at);
|
||||||
|
CREATE INDEX IF NOT EXISTS event_composite_index ON event(kind,created_at);
|
||||||
|
PRAGMA user_version = 8;
|
||||||
|
"##;
|
||||||
|
match conn.execute_batch(upgrade_sql) {
|
||||||
|
Ok(()) => {
|
||||||
|
info!("database schema upgraded v8 -> v9");
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
error!("update failed: {}", err);
|
||||||
|
panic!("database could not be upgraded");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(8)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user