Compare commits
116
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6a8c4ed1b5 | ||
|
|
966c853700 | ||
|
|
65fd0ed08b | ||
|
|
0b51675b38 | ||
|
|
2e22334631 | ||
|
|
cb2ac4bf0f | ||
|
|
38dc7789dc | ||
|
|
ce0e00ffb3 | ||
|
|
3e4ae4aeec | ||
|
|
c6a8807485 | ||
|
|
8137b6211c | ||
|
|
29effaae23 | ||
|
|
e5074f2e46 | ||
|
|
4fd7643907 | ||
|
|
1e1ec69175 | ||
|
|
e08647867c | ||
|
|
ae0f7171ed | ||
|
|
4f1a912f36 | ||
|
|
95748647f0 | ||
|
|
25480e837f | ||
|
|
b80b54cd9d | ||
|
|
8ea732cbe5 | ||
|
|
0f68c4e5c2 | ||
|
|
dab2cd5792 | ||
|
|
f411aa6fc2 | ||
|
|
d31bbda087 | ||
|
|
5917bc53b2 | ||
|
|
91177c61a1 | ||
|
|
53c2a8051c | ||
|
|
168cf513ac | ||
|
|
ea204761c9 | ||
|
|
c270ae1434 | ||
|
|
64bd983cb6 | ||
|
|
1c153bc784 | ||
|
|
dc11d9a619 | ||
|
|
cd1557787b | ||
|
|
86bb7aeb9a | ||
|
|
ce37fc1a2d | ||
|
|
2cfd384339 | ||
|
|
8c013107f9 | ||
|
|
64a4466d30 | ||
|
|
1596c23eb4 | ||
|
|
129badd4e1 | ||
|
|
6f7c080180 | ||
|
|
af92561ef6 | ||
|
|
d833a3e40d | ||
|
|
462eb46642 | ||
|
|
cf144d503d | ||
|
|
fb8375aef2 | ||
|
|
88ac31b549 | ||
|
|
677b7d39e9 | ||
|
|
b24d2f9aaa | ||
|
|
7a3899d852 | ||
|
|
818108b793 | ||
|
|
d10348f7e1 | ||
|
|
8598e443d8 | ||
|
|
43222d44e5 | ||
|
|
7c1516c4fb | ||
|
|
0c72053a49 | ||
|
|
3f32ff67ab | ||
|
|
0b9778d6ca | ||
|
|
9be04120c7 | ||
|
|
cc06167e06 | ||
|
|
b6e33f044f | ||
|
|
1b2c6f9fca | ||
|
|
0d8d39ad22 | ||
|
|
0e851d4f71 | ||
|
|
3c880b2f49 | ||
|
|
7a4c9266ec | ||
|
|
e8557d421b | ||
|
|
7ca9c864f2 | ||
|
|
838aafd079 | ||
|
|
e554b10ac2 | ||
|
|
b0bfaa48fc | ||
|
|
2e9b1b6ba7 | ||
|
|
4d9012d94c | ||
|
|
ffe7aac066 | ||
|
|
f9695bd0a9 | ||
|
|
7c4bf5cc8f | ||
|
|
e2de162931 | ||
|
|
4f606615eb | ||
|
|
84a58ebbcd | ||
|
|
c48e45686d | ||
|
|
bbe359364a | ||
|
|
9e9c494367 | ||
|
|
5fa24bc9f1 | ||
|
|
4de7490d97 | ||
|
|
d0f63dc66e | ||
|
|
06078648c8 | ||
|
|
cc0fcc5d66 | ||
|
|
dfb2096653 | ||
|
|
486508d192 | ||
|
|
84b43c144b | ||
|
|
110500bb46 | ||
|
|
83f6b11de7 | ||
|
|
6d1244434b | ||
|
|
5a91419d34 | ||
|
|
7adc5c9af7 | ||
|
|
9dd4571bee | ||
|
|
9db5a26b9c | ||
|
|
ac345b5744 | ||
|
|
675662c7fb | ||
|
|
505b0cb71f | ||
|
|
e8aa450802 | ||
|
|
5a8860bb09 | ||
|
|
11e43eccf9 | ||
|
|
50577b2dfa | ||
|
|
a6cb6f8486 | ||
|
|
ae5bf98d87 | ||
|
|
1cf9d719f0 | ||
|
|
311f4b5283 | ||
|
|
14b5a51e3a | ||
|
|
8ecce3f566 | ||
|
|
caffbbbede | ||
|
|
81045ad3d0 | ||
|
|
72f8a1aa5c |
@@ -11,6 +11,6 @@ repos:
|
|||||||
- repo: https://github.com/doublify/pre-commit-rust
|
- repo: https://github.com/doublify/pre-commit-rust
|
||||||
rev: v1.0
|
rev: v1.0
|
||||||
hooks:
|
hooks:
|
||||||
- id: fmt
|
# - id: fmt
|
||||||
- id: cargo-check
|
- id: cargo-check
|
||||||
- id: clippy
|
- id: clippy
|
||||||
|
|||||||
Generated
+374
-196
File diff suppressed because it is too large
Load Diff
+11
-2
@@ -1,7 +1,15 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "nostr-rs-relay"
|
name = "nostr-rs-relay"
|
||||||
version = "0.6.2"
|
version = "0.7.14"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
authors = ["Greg Heartsfield <scsibug@imap.cc>"]
|
||||||
|
description = "A relay implementation for the Nostr protocol"
|
||||||
|
readme = "README.md"
|
||||||
|
homepage = "https://sr.ht/~gheartsfield/nostr-rs-relay/"
|
||||||
|
repository = "https://git.sr.ht/~gheartsfield/nostr-rs-relay"
|
||||||
|
license = "MIT"
|
||||||
|
keywords = ["nostr", "server"]
|
||||||
|
categories = ["network-programming", "web-programming"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
tracing = "0.1.36"
|
tracing = "0.1.36"
|
||||||
@@ -20,7 +28,7 @@ secp256k1 = {version = "0.21", features = ["rand", "rand-std", "serde", "bitcoin
|
|||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
serde_json = {version = "1.0", features = ["preserve_order"]}
|
serde_json = {version = "1.0", features = ["preserve_order"]}
|
||||||
hex = "0.4"
|
hex = "0.4"
|
||||||
rusqlite = { version = "0.26", features = ["limits","bundled"]}
|
rusqlite = { version = "0.26", features = ["limits","bundled","modern_sqlite", "trace"]}
|
||||||
r2d2 = "0.8"
|
r2d2 = "0.8"
|
||||||
r2d2_sqlite = "0.19"
|
r2d2_sqlite = "0.19"
|
||||||
lazy_static = "1.4"
|
lazy_static = "1.4"
|
||||||
@@ -32,6 +40,7 @@ http = { version = "0.2" }
|
|||||||
parse_duration = "2"
|
parse_duration = "2"
|
||||||
rand = "0.8"
|
rand = "0.8"
|
||||||
const_format = "0.2.28"
|
const_format = "0.2.28"
|
||||||
|
regex = "1"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
anyhow = "1"
|
anyhow = "1"
|
||||||
|
|||||||
+10
-4
@@ -1,18 +1,24 @@
|
|||||||
FROM docker.io/library/rust:1.64.0@sha256:5cf09a76cb9baf4990d121221bbad64927cc5690ee54f246487e302ddc2ba300 as builder
|
FROM docker.io/library/rust:1.66.0 as builder
|
||||||
|
|
||||||
|
RUN USER=root cargo install cargo-auditable
|
||||||
RUN USER=root cargo new --bin nostr-rs-relay
|
RUN USER=root cargo new --bin nostr-rs-relay
|
||||||
WORKDIR ./nostr-rs-relay
|
WORKDIR ./nostr-rs-relay
|
||||||
COPY ./Cargo.toml ./Cargo.toml
|
COPY ./Cargo.toml ./Cargo.toml
|
||||||
COPY ./Cargo.lock ./Cargo.lock
|
COPY ./Cargo.lock ./Cargo.lock
|
||||||
RUN cargo build --release
|
# build dependencies only (caching)
|
||||||
|
RUN cargo auditable build --release --locked
|
||||||
|
# get rid of starter project code
|
||||||
RUN rm src/*.rs
|
RUN rm src/*.rs
|
||||||
|
|
||||||
|
# copy project source code
|
||||||
COPY ./src ./src
|
COPY ./src ./src
|
||||||
|
|
||||||
|
# build auditable release using locked deps
|
||||||
RUN rm ./target/release/deps/nostr*relay*
|
RUN rm ./target/release/deps/nostr*relay*
|
||||||
RUN cargo build --release
|
RUN cargo auditable build --release --locked
|
||||||
|
|
||||||
|
FROM docker.io/library/debian:bullseye-slim
|
||||||
|
|
||||||
FROM docker.io/library/debian:bullseye-20221004-slim@sha256:8b702518a671c926b5ece4efe386a476eb4777646a36d996d4bd50944f2f11a2
|
|
||||||
ARG APP=/usr/src/app
|
ARG APP=/usr/src/app
|
||||||
ARG APP_DATA=/usr/src/app/db
|
ARG APP_DATA=/usr/src/app/db
|
||||||
RUN apt-get update \
|
RUN apt-get update \
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -26,7 +26,10 @@ mirrored on [GitHub](https://github.com/scsibug/nostr-rs-relay).
|
|||||||
- [x] NIP-12: [Generic Tag Queries](https://github.com/nostr-protocol/nips/blob/master/12.md)
|
- [x] NIP-12: [Generic Tag Queries](https://github.com/nostr-protocol/nips/blob/master/12.md)
|
||||||
- [x] NIP-15: [End of Stored Events Notice](https://github.com/nostr-protocol/nips/blob/master/15.md)
|
- [x] NIP-15: [End of Stored Events Notice](https://github.com/nostr-protocol/nips/blob/master/15.md)
|
||||||
- [x] NIP-16: [Event Treatment](https://github.com/nostr-protocol/nips/blob/master/16.md)
|
- [x] NIP-16: [Event Treatment](https://github.com/nostr-protocol/nips/blob/master/16.md)
|
||||||
|
- [x] NIP-20: [Command Results](https://github.com/nostr-protocol/nips/blob/master/20.md)
|
||||||
- [x] NIP-22: [Event `created_at` limits](https://github.com/nostr-protocol/nips/blob/master/22.md) (_future-dated events only_)
|
- [x] NIP-22: [Event `created_at` limits](https://github.com/nostr-protocol/nips/blob/master/22.md) (_future-dated events only_)
|
||||||
|
- [x] NIP-26: [Event Delegation](https://github.com/nostr-protocol/nips/blob/master/26.md)
|
||||||
|
- [x] NIP-28: [Public Chat](https://github.com/nostr-protocol/nips/blob/master/28.md)
|
||||||
|
|
||||||
## Quick Start
|
## Quick Start
|
||||||
|
|
||||||
@@ -35,15 +38,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
|
||||||
@@ -62,6 +82,38 @@ Text Note [81cf...2652] from 296a...9b92 5 seconds ago
|
|||||||
A pre-built container is also available on DockerHub:
|
A pre-built container is also available on DockerHub:
|
||||||
https://hub.docker.com/r/scsibug/nostr-rs-relay
|
https://hub.docker.com/r/scsibug/nostr-rs-relay
|
||||||
|
|
||||||
|
## Build and Run (without Docker)
|
||||||
|
|
||||||
|
Building `nostr-rs-relay` requires an installation of Cargo & Rust: https://www.rust-lang.org/tools/install
|
||||||
|
|
||||||
|
Clone this repository, and then build a release version of the relay:
|
||||||
|
|
||||||
|
```console
|
||||||
|
$ git clone -q https://git.sr.ht/\~gheartsfield/nostr-rs-relay
|
||||||
|
$ cd nostr-rs-relay
|
||||||
|
$ cargo build -q -r
|
||||||
|
```
|
||||||
|
|
||||||
|
The relay executable is now located in
|
||||||
|
`target/release/nostr-rs-relay`. In order to run it with logging
|
||||||
|
enabled, execute it with the `RUST_LOG` variable set:
|
||||||
|
|
||||||
|
```console
|
||||||
|
$ RUST_LOG=warn,nostr_rs_relay=info ./target/release/nostr-rs-relay
|
||||||
|
Dec 26 10:31:56.455 INFO nostr_rs_relay: Starting up from main
|
||||||
|
Dec 26 10:31:56.464 INFO nostr_rs_relay::server: listening on: 0.0.0.0:8080
|
||||||
|
Dec 26 10:31:56.466 INFO nostr_rs_relay::server: db writer created
|
||||||
|
Dec 26 10:31:56.466 INFO nostr_rs_relay::db: Built a connection pool "event writer" (min=1, max=2)
|
||||||
|
Dec 26 10:31:56.466 INFO nostr_rs_relay::db: opened database "./nostr.db" for writing
|
||||||
|
Dec 26 10:31:56.466 INFO nostr_rs_relay::schema: DB version = 11
|
||||||
|
Dec 26 10:31:56.467 INFO nostr_rs_relay::db: Built a connection pool "maintenance writer" (min=1, max=2)
|
||||||
|
Dec 26 10:31:56.467 INFO nostr_rs_relay::server: control message listener started
|
||||||
|
Dec 26 10:31:56.468 INFO nostr_rs_relay::db: Built a connection pool "client query" (min=4, max=8)
|
||||||
|
```
|
||||||
|
|
||||||
|
You now have a running relay, on port `8080`. Use a `nostr` client or
|
||||||
|
`websocat` to connect and send/query for events.
|
||||||
|
|
||||||
## Configuration
|
## Configuration
|
||||||
|
|
||||||
The sample [`config.toml`](config.toml) file demonstrates the
|
The sample [`config.toml`](config.toml) file demonstrates the
|
||||||
|
|||||||
+29
-4
@@ -36,8 +36,9 @@ data_directory = "."
|
|||||||
# Minimum number of SQLite reader connections
|
# Minimum number of SQLite reader connections
|
||||||
#min_conn = 4
|
#min_conn = 4
|
||||||
|
|
||||||
# Maximum number of SQLite reader connections
|
# Maximum number of SQLite reader connections. Recommend setting this
|
||||||
#max_conn = 128
|
# to approx the number of cores.
|
||||||
|
#max_conn = 8
|
||||||
|
|
||||||
[network]
|
[network]
|
||||||
# Bind to this network address
|
# Bind to this network address
|
||||||
@@ -46,6 +47,14 @@ address = "0.0.0.0"
|
|||||||
# Listen on this port
|
# Listen on this port
|
||||||
port = 8080
|
port = 8080
|
||||||
|
|
||||||
|
# If present, read this HTTP header for logging client IP addresses.
|
||||||
|
# Examples for common proxies, cloudflare:
|
||||||
|
#remote_ip_header = "x-forwarded-for"
|
||||||
|
#remote_ip_header = "cf-connecting-ip"
|
||||||
|
|
||||||
|
# Websocket ping interval in seconds, defaults to 5 minutes
|
||||||
|
#ping_interval = 300
|
||||||
|
|
||||||
[options]
|
[options]
|
||||||
# Reject events that have timestamps greater than this many seconds in
|
# Reject events that have timestamps greater than this many seconds in
|
||||||
# the future. Recommended to reject anything greater than 30 minutes
|
# the future. Recommended to reject anything greater than 30 minutes
|
||||||
@@ -54,8 +63,24 @@ reject_future_seconds = 1800
|
|||||||
|
|
||||||
[limits]
|
[limits]
|
||||||
# Limit events created per second, averaged over one minute. Must be
|
# Limit events created per second, averaged over one minute. Must be
|
||||||
# an integer. If not set (or set to 0), defaults to unlimited.
|
# an integer. If not set (or set to 0), defaults to unlimited. Note:
|
||||||
#messages_per_sec = 0
|
# this is for the server as a whole, not per-connection.
|
||||||
|
# messages_per_sec = 0
|
||||||
|
|
||||||
|
# Limit client subscriptions created per second, averaged over one
|
||||||
|
# minute. Must be an integer. If not set (or set to 0), defaults to
|
||||||
|
# unlimited.
|
||||||
|
#subscriptions_per_min = 0
|
||||||
|
|
||||||
|
# UNIMPLEMENTED...
|
||||||
|
# Limit how many concurrent database connections a client can have.
|
||||||
|
# This prevents a single client from starting too many expensive
|
||||||
|
# database queries. Must be an integer. If not set (or set to 0),
|
||||||
|
# defaults to unlimited (subject to subscription limits).
|
||||||
|
#db_conns_per_client = 0
|
||||||
|
|
||||||
|
# Limit blocking threads used for database connections. Defaults to 16.
|
||||||
|
#max_blocking_threads = 16
|
||||||
|
|
||||||
# Limit the maximum size of an EVENT message. Defaults to 128 KB.
|
# Limit the maximum size of an EVENT message. Defaults to 128 KB.
|
||||||
# Set to 0 for unlimited.
|
# Set to 0 for unlimited.
|
||||||
|
|||||||
@@ -1,3 +0,0 @@
|
|||||||
#!/usr/bin/env bash
|
|
||||||
sed -E 's/@sha256:[[:alnum:]]+//g' Dockerfile > Dockerfile.any-platform
|
|
||||||
echo "Created platform-agnostic Dockerfile in 'Dockerfile.any-platform'"
|
|
||||||
@@ -1 +1,4 @@
|
|||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
#max_width = 140
|
||||||
|
#chain_width = 100
|
||||||
|
#fn_call_width = 100
|
||||||
|
|||||||
+11
-2
@@ -28,9 +28,10 @@ pub struct Database {
|
|||||||
pub struct Network {
|
pub struct Network {
|
||||||
pub port: u16,
|
pub port: u16,
|
||||||
pub address: String,
|
pub address: String,
|
||||||
|
pub remote_ip_header: Option<String>, // retrieve client IP from this HTTP header if present
|
||||||
|
pub ping_interval_seconds: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
#[allow(unused)]
|
#[allow(unused)]
|
||||||
pub struct Options {
|
pub struct Options {
|
||||||
@@ -51,6 +52,9 @@ pub struct Retention {
|
|||||||
#[allow(unused)]
|
#[allow(unused)]
|
||||||
pub struct Limits {
|
pub struct Limits {
|
||||||
pub messages_per_sec: Option<u32>, // Artificially slow down event writing to limit disk consumption (averaged over 1 minute)
|
pub messages_per_sec: Option<u32>, // Artificially slow down event writing to limit disk consumption (averaged over 1 minute)
|
||||||
|
pub subscriptions_per_min: Option<u32>, // Artificially slow down request (db query) creation to prevent abuse (averaged over 1 minute)
|
||||||
|
pub db_conns_per_client: Option<u32>, // How many concurrent database queries (not subscriptions) may a client have?
|
||||||
|
pub max_blocking_threads: usize,
|
||||||
pub max_event_bytes: Option<usize>, // Maximum size of an EVENT message
|
pub max_event_bytes: Option<usize>, // Maximum size of an EVENT message
|
||||||
pub max_ws_message_bytes: Option<usize>,
|
pub max_ws_message_bytes: Option<usize>,
|
||||||
pub max_ws_frame_bytes: Option<usize>,
|
pub max_ws_frame_bytes: Option<usize>,
|
||||||
@@ -203,14 +207,19 @@ impl Default for Settings {
|
|||||||
data_directory: ".".to_owned(),
|
data_directory: ".".to_owned(),
|
||||||
in_memory: false,
|
in_memory: false,
|
||||||
min_conn: 4,
|
min_conn: 4,
|
||||||
max_conn: 128,
|
max_conn: 8,
|
||||||
},
|
},
|
||||||
network: Network {
|
network: Network {
|
||||||
port: 8080,
|
port: 8080,
|
||||||
|
ping_interval_seconds: 300,
|
||||||
address: "0.0.0.0".to_owned(),
|
address: "0.0.0.0".to_owned(),
|
||||||
|
remote_ip_header: None,
|
||||||
},
|
},
|
||||||
limits: Limits {
|
limits: Limits {
|
||||||
messages_per_sec: None,
|
messages_per_sec: None,
|
||||||
|
subscriptions_per_min: None,
|
||||||
|
db_conns_per_client: None,
|
||||||
|
max_blocking_threads: 16,
|
||||||
max_event_bytes: Some(2 << 17), // 128K
|
max_event_bytes: Some(2 << 17), // 128K
|
||||||
max_ws_message_bytes: Some(2 << 17), // 128K
|
max_ws_message_bytes: Some(2 << 17), // 128K
|
||||||
max_ws_frame_bytes: Some(2 << 17), // 128K
|
max_ws_frame_bytes: Some(2 << 17), // 128K
|
||||||
|
|||||||
+31
-22
@@ -2,11 +2,10 @@
|
|||||||
use crate::close::Close;
|
use crate::close::Close;
|
||||||
use crate::error::Error;
|
use crate::error::Error;
|
||||||
use crate::error::Result;
|
use crate::error::Result;
|
||||||
use crate::event::Event;
|
|
||||||
|
|
||||||
use crate::subscription::Subscription;
|
use crate::subscription::Subscription;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use tracing::{debug, info};
|
use tracing::{debug, trace};
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
/// A subscription identifier has a maximum length
|
/// A subscription identifier has a maximum length
|
||||||
@@ -14,6 +13,8 @@ const MAX_SUBSCRIPTION_ID_LEN: usize = 256;
|
|||||||
|
|
||||||
/// State for a client connection
|
/// State for a client connection
|
||||||
pub struct ClientConn {
|
pub struct ClientConn {
|
||||||
|
/// Client IP (either from socket, or configured proxy header
|
||||||
|
client_ip: String,
|
||||||
/// Unique client identifier generated at connection time
|
/// Unique client identifier generated at connection time
|
||||||
client_id: Uuid,
|
client_id: Uuid,
|
||||||
/// The current set of active client subscriptions
|
/// The current set of active client subscriptions
|
||||||
@@ -24,22 +25,32 @@ pub struct ClientConn {
|
|||||||
|
|
||||||
impl Default for ClientConn {
|
impl Default for ClientConn {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self::new()
|
Self::new("unknown".to_owned())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ClientConn {
|
impl ClientConn {
|
||||||
/// Create a new, empty connection state.
|
/// Create a new, empty connection state.
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn new() -> Self {
|
pub fn new(client_ip: String) -> Self {
|
||||||
let client_id = Uuid::new_v4();
|
let client_id = Uuid::new_v4();
|
||||||
ClientConn {
|
ClientConn {
|
||||||
|
client_ip,
|
||||||
client_id,
|
client_id,
|
||||||
subscriptions: HashMap::new(),
|
subscriptions: HashMap::new(),
|
||||||
max_subs: 32,
|
max_subs: 32,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn subscriptions(&self) -> &HashMap<String, Subscription> {
|
||||||
|
&self.subscriptions
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Check if the given subscription already exists
|
||||||
|
pub fn has_subscription(&self, sub: &Subscription) -> bool {
|
||||||
|
self.subscriptions.values().any(|x| x == sub)
|
||||||
|
}
|
||||||
|
|
||||||
/// Get a short prefix of the client's unique identifier, suitable
|
/// Get a short prefix of the client's unique identifier, suitable
|
||||||
/// for logging.
|
/// for logging.
|
||||||
#[must_use]
|
#[must_use]
|
||||||
@@ -47,16 +58,9 @@ impl ClientConn {
|
|||||||
self.client_id.to_string().chars().take(8).collect()
|
self.client_id.to_string().chars().take(8).collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Find all matching subscriptions.
|
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn get_matching_subscriptions(&self, e: &Event) -> Vec<&str> {
|
pub fn ip(&self) -> &str {
|
||||||
let mut v: Vec<&str> = vec![];
|
&self.client_ip
|
||||||
for (id, sub) in &self.subscriptions {
|
|
||||||
if sub.interested_in_event(e) {
|
|
||||||
v.push(id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
v
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Add a new subscription for this connection.
|
/// Add a new subscription for this connection.
|
||||||
@@ -70,7 +74,7 @@ impl ClientConn {
|
|||||||
// prevent arbitrarily long subscription identifiers from
|
// prevent arbitrarily long subscription identifiers from
|
||||||
// being used.
|
// being used.
|
||||||
if sub_id_len > MAX_SUBSCRIPTION_ID_LEN {
|
if sub_id_len > MAX_SUBSCRIPTION_ID_LEN {
|
||||||
info!(
|
debug!(
|
||||||
"ignoring sub request with excessive length: ({})",
|
"ignoring sub request with excessive length: ({})",
|
||||||
sub_id_len
|
sub_id_len
|
||||||
);
|
);
|
||||||
@@ -79,8 +83,12 @@ impl ClientConn {
|
|||||||
// check if an existing subscription exists, and replace if so
|
// check if an existing subscription exists, and replace if so
|
||||||
if self.subscriptions.contains_key(&k) {
|
if self.subscriptions.contains_key(&k) {
|
||||||
self.subscriptions.remove(&k);
|
self.subscriptions.remove(&k);
|
||||||
self.subscriptions.insert(k, s);
|
self.subscriptions.insert(k, s.clone());
|
||||||
debug!("replaced existing subscription");
|
trace!(
|
||||||
|
"replaced existing subscription (cid: {}, sub: {:?})",
|
||||||
|
self.get_client_prefix(),
|
||||||
|
s.get_id()
|
||||||
|
);
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,9 +98,10 @@ impl ClientConn {
|
|||||||
}
|
}
|
||||||
// add subscription
|
// add subscription
|
||||||
self.subscriptions.insert(k, s);
|
self.subscriptions.insert(k, s);
|
||||||
debug!(
|
trace!(
|
||||||
"registered new subscription, currently have {} active subs",
|
"registered new subscription, currently have {} active subs (cid: {})",
|
||||||
self.subscriptions.len()
|
self.subscriptions.len(),
|
||||||
|
self.get_client_prefix(),
|
||||||
);
|
);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -101,10 +110,10 @@ impl ClientConn {
|
|||||||
pub fn unsubscribe(&mut self, c: &Close) {
|
pub fn unsubscribe(&mut self, c: &Close) {
|
||||||
// TODO: return notice if subscription did not exist.
|
// TODO: return notice if subscription did not exist.
|
||||||
self.subscriptions.remove(&c.id);
|
self.subscriptions.remove(&c.id);
|
||||||
debug!(
|
trace!(
|
||||||
"removed subscription, currently have {} active subs (cid={})",
|
"removed subscription, currently have {} active subs (cid: {})",
|
||||||
self.subscriptions.len(),
|
self.subscriptions.len(),
|
||||||
self.client_id
|
self.get_client_prefix(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ use crate::event::{single_char_tagname, Event};
|
|||||||
use crate::hexrange::hex_range;
|
use crate::hexrange::hex_range;
|
||||||
use crate::hexrange::HexSearch;
|
use crate::hexrange::HexSearch;
|
||||||
use crate::nip05;
|
use crate::nip05;
|
||||||
|
use crate::notice::Notice;
|
||||||
use crate::schema::{upgrade_db, STARTUP_SQL};
|
use crate::schema::{upgrade_db, STARTUP_SQL};
|
||||||
use crate::subscription::ReqFilter;
|
use crate::subscription::ReqFilter;
|
||||||
use crate::subscription::Subscription;
|
use crate::subscription::Subscription;
|
||||||
@@ -32,12 +33,19 @@ pub type PooledConnection = r2d2::PooledConnection<r2d2_sqlite::SqliteConnection
|
|||||||
/// Events submitted from a client, with a return channel for notices
|
/// Events submitted from a client, with a return channel for notices
|
||||||
pub struct SubmittedEvent {
|
pub struct SubmittedEvent {
|
||||||
pub event: Event,
|
pub event: Event,
|
||||||
pub notice_tx: tokio::sync::mpsc::Sender<String>,
|
pub notice_tx: tokio::sync::mpsc::Sender<Notice>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Database file
|
/// Database file
|
||||||
pub const DB_FILE: &str = "nostr.db";
|
pub const DB_FILE: &str = "nostr.db";
|
||||||
|
|
||||||
|
/// How frequently to attempt checkpointing
|
||||||
|
pub const CHECKPOINT_FREQ_SEC: u64 = 60;
|
||||||
|
|
||||||
|
/// How many persisted events before we pause for backups.
|
||||||
|
/// It isn't clear this is enough to make the online backup API work yet.
|
||||||
|
pub const EVENT_COUNT_BACKUP_PAUSE_TRIGGER: usize = 1000;
|
||||||
|
|
||||||
/// Build a database connection pool.
|
/// Build a database connection pool.
|
||||||
/// # Panics
|
/// # Panics
|
||||||
///
|
///
|
||||||
@@ -75,6 +83,7 @@ pub fn build_pool(
|
|||||||
.test_on_check_out(true) // no noticeable performance hit
|
.test_on_check_out(true) // no noticeable performance hit
|
||||||
.min_idle(Some(min_size))
|
.min_idle(Some(min_size))
|
||||||
.max_size(max_size)
|
.max_size(max_size)
|
||||||
|
.max_lifetime(Some(Duration::from_secs(30)))
|
||||||
.build(manager)
|
.build(manager)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
info!(
|
info!(
|
||||||
@@ -84,6 +93,56 @@ pub fn build_pool(
|
|||||||
pool
|
pool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Display database pool stats every 1 minute
|
||||||
|
pub async fn monitor_pool(name: &str, pool: SqlitePool) {
|
||||||
|
let sleep_dur = Duration::from_secs(60);
|
||||||
|
loop {
|
||||||
|
log_pool_stats(name, &pool);
|
||||||
|
tokio::time::sleep(sleep_dur).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Perform normal maintenance
|
||||||
|
pub fn optimize_db(conn: &mut PooledConnection) -> Result<()> {
|
||||||
|
let start = Instant::now();
|
||||||
|
conn.execute_batch("PRAGMA optimize;")?;
|
||||||
|
info!("optimize ran in {:?}", start.elapsed());
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
#[derive(Debug)]
|
||||||
|
enum SqliteStatus {
|
||||||
|
Ok,
|
||||||
|
Busy,
|
||||||
|
Error,
|
||||||
|
Other(u64),
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Checkpoint/Truncate WAL. Returns the number of WAL pages remaining.
|
||||||
|
pub fn checkpoint_db(conn: &mut PooledConnection) -> Result<usize> {
|
||||||
|
let query = "PRAGMA wal_checkpoint(TRUNCATE);";
|
||||||
|
let start = Instant::now();
|
||||||
|
let (cp_result, wal_size, _frames_checkpointed) = conn.query_row(query, [], |row| {
|
||||||
|
let checkpoint_result: u64 = row.get(0)?;
|
||||||
|
let wal_size: u64 = row.get(1)?;
|
||||||
|
let frames_checkpointed: u64 = row.get(2)?;
|
||||||
|
Ok((checkpoint_result, wal_size, frames_checkpointed))
|
||||||
|
})?;
|
||||||
|
let result = match cp_result {
|
||||||
|
0 => SqliteStatus::Ok,
|
||||||
|
1 => SqliteStatus::Busy,
|
||||||
|
2 => SqliteStatus::Error,
|
||||||
|
x => SqliteStatus::Other(x),
|
||||||
|
};
|
||||||
|
info!(
|
||||||
|
"checkpoint ran in {:?} (result: {:?}, WAL size: {})",
|
||||||
|
start.elapsed(),
|
||||||
|
result,
|
||||||
|
wal_size
|
||||||
|
);
|
||||||
|
Ok(wal_size as usize)
|
||||||
|
}
|
||||||
|
|
||||||
/// Spawn a database writer that persists events to the SQLite store.
|
/// Spawn a database writer that persists events to the SQLite store.
|
||||||
pub async fn db_writer(
|
pub async fn db_writer(
|
||||||
settings: Settings,
|
settings: Settings,
|
||||||
@@ -106,7 +165,7 @@ pub async fn db_writer(
|
|||||||
&settings,
|
&settings,
|
||||||
OpenFlags::SQLITE_OPEN_READ_WRITE | OpenFlags::SQLITE_OPEN_CREATE,
|
OpenFlags::SQLITE_OPEN_READ_WRITE | OpenFlags::SQLITE_OPEN_CREATE,
|
||||||
1,
|
1,
|
||||||
4,
|
2,
|
||||||
false,
|
false,
|
||||||
);
|
);
|
||||||
if settings.database.in_memory {
|
if settings.database.in_memory {
|
||||||
@@ -123,6 +182,10 @@ pub async fn db_writer(
|
|||||||
let rps_setting = settings.limits.messages_per_sec;
|
let rps_setting = settings.limits.messages_per_sec;
|
||||||
let mut most_recent_rate_limit = Instant::now();
|
let mut most_recent_rate_limit = Instant::now();
|
||||||
let mut lim_opt = None;
|
let mut lim_opt = None;
|
||||||
|
// Constant writing has interfered with online backups. Keep
|
||||||
|
// track of how long since we've given the backups a chance to
|
||||||
|
// run.
|
||||||
|
let mut backup_pause_counter: usize = 0;
|
||||||
let clock = governor::clock::QuantaClock::default();
|
let clock = governor::clock::QuantaClock::default();
|
||||||
if let Some(rps) = rps_setting {
|
if let Some(rps) = rps_setting {
|
||||||
if rps > 0 {
|
if rps > 0 {
|
||||||
@@ -142,12 +205,15 @@ pub async fn db_writer(
|
|||||||
if next_event.is_none() {
|
if next_event.is_none() {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
// track if an event write occurred; this is used to
|
||||||
|
// update the rate limiter
|
||||||
let mut event_write = false;
|
let mut event_write = false;
|
||||||
let subm_event = next_event.unwrap();
|
let subm_event = next_event.unwrap();
|
||||||
let event = subm_event.event;
|
let event = subm_event.event;
|
||||||
let notice_tx = subm_event.notice_tx;
|
let notice_tx = subm_event.notice_tx;
|
||||||
// check if this event is authorized.
|
// check if this event is authorized.
|
||||||
if let Some(allowed_addrs) = whitelist {
|
if let Some(allowed_addrs) = whitelist {
|
||||||
|
// TODO: incorporate delegated pubkeys
|
||||||
// if the event address is not in allowed_addrs.
|
// if the event address is not in allowed_addrs.
|
||||||
if !allowed_addrs.contains(&event.pubkey) {
|
if !allowed_addrs.contains(&event.pubkey) {
|
||||||
info!(
|
info!(
|
||||||
@@ -155,7 +221,10 @@ pub async fn db_writer(
|
|||||||
event.get_event_id_prefix()
|
event.get_event_id_prefix()
|
||||||
);
|
);
|
||||||
notice_tx
|
notice_tx
|
||||||
.try_send("pubkey is not allowed to publish to this relay".to_owned())
|
.try_send(Notice::blocked(
|
||||||
|
event.id,
|
||||||
|
"pubkey is not allowed to publish to this relay",
|
||||||
|
))
|
||||||
.ok();
|
.ok();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -181,15 +250,16 @@ pub async fn db_writer(
|
|||||||
event.get_author_prefix()
|
event.get_author_prefix()
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
info!("rejecting event, author ({:?} / {:?}) verification invalid (expired/wrong domain)",
|
info!(
|
||||||
uv.name.to_string(),
|
"rejecting event, author ({:?} / {:?}) verification invalid (expired/wrong domain)",
|
||||||
event.get_author_prefix()
|
uv.name.to_string(),
|
||||||
|
event.get_author_prefix()
|
||||||
);
|
);
|
||||||
notice_tx
|
notice_tx
|
||||||
.try_send(
|
.try_send(Notice::blocked(
|
||||||
"NIP-05 verification is no longer valid (expired/wrong domain)"
|
event.id,
|
||||||
.to_owned(),
|
"NIP-05 verification is no longer valid (expired/wrong domain)",
|
||||||
)
|
))
|
||||||
.ok();
|
.ok();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -200,7 +270,10 @@ pub async fn db_writer(
|
|||||||
event.get_author_prefix()
|
event.get_author_prefix()
|
||||||
);
|
);
|
||||||
notice_tx
|
notice_tx
|
||||||
.try_send("NIP-05 verification needed to publish events".to_owned())
|
.try_send(Notice::blocked(
|
||||||
|
event.id,
|
||||||
|
"NIP-05 verification needed to publish events",
|
||||||
|
))
|
||||||
.ok();
|
.ok();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -213,22 +286,24 @@ pub async fn db_writer(
|
|||||||
// TODO: cache recent list of authors to remove a DB call.
|
// TODO: cache recent list of authors to remove a DB call.
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
if event.kind >= 20000 && event.kind < 30000 {
|
if event.kind >= 20000 && event.kind < 30000 {
|
||||||
|
bcast_tx.send(event.clone()).ok();
|
||||||
info!(
|
info!(
|
||||||
"published ephemeral event {:?} from {:?} in {:?}",
|
"published ephemeral event: {:?} from: {:?} in: {:?}",
|
||||||
event.get_event_id_prefix(),
|
event.get_event_id_prefix(),
|
||||||
event.get_author_prefix(),
|
event.get_author_prefix(),
|
||||||
start.elapsed()
|
start.elapsed()
|
||||||
);
|
);
|
||||||
bcast_tx.send(event.clone()).ok();
|
|
||||||
event_write = true
|
event_write = true
|
||||||
} else {
|
} else {
|
||||||
|
log_pool_stats("writer", &pool);
|
||||||
match write_event(&mut pool.get()?, &event) {
|
match write_event(&mut pool.get()?, &event) {
|
||||||
Ok(updated) => {
|
Ok(updated) => {
|
||||||
if updated == 0 {
|
if updated == 0 {
|
||||||
trace!("ignoring duplicate or deleted event");
|
trace!("ignoring duplicate or deleted event");
|
||||||
|
notice_tx.try_send(Notice::duplicate(event.id)).ok();
|
||||||
} else {
|
} else {
|
||||||
info!(
|
info!(
|
||||||
"persisted event {:?} from {:?} in {:?}",
|
"persisted event: {:?} from: {:?} in: {:?}",
|
||||||
event.get_event_id_prefix(),
|
event.get_event_id_prefix(),
|
||||||
event.get_author_prefix(),
|
event.get_author_prefix(),
|
||||||
start.elapsed()
|
start.elapsed()
|
||||||
@@ -236,18 +311,21 @@ pub async fn db_writer(
|
|||||||
event_write = true;
|
event_write = true;
|
||||||
// send this out to all clients
|
// send this out to all clients
|
||||||
bcast_tx.send(event.clone()).ok();
|
bcast_tx.send(event.clone()).ok();
|
||||||
|
notice_tx.try_send(Notice::saved(event.id)).ok();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
warn!("event insert failed: {:?}", err);
|
warn!("event insert failed: {:?}", err);
|
||||||
notice_tx
|
let msg = "relay experienced an error trying to publish the latest event";
|
||||||
.try_send(
|
notice_tx.try_send(Notice::error(event.id, msg)).ok();
|
||||||
"relay experienced an error trying to publish the latest event"
|
|
||||||
.to_owned(),
|
|
||||||
)
|
|
||||||
.ok();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
backup_pause_counter += 1;
|
||||||
|
if backup_pause_counter > EVENT_COUNT_BACKUP_PAUSE_TRIGGER {
|
||||||
|
info!("pausing db write thread for a moment...");
|
||||||
|
thread::sleep(Duration::from_millis(500));
|
||||||
|
backup_pause_counter = 0
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// use rate limit, if defined, and if an event was actually written.
|
// use rate limit, if defined, and if an event was actually written.
|
||||||
@@ -284,16 +362,18 @@ pub fn write_event(conn: &mut PooledConnection, e: &Event) -> Result<usize> {
|
|||||||
let tx = conn.transaction()?;
|
let tx = conn.transaction()?;
|
||||||
// get relevant fields from event and convert to blobs.
|
// get relevant fields from event and convert to blobs.
|
||||||
let id_blob = hex::decode(&e.id).ok();
|
let id_blob = hex::decode(&e.id).ok();
|
||||||
let pubkey_blob = hex::decode(&e.pubkey).ok();
|
let pubkey_blob: Option<Vec<u8>> = hex::decode(&e.pubkey).ok();
|
||||||
|
let delegator_blob: Option<Vec<u8>> = e.delegated_by.as_ref().and_then(|d| hex::decode(d).ok());
|
||||||
let event_str = serde_json::to_string(&e).ok();
|
let event_str = serde_json::to_string(&e).ok();
|
||||||
// ignore if the event hash is a duplicate.
|
// ignore if the event hash is a duplicate.
|
||||||
let mut ins_count = tx.execute(
|
let mut ins_count = tx.execute(
|
||||||
"INSERT OR IGNORE INTO event (event_hash, created_at, kind, author, content, first_seen, hidden) VALUES (?1, ?2, ?3, ?4, ?5, strftime('%s','now'), FALSE);",
|
"INSERT OR IGNORE INTO event (event_hash, created_at, kind, author, delegated_by, content, first_seen, hidden) VALUES (?1, ?2, ?3, ?4, ?5, ?6, strftime('%s','now'), FALSE);",
|
||||||
params![id_blob, e.created_at, e.kind, pubkey_blob, event_str]
|
params![id_blob, e.created_at, e.kind, pubkey_blob, delegator_blob, event_str]
|
||||||
)?;
|
)?;
|
||||||
if ins_count == 0 {
|
if ins_count == 0 {
|
||||||
// if the event was a duplicate, no need to insert event or
|
// if the event was a duplicate, no need to insert event or
|
||||||
// pubkey references. This will abort the txn.
|
// pubkey references.
|
||||||
|
tx.rollback().ok();
|
||||||
return Ok(ins_count);
|
return Ok(ins_count);
|
||||||
}
|
}
|
||||||
// remember primary key of the event most recently inserted.
|
// remember primary key of the event most recently inserted.
|
||||||
@@ -311,9 +391,9 @@ pub fn write_event(conn: &mut PooledConnection, e: &Event) -> Result<usize> {
|
|||||||
// if tagvalue is lowercase hex;
|
// if tagvalue is lowercase hex;
|
||||||
if is_lower_hex(tagval) && (tagval.len() % 2 == 0) {
|
if is_lower_hex(tagval) && (tagval.len() % 2 == 0) {
|
||||||
tx.execute(
|
tx.execute(
|
||||||
"INSERT OR IGNORE INTO tag (event_id, name, value_hex) VALUES (?1, ?2, ?3)",
|
"INSERT OR IGNORE INTO tag (event_id, name, value_hex) VALUES (?1, ?2, ?3)",
|
||||||
params![ev_id, &tagname, hex::decode(&tagval).ok()],
|
params![ev_id, &tagname, hex::decode(tagval).ok()],
|
||||||
)?;
|
)?;
|
||||||
} else {
|
} else {
|
||||||
tx.execute(
|
tx.execute(
|
||||||
"INSERT OR IGNORE INTO tag (event_id, name, value) VALUES (?1, ?2, ?3)",
|
"INSERT OR IGNORE INTO tag (event_id, name, value) VALUES (?1, ?2, ?3)",
|
||||||
@@ -328,7 +408,7 @@ pub fn write_event(conn: &mut PooledConnection, e: &Event) -> Result<usize> {
|
|||||||
// if this event is replaceable update, hide every other replaceable
|
// if this event is replaceable update, hide every other replaceable
|
||||||
// event with the same kind from the same author that was issued
|
// event with the same kind from the same author that was issued
|
||||||
// earlier than this.
|
// earlier than this.
|
||||||
if e.kind == 0 || e.kind == 3 || (e.kind >= 10000 && e.kind < 20000) {
|
if e.kind == 0 || e.kind == 3 || e.kind == 41 || (e.kind >= 10000 && e.kind < 20000) {
|
||||||
let update_count = tx.execute(
|
let update_count = tx.execute(
|
||||||
"UPDATE event SET hidden=TRUE WHERE id!=? AND kind=? AND author=? AND created_at <= ? and hidden!=TRUE",
|
"UPDATE event SET hidden=TRUE WHERE id!=? AND kind=? AND author=? AND created_at <= ? and hidden!=TRUE",
|
||||||
params![ev_id, e.kind, hex::decode(&e.pubkey).ok(), e.created_at],
|
params![ev_id, e.kind, hex::decode(&e.pubkey).ok(), e.created_at],
|
||||||
@@ -419,14 +499,13 @@ fn query_from_filter(f: &ReqFilter) -> (String, Vec<Box<dyn ToSql>>) {
|
|||||||
|
|
||||||
// if the filter is malformed, don't return anything.
|
// if the filter is malformed, don't return anything.
|
||||||
if f.force_no_match {
|
if f.force_no_match {
|
||||||
let empty_query =
|
let empty_query = "SELECT e.content, e.created_at FROM event e WHERE 1=0".to_owned();
|
||||||
"SELECT DISTINCT(e.content), e.created_at FROM event e WHERE 1=0".to_owned();
|
|
||||||
// query parameters for SQLite
|
// query parameters for SQLite
|
||||||
let empty_params: Vec<Box<dyn ToSql>> = vec![];
|
let empty_params: Vec<Box<dyn ToSql>> = vec![];
|
||||||
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 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![];
|
||||||
|
|
||||||
@@ -439,16 +518,22 @@ fn query_from_filter(f: &ReqFilter) -> (String, Vec<Box<dyn ToSql>>) {
|
|||||||
for auth in authvec {
|
for auth in authvec {
|
||||||
match hex_range(auth) {
|
match hex_range(auth) {
|
||||||
Some(HexSearch::Exact(ex)) => {
|
Some(HexSearch::Exact(ex)) => {
|
||||||
auth_searches.push("author=?".to_owned());
|
auth_searches.push("author=? OR delegated_by=?".to_owned());
|
||||||
|
params.push(Box::new(ex.clone()));
|
||||||
params.push(Box::new(ex));
|
params.push(Box::new(ex));
|
||||||
}
|
}
|
||||||
Some(HexSearch::Range(lower, upper)) => {
|
Some(HexSearch::Range(lower, upper)) => {
|
||||||
auth_searches.push("(author>? AND author<?)".to_owned());
|
auth_searches.push(
|
||||||
|
"(author>? AND author<?) OR (delegated_by>? AND delegated_by<?)".to_owned(),
|
||||||
|
);
|
||||||
|
params.push(Box::new(lower.clone()));
|
||||||
|
params.push(Box::new(upper.clone()));
|
||||||
params.push(Box::new(lower));
|
params.push(Box::new(lower));
|
||||||
params.push(Box::new(upper));
|
params.push(Box::new(upper));
|
||||||
}
|
}
|
||||||
Some(HexSearch::LowerOnly(lower)) => {
|
Some(HexSearch::LowerOnly(lower)) => {
|
||||||
auth_searches.push("author>?".to_owned());
|
auth_searches.push("author>? OR delegated_by>?".to_owned());
|
||||||
|
params.push(Box::new(lower.clone()));
|
||||||
params.push(Box::new(lower));
|
params.push(Box::new(lower));
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
@@ -456,8 +541,14 @@ fn query_from_filter(f: &ReqFilter) -> (String, Vec<Box<dyn ToSql>>) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let authors_clause = format!("({})", auth_searches.join(" OR "));
|
if !authvec.is_empty() {
|
||||||
filter_components.push(authors_clause);
|
let authors_clause = format!("({})", auth_searches.join(" OR "));
|
||||||
|
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 {
|
||||||
@@ -490,8 +581,14 @@ fn query_from_filter(f: &ReqFilter) -> (String, Vec<Box<dyn ToSql>>) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let id_clause = format!("({})", id_searches.join(" OR "));
|
if !idvec.is_empty() {
|
||||||
filter_components.push(id_clause);
|
let id_clause = format!("({})", id_searches.join(" OR "));
|
||||||
|
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 {
|
||||||
@@ -500,7 +597,7 @@ fn query_from_filter(f: &ReqFilter) -> (String, Vec<Box<dyn ToSql>>) {
|
|||||||
let mut blob_vals: Vec<Box<dyn ToSql>> = vec![];
|
let mut blob_vals: Vec<Box<dyn ToSql>> = vec![];
|
||||||
for v in val {
|
for v in val {
|
||||||
if (v.len() % 2 == 0) && is_lower_hex(v) {
|
if (v.len() % 2 == 0) && is_lower_hex(v) {
|
||||||
if let Ok(h) = hex::decode(&v) {
|
if let Ok(h) = hex::decode(v) {
|
||||||
blob_vals.push(Box::new(h));
|
blob_vals.push(Box::new(h));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -511,7 +608,10 @@ fn query_from_filter(f: &ReqFilter) -> (String, Vec<Box<dyn ToSql>>) {
|
|||||||
let str_clause = format!("value IN ({})", repeat_vars(str_vals.len()));
|
let str_clause = format!("value IN ({})", repeat_vars(str_vals.len()));
|
||||||
let blob_clause = format!("value_hex IN ({})", repeat_vars(blob_vals.len()));
|
let blob_clause = format!("value_hex IN ({})", repeat_vars(blob_vals.len()));
|
||||||
// find evidence of the target tag name/value existing for this event.
|
// find evidence of the target tag name/value existing for this event.
|
||||||
let tag_clause = format!("e.id IN (SELECT e.id FROM event e LEFT JOIN tag t on e.id=t.event_id WHERE hidden!=TRUE and (name=? AND ({} OR {})))", str_clause, blob_clause);
|
let tag_clause = format!(
|
||||||
|
"e.id IN (SELECT e.id FROM event e LEFT JOIN tag t on e.id=t.event_id WHERE hidden!=TRUE and (name=? AND ({} OR {})))",
|
||||||
|
str_clause, blob_clause
|
||||||
|
);
|
||||||
// add the tag name as the first parameter
|
// add the tag name as the first parameter
|
||||||
params.push(Box::new(key.to_string()));
|
params.push(Box::new(key.to_string()));
|
||||||
// add all tag values that are plain strings as params
|
// add all tag values that are plain strings as params
|
||||||
@@ -564,13 +664,86 @@ fn query_from_sub(sub: &Subscription) -> (String, Vec<Box<dyn ToSql>>) {
|
|||||||
// encapsulate subqueries into select statements
|
// encapsulate subqueries into select statements
|
||||||
let subqueries_selects: Vec<String> = subqueries
|
let subqueries_selects: Vec<String> = subqueries
|
||||||
.iter()
|
.iter()
|
||||||
.map(|s| format!("SELECT content, created_at FROM ({})", s))
|
.map(|s| format!("SELECT distinct content, created_at FROM ({})", s))
|
||||||
.collect();
|
.collect();
|
||||||
let query: String = subqueries_selects.join(" UNION ");
|
let query: String = subqueries_selects.join(" UNION ");
|
||||||
debug!("final query string: {}", query);
|
|
||||||
(query, params)
|
(query, params)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Check if the pool is fully utilized
|
||||||
|
fn _pool_at_capacity(pool: &SqlitePool) -> bool {
|
||||||
|
let state: r2d2::State = pool.state();
|
||||||
|
state.idle_connections == 0
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Log pool stats
|
||||||
|
fn log_pool_stats(name: &str, pool: &SqlitePool) {
|
||||||
|
let state: r2d2::State = pool.state();
|
||||||
|
let in_use_cxns = state.connections - state.idle_connections;
|
||||||
|
debug!(
|
||||||
|
"DB pool {:?} usage (in_use: {}, available: {}, max: {})",
|
||||||
|
name,
|
||||||
|
in_use_cxns,
|
||||||
|
state.connections,
|
||||||
|
pool.max_size()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Perform database maintenance on a regular basis
|
||||||
|
pub async fn db_optimize(pool: SqlitePool) {
|
||||||
|
tokio::task::spawn(async move {
|
||||||
|
loop {
|
||||||
|
tokio::select! {
|
||||||
|
_ = tokio::time::sleep(Duration::from_secs(60*60)) => {
|
||||||
|
if let Ok(mut conn) = pool.get() {
|
||||||
|
// the busy timer will block writers, so don't set
|
||||||
|
// this any higher than you want max latency for event
|
||||||
|
// writes.
|
||||||
|
info!("running database optimizer");
|
||||||
|
optimize_db(&mut conn).ok();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Perform database WAL checkpoint on a regular basis
|
||||||
|
pub async fn db_checkpoint(pool: SqlitePool) {
|
||||||
|
tokio::task::spawn(async move {
|
||||||
|
// WAL size in pages.
|
||||||
|
let mut current_wal_size = 0;
|
||||||
|
// WAL threshold for more aggressive checkpointing (10,000 pages, or about 40MB)
|
||||||
|
let wal_threshold = 1000*10;
|
||||||
|
// default threshold for the busy timer
|
||||||
|
let busy_wait_default = Duration::from_secs(1);
|
||||||
|
// if the WAL file is getting too big, switch to this
|
||||||
|
let busy_wait_default_long = Duration::from_secs(5);
|
||||||
|
loop {
|
||||||
|
tokio::select! {
|
||||||
|
_ = tokio::time::sleep(Duration::from_secs(CHECKPOINT_FREQ_SEC)) => {
|
||||||
|
if let Ok(mut conn) = pool.get() {
|
||||||
|
// the busy timer will block writers, so don't set
|
||||||
|
// this any higher than you want max latency for event
|
||||||
|
// writes.
|
||||||
|
if current_wal_size <= wal_threshold {
|
||||||
|
conn.busy_timeout(busy_wait_default).ok();
|
||||||
|
} else {
|
||||||
|
// if the wal size has exceeded a threshold, increase the busy timeout.
|
||||||
|
conn.busy_timeout(busy_wait_default_long).ok();
|
||||||
|
}
|
||||||
|
debug!("running wal_checkpoint(TRUNCATE)");
|
||||||
|
if let Ok(new_size) = checkpoint_db(&mut conn) {
|
||||||
|
current_wal_size = new_size;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/// Perform a database query using a subscription.
|
/// Perform a database query using a subscription.
|
||||||
///
|
///
|
||||||
/// The [`Subscription`] is converted into a SQL query. Each result
|
/// The [`Subscription`] is converted into a SQL query. Each result
|
||||||
@@ -584,45 +757,101 @@ pub async fn db_query(
|
|||||||
query_tx: tokio::sync::mpsc::Sender<QueryResult>,
|
query_tx: tokio::sync::mpsc::Sender<QueryResult>,
|
||||||
mut abandon_query_rx: tokio::sync::oneshot::Receiver<()>,
|
mut abandon_query_rx: tokio::sync::oneshot::Receiver<()>,
|
||||||
) {
|
) {
|
||||||
|
let pre_spawn_start = Instant::now();
|
||||||
task::spawn_blocking(move || {
|
task::spawn_blocking(move || {
|
||||||
debug!("going to query for: {:?}", sub);
|
let db_queue_time = pre_spawn_start.elapsed();
|
||||||
let mut row_count: usize = 0;
|
// if the queue time was very long (>5 seconds), spare the DB and abort.
|
||||||
|
if db_queue_time > Duration::from_secs(5) {
|
||||||
|
info!(
|
||||||
|
"shedding DB query load from {:?} (cid: {}, sub: {:?})",
|
||||||
|
db_queue_time, client_id, sub.id
|
||||||
|
);
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
// otherwise, report queuing time if it is slow
|
||||||
|
else if db_queue_time > Duration::from_secs(1) {
|
||||||
|
debug!(
|
||||||
|
"(slow) DB query queued for {:?} (cid: {}, sub: {:?})",
|
||||||
|
db_queue_time, client_id, sub.id
|
||||||
|
);
|
||||||
|
}
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
|
let mut row_count: usize = 0;
|
||||||
// generate SQL query
|
// generate SQL query
|
||||||
let (q, p) = query_from_sub(&sub);
|
let (q, p) = query_from_sub(&sub);
|
||||||
debug!("SQL generated in {:?}", start.elapsed());
|
let sql_gen_elapsed = start.elapsed();
|
||||||
// show pool stats
|
if sql_gen_elapsed > Duration::from_millis(10) {
|
||||||
debug!("DB pool stats: {:?}", pool.state());
|
debug!("SQL (slow) generated in {:?}", start.elapsed());
|
||||||
|
}
|
||||||
|
// cutoff for displaying slow queries
|
||||||
|
let slow_cutoff = Duration::from_millis(2000);
|
||||||
|
// any client that doesn't cause us to generate new rows in 5
|
||||||
|
// seconds gets dropped.
|
||||||
|
let abort_cutoff = Duration::from_secs(5);
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
if let Ok(conn) = pool.get() {
|
let mut slow_first_event;
|
||||||
// execute the query. Don't cache, since queries vary so much.
|
let mut last_successful_send = Instant::now();
|
||||||
let mut stmt = conn.prepare(&q)?;
|
if let Ok(mut conn) = pool.get() {
|
||||||
|
// execute the query.
|
||||||
|
// make the actual SQL query (with parameters inserted) available
|
||||||
|
conn.trace(Some(|x| {trace!("SQL trace: {:?}", x)}));
|
||||||
|
let mut stmt = conn.prepare_cached(&q)?;
|
||||||
let mut event_rows = stmt.query(rusqlite::params_from_iter(p))?;
|
let mut event_rows = stmt.query(rusqlite::params_from_iter(p))?;
|
||||||
|
|
||||||
let mut first_result = true;
|
let mut first_result = true;
|
||||||
while let Some(row) = event_rows.next()? {
|
while let Some(row) = event_rows.next()? {
|
||||||
|
let first_event_elapsed = start.elapsed();
|
||||||
|
slow_first_event = first_event_elapsed >= slow_cutoff;
|
||||||
if first_result {
|
if first_result {
|
||||||
debug!(
|
debug!(
|
||||||
"time to first result: {:?} (cid={}, sub={:?})",
|
"first result in {:?} (cid: {}, sub: {:?})",
|
||||||
start.elapsed(),
|
first_event_elapsed, client_id, sub.id
|
||||||
client_id,
|
|
||||||
sub.id
|
|
||||||
);
|
);
|
||||||
first_result = false;
|
first_result = false;
|
||||||
}
|
}
|
||||||
// check if this is still active
|
// logging for slow queries; show sub and SQL.
|
||||||
// TODO: check every N rows
|
// to reduce logging; only show 1/16th of clients (leading 0)
|
||||||
if abandon_query_rx.try_recv().is_ok() {
|
if row_count == 0 && slow_first_event && client_id.starts_with('0') {
|
||||||
debug!("query aborted (sub={:?})", sub.id);
|
debug!(
|
||||||
|
"query req (slow): {:?} (cid: {}, sub: {:?})",
|
||||||
|
sub, client_id, sub.id
|
||||||
|
);
|
||||||
|
}
|
||||||
|
// check if this is still active; every 100 rows
|
||||||
|
if row_count % 100 == 0 && abandon_query_rx.try_recv().is_ok() {
|
||||||
|
debug!("query aborted (cid: {}, sub: {:?})", client_id, sub.id);
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
row_count += 1;
|
row_count += 1;
|
||||||
let event_json = row.get(0)?;
|
let event_json = row.get(0)?;
|
||||||
|
loop {
|
||||||
|
if query_tx.capacity() != 0 {
|
||||||
|
// we have capacity to add another item
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
// the queue is full
|
||||||
|
trace!("db reader thread is stalled");
|
||||||
|
if last_successful_send + abort_cutoff < Instant::now() {
|
||||||
|
// the queue has been full for too long, abort
|
||||||
|
info!("aborting database query due to slow client (cid: {}, sub: {:?})",
|
||||||
|
client_id, sub.id);
|
||||||
|
let ok: Result<()> = Ok(());
|
||||||
|
return ok;
|
||||||
|
}
|
||||||
|
// give the queue a chance to clear before trying again
|
||||||
|
thread::sleep(Duration::from_millis(100));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// TODO: we could use try_send, but we'd have to juggle
|
||||||
|
// getting the query result back as part of the error
|
||||||
|
// result.
|
||||||
query_tx
|
query_tx
|
||||||
.blocking_send(QueryResult {
|
.blocking_send(QueryResult {
|
||||||
sub_id: sub.get_id(),
|
sub_id: sub.get_id(),
|
||||||
event: event_json,
|
event: event_json,
|
||||||
})
|
})
|
||||||
.ok();
|
.ok();
|
||||||
|
last_successful_send = Instant::now();
|
||||||
}
|
}
|
||||||
query_tx
|
query_tx
|
||||||
.blocking_send(QueryResult {
|
.blocking_send(QueryResult {
|
||||||
@@ -631,11 +860,12 @@ pub async fn db_query(
|
|||||||
})
|
})
|
||||||
.ok();
|
.ok();
|
||||||
debug!(
|
debug!(
|
||||||
"query completed ({} rows) in {:?} (cid={}, sub={:?})",
|
"query completed in {:?} (cid: {}, sub: {:?}, db_time: {:?}, rows: {})",
|
||||||
row_count,
|
pre_spawn_start.elapsed(),
|
||||||
start.elapsed(),
|
|
||||||
client_id,
|
client_id,
|
||||||
sub.id
|
sub.id,
|
||||||
|
start.elapsed(),
|
||||||
|
row_count
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
warn!("Could not get a database connection for querying");
|
warn!("Could not get a database connection for querying");
|
||||||
|
|||||||
@@ -0,0 +1,403 @@
|
|||||||
|
//! Event parsing and validation
|
||||||
|
use crate::error::Error;
|
||||||
|
use crate::error::Result;
|
||||||
|
use crate::event::Event;
|
||||||
|
use bitcoin_hashes::{sha256, Hash};
|
||||||
|
use lazy_static::lazy_static;
|
||||||
|
use regex::Regex;
|
||||||
|
use secp256k1::{schnorr, Secp256k1, VerifyOnly, XOnlyPublicKey};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::str::FromStr;
|
||||||
|
use tracing::{debug, info};
|
||||||
|
|
||||||
|
// This handles everything related to delegation, in particular the
|
||||||
|
// condition/rune parsing and logic.
|
||||||
|
|
||||||
|
// Conditions are poorly specified, so we will implement the minimum
|
||||||
|
// necessary for now.
|
||||||
|
|
||||||
|
// fields MUST be either "kind" or "created_at".
|
||||||
|
// operators supported are ">", "<", "=", "!".
|
||||||
|
// no operations on 'content' are supported.
|
||||||
|
|
||||||
|
// this allows constraints for:
|
||||||
|
// valid date ranges (valid from X->Y dates).
|
||||||
|
// specific kinds (publish kind=1,5)
|
||||||
|
// kind ranges (publish ephemeral events, kind>19999&kind<30001)
|
||||||
|
|
||||||
|
// for more complex scenarios (allow delegatee to publish ephemeral
|
||||||
|
// AND replacement events), it may be necessary to generate and use
|
||||||
|
// different condition strings, since we do not support grouping or
|
||||||
|
// "OR" logic.
|
||||||
|
|
||||||
|
lazy_static! {
|
||||||
|
/// Secp256k1 verification instance.
|
||||||
|
pub static ref SECP: Secp256k1<VerifyOnly> = Secp256k1::verification_only();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
|
||||||
|
pub enum Field {
|
||||||
|
Kind,
|
||||||
|
CreatedAt,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FromStr for Field {
|
||||||
|
type Err = Error;
|
||||||
|
fn from_str(value: &str) -> Result<Self, Self::Err> {
|
||||||
|
if value == "kind" {
|
||||||
|
Ok(Field::Kind)
|
||||||
|
} else if value == "created_at" {
|
||||||
|
Ok(Field::CreatedAt)
|
||||||
|
} else {
|
||||||
|
Err(Error::DelegationParseError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
|
||||||
|
pub enum Operator {
|
||||||
|
LessThan,
|
||||||
|
GreaterThan,
|
||||||
|
Equals,
|
||||||
|
NotEquals,
|
||||||
|
}
|
||||||
|
impl FromStr for Operator {
|
||||||
|
type Err = Error;
|
||||||
|
fn from_str(value: &str) -> Result<Self, Self::Err> {
|
||||||
|
if value == "<" {
|
||||||
|
Ok(Operator::LessThan)
|
||||||
|
} else if value == ">" {
|
||||||
|
Ok(Operator::GreaterThan)
|
||||||
|
} else if value == "=" {
|
||||||
|
Ok(Operator::Equals)
|
||||||
|
} else if value == "!" {
|
||||||
|
Ok(Operator::NotEquals)
|
||||||
|
} else {
|
||||||
|
Err(Error::DelegationParseError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
|
||||||
|
pub struct ConditionQuery {
|
||||||
|
pub conditions: Vec<Condition>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ConditionQuery {
|
||||||
|
pub fn allows_event(&self, event: &Event) -> bool {
|
||||||
|
// check each condition, to ensure that the event complies
|
||||||
|
// with the restriction.
|
||||||
|
for c in &self.conditions {
|
||||||
|
if !c.allows_event(event) {
|
||||||
|
// any failing conditions invalidates the delegation
|
||||||
|
// on this event
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// delegation was permitted unconditionally, or all conditions
|
||||||
|
// were true
|
||||||
|
true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify that the delegator approved the delegation; return a ConditionQuery if so.
|
||||||
|
pub fn validate_delegation(
|
||||||
|
delegator: &str,
|
||||||
|
delegatee: &str,
|
||||||
|
cond_query: &str,
|
||||||
|
sigstr: &str,
|
||||||
|
) -> Option<ConditionQuery> {
|
||||||
|
// form the token
|
||||||
|
let tok = format!("nostr:delegation:{}:{}", delegatee, cond_query);
|
||||||
|
// form SHA256 hash
|
||||||
|
let digest: sha256::Hash = sha256::Hash::hash(tok.as_bytes());
|
||||||
|
let sig = schnorr::Signature::from_str(sigstr).unwrap();
|
||||||
|
if let Ok(msg) = secp256k1::Message::from_slice(digest.as_ref()) {
|
||||||
|
if let Ok(pubkey) = XOnlyPublicKey::from_str(delegator) {
|
||||||
|
let verify = SECP.verify_schnorr(&sig, &msg, &pubkey);
|
||||||
|
if verify.is_ok() {
|
||||||
|
// return the parsed condition query
|
||||||
|
cond_query.parse::<ConditionQuery>().ok()
|
||||||
|
} else {
|
||||||
|
debug!("client sent an delegation signature that did not validate");
|
||||||
|
None
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
debug!("client sent malformed delegation pubkey");
|
||||||
|
None
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
info!("error converting delegation digest to secp256k1 message");
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Parsed delegation condition
|
||||||
|
/// see https://github.com/nostr-protocol/nips/pull/28#pullrequestreview-1084903800
|
||||||
|
/// An example complex condition would be: kind=1,2,3&created_at<1665265999
|
||||||
|
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
|
||||||
|
pub struct Condition {
|
||||||
|
pub field: Field,
|
||||||
|
pub operator: Operator,
|
||||||
|
pub values: Vec<u64>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Condition {
|
||||||
|
/// Check if this condition allows the given event to be delegated
|
||||||
|
pub fn allows_event(&self, event: &Event) -> bool {
|
||||||
|
// determine what the right-hand side of the operator is
|
||||||
|
let resolved_field = match &self.field {
|
||||||
|
Field::Kind => event.kind,
|
||||||
|
Field::CreatedAt => event.created_at,
|
||||||
|
};
|
||||||
|
match &self.operator {
|
||||||
|
Operator::LessThan => {
|
||||||
|
// the less-than operator is only valid for single values.
|
||||||
|
if self.values.len() == 1 {
|
||||||
|
if let Some(v) = self.values.first() {
|
||||||
|
return resolved_field < *v;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Operator::GreaterThan => {
|
||||||
|
// the greater-than operator is only valid for single values.
|
||||||
|
if self.values.len() == 1 {
|
||||||
|
if let Some(v) = self.values.first() {
|
||||||
|
return resolved_field > *v;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Operator::Equals => {
|
||||||
|
// equals is interpreted as "must be equal to at least one provided value"
|
||||||
|
return self.values.iter().any(|&x| resolved_field == x);
|
||||||
|
}
|
||||||
|
Operator::NotEquals => {
|
||||||
|
// not-equals is interpreted as "must not be equal to any provided value"
|
||||||
|
// this is the one case where an empty list of values could be allowed; even though it is a pointless restriction.
|
||||||
|
return self.values.iter().all(|&x| resolved_field != x);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn str_to_condition(cs: &str) -> Option<Condition> {
|
||||||
|
// a condition is a string (alphanum+underscore), an operator (<>=!), and values (num+comma)
|
||||||
|
lazy_static! {
|
||||||
|
static ref RE: Regex = Regex::new("([[:word:]]+)([<>=!]+)([,[[:digit:]]]*)").unwrap();
|
||||||
|
}
|
||||||
|
// match against the regex
|
||||||
|
let caps = RE.captures(cs)?;
|
||||||
|
let field = caps.get(1)?.as_str().parse::<Field>().ok()?;
|
||||||
|
let operator = caps.get(2)?.as_str().parse::<Operator>().ok()?;
|
||||||
|
// values are just comma separated numbers, but all must be parsed
|
||||||
|
let rawvals = caps.get(3)?.as_str();
|
||||||
|
let values = rawvals
|
||||||
|
.split_terminator(',')
|
||||||
|
.map(|n| n.parse::<u64>().ok())
|
||||||
|
.collect::<Option<Vec<_>>>()?;
|
||||||
|
// convert field string into Field
|
||||||
|
Some(Condition {
|
||||||
|
field,
|
||||||
|
operator,
|
||||||
|
values,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Parse a condition query from a string slice
|
||||||
|
impl FromStr for ConditionQuery {
|
||||||
|
type Err = Error;
|
||||||
|
fn from_str(value: &str) -> Result<Self, Self::Err> {
|
||||||
|
// split the string with '&'
|
||||||
|
let mut conditions = vec![];
|
||||||
|
let condstrs = value.split_terminator('&');
|
||||||
|
// parse each individual condition
|
||||||
|
for c in condstrs {
|
||||||
|
conditions.push(str_to_condition(c).ok_or(Error::DelegationParseError)?);
|
||||||
|
}
|
||||||
|
Ok(ConditionQuery { conditions })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
// parse condition strings
|
||||||
|
#[test]
|
||||||
|
fn parse_empty() -> Result<()> {
|
||||||
|
// given an empty condition query, produce an empty vector
|
||||||
|
let empty_cq = ConditionQuery { conditions: vec![] };
|
||||||
|
let parsed = "".parse::<ConditionQuery>()?;
|
||||||
|
assert_eq!(parsed, empty_cq);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
// parse field 'kind'
|
||||||
|
#[test]
|
||||||
|
fn test_kind_field_parse() -> Result<()> {
|
||||||
|
let field = "kind".parse::<Field>()?;
|
||||||
|
assert_eq!(field, Field::Kind);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
// parse field 'created_at'
|
||||||
|
#[test]
|
||||||
|
fn test_created_at_field_parse() -> Result<()> {
|
||||||
|
let field = "created_at".parse::<Field>()?;
|
||||||
|
assert_eq!(field, Field::CreatedAt);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
// parse unknown field
|
||||||
|
#[test]
|
||||||
|
fn unknown_field_parse() {
|
||||||
|
let field = "unk".parse::<Field>();
|
||||||
|
assert!(field.is_err());
|
||||||
|
}
|
||||||
|
|
||||||
|
// parse a full conditional query with an empty array
|
||||||
|
#[test]
|
||||||
|
fn parse_kind_equals_empty() -> Result<()> {
|
||||||
|
// given an empty condition query, produce an empty vector
|
||||||
|
let kind_cq = ConditionQuery {
|
||||||
|
conditions: vec![Condition {
|
||||||
|
field: Field::Kind,
|
||||||
|
operator: Operator::Equals,
|
||||||
|
values: vec![],
|
||||||
|
}],
|
||||||
|
};
|
||||||
|
let parsed = "kind=".parse::<ConditionQuery>()?;
|
||||||
|
assert_eq!(parsed, kind_cq);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
// parse a full conditional query with a single value
|
||||||
|
#[test]
|
||||||
|
fn parse_kind_equals_singleval() -> Result<()> {
|
||||||
|
// given an empty condition query, produce an empty vector
|
||||||
|
let kind_cq = ConditionQuery {
|
||||||
|
conditions: vec![Condition {
|
||||||
|
field: Field::Kind,
|
||||||
|
operator: Operator::Equals,
|
||||||
|
values: vec![1],
|
||||||
|
}],
|
||||||
|
};
|
||||||
|
let parsed = "kind=1".parse::<ConditionQuery>()?;
|
||||||
|
assert_eq!(parsed, kind_cq);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
// parse a full conditional query with multiple values
|
||||||
|
#[test]
|
||||||
|
fn parse_kind_equals_multival() -> Result<()> {
|
||||||
|
// given an empty condition query, produce an empty vector
|
||||||
|
let kind_cq = ConditionQuery {
|
||||||
|
conditions: vec![Condition {
|
||||||
|
field: Field::Kind,
|
||||||
|
operator: Operator::Equals,
|
||||||
|
values: vec![1, 2, 4],
|
||||||
|
}],
|
||||||
|
};
|
||||||
|
let parsed = "kind=1,2,4".parse::<ConditionQuery>()?;
|
||||||
|
assert_eq!(parsed, kind_cq);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
// parse multiple conditions
|
||||||
|
#[test]
|
||||||
|
fn parse_multi_conditions() -> Result<()> {
|
||||||
|
// given an empty condition query, produce an empty vector
|
||||||
|
let cq = ConditionQuery {
|
||||||
|
conditions: vec![
|
||||||
|
Condition {
|
||||||
|
field: Field::Kind,
|
||||||
|
operator: Operator::GreaterThan,
|
||||||
|
values: vec![10000],
|
||||||
|
},
|
||||||
|
Condition {
|
||||||
|
field: Field::Kind,
|
||||||
|
operator: Operator::LessThan,
|
||||||
|
values: vec![20000],
|
||||||
|
},
|
||||||
|
Condition {
|
||||||
|
field: Field::Kind,
|
||||||
|
operator: Operator::NotEquals,
|
||||||
|
values: vec![10001],
|
||||||
|
},
|
||||||
|
Condition {
|
||||||
|
field: Field::CreatedAt,
|
||||||
|
operator: Operator::LessThan,
|
||||||
|
values: vec![1665867123],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
};
|
||||||
|
let parsed =
|
||||||
|
"kind>10000&kind<20000&kind!10001&created_at<1665867123".parse::<ConditionQuery>()?;
|
||||||
|
assert_eq!(parsed, cq);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
// Check for condition logic on event w/ empty values
|
||||||
|
#[test]
|
||||||
|
fn condition_with_empty_values() {
|
||||||
|
let mut c = Condition {
|
||||||
|
field: Field::Kind,
|
||||||
|
operator: Operator::GreaterThan,
|
||||||
|
values: vec![],
|
||||||
|
};
|
||||||
|
let e = Event::simple_event();
|
||||||
|
assert!(!c.allows_event(&e));
|
||||||
|
c.operator = Operator::LessThan;
|
||||||
|
assert!(!c.allows_event(&e));
|
||||||
|
c.operator = Operator::Equals;
|
||||||
|
assert!(!c.allows_event(&e));
|
||||||
|
// Not Equals applied to an empty list *is* allowed
|
||||||
|
// (pointless, but logically valid).
|
||||||
|
c.operator = Operator::NotEquals;
|
||||||
|
assert!(c.allows_event(&e));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for condition logic on event w/ single value
|
||||||
|
#[test]
|
||||||
|
fn condition_kind_gt_event_single() {
|
||||||
|
let c = Condition {
|
||||||
|
field: Field::Kind,
|
||||||
|
operator: Operator::GreaterThan,
|
||||||
|
values: vec![10],
|
||||||
|
};
|
||||||
|
let mut e = Event::simple_event();
|
||||||
|
// kind is not greater than 10, not allowed
|
||||||
|
e.kind = 1;
|
||||||
|
assert!(!c.allows_event(&e));
|
||||||
|
// kind is greater than 10, allowed
|
||||||
|
e.kind = 100;
|
||||||
|
assert!(c.allows_event(&e));
|
||||||
|
// kind is 10, not allowed
|
||||||
|
e.kind = 10;
|
||||||
|
assert!(!c.allows_event(&e));
|
||||||
|
}
|
||||||
|
// Check for condition logic on event w/ multi values
|
||||||
|
#[test]
|
||||||
|
fn condition_with_multi_values() {
|
||||||
|
let mut c = Condition {
|
||||||
|
field: Field::Kind,
|
||||||
|
operator: Operator::Equals,
|
||||||
|
values: vec![0, 10, 20],
|
||||||
|
};
|
||||||
|
let mut e = Event::simple_event();
|
||||||
|
// Allow if event kind is in list for Equals
|
||||||
|
e.kind = 10;
|
||||||
|
assert!(c.allows_event(&e));
|
||||||
|
// Deny if event kind is not in list for Equals
|
||||||
|
e.kind = 11;
|
||||||
|
assert!(!c.allows_event(&e));
|
||||||
|
// Deny if event kind is in list for NotEquals
|
||||||
|
e.kind = 10;
|
||||||
|
c.operator = Operator::NotEquals;
|
||||||
|
assert!(!c.allows_event(&e));
|
||||||
|
// Allow if event kind is not in list for NotEquals
|
||||||
|
e.kind = 99;
|
||||||
|
c.operator = Operator::NotEquals;
|
||||||
|
assert!(c.allows_event(&e));
|
||||||
|
// Always deny if GreaterThan/LessThan for a list
|
||||||
|
c.operator = Operator::LessThan;
|
||||||
|
assert!(!c.allows_event(&e));
|
||||||
|
c.operator = Operator::GreaterThan;
|
||||||
|
assert!(!c.allows_event(&e));
|
||||||
|
}
|
||||||
|
}
|
||||||
+11
-3
@@ -17,10 +17,16 @@ pub enum Error {
|
|||||||
ConnWriteError,
|
ConnWriteError,
|
||||||
#[error("EVENT parse failed")]
|
#[error("EVENT parse failed")]
|
||||||
EventParseFailed,
|
EventParseFailed,
|
||||||
#[error("ClOSE message parse failed")]
|
#[error("CLOSE message parse failed")]
|
||||||
CloseParseFailed,
|
CloseParseFailed,
|
||||||
#[error("Event validation failed")]
|
#[error("Event invalid signature")]
|
||||||
EventInvalid,
|
EventInvalidSignature,
|
||||||
|
#[error("Event invalid id")]
|
||||||
|
EventInvalidId,
|
||||||
|
#[error("Event malformed pubkey")]
|
||||||
|
EventMalformedPubkey,
|
||||||
|
#[error("Event could not canonicalize")]
|
||||||
|
EventCouldNotCanonicalize,
|
||||||
#[error("Event too large")]
|
#[error("Event too large")]
|
||||||
EventMaxLengthError(usize),
|
EventMaxLengthError(usize),
|
||||||
#[error("Subscription identifier max length exceeded")]
|
#[error("Subscription identifier max length exceeded")]
|
||||||
@@ -50,6 +56,8 @@ pub enum Error {
|
|||||||
HyperError(hyper::Error),
|
HyperError(hyper::Error),
|
||||||
#[error("Hex encoding error")]
|
#[error("Hex encoding error")]
|
||||||
HexError(hex::FromHexError),
|
HexError(hex::FromHexError),
|
||||||
|
#[error("Delegation parse error")]
|
||||||
|
DelegationParseError,
|
||||||
#[error("Unknown/Undocumented")]
|
#[error("Unknown/Undocumented")]
|
||||||
UnknownError,
|
UnknownError,
|
||||||
}
|
}
|
||||||
|
|||||||
+125
-38
@@ -1,4 +1,5 @@
|
|||||||
//! Event parsing and validation
|
//! Event parsing and validation
|
||||||
|
use crate::delegation::validate_delegation;
|
||||||
use crate::error::Error::*;
|
use crate::error::Error::*;
|
||||||
use crate::error::Result;
|
use crate::error::Result;
|
||||||
use crate::nip05;
|
use crate::nip05;
|
||||||
@@ -26,21 +27,29 @@ pub struct EventCmd {
|
|||||||
event: Event,
|
event: Event,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl EventCmd {
|
||||||
|
pub fn event_id(&self) -> &str {
|
||||||
|
&self.event.id
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Parsed nostr event.
|
/// Parsed nostr event.
|
||||||
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
|
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
|
||||||
pub struct Event {
|
pub struct Event {
|
||||||
pub id: String,
|
pub id: String,
|
||||||
pub(crate) pubkey: String,
|
pub pubkey: String,
|
||||||
pub(crate) created_at: u64,
|
#[serde(skip)]
|
||||||
pub(crate) kind: u64,
|
pub delegated_by: Option<String>,
|
||||||
|
pub created_at: u64,
|
||||||
|
pub kind: u64,
|
||||||
#[serde(deserialize_with = "tag_from_string")]
|
#[serde(deserialize_with = "tag_from_string")]
|
||||||
// NOTE: array-of-arrays may need to be more general than a string container
|
// NOTE: array-of-arrays may need to be more general than a string container
|
||||||
pub(crate) tags: Vec<Vec<String>>,
|
pub tags: Vec<Vec<String>>,
|
||||||
pub(crate) content: String,
|
pub content: String,
|
||||||
pub(crate) sig: String,
|
pub sig: String,
|
||||||
// Optimization for tag search, built on demand.
|
// Optimization for tag search, built on demand.
|
||||||
#[serde(skip)]
|
#[serde(skip)]
|
||||||
pub(crate) tagidx: Option<HashMap<char, HashSet<String>>>,
|
pub tagidx: Option<HashMap<char, HashSet<String>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Simple tag type for array of array of strings.
|
/// Simple tag type for array of array of strings.
|
||||||
@@ -80,17 +89,33 @@ impl From<EventCmd> for Result<Event> {
|
|||||||
// ensure command is correct
|
// ensure command is correct
|
||||||
if ec.cmd != "EVENT" {
|
if ec.cmd != "EVENT" {
|
||||||
Err(CommandUnknownError)
|
Err(CommandUnknownError)
|
||||||
} else if ec.event.is_valid() {
|
|
||||||
let mut e = ec.event;
|
|
||||||
e.build_index();
|
|
||||||
Ok(e)
|
|
||||||
} else {
|
} else {
|
||||||
Err(EventInvalid)
|
ec.event.validate().map(|_| {
|
||||||
|
let mut e = ec.event;
|
||||||
|
e.build_index();
|
||||||
|
e.update_delegation();
|
||||||
|
e
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Event {
|
impl Event {
|
||||||
|
#[cfg(test)]
|
||||||
|
pub fn simple_event() -> Event {
|
||||||
|
Event {
|
||||||
|
id: "0".to_owned(),
|
||||||
|
pubkey: "0".to_owned(),
|
||||||
|
delegated_by: None,
|
||||||
|
created_at: 0,
|
||||||
|
kind: 0,
|
||||||
|
tags: vec![],
|
||||||
|
content: "".to_owned(),
|
||||||
|
sig: "0".to_owned(),
|
||||||
|
tagidx: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn is_kind_metadata(&self) -> bool {
|
pub fn is_kind_metadata(&self) -> bool {
|
||||||
self.kind == 0
|
self.kind == 0
|
||||||
}
|
}
|
||||||
@@ -110,6 +135,50 @@ impl Event {
|
|||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// is this event delegated (properly)?
|
||||||
|
// does the signature match, and are conditions valid?
|
||||||
|
// if so, return an alternate author for the event
|
||||||
|
pub fn delegated_author(&self) -> Option<String> {
|
||||||
|
// is there a delegation tag?
|
||||||
|
let delegation_tag: Vec<String> = self
|
||||||
|
.tags
|
||||||
|
.iter()
|
||||||
|
.filter(|x| x.len() == 4)
|
||||||
|
.filter(|x| x.get(0).unwrap() == "delegation")
|
||||||
|
.take(1)
|
||||||
|
.next()?
|
||||||
|
.to_vec(); // get first tag
|
||||||
|
|
||||||
|
//let delegation_tag = self.tag_values_by_name("delegation");
|
||||||
|
// delegation tags should have exactly 3 elements after the name (pubkey, condition, sig)
|
||||||
|
// the event is signed by the delagatee
|
||||||
|
let delegatee = &self.pubkey;
|
||||||
|
// the delegation tag references the claimed delagator
|
||||||
|
let delegator: &str = delegation_tag.get(1)?;
|
||||||
|
let querystr: &str = delegation_tag.get(2)?;
|
||||||
|
let sig: &str = delegation_tag.get(3)?;
|
||||||
|
|
||||||
|
// attempt to get a condition query; this requires the delegation to have a valid signature.
|
||||||
|
if let Some(cond_query) = validate_delegation(delegator, delegatee, querystr, sig) {
|
||||||
|
// The signature was valid, now we ensure the delegation
|
||||||
|
// condition is valid for this event:
|
||||||
|
if cond_query.allows_event(self) {
|
||||||
|
// since this is allowed, we will provide the delegatee
|
||||||
|
Some(delegator.into())
|
||||||
|
} else {
|
||||||
|
debug!("an event failed to satisfy delegation conditions");
|
||||||
|
None
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
debug!("event had had invalid delegation signature");
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Update delegation status
|
||||||
|
fn update_delegation(&mut self) {
|
||||||
|
self.delegated_by = self.delegated_author();
|
||||||
|
}
|
||||||
/// Build an event tag index
|
/// Build an event tag index
|
||||||
fn build_index(&mut self) {
|
fn build_index(&mut self) {
|
||||||
// if there are no tags; just leave the index as None
|
// if there are no tags; just leave the index as None
|
||||||
@@ -145,7 +214,7 @@ impl Event {
|
|||||||
self.pubkey.chars().take(8).collect()
|
self.pubkey.chars().take(8).collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Retrieve tag values
|
/// Retrieve tag initial values across all tags matching the name
|
||||||
pub fn tag_values_by_name(&self, tag_name: &str) -> Vec<String> {
|
pub fn tag_values_by_name(&self, tag_name: &str) -> Vec<String> {
|
||||||
self.tags
|
self.tags
|
||||||
.iter()
|
.iter()
|
||||||
@@ -172,7 +241,7 @@ impl Event {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Check if this event has a valid signature.
|
/// Check if this event has a valid signature.
|
||||||
fn is_valid(&self) -> bool {
|
pub fn validate(&self) -> Result<()> {
|
||||||
// TODO: return a Result with a reason for invalid events
|
// TODO: return a Result with a reason for invalid events
|
||||||
// validation is performed by:
|
// validation is performed by:
|
||||||
// * parsing JSON string into event fields
|
// * parsing JSON string into event fields
|
||||||
@@ -181,8 +250,8 @@ impl Event {
|
|||||||
// * serialize with no spaces/newlines
|
// * serialize with no spaces/newlines
|
||||||
let c_opt = self.to_canonical();
|
let c_opt = self.to_canonical();
|
||||||
if c_opt.is_none() {
|
if c_opt.is_none() {
|
||||||
debug!("event could not be canonicalized");
|
debug!("could not canonicalize");
|
||||||
return false;
|
return Err(EventCouldNotCanonicalize);
|
||||||
}
|
}
|
||||||
let c = c_opt.unwrap();
|
let c = c_opt.unwrap();
|
||||||
// * compute the sha256sum.
|
// * compute the sha256sum.
|
||||||
@@ -191,21 +260,21 @@ impl Event {
|
|||||||
// * ensure the id matches the computed sha256sum.
|
// * ensure the id matches the computed sha256sum.
|
||||||
if self.id != hex_digest {
|
if self.id != hex_digest {
|
||||||
debug!("event id does not match digest");
|
debug!("event id does not match digest");
|
||||||
return false;
|
return Err(EventInvalidId);
|
||||||
}
|
}
|
||||||
// * validate the message digest (sig) using the pubkey & computed sha256 message hash.
|
// * validate the message digest (sig) using the pubkey & computed sha256 message hash.
|
||||||
let sig = schnorr::Signature::from_str(&self.sig).unwrap();
|
let sig = schnorr::Signature::from_str(&self.sig).unwrap();
|
||||||
if let Ok(msg) = secp256k1::Message::from_slice(digest.as_ref()) {
|
if let Ok(msg) = secp256k1::Message::from_slice(digest.as_ref()) {
|
||||||
if let Ok(pubkey) = XOnlyPublicKey::from_str(&self.pubkey) {
|
if let Ok(pubkey) = XOnlyPublicKey::from_str(&self.pubkey) {
|
||||||
let verify = SECP.verify_schnorr(&sig, &msg, &pubkey);
|
SECP.verify_schnorr(&sig, &msg, &pubkey)
|
||||||
matches!(verify, Ok(()))
|
.map_err(|_| EventInvalidSignature)
|
||||||
} else {
|
} else {
|
||||||
debug!("client sent malformed pubkey");
|
debug!("client sent malformed pubkey");
|
||||||
false
|
Err(EventMalformedPubkey)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
info!("error converting digest to secp256k1 message");
|
info!("error converting digest to secp256k1 message");
|
||||||
false
|
Err(EventInvalidSignature)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -265,30 +334,18 @@ impl Event {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
fn simple_event() -> Event {
|
|
||||||
Event {
|
|
||||||
id: "0".to_owned(),
|
|
||||||
pubkey: "0".to_owned(),
|
|
||||||
created_at: 0,
|
|
||||||
kind: 0,
|
|
||||||
tags: vec![],
|
|
||||||
content: "".to_owned(),
|
|
||||||
sig: "0".to_owned(),
|
|
||||||
tagidx: None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn event_creation() {
|
fn event_creation() {
|
||||||
// create an event
|
// create an event
|
||||||
let event = simple_event();
|
let event = Event::simple_event();
|
||||||
assert_eq!(event.id, "0");
|
assert_eq!(event.id, "0");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn event_serialize() -> Result<()> {
|
fn event_serialize() -> Result<()> {
|
||||||
// serialize an event to JSON string
|
// serialize an event to JSON string
|
||||||
let event = simple_event();
|
let event = Event::simple_event();
|
||||||
let j = serde_json::to_string(&event)?;
|
let j = serde_json::to_string(&event)?;
|
||||||
assert_eq!(j, "{\"id\":\"0\",\"pubkey\":\"0\",\"created_at\":0,\"kind\":0,\"tags\":[],\"content\":\"\",\"sig\":\"0\"}");
|
assert_eq!(j, "{\"id\":\"0\",\"pubkey\":\"0\",\"created_at\":0,\"kind\":0,\"tags\":[],\"content\":\"\",\"sig\":\"0\"}");
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -296,14 +353,14 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn empty_event_tag_match() {
|
fn empty_event_tag_match() {
|
||||||
let event = simple_event();
|
let event = Event::simple_event();
|
||||||
assert!(!event
|
assert!(!event
|
||||||
.generic_tag_val_intersect('e', &HashSet::from(["foo".to_owned(), "bar".to_owned()])));
|
.generic_tag_val_intersect('e', &HashSet::from(["foo".to_owned(), "bar".to_owned()])));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn single_event_tag_match() {
|
fn single_event_tag_match() {
|
||||||
let mut event = simple_event();
|
let mut event = Event::simple_event();
|
||||||
event.tags = vec![vec!["e".to_owned(), "foo".to_owned()]];
|
event.tags = vec![vec!["e".to_owned(), "foo".to_owned()]];
|
||||||
event.build_index();
|
event.build_index();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
@@ -318,7 +375,7 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn event_tags_serialize() -> Result<()> {
|
fn event_tags_serialize() -> Result<()> {
|
||||||
// serialize an event with tags to JSON string
|
// serialize an event with tags to JSON string
|
||||||
let mut event = simple_event();
|
let mut event = Event::simple_event();
|
||||||
event.tags = vec![
|
event.tags = vec![
|
||||||
vec![
|
vec![
|
||||||
"e".to_owned(),
|
"e".to_owned(),
|
||||||
@@ -350,6 +407,7 @@ mod tests {
|
|||||||
let e = Event {
|
let e = Event {
|
||||||
id: "999".to_owned(),
|
id: "999".to_owned(),
|
||||||
pubkey: "012345".to_owned(),
|
pubkey: "012345".to_owned(),
|
||||||
|
delegated_by: None,
|
||||||
created_at: 501234,
|
created_at: 501234,
|
||||||
kind: 1,
|
kind: 1,
|
||||||
tags: vec![],
|
tags: vec![],
|
||||||
@@ -367,6 +425,7 @@ mod tests {
|
|||||||
let e = Event {
|
let e = Event {
|
||||||
id: "999".to_owned(),
|
id: "999".to_owned(),
|
||||||
pubkey: "012345".to_owned(),
|
pubkey: "012345".to_owned(),
|
||||||
|
delegated_by: None,
|
||||||
created_at: 501234,
|
created_at: 501234,
|
||||||
kind: 1,
|
kind: 1,
|
||||||
tags: vec![
|
tags: vec![
|
||||||
@@ -388,11 +447,39 @@ mod tests {
|
|||||||
assert_eq!(v, vec!["foo", "bar", "baz"]);
|
assert_eq!(v, vec!["foo", "bar", "baz"]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn event_no_tag_select() {
|
||||||
|
let e = Event {
|
||||||
|
id: "999".to_owned(),
|
||||||
|
pubkey: "012345".to_owned(),
|
||||||
|
delegated_by: None,
|
||||||
|
created_at: 501234,
|
||||||
|
kind: 1,
|
||||||
|
tags: vec![
|
||||||
|
vec!["j".to_owned(), "abc".to_owned()],
|
||||||
|
vec!["e".to_owned(), "foo".to_owned()],
|
||||||
|
vec!["e".to_owned(), "baz".to_owned()],
|
||||||
|
vec![
|
||||||
|
"p".to_owned(),
|
||||||
|
"aaaa".to_owned(),
|
||||||
|
"ws://example.com".to_owned(),
|
||||||
|
],
|
||||||
|
],
|
||||||
|
content: "this is a test".to_owned(),
|
||||||
|
sig: "abcde".to_owned(),
|
||||||
|
tagidx: None,
|
||||||
|
};
|
||||||
|
let v = e.tag_values_by_name("x");
|
||||||
|
// asking for tags that don't exist just returns zero-length vector
|
||||||
|
assert_eq!(v.len(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn event_canonical_with_tags() {
|
fn event_canonical_with_tags() {
|
||||||
let e = Event {
|
let e = Event {
|
||||||
id: "999".to_owned(),
|
id: "999".to_owned(),
|
||||||
pubkey: "012345".to_owned(),
|
pubkey: "012345".to_owned(),
|
||||||
|
delegated_by: None,
|
||||||
created_at: 501234,
|
created_at: 501234,
|
||||||
kind: 1,
|
kind: 1,
|
||||||
tags: vec![
|
tags: vec![
|
||||||
|
|||||||
+1
-1
@@ -3,7 +3,7 @@ use crate::utils::is_hex;
|
|||||||
use hex;
|
use hex;
|
||||||
|
|
||||||
/// Types of hexadecimal queries.
|
/// Types of hexadecimal queries.
|
||||||
#[derive(PartialEq, Eq, Debug, Clone)]
|
#[derive(PartialEq, Eq, PartialOrd, Ord, Debug, Clone)]
|
||||||
pub enum HexSearch {
|
pub enum HexSearch {
|
||||||
// when no range is needed, exact 32-byte
|
// when no range is needed, exact 32-byte
|
||||||
Exact(Vec<u8>),
|
Exact(Vec<u8>),
|
||||||
|
|||||||
+1
-1
@@ -35,7 +35,7 @@ impl From<config::Info> for RelayInfo {
|
|||||||
description: i.description,
|
description: i.description,
|
||||||
pubkey: i.pubkey,
|
pubkey: i.pubkey,
|
||||||
contact: i.contact,
|
contact: i.contact,
|
||||||
supported_nips: Some(vec![1, 2, 9, 11, 12, 15, 16, 22]),
|
supported_nips: Some(vec![1, 2, 9, 11, 12, 15, 16, 20, 22, 26]),
|
||||||
software: Some("https://git.sr.ht/~gheartsfield/nostr-rs-relay".to_owned()),
|
software: Some("https://git.sr.ht/~gheartsfield/nostr-rs-relay".to_owned()),
|
||||||
version: CARGO_PKG_VERSION.map(|x| x.to_owned()),
|
version: CARGO_PKG_VERSION.map(|x| x.to_owned()),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,11 +2,13 @@ pub mod close;
|
|||||||
pub mod config;
|
pub mod config;
|
||||||
pub mod conn;
|
pub mod conn;
|
||||||
pub mod db;
|
pub mod db;
|
||||||
|
pub mod delegation;
|
||||||
pub mod error;
|
pub mod error;
|
||||||
pub mod event;
|
pub mod event;
|
||||||
pub mod hexrange;
|
pub mod hexrange;
|
||||||
pub mod info;
|
pub mod info;
|
||||||
pub mod nip05;
|
pub mod nip05;
|
||||||
|
pub mod notice;
|
||||||
pub mod schema;
|
pub mod schema;
|
||||||
pub mod subscription;
|
pub mod subscription;
|
||||||
pub mod utils;
|
pub mod utils;
|
||||||
|
|||||||
+1
-1
@@ -34,11 +34,11 @@ fn main() {
|
|||||||
// enable tracing with tokio-console
|
// enable tracing with tokio-console
|
||||||
ConsoleLayer::builder().with_default_env().init();
|
ConsoleLayer::builder().with_default_env().init();
|
||||||
}
|
}
|
||||||
|
|
||||||
// update with database location
|
// update with database location
|
||||||
if let Some(db) = db_dir {
|
if let Some(db) = db_dir {
|
||||||
settings.database.data_directory = db;
|
settings.database.data_directory = db;
|
||||||
}
|
}
|
||||||
|
|
||||||
let (_, ctrl_rx): (MpscSender<()>, MpscReceiver<()>) = syncmpsc::channel();
|
let (_, ctrl_rx): (MpscSender<()>, MpscReceiver<()>) = syncmpsc::channel();
|
||||||
// run this in a new thread
|
// run this in a new thread
|
||||||
let handle = thread::spawn(|| {
|
let handle = thread::spawn(|| {
|
||||||
|
|||||||
+2
-2
@@ -517,7 +517,7 @@ impl Verifier {
|
|||||||
Ok(updated) => {
|
Ok(updated) => {
|
||||||
if updated != 0 {
|
if updated != 0 {
|
||||||
info!(
|
info!(
|
||||||
"persisted event: {:?} in {:?}",
|
"persisted event (new verified pubkey): {:?} in {:?}",
|
||||||
event.get_event_id_prefix(),
|
event.get_event_id_prefix(),
|
||||||
start.elapsed()
|
start.elapsed()
|
||||||
);
|
);
|
||||||
@@ -721,7 +721,7 @@ pub fn query_oldest_user_verification(
|
|||||||
earliest: u64,
|
earliest: u64,
|
||||||
) -> Result<VerificationRecord> {
|
) -> Result<VerificationRecord> {
|
||||||
let tx = conn.transaction()?;
|
let tx = conn.transaction()?;
|
||||||
let query = "SELECT v.id, v.name, e.event_hash, e.author, e.created_at, v.verified_at, v.failed_at, v.failure_count FROM user_verification v LEFT JOIN event e ON e.id=v.metadata_event WHERE (v.verified_at < ? OR v.verified_at IS NULL) AND (v.failed_at < ? OR v.failed_at IS NULL) ORDER BY v.verified_at ASC, v.failed_at ASC LIMIT 1;";
|
let query = "SELECT v.id, v.name, e.event_hash, e.author, e.created_at, v.verified_at, v.failed_at, v.failure_count FROM user_verification v INNER JOIN event e ON e.id=v.metadata_event WHERE (v.verified_at < ? OR v.verified_at IS NULL) AND (v.failed_at < ? OR v.failed_at IS NULL) ORDER BY v.verified_at ASC, v.failed_at ASC LIMIT 1;";
|
||||||
let mut stmt = tx.prepare_cached(query)?;
|
let mut stmt = tx.prepare_cached(query)?;
|
||||||
let fields = stmt.query_row(params![earliest, earliest], |r| {
|
let fields = stmt.query_row(params![earliest, earliest], |r| {
|
||||||
let rowid: u64 = r.get(0)?;
|
let rowid: u64 = r.get(0)?;
|
||||||
|
|||||||
@@ -0,0 +1,86 @@
|
|||||||
|
pub enum EventResultStatus {
|
||||||
|
Saved,
|
||||||
|
Duplicate,
|
||||||
|
Invalid,
|
||||||
|
Blocked,
|
||||||
|
RateLimited,
|
||||||
|
Error,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct EventResult {
|
||||||
|
pub id: String,
|
||||||
|
pub msg: String,
|
||||||
|
pub status: EventResultStatus,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum Notice {
|
||||||
|
Message(String),
|
||||||
|
EventResult(EventResult),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl EventResultStatus {
|
||||||
|
pub fn to_bool(&self) -> bool {
|
||||||
|
match self {
|
||||||
|
Self::Saved => true,
|
||||||
|
Self::Duplicate => true,
|
||||||
|
Self::Invalid => false,
|
||||||
|
Self::Blocked => false,
|
||||||
|
Self::RateLimited => false,
|
||||||
|
Self::Error => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn prefix(&self) -> &'static str {
|
||||||
|
match self {
|
||||||
|
Self::Saved => "saved",
|
||||||
|
Self::Duplicate => "duplicate",
|
||||||
|
Self::Invalid => "invalid",
|
||||||
|
Self::Blocked => "blocked",
|
||||||
|
Self::RateLimited => "rate-limited",
|
||||||
|
Self::Error => "error",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Notice {
|
||||||
|
//pub fn err(err: error::Error, id: String) -> Notice {
|
||||||
|
// Notice::err_msg(format!("{}", err), id)
|
||||||
|
//}
|
||||||
|
|
||||||
|
pub fn message(msg: String) -> Notice {
|
||||||
|
Notice::Message(msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn prefixed(id: String, msg: &str, status: EventResultStatus) -> Notice {
|
||||||
|
let msg = format!("{}: {}", status.prefix(), msg);
|
||||||
|
Notice::EventResult(EventResult { id, msg, status })
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn invalid(id: String, msg: &str) -> Notice {
|
||||||
|
Notice::prefixed(id, msg, EventResultStatus::Invalid)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn blocked(id: String, msg: &str) -> Notice {
|
||||||
|
Notice::prefixed(id, msg, EventResultStatus::Blocked)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn rate_limited(id: String, msg: &str) -> Notice {
|
||||||
|
Notice::prefixed(id, msg, EventResultStatus::RateLimited)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn duplicate(id: String) -> Notice {
|
||||||
|
Notice::prefixed(id, "", EventResultStatus::Duplicate)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn error(id: String, msg: &str) -> Notice {
|
||||||
|
Notice::prefixed(id, msg, EventResultStatus::Error)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn saved(id: String) -> Notice {
|
||||||
|
Notice::EventResult(EventResult {
|
||||||
|
id,
|
||||||
|
msg: "".into(),
|
||||||
|
status: EventResultStatus::Saved,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
+126
-5
@@ -16,11 +16,11 @@ pub const STARTUP_SQL: &str = r##"
|
|||||||
PRAGMA main.synchronous=NORMAL;
|
PRAGMA main.synchronous=NORMAL;
|
||||||
PRAGMA foreign_keys = ON;
|
PRAGMA foreign_keys = ON;
|
||||||
PRAGMA journal_size_limit=32768;
|
PRAGMA journal_size_limit=32768;
|
||||||
pragma mmap_size = 536870912; -- 512MB of mmap
|
pragma mmap_size = 17179869184; -- cap mmap at 16GB
|
||||||
"##;
|
"##;
|
||||||
|
|
||||||
/// Latest database version
|
/// Latest database version
|
||||||
pub const DB_VERSION: usize = 6;
|
pub const DB_VERSION: usize = 11;
|
||||||
|
|
||||||
/// Schema definition
|
/// Schema definition
|
||||||
const INIT_SQL: &str = formatcp!(
|
const INIT_SQL: &str = formatcp!(
|
||||||
@@ -40,6 +40,7 @@ event_hash BLOB NOT NULL, -- 4-byte hash
|
|||||||
first_seen INTEGER NOT NULL, -- when the event was first seen (not authored!) (seconds since 1970)
|
first_seen INTEGER NOT NULL, -- when the event was first seen (not authored!) (seconds since 1970)
|
||||||
created_at INTEGER NOT NULL, -- when the event was authored
|
created_at INTEGER NOT NULL, -- when the event was authored
|
||||||
author BLOB NOT NULL, -- author pubkey
|
author BLOB NOT NULL, -- author pubkey
|
||||||
|
delegated_by BLOB, -- delegator pubkey (NIP-26)
|
||||||
kind INTEGER NOT NULL, -- event kind
|
kind INTEGER NOT NULL, -- event kind
|
||||||
hidden INTEGER, -- relevant for queries
|
hidden INTEGER, -- relevant for queries
|
||||||
content TEXT NOT NULL -- serialized json of event object
|
content TEXT NOT NULL -- serialized json of event object
|
||||||
@@ -47,9 +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 kind_index ON event(kind);
|
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 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
|
||||||
@@ -65,6 +67,8 @@ FOREIGN KEY(event_id) REFERENCES event(id) ON UPDATE CASCADE ON DELETE CASCADE
|
|||||||
);
|
);
|
||||||
CREATE INDEX IF NOT EXISTS tag_val_index ON tag(value);
|
CREATE INDEX IF NOT EXISTS tag_val_index ON tag(value);
|
||||||
CREATE INDEX IF NOT EXISTS tag_val_hex_index ON tag(value_hex);
|
CREATE INDEX IF NOT EXISTS tag_val_hex_index ON tag(value_hex);
|
||||||
|
CREATE INDEX IF NOT EXISTS tag_composite_index ON tag(event_id,name,value_hex,value);
|
||||||
|
CREATE INDEX IF NOT EXISTS tag_name_eid_index ON tag(name,event_id,value_hex);
|
||||||
|
|
||||||
-- NIP-05 User Validation
|
-- NIP-05 User Validation
|
||||||
CREATE TABLE IF NOT EXISTS user_verification (
|
CREATE TABLE IF NOT EXISTS user_verification (
|
||||||
@@ -152,6 +156,22 @@ pub fn upgrade_db(conn: &mut PooledConnection) -> Result<()> {
|
|||||||
if curr_version == 5 {
|
if curr_version == 5 {
|
||||||
curr_version = mig_5_to_6(conn)?;
|
curr_version = mig_5_to_6(conn)?;
|
||||||
}
|
}
|
||||||
|
if curr_version == 6 {
|
||||||
|
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 == 9 {
|
||||||
|
curr_version = mig_9_to_10(conn)?;
|
||||||
|
}
|
||||||
|
if curr_version == 10 {
|
||||||
|
curr_version = mig_10_to_11(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{}.",
|
||||||
@@ -327,7 +347,7 @@ fn mig_5_to_6(conn: &mut PooledConnection) -> Result<usize> {
|
|||||||
if (tagval.len() % 2 == 0) && is_lower_hex(tagval) {
|
if (tagval.len() % 2 == 0) && is_lower_hex(tagval) {
|
||||||
tx.execute(
|
tx.execute(
|
||||||
"INSERT INTO tag (event_id, name, value_hex) VALUES (?1, ?2, ?3);",
|
"INSERT INTO tag (event_id, name, value_hex) VALUES (?1, ?2, ?3);",
|
||||||
params![event_id, tagname, hex::decode(&tagval).ok()],
|
params![event_id, tagname, hex::decode(tagval).ok()],
|
||||||
)?;
|
)?;
|
||||||
} else {
|
} else {
|
||||||
// otherwise, insert as text
|
// otherwise, insert as text
|
||||||
@@ -348,3 +368,104 @@ fn mig_5_to_6(conn: &mut PooledConnection) -> Result<usize> {
|
|||||||
info!("vacuumed DB after tags rebuild in {:?}", start.elapsed());
|
info!("vacuumed DB after tags rebuild in {:?}", start.elapsed());
|
||||||
Ok(6)
|
Ok(6)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn mig_6_to_7(conn: &mut PooledConnection) -> Result<usize> {
|
||||||
|
info!("database schema needs update from 6->7");
|
||||||
|
// only change is adding a hidden column to events.
|
||||||
|
let upgrade_sql = r##"
|
||||||
|
ALTER TABLE event ADD delegated_by BLOB;
|
||||||
|
CREATE INDEX IF NOT EXISTS delegated_by_index ON event(delegated_by);
|
||||||
|
PRAGMA user_version = 7;
|
||||||
|
"##;
|
||||||
|
match conn.execute_batch(upgrade_sql) {
|
||||||
|
Ok(()) => {
|
||||||
|
info!("database schema upgraded v6 -> v7");
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
error!("update failed: {}", err);
|
||||||
|
panic!("database could not be upgraded");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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 = 9;
|
||||||
|
"##;
|
||||||
|
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(9)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn mig_9_to_10(conn: &mut PooledConnection) -> Result<usize> {
|
||||||
|
info!("database schema needs update from 9->10");
|
||||||
|
// Those old indexes were actually helpful...
|
||||||
|
let upgrade_sql = r##"
|
||||||
|
CREATE INDEX IF NOT EXISTS tag_composite_index ON tag(event_id,name,value_hex,value);
|
||||||
|
PRAGMA user_version = 10;
|
||||||
|
"##;
|
||||||
|
match conn.execute_batch(upgrade_sql) {
|
||||||
|
Ok(()) => {
|
||||||
|
info!("database schema upgraded v9 -> v10");
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
error!("update failed: {}", err);
|
||||||
|
panic!("database could not be upgraded");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(10)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn mig_10_to_11(conn: &mut PooledConnection) -> Result<usize> {
|
||||||
|
info!("database schema needs update from 10->11");
|
||||||
|
// Those old indexes were actually helpful...
|
||||||
|
let upgrade_sql = r##"
|
||||||
|
CREATE INDEX IF NOT EXISTS tag_name_eid_index ON tag(name,event_id,value_hex);
|
||||||
|
reindex;
|
||||||
|
pragma optimize;
|
||||||
|
PRAGMA user_version = 11;
|
||||||
|
"##;
|
||||||
|
match conn.execute_batch(upgrade_sql) {
|
||||||
|
Ok(()) => {
|
||||||
|
info!("database schema upgraded v10 -> v11");
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
error!("update failed: {}", err);
|
||||||
|
panic!("database could not be upgraded");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(11)
|
||||||
|
}
|
||||||
|
|||||||
+172
-55
@@ -10,21 +10,26 @@ use crate::event::Event;
|
|||||||
use crate::event::EventCmd;
|
use crate::event::EventCmd;
|
||||||
use crate::info::RelayInfo;
|
use crate::info::RelayInfo;
|
||||||
use crate::nip05;
|
use crate::nip05;
|
||||||
|
use crate::notice::Notice;
|
||||||
use crate::subscription::Subscription;
|
use crate::subscription::Subscription;
|
||||||
use futures::SinkExt;
|
use futures::SinkExt;
|
||||||
use futures::StreamExt;
|
use futures::StreamExt;
|
||||||
|
use governor::{Jitter, Quota, RateLimiter};
|
||||||
|
use http::header::HeaderMap;
|
||||||
use hyper::header::ACCEPT;
|
use hyper::header::ACCEPT;
|
||||||
use hyper::service::{make_service_fn, service_fn};
|
use hyper::service::{make_service_fn, service_fn};
|
||||||
use hyper::upgrade::Upgraded;
|
use hyper::upgrade::Upgraded;
|
||||||
use hyper::{
|
use hyper::{
|
||||||
header, server::conn::AddrStream, upgrade, Body, Request, Response, Server, StatusCode,
|
header, server::conn::AddrStream, upgrade, Body, Request, Response, Server, StatusCode,
|
||||||
};
|
};
|
||||||
|
use rusqlite::OpenFlags;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::convert::Infallible;
|
use std::convert::Infallible;
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
use std::sync::atomic::Ordering;
|
||||||
use std::sync::mpsc::Receiver as MpscReceiver;
|
use std::sync::mpsc::Receiver as MpscReceiver;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
@@ -84,11 +89,34 @@ async fn handle_web_request(
|
|||||||
Some(config),
|
Some(config),
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
|
let origin = get_header_string("origin", request.headers());
|
||||||
|
let user_agent = get_header_string("user-agent", request.headers());
|
||||||
|
// determine the remote IP from headers if the exist
|
||||||
|
let header_ip = settings
|
||||||
|
.network
|
||||||
|
.remote_ip_header
|
||||||
|
.as_ref()
|
||||||
|
.and_then(|x| get_header_string(x, request.headers()));
|
||||||
|
// use the socket addr as a backup
|
||||||
|
let remote_ip =
|
||||||
|
header_ip.unwrap_or_else(|| remote_addr.ip().to_string());
|
||||||
|
let client_info = ClientInfo {
|
||||||
|
remote_ip,
|
||||||
|
user_agent,
|
||||||
|
origin,
|
||||||
|
};
|
||||||
|
// spawn a nostr server with our websocket
|
||||||
tokio::spawn(nostr_server(
|
tokio::spawn(nostr_server(
|
||||||
pool, settings, ws_stream, broadcast, event_tx, shutdown,
|
pool,
|
||||||
|
client_info,
|
||||||
|
settings,
|
||||||
|
ws_stream,
|
||||||
|
broadcast,
|
||||||
|
event_tx,
|
||||||
|
shutdown,
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
// todo: trace, don't print...
|
||||||
Err(e) => println!(
|
Err(e) => println!(
|
||||||
"error when trying to upgrade connection \
|
"error when trying to upgrade connection \
|
||||||
from address {} to websocket connection. \
|
from address {} to websocket connection. \
|
||||||
@@ -148,6 +176,12 @@ async fn handle_web_request(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_header_string(header: &str, headers: &HeaderMap) -> Option<String> {
|
||||||
|
headers
|
||||||
|
.get(header)
|
||||||
|
.and_then(|x| x.to_str().ok().map(|x| x.to_string()))
|
||||||
|
}
|
||||||
|
|
||||||
// return on a control-c or internally requested shutdown signal
|
// return on a control-c or internally requested shutdown signal
|
||||||
async fn ctrl_c_or_signal(mut shutdown_signal: Receiver<()>) {
|
async fn ctrl_c_or_signal(mut shutdown_signal: Receiver<()>) {
|
||||||
let mut term_signal = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())
|
let mut term_signal = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())
|
||||||
@@ -167,7 +201,6 @@ async fn ctrl_c_or_signal(mut shutdown_signal: Receiver<()>) {
|
|||||||
info!("Shutting down webserver due to SIGTERM");
|
info!("Shutting down webserver due to SIGTERM");
|
||||||
break;
|
break;
|
||||||
},
|
},
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -215,7 +248,20 @@ pub fn start_server(settings: Settings, shutdown_rx: MpscReceiver<()>) -> Result
|
|||||||
// configure tokio runtime
|
// configure tokio runtime
|
||||||
let rt = Builder::new_multi_thread()
|
let rt = Builder::new_multi_thread()
|
||||||
.enable_all()
|
.enable_all()
|
||||||
.thread_name("tokio-ws")
|
.thread_name_fn(|| {
|
||||||
|
// give each thread a unique numeric name
|
||||||
|
static ATOMIC_ID: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
|
||||||
|
let id = ATOMIC_ID.fetch_add(1,Ordering::SeqCst);
|
||||||
|
format!("tokio-ws-{}", id)
|
||||||
|
})
|
||||||
|
// limit concurrent SQLite blocking threads
|
||||||
|
.max_blocking_threads(settings.limits.max_blocking_threads)
|
||||||
|
.on_thread_start(|| {
|
||||||
|
trace!("started new thread: {:?}", std::thread::current().name());
|
||||||
|
})
|
||||||
|
.on_thread_stop(|| {
|
||||||
|
trace!("stopped thread: {:?}", std::thread::current().name());
|
||||||
|
})
|
||||||
.build()
|
.build()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
// start tokio
|
// start tokio
|
||||||
@@ -273,6 +319,18 @@ pub fn start_server(settings: Settings, shutdown_rx: MpscReceiver<()>) -> Result
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// build a connection pool for DB maintenance
|
||||||
|
let maintenance_pool = db::build_pool(
|
||||||
|
"maintenance writer",
|
||||||
|
&settings,
|
||||||
|
OpenFlags::SQLITE_OPEN_READ_WRITE | OpenFlags::SQLITE_OPEN_CREATE,
|
||||||
|
1,
|
||||||
|
2,
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
db::db_optimize(maintenance_pool.clone()).await;
|
||||||
|
db::db_checkpoint(maintenance_pool).await;
|
||||||
|
|
||||||
// listen for (external to tokio) shutdown request
|
// listen for (external to tokio) shutdown request
|
||||||
let controlled_shutdown = invoke_shutdown.clone();
|
let controlled_shutdown = invoke_shutdown.clone();
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
@@ -283,6 +341,7 @@ pub fn start_server(settings: Settings, shutdown_rx: MpscReceiver<()>) -> Result
|
|||||||
controlled_shutdown.send(()).ok();
|
controlled_shutdown.send(()).ok();
|
||||||
}
|
}
|
||||||
Err(std::sync::mpsc::RecvError) => {
|
Err(std::sync::mpsc::RecvError) => {
|
||||||
|
// FIXME: spurious error on startup?
|
||||||
debug!("shutdown requestor is disconnected");
|
debug!("shutdown requestor is disconnected");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -301,12 +360,15 @@ pub fn start_server(settings: Settings, shutdown_rx: MpscReceiver<()>) -> Result
|
|||||||
let pool = db::build_pool(
|
let pool = db::build_pool(
|
||||||
"client query",
|
"client query",
|
||||||
&settings,
|
&settings,
|
||||||
rusqlite::OpenFlags::SQLITE_OPEN_READ_ONLY
|
rusqlite::OpenFlags::SQLITE_OPEN_READ_ONLY,
|
||||||
| rusqlite::OpenFlags::SQLITE_OPEN_SHARED_CACHE,
|
|
||||||
db_min_conn,
|
db_min_conn,
|
||||||
db_max_conn,
|
db_max_conn,
|
||||||
true,
|
true,
|
||||||
);
|
);
|
||||||
|
// spawn a task to check the pool size.
|
||||||
|
let pool_monitor = pool.clone();
|
||||||
|
tokio::spawn(async move {db::monitor_pool("reader", pool_monitor).await;});
|
||||||
|
|
||||||
// A `Service` is needed for every connection, so this
|
// A `Service` is needed for every connection, so this
|
||||||
// creates one from our `handle_request` function.
|
// creates one from our `handle_request` function.
|
||||||
let make_svc = make_service_fn(|conn: &AddrStream| {
|
let make_svc = make_service_fn(|conn: &AddrStream| {
|
||||||
@@ -378,36 +440,66 @@ fn convert_to_msg(msg: String, max_bytes: Option<usize>) -> Result<NostrMessage>
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Turn a string into a NOTICE message ready to send over a WebSocket
|
/// Turn a string into a NOTICE message ready to send over a WebSocket
|
||||||
fn make_notice_message(msg: &str) -> Message {
|
fn make_notice_message(notice: Notice) -> Message {
|
||||||
Message::text(json!(["NOTICE", msg]).to_string())
|
let json = match notice {
|
||||||
|
Notice::Message(ref msg) => json!(["NOTICE", msg]),
|
||||||
|
Notice::EventResult(ref res) => json!(["OK", res.id, res.status.to_bool(), res.msg]),
|
||||||
|
};
|
||||||
|
|
||||||
|
Message::text(json.to_string())
|
||||||
|
}
|
||||||
|
|
||||||
|
struct ClientInfo {
|
||||||
|
remote_ip: String,
|
||||||
|
user_agent: Option<String>,
|
||||||
|
origin: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Handle new client connections. This runs through an event loop
|
/// Handle new client connections. This runs through an event loop
|
||||||
/// for all client communication.
|
/// for all client communication.
|
||||||
async fn nostr_server(
|
async fn nostr_server(
|
||||||
pool: db::SqlitePool,
|
pool: db::SqlitePool,
|
||||||
|
client_info: ClientInfo,
|
||||||
settings: Settings,
|
settings: Settings,
|
||||||
mut ws_stream: WebSocketStream<Upgraded>,
|
mut ws_stream: WebSocketStream<Upgraded>,
|
||||||
broadcast: Sender<Event>,
|
broadcast: Sender<Event>,
|
||||||
event_tx: mpsc::Sender<SubmittedEvent>,
|
event_tx: mpsc::Sender<SubmittedEvent>,
|
||||||
mut shutdown: Receiver<()>,
|
mut shutdown: Receiver<()>,
|
||||||
) {
|
) {
|
||||||
|
// the time this websocket nostr server started
|
||||||
|
let orig_start = Instant::now();
|
||||||
// get a broadcast channel for clients to communicate on
|
// get a broadcast channel for clients to communicate on
|
||||||
let mut bcast_rx = broadcast.subscribe();
|
let mut bcast_rx = broadcast.subscribe();
|
||||||
// Track internal client state
|
// Track internal client state
|
||||||
let mut conn = conn::ClientConn::new();
|
let mut conn = conn::ClientConn::new(client_info.remote_ip);
|
||||||
|
// subscription creation rate limiting
|
||||||
|
let mut sub_lim_opt = None;
|
||||||
|
// 100ms jitter when the rate limiter returns
|
||||||
|
let jitter = Jitter::up_to(Duration::from_millis(100));
|
||||||
|
let sub_per_min_setting = settings.limits.subscriptions_per_min;
|
||||||
|
if let Some(sub_per_min) = sub_per_min_setting {
|
||||||
|
if sub_per_min > 0 {
|
||||||
|
trace!("Rate limits for sub creation ({}/min)", sub_per_min);
|
||||||
|
let quota_time = core::num::NonZeroU32::new(sub_per_min).unwrap();
|
||||||
|
let quota = Quota::per_minute(quota_time);
|
||||||
|
sub_lim_opt = Some(RateLimiter::direct(quota));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Use the remote IP as the client identifier
|
||||||
let cid = conn.get_client_prefix();
|
let cid = conn.get_client_prefix();
|
||||||
// Create a channel for receiving query results from the database.
|
// Create a channel for receiving query results from the database.
|
||||||
// we will send out the tx handle to any query we generate.
|
// we will send out the tx handle to any query we generate.
|
||||||
let (query_tx, mut query_rx) = mpsc::channel::<db::QueryResult>(256);
|
// this has capacity for some of the larger requests we see, which
|
||||||
|
// should allow the DB thread to release the handle earlier.
|
||||||
|
let (query_tx, mut query_rx) = mpsc::channel::<db::QueryResult>(20000);
|
||||||
// Create channel for receiving NOTICEs
|
// Create channel for receiving NOTICEs
|
||||||
let (notice_tx, mut notice_rx) = mpsc::channel::<String>(32);
|
let (notice_tx, mut notice_rx) = mpsc::channel::<Notice>(128);
|
||||||
|
|
||||||
// last time this client sent data (message, ping, etc.)
|
// last time this client sent data (message, ping, etc.)
|
||||||
let mut last_message_time = Instant::now();
|
let mut last_message_time = Instant::now();
|
||||||
|
|
||||||
// ping interval (every 5 minutes)
|
// ping interval (every 5 minutes)
|
||||||
let default_ping_dur = Duration::from_secs(300);
|
let default_ping_dur = Duration::from_secs(settings.network.ping_interval_seconds.into());
|
||||||
|
|
||||||
// disconnect after 20 minutes without a ping response or event.
|
// disconnect after 20 minutes without a ping response or event.
|
||||||
let max_quiet_time = Duration::from_secs(60 * 20);
|
let max_quiet_time = Duration::from_secs(60 * 20);
|
||||||
@@ -419,16 +511,23 @@ async fn nostr_server(
|
|||||||
// when these subscriptions are cancelled, make a message
|
// when these subscriptions are cancelled, make a message
|
||||||
// available to the executing query so it knows to stop.
|
// available to the executing query so it knows to stop.
|
||||||
let mut running_queries: HashMap<String, oneshot::Sender<()>> = HashMap::new();
|
let mut running_queries: HashMap<String, oneshot::Sender<()>> = HashMap::new();
|
||||||
|
|
||||||
// for stats, keep track of how many events the client published,
|
// for stats, keep track of how many events the client published,
|
||||||
// and how many it received from queries.
|
// and how many it received from queries.
|
||||||
let mut client_published_event_count: usize = 0;
|
let mut client_published_event_count: usize = 0;
|
||||||
let mut client_received_event_count: usize = 0;
|
let mut client_received_event_count: usize = 0;
|
||||||
info!("new connection for client: {:?}", cid);
|
debug!("new client connection (cid: {}, ip: {:?})", cid, conn.ip());
|
||||||
|
let origin = client_info.origin.unwrap_or_else(|| "<unspecified>".into());
|
||||||
|
let user_agent = client_info
|
||||||
|
.user_agent
|
||||||
|
.unwrap_or_else(|| "<unspecified>".into());
|
||||||
|
debug!(
|
||||||
|
"cid: {}, origin: {:?}, user-agent: {:?}",
|
||||||
|
cid, origin, user_agent
|
||||||
|
);
|
||||||
loop {
|
loop {
|
||||||
tokio::select! {
|
tokio::select! {
|
||||||
_ = shutdown.recv() => {
|
_ = shutdown.recv() => {
|
||||||
info!("Shutting client connection down due to shutdown: {:?}", cid);
|
info!("Close connection down due to shutdown, client: {}, ip: {:?}, connected: {:?}", cid, conn.ip(), orig_start.elapsed());
|
||||||
// server shutting down, exit loop
|
// server shutting down, exit loop
|
||||||
break;
|
break;
|
||||||
},
|
},
|
||||||
@@ -443,7 +542,7 @@ async fn nostr_server(
|
|||||||
ws_stream.send(Message::Ping(Vec::new())).await.ok();
|
ws_stream.send(Message::Ping(Vec::new())).await.ok();
|
||||||
},
|
},
|
||||||
Some(notice_msg) = notice_rx.recv() => {
|
Some(notice_msg) = notice_rx.recv() => {
|
||||||
ws_stream.send(make_notice_message(¬ice_msg)).await.ok();
|
ws_stream.send(make_notice_message(notice_msg)).await.ok();
|
||||||
},
|
},
|
||||||
Some(query_result) = query_rx.recv() => {
|
Some(query_result) = query_rx.recv() => {
|
||||||
// database informed us of a query result we asked for
|
// database informed us of a query result we asked for
|
||||||
@@ -462,12 +561,15 @@ async fn nostr_server(
|
|||||||
Ok(global_event) = bcast_rx.recv() => {
|
Ok(global_event) = bcast_rx.recv() => {
|
||||||
// an event has been broadcast to all clients
|
// an event has been broadcast to all clients
|
||||||
// first check if there is a subscription for this event.
|
// first check if there is a subscription for this event.
|
||||||
let matching_subs = conn.get_matching_subscriptions(&global_event);
|
for (s, sub) in conn.subscriptions() {
|
||||||
for s in matching_subs {
|
if !sub.interested_in_event(&global_event) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: serialize at broadcast time, instead of
|
// TODO: serialize at broadcast time, instead of
|
||||||
// once for each consumer.
|
// once for each consumer.
|
||||||
if let Ok(event_str) = serde_json::to_string(&global_event) {
|
if let Ok(event_str) = serde_json::to_string(&global_event) {
|
||||||
debug!("sub match for client: {:?}, sub: {:?}, event: {:?}",
|
trace!("sub match for client: {}, sub: {:?}, event: {:?}",
|
||||||
cid, s,
|
cid, s,
|
||||||
global_event.get_event_id_prefix());
|
global_event.get_event_id_prefix());
|
||||||
// create an event response and send it
|
// create an event response and send it
|
||||||
@@ -488,7 +590,7 @@ async fn nostr_server(
|
|||||||
},
|
},
|
||||||
Some(Ok(Message::Binary(_))) => {
|
Some(Ok(Message::Binary(_))) => {
|
||||||
ws_stream.send(
|
ws_stream.send(
|
||||||
make_notice_message("binary messages are not accepted")).await.ok();
|
make_notice_message(Notice::message("binary messages are not accepted".into()))).await.ok();
|
||||||
continue;
|
continue;
|
||||||
},
|
},
|
||||||
Some(Ok(Message::Ping(_) | Message::Pong(_))) => {
|
Some(Ok(Message::Ping(_) | Message::Pong(_))) => {
|
||||||
@@ -498,8 +600,7 @@ async fn nostr_server(
|
|||||||
},
|
},
|
||||||
Some(Err(WsError::Capacity(MessageTooLong{size, max_size}))) => {
|
Some(Err(WsError::Capacity(MessageTooLong{size, max_size}))) => {
|
||||||
ws_stream.send(
|
ws_stream.send(
|
||||||
make_notice_message(
|
make_notice_message(Notice::message(format!("message too large ({} > {})",size, max_size)))).await.ok();
|
||||||
&format!("message too large ({} > {})",size, max_size))).await.ok();
|
|
||||||
continue;
|
continue;
|
||||||
},
|
},
|
||||||
None |
|
None |
|
||||||
@@ -507,17 +608,17 @@ async fn nostr_server(
|
|||||||
Err(WsError::AlreadyClosed | WsError::ConnectionClosed |
|
Err(WsError::AlreadyClosed | WsError::ConnectionClosed |
|
||||||
WsError::Protocol(tungstenite::error::ProtocolError::ResetWithoutClosingHandshake)))
|
WsError::Protocol(tungstenite::error::ProtocolError::ResetWithoutClosingHandshake)))
|
||||||
=> {
|
=> {
|
||||||
debug!("websocket close from client: {:?}",cid);
|
debug!("websocket close from client (cid: {}, ip: {:?})",cid, conn.ip());
|
||||||
break;
|
break;
|
||||||
},
|
},
|
||||||
Some(Err(WsError::Io(e))) => {
|
Some(Err(WsError::Io(e))) => {
|
||||||
// IO errors are considered fatal
|
// IO errors are considered fatal
|
||||||
warn!("IO error (client: {:?}): {:?}", cid, e);
|
warn!("IO error (cid: {}, ip: {:?}): {:?}", cid, conn.ip(), e);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
x => {
|
x => {
|
||||||
// default condition on error is to close the client connection
|
// default condition on error is to close the client connection
|
||||||
info!("unknown error (client: {:?}): {:?} (closing conn)", cid, x);
|
info!("unknown error (cid: {}, ip: {:?}): {:?} (closing conn)", cid, conn.ip(), x);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -527,11 +628,12 @@ async fn nostr_server(
|
|||||||
Ok(NostrMessage::EventMsg(ec)) => {
|
Ok(NostrMessage::EventMsg(ec)) => {
|
||||||
// An EventCmd needs to be validated to be converted into an Event
|
// An EventCmd needs to be validated to be converted into an Event
|
||||||
// handle each type of message
|
// handle each type of message
|
||||||
|
let evid = ec.event_id().to_owned();
|
||||||
let parsed : Result<Event> = Result::<Event>::from(ec);
|
let parsed : Result<Event> = Result::<Event>::from(ec);
|
||||||
match parsed {
|
match parsed {
|
||||||
Ok(e) => {
|
Ok(e) => {
|
||||||
let id_prefix:String = e.id.chars().take(8).collect();
|
let id_prefix:String = e.id.chars().take(8).collect();
|
||||||
debug!("successfully parsed/validated event: {:?} from client: {:?}", id_prefix, cid);
|
debug!("successfully parsed/validated event: {:?} (cid: {})", id_prefix, cid);
|
||||||
// check if the event is too far in the future.
|
// check if the event is too far in the future.
|
||||||
if e.is_valid_timestamp(settings.options.reject_future_seconds) {
|
if e.is_valid_timestamp(settings.options.reject_future_seconds) {
|
||||||
// Write this to the database.
|
// Write this to the database.
|
||||||
@@ -539,39 +641,50 @@ async fn nostr_server(
|
|||||||
event_tx.send(submit_event).await.ok();
|
event_tx.send(submit_event).await.ok();
|
||||||
client_published_event_count += 1;
|
client_published_event_count += 1;
|
||||||
} else {
|
} else {
|
||||||
info!("client {:?} sent a far future-dated event", cid);
|
info!("client: {} sent a far future-dated event", cid);
|
||||||
if let Some(fut_sec) = settings.options.reject_future_seconds {
|
if let Some(fut_sec) = settings.options.reject_future_seconds {
|
||||||
ws_stream.send(make_notice_message(&format!("The event created_at field is out of the acceptable range (+{}sec) for this relay and was not stored.",fut_sec))).await.ok();
|
let msg = format!("The event created_at field is out of the acceptable range (+{}sec) for this relay.",fut_sec);
|
||||||
|
let notice = Notice::invalid(e.id, &msg);
|
||||||
|
ws_stream.send(make_notice_message(notice)).await.ok();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Err(_) => {
|
Err(e) => {
|
||||||
info!("client {:?} sent an invalid event", cid);
|
info!("client sent an invalid event (cid: {})", cid);
|
||||||
ws_stream.send(make_notice_message("event was invalid")).await.ok();
|
ws_stream.send(make_notice_message(Notice::invalid(evid, &format!("{}", e)))).await.ok();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Ok(NostrMessage::SubMsg(s)) => {
|
Ok(NostrMessage::SubMsg(s)) => {
|
||||||
debug!("client {} requesting a subscription", cid);
|
debug!("subscription requested (cid: {}, sub: {:?})", cid, s.id);
|
||||||
// subscription handling consists of:
|
// subscription handling consists of:
|
||||||
|
// * check for rate limits
|
||||||
// * registering the subscription so future events can be matched
|
// * registering the subscription so future events can be matched
|
||||||
// * making a channel to cancel to request later
|
// * making a channel to cancel to request later
|
||||||
// * sending a request for a SQL query
|
// * sending a request for a SQL query
|
||||||
let (abandon_query_tx, abandon_query_rx) = oneshot::channel::<()>();
|
// Do nothing if the sub already exists.
|
||||||
match conn.subscribe(s.clone()) {
|
if !conn.has_subscription(&s) {
|
||||||
Ok(()) => {
|
if let Some(ref lim) = sub_lim_opt {
|
||||||
// when we insert, if there was a previous query running with the same name, cancel it.
|
lim.until_ready_with_jitter(jitter).await;
|
||||||
if let Some(previous_query) = running_queries.insert(s.id.to_owned(), abandon_query_tx) {
|
}
|
||||||
previous_query.send(()).ok();
|
let (abandon_query_tx, abandon_query_rx) = oneshot::channel::<()>();
|
||||||
}
|
match conn.subscribe(s.clone()) {
|
||||||
// start a database query
|
Ok(()) => {
|
||||||
db::db_query(s, cid.to_owned(), pool.clone(), query_tx.clone(), abandon_query_rx).await;
|
// when we insert, if there was a previous query running with the same name, cancel it.
|
||||||
},
|
if let Some(previous_query) = running_queries.insert(s.id.to_owned(), abandon_query_tx) {
|
||||||
Err(e) => {
|
previous_query.send(()).ok();
|
||||||
info!("Subscription error: {}", e);
|
}
|
||||||
ws_stream.send(make_notice_message(&e.to_string())).await.ok();
|
// start a database query. this spawns a blocking database query on a worker thread.
|
||||||
|
db::db_query(s, cid.to_owned(), pool.clone(), query_tx.clone(), abandon_query_rx).await;
|
||||||
|
},
|
||||||
|
Err(e) => {
|
||||||
|
info!("Subscription error: {} (cid: {}, sub: {:?})", e, cid, s.id);
|
||||||
|
ws_stream.send(make_notice_message(Notice::message(format!("Subscription error: {}", e)))).await.ok();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
|
info!("client sent duplicate subscription, ignoring (cid: {}, sub: {:?})", cid, s.id);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
Ok(NostrMessage::CloseMsg(cc)) => {
|
Ok(NostrMessage::CloseMsg(cc)) => {
|
||||||
// closing a request simply removes the subscription.
|
// closing a request simply removes the subscription.
|
||||||
@@ -588,23 +701,23 @@ async fn nostr_server(
|
|||||||
conn.unsubscribe(&c);
|
conn.unsubscribe(&c);
|
||||||
} else {
|
} else {
|
||||||
info!("invalid command ignored");
|
info!("invalid command ignored");
|
||||||
ws_stream.send(make_notice_message("could not parse command")).await.ok();
|
ws_stream.send(make_notice_message(Notice::message("could not parse command".into()))).await.ok();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Err(Error::ConnError) => {
|
Err(Error::ConnError) => {
|
||||||
debug!("got connection close/error, disconnecting client: {:?}",cid);
|
debug!("got connection close/error, disconnecting cid: {}, ip: {:?}",cid, conn.ip());
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
Err(Error::EventMaxLengthError(s)) => {
|
Err(Error::EventMaxLengthError(s)) => {
|
||||||
info!("client {:?} sent event larger ({} bytes) than max size", cid, s);
|
info!("client sent event larger ({} bytes) than max size (cid: {})", s, cid);
|
||||||
ws_stream.send(make_notice_message("event exceeded max size")).await.ok();
|
ws_stream.send(make_notice_message(Notice::message("event exceeded max size".into()))).await.ok();
|
||||||
},
|
},
|
||||||
Err(Error::ProtoParseError) => {
|
Err(Error::ProtoParseError) => {
|
||||||
info!("client {:?} sent event that could not be parsed", cid);
|
info!("client sent event that could not be parsed (cid: {})", cid);
|
||||||
ws_stream.send(make_notice_message("could not parse command")).await.ok();
|
ws_stream.send(make_notice_message(Notice::message("could not parse command".into()))).await.ok();
|
||||||
},
|
},
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
info!("got non-fatal error from client: {:?}, error: {:?}", cid, e);
|
info!("got non-fatal error from client (cid: {}, error: {:?}", cid, e);
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -615,7 +728,11 @@ async fn nostr_server(
|
|||||||
stop_tx.send(()).ok();
|
stop_tx.send(()).ok();
|
||||||
}
|
}
|
||||||
info!(
|
info!(
|
||||||
"stopping connection for client: {:?} (client sent {} event(s), received {})",
|
"stopping client connection (cid: {}, ip: {:?}, sent: {} events, recv: {} events, connected: {:?})",
|
||||||
cid, client_published_event_count, client_received_event_count
|
cid,
|
||||||
|
conn.ip(),
|
||||||
|
client_published_event_count,
|
||||||
|
client_received_event_count,
|
||||||
|
orig_start.elapsed()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
+63
-3
@@ -37,6 +37,9 @@ pub struct ReqFilter {
|
|||||||
#[serde(skip)]
|
#[serde(skip)]
|
||||||
pub tags: Option<HashMap<char, HashSet<String>>>,
|
pub tags: Option<HashMap<char, HashSet<String>>>,
|
||||||
/// Force no matches due to malformed data
|
/// Force no matches due to malformed data
|
||||||
|
// we can't represent it in the req filter, so we don't want to
|
||||||
|
// erroneously match. This basically indicates the req tried to
|
||||||
|
// do something invalid.
|
||||||
pub force_no_match: bool,
|
pub force_no_match: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,12 +65,21 @@ impl<'de> Deserialize<'de> for ReqFilter {
|
|||||||
tags: None,
|
tags: None,
|
||||||
force_no_match: false,
|
force_no_match: false,
|
||||||
};
|
};
|
||||||
|
let empty_string = "".into();
|
||||||
let mut ts = None;
|
let mut ts = None;
|
||||||
// iterate through each key, and assign values that exist
|
// iterate through each key, and assign values that exist
|
||||||
for (key, val) in filter.into_iter() {
|
for (key, val) in filter.into_iter() {
|
||||||
// ids
|
// ids
|
||||||
if key == "ids" {
|
if key == "ids" {
|
||||||
rf.ids = Deserialize::deserialize(val).ok();
|
let raw_ids: Option<Vec<String>>= Deserialize::deserialize(val).ok();
|
||||||
|
if let Some(a) = raw_ids.as_ref() {
|
||||||
|
if a.contains(&empty_string) {
|
||||||
|
return Err(serde::de::Error::invalid_type(
|
||||||
|
Unexpected::Other("prefix matches must not be empty strings"),
|
||||||
|
&"a json object"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
rf.ids =raw_ids;
|
||||||
} else if key == "kinds" {
|
} else if key == "kinds" {
|
||||||
rf.kinds = Deserialize::deserialize(val).ok();
|
rf.kinds = Deserialize::deserialize(val).ok();
|
||||||
} else if key == "since" {
|
} else if key == "since" {
|
||||||
@@ -77,7 +89,15 @@ impl<'de> Deserialize<'de> for ReqFilter {
|
|||||||
} else if key == "limit" {
|
} else if key == "limit" {
|
||||||
rf.limit = Deserialize::deserialize(val).ok();
|
rf.limit = Deserialize::deserialize(val).ok();
|
||||||
} else if key == "authors" {
|
} else if key == "authors" {
|
||||||
rf.authors = Deserialize::deserialize(val).ok();
|
let raw_authors: Option<Vec<String>>= Deserialize::deserialize(val).ok();
|
||||||
|
if let Some(a) = raw_authors.as_ref() {
|
||||||
|
if a.contains(&empty_string) {
|
||||||
|
return Err(serde::de::Error::invalid_type(
|
||||||
|
Unexpected::Other("prefix matches must not be empty strings"),
|
||||||
|
&"a json object"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
rf.authors = raw_authors;
|
||||||
} else if key.starts_with('#') && key.len() > 1 && val.is_array() {
|
} else if key.starts_with('#') && key.len() > 1 && val.is_array() {
|
||||||
if let Some(tag_search) = tag_search_char_from_filter(key) {
|
if let Some(tag_search) = tag_search_char_from_filter(key) {
|
||||||
if ts.is_none() {
|
if ts.is_none() {
|
||||||
@@ -217,6 +237,17 @@ impl ReqFilter {
|
|||||||
.unwrap_or(true)
|
.unwrap_or(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn delegated_authors_match(&self, event: &Event) -> bool {
|
||||||
|
if let Some(delegated_pubkey) = &event.delegated_by {
|
||||||
|
self.authors
|
||||||
|
.as_ref()
|
||||||
|
.map(|vs| prefix_match(vs, delegated_pubkey))
|
||||||
|
.unwrap_or(true)
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn tag_match(&self, event: &Event) -> bool {
|
fn tag_match(&self, event: &Event) -> bool {
|
||||||
// get the hashset from the filter.
|
// get the hashset from the filter.
|
||||||
if let Some(map) = &self.tags {
|
if let Some(map) = &self.tags {
|
||||||
@@ -248,7 +279,7 @@ impl ReqFilter {
|
|||||||
&& self.since.map(|t| event.created_at > t).unwrap_or(true)
|
&& self.since.map(|t| event.created_at > t).unwrap_or(true)
|
||||||
&& self.until.map(|t| event.created_at < t).unwrap_or(true)
|
&& self.until.map(|t| event.created_at < t).unwrap_or(true)
|
||||||
&& self.kind_match(event.kind)
|
&& self.kind_match(event.kind)
|
||||||
&& self.authors_match(event)
|
&& (self.authors_match(event) || self.delegated_authors_match(event))
|
||||||
&& self.tag_match(event)
|
&& self.tag_match(event)
|
||||||
&& !self.force_no_match
|
&& !self.force_no_match
|
||||||
}
|
}
|
||||||
@@ -280,6 +311,24 @@ mod tests {
|
|||||||
assert!(serde_json::from_str::<Subscription>(raw_json).is_err());
|
assert!(serde_json::from_str::<Subscription>(raw_json).is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn req_empty_authors_prefix() {
|
||||||
|
let raw_json = "[\"REQ\",\"some-id\",{\"authors\": [\"\"]}]";
|
||||||
|
assert!(serde_json::from_str::<Subscription>(raw_json).is_err());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn req_empty_ids_prefix() {
|
||||||
|
let raw_json = "[\"REQ\",\"some-id\",{\"ids\": [\"\"]}]";
|
||||||
|
assert!(serde_json::from_str::<Subscription>(raw_json).is_err());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn req_empty_ids_prefix_mixed() {
|
||||||
|
let raw_json = "[\"REQ\",\"some-id\",{\"ids\": [\"\",\"aaa\"]}]";
|
||||||
|
assert!(serde_json::from_str::<Subscription>(raw_json).is_err());
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn legacy_filter() {
|
fn legacy_filter() {
|
||||||
// legacy field in filter
|
// legacy field in filter
|
||||||
@@ -308,6 +357,7 @@ mod tests {
|
|||||||
let e = Event {
|
let e = Event {
|
||||||
id: "foo".to_owned(),
|
id: "foo".to_owned(),
|
||||||
pubkey: "abcd".to_owned(),
|
pubkey: "abcd".to_owned(),
|
||||||
|
delegated_by: None,
|
||||||
created_at: 0,
|
created_at: 0,
|
||||||
kind: 0,
|
kind: 0,
|
||||||
tags: Vec::new(),
|
tags: Vec::new(),
|
||||||
@@ -326,6 +376,7 @@ mod tests {
|
|||||||
let e = Event {
|
let e = Event {
|
||||||
id: "abcd".to_owned(),
|
id: "abcd".to_owned(),
|
||||||
pubkey: "".to_owned(),
|
pubkey: "".to_owned(),
|
||||||
|
delegated_by: None,
|
||||||
created_at: 0,
|
created_at: 0,
|
||||||
kind: 0,
|
kind: 0,
|
||||||
tags: Vec::new(),
|
tags: Vec::new(),
|
||||||
@@ -344,6 +395,7 @@ mod tests {
|
|||||||
let e = Event {
|
let e = Event {
|
||||||
id: "abcde".to_owned(),
|
id: "abcde".to_owned(),
|
||||||
pubkey: "".to_owned(),
|
pubkey: "".to_owned(),
|
||||||
|
delegated_by: None,
|
||||||
created_at: 0,
|
created_at: 0,
|
||||||
kind: 0,
|
kind: 0,
|
||||||
tags: Vec::new(),
|
tags: Vec::new(),
|
||||||
@@ -363,6 +415,7 @@ mod tests {
|
|||||||
let e = Event {
|
let e = Event {
|
||||||
id: "abc".to_owned(),
|
id: "abc".to_owned(),
|
||||||
pubkey: "".to_owned(),
|
pubkey: "".to_owned(),
|
||||||
|
delegated_by: None,
|
||||||
created_at: 50,
|
created_at: 50,
|
||||||
kind: 0,
|
kind: 0,
|
||||||
tags: Vec::new(),
|
tags: Vec::new(),
|
||||||
@@ -386,6 +439,7 @@ mod tests {
|
|||||||
let e = Event {
|
let e = Event {
|
||||||
id: "abc".to_owned(),
|
id: "abc".to_owned(),
|
||||||
pubkey: "".to_owned(),
|
pubkey: "".to_owned(),
|
||||||
|
delegated_by: None,
|
||||||
created_at: 150,
|
created_at: 150,
|
||||||
kind: 0,
|
kind: 0,
|
||||||
tags: Vec::new(),
|
tags: Vec::new(),
|
||||||
@@ -407,6 +461,7 @@ mod tests {
|
|||||||
let e = Event {
|
let e = Event {
|
||||||
id: "abc".to_owned(),
|
id: "abc".to_owned(),
|
||||||
pubkey: "".to_owned(),
|
pubkey: "".to_owned(),
|
||||||
|
delegated_by: None,
|
||||||
created_at: 50,
|
created_at: 50,
|
||||||
kind: 0,
|
kind: 0,
|
||||||
tags: Vec::new(),
|
tags: Vec::new(),
|
||||||
@@ -425,6 +480,7 @@ mod tests {
|
|||||||
let e = Event {
|
let e = Event {
|
||||||
id: "abc".to_owned(),
|
id: "abc".to_owned(),
|
||||||
pubkey: "".to_owned(),
|
pubkey: "".to_owned(),
|
||||||
|
delegated_by: None,
|
||||||
created_at: 1001,
|
created_at: 1001,
|
||||||
kind: 0,
|
kind: 0,
|
||||||
tags: Vec::new(),
|
tags: Vec::new(),
|
||||||
@@ -443,6 +499,7 @@ mod tests {
|
|||||||
let e = Event {
|
let e = Event {
|
||||||
id: "abc".to_owned(),
|
id: "abc".to_owned(),
|
||||||
pubkey: "".to_owned(),
|
pubkey: "".to_owned(),
|
||||||
|
delegated_by: None,
|
||||||
created_at: 0,
|
created_at: 0,
|
||||||
kind: 0,
|
kind: 0,
|
||||||
tags: Vec::new(),
|
tags: Vec::new(),
|
||||||
@@ -461,6 +518,7 @@ mod tests {
|
|||||||
let e = Event {
|
let e = Event {
|
||||||
id: "123".to_owned(),
|
id: "123".to_owned(),
|
||||||
pubkey: "abc".to_owned(),
|
pubkey: "abc".to_owned(),
|
||||||
|
delegated_by: None,
|
||||||
created_at: 0,
|
created_at: 0,
|
||||||
kind: 0,
|
kind: 0,
|
||||||
tags: Vec::new(),
|
tags: Vec::new(),
|
||||||
@@ -479,6 +537,7 @@ mod tests {
|
|||||||
let e = Event {
|
let e = Event {
|
||||||
id: "123".to_owned(),
|
id: "123".to_owned(),
|
||||||
pubkey: "bcd".to_owned(),
|
pubkey: "bcd".to_owned(),
|
||||||
|
delegated_by: None,
|
||||||
created_at: 0,
|
created_at: 0,
|
||||||
kind: 0,
|
kind: 0,
|
||||||
tags: Vec::new(),
|
tags: Vec::new(),
|
||||||
@@ -497,6 +556,7 @@ mod tests {
|
|||||||
let e = Event {
|
let e = Event {
|
||||||
id: "123".to_owned(),
|
id: "123".to_owned(),
|
||||||
pubkey: "xyz".to_owned(),
|
pubkey: "xyz".to_owned(),
|
||||||
|
delegated_by: None,
|
||||||
created_at: 0,
|
created_at: 0,
|
||||||
kind: 0,
|
kind: 0,
|
||||||
tags: Vec::new(),
|
tags: Vec::new(),
|
||||||
|
|||||||
Reference in New Issue
Block a user