Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
26f296f76f | ||
|
|
c3c9b5dcd2 | ||
|
|
da29bdd837 | ||
|
|
bacb85024c | ||
|
|
7a77c459bb | ||
|
|
34c8b04926 | ||
|
|
1032a51220 | ||
|
|
79abd981e1 | ||
|
|
b1957ab2b1 | ||
|
|
23aa6e7313 | ||
|
|
fb751ba252 | ||
|
|
7c5e851b82 | ||
|
|
f965c53434 | ||
|
|
74376d94e5 | ||
|
|
21d1bbcfe3 | ||
|
|
c3e13af9e3 | ||
|
|
05f70112e8 | ||
|
|
eab522dc39 | ||
|
|
edf7af1573 | ||
|
|
34f497a650 | ||
|
|
4adad4c3a9 | ||
|
|
70dfcb6a04 | ||
|
|
c50e10aa21 | ||
|
|
9e22776227 | ||
|
|
dad6911807 | ||
|
|
ddc58a2f1c | ||
|
|
1131c1986e | ||
|
|
06fcaad9a1 | ||
|
|
087b68128f | ||
|
|
4647476622 | ||
|
|
7a72e588ea | ||
|
|
9237eed735 | ||
|
|
f4beb884b3 | ||
|
|
73285683a3 | ||
|
|
2f10271903 | ||
|
|
a34516628b | ||
|
|
eba7a32615 | ||
|
|
4d746fad85 | ||
|
|
0582a891cc | ||
|
|
2bcddf8bbf | ||
|
|
1595ec783d | ||
|
|
a2d1d78e23 | ||
|
|
04db2203bb | ||
|
|
1c1b1a1802 | ||
|
|
993fec4eed | ||
|
|
beffeb4d86 | ||
|
|
5135f3b007 | ||
|
|
ba0b50bc9c | ||
|
|
c65c64275e | ||
|
|
80c459c36c | ||
|
|
8e4e2d824b | ||
|
|
c13961a5c4 | ||
|
|
05b08c7916 | ||
|
|
9a141dc950 | ||
|
|
8c9170d4e3 | ||
|
|
5508020777 | ||
|
|
43021910ea | ||
|
|
c0158af18b | ||
|
|
164603dedd | ||
|
|
c1c25a22f5 | ||
|
|
6df92f9580 | ||
|
|
440217e1ee | ||
|
|
96359aafab | ||
|
|
5414629298 |
@@ -9,7 +9,7 @@ jobs:
|
|||||||
test_nostr-rs-relay:
|
test_nostr-rs-relay:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v3
|
||||||
|
|
||||||
- name: Update local toolchain
|
- name: Update local toolchain
|
||||||
run: |
|
run: |
|
||||||
|
|||||||
@@ -1,2 +1,4 @@
|
|||||||
**/target/
|
**/target/
|
||||||
nostr.db
|
nostr.db
|
||||||
|
nostr.db-*
|
||||||
|
justfile
|
||||||
|
|||||||
Generated
+1098
-799
File diff suppressed because it is too large
Load Diff
+10
-4
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "nostr-rs-relay"
|
name = "nostr-rs-relay"
|
||||||
version = "0.8.8"
|
version = "0.8.13"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
authors = ["Greg Heartsfield <scsibug@imap.cc>"]
|
authors = ["Greg Heartsfield <scsibug@imap.cc>"]
|
||||||
description = "A relay implementation for the Nostr protocol"
|
description = "A relay implementation for the Nostr protocol"
|
||||||
@@ -13,8 +13,9 @@ categories = ["network-programming", "web-programming"]
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
clap = { version = "4.0.32", features = ["env", "default", "derive"]}
|
clap = { version = "4.0.32", features = ["env", "default", "derive"]}
|
||||||
tracing = "0.1.36"
|
tracing = "0.1.37"
|
||||||
tracing-subscriber = "0.2.0"
|
tracing-appender = "0.2.2"
|
||||||
|
tracing-subscriber = "0.3.16"
|
||||||
tokio = { version = "1", features = ["full", "tracing", "signal"] }
|
tokio = { version = "1", features = ["full", "tracing", "signal"] }
|
||||||
prost = "0.11"
|
prost = "0.11"
|
||||||
tonic = "0.8.3"
|
tonic = "0.8.3"
|
||||||
@@ -38,7 +39,7 @@ lazy_static = "1.4"
|
|||||||
governor = "0.4"
|
governor = "0.4"
|
||||||
nonzero_ext = "0.3"
|
nonzero_ext = "0.3"
|
||||||
hyper = { version="0.14", features=["client", "server","http1","http2","tcp"] }
|
hyper = { version="0.14", features=["client", "server","http1","http2","tcp"] }
|
||||||
hyper-tls = "0.5"
|
hyper-rustls = { version = "0.24" }
|
||||||
http = { version = "0.2" }
|
http = { version = "0.2" }
|
||||||
parse_duration = "2"
|
parse_duration = "2"
|
||||||
rand = "0.8"
|
rand = "0.8"
|
||||||
@@ -52,6 +53,11 @@ prometheus = "0.13.3"
|
|||||||
indicatif = "0.17.3"
|
indicatif = "0.17.3"
|
||||||
bech32 = "0.9.1"
|
bech32 = "0.9.1"
|
||||||
url = "2.3.1"
|
url = "2.3.1"
|
||||||
|
qrcode = { version = "0.12.0", default-features = false, features = ["svg"] }
|
||||||
|
nostr = { version = "0.18.0", default-features = false, features = ["base", "nip04", "nip19"] }
|
||||||
|
[target.'cfg(not(target_env = "msvc"))'.dependencies]
|
||||||
|
tikv-jemallocator = "0.5"
|
||||||
|
log = "0.4"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
anyhow = "1"
|
anyhow = "1"
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ mirrored on [GitHub](https://github.com/scsibug/nostr-rs-relay).
|
|||||||
- [ ] NIP-26: [Event Delegation](https://github.com/nostr-protocol/nips/blob/master/26.md) (_implemented, but currently disabled_)
|
- [ ] NIP-26: [Event Delegation](https://github.com/nostr-protocol/nips/blob/master/26.md) (_implemented, but currently disabled_)
|
||||||
- [x] NIP-28: [Public Chat](https://github.com/nostr-protocol/nips/blob/master/28.md)
|
- [x] NIP-28: [Public Chat](https://github.com/nostr-protocol/nips/blob/master/28.md)
|
||||||
- [x] NIP-33: [Parameterized Replaceable Events](https://github.com/nostr-protocol/nips/blob/master/33.md)
|
- [x] NIP-33: [Parameterized Replaceable Events](https://github.com/nostr-protocol/nips/blob/master/33.md)
|
||||||
|
- [x] NIP-40: [Expiration Timestamp](https://github.com/nostr-protocol/nips/blob/master/40.md)
|
||||||
- [x] NIP-42: [Authentication of clients to relays](https://github.com/nostr-protocol/nips/blob/master/42.md)
|
- [x] NIP-42: [Authentication of clients to relays](https://github.com/nostr-protocol/nips/blob/master/42.md)
|
||||||
|
|
||||||
## Quick Start
|
## Quick Start
|
||||||
@@ -92,6 +93,11 @@ https://hub.docker.com/r/scsibug/nostr-rs-relay
|
|||||||
|
|
||||||
Building `nostr-rs-relay` requires an installation of Cargo & Rust: https://www.rust-lang.org/tools/install
|
Building `nostr-rs-relay` requires an installation of Cargo & Rust: https://www.rust-lang.org/tools/install
|
||||||
|
|
||||||
|
The following OS packages will be helpful; on Debian/Ubuntu:
|
||||||
|
```console
|
||||||
|
$ sudo apt-get install build-essential cmake protobuf-compiler pkg-config libssl-dev
|
||||||
|
```
|
||||||
|
|
||||||
Clone this repository, and then build a release version of the relay:
|
Clone this repository, and then build a release version of the relay:
|
||||||
|
|
||||||
```console
|
```console
|
||||||
|
|||||||
@@ -2,9 +2,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
tonic_build::configure()
|
tonic_build::configure()
|
||||||
.build_server(false)
|
.build_server(false)
|
||||||
.protoc_arg("--experimental_allow_proto3_optional")
|
.protoc_arg("--experimental_allow_proto3_optional")
|
||||||
.compile(
|
.compile(&["proto/nauthz.proto"], &["proto"])?;
|
||||||
&["proto/nauthz.proto"],
|
|
||||||
&["proto"],
|
|
||||||
)?;
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
+65
-1
@@ -20,6 +20,9 @@ description = "A newly created nostr-rs-relay.\n\nCustomize this with your own i
|
|||||||
# ICO format.
|
# ICO format.
|
||||||
#favicon = "favicon.ico"
|
#favicon = "favicon.ico"
|
||||||
|
|
||||||
|
# URL of Relay's icon.
|
||||||
|
#relay_icon = "https://example.test/img.png"
|
||||||
|
|
||||||
[diagnostics]
|
[diagnostics]
|
||||||
# Enable tokio tracing (for use with tokio-console)
|
# Enable tokio tracing (for use with tokio-console)
|
||||||
#tracing = false
|
#tracing = false
|
||||||
@@ -42,7 +45,7 @@ description = "A newly created nostr-rs-relay.\n\nCustomize this with your own i
|
|||||||
# Database connection pool settings for subscribers:
|
# Database connection pool settings for subscribers:
|
||||||
|
|
||||||
# Minimum number of SQLite reader connections
|
# Minimum number of SQLite reader connections
|
||||||
#min_conn = 4
|
#min_conn = 0
|
||||||
|
|
||||||
# Maximum number of SQLite reader connections. Recommend setting this
|
# Maximum number of SQLite reader connections. Recommend setting this
|
||||||
# to approx the number of cores.
|
# to approx the number of cores.
|
||||||
@@ -52,6 +55,16 @@ description = "A newly created nostr-rs-relay.\n\nCustomize this with your own i
|
|||||||
# sqlite.
|
# sqlite.
|
||||||
#connection = "postgresql://postgres:nostr@localhost:7500/nostr"
|
#connection = "postgresql://postgres:nostr@localhost:7500/nostr"
|
||||||
|
|
||||||
|
# Optional database connection string for writing. Use this for
|
||||||
|
# postgres clusters where you want to separate reads and writes to
|
||||||
|
# different nodes. Ignore for single-database instances.
|
||||||
|
#connection_write = "postgresql://postgres:nostr@localhost:7500/nostr"
|
||||||
|
|
||||||
|
[logging]
|
||||||
|
# Directory to store log files. Log files roll over daily.
|
||||||
|
#folder_path = "./log"
|
||||||
|
#file_prefix = "nostr-relay"
|
||||||
|
|
||||||
[grpc]
|
[grpc]
|
||||||
# gRPC interfaces for externalized decisions and other extensions to
|
# gRPC interfaces for externalized decisions and other extensions to
|
||||||
# functionality.
|
# functionality.
|
||||||
@@ -132,6 +145,11 @@ reject_future_seconds = 1800
|
|||||||
# 70202,
|
# 70202,
|
||||||
#]
|
#]
|
||||||
|
|
||||||
|
# Event kind allowlist. Events other than these kinds will be discarded.
|
||||||
|
#event_kind_allowlist = [
|
||||||
|
# 0, 1, 2, 3, 7, 40, 41, 42, 43, 44, 30023,
|
||||||
|
#]
|
||||||
|
|
||||||
[authorization]
|
[authorization]
|
||||||
# Pubkey addresses in this array are whitelisted for event publishing.
|
# Pubkey addresses in this array are whitelisted for event publishing.
|
||||||
# Only valid events by these authors will be accepted, if the variable
|
# Only valid events by these authors will be accepted, if the variable
|
||||||
@@ -142,6 +160,8 @@ reject_future_seconds = 1800
|
|||||||
#]
|
#]
|
||||||
# Enable NIP-42 authentication
|
# Enable NIP-42 authentication
|
||||||
#nip42_auth = false
|
#nip42_auth = false
|
||||||
|
# Send DMs (kind 4 and 44) and gift wraps (kind 1059) only to their authenticated recipients
|
||||||
|
#nip42_dms = false
|
||||||
|
|
||||||
[verified_users]
|
[verified_users]
|
||||||
# NIP-05 verification of users. Can be "enabled" to require NIP-05
|
# NIP-05 verification of users. Can be "enabled" to require NIP-05
|
||||||
@@ -168,3 +188,47 @@ reject_future_seconds = 1800
|
|||||||
# How many consecutive failed checks before we give up on verifying
|
# How many consecutive failed checks before we give up on verifying
|
||||||
# this author.
|
# this author.
|
||||||
#max_consecutive_failures = 20
|
#max_consecutive_failures = 20
|
||||||
|
|
||||||
|
[pay_to_relay]
|
||||||
|
# Enable pay to relay
|
||||||
|
#enabled = false
|
||||||
|
|
||||||
|
# The cost to be admitted to relay
|
||||||
|
#admission_cost = 4200
|
||||||
|
|
||||||
|
# The cost in sats per post
|
||||||
|
#cost_per_event = 0
|
||||||
|
|
||||||
|
# Url of lnbits api
|
||||||
|
#node_url = "<node url>"
|
||||||
|
|
||||||
|
# LNBits api secret
|
||||||
|
#api_secret = "<ln bits api>"
|
||||||
|
|
||||||
|
# Nostr direct message on signup
|
||||||
|
#direct_message=true
|
||||||
|
|
||||||
|
# Terms of service
|
||||||
|
#terms_message = """
|
||||||
|
#This service (and supporting services) are provided "as is", without warranty of any kind, express or implied.
|
||||||
|
#
|
||||||
|
#By using this service, you agree:
|
||||||
|
#* Not to engage in spam or abuse the relay service
|
||||||
|
#* Not to disseminate illegal content
|
||||||
|
#* That requests to delete content cannot be guaranteed
|
||||||
|
#* To use the service in compliance with all applicable laws
|
||||||
|
#* To grant necessary rights to your content for unlimited time
|
||||||
|
#* To be of legal age and have capacity to use this service
|
||||||
|
#* That the service may be terminated at any time without notice
|
||||||
|
#* That the content you publish may be removed at any time without notice
|
||||||
|
#* To have your IP address collected to detect abuse or misuse
|
||||||
|
#* To cooperate with the relay to combat abuse or misuse
|
||||||
|
#* You may be exposed to content that you might find triggering or distasteful
|
||||||
|
#* The relay operator is not liable for content produced by users of the relay
|
||||||
|
#"""
|
||||||
|
|
||||||
|
# Whether or not new sign ups should be allowed
|
||||||
|
#sign_ups = false
|
||||||
|
|
||||||
|
# optional if `direct_message=false`
|
||||||
|
#secret_key = "<nostr nsec>"
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
[Unit]
|
||||||
|
Description=nostr-rs-relay
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
User=REPLACE_WITH_YOUR_USERNAME
|
||||||
|
WorkingDirectory=/var/lib/nostr-rs-relay
|
||||||
|
Environment=RUST_LOG=warn,nostr_rs_relay=info
|
||||||
|
ExecStart=/usr/bin/nostr-rs-relay --config /etc/nostr-rs-relay/config.toml
|
||||||
|
TimeoutStopSec=10
|
||||||
|
Restart=on-failure
|
||||||
|
RestartSec=5
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
||||||
@@ -78,18 +78,24 @@ PRAGMA foreign_keys = ON;
|
|||||||
delete from event where event_hash=x'00000000000c1271675dc86e3e1dd1336827bccabb90dc4c9d3b4465efefe00e';
|
delete from event where event_hash=x'00000000000c1271675dc86e3e1dd1336827bccabb90dc4c9d3b4465efefe00e';
|
||||||
```
|
```
|
||||||
|
|
||||||
### Deleting All Events for Pubkey
|
### Querying and Deleting All Events for Pubkey
|
||||||
|
|
||||||
```console
|
```console
|
||||||
PRAGMA foreign_keys = ON;
|
PRAGMA foreign_keys = ON;
|
||||||
|
|
||||||
|
select lower(hex(author)) as author, count(*) as c from event group by author order by c asc;
|
||||||
|
|
||||||
delete from event where author=x'000000000002c7831d9c5a99f183afc2813a6f69a16edda7f6fc0ed8110566e6';
|
delete from event where author=x'000000000002c7831d9c5a99f183afc2813a6f69a16edda7f6fc0ed8110566e6';
|
||||||
```
|
```
|
||||||
|
|
||||||
### Deleting All Events of a Kind
|
### Querying and Deleting All Events of a Kind
|
||||||
|
|
||||||
|
|
||||||
```console
|
```console
|
||||||
PRAGMA foreign_keys = ON;
|
PRAGMA foreign_keys = ON;
|
||||||
|
|
||||||
|
select printf('%7d', kind), count(*) as c from event group by kind order by c;
|
||||||
|
|
||||||
delete from event where kind=70202;
|
delete from event where kind=70202;
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -106,7 +112,8 @@ seen" policy.
|
|||||||
|
|
||||||
```console
|
```console
|
||||||
PRAGMA foreign_keys = ON;
|
PRAGMA foreign_keys = ON;
|
||||||
TODO!
|
|
||||||
|
DELETE FROM event WHERE first_seen < CAST(strftime('%s', date('now', '-30 day')) AS INT);
|
||||||
```
|
```
|
||||||
|
|
||||||
### Delete Profile Events with No Recent Events
|
### Delete Profile Events with No Recent Events
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ and reduce spam and abuse.
|
|||||||
|
|
||||||
This will likely evolve substantially, the first goal is to get a
|
This will likely evolve substantially, the first goal is to get a
|
||||||
basic one-way service that lets an externalized program decide on
|
basic one-way service that lets an externalized program decide on
|
||||||
event persistance. This does not represent the final state of gRPC
|
event persistence. This does not represent the final state of gRPC
|
||||||
extensibility in `nostr-rs-relay`.
|
extensibility in `nostr-rs-relay`.
|
||||||
|
|
||||||
## Considerations
|
## Considerations
|
||||||
|
|||||||
@@ -0,0 +1,84 @@
|
|||||||
|
# Pay to Relay Design Document
|
||||||
|
|
||||||
|
The relay with use payment as a form of spam prevention. In order to post to the relay a user must pay a set rate. There is also the option to require a payment for each note posted to the relay. There is no cost to read from the relay.
|
||||||
|
|
||||||
|
## Configuration
|
||||||
|
|
||||||
|
Currently, [LNBits](https://github.com/lnbits/lnbits) is implemented as the payment processor. LNBits exposes a simple API for creating invoices, to use this API create a wallet and on the right side find "API info" you will need to add the invoice/read key to this relays config file.
|
||||||
|
|
||||||
|
The below configuration will need to be added to config.toml
|
||||||
|
```
|
||||||
|
[pay_to_relay]
|
||||||
|
# Enable pay to relay
|
||||||
|
enabled = true
|
||||||
|
# The cost to be admitted to relay
|
||||||
|
admission_cost = 1000
|
||||||
|
# The cost in sats per post
|
||||||
|
cost_per_event = 0
|
||||||
|
# Url of lnbits api
|
||||||
|
node_url = "https://<IP of node>:5001/api/v1/payments"
|
||||||
|
# LNBits api secret
|
||||||
|
api_secret = "<LNbits api key>"
|
||||||
|
# Terms of service
|
||||||
|
terms_message = """This service ....
|
||||||
|
"""
|
||||||
|
# Whether or not new sign ups should be allowed
|
||||||
|
sign_ups = true
|
||||||
|
secret_key = "<nostr secret key to send dms>"
|
||||||
|
```
|
||||||
|
|
||||||
|
The LNBits instance must have a signed HTTPS a self signed certificate will not work.
|
||||||
|
|
||||||
|
## Design Overview
|
||||||
|
|
||||||
|
### Concepts
|
||||||
|
|
||||||
|
All authors are initially not admitted to write to the relay. There are two ways to gain access write to the relay. The first is by attempting to post the the relay, upon receiving an event from an author that is not admitted, the relay will send a direct message including the terms of service of the relay and a lighting invoice for the admission cost. Once this invoice is paid the author can write to the relay. For this method to work the author must be reading from the relay. An author can also pay and accept the terms of service via a webpage `https://<relay-url>/join`.
|
||||||
|
|
||||||
|
## Design Details
|
||||||
|
|
||||||
|
Authors are stored in a dedicated table. This tracks:
|
||||||
|
|
||||||
|
* `pubkey`
|
||||||
|
* `is_admitted` whether on no the admission invoice has been paid, accepting the terms of service.
|
||||||
|
* `balance` the current balance in sats of the author, used if there is a cost per post
|
||||||
|
* `tos_accepted_at` the timestamp of when the author accepted the tos
|
||||||
|
|
||||||
|
Invoice information is stored in a dedicated table. This tracks:
|
||||||
|
* `payment_hash` the payment hash of the lighting invoice
|
||||||
|
* `pubkey` of the author the invoice is issued to
|
||||||
|
* `invoice` bolt11 invoice
|
||||||
|
* `amount` in sats
|
||||||
|
* `status` (Paid/Unpaid/Expired)
|
||||||
|
* `description`
|
||||||
|
* `created_at` timestamp of creation
|
||||||
|
* `confirmed_at` timestamp of payment
|
||||||
|
|
||||||
|
### Event Handling
|
||||||
|
|
||||||
|
If "pay to relay" is enabled, all incoming events are evaluated to determine whether the author is on the relay's whitelist or if they have paid the admission fee and accepted the terms. If "pay per note" is enabled, there is an additional check to ensure that the author has enough balance, which is then reduced by the cost per note. If the author is on the whitelist, this balance check is not necessary.
|
||||||
|
|
||||||
|
### Integration
|
||||||
|
|
||||||
|
We have an existing database writer thread, which receives events and
|
||||||
|
attempts to persist them to disk. Once validated and persisted, these
|
||||||
|
events are broadcast to all subscribers.
|
||||||
|
|
||||||
|
When "pay to relay" is enabled, the writer must check if the author is admitted to post. If the author is not admitted to post the event is forwarded to the payment module. Where an invoice is generated, persisted and broadcast as an direct message to the author.
|
||||||
|
|
||||||
|
### Threat Scenarios
|
||||||
|
|
||||||
|
Some of these mitigation's are fully implemented, others are documented
|
||||||
|
simply to demonstrate a mitigation is possible.
|
||||||
|
|
||||||
|
### Sign up Spamming
|
||||||
|
|
||||||
|
*Threat*: An attacker generates a large number of new pubkeys publishing to the relays. Causing a large number of new invoices to be created for each new pubkey.
|
||||||
|
|
||||||
|
*Mitigation*: Rate limit number of new sign ups
|
||||||
|
|
||||||
|
### Admitted Author Spamming
|
||||||
|
|
||||||
|
*Threat*: An attacker gains write access by paying the admission fee, and then floods the relay with a large number of spam events.
|
||||||
|
|
||||||
|
*Mitigation*: The attacker's admission can be revoked and their admission fee will not be refunded. Enabling "cost per event" and increasing the admission cost can also discourage this type of behavior.
|
||||||
+91
-3
@@ -1,8 +1,8 @@
|
|||||||
# Reverse Proxy Setup Guide
|
# Reverse Proxy Setup Guide
|
||||||
|
|
||||||
It is recommended to run `nostr-rs-relay` behind a reverse proxy such
|
It is recommended to run `nostr-rs-relay` behind a reverse proxy such
|
||||||
as `haproxy` or `nginx` to provide TLS termination. Simple examples
|
as `haproxy`, `nginx` or `traefik` to provide TLS termination. Simple examples
|
||||||
of `haproxy` and `nginx` configurations are documented here.
|
for `haproxy`, `nginx` and `traefik` configurations are documented here.
|
||||||
|
|
||||||
## Minimal HAProxy Configuration
|
## Minimal HAProxy Configuration
|
||||||
|
|
||||||
@@ -29,7 +29,7 @@ frontend fe_prod
|
|||||||
bind :80
|
bind :80
|
||||||
http-request set-header X-Forwarded-Proto https if { ssl_fc }
|
http-request set-header X-Forwarded-Proto https if { ssl_fc }
|
||||||
redirect scheme https code 301 if !{ ssl_fc }
|
redirect scheme https code 301 if !{ ssl_fc }
|
||||||
acl host_relay hdr(host) -i relay.example.com
|
acl host_relay hdr(host) -i -m beg relay.example.com
|
||||||
use_backend relay if host_relay
|
use_backend relay if host_relay
|
||||||
# HSTS (1 year)
|
# HSTS (1 year)
|
||||||
http-response set-header Strict-Transport-Security max-age=31536000
|
http-response set-header Strict-Transport-Security max-age=31536000
|
||||||
@@ -109,3 +109,91 @@ The above configuration was tested on `nginx` `1.18.0` on `Ubuntu` `20.04` and `
|
|||||||
For help installing `nginx` on `Ubuntu`, see [this guide](https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-20-04).
|
For help installing `nginx` on `Ubuntu`, see [this guide](https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-20-04).
|
||||||
|
|
||||||
For guidance on using `letsencrypt` to obtain a cert on `Ubuntu`, including an `nginx` plugin, see [this post](https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-20-04).
|
For guidance on using `letsencrypt` to obtain a cert on `Ubuntu`, including an `nginx` plugin, see [this post](https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-20-04).
|
||||||
|
|
||||||
|
|
||||||
|
## Example Traefik Configuration
|
||||||
|
|
||||||
|
Assumptions:
|
||||||
|
|
||||||
|
* `Traefik` version is `2.9` (other versions not tested).
|
||||||
|
* `Traefik` is used for provisioning of Let's Encrypt certificates.
|
||||||
|
* `Traefik` is running in `Docker`, using `docker compose` and labels for the static configuration. An equivalent setup using a Traefik config file is possible too (but not covered here).
|
||||||
|
* Strict Transport Security is enabled.
|
||||||
|
* Hostname for the relay is `relay.example.com`, email address for ACME certificates provider is `name@example.com`.
|
||||||
|
* ipv6 is enabled, a viable private ipv6 subnet is specified in the example below.
|
||||||
|
* Relay is running on port `8080`.
|
||||||
|
|
||||||
|
```
|
||||||
|
version: '3'
|
||||||
|
|
||||||
|
networks:
|
||||||
|
nostr:
|
||||||
|
enable_ipv6: true
|
||||||
|
ipam:
|
||||||
|
config:
|
||||||
|
- subnet: fd00:db8:a::/64
|
||||||
|
gateway: fd00:db8:a::1
|
||||||
|
|
||||||
|
services:
|
||||||
|
traefik:
|
||||||
|
image: traefik:v2.9
|
||||||
|
networks:
|
||||||
|
nostr:
|
||||||
|
command:
|
||||||
|
- "--log.level=ERROR"
|
||||||
|
# letsencrypt configuration
|
||||||
|
- "--certificatesResolvers.http.acme.email==name@example.com"
|
||||||
|
- "--certificatesResolvers.http.acme.storage=/certs/acme.json"
|
||||||
|
- "--certificatesResolvers.http.acme.httpChallenge.entryPoint=http"
|
||||||
|
# define entrypoints
|
||||||
|
- "--entryPoints.http.address=:80"
|
||||||
|
- "--entryPoints.http.http.redirections.entryPoint.to=https"
|
||||||
|
- "--entryPoints.http.http.redirections.entryPoint.scheme=https"
|
||||||
|
- "--entryPoints.https.address=:443"
|
||||||
|
- "--entryPoints.https.forwardedHeaders.insecure=true"
|
||||||
|
- "--entryPoints.https.proxyProtocol.insecure=true"
|
||||||
|
# docker provider (get configuration from container labels)
|
||||||
|
- "--providers.docker.endpoint=unix:///var/run/docker.sock"
|
||||||
|
- "--providers.docker.exposedByDefault=false"
|
||||||
|
- "--providers.file.directory=/config"
|
||||||
|
- "--providers.file.watch=true"
|
||||||
|
ports:
|
||||||
|
- "80:80"
|
||||||
|
- "443:443"
|
||||||
|
volumes:
|
||||||
|
- "/var/run/docker.sock:/var/run/docker.sock:ro"
|
||||||
|
- "$(pwd)/traefik/certs:/certs"
|
||||||
|
- "$(pwd)/traefik/config:/config"
|
||||||
|
logging:
|
||||||
|
driver: "local"
|
||||||
|
restart: always
|
||||||
|
|
||||||
|
# example nostr config. only labels: section is relevant for Traefik config
|
||||||
|
nostr:
|
||||||
|
image: nostr-rs-relay:latest
|
||||||
|
container_name: nostr-relay
|
||||||
|
networks:
|
||||||
|
nostr:
|
||||||
|
restart: always
|
||||||
|
user: 100:100
|
||||||
|
volumes:
|
||||||
|
- '$(pwd)/nostr/data:/usr/src/app/db:Z'
|
||||||
|
- '$(pwd)/nostr/config/config.toml:/usr/src/app/config.toml:ro,Z'
|
||||||
|
labels:
|
||||||
|
- "traefik.enable=true"
|
||||||
|
- "traefik.http.routers.nostr.entrypoints=https"
|
||||||
|
- "traefik.http.routers.nostr.rule=Host(`relay.example.com`)"
|
||||||
|
- "traefik.http.routers.nostr.tls.certresolver=http"
|
||||||
|
- "traefik.http.routers.nostr.service=nostr"
|
||||||
|
- "traefik.http.services.nostr.loadbalancer.server.port=8080"
|
||||||
|
- "traefik.http.services.nostr.loadbalancer.passHostHeader=true"
|
||||||
|
- "traefik.http.middlewares.nostr.headers.sslredirect=true"
|
||||||
|
- "traefik.http.middlewares.nostr.headers.stsincludesubdomains=true"
|
||||||
|
- "traefik.http.middlewares.nostr.headers.stspreload=true"
|
||||||
|
- "traefik.http.middlewares.nostr.headers.stsseconds=63072000"
|
||||||
|
- "traefik.http.routers.nostr.middlewares=nostr"
|
||||||
|
```
|
||||||
|
|
||||||
|
### Traefik Notes
|
||||||
|
|
||||||
|
Traefik will take care of the provisioning and renewal of certificates. In case of an ipv4-only relay, simply detele the `enable_ipv6:` and `ipam:` entries in the `networks:` section of the docker-compose file.
|
||||||
|
|||||||
@@ -0,0 +1,39 @@
|
|||||||
|
# Run as a linux system process
|
||||||
|
|
||||||
|
Docker makes it easy to spin up and down environments but it's also possible to run `nostr-rs-relay` as a systemd linux process.
|
||||||
|
This guide assumes you're on a Linux machine and that Rust is already installed.
|
||||||
|
|
||||||
|
## Instructions
|
||||||
|
|
||||||
|
### Build nostr-rs-relay from source
|
||||||
|
Start by building the application from source. Here is how to do that:
|
||||||
|
1. `git clone https://github.com/scsibug/nostr-rs-relay.git`
|
||||||
|
2. `cd nostr-rs-relay`
|
||||||
|
3. `cargo build --release`
|
||||||
|
|
||||||
|
### Place the files where they belong
|
||||||
|
We want to place the nostr-rs-relay binary and the config.toml file where they belong. While still in the root level of the nostr-rs-relay folder you cloned in last step, run the following commands:
|
||||||
|
1. `sudo cp target/release/nostr-rs-relay /usr/local/bin/`
|
||||||
|
2. `sudo mkdir /etc/nostr-rs-relay`
|
||||||
|
2. `sudo cp config.toml /etc/nostr-rs-relay`
|
||||||
|
|
||||||
|
### Create the Systemd service file
|
||||||
|
We need to create a new Systemd service file. These files are placed in the `/etc/systemd/system/` folder where you will find many other services running.
|
||||||
|
|
||||||
|
1. `sudo vim /etc/systemd/system/nostr-rs-relay.service`
|
||||||
|
2. Paste in the contents of [this service file](../contrib/nostr-rs-relay.service). Remember to replace the `User` value with your own username.
|
||||||
|
3. Save the file and exit your text editor
|
||||||
|
|
||||||
|
|
||||||
|
### Run the service
|
||||||
|
To get the service running, we need to reload the systemd daemon and enable the service.
|
||||||
|
|
||||||
|
1. `sudo systemctl daemon-reload`
|
||||||
|
2. `sudo systemctl start nostr-rs-relay.service`
|
||||||
|
3. `sudo systemctl status nostr-rs-relay.service`
|
||||||
|
|
||||||
|
|
||||||
|
### Tips
|
||||||
|
|
||||||
|
#### Logs
|
||||||
|
The application will write logs to the journal. To read it, execute `sudo journalctl -f -u nostr-rs-relay`
|
||||||
@@ -179,7 +179,7 @@ attempts to persist them to disk. Once validated and persisted, these
|
|||||||
events are broadcast to all subscribers.
|
events are broadcast to all subscribers.
|
||||||
|
|
||||||
When verification is enabled, the writer must check to ensure a valid,
|
When verification is enabled, the writer must check to ensure a valid,
|
||||||
unexpired verification record exists for the auther. All metadata
|
unexpired verification record exists for the author. All metadata
|
||||||
events (regardless of verification status) are forwarded to a verifier
|
events (regardless of verification status) are forwarded to a verifier
|
||||||
module. If the verifier determines a new verification record is
|
module. If the verifier determines a new verification record is
|
||||||
needed, it is also responsible for persisting and broadcasting the
|
needed, it is also responsible for persisting and broadcasting the
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
tonic_build::configure()
|
tonic_build::configure()
|
||||||
.build_server(false)
|
.build_server(true)
|
||||||
.protoc_arg("--experimental_allow_proto3_optional")
|
.protoc_arg("--experimental_allow_proto3_optional")
|
||||||
.compile(&["../../proto/nauthz.proto"], &["../../proto"])?;
|
.compile(&["../../proto/nauthz.proto"], &["../../proto"])?;
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
+96
-90
@@ -1,16 +1,16 @@
|
|||||||
|
use nostr_rs_relay::config;
|
||||||
|
use nostr_rs_relay::error::{Error, Result};
|
||||||
|
use nostr_rs_relay::event::{single_char_tagname, Event};
|
||||||
|
use nostr_rs_relay::repo::sqlite::{build_pool, PooledConnection};
|
||||||
|
use nostr_rs_relay::repo::sqlite_migration::{curr_db_version, DB_VERSION};
|
||||||
|
use nostr_rs_relay::utils::is_lower_hex;
|
||||||
|
use rusqlite::params;
|
||||||
|
use rusqlite::{OpenFlags, Transaction};
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use nostr_rs_relay::utils::is_lower_hex;
|
|
||||||
use tracing::info;
|
|
||||||
use nostr_rs_relay::config;
|
|
||||||
use nostr_rs_relay::event::{Event,single_char_tagname};
|
|
||||||
use nostr_rs_relay::error::{Error, Result};
|
|
||||||
use nostr_rs_relay::repo::sqlite::{PooledConnection, build_pool};
|
|
||||||
use nostr_rs_relay::repo::sqlite_migration::{curr_db_version, DB_VERSION};
|
|
||||||
use rusqlite::{OpenFlags, Transaction};
|
|
||||||
use std::sync::mpsc;
|
use std::sync::mpsc;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use rusqlite::params;
|
use tracing::info;
|
||||||
|
|
||||||
/// Bulk load JSONL data from STDIN to the database specified in config.toml (or ./nostr.db as a default).
|
/// Bulk load JSONL data from STDIN to the database specified in config.toml (or ./nostr.db as a default).
|
||||||
/// The database must already exist, this will not create a new one.
|
/// The database must already exist, this will not create a new one.
|
||||||
@@ -20,95 +20,101 @@ pub fn main() -> Result<()> {
|
|||||||
let _trace_sub = tracing_subscriber::fmt::try_init();
|
let _trace_sub = tracing_subscriber::fmt::try_init();
|
||||||
println!("Nostr-rs-relay Bulk Loader");
|
println!("Nostr-rs-relay Bulk Loader");
|
||||||
// check for a database file, or create one.
|
// check for a database file, or create one.
|
||||||
let settings = config::Settings::new(&None);
|
let settings = config::Settings::new(&None)?;
|
||||||
if !Path::new(&settings.database.data_directory).is_dir() {
|
if !Path::new(&settings.database.data_directory).is_dir() {
|
||||||
info!("Database directory does not exist");
|
info!("Database directory does not exist");
|
||||||
return Err(Error::DatabaseDirError);
|
return Err(Error::DatabaseDirError);
|
||||||
}
|
}
|
||||||
// Get a database pool
|
// Get a database pool
|
||||||
let pool = build_pool("bulk-loader", &settings, OpenFlags::SQLITE_OPEN_READ_WRITE | OpenFlags::SQLITE_OPEN_CREATE, 1,4,false);
|
let pool = build_pool(
|
||||||
|
"bulk-loader",
|
||||||
|
&settings,
|
||||||
|
OpenFlags::SQLITE_OPEN_READ_WRITE | OpenFlags::SQLITE_OPEN_CREATE,
|
||||||
|
1,
|
||||||
|
4,
|
||||||
|
false,
|
||||||
|
);
|
||||||
{
|
{
|
||||||
// check for database schema version
|
// check for database schema version
|
||||||
let mut conn: PooledConnection = pool.get()?;
|
let mut conn: PooledConnection = pool.get()?;
|
||||||
let version = curr_db_version(&mut conn)?;
|
let version = curr_db_version(&mut conn)?;
|
||||||
info!("current version is: {:?}", version);
|
info!("current version is: {:?}", version);
|
||||||
// ensure the schema version is current.
|
// ensure the schema version is current.
|
||||||
if version != DB_VERSION {
|
if version != DB_VERSION {
|
||||||
info!("version is not current, exiting");
|
info!("version is not current, exiting");
|
||||||
panic!("cannot write to schema other than v{DB_VERSION}");
|
panic!("cannot write to schema other than v{DB_VERSION}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// this channel will contain parsed events ready to be inserted
|
// this channel will contain parsed events ready to be inserted
|
||||||
let (event_tx, event_rx) = mpsc::sync_channel(100_000);
|
let (event_tx, event_rx) = mpsc::sync_channel(100_000);
|
||||||
// Thread for reading events
|
// Thread for reading events
|
||||||
let _stdin_reader_handler = thread::spawn(move || {
|
let _stdin_reader_handler = thread::spawn(move || {
|
||||||
let stdin = io::stdin();
|
let stdin = io::stdin();
|
||||||
for readline in stdin.lines() {
|
for readline in stdin.lines() {
|
||||||
if let Ok(line) = readline {
|
if let Ok(line) = readline {
|
||||||
// try to parse a nostr event
|
// try to parse a nostr event
|
||||||
let eres: Result<Event, serde_json::Error> = serde_json::from_str(&line);
|
let eres: Result<Event, serde_json::Error> = serde_json::from_str(&line);
|
||||||
if let Ok(mut e) = eres {
|
if let Ok(mut e) = eres {
|
||||||
if let Ok(()) = e.validate() {
|
if let Ok(()) = e.validate() {
|
||||||
e.build_index();
|
e.build_index();
|
||||||
//debug!("Event: {:?}", e);
|
//debug!("Event: {:?}", e);
|
||||||
event_tx.send(Some(e)).ok();
|
event_tx.send(Some(e)).ok();
|
||||||
} else {
|
} else {
|
||||||
info!("could not validate event");
|
info!("could not validate event");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
info!("error reading event: {:?}", eres);
|
info!("error reading event: {:?}", eres);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// error reading
|
// error reading
|
||||||
info!("error reading: {:?}", readline);
|
info!("error reading: {:?}", readline);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
info!("finished parsing events");
|
info!("finished parsing events");
|
||||||
event_tx.send(None).ok();
|
event_tx.send(None).ok();
|
||||||
let ok: Result<()> = Ok(());
|
let ok: Result<()> = Ok(());
|
||||||
ok
|
ok
|
||||||
});
|
});
|
||||||
let mut conn: PooledConnection = pool.get()?;
|
let mut conn: PooledConnection = pool.get()?;
|
||||||
let mut events_read = 0;
|
let mut events_read = 0;
|
||||||
let event_batch_size =50_000;
|
let event_batch_size = 50_000;
|
||||||
let mut new_events = 0;
|
let mut new_events = 0;
|
||||||
let mut has_more_events = true;
|
let mut has_more_events = true;
|
||||||
while has_more_events {
|
while has_more_events {
|
||||||
// begin a transaction
|
// begin a transaction
|
||||||
let tx = conn.transaction()?;
|
let tx = conn.transaction()?;
|
||||||
// read in batch_size events and commit
|
// read in batch_size events and commit
|
||||||
for _ in 0..event_batch_size {
|
for _ in 0..event_batch_size {
|
||||||
match event_rx.recv() {
|
match event_rx.recv() {
|
||||||
Ok(Some(e)) => {
|
Ok(Some(e)) => {
|
||||||
events_read += 1;
|
events_read += 1;
|
||||||
// ignore ephemeral events
|
// ignore ephemeral events
|
||||||
if !(e.kind >= 20000 && e.kind < 30000) {
|
if !(e.kind >= 20000 && e.kind < 30000) {
|
||||||
match write_event(&tx, e) {
|
match write_event(&tx, e) {
|
||||||
Ok(c) => {
|
Ok(c) => {
|
||||||
new_events += c;
|
new_events += c;
|
||||||
},
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
info!("error inserting event: {:?}", e);
|
info!("error inserting event: {:?}", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
Ok(None) => {
|
Ok(None) => {
|
||||||
// signal that the sender will never produce more
|
// signal that the sender will never produce more
|
||||||
// events
|
// events
|
||||||
has_more_events=false;
|
has_more_events = false;
|
||||||
break;
|
break;
|
||||||
},
|
}
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
info!("sender is closed");
|
info!("sender is closed");
|
||||||
// sender is done
|
// sender is done
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
info!("committed {} events...", new_events);
|
info!("committed {} events...", new_events);
|
||||||
tx.commit()?;
|
tx.commit()?;
|
||||||
conn.execute_batch("pragma wal_checkpoint(truncate)")?;
|
conn.execute_batch("pragma wal_checkpoint(truncate)")?;
|
||||||
|
|
||||||
}
|
}
|
||||||
info!("processed {} events", events_read);
|
info!("processed {} events", events_read);
|
||||||
info!("stored {} new events", new_events);
|
info!("stored {} new events", new_events);
|
||||||
@@ -131,7 +137,7 @@ fn write_event(tx: &Transaction, e: Event) -> Result<usize> {
|
|||||||
params![id_blob, e.created_at, e.kind, pubkey_blob, delegator_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 {
|
||||||
return Ok(0);
|
return Ok(0);
|
||||||
}
|
}
|
||||||
// we want to capture the event_id that had the tag, the tag name, and the tag hex value.
|
// we want to capture the event_id that had the tag, the tag name, and the tag hex value.
|
||||||
let event_id = tx.last_insert_rowid();
|
let event_id = tx.last_insert_rowid();
|
||||||
@@ -140,30 +146,30 @@ fn write_event(tx: &Transaction, e: Event) -> Result<usize> {
|
|||||||
let tagname = t.get(0).unwrap();
|
let tagname = t.get(0).unwrap();
|
||||||
let tagnamechar_opt = single_char_tagname(tagname);
|
let tagnamechar_opt = single_char_tagname(tagname);
|
||||||
if tagnamechar_opt.is_none() {
|
if tagnamechar_opt.is_none() {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// safe because len was > 1
|
// safe because len was > 1
|
||||||
let tagval = t.get(1).unwrap();
|
let tagval = t.get(1).unwrap();
|
||||||
// insert as BLOB if we can restore it losslessly.
|
// insert as BLOB if we can restore it losslessly.
|
||||||
// this means it needs to be even length and lowercase.
|
// this means it needs to be even length and lowercase.
|
||||||
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
|
||||||
tx.execute(
|
tx.execute(
|
||||||
"INSERT INTO tag (event_id, name, value) VALUES (?1, ?2, ?3);",
|
"INSERT INTO tag (event_id, name, value) VALUES (?1, ?2, ?3);",
|
||||||
params![event_id, tagname, &tagval],
|
params![event_id, tagname, &tagval],
|
||||||
)?;
|
)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if e.is_replaceable() {
|
if e.is_replaceable() {
|
||||||
//let query = "SELECT id FROM event WHERE kind=? AND author=? ORDER BY created_at DESC LIMIT 1;";
|
//let query = "SELECT id FROM event WHERE kind=? AND author=? ORDER BY created_at DESC LIMIT 1;";
|
||||||
//let count: usize = tx.query_row(query, params![e.kind, pubkey_blob], |row| row.get(0))?;
|
//let count: usize = tx.query_row(query, params![e.kind, pubkey_blob], |row| row.get(0))?;
|
||||||
//info!("found {} rows that /would/ be preserved", count);
|
//info!("found {} rows that /would/ be preserved", count);
|
||||||
match tx.execute(
|
match tx.execute(
|
||||||
"DELETE FROM event WHERE kind=? and author=? and id NOT IN (SELECT id FROM event WHERE kind=? AND author=? ORDER BY created_at DESC LIMIT 1);",
|
"DELETE FROM event WHERE kind=? and author=? and id NOT IN (SELECT id FROM event WHERE kind=? AND author=? ORDER BY created_at DESC LIMIT 1);",
|
||||||
params![e.kind, pubkey_blob, e.kind, pubkey_blob],
|
params![e.kind, pubkey_blob, e.kind, pubkey_blob],
|
||||||
) {
|
) {
|
||||||
|
|||||||
+2
-2
@@ -7,14 +7,14 @@ pub struct CLIArgs {
|
|||||||
short,
|
short,
|
||||||
long,
|
long,
|
||||||
help = "Use the <directory> as the location of the database",
|
help = "Use the <directory> as the location of the database",
|
||||||
required = false,
|
required = false
|
||||||
)]
|
)]
|
||||||
pub db: Option<String>,
|
pub db: Option<String>,
|
||||||
#[arg(
|
#[arg(
|
||||||
short,
|
short,
|
||||||
long,
|
long,
|
||||||
help = "Use the <file name> as the location of the config file",
|
help = "Use the <file name> as the location of the config file",
|
||||||
required = false,
|
required = false
|
||||||
)]
|
)]
|
||||||
pub config: Option<String>,
|
pub config: Option<String>,
|
||||||
}
|
}
|
||||||
|
|||||||
+88
-15
@@ -1,8 +1,8 @@
|
|||||||
//! Configuration file and settings management
|
//! Configuration file and settings management
|
||||||
|
use crate::payment::Processor;
|
||||||
use config::{Config, ConfigError, File};
|
use config::{Config, ConfigError, File};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use tracing::warn;
|
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||||
#[allow(unused)]
|
#[allow(unused)]
|
||||||
@@ -13,6 +13,7 @@ pub struct Info {
|
|||||||
pub pubkey: Option<String>,
|
pub pubkey: Option<String>,
|
||||||
pub contact: Option<String>,
|
pub contact: Option<String>,
|
||||||
pub favicon: Option<String>,
|
pub favicon: Option<String>,
|
||||||
|
pub relay_icon: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
@@ -24,6 +25,7 @@ pub struct Database {
|
|||||||
pub min_conn: u32,
|
pub min_conn: u32,
|
||||||
pub max_conn: u32,
|
pub max_conn: u32,
|
||||||
pub connection: String,
|
pub connection: String,
|
||||||
|
pub connection_write: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
@@ -69,14 +71,31 @@ pub struct Limits {
|
|||||||
pub max_ws_frame_bytes: Option<usize>,
|
pub max_ws_frame_bytes: Option<usize>,
|
||||||
pub broadcast_buffer: usize, // events to buffer for subscribers (prevents slow readers from consuming memory)
|
pub broadcast_buffer: usize, // events to buffer for subscribers (prevents slow readers from consuming memory)
|
||||||
pub event_persist_buffer: usize, // events to buffer for database commits (block senders if database writes are too slow)
|
pub event_persist_buffer: usize, // events to buffer for database commits (block senders if database writes are too slow)
|
||||||
pub event_kind_blacklist: Option<Vec<u64>>
|
pub event_kind_blacklist: Option<Vec<u64>>,
|
||||||
|
pub event_kind_allowlist: Option<Vec<u64>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
#[allow(unused)]
|
#[allow(unused)]
|
||||||
pub struct Authorization {
|
pub struct Authorization {
|
||||||
pub pubkey_whitelist: Option<Vec<String>>, // If present, only allow these pubkeys to publish events
|
pub pubkey_whitelist: Option<Vec<String>>, // If present, only allow these pubkeys to publish events
|
||||||
pub nip42_auth: bool, // if true enables NIP-42 authentication
|
pub nip42_auth: bool, // if true enables NIP-42 authentication
|
||||||
|
pub nip42_dms: bool, // if true send DMs only to their authenticated recipients
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
#[allow(unused)]
|
||||||
|
pub struct PayToRelay {
|
||||||
|
pub enabled: bool,
|
||||||
|
pub admission_cost: u64, // Cost to have pubkey whitelisted
|
||||||
|
pub cost_per_event: u64, // Cost author to pay per event
|
||||||
|
pub node_url: String,
|
||||||
|
pub api_secret: String,
|
||||||
|
pub terms_message: String,
|
||||||
|
pub sign_ups: bool, // allow new users to sign up to relay
|
||||||
|
pub direct_message: bool, // Send direct message to user with invoice and terms
|
||||||
|
pub secret_key: Option<String>,
|
||||||
|
pub processor: Processor,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
@@ -147,6 +166,13 @@ impl VerifiedUsers {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
#[allow(unused)]
|
||||||
|
pub struct Logging {
|
||||||
|
pub folder_path: Option<String>,
|
||||||
|
pub file_prefix: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
#[allow(unused)]
|
#[allow(unused)]
|
||||||
pub struct Settings {
|
pub struct Settings {
|
||||||
@@ -157,38 +183,48 @@ pub struct Settings {
|
|||||||
pub network: Network,
|
pub network: Network,
|
||||||
pub limits: Limits,
|
pub limits: Limits,
|
||||||
pub authorization: Authorization,
|
pub authorization: Authorization,
|
||||||
|
pub pay_to_relay: PayToRelay,
|
||||||
pub verified_users: VerifiedUsers,
|
pub verified_users: VerifiedUsers,
|
||||||
pub retention: Retention,
|
pub retention: Retention,
|
||||||
pub options: Options,
|
pub options: Options,
|
||||||
|
pub logging: Logging,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Settings {
|
impl Settings {
|
||||||
#[must_use]
|
pub fn new(config_file_name: &Option<String>) -> Result<Self, ConfigError> {
|
||||||
pub fn new(config_file_name: &Option<String>) -> Self {
|
|
||||||
let default_settings = Self::default();
|
let default_settings = Self::default();
|
||||||
// attempt to construct settings with file
|
// attempt to construct settings with file
|
||||||
let from_file = Self::new_from_default(&default_settings, config_file_name);
|
let from_file = Self::new_from_default(&default_settings, config_file_name);
|
||||||
match from_file {
|
match from_file {
|
||||||
Ok(f) => f,
|
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
warn!("Error reading config file ({:?})", e);
|
// pass up the parse error if the config file was specified,
|
||||||
default_settings
|
// otherwise use the default config (with a warning).
|
||||||
|
if config_file_name.is_some() {
|
||||||
|
Err(e)
|
||||||
|
} else {
|
||||||
|
eprintln!("Error reading config file ({:?})", e);
|
||||||
|
eprintln!("WARNING: Default configuration settings will be used");
|
||||||
|
Ok(default_settings)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
ok => ok,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn new_from_default(
|
||||||
fn new_from_default(default: &Settings, config_file_name: &Option<String>) -> Result<Self, ConfigError> {
|
default: &Settings,
|
||||||
|
config_file_name: &Option<String>,
|
||||||
|
) -> Result<Self, ConfigError> {
|
||||||
let default_config_file_name = "config.toml".to_string();
|
let default_config_file_name = "config.toml".to_string();
|
||||||
let config: &String = match config_file_name {
|
let config: &String = match config_file_name {
|
||||||
Some(value) => value,
|
Some(value) => value,
|
||||||
None => &default_config_file_name
|
None => &default_config_file_name,
|
||||||
};
|
};
|
||||||
let builder = Config::builder();
|
let builder = Config::builder();
|
||||||
let config: Config = builder
|
let config: Config = builder
|
||||||
// use defaults
|
// use defaults
|
||||||
.add_source(Config::try_from(default)?)
|
.add_source(Config::try_from(default)?)
|
||||||
// override with file contents
|
// override with file contents
|
||||||
.add_source(File::with_name(config))
|
.add_source(File::with_name(config))
|
||||||
.build()?;
|
.build()?;
|
||||||
let mut settings: Settings = config.try_deserialize()?;
|
let mut settings: Settings = config.try_deserialize()?;
|
||||||
@@ -206,6 +242,23 @@ impl Settings {
|
|||||||
);
|
);
|
||||||
// initialize durations for verified users
|
// initialize durations for verified users
|
||||||
settings.verified_users.init();
|
settings.verified_users.init();
|
||||||
|
|
||||||
|
// Validate pay to relay settings
|
||||||
|
if settings.pay_to_relay.enabled {
|
||||||
|
assert_ne!(settings.pay_to_relay.api_secret, "");
|
||||||
|
// Should check that url is valid
|
||||||
|
assert_ne!(settings.pay_to_relay.node_url, "");
|
||||||
|
assert_ne!(settings.pay_to_relay.terms_message, "");
|
||||||
|
|
||||||
|
if settings.pay_to_relay.direct_message {
|
||||||
|
assert_ne!(
|
||||||
|
settings.pay_to_relay.secret_key,
|
||||||
|
Some("<nostr nsec>".to_string())
|
||||||
|
);
|
||||||
|
assert!(settings.pay_to_relay.secret_key.is_some());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Ok(settings)
|
Ok(settings)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -220,6 +273,7 @@ impl Default for Settings {
|
|||||||
pubkey: None,
|
pubkey: None,
|
||||||
contact: None,
|
contact: None,
|
||||||
favicon: None,
|
favicon: None,
|
||||||
|
relay_icon: None,
|
||||||
},
|
},
|
||||||
diagnostics: Diagnostics { tracing: false },
|
diagnostics: Diagnostics { tracing: false },
|
||||||
database: Database {
|
database: Database {
|
||||||
@@ -228,7 +282,8 @@ impl Default for Settings {
|
|||||||
in_memory: false,
|
in_memory: false,
|
||||||
min_conn: 4,
|
min_conn: 4,
|
||||||
max_conn: 8,
|
max_conn: 8,
|
||||||
connection: "".to_owned(),
|
connection: "".to_owned(),
|
||||||
|
connection_write: None,
|
||||||
},
|
},
|
||||||
grpc: Grpc {
|
grpc: Grpc {
|
||||||
event_admission_server: None,
|
event_admission_server: None,
|
||||||
@@ -250,10 +305,24 @@ impl Default for Settings {
|
|||||||
broadcast_buffer: 16384,
|
broadcast_buffer: 16384,
|
||||||
event_persist_buffer: 4096,
|
event_persist_buffer: 4096,
|
||||||
event_kind_blacklist: None,
|
event_kind_blacklist: None,
|
||||||
|
event_kind_allowlist: None,
|
||||||
},
|
},
|
||||||
authorization: Authorization {
|
authorization: Authorization {
|
||||||
pubkey_whitelist: None, // Allow any address to publish
|
pubkey_whitelist: None, // Allow any address to publish
|
||||||
nip42_auth: false, // Disable NIP-42 authentication
|
nip42_auth: false, // Disable NIP-42 authentication
|
||||||
|
nip42_dms: false, // Send DMs to everybody
|
||||||
|
},
|
||||||
|
pay_to_relay: PayToRelay {
|
||||||
|
enabled: false,
|
||||||
|
admission_cost: 4200,
|
||||||
|
cost_per_event: 0,
|
||||||
|
terms_message: "".to_string(),
|
||||||
|
node_url: "".to_string(),
|
||||||
|
api_secret: "".to_string(),
|
||||||
|
sign_ups: false,
|
||||||
|
direct_message: true,
|
||||||
|
secret_key: None,
|
||||||
|
processor: Processor::LNBits,
|
||||||
},
|
},
|
||||||
verified_users: VerifiedUsers {
|
verified_users: VerifiedUsers {
|
||||||
mode: VerifiedUsersMode::Disabled,
|
mode: VerifiedUsersMode::Disabled,
|
||||||
@@ -274,6 +343,10 @@ impl Default for Settings {
|
|||||||
options: Options {
|
options: Options {
|
||||||
reject_future_seconds: None, // Reject events in the future if defined
|
reject_future_seconds: None, // Reject events in the future if defined
|
||||||
},
|
},
|
||||||
|
logging: Logging {
|
||||||
|
folder_path: None,
|
||||||
|
file_prefix: None,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+9
-9
@@ -156,17 +156,17 @@ impl ClientConn {
|
|||||||
self.auth = Challenge(Uuid::new_v4().to_string());
|
self.auth = Challenge(Uuid::new_v4().to_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn authenticate(&mut self, event: &Event, relay_url: &String) -> Result<()> {
|
pub fn authenticate(&mut self, event: &Event, relay_url: &str) -> Result<()> {
|
||||||
match &self.auth {
|
match &self.auth {
|
||||||
Challenge(_) => (),
|
Challenge(_) => (),
|
||||||
AuthPubkey(_) => {
|
AuthPubkey(_) => {
|
||||||
// already authenticated
|
// already authenticated
|
||||||
return Ok(())
|
return Ok(());
|
||||||
},
|
}
|
||||||
NoAuth => {
|
NoAuth => {
|
||||||
// unexpected AUTH request
|
// unexpected AUTH request
|
||||||
return Err(Error::AuthFailure);
|
return Err(Error::AuthFailure);
|
||||||
},
|
}
|
||||||
}
|
}
|
||||||
match event.validate() {
|
match event.validate() {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
@@ -181,15 +181,15 @@ impl ClientConn {
|
|||||||
return Err(Error::AuthFailure);
|
return Err(Error::AuthFailure);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut challenge: Option<&String> = None;
|
let mut challenge: Option<&str> = None;
|
||||||
let mut relay: Option<&String> = None;
|
let mut relay: Option<&str> = None;
|
||||||
|
|
||||||
for tag in &event.tags {
|
for tag in &event.tags {
|
||||||
if tag.len() == 2 && tag.get(0) == Some(&"challenge".into()) {
|
if tag.len() == 2 && tag.get(0) == Some(&"challenge".into()) {
|
||||||
challenge = tag.get(1);
|
challenge = tag.get(1).map(|x| x.as_str());
|
||||||
}
|
}
|
||||||
if tag.len() == 2 && tag.get(0) == Some(&"relay".into()) {
|
if tag.len() == 2 && tag.get(0) == Some(&"relay".into()) {
|
||||||
relay = tag.get(1);
|
relay = tag.get(1).map(|x| x.as_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -204,7 +204,7 @@ impl ClientConn {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
match (relay.and_then(|url| host_str(url)), host_str(relay_url)) {
|
match (relay.and_then(host_str), host_str(relay_url)) {
|
||||||
(Some(received_relay), Some(our_relay)) => {
|
(Some(received_relay), Some(our_relay)) => {
|
||||||
if received_relay != our_relay {
|
if received_relay != our_relay {
|
||||||
return Err(Error::AuthFailure);
|
return Err(Error::AuthFailure);
|
||||||
|
|||||||
@@ -2,22 +2,25 @@
|
|||||||
use crate::config::Settings;
|
use crate::config::Settings;
|
||||||
use crate::error::{Error, Result};
|
use crate::error::{Error, Result};
|
||||||
use crate::event::Event;
|
use crate::event::Event;
|
||||||
use crate::notice::Notice;
|
|
||||||
use crate::server::NostrMetrics;
|
|
||||||
use crate::nauthz;
|
use crate::nauthz;
|
||||||
|
use crate::notice::Notice;
|
||||||
|
use crate::payment::PaymentMessage;
|
||||||
|
use crate::repo::postgres::{PostgresPool, PostgresRepo};
|
||||||
|
use crate::repo::sqlite::SqliteRepo;
|
||||||
|
use crate::repo::NostrRepo;
|
||||||
|
use crate::server::NostrMetrics;
|
||||||
use governor::clock::Clock;
|
use governor::clock::Clock;
|
||||||
use governor::{Quota, RateLimiter};
|
use governor::{Quota, RateLimiter};
|
||||||
|
use log::LevelFilter;
|
||||||
|
use nostr::key::FromPkStr;
|
||||||
|
use nostr::key::Keys;
|
||||||
use r2d2;
|
use r2d2;
|
||||||
use std::sync::Arc;
|
|
||||||
use std::thread;
|
|
||||||
use sqlx::pool::PoolOptions;
|
use sqlx::pool::PoolOptions;
|
||||||
use sqlx::postgres::PgConnectOptions;
|
use sqlx::postgres::PgConnectOptions;
|
||||||
use sqlx::ConnectOptions;
|
use sqlx::ConnectOptions;
|
||||||
use crate::repo::sqlite::SqliteRepo;
|
use std::sync::Arc;
|
||||||
use crate::repo::postgres::{PostgresRepo,PostgresPool};
|
use std::thread;
|
||||||
use crate::repo::NostrRepo;
|
use std::time::{Duration, Instant};
|
||||||
use std::time::{Instant, Duration};
|
|
||||||
use tracing::log::LevelFilter;
|
|
||||||
use tracing::{debug, info, trace, warn};
|
use tracing::{debug, info, trace, warn};
|
||||||
|
|
||||||
pub type SqlitePool = r2d2::Pool<r2d2_sqlite::SqliteConnectionManager>;
|
pub type SqlitePool = r2d2::Pool<r2d2_sqlite::SqliteConnectionManager>;
|
||||||
@@ -42,8 +45,8 @@ pub const DB_FILE: &str = "nostr.db";
|
|||||||
/// Will panic if the pool could not be created.
|
/// Will panic if the pool could not be created.
|
||||||
pub async fn build_repo(settings: &Settings, metrics: NostrMetrics) -> Arc<dyn NostrRepo> {
|
pub async fn build_repo(settings: &Settings, metrics: NostrMetrics) -> Arc<dyn NostrRepo> {
|
||||||
match settings.database.engine.as_str() {
|
match settings.database.engine.as_str() {
|
||||||
"sqlite" => {Arc::new(build_sqlite_pool(settings, metrics).await)},
|
"sqlite" => Arc::new(build_sqlite_pool(settings, metrics).await),
|
||||||
"postgres" => {Arc::new(build_postgres_pool(settings, metrics).await)},
|
"postgres" => Arc::new(build_postgres_pool(settings, metrics).await),
|
||||||
_ => panic!("Unknown database engine"),
|
_ => panic!("Unknown database engine"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -67,7 +70,26 @@ async fn build_postgres_pool(settings: &Settings, metrics: NostrMetrics) -> Post
|
|||||||
.connect_with(options)
|
.connect_with(options)
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let repo = PostgresRepo::new(pool, metrics);
|
|
||||||
|
let write_pool: PostgresPool = match &settings.database.connection_write {
|
||||||
|
Some(cfg_write) => {
|
||||||
|
let mut options_write: PgConnectOptions = cfg_write.as_str().parse().unwrap();
|
||||||
|
options_write.log_statements(LevelFilter::Debug);
|
||||||
|
options_write.log_slow_statements(LevelFilter::Warn, Duration::from_secs(60));
|
||||||
|
|
||||||
|
PoolOptions::new()
|
||||||
|
.max_connections(settings.database.max_conn)
|
||||||
|
.min_connections(settings.database.min_conn)
|
||||||
|
.idle_timeout(Duration::from_secs(60))
|
||||||
|
.connect_with(options_write)
|
||||||
|
.await
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
None => pool.clone(),
|
||||||
|
};
|
||||||
|
|
||||||
|
let repo = PostgresRepo::new(pool, write_pool, metrics);
|
||||||
|
|
||||||
// Panic on migration failure
|
// Panic on migration failure
|
||||||
let version = repo.migrate_up().await.unwrap();
|
let version = repo.migrate_up().await.unwrap();
|
||||||
info!("Postgres migration completed, at v{}", version);
|
info!("Postgres migration completed, at v{}", version);
|
||||||
@@ -83,6 +105,7 @@ pub async fn db_writer(
|
|||||||
mut event_rx: tokio::sync::mpsc::Receiver<SubmittedEvent>,
|
mut event_rx: tokio::sync::mpsc::Receiver<SubmittedEvent>,
|
||||||
bcast_tx: tokio::sync::broadcast::Sender<Event>,
|
bcast_tx: tokio::sync::broadcast::Sender<Event>,
|
||||||
metadata_tx: tokio::sync::broadcast::Sender<Event>,
|
metadata_tx: tokio::sync::broadcast::Sender<Event>,
|
||||||
|
payment_tx: tokio::sync::broadcast::Sender<PaymentMessage>,
|
||||||
mut shutdown: tokio::sync::broadcast::Receiver<()>,
|
mut shutdown: tokio::sync::broadcast::Receiver<()>,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
// are we performing NIP-05 checking?
|
// are we performing NIP-05 checking?
|
||||||
@@ -90,6 +113,10 @@ pub async fn db_writer(
|
|||||||
// are we requriing NIP-05 user verification?
|
// are we requriing NIP-05 user verification?
|
||||||
let nip05_enabled = settings.verified_users.is_enabled();
|
let nip05_enabled = settings.verified_users.is_enabled();
|
||||||
|
|
||||||
|
let pay_to_relay_enabled = settings.pay_to_relay.enabled;
|
||||||
|
let cost_per_event = settings.pay_to_relay.cost_per_event;
|
||||||
|
debug!("Pay to relay: {}", pay_to_relay_enabled);
|
||||||
|
|
||||||
//upgrade_db(&mut pool.get()?)?;
|
//upgrade_db(&mut pool.get()?)?;
|
||||||
|
|
||||||
// Make a copy of the whitelist
|
// Make a copy of the whitelist
|
||||||
@@ -116,8 +143,8 @@ pub async fn db_writer(
|
|||||||
};
|
};
|
||||||
|
|
||||||
//let gprc_client = settings.grpc.event_admission_server.map(|s| {
|
//let gprc_client = settings.grpc.event_admission_server.map(|s| {
|
||||||
// event_admitter_connect(&s);
|
// event_admitter_connect(&s);
|
||||||
// });
|
// });
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
if shutdown.try_recv().is_ok() {
|
if shutdown.try_recv().is_ok() {
|
||||||
@@ -136,24 +163,6 @@ pub async fn db_writer(
|
|||||||
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.
|
|
||||||
if let Some(allowed_addrs) = whitelist {
|
|
||||||
// TODO: incorporate delegated pubkeys
|
|
||||||
// if the event address is not in allowed_addrs.
|
|
||||||
if !allowed_addrs.contains(&event.pubkey) {
|
|
||||||
debug!(
|
|
||||||
"rejecting event: {}, unauthorized author",
|
|
||||||
event.get_event_id_prefix()
|
|
||||||
);
|
|
||||||
notice_tx
|
|
||||||
.try_send(Notice::blocked(
|
|
||||||
event.id,
|
|
||||||
"pubkey is not allowed to publish to this relay",
|
|
||||||
))
|
|
||||||
.ok();
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check that event kind isn't blacklisted
|
// Check that event kind isn't blacklisted
|
||||||
let kinds_blacklist = &settings.limits.event_kind_blacklist.clone();
|
let kinds_blacklist = &settings.limits.event_kind_blacklist.clone();
|
||||||
@@ -165,15 +174,113 @@ pub async fn db_writer(
|
|||||||
&event.kind
|
&event.kind
|
||||||
);
|
);
|
||||||
notice_tx
|
notice_tx
|
||||||
.try_send(Notice::blocked(
|
.try_send(Notice::blocked(event.id, "event kind is blocked by relay"))
|
||||||
event.id,
|
|
||||||
"event kind is blocked by relay"
|
|
||||||
))
|
|
||||||
.ok();
|
.ok();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check that event kind isn't allowlisted
|
||||||
|
let kinds_allowlist = &settings.limits.event_kind_allowlist.clone();
|
||||||
|
if let Some(event_kind_allowlist) = kinds_allowlist {
|
||||||
|
if !event_kind_allowlist.contains(&event.kind) {
|
||||||
|
debug!(
|
||||||
|
"rejecting event: {}, allowlist kind: {}",
|
||||||
|
&event.get_event_id_prefix(),
|
||||||
|
&event.kind
|
||||||
|
);
|
||||||
|
notice_tx
|
||||||
|
.try_send(Notice::blocked(event.id, "event kind is blocked by relay"))
|
||||||
|
.ok();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set to none until balance is got from db
|
||||||
|
// Will stay none if user in whitelisted and does not have to pay to post
|
||||||
|
// When pay to relay is enabled the whitelist is not a list of who can post
|
||||||
|
// It is a list of who can post for free
|
||||||
|
let mut user_balance: Option<u64> = None;
|
||||||
|
if !pay_to_relay_enabled {
|
||||||
|
// check if this event is authorized.
|
||||||
|
if let Some(allowed_addrs) = whitelist {
|
||||||
|
// TODO: incorporate delegated pubkeys
|
||||||
|
// if the event address is not in allowed_addrs.
|
||||||
|
if !allowed_addrs.contains(&event.pubkey) {
|
||||||
|
debug!(
|
||||||
|
"rejecting event: {}, unauthorized author",
|
||||||
|
event.get_event_id_prefix()
|
||||||
|
);
|
||||||
|
notice_tx
|
||||||
|
.try_send(Notice::blocked(
|
||||||
|
event.id,
|
||||||
|
"pubkey is not allowed to publish to this relay",
|
||||||
|
))
|
||||||
|
.ok();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// If the user is on whitelist there is no need to check if the user is admitted or has balance to post
|
||||||
|
if whitelist.is_none()
|
||||||
|
|| (whitelist.is_some() && !whitelist.as_ref().unwrap().contains(&event.pubkey))
|
||||||
|
{
|
||||||
|
let key = Keys::from_pk_str(&event.pubkey).unwrap();
|
||||||
|
match repo.get_account_balance(&key).await {
|
||||||
|
Ok((user_admitted, balance)) => {
|
||||||
|
// Checks to make sure user is admitted
|
||||||
|
if !user_admitted {
|
||||||
|
debug!("user: {}, is not admitted", &event.pubkey);
|
||||||
|
|
||||||
|
// If the user is in DB but not admitted
|
||||||
|
// Send meeage to payment thread to check if outstanding invoice has been paid
|
||||||
|
payment_tx
|
||||||
|
.send(PaymentMessage::CheckAccount(event.pubkey))
|
||||||
|
.ok();
|
||||||
|
notice_tx
|
||||||
|
.try_send(Notice::blocked(event.id, "User is not admitted"))
|
||||||
|
.ok();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Checks that user has enough balance to post
|
||||||
|
// TODO: this should send an invoice to user to top up
|
||||||
|
if balance < cost_per_event {
|
||||||
|
debug!("user: {}, does not have a balance", &event.pubkey,);
|
||||||
|
notice_tx
|
||||||
|
.try_send(Notice::blocked(event.id, "Insufficient balance"))
|
||||||
|
.ok();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
user_balance = Some(balance);
|
||||||
|
debug!("User balance: {:?}", user_balance);
|
||||||
|
}
|
||||||
|
Err(
|
||||||
|
Error::SqlError(rusqlite::Error::QueryReturnedNoRows)
|
||||||
|
| Error::SqlxError(sqlx::Error::RowNotFound),
|
||||||
|
) => {
|
||||||
|
// User does not exist
|
||||||
|
info!("Unregistered user");
|
||||||
|
if settings.pay_to_relay.sign_ups {
|
||||||
|
payment_tx
|
||||||
|
.send(PaymentMessage::NewAccount(event.pubkey))
|
||||||
|
.ok();
|
||||||
|
}
|
||||||
|
let msg = "Pubkey not registered";
|
||||||
|
notice_tx.try_send(Notice::error(event.id, msg)).ok();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
warn!("Error checking admission status: {:?}", err);
|
||||||
|
let msg = "relay experienced an error checking your admission status";
|
||||||
|
notice_tx.try_send(Notice::error(event.id, msg)).ok();
|
||||||
|
// Other error
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// send any metadata events to the NIP-05 verifier
|
// send any metadata events to the NIP-05 verifier
|
||||||
if nip05_active && event.is_kind_metadata() {
|
if nip05_active && event.is_kind_metadata() {
|
||||||
// we are sending this prior to even deciding if we
|
// we are sending this prior to even deciding if we
|
||||||
@@ -200,7 +307,6 @@ pub async fn db_writer(
|
|||||||
uv.name.to_string(),
|
uv.name.to_string(),
|
||||||
event.get_author_prefix()
|
event.get_author_prefix()
|
||||||
);
|
);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
info!(
|
info!(
|
||||||
"rejecting event, author ({:?} / {:?}) verification invalid (expired/wrong domain)",
|
"rejecting event, author ({:?} / {:?}) verification invalid (expired/wrong domain)",
|
||||||
@@ -216,7 +322,10 @@ pub async fn db_writer(
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(Error::SqlError(rusqlite::Error::QueryReturnedNoRows)) => {
|
Err(
|
||||||
|
Error::SqlError(rusqlite::Error::QueryReturnedNoRows)
|
||||||
|
| Error::SqlxError(sqlx::Error::RowNotFound),
|
||||||
|
) => {
|
||||||
debug!(
|
debug!(
|
||||||
"no verification records found for pubkey: {:?}",
|
"no verification records found for pubkey: {:?}",
|
||||||
event.get_author_prefix()
|
event.get_author_prefix()
|
||||||
@@ -237,27 +346,44 @@ pub async fn db_writer(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// nip05 address
|
// nip05 address
|
||||||
let nip05_address : Option<crate::nip05::Nip05Name> = validation.and_then(|x| x.ok().map(|y| y.name));
|
let nip05_address: Option<crate::nip05::Nip05Name> =
|
||||||
|
validation.and_then(|x| x.ok().map(|y| y.name));
|
||||||
|
|
||||||
// GRPC check
|
// GRPC check
|
||||||
if let Some(ref mut c) = grpc_client {
|
if let Some(ref mut c) = grpc_client {
|
||||||
trace!("checking if grpc permits");
|
trace!("checking if grpc permits");
|
||||||
let grpc_start = Instant::now();
|
let grpc_start = Instant::now();
|
||||||
let decision_res = c.admit_event(&event, &subm_event.source_ip, subm_event.origin, subm_event.user_agent, nip05_address, subm_event.auth_pubkey).await;
|
let decision_res = c
|
||||||
|
.admit_event(
|
||||||
|
&event,
|
||||||
|
&subm_event.source_ip,
|
||||||
|
subm_event.origin,
|
||||||
|
subm_event.user_agent,
|
||||||
|
nip05_address,
|
||||||
|
subm_event.auth_pubkey,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
match decision_res {
|
match decision_res {
|
||||||
Ok(decision) => {
|
Ok(decision) => {
|
||||||
if !decision.permitted() {
|
if !decision.permitted() {
|
||||||
// GPRC returned a decision to reject this event
|
// GPRC returned a decision to reject this event
|
||||||
info!("GRPC rejected event: {:?} (kind: {}) from: {:?} in: {:?} (IP: {:?})",
|
info!(
|
||||||
event.get_event_id_prefix(),
|
"GRPC rejected event: {:?} (kind: {}) from: {:?} in: {:?} (IP: {:?})",
|
||||||
event.kind,
|
event.get_event_id_prefix(),
|
||||||
event.get_author_prefix(),
|
event.kind,
|
||||||
grpc_start.elapsed(),
|
event.get_author_prefix(),
|
||||||
subm_event.source_ip);
|
grpc_start.elapsed(),
|
||||||
notice_tx.try_send(Notice::blocked(event.id, &decision.message().unwrap_or_else(|| "".to_string()))).ok();
|
subm_event.source_ip
|
||||||
|
);
|
||||||
|
notice_tx
|
||||||
|
.try_send(Notice::blocked(
|
||||||
|
event.id,
|
||||||
|
&decision.message().unwrap_or_default(),
|
||||||
|
))
|
||||||
|
.ok();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
warn!("GRPC server error: {:?}", e);
|
warn!("GRPC server error: {:?}", e);
|
||||||
}
|
}
|
||||||
@@ -306,6 +432,17 @@ pub async fn db_writer(
|
|||||||
|
|
||||||
// use rate limit, if defined, and if an event was actually written.
|
// use rate limit, if defined, and if an event was actually written.
|
||||||
if event_write {
|
if event_write {
|
||||||
|
// If pay to relay is diabaled or the cost per event is 0
|
||||||
|
// No need to update user balance
|
||||||
|
if pay_to_relay_enabled && cost_per_event > 0 {
|
||||||
|
// If the user balance is some, user was not on whitelist
|
||||||
|
// Their balance should be reduced by the cost per event
|
||||||
|
if let Some(_balance) = user_balance {
|
||||||
|
let pubkey = Keys::from_pk_str(&event.pubkey)?;
|
||||||
|
repo.update_account_balance(&pubkey, false, cost_per_event)
|
||||||
|
.await?;
|
||||||
|
}
|
||||||
|
}
|
||||||
if let Some(ref lim) = lim_opt {
|
if let Some(ref lim) = lim_opt {
|
||||||
if let Err(n) = lim.check() {
|
if let Err(n) = lim.check() {
|
||||||
let wait_for = n.wait_time_from(clock.now());
|
let wait_for = n.wait_time_from(clock.now());
|
||||||
|
|||||||
+6
-3
@@ -84,7 +84,8 @@ pub struct ConditionQuery {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl ConditionQuery {
|
impl ConditionQuery {
|
||||||
#[must_use] pub fn allows_event(&self, event: &Event) -> bool {
|
#[must_use]
|
||||||
|
pub fn allows_event(&self, event: &Event) -> bool {
|
||||||
// check each condition, to ensure that the event complies
|
// check each condition, to ensure that the event complies
|
||||||
// with the restriction.
|
// with the restriction.
|
||||||
for c in &self.conditions {
|
for c in &self.conditions {
|
||||||
@@ -101,7 +102,8 @@ impl ConditionQuery {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Verify that the delegator approved the delegation; return a ConditionQuery if so.
|
// Verify that the delegator approved the delegation; return a ConditionQuery if so.
|
||||||
#[must_use] pub fn validate_delegation(
|
#[must_use]
|
||||||
|
pub fn validate_delegation(
|
||||||
delegator: &str,
|
delegator: &str,
|
||||||
delegatee: &str,
|
delegatee: &str,
|
||||||
cond_query: &str,
|
cond_query: &str,
|
||||||
@@ -144,7 +146,8 @@ pub struct Condition {
|
|||||||
|
|
||||||
impl Condition {
|
impl Condition {
|
||||||
/// Check if this condition allows the given event to be delegated
|
/// Check if this condition allows the given event to be delegated
|
||||||
#[must_use] pub fn allows_event(&self, event: &Event) -> bool {
|
#[must_use]
|
||||||
|
pub fn allows_event(&self, event: &Event) -> bool {
|
||||||
// determine what the right-hand side of the operator is
|
// determine what the right-hand side of the operator is
|
||||||
let resolved_field = match &self.field {
|
let resolved_field = match &self.field {
|
||||||
Field::Kind => event.kind,
|
Field::Kind => event.kind,
|
||||||
|
|||||||
@@ -72,6 +72,16 @@ pub enum Error {
|
|||||||
AuthFailure,
|
AuthFailure,
|
||||||
#[error("I/O Error")]
|
#[error("I/O Error")]
|
||||||
IoError(std::io::Error),
|
IoError(std::io::Error),
|
||||||
|
#[error("Event builder error")]
|
||||||
|
EventError(nostr::event::builder::Error),
|
||||||
|
#[error("Nostr key error")]
|
||||||
|
NostrKeyError(nostr::key::Error),
|
||||||
|
#[error("Payment hash mismatch")]
|
||||||
|
PaymentHash,
|
||||||
|
#[error("Error parsing url")]
|
||||||
|
URLParseError(url::ParseError),
|
||||||
|
#[error("HTTP error")]
|
||||||
|
HTTPError(http::Error),
|
||||||
#[error("Unknown/Undocumented")]
|
#[error("Unknown/Undocumented")]
|
||||||
UnknownError,
|
UnknownError,
|
||||||
}
|
}
|
||||||
@@ -153,3 +163,30 @@ impl From<std::io::Error> for Error {
|
|||||||
Error::IoError(r)
|
Error::IoError(r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
impl From<nostr::event::builder::Error> for Error {
|
||||||
|
/// Wrap event builder error
|
||||||
|
fn from(r: nostr::event::builder::Error) -> Self {
|
||||||
|
Error::EventError(r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<nostr::key::Error> for Error {
|
||||||
|
/// Wrap nostr key error
|
||||||
|
fn from(r: nostr::key::Error) -> Self {
|
||||||
|
Error::NostrKeyError(r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<url::ParseError> for Error {
|
||||||
|
/// Wrap nostr key error
|
||||||
|
fn from(r: url::ParseError) -> Self {
|
||||||
|
Error::URLParseError(r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<http::Error> for Error {
|
||||||
|
/// Wrap nostr key error
|
||||||
|
fn from(r: http::Error) -> Self {
|
||||||
|
Error::HTTPError(r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
+98
-74
@@ -1,7 +1,12 @@
|
|||||||
//! Event parsing and validation
|
//! Event parsing and validation
|
||||||
use crate::delegation::validate_delegation;
|
use crate::delegation::validate_delegation;
|
||||||
use crate::error::Error::{CommandUnknownError, EventCouldNotCanonicalize, EventInvalidId, EventInvalidSignature, EventMalformedPubkey};
|
use crate::error::Error::{
|
||||||
|
CommandUnknownError, EventCouldNotCanonicalize, EventInvalidId, EventInvalidSignature,
|
||||||
|
EventMalformedPubkey,
|
||||||
|
};
|
||||||
use crate::error::Result;
|
use crate::error::Result;
|
||||||
|
use crate::event::EventWrapper::WrappedAuth;
|
||||||
|
use crate::event::EventWrapper::WrappedEvent;
|
||||||
use crate::nip05;
|
use crate::nip05;
|
||||||
use crate::utils::unix_time;
|
use crate::utils::unix_time;
|
||||||
use bitcoin_hashes::{sha256, Hash};
|
use bitcoin_hashes::{sha256, Hash};
|
||||||
@@ -14,8 +19,6 @@ use std::collections::HashMap;
|
|||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use tracing::{debug, info};
|
use tracing::{debug, info};
|
||||||
use crate::event::EventWrapper::WrappedEvent;
|
|
||||||
use crate::event::EventWrapper::WrappedAuth;
|
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
/// Secp256k1 verification instance.
|
/// Secp256k1 verification instance.
|
||||||
@@ -30,7 +33,8 @@ pub struct EventCmd {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl EventCmd {
|
impl EventCmd {
|
||||||
#[must_use] pub fn event_id(&self) -> &str {
|
#[must_use]
|
||||||
|
pub fn event_id(&self) -> &str {
|
||||||
&self.event.id
|
&self.event.id
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -67,7 +71,8 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Attempt to form a single-char tag name.
|
/// Attempt to form a single-char tag name.
|
||||||
#[must_use] pub fn single_char_tagname(tagname: &str) -> Option<char> {
|
#[must_use]
|
||||||
|
pub fn single_char_tagname(tagname: &str) -> Option<char> {
|
||||||
// We return the tag character if and only if the tagname consists
|
// We return the tag character if and only if the tagname consists
|
||||||
// of a single char.
|
// of a single char.
|
||||||
let mut tagnamechars = tagname.chars();
|
let mut tagnamechars = tagname.chars();
|
||||||
@@ -85,10 +90,9 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub enum EventWrapper {
|
pub enum EventWrapper {
|
||||||
WrappedEvent(Event),
|
WrappedEvent(Event),
|
||||||
WrappedAuth(Event)
|
WrappedAuth(Event),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Convert network event to parsed/validated event.
|
/// Convert network event to parsed/validated event.
|
||||||
@@ -114,7 +118,8 @@ impl From<EventCmd> for Result<EventWrapper> {
|
|||||||
|
|
||||||
impl Event {
|
impl Event {
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
#[must_use] pub fn simple_event() -> Event {
|
#[must_use]
|
||||||
|
pub fn simple_event() -> Event {
|
||||||
Event {
|
Event {
|
||||||
id: "0".to_owned(),
|
id: "0".to_owned(),
|
||||||
pubkey: "0".to_owned(),
|
pubkey: "0".to_owned(),
|
||||||
@@ -128,12 +133,14 @@ impl Event {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[must_use] pub fn is_kind_metadata(&self) -> bool {
|
#[must_use]
|
||||||
|
pub fn is_kind_metadata(&self) -> bool {
|
||||||
self.kind == 0
|
self.kind == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Should this event be persisted?
|
/// Should this event be persisted?
|
||||||
#[must_use] pub fn is_ephemeral(&self) -> bool {
|
#[must_use]
|
||||||
|
pub fn is_ephemeral(&self) -> bool {
|
||||||
self.kind >= 20000 && self.kind < 30000
|
self.kind >= 20000 && self.kind < 30000
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -149,40 +156,50 @@ impl Event {
|
|||||||
/// Determine the time at which this event should expire
|
/// Determine the time at which this event should expire
|
||||||
pub fn expiration(&self) -> Option<u64> {
|
pub fn expiration(&self) -> Option<u64> {
|
||||||
let default = "".to_string();
|
let default = "".to_string();
|
||||||
let dvals:Vec<&String> = self.tags
|
let dvals: Vec<&String> = self
|
||||||
|
.tags
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|x| !x.is_empty())
|
.filter(|x| !x.is_empty())
|
||||||
.filter(|x| x.get(0).unwrap() == "expiration")
|
.filter(|x| x.get(0).unwrap() == "expiration")
|
||||||
.map(|x| x.get(1).unwrap_or(&default)).take(1)
|
.map(|x| x.get(1).unwrap_or(&default))
|
||||||
|
.take(1)
|
||||||
.collect();
|
.collect();
|
||||||
let val_first = dvals.get(0);
|
let val_first = dvals.get(0);
|
||||||
val_first.and_then(|t| t.parse::<u64>().ok())
|
val_first.and_then(|t| t.parse::<u64>().ok())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Should this event be replaced with newer timestamps from same author?
|
/// Should this event be replaced with newer timestamps from same author?
|
||||||
#[must_use] pub fn is_replaceable(&self) -> bool {
|
#[must_use]
|
||||||
self.kind == 0 || self.kind == 3 || self.kind == 41 || (self.kind >= 10000 && self.kind < 20000)
|
pub fn is_replaceable(&self) -> bool {
|
||||||
|
self.kind == 0
|
||||||
|
|| self.kind == 3
|
||||||
|
|| self.kind == 41
|
||||||
|
|| (self.kind >= 10000 && self.kind < 20000)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Should this event be replaced with newer timestamps from same author, for distinct `d` tag values?
|
/// Should this event be replaced with newer timestamps from same author, for distinct `d` tag values?
|
||||||
#[must_use] pub fn is_param_replaceable(&self) -> bool {
|
#[must_use]
|
||||||
|
pub fn is_param_replaceable(&self) -> bool {
|
||||||
self.kind >= 30000 && self.kind < 40000
|
self.kind >= 30000 && self.kind < 40000
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Should this event be replaced with newer timestamps from same author, for distinct `d` tag values?
|
/// Should this event be replaced with newer timestamps from same author, for distinct `d` tag values?
|
||||||
#[must_use] pub fn distinct_param(&self) -> Option<String> {
|
#[must_use]
|
||||||
|
pub fn distinct_param(&self) -> Option<String> {
|
||||||
if self.is_param_replaceable() {
|
if self.is_param_replaceable() {
|
||||||
let default = "".to_string();
|
let default = "".to_string();
|
||||||
let dvals:Vec<&String> = self.tags
|
let dvals: Vec<&String> = self
|
||||||
|
.tags
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|x| !x.is_empty())
|
.filter(|x| !x.is_empty())
|
||||||
.filter(|x| x.get(0).unwrap() == "d")
|
.filter(|x| x.get(0).unwrap() == "d")
|
||||||
.map(|x| x.get(1).unwrap_or(&default)).take(1)
|
.map(|x| x.get(1).unwrap_or(&default))
|
||||||
|
.take(1)
|
||||||
.collect();
|
.collect();
|
||||||
let dval_first = dvals.get(0);
|
let dval_first = dvals.get(0);
|
||||||
match dval_first {
|
match dval_first {
|
||||||
Some(_) => {dval_first.map(|x| x.to_string())},
|
Some(_) => dval_first.map(|x| x.to_string()),
|
||||||
None => Some(default)
|
None => Some(default),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
@@ -190,7 +207,8 @@ impl Event {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Pull a NIP-05 Name out of the event, if one exists
|
/// Pull a NIP-05 Name out of the event, if one exists
|
||||||
#[must_use] pub fn get_nip05_addr(&self) -> Option<nip05::Nip05Name> {
|
#[must_use]
|
||||||
|
pub fn get_nip05_addr(&self) -> Option<nip05::Nip05Name> {
|
||||||
if self.is_kind_metadata() {
|
if self.is_kind_metadata() {
|
||||||
// very quick check if we should attempt to parse this json
|
// very quick check if we should attempt to parse this json
|
||||||
if self.content.contains("\"nip05\"") {
|
if self.content.contains("\"nip05\"") {
|
||||||
@@ -207,7 +225,8 @@ impl Event {
|
|||||||
// is this event delegated (properly)?
|
// is this event delegated (properly)?
|
||||||
// does the signature match, and are conditions valid?
|
// does the signature match, and are conditions valid?
|
||||||
// if so, return an alternate author for the event
|
// if so, return an alternate author for the event
|
||||||
#[must_use] pub fn delegated_author(&self) -> Option<String> {
|
#[must_use]
|
||||||
|
pub fn delegated_author(&self) -> Option<String> {
|
||||||
// is there a delegation tag?
|
// is there a delegation tag?
|
||||||
let delegation_tag: Vec<String> = self
|
let delegation_tag: Vec<String> = self
|
||||||
.tags
|
.tags
|
||||||
@@ -215,7 +234,8 @@ impl Event {
|
|||||||
.filter(|x| x.len() == 4)
|
.filter(|x| x.len() == 4)
|
||||||
.filter(|x| x.get(0).unwrap() == "delegation")
|
.filter(|x| x.get(0).unwrap() == "delegation")
|
||||||
.take(1)
|
.take(1)
|
||||||
.next()?.clone(); // get first tag
|
.next()?
|
||||||
|
.clone(); // get first tag
|
||||||
|
|
||||||
//let delegation_tag = self.tag_values_by_name("delegation");
|
//let delegation_tag = self.tag_values_by_name("delegation");
|
||||||
// delegation tags should have exactly 3 elements after the name (pubkey, condition, sig)
|
// delegation tags should have exactly 3 elements after the name (pubkey, condition, sig)
|
||||||
@@ -275,15 +295,18 @@ impl Event {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Create a short event identifier, suitable for logging.
|
/// Create a short event identifier, suitable for logging.
|
||||||
#[must_use] pub fn get_event_id_prefix(&self) -> String {
|
#[must_use]
|
||||||
|
pub fn get_event_id_prefix(&self) -> String {
|
||||||
self.id.chars().take(8).collect()
|
self.id.chars().take(8).collect()
|
||||||
}
|
}
|
||||||
#[must_use] pub fn get_author_prefix(&self) -> String {
|
#[must_use]
|
||||||
|
pub fn get_author_prefix(&self) -> String {
|
||||||
self.pubkey.chars().take(8).collect()
|
self.pubkey.chars().take(8).collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Retrieve tag initial values across all tags matching the name
|
/// Retrieve tag initial values across all tags matching the name
|
||||||
#[must_use] pub fn tag_values_by_name(&self, tag_name: &str) -> Vec<String> {
|
#[must_use]
|
||||||
|
pub fn tag_values_by_name(&self, tag_name: &str) -> Vec<String> {
|
||||||
self.tags
|
self.tags
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|x| x.len() > 1)
|
.filter(|x| x.len() > 1)
|
||||||
@@ -292,7 +315,8 @@ impl Event {
|
|||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[must_use] pub fn is_valid_timestamp(&self, reject_future_seconds: Option<usize>) -> bool {
|
#[must_use]
|
||||||
|
pub fn is_valid_timestamp(&self, reject_future_seconds: Option<usize>) -> bool {
|
||||||
if let Some(allowable_future) = reject_future_seconds {
|
if let Some(allowable_future) = reject_future_seconds {
|
||||||
let curr_time = unix_time();
|
let curr_time = unix_time();
|
||||||
// calculate difference, plus how far future we allow
|
// calculate difference, plus how far future we allow
|
||||||
@@ -331,7 +355,7 @@ impl Event {
|
|||||||
return Err(EventInvalidId);
|
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).map_err(|_| EventInvalidSignature)?;
|
||||||
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) {
|
||||||
SECP.verify_schnorr(&sig, &msg, &pubkey)
|
SECP.verify_schnorr(&sig, &msg, &pubkey)
|
||||||
@@ -384,7 +408,8 @@ impl Event {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Determine if the given tag and value set intersect with tags in this event.
|
/// Determine if the given tag and value set intersect with tags in this event.
|
||||||
#[must_use] pub fn generic_tag_val_intersect(&self, tagname: char, check: &HashSet<String>) -> bool {
|
#[must_use]
|
||||||
|
pub fn generic_tag_val_intersect(&self, tagname: char, check: &HashSet<String>) -> bool {
|
||||||
match &self.tagidx {
|
match &self.tagidx {
|
||||||
// check if this is indexable tagname
|
// check if this is indexable tagname
|
||||||
Some(idx) => match idx.get(&tagname) {
|
Some(idx) => match idx.get(&tagname) {
|
||||||
@@ -399,6 +424,22 @@ impl Event {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<nostr::Event> for Event {
|
||||||
|
fn from(nostr_event: nostr::Event) -> Self {
|
||||||
|
Event {
|
||||||
|
id: nostr_event.id.to_hex(),
|
||||||
|
pubkey: nostr_event.pubkey.to_string(),
|
||||||
|
created_at: nostr_event.created_at.as_u64(),
|
||||||
|
kind: nostr_event.kind.as_u64(),
|
||||||
|
tags: nostr_event.tags.iter().map(|x| x.as_vec()).collect(),
|
||||||
|
content: nostr_event.content,
|
||||||
|
sig: nostr_event.sig.to_string(),
|
||||||
|
delegated_by: None,
|
||||||
|
tagidx: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
@@ -423,7 +464,7 @@ mod tests {
|
|||||||
fn empty_event_tag_match() {
|
fn empty_event_tag_match() {
|
||||||
let event = 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]
|
||||||
@@ -431,12 +472,11 @@ mod tests {
|
|||||||
let mut event = 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!(
|
||||||
event.generic_tag_val_intersect(
|
event.generic_tag_val_intersect(
|
||||||
'e',
|
'e',
|
||||||
&HashSet::from(["foo".to_owned(), "bar".to_owned()])
|
&HashSet::from(["foo".to_owned(), "bar".to_owned()])
|
||||||
),
|
)
|
||||||
true
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -571,28 +611,28 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn ephemeral_event() {
|
fn ephemeral_event() {
|
||||||
let mut event = Event::simple_event();
|
let mut event = Event::simple_event();
|
||||||
event.kind=20000;
|
event.kind = 20000;
|
||||||
assert!(event.is_ephemeral());
|
assert!(event.is_ephemeral());
|
||||||
event.kind=29999;
|
event.kind = 29999;
|
||||||
assert!(event.is_ephemeral());
|
assert!(event.is_ephemeral());
|
||||||
event.kind=30000;
|
event.kind = 30000;
|
||||||
assert!(!event.is_ephemeral());
|
assert!(!event.is_ephemeral());
|
||||||
event.kind=19999;
|
event.kind = 19999;
|
||||||
assert!(!event.is_ephemeral());
|
assert!(!event.is_ephemeral());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn replaceable_event() {
|
fn replaceable_event() {
|
||||||
let mut event = Event::simple_event();
|
let mut event = Event::simple_event();
|
||||||
event.kind=0;
|
event.kind = 0;
|
||||||
assert!(event.is_replaceable());
|
assert!(event.is_replaceable());
|
||||||
event.kind=3;
|
event.kind = 3;
|
||||||
assert!(event.is_replaceable());
|
assert!(event.is_replaceable());
|
||||||
event.kind=10000;
|
event.kind = 10000;
|
||||||
assert!(event.is_replaceable());
|
assert!(event.is_replaceable());
|
||||||
event.kind=19999;
|
event.kind = 19999;
|
||||||
assert!(event.is_replaceable());
|
assert!(event.is_replaceable());
|
||||||
event.kind=20000;
|
event.kind = 20000;
|
||||||
assert!(!event.is_replaceable());
|
assert!(!event.is_replaceable());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -614,8 +654,7 @@ mod tests {
|
|||||||
// NIP case #1: "tags":[["d",""]]
|
// NIP case #1: "tags":[["d",""]]
|
||||||
let mut event = Event::simple_event();
|
let mut event = Event::simple_event();
|
||||||
event.kind = 30000;
|
event.kind = 30000;
|
||||||
event.tags = vec![
|
event.tags = vec![vec!["d".to_owned(), "".to_owned()]];
|
||||||
vec!["d".to_owned(), "".to_owned()]];
|
|
||||||
assert_eq!(event.distinct_param(), Some("".to_string()));
|
assert_eq!(event.distinct_param(), Some("".to_string()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -632,8 +671,7 @@ mod tests {
|
|||||||
// NIP case #3: "tags":[["d"]]: implicit empty value ""
|
// NIP case #3: "tags":[["d"]]: implicit empty value ""
|
||||||
let mut event = Event::simple_event();
|
let mut event = Event::simple_event();
|
||||||
event.kind = 30000;
|
event.kind = 30000;
|
||||||
event.tags = vec![
|
event.tags = vec![vec!["d".to_owned()]];
|
||||||
vec!["d".to_owned()]];
|
|
||||||
assert_eq!(event.distinct_param(), Some("".to_string()));
|
assert_eq!(event.distinct_param(), Some("".to_string()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -644,7 +682,7 @@ mod tests {
|
|||||||
event.kind = 30000;
|
event.kind = 30000;
|
||||||
event.tags = vec![
|
event.tags = vec![
|
||||||
vec!["d".to_owned(), "".to_string()],
|
vec!["d".to_owned(), "".to_string()],
|
||||||
vec!["d".to_owned(), "not empty".to_string()]
|
vec!["d".to_owned(), "not empty".to_string()],
|
||||||
];
|
];
|
||||||
assert_eq!(event.distinct_param(), Some("".to_string()));
|
assert_eq!(event.distinct_param(), Some("".to_string()));
|
||||||
}
|
}
|
||||||
@@ -657,7 +695,7 @@ mod tests {
|
|||||||
event.kind = 30000;
|
event.kind = 30000;
|
||||||
event.tags = vec![
|
event.tags = vec![
|
||||||
vec!["d".to_owned(), "not empty".to_string()],
|
vec!["d".to_owned(), "not empty".to_string()],
|
||||||
vec!["d".to_owned(), "".to_string()]
|
vec!["d".to_owned(), "".to_string()],
|
||||||
];
|
];
|
||||||
assert_eq!(event.distinct_param(), Some("not empty".to_string()));
|
assert_eq!(event.distinct_param(), Some("not empty".to_string()));
|
||||||
}
|
}
|
||||||
@@ -670,7 +708,7 @@ mod tests {
|
|||||||
event.tags = vec![
|
event.tags = vec![
|
||||||
vec!["d".to_owned()],
|
vec!["d".to_owned()],
|
||||||
vec!["d".to_owned(), "second value".to_string()],
|
vec!["d".to_owned(), "second value".to_string()],
|
||||||
vec!["d".to_owned(), "third value".to_string()]
|
vec!["d".to_owned(), "third value".to_string()],
|
||||||
];
|
];
|
||||||
assert_eq!(event.distinct_param(), Some("".to_string()));
|
assert_eq!(event.distinct_param(), Some("".to_string()));
|
||||||
}
|
}
|
||||||
@@ -680,9 +718,7 @@ mod tests {
|
|||||||
// NIP case #6: "tags":[["e"]]: same as no tags
|
// NIP case #6: "tags":[["e"]]: same as no tags
|
||||||
let mut event = Event::simple_event();
|
let mut event = Event::simple_event();
|
||||||
event.kind = 30000;
|
event.kind = 30000;
|
||||||
event.tags = vec![
|
event.tags = vec![vec!["e".to_owned()]];
|
||||||
vec!["e".to_owned()],
|
|
||||||
];
|
|
||||||
assert_eq!(event.distinct_param(), Some("".to_string()));
|
assert_eq!(event.distinct_param(), Some("".to_string()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -691,9 +727,7 @@ mod tests {
|
|||||||
// regular events do not expire
|
// regular events do not expire
|
||||||
let mut event = Event::simple_event();
|
let mut event = Event::simple_event();
|
||||||
event.kind = 7;
|
event.kind = 7;
|
||||||
event.tags = vec![
|
event.tags = vec![vec!["test".to_string(), "foo".to_string()]];
|
||||||
vec!["test".to_string(), "foo".to_string()],
|
|
||||||
];
|
|
||||||
assert_eq!(event.expiration(), None);
|
assert_eq!(event.expiration(), None);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -702,57 +736,47 @@ mod tests {
|
|||||||
// regular events do not expire
|
// regular events do not expire
|
||||||
let mut event = Event::simple_event();
|
let mut event = Event::simple_event();
|
||||||
event.kind = 7;
|
event.kind = 7;
|
||||||
event.tags = vec![
|
event.tags = vec![vec!["expiration".to_string()]];
|
||||||
vec!["expiration".to_string()],
|
|
||||||
];
|
|
||||||
assert_eq!(event.expiration(), None);
|
assert_eq!(event.expiration(), None);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn expiring_event_future() {
|
fn expiring_event_future() {
|
||||||
// a normal expiring event
|
// a normal expiring event
|
||||||
let exp:u64 = 1676264138;
|
let exp: u64 = 1676264138;
|
||||||
let mut event = Event::simple_event();
|
let mut event = Event::simple_event();
|
||||||
event.kind = 1;
|
event.kind = 1;
|
||||||
event.tags = vec![
|
event.tags = vec![vec!["expiration".to_string(), exp.to_string()]];
|
||||||
vec!["expiration".to_string(), exp.to_string()],
|
|
||||||
];
|
|
||||||
assert_eq!(event.expiration(), Some(exp));
|
assert_eq!(event.expiration(), Some(exp));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn expiring_event_negative() {
|
fn expiring_event_negative() {
|
||||||
// expiration set to a negative value (invalid)
|
// expiration set to a negative value (invalid)
|
||||||
let exp:i64 = -90;
|
let exp: i64 = -90;
|
||||||
let mut event = Event::simple_event();
|
let mut event = Event::simple_event();
|
||||||
event.kind = 1;
|
event.kind = 1;
|
||||||
event.tags = vec![
|
event.tags = vec![vec!["expiration".to_string(), exp.to_string()]];
|
||||||
vec!["expiration".to_string(), exp.to_string()],
|
|
||||||
];
|
|
||||||
assert_eq!(event.expiration(), None);
|
assert_eq!(event.expiration(), None);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn expiring_event_zero() {
|
fn expiring_event_zero() {
|
||||||
// a normal expiring event set to zero
|
// a normal expiring event set to zero
|
||||||
let exp:i64 = 0;
|
let exp: i64 = 0;
|
||||||
let mut event = Event::simple_event();
|
let mut event = Event::simple_event();
|
||||||
event.kind = 1;
|
event.kind = 1;
|
||||||
event.tags = vec![
|
event.tags = vec![vec!["expiration".to_string(), exp.to_string()]];
|
||||||
vec!["expiration".to_string(), exp.to_string()],
|
|
||||||
];
|
|
||||||
assert_eq!(event.expiration(), Some(0));
|
assert_eq!(event.expiration(), Some(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn expiring_event_fraction() {
|
fn expiring_event_fraction() {
|
||||||
// expiration is fractional (invalid)
|
// expiration is fractional (invalid)
|
||||||
let exp:f64 = 23.334;
|
let exp: f64 = 23.334;
|
||||||
let mut event = Event::simple_event();
|
let mut event = Event::simple_event();
|
||||||
event.kind = 1;
|
event.kind = 1;
|
||||||
event.tags = vec![
|
event.tags = vec![vec!["expiration".to_string(), exp.to_string()]];
|
||||||
vec!["expiration".to_string(), exp.to_string()],
|
|
||||||
];
|
|
||||||
assert_eq!(event.expiration(), None);
|
assert_eq!(event.expiration(), None);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+5
-4
@@ -1,5 +1,5 @@
|
|||||||
//! Utilities for searching hexadecimal
|
//! Utilities for searching hexadecimal
|
||||||
use crate::utils::{is_hex};
|
use crate::utils::is_hex;
|
||||||
use hex;
|
use hex;
|
||||||
|
|
||||||
/// Types of hexadecimal queries.
|
/// Types of hexadecimal queries.
|
||||||
@@ -19,7 +19,8 @@ fn is_all_fs(s: &str) -> bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Find the next hex sequence greater than the argument.
|
/// Find the next hex sequence greater than the argument.
|
||||||
#[must_use] pub fn hex_range(s: &str) -> Option<HexSearch> {
|
#[must_use]
|
||||||
|
pub fn hex_range(s: &str) -> Option<HexSearch> {
|
||||||
let mut hash_base = s.to_owned();
|
let mut hash_base = s.to_owned();
|
||||||
if !is_hex(&hash_base) || hash_base.len() > 64 {
|
if !is_hex(&hash_base) || hash_base.len() > 64 {
|
||||||
return None;
|
return None;
|
||||||
@@ -56,9 +57,9 @@ fn is_all_fs(s: &str) -> bool {
|
|||||||
} else if odd {
|
} else if odd {
|
||||||
// check if first char in this byte is NOT 'f'
|
// check if first char in this byte is NOT 'f'
|
||||||
if b < 240 {
|
if b < 240 {
|
||||||
// bump up the first character in this byte
|
// bump up the first character in this byte
|
||||||
upper[byte_len] = b + 16;
|
upper[byte_len] = b + 16;
|
||||||
// increment done, stop iterating through the vec
|
// increment done, stop iterating through the vec
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
// if it is 'f', reset the byte to 0 and do a carry
|
// if it is 'f', reset the byte to 0 and do a carry
|
||||||
|
|||||||
+81
-1
@@ -4,6 +4,32 @@ use crate::config::Settings;
|
|||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
pub const CARGO_PKG_VERSION: Option<&'static str> = option_env!("CARGO_PKG_VERSION");
|
pub const CARGO_PKG_VERSION: Option<&'static str> = option_env!("CARGO_PKG_VERSION");
|
||||||
|
pub const UNIT: &str = "sats";
|
||||||
|
|
||||||
|
/// Limitations of the relay as specified in NIP-111
|
||||||
|
/// (This nip isn't finalized so may change)
|
||||||
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
|
#[allow(unused)]
|
||||||
|
pub struct Limitation {
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
payment_required: Option<bool>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
#[allow(unused)]
|
||||||
|
pub struct Fees {
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
admission: Option<Vec<Fee>>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
publication: Option<Vec<Fee>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
#[allow(unused)]
|
||||||
|
pub struct Fee {
|
||||||
|
amount: u64,
|
||||||
|
unit: String,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
#[allow(unused)]
|
#[allow(unused)]
|
||||||
@@ -19,17 +45,25 @@ pub struct RelayInfo {
|
|||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub contact: Option<String>,
|
pub contact: Option<String>,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub icon: Option<String>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub supported_nips: Option<Vec<i64>>,
|
pub supported_nips: Option<Vec<i64>>,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub software: Option<String>,
|
pub software: Option<String>,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub version: Option<String>,
|
pub version: Option<String>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub limitation: Option<Limitation>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub payment_url: Option<String>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub fees: Option<Fees>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Convert an Info configuration into public Relay Info
|
/// Convert an Info configuration into public Relay Info
|
||||||
impl From<Settings> for RelayInfo {
|
impl From<Settings> for RelayInfo {
|
||||||
fn from(c: Settings) -> Self {
|
fn from(c: Settings) -> Self {
|
||||||
let mut supported_nips = vec![1, 2, 9, 11, 12, 15, 16, 20, 22, 33, 40, 42];
|
let mut supported_nips = vec![1, 2, 9, 11, 12, 15, 16, 20, 22, 33, 40];
|
||||||
|
|
||||||
if c.authorization.nip42_auth {
|
if c.authorization.nip42_auth {
|
||||||
supported_nips.push(42);
|
supported_nips.push(42);
|
||||||
@@ -37,6 +71,48 @@ impl From<Settings> for RelayInfo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let i = c.info;
|
let i = c.info;
|
||||||
|
let p = c.pay_to_relay;
|
||||||
|
|
||||||
|
let limitations = Limitation {
|
||||||
|
payment_required: Some(p.enabled),
|
||||||
|
};
|
||||||
|
|
||||||
|
let (payment_url, fees) = if p.enabled {
|
||||||
|
let admission_fee = if p.admission_cost > 0 {
|
||||||
|
Some(vec![Fee {
|
||||||
|
amount: p.admission_cost,
|
||||||
|
unit: UNIT.to_string(),
|
||||||
|
}])
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
|
let post_fee = if p.cost_per_event > 0 {
|
||||||
|
Some(vec![Fee {
|
||||||
|
amount: p.cost_per_event,
|
||||||
|
unit: UNIT.to_string(),
|
||||||
|
}])
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
|
let fees = Fees {
|
||||||
|
admission: admission_fee,
|
||||||
|
publication: post_fee,
|
||||||
|
};
|
||||||
|
|
||||||
|
let payment_url = if p.enabled && i.relay_url.is_some() {
|
||||||
|
Some(format!(
|
||||||
|
"{}join",
|
||||||
|
i.relay_url.clone().unwrap().replace("ws", "http")
|
||||||
|
))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
(payment_url, Some(fees))
|
||||||
|
} else {
|
||||||
|
(None, None)
|
||||||
|
};
|
||||||
|
|
||||||
RelayInfo {
|
RelayInfo {
|
||||||
id: i.relay_url,
|
id: i.relay_url,
|
||||||
@@ -47,6 +123,10 @@ impl From<Settings> for RelayInfo {
|
|||||||
supported_nips: Some(supported_nips),
|
supported_nips: Some(supported_nips),
|
||||||
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(std::borrow::ToOwned::to_owned),
|
version: CARGO_PKG_VERSION.map(std::borrow::ToOwned::to_owned),
|
||||||
|
limitation: Some(limitations),
|
||||||
|
payment_url,
|
||||||
|
fees,
|
||||||
|
icon: i.relay_icon,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-2
@@ -8,11 +8,12 @@ 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 nauthz;
|
pub mod nauthz;
|
||||||
|
pub mod nip05;
|
||||||
pub mod notice;
|
pub mod notice;
|
||||||
pub mod repo;
|
pub mod repo;
|
||||||
pub mod subscription;
|
pub mod subscription;
|
||||||
pub mod utils;
|
pub mod utils;
|
||||||
// Public API for creating relays programatically
|
// Public API for creating relays programmatically
|
||||||
|
pub mod payment;
|
||||||
pub mod server;
|
pub mod server;
|
||||||
|
|||||||
+60
-3
@@ -1,13 +1,24 @@
|
|||||||
//! Server process
|
//! Server process
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
|
use console_subscriber::ConsoleLayer;
|
||||||
use nostr_rs_relay::cli::CLIArgs;
|
use nostr_rs_relay::cli::CLIArgs;
|
||||||
use nostr_rs_relay::config;
|
use nostr_rs_relay::config;
|
||||||
use nostr_rs_relay::server::start_server;
|
use nostr_rs_relay::server::start_server;
|
||||||
|
use std::fs;
|
||||||
|
use std::path::Path;
|
||||||
|
use std::process;
|
||||||
use std::sync::mpsc as syncmpsc;
|
use std::sync::mpsc as syncmpsc;
|
||||||
use std::sync::mpsc::{Receiver as MpscReceiver, Sender as MpscSender};
|
use std::sync::mpsc::{Receiver as MpscReceiver, Sender as MpscSender};
|
||||||
use std::thread;
|
use std::thread;
|
||||||
|
#[cfg(not(target_env = "msvc"))]
|
||||||
|
use tikv_jemallocator::Jemalloc;
|
||||||
use tracing::info;
|
use tracing::info;
|
||||||
use console_subscriber::ConsoleLayer;
|
use tracing_appender::non_blocking::WorkerGuard;
|
||||||
|
use tracing_subscriber::EnvFilter;
|
||||||
|
|
||||||
|
#[cfg(not(target_env = "msvc"))]
|
||||||
|
#[global_allocator]
|
||||||
|
static GLOBAL: Jemalloc = Jemalloc;
|
||||||
|
|
||||||
/// Start running a Nostr relay server.
|
/// Start running a Nostr relay server.
|
||||||
fn main() {
|
fn main() {
|
||||||
@@ -16,9 +27,35 @@ fn main() {
|
|||||||
// get config file name from args
|
// get config file name from args
|
||||||
let config_file_arg = args.config;
|
let config_file_arg = args.config;
|
||||||
|
|
||||||
|
// Ensure the config file is readable if it was explicitly set
|
||||||
|
if let Some(config_path) = config_file_arg.as_ref() {
|
||||||
|
let path = Path::new(&config_path);
|
||||||
|
if !path.exists() {
|
||||||
|
eprintln!("Config file not found: {}", &config_path);
|
||||||
|
process::exit(1);
|
||||||
|
}
|
||||||
|
if !path.is_file() {
|
||||||
|
eprintln!("Invalid config file path: {}", &config_path);
|
||||||
|
process::exit(1);
|
||||||
|
}
|
||||||
|
if let Err(err) = fs::metadata(path) {
|
||||||
|
eprintln!("Error while accessing file metadata: {}", err);
|
||||||
|
process::exit(1);
|
||||||
|
}
|
||||||
|
if let Err(err) = fs::File::open(path) {
|
||||||
|
eprintln!("Config file is not readable: {}", err);
|
||||||
|
process::exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut _log_guard: Option<WorkerGuard> = None;
|
||||||
|
|
||||||
// configure settings from the config file (defaults to config.toml)
|
// configure settings from the config file (defaults to config.toml)
|
||||||
// replace default settings with those read from the config file
|
// replace default settings with those read from the config file
|
||||||
let mut settings = config::Settings::new(&config_file_arg);
|
let mut settings = config::Settings::new(&config_file_arg).unwrap_or_else(|e| {
|
||||||
|
eprintln!("Error reading config file ({:?})", e);
|
||||||
|
process::exit(1);
|
||||||
|
});
|
||||||
|
|
||||||
// setup tracing
|
// setup tracing
|
||||||
if settings.diagnostics.tracing {
|
if settings.diagnostics.tracing {
|
||||||
@@ -26,7 +63,27 @@ fn main() {
|
|||||||
ConsoleLayer::builder().with_default_env().init();
|
ConsoleLayer::builder().with_default_env().init();
|
||||||
} else {
|
} else {
|
||||||
// standard logging
|
// standard logging
|
||||||
tracing_subscriber::fmt::try_init().unwrap();
|
if let Some(path) = &settings.logging.folder_path {
|
||||||
|
// write logs to a folder
|
||||||
|
let prefix = match &settings.logging.file_prefix {
|
||||||
|
Some(p) => p.as_str(),
|
||||||
|
None => "relay",
|
||||||
|
};
|
||||||
|
let file_appender = tracing_appender::rolling::daily(path, prefix);
|
||||||
|
let (non_blocking, guard) = tracing_appender::non_blocking(file_appender);
|
||||||
|
let filter = EnvFilter::from_default_env();
|
||||||
|
// assign to a variable that is not dropped till the program ends
|
||||||
|
_log_guard = Some(guard);
|
||||||
|
|
||||||
|
tracing_subscriber::fmt()
|
||||||
|
.with_env_filter(filter)
|
||||||
|
.with_writer(non_blocking)
|
||||||
|
.try_init()
|
||||||
|
.unwrap();
|
||||||
|
} else {
|
||||||
|
// write to stdout
|
||||||
|
tracing_subscriber::fmt::try_init().unwrap();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
info!("Starting up from main");
|
info!("Starting up from main");
|
||||||
|
|
||||||
|
|||||||
+8
-8
@@ -35,13 +35,13 @@ impl std::convert::From<Nip05Name> for nauthz_grpc::event_request::Nip05Name {
|
|||||||
fn from(value: Nip05Name) -> Self {
|
fn from(value: Nip05Name) -> Self {
|
||||||
nauthz_grpc::event_request::Nip05Name {
|
nauthz_grpc::event_request::Nip05Name {
|
||||||
local: value.local.clone(),
|
local: value.local.clone(),
|
||||||
domain: value.domain.clone(),
|
domain: value.domain,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// conversion of event tags into gprc struct
|
// conversion of event tags into gprc struct
|
||||||
fn tags_to_protobuf(tags: &Vec<Vec<String>>) -> Vec<TagEntry> {
|
fn tags_to_protobuf(tags: &[Vec<String>]) -> Vec<TagEntry> {
|
||||||
tags.iter()
|
tags.iter()
|
||||||
.map(|x| TagEntry { values: x.clone() })
|
.map(|x| TagEntry { values: x.clone() })
|
||||||
.collect()
|
.collect()
|
||||||
@@ -57,7 +57,7 @@ impl EventAuthzService {
|
|||||||
eas
|
eas
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn ready_connection(self: &mut Self) {
|
pub async fn ready_connection(&mut self) {
|
||||||
if self.conn.is_none() {
|
if self.conn.is_none() {
|
||||||
let client = AuthorizationClient::connect(self.server_addr.to_string()).await;
|
let client = AuthorizationClient::connect(self.server_addr.to_string()).await;
|
||||||
if let Err(ref msg) = client {
|
if let Err(ref msg) = client {
|
||||||
@@ -70,13 +70,13 @@ impl EventAuthzService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn admit_event(
|
pub async fn admit_event(
|
||||||
self: &mut Self,
|
&mut self,
|
||||||
event: &Event,
|
event: &Event,
|
||||||
ip: &str,
|
ip: &str,
|
||||||
origin: Option<String>,
|
origin: Option<String>,
|
||||||
user_agent: Option<String>,
|
user_agent: Option<String>,
|
||||||
nip05: Option<Nip05Name>,
|
nip05: Option<Nip05Name>,
|
||||||
auth_pubkey: Option<Vec<u8>>
|
auth_pubkey: Option<Vec<u8>>,
|
||||||
) -> Result<Box<dyn AuthzDecision>> {
|
) -> Result<Box<dyn AuthzDecision>> {
|
||||||
self.ready_connection().await;
|
self.ready_connection().await;
|
||||||
let id_blob = hex::decode(&event.id)?;
|
let id_blob = hex::decode(&event.id)?;
|
||||||
@@ -99,13 +99,13 @@ impl EventAuthzService {
|
|||||||
origin,
|
origin,
|
||||||
user_agent,
|
user_agent,
|
||||||
auth_pubkey,
|
auth_pubkey,
|
||||||
nip05: nip05.map(|x| nauthz_grpc::event_request::Nip05Name::from(x)),
|
nip05: nip05.map(nauthz_grpc::event_request::Nip05Name::from),
|
||||||
})
|
})
|
||||||
.await?;
|
.await?;
|
||||||
let reply = svr_res.into_inner();
|
let reply = svr_res.into_inner();
|
||||||
return Ok(Box::new(reply));
|
Ok(Box::new(reply))
|
||||||
} else {
|
} else {
|
||||||
return Err(Error::AuthzError);
|
Err(Error::AuthzError)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+50
-38
@@ -8,11 +8,11 @@ use crate::config::VerifiedUsers;
|
|||||||
use crate::error::{Error, Result};
|
use crate::error::{Error, Result};
|
||||||
use crate::event::Event;
|
use crate::event::Event;
|
||||||
use crate::repo::NostrRepo;
|
use crate::repo::NostrRepo;
|
||||||
use std::sync::Arc;
|
|
||||||
use hyper::body::HttpBody;
|
use hyper::body::HttpBody;
|
||||||
use hyper::client::connect::HttpConnector;
|
use hyper::client::connect::HttpConnector;
|
||||||
use hyper::Client;
|
use hyper::Client;
|
||||||
use hyper_tls::HttpsConnector;
|
use hyper_rustls::HttpsConnector;
|
||||||
|
use std::sync::Arc;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
use std::time::SystemTime;
|
use std::time::SystemTime;
|
||||||
@@ -48,7 +48,8 @@ pub struct Nip05Name {
|
|||||||
|
|
||||||
impl Nip05Name {
|
impl Nip05Name {
|
||||||
/// Does this name represent the entire domain?
|
/// Does this name represent the entire domain?
|
||||||
#[must_use] pub fn is_domain_only(&self) -> bool {
|
#[must_use]
|
||||||
|
pub fn is_domain_only(&self) -> bool {
|
||||||
self.local == "_"
|
self.local == "_"
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -58,8 +59,8 @@ impl Nip05Name {
|
|||||||
"https://{}/.well-known/nostr.json?name={}",
|
"https://{}/.well-known/nostr.json?name={}",
|
||||||
self.domain, self.local
|
self.domain, self.local
|
||||||
)
|
)
|
||||||
.parse::<http::Uri>()
|
.parse::<http::Uri>()
|
||||||
.ok()
|
.ok()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -73,7 +74,10 @@ impl std::convert::TryFrom<&str> for Nip05Name {
|
|||||||
// check if local name is valid
|
// check if local name is valid
|
||||||
let local = components[0];
|
let local = components[0];
|
||||||
let domain = components[1];
|
let domain = components[1];
|
||||||
if local.chars().all(|x| x.is_alphanumeric() || x == '_' || x == '-' || x == '.') {
|
if local
|
||||||
|
.chars()
|
||||||
|
.all(|x| x.is_alphanumeric() || x == '_' || x == '-' || x == '.')
|
||||||
|
{
|
||||||
if domain
|
if domain
|
||||||
.chars()
|
.chars()
|
||||||
.all(|x| x.is_alphanumeric() || x == '-' || x == '.')
|
.all(|x| x.is_alphanumeric() || x == '-' || x == '.')
|
||||||
@@ -129,7 +133,12 @@ impl Verifier {
|
|||||||
) -> Result<Self> {
|
) -> Result<Self> {
|
||||||
info!("creating NIP-05 verifier");
|
info!("creating NIP-05 verifier");
|
||||||
// setup hyper client
|
// setup hyper client
|
||||||
let https = HttpsConnector::new();
|
let https = hyper_rustls::HttpsConnectorBuilder::new()
|
||||||
|
.with_native_roots()
|
||||||
|
.https_or_http()
|
||||||
|
.enable_http1()
|
||||||
|
.build();
|
||||||
|
|
||||||
let client = Client::builder().build::<_, hyper::Body>(https);
|
let client = Client::builder().build::<_, hyper::Body>(https);
|
||||||
|
|
||||||
// After all accounts have been re-verified, don't check again
|
// After all accounts have been re-verified, don't check again
|
||||||
@@ -261,10 +270,10 @@ impl Verifier {
|
|||||||
Err(Error::ChannelClosed) => {
|
Err(Error::ChannelClosed) => {
|
||||||
// channel was closed, we are shutting down
|
// channel was closed, we are shutting down
|
||||||
return;
|
return;
|
||||||
},
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
info!("error in verifier: {:?}", e);
|
info!("error in verifier: {:?}", e);
|
||||||
},
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -349,42 +358,41 @@ impl Verifier {
|
|||||||
UserWebVerificationStatus::Verified => {
|
UserWebVerificationStatus::Verified => {
|
||||||
// freshly verified account, update the
|
// freshly verified account, update the
|
||||||
// timestamp.
|
// timestamp.
|
||||||
self.repo.update_verification_timestamp(v.rowid)
|
self.repo.update_verification_timestamp(v.rowid).await?;
|
||||||
.await?;
|
|
||||||
info!("verification updated for {}", v.to_string());
|
info!("verification updated for {}", v.to_string());
|
||||||
|
|
||||||
}
|
}
|
||||||
UserWebVerificationStatus::DomainNotAllowed
|
UserWebVerificationStatus::DomainNotAllowed
|
||||||
| UserWebVerificationStatus::Unknown => {
|
| UserWebVerificationStatus::Unknown => {
|
||||||
// server may be offline, or temporarily
|
// server may be offline, or temporarily
|
||||||
// blocked by the config file. Note the
|
// blocked by the config file. Note the
|
||||||
// failure so we can process something
|
// failure so we can process something
|
||||||
// else.
|
// else.
|
||||||
|
|
||||||
// have we had enough failures to give up?
|
// have we had enough failures to give up?
|
||||||
if v.failure_count >= max_failures as u64 {
|
if v.failure_count >= max_failures as u64 {
|
||||||
info!(
|
info!(
|
||||||
"giving up on verifying {:?} after {} failures",
|
"giving up on verifying {:?} after {} failures",
|
||||||
v.name, v.failure_count
|
v.name, v.failure_count
|
||||||
);
|
);
|
||||||
self.repo.delete_verification(v.rowid)
|
self.repo.delete_verification(v.rowid).await?;
|
||||||
.await?;
|
} else {
|
||||||
} else {
|
// record normal failure, incrementing failure count
|
||||||
// record normal failure, incrementing failure count
|
info!("verification failed for {}", v.to_string());
|
||||||
info!("verification failed for {}", v.to_string());
|
self.repo.fail_verification(v.rowid).await?;
|
||||||
self.repo.fail_verification(v.rowid).await?;
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
UserWebVerificationStatus::Unverified => {
|
UserWebVerificationStatus::Unverified => {
|
||||||
// domain has removed the verification, drop
|
// domain has removed the verification, drop
|
||||||
// the record on our side.
|
// the record on our side.
|
||||||
info!("verification rescinded for {}", v.to_string());
|
info!("verification rescinded for {}", v.to_string());
|
||||||
self.repo.delete_verification(v.rowid)
|
self.repo.delete_verification(v.rowid).await?;
|
||||||
.await?;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(Error::SqlError(rusqlite::Error::QueryReturnedNoRows)) => {
|
Err(
|
||||||
|
Error::SqlError(rusqlite::Error::QueryReturnedNoRows)
|
||||||
|
| Error::SqlxError(sqlx::Error::RowNotFound),
|
||||||
|
) => {
|
||||||
// No users need verification. Reset the interval to
|
// No users need verification. Reset the interval to
|
||||||
// the next verification attempt.
|
// the next verification attempt.
|
||||||
let start = tokio::time::Instant::now() + self.wait_after_finish;
|
let start = tokio::time::Instant::now() + self.wait_after_finish;
|
||||||
@@ -433,7 +441,9 @@ impl Verifier {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// write the verification record
|
// write the verification record
|
||||||
self.repo.create_verification_record(&event.id, name).await?;
|
self.repo
|
||||||
|
.create_verification_record(&event.id, name)
|
||||||
|
.await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -463,7 +473,8 @@ pub struct VerificationRecord {
|
|||||||
|
|
||||||
/// Check with settings to determine if a given domain is allowed to
|
/// Check with settings to determine if a given domain is allowed to
|
||||||
/// publish.
|
/// publish.
|
||||||
#[must_use] pub fn is_domain_allowed(
|
#[must_use]
|
||||||
|
pub fn is_domain_allowed(
|
||||||
domain: &str,
|
domain: &str,
|
||||||
whitelist: &Option<Vec<String>>,
|
whitelist: &Option<Vec<String>>,
|
||||||
blacklist: &Option<Vec<String>>,
|
blacklist: &Option<Vec<String>>,
|
||||||
@@ -483,7 +494,8 @@ pub struct VerificationRecord {
|
|||||||
impl VerificationRecord {
|
impl VerificationRecord {
|
||||||
/// Check if the record is recent enough to be considered valid,
|
/// Check if the record is recent enough to be considered valid,
|
||||||
/// and the domain is allowed.
|
/// and the domain is allowed.
|
||||||
#[must_use] pub fn is_valid(&self, verified_users_settings: &VerifiedUsers) -> bool {
|
#[must_use]
|
||||||
|
pub fn is_valid(&self, verified_users_settings: &VerifiedUsers) -> bool {
|
||||||
//let settings = SETTINGS.read().unwrap();
|
//let settings = SETTINGS.read().unwrap();
|
||||||
// how long a verification record is good for
|
// how long a verification record is good for
|
||||||
let nip05_expiration = &verified_users_settings.verify_expiration_duration;
|
let nip05_expiration = &verified_users_settings.verify_expiration_duration;
|
||||||
|
|||||||
+27
-11
@@ -5,6 +5,7 @@ pub enum EventResultStatus {
|
|||||||
Blocked,
|
Blocked,
|
||||||
RateLimited,
|
RateLimited,
|
||||||
Error,
|
Error,
|
||||||
|
Restricted,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct EventResult {
|
pub struct EventResult {
|
||||||
@@ -16,18 +17,20 @@ pub struct EventResult {
|
|||||||
pub enum Notice {
|
pub enum Notice {
|
||||||
Message(String),
|
Message(String),
|
||||||
EventResult(EventResult),
|
EventResult(EventResult),
|
||||||
AuthChallenge(String)
|
AuthChallenge(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl EventResultStatus {
|
impl EventResultStatus {
|
||||||
#[must_use] pub fn to_bool(&self) -> bool {
|
#[must_use]
|
||||||
|
pub fn to_bool(&self) -> bool {
|
||||||
match self {
|
match self {
|
||||||
Self::Duplicate | Self::Saved => true,
|
Self::Duplicate | Self::Saved => true,
|
||||||
Self::Invalid |Self::Blocked | Self::RateLimited | Self::Error => false,
|
Self::Invalid | Self::Blocked | Self::RateLimited | Self::Error | Self::Restricted => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[must_use] pub fn prefix(&self) -> &'static str {
|
#[must_use]
|
||||||
|
pub fn prefix(&self) -> &'static str {
|
||||||
match self {
|
match self {
|
||||||
Self::Saved => "saved",
|
Self::Saved => "saved",
|
||||||
Self::Duplicate => "duplicate",
|
Self::Duplicate => "duplicate",
|
||||||
@@ -35,6 +38,7 @@ impl EventResultStatus {
|
|||||||
Self::Blocked => "blocked",
|
Self::Blocked => "blocked",
|
||||||
Self::RateLimited => "rate-limited",
|
Self::RateLimited => "rate-limited",
|
||||||
Self::Error => "error",
|
Self::Error => "error",
|
||||||
|
Self::Restricted => "restricted",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -44,7 +48,8 @@ impl Notice {
|
|||||||
// Notice::err_msg(format!("{}", err), id)
|
// Notice::err_msg(format!("{}", err), id)
|
||||||
//}
|
//}
|
||||||
|
|
||||||
#[must_use] pub fn message(msg: String) -> Notice {
|
#[must_use]
|
||||||
|
pub fn message(msg: String) -> Notice {
|
||||||
Notice::Message(msg)
|
Notice::Message(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,27 +58,38 @@ impl Notice {
|
|||||||
Notice::EventResult(EventResult { id, msg, status })
|
Notice::EventResult(EventResult { id, msg, status })
|
||||||
}
|
}
|
||||||
|
|
||||||
#[must_use] pub fn invalid(id: String, msg: &str) -> Notice {
|
#[must_use]
|
||||||
|
pub fn invalid(id: String, msg: &str) -> Notice {
|
||||||
Notice::prefixed(id, msg, EventResultStatus::Invalid)
|
Notice::prefixed(id, msg, EventResultStatus::Invalid)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[must_use] pub fn blocked(id: String, msg: &str) -> Notice {
|
#[must_use]
|
||||||
|
pub fn blocked(id: String, msg: &str) -> Notice {
|
||||||
Notice::prefixed(id, msg, EventResultStatus::Blocked)
|
Notice::prefixed(id, msg, EventResultStatus::Blocked)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[must_use] pub fn rate_limited(id: String, msg: &str) -> Notice {
|
#[must_use]
|
||||||
|
pub fn rate_limited(id: String, msg: &str) -> Notice {
|
||||||
Notice::prefixed(id, msg, EventResultStatus::RateLimited)
|
Notice::prefixed(id, msg, EventResultStatus::RateLimited)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[must_use] pub fn duplicate(id: String) -> Notice {
|
#[must_use]
|
||||||
|
pub fn duplicate(id: String) -> Notice {
|
||||||
Notice::prefixed(id, "", EventResultStatus::Duplicate)
|
Notice::prefixed(id, "", EventResultStatus::Duplicate)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[must_use] pub fn error(id: String, msg: &str) -> Notice {
|
#[must_use]
|
||||||
|
pub fn error(id: String, msg: &str) -> Notice {
|
||||||
Notice::prefixed(id, msg, EventResultStatus::Error)
|
Notice::prefixed(id, msg, EventResultStatus::Error)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[must_use] pub fn saved(id: String) -> Notice {
|
#[must_use]
|
||||||
|
pub fn restricted(id: String, msg: &str) -> Notice {
|
||||||
|
Notice::prefixed(id, msg, EventResultStatus::Restricted)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[must_use]
|
||||||
|
pub fn saved(id: String) -> Notice {
|
||||||
Notice::EventResult(EventResult {
|
Notice::EventResult(EventResult {
|
||||||
id,
|
id,
|
||||||
msg: "".into(),
|
msg: "".into(),
|
||||||
|
|||||||
@@ -0,0 +1,176 @@
|
|||||||
|
//! LNBits payment processor
|
||||||
|
use http::Uri;
|
||||||
|
use hyper::client::connect::HttpConnector;
|
||||||
|
use hyper::Client;
|
||||||
|
use hyper_rustls::HttpsConnector;
|
||||||
|
use nostr::Keys;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use serde_json::Value;
|
||||||
|
|
||||||
|
use async_trait::async_trait;
|
||||||
|
use rand::Rng;
|
||||||
|
|
||||||
|
use std::str::FromStr;
|
||||||
|
use url::Url;
|
||||||
|
|
||||||
|
use crate::{config::Settings, error::Error};
|
||||||
|
|
||||||
|
use super::{InvoiceInfo, InvoiceStatus, PaymentProcessor};
|
||||||
|
|
||||||
|
const APIPATH: &str = "/api/v1/payments/";
|
||||||
|
|
||||||
|
/// Info LNBits expects in create invoice request
|
||||||
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
pub struct LNBitsCreateInvoice {
|
||||||
|
out: bool,
|
||||||
|
amount: u64,
|
||||||
|
memo: String,
|
||||||
|
webhook: String,
|
||||||
|
unit: String,
|
||||||
|
internal: bool,
|
||||||
|
expiry: u64,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Invoice response for LN bits
|
||||||
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
|
pub struct LNBitsCreateInvoiceResponse {
|
||||||
|
payment_hash: String,
|
||||||
|
payment_request: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// LNBits call back response
|
||||||
|
/// Used when an invoice is paid
|
||||||
|
/// lnbits to post the status change to relay
|
||||||
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||||
|
pub struct LNBitsCallback {
|
||||||
|
pub checking_id: String,
|
||||||
|
pub pending: bool,
|
||||||
|
pub amount: u64,
|
||||||
|
pub memo: String,
|
||||||
|
pub time: u64,
|
||||||
|
pub bolt11: String,
|
||||||
|
pub preimage: String,
|
||||||
|
pub payment_hash: String,
|
||||||
|
pub wallet_id: String,
|
||||||
|
pub webhook: String,
|
||||||
|
pub webhook_status: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// LN Bits repose for check invoice endpoint
|
||||||
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||||
|
pub struct LNBitsCheckInvoiceResponse {
|
||||||
|
paid: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct LNBitsPaymentProcessor {
|
||||||
|
/// HTTP client
|
||||||
|
client: hyper::Client<HttpsConnector<HttpConnector>, hyper::Body>,
|
||||||
|
settings: Settings,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl LNBitsPaymentProcessor {
|
||||||
|
pub fn new(settings: &Settings) -> Self {
|
||||||
|
// setup hyper client
|
||||||
|
let https = hyper_rustls::HttpsConnectorBuilder::new()
|
||||||
|
.with_native_roots()
|
||||||
|
.https_only()
|
||||||
|
.enable_http1()
|
||||||
|
.build();
|
||||||
|
let client = Client::builder().build::<_, hyper::Body>(https);
|
||||||
|
|
||||||
|
Self {
|
||||||
|
client,
|
||||||
|
settings: settings.clone(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl PaymentProcessor for LNBitsPaymentProcessor {
|
||||||
|
/// Calls LNBits api to ger new invoice
|
||||||
|
async fn get_invoice(&self, key: &Keys, amount: u64) -> Result<InvoiceInfo, Error> {
|
||||||
|
let random_number: u16 = rand::thread_rng().gen();
|
||||||
|
let memo = format!("{}: {}", random_number, key.public_key());
|
||||||
|
|
||||||
|
let callback_url = Url::parse(
|
||||||
|
&self
|
||||||
|
.settings
|
||||||
|
.info
|
||||||
|
.relay_url
|
||||||
|
.clone()
|
||||||
|
.unwrap()
|
||||||
|
.replace("ws", "http"),
|
||||||
|
)?
|
||||||
|
.join("lnbits")?;
|
||||||
|
|
||||||
|
let body = LNBitsCreateInvoice {
|
||||||
|
out: false,
|
||||||
|
amount,
|
||||||
|
memo: memo.clone(),
|
||||||
|
webhook: callback_url.to_string(),
|
||||||
|
unit: "sat".to_string(),
|
||||||
|
internal: false,
|
||||||
|
expiry: 3600,
|
||||||
|
};
|
||||||
|
let url = Url::parse(&self.settings.pay_to_relay.node_url)?.join(APIPATH)?;
|
||||||
|
let uri = Uri::from_str(url.as_str().strip_suffix('/').unwrap_or(url.as_str())).unwrap();
|
||||||
|
|
||||||
|
let req = hyper::Request::builder()
|
||||||
|
.method(hyper::Method::POST)
|
||||||
|
.uri(uri)
|
||||||
|
.header("X-Api-Key", &self.settings.pay_to_relay.api_secret)
|
||||||
|
.body(hyper::Body::from(serde_json::to_string(&body)?))
|
||||||
|
.expect("request builder");
|
||||||
|
|
||||||
|
let res = self.client.request(req).await?;
|
||||||
|
|
||||||
|
// Json to Struct of LNbits callback
|
||||||
|
let body = hyper::body::to_bytes(res.into_body()).await?;
|
||||||
|
let invoice_response: LNBitsCreateInvoiceResponse = serde_json::from_slice(&body)?;
|
||||||
|
|
||||||
|
Ok(InvoiceInfo {
|
||||||
|
pubkey: key.public_key().to_string(),
|
||||||
|
payment_hash: invoice_response.payment_hash,
|
||||||
|
bolt11: invoice_response.payment_request,
|
||||||
|
amount,
|
||||||
|
memo,
|
||||||
|
status: InvoiceStatus::Unpaid,
|
||||||
|
confirmed_at: None,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Calls LNBits Api to check the payment status of invoice
|
||||||
|
async fn check_invoice(&self, payment_hash: &str) -> Result<InvoiceStatus, Error> {
|
||||||
|
let url = Url::parse(&self.settings.pay_to_relay.node_url)?
|
||||||
|
.join(APIPATH)?
|
||||||
|
.join(payment_hash)?;
|
||||||
|
let uri = Uri::from_str(url.as_str()).unwrap();
|
||||||
|
|
||||||
|
let req = hyper::Request::builder()
|
||||||
|
.method(hyper::Method::GET)
|
||||||
|
.uri(uri)
|
||||||
|
.header("X-Api-Key", &self.settings.pay_to_relay.api_secret)
|
||||||
|
.body(hyper::Body::empty())
|
||||||
|
.expect("request builder");
|
||||||
|
|
||||||
|
let res = self.client.request(req).await?;
|
||||||
|
// Json to Struct of LNbits callback
|
||||||
|
let body = hyper::body::to_bytes(res.into_body()).await?;
|
||||||
|
let invoice_response: Value = serde_json::from_slice(&body)?;
|
||||||
|
|
||||||
|
let status = if let Ok(invoice_response) =
|
||||||
|
serde_json::from_value::<LNBitsCheckInvoiceResponse>(invoice_response)
|
||||||
|
{
|
||||||
|
if invoice_response.paid {
|
||||||
|
InvoiceStatus::Paid
|
||||||
|
} else {
|
||||||
|
InvoiceStatus::Unpaid
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
InvoiceStatus::Expired
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(status)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,276 @@
|
|||||||
|
use crate::error::{Error, Result};
|
||||||
|
use crate::event::Event;
|
||||||
|
use crate::payment::lnbits::LNBitsPaymentProcessor;
|
||||||
|
use crate::repo::NostrRepo;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::sync::Arc;
|
||||||
|
use tracing::{info, warn};
|
||||||
|
|
||||||
|
use async_trait::async_trait;
|
||||||
|
use nostr::key::{FromPkStr, FromSkStr};
|
||||||
|
use nostr::{key::Keys, Event as NostrEvent, EventBuilder};
|
||||||
|
|
||||||
|
pub mod lnbits;
|
||||||
|
|
||||||
|
/// Payment handler
|
||||||
|
pub struct Payment {
|
||||||
|
/// Repository for saving/retrieving events and events
|
||||||
|
repo: Arc<dyn NostrRepo>,
|
||||||
|
/// Newly validated events get written and then broadcast on this channel to subscribers
|
||||||
|
event_tx: tokio::sync::broadcast::Sender<Event>,
|
||||||
|
/// Payment message sender
|
||||||
|
payment_tx: tokio::sync::broadcast::Sender<PaymentMessage>,
|
||||||
|
/// Payment message receiver
|
||||||
|
payment_rx: tokio::sync::broadcast::Receiver<PaymentMessage>,
|
||||||
|
/// Settings
|
||||||
|
settings: crate::config::Settings,
|
||||||
|
// Nostr Keys
|
||||||
|
nostr_keys: Option<Keys>,
|
||||||
|
/// Payment Processor
|
||||||
|
processor: Arc<dyn PaymentProcessor>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
pub trait PaymentProcessor: Send + Sync {
|
||||||
|
/// Get invoice from processor
|
||||||
|
async fn get_invoice(&self, keys: &Keys, amount: u64) -> Result<InvoiceInfo, Error>;
|
||||||
|
/// Check payment status of an invoice
|
||||||
|
async fn check_invoice(&self, payment_hash: &str) -> Result<InvoiceStatus, Error>;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
|
||||||
|
pub enum Processor {
|
||||||
|
LNBits,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Possible states of an invoice
|
||||||
|
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, sqlx::Type)]
|
||||||
|
#[sqlx(type_name = "status")]
|
||||||
|
pub enum InvoiceStatus {
|
||||||
|
Unpaid,
|
||||||
|
Paid,
|
||||||
|
Expired,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ToString for InvoiceStatus {
|
||||||
|
fn to_string(&self) -> String {
|
||||||
|
match self {
|
||||||
|
InvoiceStatus::Paid => "Paid".to_string(),
|
||||||
|
InvoiceStatus::Unpaid => "Unpaid".to_string(),
|
||||||
|
InvoiceStatus::Expired => "Expired".to_string(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Invoice information
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct InvoiceInfo {
|
||||||
|
pub pubkey: String,
|
||||||
|
pub payment_hash: String,
|
||||||
|
pub bolt11: String,
|
||||||
|
pub amount: u64,
|
||||||
|
pub status: InvoiceStatus,
|
||||||
|
pub memo: String,
|
||||||
|
pub confirmed_at: Option<u64>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Message variants for the payment channel
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub enum PaymentMessage {
|
||||||
|
/// New account
|
||||||
|
NewAccount(String),
|
||||||
|
/// Check account,
|
||||||
|
CheckAccount(String),
|
||||||
|
/// Account Admitted
|
||||||
|
AccountAdmitted(String),
|
||||||
|
/// Invoice generated
|
||||||
|
Invoice(String, InvoiceInfo),
|
||||||
|
/// Invoice call back
|
||||||
|
/// Payment hash is passed
|
||||||
|
// This may have to be changed to better support other processors
|
||||||
|
InvoicePaid(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Payment {
|
||||||
|
pub fn new(
|
||||||
|
repo: Arc<dyn NostrRepo>,
|
||||||
|
payment_tx: tokio::sync::broadcast::Sender<PaymentMessage>,
|
||||||
|
payment_rx: tokio::sync::broadcast::Receiver<PaymentMessage>,
|
||||||
|
event_tx: tokio::sync::broadcast::Sender<Event>,
|
||||||
|
settings: crate::config::Settings,
|
||||||
|
) -> Result<Self> {
|
||||||
|
info!("Create payment handler");
|
||||||
|
|
||||||
|
// Create nostr key from sk string
|
||||||
|
let nostr_keys = if let Some(secret_key) = &settings.pay_to_relay.secret_key {
|
||||||
|
Some(Keys::from_sk_str(secret_key)?)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
|
// Create processor kind defined in settings
|
||||||
|
let processor = match &settings.pay_to_relay.processor {
|
||||||
|
Processor::LNBits => Arc::new(LNBitsPaymentProcessor::new(&settings)),
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(Payment {
|
||||||
|
repo,
|
||||||
|
payment_tx,
|
||||||
|
payment_rx,
|
||||||
|
event_tx,
|
||||||
|
settings,
|
||||||
|
nostr_keys,
|
||||||
|
processor,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Perform Payment tasks
|
||||||
|
pub async fn run(&mut self) {
|
||||||
|
loop {
|
||||||
|
let res = self.run_internal().await;
|
||||||
|
if let Err(e) = res {
|
||||||
|
info!("error in payment: {:?}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Internal select loop for preforming payment operations
|
||||||
|
async fn run_internal(&mut self) -> Result<()> {
|
||||||
|
tokio::select! {
|
||||||
|
m = self.payment_rx.recv() => {
|
||||||
|
match m {
|
||||||
|
Ok(PaymentMessage::NewAccount(pubkey)) => {
|
||||||
|
info!("payment event for {:?}", pubkey);
|
||||||
|
// REVIEW: This will need to change for cost per event
|
||||||
|
let amount = self.settings.pay_to_relay.admission_cost;
|
||||||
|
let invoice_info = self.get_invoice_info(&pubkey, amount).await?;
|
||||||
|
// TODO: should handle this error
|
||||||
|
self.payment_tx.send(PaymentMessage::Invoice(pubkey, invoice_info)).ok();
|
||||||
|
},
|
||||||
|
// Gets the most recent unpaid invoice from database
|
||||||
|
// Checks LNbits to verify if paid/unpaid
|
||||||
|
Ok(PaymentMessage::CheckAccount(pubkey)) => {
|
||||||
|
let keys = Keys::from_pk_str(&pubkey)?;
|
||||||
|
|
||||||
|
if let Ok(Some(invoice_info)) = self.repo.get_unpaid_invoice(&keys).await {
|
||||||
|
match self.check_invoice_status(&invoice_info.payment_hash).await? {
|
||||||
|
InvoiceStatus::Paid => {
|
||||||
|
self.repo.admit_account(&keys, self.settings.pay_to_relay.admission_cost).await?;
|
||||||
|
self.payment_tx.send(PaymentMessage::AccountAdmitted(pubkey)).ok();
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
self.payment_tx.send(PaymentMessage::Invoice(pubkey, invoice_info)).ok();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
let amount = self.settings.pay_to_relay.admission_cost;
|
||||||
|
let invoice_info = self.get_invoice_info(&pubkey, amount).await?;
|
||||||
|
self.payment_tx.send(PaymentMessage::Invoice(pubkey, invoice_info)).ok();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(PaymentMessage::InvoicePaid(payment_hash)) => {
|
||||||
|
if self.check_invoice_status(&payment_hash).await?.eq(&InvoiceStatus::Paid) {
|
||||||
|
let pubkey = self.repo
|
||||||
|
.update_invoice(&payment_hash, InvoiceStatus::Paid)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let key = Keys::from_pk_str(&pubkey)?;
|
||||||
|
self.repo.admit_account(&key, self.settings.pay_to_relay.admission_cost).await?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(_) => {
|
||||||
|
// For this variant nothing need to be done here
|
||||||
|
// it is used by `server`
|
||||||
|
}
|
||||||
|
Err(err) => warn!("Payment RX: {err}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sends Nostr DM to pubkey that requested invoice
|
||||||
|
/// Two events the terms followed by the bolt11 invoice
|
||||||
|
pub async fn send_admission_message(
|
||||||
|
&self,
|
||||||
|
pubkey: &str,
|
||||||
|
invoice_info: &InvoiceInfo,
|
||||||
|
) -> Result<()> {
|
||||||
|
let nostr_keys = match &self.nostr_keys {
|
||||||
|
Some(key) => key,
|
||||||
|
None => return Err(Error::CustomError("Nostr key not defined".to_string())),
|
||||||
|
};
|
||||||
|
|
||||||
|
// Create Nostr key from pk
|
||||||
|
let key = Keys::from_pk_str(pubkey)?;
|
||||||
|
|
||||||
|
let pubkey = key.public_key();
|
||||||
|
|
||||||
|
// Event DM with terms of service
|
||||||
|
let message_event: NostrEvent = EventBuilder::new_encrypted_direct_msg(
|
||||||
|
nostr_keys,
|
||||||
|
pubkey,
|
||||||
|
&self.settings.pay_to_relay.terms_message,
|
||||||
|
)?
|
||||||
|
.to_event(nostr_keys)?;
|
||||||
|
|
||||||
|
// Event DM with invoice
|
||||||
|
let invoice_event: NostrEvent =
|
||||||
|
EventBuilder::new_encrypted_direct_msg(nostr_keys, pubkey, &invoice_info.bolt11)?
|
||||||
|
.to_event(nostr_keys)?;
|
||||||
|
|
||||||
|
// Persist DM events to DB
|
||||||
|
self.repo.write_event(&message_event.clone().into()).await?;
|
||||||
|
self.repo.write_event(&invoice_event.clone().into()).await?;
|
||||||
|
|
||||||
|
// Broadcast DM events
|
||||||
|
self.event_tx.send(message_event.clone().into()).ok();
|
||||||
|
self.event_tx.send(invoice_event.clone().into()).ok();
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get Invoice Info
|
||||||
|
/// If the has an active invoice that will be return
|
||||||
|
/// Otherwise a new invoice will be generated by the payment processor
|
||||||
|
pub async fn get_invoice_info(&self, pubkey: &str, amount: u64) -> Result<InvoiceInfo> {
|
||||||
|
// If user is already in DB this will be false
|
||||||
|
// This avoids recreating admission invoices
|
||||||
|
// I think it will continue to send DMs with the invoice
|
||||||
|
// If client continues to try and write to the relay (will be same invoice)
|
||||||
|
let key = Keys::from_pk_str(pubkey)?;
|
||||||
|
if !self.repo.create_account(&key).await? {
|
||||||
|
if let Ok(Some(invoice_info)) = self.repo.get_unpaid_invoice(&key).await {
|
||||||
|
return Ok(invoice_info);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let key = Keys::from_pk_str(pubkey)?;
|
||||||
|
|
||||||
|
let invoice_info = self.processor.get_invoice(&key, amount).await?;
|
||||||
|
|
||||||
|
// Persist invoice to DB
|
||||||
|
self.repo
|
||||||
|
.create_invoice_record(&key, invoice_info.clone())
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
if self.settings.pay_to_relay.direct_message {
|
||||||
|
// Admission event invoice and terms to pubkey that is joining
|
||||||
|
self.send_admission_message(pubkey, &invoice_info).await?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(invoice_info)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Check paid status of invoice with LNbits
|
||||||
|
pub async fn check_invoice_status(&self, payment_hash: &str) -> Result<InvoiceStatus, Error> {
|
||||||
|
// Check base if passed expiry time
|
||||||
|
let status = self.processor.check_invoice(payment_hash).await?;
|
||||||
|
self.repo
|
||||||
|
.update_invoice(payment_hash, status.clone())
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(status)
|
||||||
|
}
|
||||||
|
}
|
||||||
+31
-2
@@ -2,15 +2,17 @@ use crate::db::QueryResult;
|
|||||||
use crate::error::Result;
|
use crate::error::Result;
|
||||||
use crate::event::Event;
|
use crate::event::Event;
|
||||||
use crate::nip05::VerificationRecord;
|
use crate::nip05::VerificationRecord;
|
||||||
|
use crate::payment::{InvoiceInfo, InvoiceStatus};
|
||||||
use crate::subscription::Subscription;
|
use crate::subscription::Subscription;
|
||||||
use crate::utils::unix_time;
|
use crate::utils::unix_time;
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
|
use nostr::Keys;
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
|
|
||||||
pub mod sqlite;
|
|
||||||
pub mod sqlite_migration;
|
|
||||||
pub mod postgres;
|
pub mod postgres;
|
||||||
pub mod postgres_migration;
|
pub mod postgres_migration;
|
||||||
|
pub mod sqlite;
|
||||||
|
pub mod sqlite_migration;
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
pub trait NostrRepo: Send + Sync {
|
pub trait NostrRepo: Send + Sync {
|
||||||
@@ -57,6 +59,33 @@ pub trait NostrRepo: Send + Sync {
|
|||||||
|
|
||||||
/// Get oldest verification before timestamp
|
/// Get oldest verification before timestamp
|
||||||
async fn get_oldest_user_verification(&self, before: u64) -> Result<VerificationRecord>;
|
async fn get_oldest_user_verification(&self, before: u64) -> Result<VerificationRecord>;
|
||||||
|
|
||||||
|
/// Create a new account
|
||||||
|
async fn create_account(&self, pubkey: &Keys) -> Result<bool>;
|
||||||
|
|
||||||
|
/// Admit an account
|
||||||
|
async fn admit_account(&self, pubkey: &Keys, admission_cost: u64) -> Result<()>;
|
||||||
|
|
||||||
|
/// Gets user balance if they are an admitted pubkey
|
||||||
|
async fn get_account_balance(&self, pubkey: &Keys) -> Result<(bool, u64)>;
|
||||||
|
|
||||||
|
/// Update account balance
|
||||||
|
async fn update_account_balance(
|
||||||
|
&self,
|
||||||
|
pub_key: &Keys,
|
||||||
|
positive: bool,
|
||||||
|
new_balance: u64,
|
||||||
|
) -> Result<()>;
|
||||||
|
|
||||||
|
/// Create invoice record
|
||||||
|
async fn create_invoice_record(&self, pubkey: &Keys, invoice_info: InvoiceInfo) -> Result<()>;
|
||||||
|
|
||||||
|
/// Update Invoice for given payment hash
|
||||||
|
async fn update_invoice(&self, payment_hash: &str, status: InvoiceStatus) -> Result<String>;
|
||||||
|
|
||||||
|
/// Get the most recent invoice for a given pubkey
|
||||||
|
/// invoice must be unpaid and not expired
|
||||||
|
async fn get_unpaid_invoice(&self, pubkey: &Keys) -> Result<Option<InvoiceInfo>>;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Current time, with a slight forward jitter in seconds
|
// Current time, with a slight forward jitter in seconds
|
||||||
|
|||||||
+227
-48
@@ -2,44 +2,45 @@ use crate::db::QueryResult;
|
|||||||
use crate::error::Result;
|
use crate::error::Result;
|
||||||
use crate::event::{single_char_tagname, Event};
|
use crate::event::{single_char_tagname, Event};
|
||||||
use crate::nip05::{Nip05Name, VerificationRecord};
|
use crate::nip05::{Nip05Name, VerificationRecord};
|
||||||
|
use crate::payment::{InvoiceInfo, InvoiceStatus};
|
||||||
use crate::repo::{now_jitter, NostrRepo};
|
use crate::repo::{now_jitter, NostrRepo};
|
||||||
use crate::subscription::{ReqFilter, Subscription};
|
use crate::subscription::{ReqFilter, Subscription};
|
||||||
use async_std::stream::StreamExt;
|
use async_std::stream::StreamExt;
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use chrono::{DateTime, TimeZone, Utc};
|
use chrono::{DateTime, TimeZone, Utc};
|
||||||
use sqlx::postgres::PgRow;
|
use sqlx::postgres::PgRow;
|
||||||
|
use sqlx::Error::RowNotFound;
|
||||||
use sqlx::{Error, Execute, FromRow, Postgres, QueryBuilder, Row};
|
use sqlx::{Error, Execute, FromRow, Postgres, QueryBuilder, Row};
|
||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
use sqlx::Error::RowNotFound;
|
|
||||||
|
|
||||||
|
use crate::error;
|
||||||
use crate::hexrange::{hex_range, HexSearch};
|
use crate::hexrange::{hex_range, HexSearch};
|
||||||
use crate::repo::postgres_migration::run_migrations;
|
use crate::repo::postgres_migration::run_migrations;
|
||||||
use crate::server::NostrMetrics;
|
use crate::server::NostrMetrics;
|
||||||
use crate::utils::{is_hex, is_lower_hex, self};
|
use crate::utils::{self, is_hex, is_lower_hex};
|
||||||
|
use nostr::key::Keys;
|
||||||
use tokio::sync::mpsc::Sender;
|
use tokio::sync::mpsc::Sender;
|
||||||
use tokio::sync::oneshot::Receiver;
|
use tokio::sync::oneshot::Receiver;
|
||||||
use tracing::log::trace;
|
use tracing::{debug, error, info, trace, warn};
|
||||||
use tracing::{debug, error, warn, info};
|
|
||||||
use crate::error;
|
|
||||||
|
|
||||||
pub type PostgresPool = sqlx::pool::Pool<Postgres>;
|
pub type PostgresPool = sqlx::pool::Pool<Postgres>;
|
||||||
|
|
||||||
pub struct PostgresRepo {
|
pub struct PostgresRepo {
|
||||||
conn: PostgresPool,
|
conn: PostgresPool,
|
||||||
|
conn_write: PostgresPool,
|
||||||
metrics: NostrMetrics,
|
metrics: NostrMetrics,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PostgresRepo {
|
impl PostgresRepo {
|
||||||
pub fn new(c: PostgresPool, m: NostrMetrics) -> PostgresRepo {
|
pub fn new(c: PostgresPool, cw: PostgresPool, m: NostrMetrics) -> PostgresRepo {
|
||||||
PostgresRepo {
|
PostgresRepo {
|
||||||
conn: c,
|
conn: c,
|
||||||
|
conn_write: cw,
|
||||||
metrics: m,
|
metrics: m,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Cleanup expired events on a regular basis
|
/// Cleanup expired events on a regular basis
|
||||||
async fn cleanup_expired(conn: PostgresPool, frequency: Duration) -> Result<()> {
|
async fn cleanup_expired(conn: PostgresPool, frequency: Duration) -> Result<()> {
|
||||||
tokio::task::spawn(async move {
|
tokio::task::spawn(async move {
|
||||||
@@ -66,32 +67,32 @@ async fn cleanup_expired(conn: PostgresPool, frequency: Duration) -> Result<()>
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// One-time deletion of all expired events
|
/// One-time deletion of all expired events
|
||||||
async fn delete_expired(conn:PostgresPool) -> Result<u64> {
|
async fn delete_expired(conn: PostgresPool) -> Result<u64> {
|
||||||
let mut tx = conn.begin().await?;
|
let mut tx = conn.begin().await?;
|
||||||
let update_count = sqlx::query("DELETE FROM \"event\" WHERE expires_at <= $1;")
|
let update_count = sqlx::query("DELETE FROM \"event\" WHERE expires_at <= $1;")
|
||||||
.bind(Utc.timestamp_opt(utils::unix_time() as i64, 0).unwrap())
|
.bind(Utc.timestamp_opt(utils::unix_time() as i64, 0).unwrap())
|
||||||
.execute(&mut tx)
|
.execute(&mut tx)
|
||||||
.await?.rows_affected();
|
.await?
|
||||||
|
.rows_affected();
|
||||||
tx.commit().await?;
|
tx.commit().await?;
|
||||||
Ok(update_count)
|
Ok(update_count)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl NostrRepo for PostgresRepo {
|
impl NostrRepo for PostgresRepo {
|
||||||
|
|
||||||
async fn start(&self) -> Result<()> {
|
async fn start(&self) -> Result<()> {
|
||||||
// begin a cleanup task for expired events.
|
// begin a cleanup task for expired events.
|
||||||
cleanup_expired(self.conn.clone(), Duration::from_secs(600)).await?;
|
cleanup_expired(self.conn_write.clone(), Duration::from_secs(600)).await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn migrate_up(&self) -> Result<usize> {
|
async fn migrate_up(&self) -> Result<usize> {
|
||||||
Ok(run_migrations(&self.conn).await?)
|
Ok(run_migrations(&self.conn_write).await?)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn write_event(&self, e: &Event) -> Result<u64> {
|
async fn write_event(&self, e: &Event) -> Result<u64> {
|
||||||
// start transaction
|
// start transaction
|
||||||
let mut tx = self.conn.begin().await?;
|
let mut tx = self.conn_write.begin().await?;
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
|
|
||||||
// get relevant fields from event and convert to blobs.
|
// get relevant fields from event and convert to blobs.
|
||||||
@@ -116,7 +117,7 @@ impl NostrRepo for PostgresRepo {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if let Some(d_tag) = e.distinct_param() {
|
if let Some(d_tag) = e.distinct_param() {
|
||||||
let repl_count:i64 = if is_lower_hex(&d_tag) && (d_tag.len() % 2 == 0) {
|
let repl_count: i64 = if is_lower_hex(&d_tag) && (d_tag.len() % 2 == 0) {
|
||||||
sqlx::query_scalar(
|
sqlx::query_scalar(
|
||||||
"SELECT count(*) AS count FROM event e LEFT JOIN tag t ON e.id=t.event_id WHERE e.pub_key=$1 AND e.kind=$2 AND t.name='d' AND t.value_hex=$3 AND e.created_at >= $4 LIMIT 1;")
|
"SELECT count(*) AS count FROM event e LEFT JOIN tag t ON e.id=t.event_id WHERE e.pub_key=$1 AND e.kind=$2 AND t.name='d' AND t.value_hex=$3 AND e.created_at >= $4 LIMIT 1;")
|
||||||
.bind(hex::decode(&e.pubkey).ok())
|
.bind(hex::decode(&e.pubkey).ok())
|
||||||
@@ -139,7 +140,7 @@ impl NostrRepo for PostgresRepo {
|
|||||||
// the same author/kind/tag value exist, and we can ignore
|
// the same author/kind/tag value exist, and we can ignore
|
||||||
// this event.
|
// this event.
|
||||||
if repl_count > 0 {
|
if repl_count > 0 {
|
||||||
return Ok(0)
|
return Ok(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// ignore if the event hash is a duplicate.
|
// ignore if the event hash is a duplicate.
|
||||||
@@ -149,16 +150,19 @@ impl NostrRepo for PostgresRepo {
|
|||||||
VALUES($1, $2, $3, $4, $5, $6, $7)
|
VALUES($1, $2, $3, $4, $5, $6, $7)
|
||||||
ON CONFLICT (id) DO NOTHING"#,
|
ON CONFLICT (id) DO NOTHING"#,
|
||||||
)
|
)
|
||||||
.bind(&id_blob)
|
.bind(&id_blob)
|
||||||
.bind(&pubkey_blob)
|
.bind(&pubkey_blob)
|
||||||
.bind(Utc.timestamp_opt(e.created_at as i64, 0).unwrap())
|
.bind(Utc.timestamp_opt(e.created_at as i64, 0).unwrap())
|
||||||
.bind(e.expiration().and_then(|x| Utc.timestamp_opt(x as i64, 0).latest()))
|
.bind(
|
||||||
.bind(e.kind as i64)
|
e.expiration()
|
||||||
.bind(event_str.into_bytes())
|
.and_then(|x| Utc.timestamp_opt(x as i64, 0).latest()),
|
||||||
.bind(delegator_blob)
|
)
|
||||||
.execute(&mut tx)
|
.bind(e.kind as i64)
|
||||||
.await?
|
.bind(event_str.into_bytes())
|
||||||
.rows_affected();
|
.bind(delegator_blob)
|
||||||
|
.execute(&mut tx)
|
||||||
|
.await?
|
||||||
|
.rows_affected();
|
||||||
|
|
||||||
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
|
||||||
@@ -184,7 +188,8 @@ ON CONFLICT (id) DO NOTHING"#,
|
|||||||
.bind(tag_name)
|
.bind(tag_name)
|
||||||
.bind(hex::decode(tag_val).ok())
|
.bind(hex::decode(tag_val).ok())
|
||||||
.execute(&mut tx)
|
.execute(&mut tx)
|
||||||
.await?;
|
.await
|
||||||
|
.unwrap();
|
||||||
} else {
|
} else {
|
||||||
sqlx::query("INSERT INTO tag (event_id, \"name\", value, value_hex) VALUES($1, $2, $3, NULL) \
|
sqlx::query("INSERT INTO tag (event_id, \"name\", value, value_hex) VALUES($1, $2, $3, NULL) \
|
||||||
ON CONFLICT (event_id, \"name\", value, value_hex) DO NOTHING")
|
ON CONFLICT (event_id, \"name\", value, value_hex) DO NOTHING")
|
||||||
@@ -192,7 +197,8 @@ ON CONFLICT (id) DO NOTHING"#,
|
|||||||
.bind(tag_name)
|
.bind(tag_name)
|
||||||
.bind(tag_val.as_bytes())
|
.bind(tag_val.as_bytes())
|
||||||
.execute(&mut tx)
|
.execute(&mut tx)
|
||||||
.await?;
|
.await
|
||||||
|
.unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None => {}
|
None => {}
|
||||||
@@ -276,10 +282,10 @@ ON CONFLICT (id) DO NOTHING"#,
|
|||||||
LEFT JOIN tag t ON e.id = t.event_id \
|
LEFT JOIN tag t ON e.id = t.event_id \
|
||||||
WHERE e.pub_key = $1 AND t.\"name\" = 'e' AND e.kind = 5 AND t.value = $2 LIMIT 1",
|
WHERE e.pub_key = $1 AND t.\"name\" = 'e' AND e.kind = 5 AND t.value = $2 LIMIT 1",
|
||||||
)
|
)
|
||||||
.bind(&pubkey_blob)
|
.bind(&pubkey_blob)
|
||||||
.bind(&id_blob)
|
.bind(&id_blob)
|
||||||
.fetch_optional(&mut tx)
|
.fetch_optional(&mut tx)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
// check if a the query returned a result, meaning we should
|
// check if a the query returned a result, meaning we should
|
||||||
// hid the current event
|
// hid the current event
|
||||||
@@ -380,7 +386,10 @@ ON CONFLICT (id) DO NOTHING"#,
|
|||||||
|
|
||||||
// check if this is still active; every 100 rows
|
// check if this is still active; every 100 rows
|
||||||
if row_count % 100 == 0 && abandon_query_rx.try_recv().is_ok() {
|
if row_count % 100 == 0 && abandon_query_rx.try_recv().is_ok() {
|
||||||
debug!("query cancelled by client (cid: {}, sub: {:?})", client_id, sub.id);
|
debug!(
|
||||||
|
"query cancelled by client (cid: {}, sub: {:?})",
|
||||||
|
client_id, sub.id
|
||||||
|
);
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -396,7 +405,10 @@ ON CONFLICT (id) DO NOTHING"#,
|
|||||||
if last_successful_send + abort_cutoff < Instant::now() {
|
if last_successful_send + abort_cutoff < Instant::now() {
|
||||||
// the queue has been full for too long, abort
|
// the queue has been full for too long, abort
|
||||||
info!("aborting database query due to slow client");
|
info!("aborting database query due to slow client");
|
||||||
metrics.query_aborts.with_label_values(&["slowclient"]).inc();
|
metrics
|
||||||
|
.query_aborts
|
||||||
|
.with_label_values(&["slowclient"])
|
||||||
|
.inc();
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
// give the queue a chance to clear before trying again
|
// give the queue a chance to clear before trying again
|
||||||
@@ -444,7 +456,7 @@ ON CONFLICT (id) DO NOTHING"#,
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn create_verification_record(&self, event_id: &str, name: &str) -> Result<()> {
|
async fn create_verification_record(&self, event_id: &str, name: &str) -> Result<()> {
|
||||||
let mut tx = self.conn.begin().await?;
|
let mut tx = self.conn_write.begin().await?;
|
||||||
|
|
||||||
sqlx::query("DELETE FROM user_verification WHERE \"name\" = $1")
|
sqlx::query("DELETE FROM user_verification WHERE \"name\" = $1")
|
||||||
.bind(name)
|
.bind(name)
|
||||||
@@ -467,12 +479,10 @@ ON CONFLICT (id) DO NOTHING"#,
|
|||||||
let verify_time = now_jitter(600);
|
let verify_time = now_jitter(600);
|
||||||
|
|
||||||
// update verification time and reset any failure count
|
// update verification time and reset any failure count
|
||||||
sqlx::query(
|
sqlx::query("UPDATE user_verification SET verified_at = $1, fail_count = 0 WHERE id = $2")
|
||||||
"UPDATE user_verification SET verified_at = $1, fail_count = 0 WHERE id = $2",
|
|
||||||
)
|
|
||||||
.bind(Utc.timestamp_opt(verify_time as i64, 0).unwrap())
|
.bind(Utc.timestamp_opt(verify_time as i64, 0).unwrap())
|
||||||
.bind(id as i64)
|
.bind(id as i64)
|
||||||
.execute(&self.conn)
|
.execute(&self.conn_write)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
info!("verification updated for {}", id);
|
info!("verification updated for {}", id);
|
||||||
@@ -482,7 +492,7 @@ ON CONFLICT (id) DO NOTHING"#,
|
|||||||
async fn fail_verification(&self, id: u64) -> Result<()> {
|
async fn fail_verification(&self, id: u64) -> Result<()> {
|
||||||
sqlx::query("UPDATE user_verification SET failed_at = now(), fail_count = fail_count + 1 WHERE id = $1")
|
sqlx::query("UPDATE user_verification SET failed_at = now(), fail_count = fail_count + 1 WHERE id = $1")
|
||||||
.bind(id as i64)
|
.bind(id as i64)
|
||||||
.execute(&self.conn)
|
.execute(&self.conn_write)
|
||||||
.await?;
|
.await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -490,7 +500,7 @@ ON CONFLICT (id) DO NOTHING"#,
|
|||||||
async fn delete_verification(&self, id: u64) -> Result<()> {
|
async fn delete_verification(&self, id: u64) -> Result<()> {
|
||||||
sqlx::query("DELETE FROM user_verification WHERE id = $1")
|
sqlx::query("DELETE FROM user_verification WHERE id = $1")
|
||||||
.bind(id as i64)
|
.bind(id as i64)
|
||||||
.execute(&self.conn)
|
.execute(&self.conn_write)
|
||||||
.await?;
|
.await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -539,6 +549,172 @@ ON CONFLICT (id) DO NOTHING"#,
|
|||||||
.await?
|
.await?
|
||||||
.ok_or(error::Error::SqlxError(RowNotFound))
|
.ok_or(error::Error::SqlxError(RowNotFound))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn create_account(&self, pub_key: &Keys) -> Result<bool> {
|
||||||
|
let pub_key = pub_key.public_key().to_string();
|
||||||
|
let mut tx = self.conn_write.begin().await?;
|
||||||
|
|
||||||
|
let result = sqlx::query("INSERT INTO account (pubkey, balance) VALUES ($1, 0);")
|
||||||
|
.bind(pub_key)
|
||||||
|
.execute(&mut tx)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
let success = match result {
|
||||||
|
Ok(res) => {
|
||||||
|
tx.commit().await?;
|
||||||
|
res.rows_affected() == 1
|
||||||
|
}
|
||||||
|
Err(_err) => false,
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(success)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Admit account
|
||||||
|
async fn admit_account(&self, pub_key: &Keys, admission_cost: u64) -> Result<()> {
|
||||||
|
let pub_key = pub_key.public_key().to_string();
|
||||||
|
sqlx::query(
|
||||||
|
"UPDATE account SET is_admitted = TRUE, balance = balance - $1 WHERE pubkey = $2",
|
||||||
|
)
|
||||||
|
.bind(admission_cost as i64)
|
||||||
|
.bind(pub_key)
|
||||||
|
.execute(&self.conn_write)
|
||||||
|
.await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Gets if the account is admitted and balance
|
||||||
|
async fn get_account_balance(&self, pub_key: &Keys) -> Result<(bool, u64)> {
|
||||||
|
let pub_key = pub_key.public_key().to_string();
|
||||||
|
let query = r#"SELECT
|
||||||
|
is_admitted,
|
||||||
|
balance
|
||||||
|
FROM account
|
||||||
|
WHERE pubkey = $1
|
||||||
|
LIMIT 1"#;
|
||||||
|
|
||||||
|
let result = sqlx::query_as::<_, (bool, i64)>(query)
|
||||||
|
.bind(pub_key)
|
||||||
|
.fetch_optional(&self.conn_write)
|
||||||
|
.await?
|
||||||
|
.ok_or(error::Error::SqlxError(RowNotFound))?;
|
||||||
|
|
||||||
|
Ok((result.0, result.1 as u64))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Update account balance
|
||||||
|
async fn update_account_balance(
|
||||||
|
&self,
|
||||||
|
pub_key: &Keys,
|
||||||
|
positive: bool,
|
||||||
|
new_balance: u64,
|
||||||
|
) -> Result<()> {
|
||||||
|
let pub_key = pub_key.public_key().to_string();
|
||||||
|
match positive {
|
||||||
|
true => {
|
||||||
|
sqlx::query("UPDATE account SET balance = balance + $1 WHERE pubkey = $2")
|
||||||
|
.bind(new_balance as i64)
|
||||||
|
.bind(pub_key)
|
||||||
|
.execute(&self.conn_write)
|
||||||
|
.await?
|
||||||
|
}
|
||||||
|
false => {
|
||||||
|
sqlx::query("UPDATE account SET balance = balance - $1 WHERE pubkey = $2")
|
||||||
|
.bind(new_balance as i64)
|
||||||
|
.bind(pub_key)
|
||||||
|
.execute(&self.conn_write)
|
||||||
|
.await?
|
||||||
|
}
|
||||||
|
};
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create invoice record
|
||||||
|
async fn create_invoice_record(&self, pub_key: &Keys, invoice_info: InvoiceInfo) -> Result<()> {
|
||||||
|
let pub_key = pub_key.public_key().to_string();
|
||||||
|
let mut tx = self.conn_write.begin().await?;
|
||||||
|
|
||||||
|
sqlx::query(
|
||||||
|
"INSERT INTO invoice (pubkey, payment_hash, amount, status, description, created_at, invoice) VALUES ($1, $2, $3, $4, $5, now(), $6)",
|
||||||
|
)
|
||||||
|
.bind(pub_key)
|
||||||
|
.bind(invoice_info.payment_hash)
|
||||||
|
.bind(invoice_info.amount as i64)
|
||||||
|
.bind(invoice_info.status)
|
||||||
|
.bind(invoice_info.memo)
|
||||||
|
.bind(invoice_info.bolt11)
|
||||||
|
.execute(&mut tx)
|
||||||
|
.await.unwrap();
|
||||||
|
|
||||||
|
debug!("Invoice added");
|
||||||
|
|
||||||
|
tx.commit().await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Update invoice record
|
||||||
|
async fn update_invoice(&self, payment_hash: &str, status: InvoiceStatus) -> Result<String> {
|
||||||
|
debug!("Payment Hash: {}", payment_hash);
|
||||||
|
let query = "SELECT pubkey, status, amount FROM invoice WHERE payment_hash=$1;";
|
||||||
|
let (pubkey, prev_invoice_status, amount) =
|
||||||
|
sqlx::query_as::<_, (String, InvoiceStatus, i64)>(query)
|
||||||
|
.bind(payment_hash)
|
||||||
|
.fetch_optional(&self.conn_write)
|
||||||
|
.await?
|
||||||
|
.ok_or(error::Error::SqlxError(RowNotFound))?;
|
||||||
|
|
||||||
|
// If the invoice is paid update the confirmed at timestamp
|
||||||
|
let query = if status.eq(&InvoiceStatus::Paid) {
|
||||||
|
"UPDATE invoice SET status=$1, confirmed_at = now() WHERE payment_hash=$2;"
|
||||||
|
} else {
|
||||||
|
"UPDATE invoice SET status=$1 WHERE payment_hash=$2;"
|
||||||
|
};
|
||||||
|
|
||||||
|
sqlx::query(query)
|
||||||
|
.bind(&status)
|
||||||
|
.bind(payment_hash)
|
||||||
|
.execute(&self.conn_write)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
if prev_invoice_status.eq(&InvoiceStatus::Unpaid) && status.eq(&InvoiceStatus::Paid) {
|
||||||
|
sqlx::query("UPDATE account SET balance = balance + $1 WHERE pubkey = $2")
|
||||||
|
.bind(amount)
|
||||||
|
.bind(&pubkey)
|
||||||
|
.execute(&self.conn_write)
|
||||||
|
.await?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(pubkey)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get the most recent invoice for a given pubkey
|
||||||
|
/// invoice must be unpaid and not expired
|
||||||
|
async fn get_unpaid_invoice(&self, pubkey: &Keys) -> Result<Option<InvoiceInfo>> {
|
||||||
|
let query = r#"
|
||||||
|
SELECT amount, payment_hash, description, invoice
|
||||||
|
FROM invoice
|
||||||
|
WHERE pubkey = $1
|
||||||
|
ORDER BY created_at DESC
|
||||||
|
LIMIT 1;
|
||||||
|
"#;
|
||||||
|
match sqlx::query_as::<_, (i64, String, String, String)>(query)
|
||||||
|
.bind(pubkey.public_key().to_string())
|
||||||
|
.fetch_optional(&self.conn_write)
|
||||||
|
.await
|
||||||
|
.unwrap()
|
||||||
|
{
|
||||||
|
Some((amount, payment_hash, description, invoice)) => Ok(Some(InvoiceInfo {
|
||||||
|
pubkey: pubkey.public_key().to_string(),
|
||||||
|
payment_hash,
|
||||||
|
bolt11: invoice,
|
||||||
|
amount: amount as u64,
|
||||||
|
status: InvoiceStatus::Unpaid,
|
||||||
|
memo: description,
|
||||||
|
confirmed_at: None,
|
||||||
|
})),
|
||||||
|
None => Ok(None),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a dynamic SQL query and params from a subscription filter.
|
/// Create a dynamic SQL query and params from a subscription filter.
|
||||||
@@ -727,7 +903,7 @@ fn query_from_filter(f: &ReqFilter) -> Option<QueryBuilder<Postgres>> {
|
|||||||
}
|
}
|
||||||
push_and = true;
|
push_and = true;
|
||||||
query
|
query
|
||||||
.push("e.created_at > ")
|
.push("e.created_at >= ")
|
||||||
.push_bind(Utc.timestamp_opt(f.since.unwrap() as i64, 0).unwrap());
|
.push_bind(Utc.timestamp_opt(f.since.unwrap() as i64, 0).unwrap());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -738,7 +914,7 @@ fn query_from_filter(f: &ReqFilter) -> Option<QueryBuilder<Postgres>> {
|
|||||||
}
|
}
|
||||||
push_and = true;
|
push_and = true;
|
||||||
query
|
query
|
||||||
.push("e.created_at < ")
|
.push("e.created_at <= ")
|
||||||
.push_bind(Utc.timestamp_opt(f.until.unwrap() as i64, 0).unwrap());
|
.push_bind(Utc.timestamp_opt(f.until.unwrap() as i64, 0).unwrap());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -751,7 +927,8 @@ fn query_from_filter(f: &ReqFilter) -> Option<QueryBuilder<Postgres>> {
|
|||||||
// never display expired events
|
// never display expired events
|
||||||
query
|
query
|
||||||
.push(" AND (e.expires_at IS NULL OR e.expires_at > ")
|
.push(" AND (e.expires_at IS NULL OR e.expires_at > ")
|
||||||
.push_bind(Utc.timestamp_opt(utils::unix_time() as i64, 0).unwrap()).push(")");
|
.push_bind(Utc.timestamp_opt(utils::unix_time() as i64, 0).unwrap())
|
||||||
|
.push(")");
|
||||||
|
|
||||||
// Apply per-filter limit to this query.
|
// Apply per-filter limit to this query.
|
||||||
// The use of a LIMIT implies a DESC order, to capture only the most recent events.
|
// The use of a LIMIT implies a DESC order, to capture only the most recent events.
|
||||||
@@ -767,15 +944,17 @@ fn query_from_filter(f: &ReqFilter) -> Option<QueryBuilder<Postgres>> {
|
|||||||
|
|
||||||
impl FromRow<'_, PgRow> for VerificationRecord {
|
impl FromRow<'_, PgRow> for VerificationRecord {
|
||||||
fn from_row(row: &'_ PgRow) -> std::result::Result<Self, Error> {
|
fn from_row(row: &'_ PgRow) -> std::result::Result<Self, Error> {
|
||||||
let name =
|
let name = Nip05Name::try_from(row.get::<'_, &str, &str>("name")).or(Err(RowNotFound))?;
|
||||||
Nip05Name::try_from(row.get::<'_, &str, &str>("name")).or(Err(RowNotFound))?;
|
|
||||||
Ok(VerificationRecord {
|
Ok(VerificationRecord {
|
||||||
rowid: row.get::<'_, i64, &str>("id") as u64,
|
rowid: row.get::<'_, i64, &str>("id") as u64,
|
||||||
name,
|
name,
|
||||||
address: hex::encode(row.get::<'_, Vec<u8>, &str>("pub_key")),
|
address: hex::encode(row.get::<'_, Vec<u8>, &str>("pub_key")),
|
||||||
event: hex::encode(row.get::<'_, Vec<u8>, &str>("event_id")),
|
event: hex::encode(row.get::<'_, Vec<u8>, &str>("event_id")),
|
||||||
event_created: row.get::<'_, DateTime<Utc>, &str>("created_at").timestamp() as u64,
|
event_created: row.get::<'_, DateTime<Utc>, &str>("created_at").timestamp() as u64,
|
||||||
last_success: None,
|
last_success: match row.try_get::<'_, DateTime<Utc>, &str>("verified_at") {
|
||||||
|
Ok(x) => Some(x.timestamp() as u64),
|
||||||
|
_ => None,
|
||||||
|
},
|
||||||
last_failure: match row.try_get::<'_, DateTime<Utc>, &str>("failed_at") {
|
last_failure: match row.try_get::<'_, DateTime<Utc>, &str>("failed_at") {
|
||||||
Ok(x) => Some(x.timestamp() as u64),
|
Ok(x) => Some(x.timestamp() as u64),
|
||||||
_ => None,
|
_ => None,
|
||||||
|
|||||||
@@ -36,6 +36,7 @@ pub async fn run_migrations(db: &PostgresPool) -> crate::error::Result<usize> {
|
|||||||
}
|
}
|
||||||
run_migration(m003::migration(), db).await;
|
run_migration(m003::migration(), db).await;
|
||||||
run_migration(m004::migration(), db).await;
|
run_migration(m004::migration(), db).await;
|
||||||
|
run_migration(m005::migration(), db).await;
|
||||||
Ok(current_version(db).await as usize)
|
Ok(current_version(db).await as usize)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -122,7 +123,7 @@ CREATE TABLE "tag" (
|
|||||||
CREATE INDEX tag_event_id_idx ON tag USING btree (event_id, name);
|
CREATE INDEX tag_event_id_idx ON tag USING btree (event_id, name);
|
||||||
CREATE INDEX tag_value_idx ON tag USING btree (value);
|
CREATE INDEX tag_value_idx ON tag USING btree (value);
|
||||||
|
|
||||||
-- NIP-05 Verfication table
|
-- NIP-05 Verification table
|
||||||
CREATE TABLE "user_verification" (
|
CREATE TABLE "user_verification" (
|
||||||
id int8 NOT NULL GENERATED BY DEFAULT AS IDENTITY,
|
id int8 NOT NULL GENERATED BY DEFAULT AS IDENTITY,
|
||||||
event_id bytea NOT NULL,
|
event_id bytea NOT NULL,
|
||||||
@@ -277,3 +278,43 @@ CREATE INDEX event_expires_at_idx ON "event" (expires_at);
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mod m005 {
|
||||||
|
use crate::repo::postgres_migration::{Migration, SimpleSqlMigration};
|
||||||
|
|
||||||
|
pub const VERSION: i64 = 5;
|
||||||
|
|
||||||
|
pub fn migration() -> impl Migration {
|
||||||
|
SimpleSqlMigration {
|
||||||
|
serial_number: VERSION,
|
||||||
|
sql: vec![
|
||||||
|
r#"
|
||||||
|
-- Create account table
|
||||||
|
CREATE TABLE "account" (
|
||||||
|
pubkey varchar NOT NULL,
|
||||||
|
is_admitted BOOLEAN NOT NULL DEFAULT FALSE,
|
||||||
|
balance BIGINT NOT NULL DEFAULT 0,
|
||||||
|
tos_accepted_at TIMESTAMP,
|
||||||
|
CONSTRAINT account_pkey PRIMARY KEY (pubkey)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TYPE status AS ENUM ('Paid', 'Unpaid', 'Expired');
|
||||||
|
|
||||||
|
|
||||||
|
CREATE TABLE "invoice" (
|
||||||
|
payment_hash varchar NOT NULL,
|
||||||
|
pubkey varchar NOT NULL,
|
||||||
|
invoice varchar NOT NULL,
|
||||||
|
amount BIGINT NOT NULL,
|
||||||
|
status status NOT NULL DEFAULT 'Unpaid',
|
||||||
|
description varchar,
|
||||||
|
created_at timestamp,
|
||||||
|
confirmed_at timestamp,
|
||||||
|
CONSTRAINT invoice_payment_hash PRIMARY KEY (payment_hash),
|
||||||
|
CONSTRAINT invoice_pubkey_fkey FOREIGN KEY (pubkey) REFERENCES account (pubkey) ON DELETE CASCADE
|
||||||
|
);
|
||||||
|
"#,
|
||||||
|
],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
+346
-80
@@ -1,34 +1,36 @@
|
|||||||
//! Event persistence and querying
|
//! Event persistence and querying
|
||||||
//use crate::config::SETTINGS;
|
//use crate::config::SETTINGS;
|
||||||
use crate::config::Settings;
|
use crate::config::Settings;
|
||||||
use crate::error::{Result,Error::SqlError};
|
use crate::db::QueryResult;
|
||||||
|
use crate::error::{Error::SqlError, Result};
|
||||||
use crate::event::{single_char_tagname, Event};
|
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::repo::sqlite_migration::{STARTUP_SQL,upgrade_db};
|
|
||||||
use crate::utils::{is_hex,unix_time};
|
|
||||||
use crate::nip05::{Nip05Name, VerificationRecord};
|
use crate::nip05::{Nip05Name, VerificationRecord};
|
||||||
use crate::subscription::{ReqFilter, Subscription};
|
use crate::payment::{InvoiceInfo, InvoiceStatus};
|
||||||
|
use crate::repo::sqlite_migration::{upgrade_db, STARTUP_SQL};
|
||||||
use crate::server::NostrMetrics;
|
use crate::server::NostrMetrics;
|
||||||
|
use crate::subscription::{ReqFilter, Subscription};
|
||||||
|
use crate::utils::{is_hex, unix_time};
|
||||||
|
use async_trait::async_trait;
|
||||||
use hex;
|
use hex;
|
||||||
use r2d2;
|
use r2d2;
|
||||||
use r2d2_sqlite::SqliteConnectionManager;
|
use r2d2_sqlite::SqliteConnectionManager;
|
||||||
use rusqlite::params;
|
use rusqlite::params;
|
||||||
use rusqlite::types::ToSql;
|
use rusqlite::types::ToSql;
|
||||||
use rusqlite::OpenFlags;
|
use rusqlite::OpenFlags;
|
||||||
use tokio::sync::{Mutex, MutexGuard, Semaphore};
|
|
||||||
use std::fmt::Write as _;
|
use std::fmt::Write as _;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
|
use tokio::sync::{Mutex, MutexGuard, Semaphore};
|
||||||
use tokio::task;
|
use tokio::task;
|
||||||
use tracing::{debug, info, trace, warn};
|
use tracing::{debug, info, trace, warn};
|
||||||
use async_trait::async_trait;
|
|
||||||
use crate::db::QueryResult;
|
|
||||||
|
|
||||||
use crate::repo::{now_jitter, NostrRepo};
|
use crate::repo::{now_jitter, NostrRepo};
|
||||||
|
use nostr::key::Keys;
|
||||||
|
|
||||||
pub type SqlitePool = r2d2::Pool<r2d2_sqlite::SqliteConnectionManager>;
|
pub type SqlitePool = r2d2::Pool<r2d2_sqlite::SqliteConnectionManager>;
|
||||||
pub type PooledConnection = r2d2::PooledConnection<r2d2_sqlite::SqliteConnectionManager>;
|
pub type PooledConnection = r2d2::PooledConnection<r2d2_sqlite::SqliteConnectionManager>;
|
||||||
@@ -54,12 +56,13 @@ pub struct SqliteRepo {
|
|||||||
|
|
||||||
impl SqliteRepo {
|
impl SqliteRepo {
|
||||||
// build all the pools needed
|
// build all the pools needed
|
||||||
#[must_use] pub fn new(settings: &Settings, metrics: NostrMetrics) -> SqliteRepo {
|
#[must_use]
|
||||||
|
pub fn new(settings: &Settings, metrics: NostrMetrics) -> SqliteRepo {
|
||||||
let write_pool = build_pool(
|
let write_pool = build_pool(
|
||||||
"writer",
|
"writer",
|
||||||
settings,
|
settings,
|
||||||
OpenFlags::SQLITE_OPEN_READ_WRITE | OpenFlags::SQLITE_OPEN_CREATE,
|
OpenFlags::SQLITE_OPEN_READ_WRITE | OpenFlags::SQLITE_OPEN_CREATE,
|
||||||
1,
|
0,
|
||||||
2,
|
2,
|
||||||
false,
|
false,
|
||||||
);
|
);
|
||||||
@@ -67,7 +70,7 @@ impl SqliteRepo {
|
|||||||
"maintenance",
|
"maintenance",
|
||||||
settings,
|
settings,
|
||||||
OpenFlags::SQLITE_OPEN_READ_WRITE | OpenFlags::SQLITE_OPEN_CREATE,
|
OpenFlags::SQLITE_OPEN_READ_WRITE | OpenFlags::SQLITE_OPEN_CREATE,
|
||||||
1,
|
0,
|
||||||
2,
|
2,
|
||||||
true,
|
true,
|
||||||
);
|
);
|
||||||
@@ -110,7 +113,8 @@ impl SqliteRepo {
|
|||||||
// 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: Option<Vec<u8>> = 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 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();
|
||||||
// check for replaceable events that would hide this one; we won't even attempt to insert these.
|
// check for replaceable events that would hide this one; we won't even attempt to insert these.
|
||||||
if e.is_replaceable() {
|
if e.is_replaceable() {
|
||||||
@@ -130,7 +134,7 @@ impl SqliteRepo {
|
|||||||
// the same author/kind/tag value exist, and we can ignore
|
// the same author/kind/tag value exist, and we can ignore
|
||||||
// this event.
|
// this event.
|
||||||
if repl_count.ok().is_some() {
|
if repl_count.ok().is_some() {
|
||||||
return Ok(0)
|
return Ok(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// ignore if the event hash is a duplicate.
|
// ignore if the event hash is a duplicate.
|
||||||
@@ -249,21 +253,26 @@ impl SqliteRepo {
|
|||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl NostrRepo for SqliteRepo {
|
impl NostrRepo for SqliteRepo {
|
||||||
|
|
||||||
async fn start(&self) -> Result<()> {
|
async fn start(&self) -> Result<()> {
|
||||||
db_checkpoint_task(self.maint_pool.clone(), Duration::from_secs(60),
|
db_checkpoint_task(
|
||||||
self.write_in_progress.clone(),
|
self.maint_pool.clone(),
|
||||||
self.checkpoint_in_progress.clone()).await?;
|
Duration::from_secs(60),
|
||||||
cleanup_expired(self.maint_pool.clone(), Duration::from_secs(600),
|
self.write_in_progress.clone(),
|
||||||
self.write_in_progress.clone()).await
|
self.checkpoint_in_progress.clone(),
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
cleanup_expired(
|
||||||
|
self.maint_pool.clone(),
|
||||||
|
Duration::from_secs(600),
|
||||||
|
self.write_in_progress.clone(),
|
||||||
|
)
|
||||||
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn migrate_up(&self) -> Result<usize> {
|
async fn migrate_up(&self) -> Result<usize> {
|
||||||
let _write_guard = self.write_in_progress.lock().await;
|
let _write_guard = self.write_in_progress.lock().await;
|
||||||
let mut conn = self.write_pool.get()?;
|
let mut conn = self.write_pool.get()?;
|
||||||
task::spawn_blocking(move || {
|
task::spawn_blocking(move || upgrade_db(&mut conn)).await?
|
||||||
upgrade_db(&mut conn)
|
|
||||||
}).await?
|
|
||||||
}
|
}
|
||||||
/// Persist event to database
|
/// Persist event to database
|
||||||
async fn write_event(&self, e: &Event) -> Result<u64> {
|
async fn write_event(&self, e: &Event) -> Result<u64> {
|
||||||
@@ -280,24 +289,29 @@ impl NostrRepo for SqliteRepo {
|
|||||||
// this could fail because the database was busy; try
|
// this could fail because the database was busy; try
|
||||||
// multiple times before giving up.
|
// multiple times before giving up.
|
||||||
loop {
|
loop {
|
||||||
attempts+=1;
|
attempts += 1;
|
||||||
let wr = SqliteRepo::persist_event(&mut conn, &e);
|
let wr = SqliteRepo::persist_event(&mut conn, &e);
|
||||||
match wr {
|
match wr {
|
||||||
Err(SqlError(rusqlite::Error::SqliteFailure(e,_))) => {
|
Err(SqlError(rusqlite::Error::SqliteFailure(e, _))) => {
|
||||||
// this basically means that NIP-05 or another
|
// this basically means that NIP-05 or another
|
||||||
// writer was using the database between us
|
// writer was using the database between us
|
||||||
// reading and promoting the connection to a
|
// reading and promoting the connection to a
|
||||||
// write lock.
|
// write lock.
|
||||||
info!("event write failed, DB locked (attempt: {}); sqlite err: {}",
|
info!(
|
||||||
attempts, e.extended_code);
|
"event write failed, DB locked (attempt: {}); sqlite err: {}",
|
||||||
},
|
attempts, e.extended_code
|
||||||
_ => {return wr;},
|
);
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
return wr;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if attempts >= max_write_attempts {
|
if attempts >= max_write_attempts {
|
||||||
return wr;
|
return wr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}).await?;
|
})
|
||||||
|
.await?;
|
||||||
self.metrics
|
self.metrics
|
||||||
.write_events
|
.write_events
|
||||||
.observe(start.elapsed().as_secs_f64());
|
.observe(start.elapsed().as_secs_f64());
|
||||||
@@ -322,9 +336,14 @@ impl NostrRepo for SqliteRepo {
|
|||||||
// thread pool waiting for queries to finish under high load.
|
// thread pool waiting for queries to finish under high load.
|
||||||
// Instead, don't bother spawning threads when they will just
|
// Instead, don't bother spawning threads when they will just
|
||||||
// block on a database connection.
|
// block on a database connection.
|
||||||
let sem = self.reader_threads_ready.clone().acquire_owned().await.unwrap();
|
let sem = self
|
||||||
let self=self.clone();
|
.reader_threads_ready
|
||||||
let metrics=self.metrics.clone();
|
.clone()
|
||||||
|
.acquire_owned()
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
let self = self.clone();
|
||||||
|
let metrics = self.metrics.clone();
|
||||||
task::spawn_blocking(move || {
|
task::spawn_blocking(move || {
|
||||||
{
|
{
|
||||||
// if we are waiting on a checkpoint, stop until it is complete
|
// if we are waiting on a checkpoint, stop until it is complete
|
||||||
@@ -349,7 +368,10 @@ impl NostrRepo for SqliteRepo {
|
|||||||
}
|
}
|
||||||
// check before getting a DB connection if the client still wants the results
|
// check before getting a DB connection if the client still wants the results
|
||||||
if abandon_query_rx.try_recv().is_ok() {
|
if abandon_query_rx.try_recv().is_ok() {
|
||||||
debug!("query cancelled by client (before execution) (cid: {}, sub: {:?})", client_id, sub.id);
|
debug!(
|
||||||
|
"query cancelled by client (before execution) (cid: {}, sub: {:?})",
|
||||||
|
client_id, sub.id
|
||||||
|
);
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -362,7 +384,9 @@ impl NostrRepo for SqliteRepo {
|
|||||||
if let Ok(mut conn) = self.read_pool.get() {
|
if let Ok(mut conn) = self.read_pool.get() {
|
||||||
{
|
{
|
||||||
let pool_state = self.read_pool.state();
|
let pool_state = self.read_pool.state();
|
||||||
metrics.db_connections.set((pool_state.connections - pool_state.idle_connections).into());
|
metrics
|
||||||
|
.db_connections
|
||||||
|
.set((pool_state.connections - pool_state.idle_connections).into());
|
||||||
}
|
}
|
||||||
for filter in sub.filters.iter() {
|
for filter in sub.filters.iter() {
|
||||||
let filter_start = Instant::now();
|
let filter_start = Instant::now();
|
||||||
@@ -379,7 +403,7 @@ impl NostrRepo for SqliteRepo {
|
|||||||
let mut last_successful_send = Instant::now();
|
let mut last_successful_send = Instant::now();
|
||||||
// execute the query.
|
// execute the query.
|
||||||
// make the actual SQL query (with parameters inserted) available
|
// make the actual SQL query (with parameters inserted) available
|
||||||
conn.trace(Some(|x| {trace!("SQL trace: {:?}", x)}));
|
conn.trace(Some(|x| trace!("SQL trace: {:?}", x)));
|
||||||
let mut stmt = conn.prepare_cached(&q)?;
|
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))?;
|
||||||
|
|
||||||
@@ -397,7 +421,10 @@ impl NostrRepo for SqliteRepo {
|
|||||||
if slow_first_event && client_id.starts_with('0') {
|
if slow_first_event && client_id.starts_with('0') {
|
||||||
debug!(
|
debug!(
|
||||||
"filter first result in {:?} (slow): {} (cid: {}, sub: {:?})",
|
"filter first result in {:?} (slow): {} (cid: {}, sub: {:?})",
|
||||||
first_event_elapsed, serde_json::to_string(&filter)?, client_id, sub.id
|
first_event_elapsed,
|
||||||
|
serde_json::to_string(&filter)?,
|
||||||
|
client_id,
|
||||||
|
sub.id
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
first_result = false;
|
first_result = false;
|
||||||
@@ -407,8 +434,14 @@ impl NostrRepo for SqliteRepo {
|
|||||||
{
|
{
|
||||||
if self.checkpoint_in_progress.try_lock().is_err() {
|
if self.checkpoint_in_progress.try_lock().is_err() {
|
||||||
// lock was held, abort this query
|
// lock was held, abort this query
|
||||||
debug!("query aborted due to checkpoint (cid: {}, sub: {:?})", client_id, sub.id);
|
debug!(
|
||||||
metrics.query_aborts.with_label_values(&["checkpoint"]).inc();
|
"query aborted due to checkpoint (cid: {}, sub: {:?})",
|
||||||
|
client_id, sub.id
|
||||||
|
);
|
||||||
|
metrics
|
||||||
|
.query_aborts
|
||||||
|
.with_label_values(&["checkpoint"])
|
||||||
|
.inc();
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -416,7 +449,10 @@ impl NostrRepo for SqliteRepo {
|
|||||||
|
|
||||||
// check if this is still active; every 100 rows
|
// check if this is still active; every 100 rows
|
||||||
if row_count % 100 == 0 && abandon_query_rx.try_recv().is_ok() {
|
if row_count % 100 == 0 && abandon_query_rx.try_recv().is_ok() {
|
||||||
debug!("query cancelled by client (cid: {}, sub: {:?})", client_id, sub.id);
|
debug!(
|
||||||
|
"query cancelled by client (cid: {}, sub: {:?})",
|
||||||
|
client_id, sub.id
|
||||||
|
);
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
row_count += 1;
|
row_count += 1;
|
||||||
@@ -432,19 +468,31 @@ impl NostrRepo for SqliteRepo {
|
|||||||
// the queue has been full for too long, abort
|
// the queue has been full for too long, abort
|
||||||
info!("aborting database query due to slow client (cid: {}, sub: {:?})",
|
info!("aborting database query due to slow client (cid: {}, sub: {:?})",
|
||||||
client_id, sub.id);
|
client_id, sub.id);
|
||||||
metrics.query_aborts.with_label_values(&["slowclient"]).inc();
|
metrics
|
||||||
|
.query_aborts
|
||||||
|
.with_label_values(&["slowclient"])
|
||||||
|
.inc();
|
||||||
let ok: Result<()> = Ok(());
|
let ok: Result<()> = Ok(());
|
||||||
return ok;
|
return ok;
|
||||||
}
|
}
|
||||||
// check if a checkpoint is trying to run, and abort
|
// check if a checkpoint is trying to run, and abort
|
||||||
if self.checkpoint_in_progress.try_lock().is_err() {
|
if self.checkpoint_in_progress.try_lock().is_err() {
|
||||||
// lock was held, abort this query
|
// lock was held, abort this query
|
||||||
debug!("query aborted due to checkpoint (cid: {}, sub: {:?})", client_id, sub.id);
|
debug!(
|
||||||
metrics.query_aborts.with_label_values(&["checkpoint"]).inc();
|
"query aborted due to checkpoint (cid: {}, sub: {:?})",
|
||||||
|
client_id, sub.id
|
||||||
|
);
|
||||||
|
metrics
|
||||||
|
.query_aborts
|
||||||
|
.with_label_values(&["checkpoint"])
|
||||||
|
.inc();
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
// give the queue a chance to clear before trying again
|
// give the queue a chance to clear before trying again
|
||||||
debug!("query thread sleeping due to full query_tx (cid: {}, sub: {:?})", client_id, sub.id);
|
debug!(
|
||||||
|
"query thread sleeping due to full query_tx (cid: {}, sub: {:?})",
|
||||||
|
client_id, sub.id
|
||||||
|
);
|
||||||
thread::sleep(Duration::from_millis(500));
|
thread::sleep(Duration::from_millis(500));
|
||||||
}
|
}
|
||||||
// TODO: we could use try_send, but we'd have to juggle
|
// TODO: we could use try_send, but we'd have to juggle
|
||||||
@@ -465,10 +513,12 @@ impl NostrRepo for SqliteRepo {
|
|||||||
if filter_start.elapsed() > slow_cutoff && client_id.starts_with('0') {
|
if filter_start.elapsed() > slow_cutoff && client_id.starts_with('0') {
|
||||||
debug!(
|
debug!(
|
||||||
"query filter req (slow): {} (cid: {}, sub: {:?}, filter: {})",
|
"query filter req (slow): {} (cid: {}, sub: {:?}, filter: {})",
|
||||||
serde_json::to_string(&filter)?, client_id, sub.id, filter_count
|
serde_json::to_string(&filter)?,
|
||||||
|
client_id,
|
||||||
|
sub.id,
|
||||||
|
filter_count
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
warn!("Could not get a database connection for querying");
|
warn!("Could not get a database connection for querying");
|
||||||
@@ -504,7 +554,8 @@ impl NostrRepo for SqliteRepo {
|
|||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
conn.execute_batch("PRAGMA optimize;").ok();
|
conn.execute_batch("PRAGMA optimize;").ok();
|
||||||
info!("optimize ran in {:?}", start.elapsed());
|
info!("optimize ran in {:?}", start.elapsed());
|
||||||
}).await?;
|
})
|
||||||
|
.await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -557,8 +608,7 @@ impl NostrRepo for SqliteRepo {
|
|||||||
let ok: Result<()> = Ok(());
|
let ok: Result<()> = Ok(());
|
||||||
ok
|
ok
|
||||||
})
|
})
|
||||||
.await?
|
.await?
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Update verification record as failed
|
/// Update verification record as failed
|
||||||
@@ -596,7 +646,7 @@ impl NostrRepo for SqliteRepo {
|
|||||||
let ok: Result<()> = Ok(());
|
let ok: Result<()> = Ok(());
|
||||||
ok
|
ok
|
||||||
})
|
})
|
||||||
.await?
|
.await?
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the latest verification record for a given pubkey.
|
/// Get the latest verification record for a given pubkey.
|
||||||
@@ -674,6 +724,209 @@ impl NostrRepo for SqliteRepo {
|
|||||||
Ok(vr)
|
Ok(vr)
|
||||||
}).await?
|
}).await?
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Create account
|
||||||
|
async fn create_account(&self, pub_key: &Keys) -> Result<bool> {
|
||||||
|
let pub_key = pub_key.public_key().to_string();
|
||||||
|
|
||||||
|
let mut conn = self.write_pool.get()?;
|
||||||
|
let ins_count = tokio::task::spawn_blocking(move || {
|
||||||
|
let tx = conn.transaction()?;
|
||||||
|
let ins_count: u64;
|
||||||
|
{
|
||||||
|
// Ignore if user is already in db
|
||||||
|
let query = "INSERT OR IGNORE INTO account (pubkey, is_admitted, balance) VALUES (?1, ?2, ?3);";
|
||||||
|
let mut stmt = tx.prepare(query)?;
|
||||||
|
ins_count = stmt.execute(params![&pub_key, false, 0])? as u64;
|
||||||
|
}
|
||||||
|
tx.commit()?;
|
||||||
|
let ok: Result<u64> = Ok(ins_count);
|
||||||
|
ok
|
||||||
|
}).await??;
|
||||||
|
|
||||||
|
if ins_count != 1 {
|
||||||
|
return Ok(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(true)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Admit account
|
||||||
|
async fn admit_account(&self, pub_key: &Keys, admission_cost: u64) -> Result<()> {
|
||||||
|
let pub_key = pub_key.public_key().to_string();
|
||||||
|
let mut conn = self.write_pool.get()?;
|
||||||
|
let pub_key = pub_key.to_owned();
|
||||||
|
tokio::task::spawn_blocking(move || {
|
||||||
|
let tx = conn.transaction()?;
|
||||||
|
{
|
||||||
|
let query = "UPDATE account SET is_admitted = TRUE, tos_accepted_at = strftime('%s','now'), balance = balance - ?1 WHERE pubkey=?2;";
|
||||||
|
let mut stmt = tx.prepare(query)?;
|
||||||
|
stmt.execute(params![admission_cost, pub_key])?;
|
||||||
|
}
|
||||||
|
tx.commit()?;
|
||||||
|
let ok: Result<()> = Ok(());
|
||||||
|
ok
|
||||||
|
})
|
||||||
|
.await?
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Gets if the account is admitted and balance
|
||||||
|
async fn get_account_balance(&self, pub_key: &Keys) -> Result<(bool, u64)> {
|
||||||
|
let pub_key = pub_key.public_key().to_string();
|
||||||
|
let mut conn = self.write_pool.get()?;
|
||||||
|
let pub_key = pub_key.to_owned();
|
||||||
|
tokio::task::spawn_blocking(move || {
|
||||||
|
let tx = conn.transaction()?;
|
||||||
|
let query = "SELECT is_admitted, balance FROM account WHERE pubkey = ?1;";
|
||||||
|
let mut stmt = tx.prepare_cached(query)?;
|
||||||
|
let fields = stmt.query_row(params![pub_key], |r| {
|
||||||
|
let is_admitted: bool = r.get(0)?;
|
||||||
|
let balance: u64 = r.get(1)?;
|
||||||
|
// create a tuple since we can't throw non-rusqlite errors in this closure
|
||||||
|
Ok((is_admitted, balance))
|
||||||
|
})?;
|
||||||
|
Ok(fields)
|
||||||
|
})
|
||||||
|
.await?
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Update account balance
|
||||||
|
async fn update_account_balance(
|
||||||
|
&self,
|
||||||
|
pub_key: &Keys,
|
||||||
|
positive: bool,
|
||||||
|
new_balance: u64,
|
||||||
|
) -> Result<()> {
|
||||||
|
let pub_key = pub_key.public_key().to_string();
|
||||||
|
|
||||||
|
let mut conn = self.write_pool.get()?;
|
||||||
|
tokio::task::spawn_blocking(move || {
|
||||||
|
let tx = conn.transaction()?;
|
||||||
|
{
|
||||||
|
let query = if positive {
|
||||||
|
"UPDATE account SET balance=balance + ?1 WHERE pubkey=?2"
|
||||||
|
} else {
|
||||||
|
"UPDATE account SET balance=balance - ?1 WHERE pubkey=?2"
|
||||||
|
};
|
||||||
|
let mut stmt = tx.prepare(query)?;
|
||||||
|
stmt.execute(params![new_balance, pub_key])?;
|
||||||
|
}
|
||||||
|
tx.commit()?;
|
||||||
|
let ok: Result<()> = Ok(());
|
||||||
|
ok
|
||||||
|
})
|
||||||
|
.await?
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create invoice record
|
||||||
|
async fn create_invoice_record(&self, pub_key: &Keys, invoice_info: InvoiceInfo) -> Result<()> {
|
||||||
|
let pub_key = pub_key.public_key().to_string();
|
||||||
|
let pub_key = pub_key.to_owned();
|
||||||
|
let mut conn = self.write_pool.get()?;
|
||||||
|
tokio::task::spawn_blocking(move || {
|
||||||
|
let tx = conn.transaction()?;
|
||||||
|
{
|
||||||
|
let query = "INSERT INTO invoice (pubkey, payment_hash, amount, status, description, created_at, invoice) VALUES (?1, ?2, ?3, ?4, ?5, strftime('%s','now'), ?6);";
|
||||||
|
let mut stmt = tx.prepare(query)?;
|
||||||
|
stmt.execute(params![&pub_key, invoice_info.payment_hash, invoice_info.amount, invoice_info.status.to_string(), invoice_info.memo, invoice_info.bolt11])?;
|
||||||
|
}
|
||||||
|
tx.commit()?;
|
||||||
|
let ok: Result<()> = Ok(());
|
||||||
|
ok
|
||||||
|
}).await??;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Update invoice record
|
||||||
|
async fn update_invoice(&self, payment_hash: &str, status: InvoiceStatus) -> Result<String> {
|
||||||
|
let mut conn = self.write_pool.get()?;
|
||||||
|
let payment_hash = payment_hash.to_owned();
|
||||||
|
|
||||||
|
tokio::task::spawn_blocking(move || {
|
||||||
|
let tx = conn.transaction()?;
|
||||||
|
let pubkey: String;
|
||||||
|
{
|
||||||
|
|
||||||
|
// Get required invoice info for given payment hash
|
||||||
|
let query = "SELECT pubkey, status, amount FROM invoice WHERE payment_hash=?1;";
|
||||||
|
let mut stmt = tx.prepare(query)?;
|
||||||
|
let (pub_key, prev_status, amount) = stmt.query_row(params![payment_hash], |r| {
|
||||||
|
let pub_key: String = r.get(0)?;
|
||||||
|
let status: String = r.get(1)?;
|
||||||
|
let amount: u64 = r.get(2)?;
|
||||||
|
|
||||||
|
|
||||||
|
Ok((pub_key, status, amount))
|
||||||
|
|
||||||
|
})?;
|
||||||
|
|
||||||
|
// If the invoice is paid update the confirmed_at timestamp
|
||||||
|
let query = if status.eq(&InvoiceStatus::Paid) {
|
||||||
|
"UPDATE invoice SET status=?1, confirmed_at = strftime('%s', 'now') WHERE payment_hash=?2;"
|
||||||
|
} else {
|
||||||
|
"UPDATE invoice SET status=?1 WHERE payment_hash=?2;"
|
||||||
|
};
|
||||||
|
let mut stmt = tx.prepare(query)?;
|
||||||
|
stmt.execute(params![status.to_string(), payment_hash])?;
|
||||||
|
|
||||||
|
// Increase account balance by given invoice amount
|
||||||
|
if prev_status == "Unpaid" && status.eq(&InvoiceStatus::Paid) {
|
||||||
|
let query =
|
||||||
|
"UPDATE account SET balance = balance + ?1 WHERE pubkey = ?2;";
|
||||||
|
let mut stmt = tx.prepare(query)?;
|
||||||
|
stmt.execute(params![amount, pub_key])?;
|
||||||
|
}
|
||||||
|
|
||||||
|
pubkey = pub_key;
|
||||||
|
}
|
||||||
|
|
||||||
|
tx.commit()?;
|
||||||
|
let ok: Result<String> = Ok(pubkey);
|
||||||
|
ok
|
||||||
|
})
|
||||||
|
.await?
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get the most recent invoice for a given pubkey
|
||||||
|
/// invoice must be unpaid and not expired
|
||||||
|
async fn get_unpaid_invoice(&self, pubkey: &Keys) -> Result<Option<InvoiceInfo>> {
|
||||||
|
let mut conn = self.write_pool.get()?;
|
||||||
|
|
||||||
|
let pubkey = pubkey.to_owned();
|
||||||
|
let pubkey_str = pubkey.clone().public_key().to_string();
|
||||||
|
let (payment_hash, invoice, amount, description) = tokio::task::spawn_blocking(move || {
|
||||||
|
let tx = conn.transaction()?;
|
||||||
|
|
||||||
|
let query = r#"
|
||||||
|
SELECT amount, payment_hash, description, invoice
|
||||||
|
FROM invoice
|
||||||
|
WHERE pubkey = ?1 AND status = 'Unpaid'
|
||||||
|
ORDER BY created_at DESC
|
||||||
|
LIMIT 1;
|
||||||
|
"#;
|
||||||
|
let mut stmt = tx.prepare(query).unwrap();
|
||||||
|
stmt.query_row(params![&pubkey_str], |r| {
|
||||||
|
let amount: u64 = r.get(0)?;
|
||||||
|
let payment_hash: String = r.get(1)?;
|
||||||
|
let description: String = r.get(2)?;
|
||||||
|
let invoice: String = r.get(3)?;
|
||||||
|
|
||||||
|
Ok((payment_hash, invoice, amount, description))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.await??;
|
||||||
|
|
||||||
|
Ok(Some(InvoiceInfo {
|
||||||
|
pubkey: pubkey.public_key().to_string(),
|
||||||
|
payment_hash,
|
||||||
|
bolt11: invoice,
|
||||||
|
amount,
|
||||||
|
status: InvoiceStatus::Unpaid,
|
||||||
|
memo: description,
|
||||||
|
confirmed_at: None,
|
||||||
|
}))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Decide if there is an index that should be used explicitly
|
/// Decide if there is an index that should be used explicitly
|
||||||
@@ -684,14 +937,15 @@ fn override_index(f: &ReqFilter) -> Option<String> {
|
|||||||
// queries for multiple kinds default to kind_index, which is
|
// queries for multiple kinds default to kind_index, which is
|
||||||
// significantly slower than kind_created_at_index.
|
// significantly slower than kind_created_at_index.
|
||||||
if let Some(ks) = &f.kinds {
|
if let Some(ks) = &f.kinds {
|
||||||
if f.ids.is_none() &&
|
if f.ids.is_none()
|
||||||
ks.len() > 1 &&
|
&& ks.len() > 1
|
||||||
f.since.is_none() &&
|
&& f.since.is_none()
|
||||||
f.until.is_none() &&
|
&& f.until.is_none()
|
||||||
f.tags.is_none() &&
|
&& f.tags.is_none()
|
||||||
f.authors.is_none() {
|
&& f.authors.is_none()
|
||||||
return Some("kind_created_at_index".into());
|
{
|
||||||
}
|
return Some("kind_created_at_index".into());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// if there is an author, it is much better to force the authors index.
|
// if there is an author, it is much better to force the authors index.
|
||||||
if f.authors.is_some() {
|
if f.authors.is_some() {
|
||||||
@@ -724,9 +978,11 @@ fn query_from_filter(f: &ReqFilter) -> (String, Vec<Box<dyn ToSql>>, Option<Stri
|
|||||||
return (empty_query, empty_params, None);
|
return (empty_query, empty_params, None);
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if the index needs to be overriden
|
// check if the index needs to be overridden
|
||||||
let idx_name = override_index(f);
|
let idx_name = override_index(f);
|
||||||
let idx_stmt = idx_name.as_ref().map_or_else(|| "".to_owned(), |i| format!("INDEXED BY {i}"));
|
let idx_stmt = idx_name
|
||||||
|
.as_ref()
|
||||||
|
.map_or_else(|| "".to_owned(), |i| format!("INDEXED BY {i}"));
|
||||||
let mut query = format!("SELECT e.content FROM event e {idx_stmt}");
|
let mut query = format!("SELECT e.content FROM event e {idx_stmt}");
|
||||||
// query parameters for SQLite
|
// query parameters for SQLite
|
||||||
let mut params: Vec<Box<dyn ToSql>> = vec![];
|
let mut params: Vec<Box<dyn ToSql>> = vec![];
|
||||||
@@ -744,9 +1000,7 @@ fn query_from_filter(f: &ReqFilter) -> (String, Vec<Box<dyn ToSql>>, Option<Stri
|
|||||||
params.push(Box::new(ex));
|
params.push(Box::new(ex));
|
||||||
}
|
}
|
||||||
Some(HexSearch::Range(lower, upper)) => {
|
Some(HexSearch::Range(lower, upper)) => {
|
||||||
auth_searches.push(
|
auth_searches.push("(author>? AND author<?)".to_owned());
|
||||||
"(author>? AND author<?)".to_owned(),
|
|
||||||
);
|
|
||||||
params.push(Box::new(lower));
|
params.push(Box::new(lower));
|
||||||
params.push(Box::new(upper));
|
params.push(Box::new(upper));
|
||||||
}
|
}
|
||||||
@@ -755,7 +1009,7 @@ fn query_from_filter(f: &ReqFilter) -> (String, Vec<Box<dyn ToSql>>, Option<Stri
|
|||||||
params.push(Box::new(lower));
|
params.push(Box::new(lower));
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
info!("Could not parse hex range from author {:?}", auth);
|
trace!("Could not parse hex range from author {:?}", auth);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -818,25 +1072,24 @@ fn query_from_filter(f: &ReqFilter) -> (String, Vec<Box<dyn ToSql>>, Option<Stri
|
|||||||
// find evidence of the target tag name/value existing for this event.
|
// find evidence of the target tag name/value existing for this event.
|
||||||
// Query for Kind/Since/Until additionally, to reduce the number of tags that come back.
|
// Query for Kind/Since/Until additionally, to reduce the number of tags that come back.
|
||||||
let kind_clause;
|
let kind_clause;
|
||||||
let since_clause;
|
|
||||||
let until_clause;
|
|
||||||
if let Some(ks) = &f.kinds {
|
if let Some(ks) = &f.kinds {
|
||||||
// kind is number, no escaping needed
|
// kind is number, no escaping needed
|
||||||
let str_kinds: Vec<String> = ks.iter().map(std::string::ToString::to_string).collect();
|
let str_kinds: Vec<String> =
|
||||||
|
ks.iter().map(std::string::ToString::to_string).collect();
|
||||||
kind_clause = format!("AND kind IN ({})", str_kinds.join(", "));
|
kind_clause = format!("AND kind IN ({})", str_kinds.join(", "));
|
||||||
} else {
|
} else {
|
||||||
kind_clause = format!("");
|
kind_clause = String::new();
|
||||||
};
|
};
|
||||||
if f.since.is_some() {
|
let since_clause = if f.since.is_some() {
|
||||||
since_clause = format!("AND created_at > {}", f.since.unwrap());
|
format!("AND created_at >= {}", f.since.unwrap())
|
||||||
} else {
|
} else {
|
||||||
since_clause = format!("");
|
String::new()
|
||||||
};
|
};
|
||||||
// Query for timestamp
|
// Query for timestamp
|
||||||
if f.until.is_some() {
|
let until_clause = if f.until.is_some() {
|
||||||
until_clause = format!("AND created_at < {}", f.until.unwrap());
|
format!("AND created_at <= {}", f.until.unwrap())
|
||||||
} else {
|
} else {
|
||||||
until_clause = format!("");
|
String::new()
|
||||||
};
|
};
|
||||||
|
|
||||||
let tag_clause = format!(
|
let tag_clause = format!(
|
||||||
@@ -852,12 +1105,12 @@ fn query_from_filter(f: &ReqFilter) -> (String, Vec<Box<dyn ToSql>>, Option<Stri
|
|||||||
}
|
}
|
||||||
// Query for timestamp
|
// Query for timestamp
|
||||||
if f.since.is_some() {
|
if f.since.is_some() {
|
||||||
let created_clause = format!("created_at > {}", f.since.unwrap());
|
let created_clause = format!("created_at >= {}", f.since.unwrap());
|
||||||
filter_components.push(created_clause);
|
filter_components.push(created_clause);
|
||||||
}
|
}
|
||||||
// Query for timestamp
|
// Query for timestamp
|
||||||
if f.until.is_some() {
|
if f.until.is_some() {
|
||||||
let until_clause = format!("created_at < {}", f.until.unwrap());
|
let until_clause = format!("created_at <= {}", f.until.unwrap());
|
||||||
filter_components.push(until_clause);
|
filter_components.push(until_clause);
|
||||||
}
|
}
|
||||||
// never display hidden events
|
// never display hidden events
|
||||||
@@ -903,7 +1156,7 @@ fn _query_from_sub(sub: &Subscription) -> (String, Vec<Box<dyn ToSql>>, Vec<Stri
|
|||||||
.map(|s| format!("SELECT distinct 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 ");
|
||||||
(query, params,indexes)
|
(query, params, indexes)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Build a database connection pool.
|
/// Build a database connection pool.
|
||||||
@@ -944,9 +1197,15 @@ 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)
|
||||||
|
.idle_timeout(Some(Duration::from_secs(10)))
|
||||||
.max_lifetime(Some(Duration::from_secs(30)))
|
.max_lifetime(Some(Duration::from_secs(30)))
|
||||||
.build(manager)
|
.build(manager)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
// retrieve a connection to ensure the startup statements run immediately
|
||||||
|
{
|
||||||
|
let _ = pool.get();
|
||||||
|
}
|
||||||
|
|
||||||
info!(
|
info!(
|
||||||
"Built a connection pool {:?} (min={}, max={})",
|
"Built a connection pool {:?} (min={}, max={})",
|
||||||
name, min_size, max_size
|
name, min_size, max_size
|
||||||
@@ -955,7 +1214,11 @@ pub fn build_pool(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Cleanup expired events on a regular basis
|
/// Cleanup expired events on a regular basis
|
||||||
async fn cleanup_expired(pool: SqlitePool, frequency: Duration, write_in_progress: Arc<Mutex<u64>>) -> Result<()> {
|
async fn cleanup_expired(
|
||||||
|
pool: SqlitePool,
|
||||||
|
frequency: Duration,
|
||||||
|
write_in_progress: Arc<Mutex<u64>>,
|
||||||
|
) -> Result<()> {
|
||||||
tokio::task::spawn(async move {
|
tokio::task::spawn(async move {
|
||||||
loop {
|
loop {
|
||||||
tokio::select! {
|
tokio::select! {
|
||||||
@@ -1003,13 +1266,18 @@ pub fn delete_expired(conn: &mut PooledConnection) -> Result<usize> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Perform database WAL checkpoint on a regular basis
|
/// Perform database WAL checkpoint on a regular basis
|
||||||
pub async fn db_checkpoint_task(pool: SqlitePool, frequency: Duration, write_in_progress: Arc<Mutex<u64>>, checkpoint_in_progress: Arc<Mutex<u64>>) -> Result<()> {
|
pub async fn db_checkpoint_task(
|
||||||
|
pool: SqlitePool,
|
||||||
|
frequency: Duration,
|
||||||
|
write_in_progress: Arc<Mutex<u64>>,
|
||||||
|
checkpoint_in_progress: Arc<Mutex<u64>>,
|
||||||
|
) -> Result<()> {
|
||||||
// TODO; use acquire_many on the reader semaphore to stop them from interrupting this.
|
// TODO; use acquire_many on the reader semaphore to stop them from interrupting this.
|
||||||
tokio::task::spawn(async move {
|
tokio::task::spawn(async move {
|
||||||
// WAL size in pages.
|
// WAL size in pages.
|
||||||
let mut current_wal_size = 0;
|
let mut current_wal_size = 0;
|
||||||
// WAL threshold for more aggressive checkpointing (10,000 pages, or about 40MB)
|
// WAL threshold for more aggressive checkpointing (10,000 pages, or about 40MB)
|
||||||
let wal_threshold = 1000*10;
|
let wal_threshold = 1000 * 10;
|
||||||
// default threshold for the busy timer
|
// default threshold for the busy timer
|
||||||
let busy_wait_default = Duration::from_secs(1);
|
let busy_wait_default = Duration::from_secs(1);
|
||||||
// if the WAL file is getting too big, switch to this
|
// if the WAL file is getting too big, switch to this
|
||||||
@@ -1079,7 +1347,6 @@ pub fn checkpoint_db(conn: &mut PooledConnection) -> Result<usize> {
|
|||||||
Ok(wal_size as usize)
|
Ok(wal_size as usize)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Produce a arbitrary list of '?' parameters.
|
/// Produce a arbitrary list of '?' parameters.
|
||||||
fn repeat_vars(count: usize) -> String {
|
fn repeat_vars(count: usize) -> String {
|
||||||
if count == 0 {
|
if count == 0 {
|
||||||
@@ -1113,7 +1380,6 @@ fn log_pool_stats(name: &str, pool: &SqlitePool) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Check if the pool is fully utilized
|
/// Check if the pool is fully utilized
|
||||||
fn _pool_at_capacity(pool: &SqlitePool) -> bool {
|
fn _pool_at_capacity(pool: &SqlitePool) -> bool {
|
||||||
let state: r2d2::State = pool.state();
|
let state: r2d2::State = pool.state();
|
||||||
|
|||||||
+102
-16
@@ -4,13 +4,13 @@ use crate::error::Result;
|
|||||||
use crate::event::{single_char_tagname, Event};
|
use crate::event::{single_char_tagname, Event};
|
||||||
use crate::utils::is_lower_hex;
|
use crate::utils::is_lower_hex;
|
||||||
use const_format::formatcp;
|
use const_format::formatcp;
|
||||||
|
use indicatif::{ProgressBar, ProgressStyle};
|
||||||
use rusqlite::limits::Limit;
|
use rusqlite::limits::Limit;
|
||||||
use rusqlite::params;
|
use rusqlite::params;
|
||||||
use rusqlite::Connection;
|
use rusqlite::Connection;
|
||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
use tracing::{debug, error, info};
|
use tracing::{debug, error, info};
|
||||||
use indicatif::{ProgressBar, ProgressStyle};
|
|
||||||
|
|
||||||
/// Startup DB Pragmas
|
/// Startup DB Pragmas
|
||||||
pub const STARTUP_SQL: &str = r##"
|
pub const STARTUP_SQL: &str = r##"
|
||||||
@@ -19,11 +19,11 @@ PRAGMA foreign_keys = ON;
|
|||||||
PRAGMA journal_size_limit = 32768;
|
PRAGMA journal_size_limit = 32768;
|
||||||
PRAGMA temp_store = 2; -- use memory, not temp files
|
PRAGMA temp_store = 2; -- use memory, not temp files
|
||||||
PRAGMA main.cache_size = 20000; -- 80MB max cache size per conn
|
PRAGMA main.cache_size = 20000; -- 80MB max cache size per conn
|
||||||
pragma mmap_size = 17179869184; -- cap mmap at 16GB
|
pragma mmap_size = 0; -- disable mmap (default)
|
||||||
"##;
|
"##;
|
||||||
|
|
||||||
/// Latest database version
|
/// Latest database version
|
||||||
pub const DB_VERSION: usize = 17;
|
pub const DB_VERSION: usize = 18;
|
||||||
|
|
||||||
/// Schema definition
|
/// Schema definition
|
||||||
const INIT_SQL: &str = formatcp!(
|
const INIT_SQL: &str = formatcp!(
|
||||||
@@ -96,6 +96,35 @@ FOREIGN KEY(metadata_event) REFERENCES event(id) ON UPDATE CASCADE ON DELETE CAS
|
|||||||
);
|
);
|
||||||
CREATE INDEX IF NOT EXISTS user_verification_name_index ON user_verification(name);
|
CREATE INDEX IF NOT EXISTS user_verification_name_index ON user_verification(name);
|
||||||
CREATE INDEX IF NOT EXISTS user_verification_event_index ON user_verification(metadata_event);
|
CREATE INDEX IF NOT EXISTS user_verification_event_index ON user_verification(metadata_event);
|
||||||
|
|
||||||
|
-- Create account table
|
||||||
|
CREATE TABLE IF NOT EXISTS account (
|
||||||
|
pubkey TEXT PRIMARY KEY,
|
||||||
|
is_admitted INTEGER NOT NULL DEFAULT 0,
|
||||||
|
balance INTEGER NOT NULL DEFAULT 0,
|
||||||
|
tos_accepted_at INTEGER
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Create account index
|
||||||
|
CREATE INDEX IF NOT EXISTS user_pubkey_index ON account(pubkey);
|
||||||
|
|
||||||
|
-- Invoice table
|
||||||
|
CREATE TABLE IF NOT EXISTS invoice (
|
||||||
|
payment_hash TEXT PRIMARY KEY,
|
||||||
|
pubkey TEXT NOT NULL,
|
||||||
|
invoice TEXT NOT NULL,
|
||||||
|
amount INTEGER NOT NULL,
|
||||||
|
status TEXT CHECK ( status IN ('Paid', 'Unpaid', 'Expired' ) ) NOT NUll DEFAULT 'Unpaid',
|
||||||
|
description TEXT,
|
||||||
|
created_at INTEGER NOT NULL,
|
||||||
|
confirmed_at INTEGER,
|
||||||
|
CONSTRAINT invoice_pubkey_fkey FOREIGN KEY (pubkey) REFERENCES account (pubkey) ON DELETE CASCADE
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Create invoice index
|
||||||
|
CREATE INDEX IF NOT EXISTS invoice_pubkey_index ON invoice(pubkey);
|
||||||
|
|
||||||
|
|
||||||
"##,
|
"##,
|
||||||
DB_VERSION
|
DB_VERSION
|
||||||
);
|
);
|
||||||
@@ -213,6 +242,9 @@ pub fn upgrade_db(conn: &mut PooledConnection) -> Result<usize> {
|
|||||||
if curr_version == 16 {
|
if curr_version == 16 {
|
||||||
curr_version = mig_16_to_17(conn)?;
|
curr_version = mig_16_to_17(conn)?;
|
||||||
}
|
}
|
||||||
|
if curr_version == 17 {
|
||||||
|
curr_version = mig_17_to_18(conn)?;
|
||||||
|
}
|
||||||
|
|
||||||
if curr_version == DB_VERSION {
|
if curr_version == DB_VERSION {
|
||||||
info!(
|
info!(
|
||||||
@@ -253,8 +285,8 @@ pub fn rebuild_tags(conn: &mut PooledConnection) -> Result<()> {
|
|||||||
let mut stmt = tx.prepare("select id, content from event order by id;")?;
|
let mut stmt = tx.prepare("select id, content from event order by id;")?;
|
||||||
let mut tag_rows = stmt.query([])?;
|
let mut tag_rows = stmt.query([])?;
|
||||||
while let Some(row) = tag_rows.next()? {
|
while let Some(row) = tag_rows.next()? {
|
||||||
if (events_processed as f32)/(count as f32) > percent_done {
|
if (events_processed as f32) / (count as f32) > percent_done {
|
||||||
info!("Tag update {}% complete...", (100.0*percent_done).round());
|
info!("Tag update {}% complete...", (100.0 * percent_done).round());
|
||||||
percent_done += update_each_percent;
|
percent_done += update_each_percent;
|
||||||
}
|
}
|
||||||
// we want to capture the event_id that had the tag, the tag name, and the tag hex value.
|
// we want to capture the event_id that had the tag, the tag name, and the tag hex value.
|
||||||
@@ -293,8 +325,6 @@ pub fn rebuild_tags(conn: &mut PooledConnection) -> Result<()> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//// Migration Scripts
|
//// Migration Scripts
|
||||||
|
|
||||||
fn mig_1_to_2(conn: &mut PooledConnection) -> Result<usize> {
|
fn mig_1_to_2(conn: &mut PooledConnection) -> Result<usize> {
|
||||||
@@ -586,11 +616,17 @@ fn mig_11_to_12(conn: &mut PooledConnection) -> Result<usize> {
|
|||||||
tx.execute("PRAGMA user_version = 12;", [])?;
|
tx.execute("PRAGMA user_version = 12;", [])?;
|
||||||
}
|
}
|
||||||
tx.commit()?;
|
tx.commit()?;
|
||||||
info!("database schema upgraded v11 -> v12 in {:?}", start.elapsed());
|
info!(
|
||||||
|
"database schema upgraded v11 -> v12 in {:?}",
|
||||||
|
start.elapsed()
|
||||||
|
);
|
||||||
// vacuum after large table modification
|
// vacuum after large table modification
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
conn.execute("VACUUM;", [])?;
|
conn.execute("VACUUM;", [])?;
|
||||||
info!("vacuumed DB after hidden event cleanup in {:?}", start.elapsed());
|
info!(
|
||||||
|
"vacuumed DB after hidden event cleanup in {:?}",
|
||||||
|
start.elapsed()
|
||||||
|
);
|
||||||
Ok(12)
|
Ok(12)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -656,7 +692,7 @@ PRAGMA user_version = 15;
|
|||||||
match conn.execute_batch(clear_hidden_sql) {
|
match conn.execute_batch(clear_hidden_sql) {
|
||||||
Ok(()) => {
|
Ok(()) => {
|
||||||
info!("all hidden events removed");
|
info!("all hidden events removed");
|
||||||
},
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
error!("delete failed: {}", err);
|
error!("delete failed: {}", err);
|
||||||
panic!("could not remove hidden events");
|
panic!("could not remove hidden events");
|
||||||
@@ -688,22 +724,22 @@ CREATE INDEX IF NOT EXISTS tag_covering_index ON tag(name,kind,value,created_at,
|
|||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
let tx = conn.transaction()?;
|
let tx = conn.transaction()?;
|
||||||
|
|
||||||
let bar = ProgressBar::new(count.try_into().unwrap())
|
let bar = ProgressBar::new(count.try_into().unwrap()).with_message("rebuilding tags table");
|
||||||
.with_message("rebuilding tags table");
|
|
||||||
bar.set_style(
|
bar.set_style(
|
||||||
ProgressStyle::with_template(
|
ProgressStyle::with_template(
|
||||||
"[{elapsed_precise}] {bar:40.white/blue} {pos:>7}/{len:7} [{percent}%] {msg}",
|
"[{elapsed_precise}] {bar:40.white/blue} {pos:>7}/{len:7} [{percent}%] {msg}",
|
||||||
)
|
)
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
);
|
);
|
||||||
{
|
{
|
||||||
tx.execute_batch(upgrade_sql)?;
|
tx.execute_batch(upgrade_sql)?;
|
||||||
let mut stmt = tx.prepare("select id, kind, created_at, content from event order by id;")?;
|
let mut stmt =
|
||||||
|
tx.prepare("select id, kind, created_at, content from event order by id;")?;
|
||||||
let mut tag_rows = stmt.query([])?;
|
let mut tag_rows = stmt.query([])?;
|
||||||
let mut count = 0;
|
let mut count = 0;
|
||||||
while let Some(row) = tag_rows.next()? {
|
while let Some(row) = tag_rows.next()? {
|
||||||
count += 1;
|
count += 1;
|
||||||
if count%10==0 {
|
if count % 10 == 0 {
|
||||||
bar.inc(10);
|
bar.inc(10);
|
||||||
}
|
}
|
||||||
let event_id: u64 = row.get(0)?;
|
let event_id: u64 = row.get(0)?;
|
||||||
@@ -731,7 +767,10 @@ CREATE INDEX IF NOT EXISTS tag_covering_index ON tag(name,kind,value,created_at,
|
|||||||
}
|
}
|
||||||
bar.finish();
|
bar.finish();
|
||||||
tx.commit()?;
|
tx.commit()?;
|
||||||
info!("database schema upgraded v15 -> v16 in {:?}", start.elapsed());
|
info!(
|
||||||
|
"database schema upgraded v15 -> v16 in {:?}",
|
||||||
|
start.elapsed()
|
||||||
|
);
|
||||||
Ok(16)
|
Ok(16)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -753,3 +792,50 @@ PRAGMA user_version = 17;
|
|||||||
}
|
}
|
||||||
Ok(17)
|
Ok(17)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn mig_17_to_18(conn: &mut PooledConnection) -> Result<usize> {
|
||||||
|
info!("database schema needs update from 17->18");
|
||||||
|
let upgrade_sql = r##"
|
||||||
|
-- Create invoices table
|
||||||
|
CREATE TABLE IF NOT EXISTS invoice (
|
||||||
|
payment_hash TEXT PRIMARY KEY,
|
||||||
|
pubkey TEXT NOT NULL,
|
||||||
|
invoice TEXT NOT NULL,
|
||||||
|
amount INTEGER NOT NULL,
|
||||||
|
status TEXT CHECK ( status IN ('Paid', 'Unpaid', 'Expired' ) ) NOT NUll DEFAULT 'Unpaid',
|
||||||
|
description TEXT,
|
||||||
|
created_at INTEGER NOT NULL,
|
||||||
|
confirmed_at INTEGER,
|
||||||
|
CONSTRAINT invoice_pubkey_fkey FOREIGN KEY (pubkey) REFERENCES account (pubkey) ON DELETE CASCADE
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Create invoice index
|
||||||
|
CREATE INDEX IF NOT EXISTS invoice_pubkey_index ON invoice(pubkey);
|
||||||
|
|
||||||
|
-- Create account table
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS account (
|
||||||
|
pubkey TEXT PRIMARY KEY,
|
||||||
|
is_admitted INTEGER NOT NULL DEFAULT 0,
|
||||||
|
balance INTEGER NOT NULL DEFAULT 0,
|
||||||
|
tos_accepted_at INTEGER
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Create account index
|
||||||
|
CREATE INDEX IF NOT EXISTS account_pubkey_index ON account(pubkey);
|
||||||
|
|
||||||
|
|
||||||
|
pragma optimize;
|
||||||
|
PRAGMA user_version = 18;
|
||||||
|
"##;
|
||||||
|
match conn.execute_batch(upgrade_sql) {
|
||||||
|
Ok(()) => {
|
||||||
|
info!("database schema upgraded v17 -> v18");
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
error!("update failed: {}", err);
|
||||||
|
panic!("database could not be upgraded");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(18)
|
||||||
|
}
|
||||||
|
|||||||
+600
-125
File diff suppressed because it is too large
Load Diff
+27
-20
@@ -45,7 +45,8 @@ pub struct ReqFilter {
|
|||||||
|
|
||||||
impl Serialize for ReqFilter {
|
impl Serialize for ReqFilter {
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where S:Serializer,
|
where
|
||||||
|
S: Serializer,
|
||||||
{
|
{
|
||||||
let mut map = serializer.serialize_map(None)?;
|
let mut map = serializer.serialize_map(None)?;
|
||||||
if let Some(ids) = &self.ids {
|
if let Some(ids) = &self.ids {
|
||||||
@@ -68,8 +69,8 @@ impl Serialize for ReqFilter {
|
|||||||
}
|
}
|
||||||
// serialize tags
|
// serialize tags
|
||||||
if let Some(tags) = &self.tags {
|
if let Some(tags) = &self.tags {
|
||||||
for (k,v) in tags {
|
for (k, v) in tags {
|
||||||
let vals:Vec<&String> = v.iter().collect();
|
let vals: Vec<&String> = v.iter().collect();
|
||||||
map.serialize_entry(&format!("#{k}"), &vals)?;
|
map.serialize_entry(&format!("#{k}"), &vals)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -105,15 +106,16 @@ impl<'de> Deserialize<'de> for ReqFilter {
|
|||||||
for (key, val) in filter {
|
for (key, val) in filter {
|
||||||
// ids
|
// ids
|
||||||
if key == "ids" {
|
if key == "ids" {
|
||||||
let raw_ids: Option<Vec<String>>= Deserialize::deserialize(val).ok();
|
let raw_ids: Option<Vec<String>> = Deserialize::deserialize(val).ok();
|
||||||
if let Some(a) = raw_ids.as_ref() {
|
if let Some(a) = raw_ids.as_ref() {
|
||||||
if a.contains(&empty_string) {
|
if a.contains(&empty_string) {
|
||||||
return Err(serde::de::Error::invalid_type(
|
return Err(serde::de::Error::invalid_type(
|
||||||
Unexpected::Other("prefix matches must not be empty strings"),
|
Unexpected::Other("prefix matches must not be empty strings"),
|
||||||
&"a json object"));
|
&"a json object",
|
||||||
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
rf.ids =raw_ids;
|
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" {
|
||||||
@@ -123,12 +125,13 @@ 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" {
|
||||||
let raw_authors: Option<Vec<String>>= Deserialize::deserialize(val).ok();
|
let raw_authors: Option<Vec<String>> = Deserialize::deserialize(val).ok();
|
||||||
if let Some(a) = raw_authors.as_ref() {
|
if let Some(a) = raw_authors.as_ref() {
|
||||||
if a.contains(&empty_string) {
|
if a.contains(&empty_string) {
|
||||||
return Err(serde::de::Error::invalid_type(
|
return Err(serde::de::Error::invalid_type(
|
||||||
Unexpected::Other("prefix matches must not be empty strings"),
|
Unexpected::Other("prefix matches must not be empty strings"),
|
||||||
&"a json object"));
|
&"a json object",
|
||||||
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
rf.authors = raw_authors;
|
rf.authors = raw_authors;
|
||||||
@@ -185,7 +188,7 @@ impl<'de> Deserialize<'de> for Subscription {
|
|||||||
D: Deserializer<'de>,
|
D: Deserializer<'de>,
|
||||||
{
|
{
|
||||||
let mut v: Value = Deserialize::deserialize(deserializer)?;
|
let mut v: Value = Deserialize::deserialize(deserializer)?;
|
||||||
// this shoud be a 3-or-more element array.
|
// this should be a 3-or-more element array.
|
||||||
// verify the first element is a String, REQ
|
// verify the first element is a String, REQ
|
||||||
// get the subscription from the second element.
|
// get the subscription from the second element.
|
||||||
// convert each of the remaining objects into filters
|
// convert each of the remaining objects into filters
|
||||||
@@ -232,19 +235,22 @@ impl<'de> Deserialize<'de> for Subscription {
|
|||||||
|
|
||||||
impl Subscription {
|
impl Subscription {
|
||||||
/// Get a copy of the subscription identifier.
|
/// Get a copy of the subscription identifier.
|
||||||
#[must_use] pub fn get_id(&self) -> String {
|
#[must_use]
|
||||||
|
pub fn get_id(&self) -> String {
|
||||||
self.id.clone()
|
self.id.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Determine if any filter is requesting historical (database)
|
/// Determine if any filter is requesting historical (database)
|
||||||
/// queries. If every filter has limit:0, we do not need to query the DB.
|
/// queries. If every filter has limit:0, we do not need to query the DB.
|
||||||
#[must_use] pub fn needs_historical_events(&self) -> bool {
|
#[must_use]
|
||||||
self.filters.iter().any(|f| f.limit!=Some(0))
|
pub fn needs_historical_events(&self) -> bool {
|
||||||
|
self.filters.iter().any(|f| f.limit != Some(0))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Determine if this subscription matches a given [`Event`]. Any
|
/// Determine if this subscription matches a given [`Event`]. Any
|
||||||
/// individual filter match is sufficient.
|
/// individual filter match is sufficient.
|
||||||
#[must_use] pub fn interested_in_event(&self, event: &Event) -> bool {
|
#[must_use]
|
||||||
|
pub fn interested_in_event(&self, event: &Event) -> bool {
|
||||||
for f in &self.filters {
|
for f in &self.filters {
|
||||||
if f.interested_in_event(event) {
|
if f.interested_in_event(event) {
|
||||||
return true;
|
return true;
|
||||||
@@ -305,17 +311,16 @@ impl ReqFilter {
|
|||||||
|
|
||||||
/// Check if this filter either matches, or does not care about the kind.
|
/// Check if this filter either matches, or does not care about the kind.
|
||||||
fn kind_match(&self, kind: u64) -> bool {
|
fn kind_match(&self, kind: u64) -> bool {
|
||||||
self.kinds
|
self.kinds.as_ref().map_or(true, |ks| ks.contains(&kind))
|
||||||
.as_ref()
|
|
||||||
.map_or(true, |ks| ks.contains(&kind))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Determine if all populated fields in this filter match the provided event.
|
/// Determine if all populated fields in this filter match the provided event.
|
||||||
#[must_use] pub fn interested_in_event(&self, event: &Event) -> bool {
|
#[must_use]
|
||||||
|
pub fn interested_in_event(&self, event: &Event) -> bool {
|
||||||
// self.id.as_ref().map(|v| v == &event.id).unwrap_or(true)
|
// self.id.as_ref().map(|v| v == &event.id).unwrap_or(true)
|
||||||
self.ids_match(event)
|
self.ids_match(event)
|
||||||
&& self.since.map_or(true, |t| event.created_at > t)
|
&& self.since.map_or(true, |t| event.created_at >= t)
|
||||||
&& self.until.map_or(true, |t| event.created_at < t)
|
&& self.until.map_or(true, |t| event.created_at <= t)
|
||||||
&& self.kind_match(event.kind)
|
&& self.kind_match(event.kind)
|
||||||
&& (self.authors_match(event) || self.delegated_authors_match(event))
|
&& (self.authors_match(event) || self.delegated_authors_match(event))
|
||||||
&& self.tag_match(event)
|
&& self.tag_match(event)
|
||||||
@@ -625,7 +630,9 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn serialize_filter() -> Result<()> {
|
fn serialize_filter() -> Result<()> {
|
||||||
let s: Subscription = serde_json::from_str(r##"["REQ","xyz",{"authors":["abc", "bcd"], "since": 10, "until": 20, "limit":100, "#e": ["foo", "bar"], "#d": ["test"]}]"##)?;
|
let s: Subscription = serde_json::from_str(
|
||||||
|
r##"["REQ","xyz",{"authors":["abc", "bcd"], "since": 10, "until": 20, "limit":100, "#e": ["foo", "bar"], "#d": ["test"]}]"##,
|
||||||
|
)?;
|
||||||
let f = s.filters.get(0);
|
let f = s.filters.get(0);
|
||||||
let serialized = serde_json::to_string(&f)?;
|
let serialized = serde_json::to_string(&f)?;
|
||||||
let serialized_wrapped = format!(r##"["REQ", "xyz",{}]"##, serialized);
|
let serialized_wrapped = format!(r##"["REQ", "xyz",{}]"##, serialized);
|
||||||
|
|||||||
+13
-8
@@ -4,7 +4,8 @@ use std::time::SystemTime;
|
|||||||
use url::Url;
|
use url::Url;
|
||||||
|
|
||||||
/// Seconds since 1970.
|
/// Seconds since 1970.
|
||||||
#[must_use] pub fn unix_time() -> u64 {
|
#[must_use]
|
||||||
|
pub fn unix_time() -> u64 {
|
||||||
SystemTime::now()
|
SystemTime::now()
|
||||||
.duration_since(SystemTime::UNIX_EPOCH)
|
.duration_since(SystemTime::UNIX_EPOCH)
|
||||||
.map(|x| x.as_secs())
|
.map(|x| x.as_secs())
|
||||||
@@ -12,7 +13,8 @@ use url::Url;
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Check if a string contains only hex characters.
|
/// Check if a string contains only hex characters.
|
||||||
#[must_use] pub fn is_hex(s: &str) -> bool {
|
#[must_use]
|
||||||
|
pub fn is_hex(s: &str) -> bool {
|
||||||
s.chars().all(|x| char::is_ascii_hexdigit(&x))
|
s.chars().all(|x| char::is_ascii_hexdigit(&x))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -28,14 +30,17 @@ pub fn nip19_to_hex(s: &str) -> Result<String, bech32::Error> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Check if a string contains only lower-case hex chars.
|
/// Check if a string contains only lower-case hex chars.
|
||||||
#[must_use] pub fn is_lower_hex(s: &str) -> bool {
|
#[must_use]
|
||||||
|
pub fn is_lower_hex(s: &str) -> bool {
|
||||||
s.chars().all(|x| {
|
s.chars().all(|x| {
|
||||||
(char::is_ascii_lowercase(&x) || char::is_ascii_digit(&x)) && char::is_ascii_hexdigit(&x)
|
(char::is_ascii_lowercase(&x) || char::is_ascii_digit(&x)) && char::is_ascii_hexdigit(&x)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn host_str(url: &String) -> Option<String> {
|
pub fn host_str(url: &str) -> Option<String> {
|
||||||
Url::parse(url).ok().and_then(|u| u.host_str().map(|s| s.to_string()))
|
Url::parse(url)
|
||||||
|
.ok()
|
||||||
|
.and_then(|u| u.host_str().map(|s| s.to_string()))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
@@ -45,15 +50,15 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn lower_hex() {
|
fn lower_hex() {
|
||||||
let hexstr = "abcd0123";
|
let hexstr = "abcd0123";
|
||||||
assert_eq!(is_lower_hex(hexstr), true);
|
assert!(is_lower_hex(hexstr));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn nip19() {
|
fn nip19() {
|
||||||
let hexkey = "3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d";
|
let hexkey = "3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d";
|
||||||
let nip19key = "npub180cvv07tjdrrgpa0j7j7tmnyl2yr6yr7l8j4s3evf6u64th6gkwsyjh6w6";
|
let nip19key = "npub180cvv07tjdrrgpa0j7j7tmnyl2yr6yr7l8j4s3evf6u64th6gkwsyjh6w6";
|
||||||
assert_eq!(is_nip19(hexkey), false);
|
assert!(!is_nip19(hexkey));
|
||||||
assert_eq!(is_nip19(nip19key), true);
|
assert!(is_nip19(nip19key));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|||||||
+1
-1
@@ -7,4 +7,4 @@ mod tests {
|
|||||||
use clap::CommandFactory;
|
use clap::CommandFactory;
|
||||||
CLIArgs::command().debug_assert();
|
CLIArgs::command().debug_assert();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+16
-16
@@ -52,7 +52,7 @@ mod tests {
|
|||||||
let challenge = client_conn.auth_challenge().unwrap();
|
let challenge = client_conn.auth_challenge().unwrap();
|
||||||
let event = auth_event(challenge);
|
let event = auth_event(challenge);
|
||||||
|
|
||||||
let result = client_conn.authenticate(&event, &RELAY.into());
|
let result = client_conn.authenticate(&event, RELAY.into());
|
||||||
|
|
||||||
assert!(matches!(result, Ok(())));
|
assert!(matches!(result, Ok(())));
|
||||||
assert_eq!(client_conn.auth_challenge(), None);
|
assert_eq!(client_conn.auth_challenge(), None);
|
||||||
@@ -67,7 +67,7 @@ mod tests {
|
|||||||
assert_eq!(client_conn.auth_pubkey(), None);
|
assert_eq!(client_conn.auth_pubkey(), None);
|
||||||
|
|
||||||
let event = auth_event(&"challenge".into());
|
let event = auth_event(&"challenge".into());
|
||||||
let result = client_conn.authenticate(&event, &RELAY.into());
|
let result = client_conn.authenticate(&event, RELAY.into());
|
||||||
|
|
||||||
assert!(matches!(result, Err(Error::AuthFailure)));
|
assert!(matches!(result, Err(Error::AuthFailure)));
|
||||||
}
|
}
|
||||||
@@ -87,14 +87,14 @@ mod tests {
|
|||||||
let challenge = client_conn.auth_challenge().unwrap().clone();
|
let challenge = client_conn.auth_challenge().unwrap().clone();
|
||||||
|
|
||||||
let event = auth_event(&challenge);
|
let event = auth_event(&challenge);
|
||||||
let result = client_conn.authenticate(&event, &RELAY.into());
|
let result = client_conn.authenticate(&event, RELAY.into());
|
||||||
|
|
||||||
assert!(matches!(result, Ok(())));
|
assert!(matches!(result, Ok(())));
|
||||||
assert_eq!(client_conn.auth_challenge(), None);
|
assert_eq!(client_conn.auth_challenge(), None);
|
||||||
assert_eq!(client_conn.auth_pubkey(), Some(&event.pubkey));
|
assert_eq!(client_conn.auth_pubkey(), Some(&event.pubkey));
|
||||||
|
|
||||||
let event1 = auth_event(&challenge);
|
let event1 = auth_event(&challenge);
|
||||||
let result1 = client_conn.authenticate(&event1, &RELAY.into());
|
let result1 = client_conn.authenticate(&event1, RELAY.into());
|
||||||
|
|
||||||
assert!(matches!(result1, Ok(())));
|
assert!(matches!(result1, Ok(())));
|
||||||
assert_eq!(client_conn.auth_challenge(), None);
|
assert_eq!(client_conn.auth_challenge(), None);
|
||||||
@@ -118,7 +118,7 @@ mod tests {
|
|||||||
let mut event = auth_event(challenge);
|
let mut event = auth_event(challenge);
|
||||||
event.sig = event.sig.chars().rev().collect::<String>();
|
event.sig = event.sig.chars().rev().collect::<String>();
|
||||||
|
|
||||||
let result = client_conn.authenticate(&event, &RELAY.into());
|
let result = client_conn.authenticate(&event, RELAY.into());
|
||||||
|
|
||||||
assert!(matches!(result, Err(Error::AuthFailure)));
|
assert!(matches!(result, Err(Error::AuthFailure)));
|
||||||
}
|
}
|
||||||
@@ -138,7 +138,7 @@ mod tests {
|
|||||||
let challenge = client_conn.auth_challenge().unwrap();
|
let challenge = client_conn.auth_challenge().unwrap();
|
||||||
let event = auth_event_with_kind(challenge, 9999999999999999);
|
let event = auth_event_with_kind(challenge, 9999999999999999);
|
||||||
|
|
||||||
let result = client_conn.authenticate(&event, &RELAY.into());
|
let result = client_conn.authenticate(&event, RELAY.into());
|
||||||
|
|
||||||
assert!(matches!(result, Err(Error::AuthFailure)));
|
assert!(matches!(result, Err(Error::AuthFailure)));
|
||||||
}
|
}
|
||||||
@@ -158,7 +158,7 @@ mod tests {
|
|||||||
let challenge = client_conn.auth_challenge().unwrap();
|
let challenge = client_conn.auth_challenge().unwrap();
|
||||||
let event = auth_event_with_created_at(challenge, unix_time() - 1200); // 20 minutes
|
let event = auth_event_with_created_at(challenge, unix_time() - 1200); // 20 minutes
|
||||||
|
|
||||||
let result = client_conn.authenticate(&event, &RELAY.into());
|
let result = client_conn.authenticate(&event, RELAY.into());
|
||||||
|
|
||||||
assert!(matches!(result, Err(Error::AuthFailure)));
|
assert!(matches!(result, Err(Error::AuthFailure)));
|
||||||
}
|
}
|
||||||
@@ -178,7 +178,7 @@ mod tests {
|
|||||||
let challenge = client_conn.auth_challenge().unwrap();
|
let challenge = client_conn.auth_challenge().unwrap();
|
||||||
let event = auth_event_with_created_at(challenge, unix_time() + 1200); // 20 minutes
|
let event = auth_event_with_created_at(challenge, unix_time() + 1200); // 20 minutes
|
||||||
|
|
||||||
let result = client_conn.authenticate(&event, &RELAY.into());
|
let result = client_conn.authenticate(&event, RELAY.into());
|
||||||
|
|
||||||
assert!(matches!(result, Err(Error::AuthFailure)));
|
assert!(matches!(result, Err(Error::AuthFailure)));
|
||||||
}
|
}
|
||||||
@@ -197,7 +197,7 @@ mod tests {
|
|||||||
|
|
||||||
let event = auth_event_without_tags();
|
let event = auth_event_without_tags();
|
||||||
|
|
||||||
let result = client_conn.authenticate(&event, &RELAY.into());
|
let result = client_conn.authenticate(&event, RELAY.into());
|
||||||
|
|
||||||
assert!(matches!(result, Err(Error::AuthFailure)));
|
assert!(matches!(result, Err(Error::AuthFailure)));
|
||||||
}
|
}
|
||||||
@@ -216,7 +216,7 @@ mod tests {
|
|||||||
|
|
||||||
let event = auth_event_without_challenge();
|
let event = auth_event_without_challenge();
|
||||||
|
|
||||||
let result = client_conn.authenticate(&event, &RELAY.into());
|
let result = client_conn.authenticate(&event, RELAY.into());
|
||||||
|
|
||||||
assert!(matches!(result, Err(Error::AuthFailure)));
|
assert!(matches!(result, Err(Error::AuthFailure)));
|
||||||
}
|
}
|
||||||
@@ -236,7 +236,7 @@ mod tests {
|
|||||||
let challenge = client_conn.auth_challenge().unwrap();
|
let challenge = client_conn.auth_challenge().unwrap();
|
||||||
let event = auth_event_without_relay(challenge);
|
let event = auth_event_without_relay(challenge);
|
||||||
|
|
||||||
let result = client_conn.authenticate(&event, &RELAY.into());
|
let result = client_conn.authenticate(&event, RELAY.into());
|
||||||
|
|
||||||
assert!(matches!(result, Err(Error::AuthFailure)));
|
assert!(matches!(result, Err(Error::AuthFailure)));
|
||||||
}
|
}
|
||||||
@@ -255,7 +255,7 @@ mod tests {
|
|||||||
|
|
||||||
let event = auth_event(&"invalid challenge".into());
|
let event = auth_event(&"invalid challenge".into());
|
||||||
|
|
||||||
let result = client_conn.authenticate(&event, &RELAY.into());
|
let result = client_conn.authenticate(&event, RELAY.into());
|
||||||
|
|
||||||
assert!(matches!(result, Err(Error::AuthFailure)));
|
assert!(matches!(result, Err(Error::AuthFailure)));
|
||||||
}
|
}
|
||||||
@@ -275,7 +275,7 @@ mod tests {
|
|||||||
let challenge = client_conn.auth_challenge().unwrap();
|
let challenge = client_conn.auth_challenge().unwrap();
|
||||||
let event = auth_event_with_relay(challenge, &"xyz".into());
|
let event = auth_event_with_relay(challenge, &"xyz".into());
|
||||||
|
|
||||||
let result = client_conn.authenticate(&event, &RELAY.into());
|
let result = client_conn.authenticate(&event, RELAY.into());
|
||||||
|
|
||||||
assert!(matches!(result, Err(Error::AuthFailure)));
|
assert!(matches!(result, Err(Error::AuthFailure)));
|
||||||
}
|
}
|
||||||
@@ -334,9 +334,9 @@ mod tests {
|
|||||||
id: "0".to_owned(),
|
id: "0".to_owned(),
|
||||||
pubkey: public_key.to_hex(),
|
pubkey: public_key.to_hex(),
|
||||||
delegated_by: None,
|
delegated_by: None,
|
||||||
created_at: created_at,
|
created_at,
|
||||||
kind: kind,
|
kind,
|
||||||
tags: tags,
|
tags,
|
||||||
content: "".to_owned(),
|
content: "".to_owned(),
|
||||||
sig: "0".to_owned(),
|
sig: "0".to_owned(),
|
||||||
tagidx: None,
|
tagidx: None,
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
|
use futures::SinkExt;
|
||||||
|
use futures::StreamExt;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
use tokio_tungstenite::connect_async;
|
||||||
|
use tracing::info;
|
||||||
mod common;
|
mod common;
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
@@ -45,3 +47,33 @@ async fn relay_home_page() -> Result<()> {
|
|||||||
let _res = relay.shutdown_tx.send(());
|
let _res = relay.shutdown_tx.send(());
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//#[tokio::test]
|
||||||
|
// Still inwork
|
||||||
|
async fn publish_test() -> Result<()> {
|
||||||
|
// get a relay and wait for startup
|
||||||
|
let relay = common::start_relay()?;
|
||||||
|
common::wait_for_healthy_relay(&relay).await?;
|
||||||
|
// open a non-secure websocket connection.
|
||||||
|
let (mut ws, _res) = connect_async(format!("ws://localhost:{}", relay.port)).await?;
|
||||||
|
// send a simple pre-made message
|
||||||
|
let simple_event = r#"["EVENT", {"content": "hello world","created_at": 1691239763,
|
||||||
|
"id":"f3ce6798d70e358213ebbeba4886bbdfacf1ecfd4f65ee5323ef5f404de32b86",
|
||||||
|
"kind": 1,
|
||||||
|
"pubkey": "79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798",
|
||||||
|
"sig": "30ca29e8581eeee75bf838171dec818af5e6de2b74f5337de940f5cc91186534c0b20d6cf7ad1043a2c51dbd60b979447720a471d346322103c83f6cb66e4e98",
|
||||||
|
"tags": []}]"#;
|
||||||
|
ws.send(simple_event.into()).await?;
|
||||||
|
// get response from server, confirm it is an array with first element "OK"
|
||||||
|
let event_confirm = ws.next().await;
|
||||||
|
ws.close(None).await?;
|
||||||
|
info!("event confirmed: {:?}", event_confirm);
|
||||||
|
// open a new connection, and wait for some time to get the event.
|
||||||
|
let (mut sub_ws, _res) = connect_async(format!("ws://localhost:{}", relay.port)).await?;
|
||||||
|
let event_sub = r#"["REQ", "simple", {}]"#;
|
||||||
|
sub_ws.send(event_sub.into()).await?;
|
||||||
|
// read from subscription
|
||||||
|
let _ws_next = sub_ws.next().await;
|
||||||
|
let _res = relay.shutdown_tx.send(());
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user