mirror of
https://github.com/scsibug/nostr-rs-relay.git
synced 2024-11-09 21:29:06 -05:00
feat(NIP-20): send command results to clients
When submitting events to relays, clients currently have no way to know if an event was successfully committed to the database. This NIP introduces the concept of command results which are like NOTICE's except provide more information about if an event was accepted or rejected. A command result is a JSON object with the following structure that is returned when an event is successfully saved to the database or rejected: ["OK", <event_id>, <true|false>, <message>] nip20: https://github.com/nostr-protocol/nips/pull/62
This commit is contained in:
parent
7adc5c9af7
commit
5a91419d34
31
src/db.rs
31
src/db.rs
|
@ -6,6 +6,7 @@ use crate::event::{single_char_tagname, Event};
|
||||||
use crate::hexrange::hex_range;
|
use crate::hexrange::hex_range;
|
||||||
use crate::hexrange::HexSearch;
|
use crate::hexrange::HexSearch;
|
||||||
use crate::nip05;
|
use crate::nip05;
|
||||||
|
use crate::notice::Notice;
|
||||||
use crate::schema::{upgrade_db, STARTUP_SQL};
|
use crate::schema::{upgrade_db, STARTUP_SQL};
|
||||||
use crate::subscription::ReqFilter;
|
use crate::subscription::ReqFilter;
|
||||||
use crate::subscription::Subscription;
|
use crate::subscription::Subscription;
|
||||||
|
@ -32,7 +33,7 @@ pub type PooledConnection = r2d2::PooledConnection<r2d2_sqlite::SqliteConnection
|
||||||
/// Events submitted from a client, with a return channel for notices
|
/// Events submitted from a client, with a return channel for notices
|
||||||
pub struct SubmittedEvent {
|
pub struct SubmittedEvent {
|
||||||
pub event: Event,
|
pub event: Event,
|
||||||
pub notice_tx: tokio::sync::mpsc::Sender<String>,
|
pub notice_tx: tokio::sync::mpsc::Sender<Notice>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Database file
|
/// Database file
|
||||||
|
@ -158,7 +159,10 @@ pub async fn db_writer(
|
||||||
event.get_event_id_prefix()
|
event.get_event_id_prefix()
|
||||||
);
|
);
|
||||||
notice_tx
|
notice_tx
|
||||||
.try_send("pubkey is not allowed to publish to this relay".to_owned())
|
.try_send(Notice::blocked(
|
||||||
|
event.id,
|
||||||
|
"pubkey is not allowed to publish to this relay",
|
||||||
|
))
|
||||||
.ok();
|
.ok();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -189,10 +193,10 @@ pub async fn db_writer(
|
||||||
event.get_author_prefix()
|
event.get_author_prefix()
|
||||||
);
|
);
|
||||||
notice_tx
|
notice_tx
|
||||||
.try_send(
|
.try_send(Notice::blocked(
|
||||||
"NIP-05 verification is no longer valid (expired/wrong domain)"
|
event.id,
|
||||||
.to_owned(),
|
"NIP-05 verification is no longer valid (expired/wrong domain)",
|
||||||
)
|
))
|
||||||
.ok();
|
.ok();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -203,7 +207,10 @@ pub async fn db_writer(
|
||||||
event.get_author_prefix()
|
event.get_author_prefix()
|
||||||
);
|
);
|
||||||
notice_tx
|
notice_tx
|
||||||
.try_send("NIP-05 verification needed to publish events".to_owned())
|
.try_send(Notice::blocked(
|
||||||
|
event.id,
|
||||||
|
"NIP-05 verification needed to publish events",
|
||||||
|
))
|
||||||
.ok();
|
.ok();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -229,6 +236,7 @@ pub async fn db_writer(
|
||||||
Ok(updated) => {
|
Ok(updated) => {
|
||||||
if updated == 0 {
|
if updated == 0 {
|
||||||
trace!("ignoring duplicate or deleted event");
|
trace!("ignoring duplicate or deleted event");
|
||||||
|
notice_tx.try_send(Notice::duplicate(event.id)).ok();
|
||||||
} else {
|
} else {
|
||||||
info!(
|
info!(
|
||||||
"persisted event: {:?} from: {:?} in: {:?}",
|
"persisted event: {:?} from: {:?} in: {:?}",
|
||||||
|
@ -239,16 +247,13 @@ pub async fn db_writer(
|
||||||
event_write = true;
|
event_write = true;
|
||||||
// send this out to all clients
|
// send this out to all clients
|
||||||
bcast_tx.send(event.clone()).ok();
|
bcast_tx.send(event.clone()).ok();
|
||||||
|
notice_tx.try_send(Notice::saved(event.id)).ok();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
warn!("event insert failed: {:?}", err);
|
warn!("event insert failed: {:?}", err);
|
||||||
notice_tx
|
let msg = "relay experienced an error trying to publish the latest event";
|
||||||
.try_send(
|
notice_tx.try_send(Notice::error(event.id, msg)).ok();
|
||||||
"relay experienced an error trying to publish the latest event"
|
|
||||||
.to_owned(),
|
|
||||||
)
|
|
||||||
.ok();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,7 @@ pub mod event;
|
||||||
pub mod hexrange;
|
pub mod hexrange;
|
||||||
pub mod info;
|
pub mod info;
|
||||||
pub mod nip05;
|
pub mod nip05;
|
||||||
|
pub mod notice;
|
||||||
pub mod schema;
|
pub mod schema;
|
||||||
pub mod subscription;
|
pub mod subscription;
|
||||||
pub mod utils;
|
pub mod utils;
|
||||||
|
|
86
src/notice.rs
Normal file
86
src/notice.rs
Normal file
|
@ -0,0 +1,86 @@
|
||||||
|
pub enum EventResultStatus {
|
||||||
|
Saved,
|
||||||
|
Duplicate,
|
||||||
|
Invalid,
|
||||||
|
Blocked,
|
||||||
|
RateLimited,
|
||||||
|
Error,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct EventResult {
|
||||||
|
pub id: String,
|
||||||
|
pub msg: String,
|
||||||
|
pub status: EventResultStatus,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum Notice {
|
||||||
|
Message(String),
|
||||||
|
EventResult(EventResult),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl EventResultStatus {
|
||||||
|
pub fn to_bool(&self) -> bool {
|
||||||
|
match self {
|
||||||
|
Self::Saved => true,
|
||||||
|
Self::Duplicate => true,
|
||||||
|
Self::Invalid => false,
|
||||||
|
Self::Blocked => false,
|
||||||
|
Self::RateLimited => false,
|
||||||
|
Self::Error => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn prefix(&self) -> &'static str {
|
||||||
|
match self {
|
||||||
|
Self::Saved => "saved",
|
||||||
|
Self::Duplicate => "duplicate",
|
||||||
|
Self::Invalid => "invalid",
|
||||||
|
Self::Blocked => "blocked",
|
||||||
|
Self::RateLimited => "rate-limited",
|
||||||
|
Self::Error => "error",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Notice {
|
||||||
|
//pub fn err(err: error::Error, id: String) -> Notice {
|
||||||
|
// Notice::err_msg(format!("{}", err), id)
|
||||||
|
//}
|
||||||
|
|
||||||
|
pub fn message(msg: String) -> Notice {
|
||||||
|
Notice::Message(msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn prefixed(id: String, msg: &str, status: EventResultStatus) -> Notice {
|
||||||
|
let msg = format!("{}: {}", status.prefix(), msg);
|
||||||
|
Notice::EventResult(EventResult { id, msg, status })
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn invalid(id: String, msg: &str) -> Notice {
|
||||||
|
Notice::prefixed(id, msg, EventResultStatus::Invalid)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn blocked(id: String, msg: &str) -> Notice {
|
||||||
|
Notice::prefixed(id, msg, EventResultStatus::Blocked)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn rate_limited(id: String, msg: &str) -> Notice {
|
||||||
|
Notice::prefixed(id, msg, EventResultStatus::RateLimited)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn duplicate(id: String) -> Notice {
|
||||||
|
Notice::prefixed(id, "", EventResultStatus::Duplicate)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn error(id: String, msg: &str) -> Notice {
|
||||||
|
Notice::prefixed(id, msg, EventResultStatus::Error)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn saved(id: String) -> Notice {
|
||||||
|
Notice::EventResult(EventResult {
|
||||||
|
id,
|
||||||
|
msg: "".into(),
|
||||||
|
status: EventResultStatus::Saved,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
|
@ -10,6 +10,7 @@ use crate::event::Event;
|
||||||
use crate::event::EventCmd;
|
use crate::event::EventCmd;
|
||||||
use crate::info::RelayInfo;
|
use crate::info::RelayInfo;
|
||||||
use crate::nip05;
|
use crate::nip05;
|
||||||
|
use crate::notice::Notice;
|
||||||
use crate::subscription::Subscription;
|
use crate::subscription::Subscription;
|
||||||
use futures::SinkExt;
|
use futures::SinkExt;
|
||||||
use futures::StreamExt;
|
use futures::StreamExt;
|
||||||
|
@ -405,8 +406,13 @@ fn convert_to_msg(msg: String, max_bytes: Option<usize>) -> Result<NostrMessage>
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Turn a string into a NOTICE message ready to send over a WebSocket
|
/// Turn a string into a NOTICE message ready to send over a WebSocket
|
||||||
fn make_notice_message(msg: &str) -> Message {
|
fn make_notice_message(notice: Notice) -> Message {
|
||||||
Message::text(json!(["NOTICE", msg]).to_string())
|
let json = match notice {
|
||||||
|
Notice::Message(ref msg) => json!(["NOTICE", msg]),
|
||||||
|
Notice::EventResult(ref res) => json!(["OK", res.id, res.status.to_bool(), res.msg]),
|
||||||
|
};
|
||||||
|
|
||||||
|
Message::text(json.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ClientInfo {
|
struct ClientInfo {
|
||||||
|
@ -435,7 +441,7 @@ async fn nostr_server(
|
||||||
// we will send out the tx handle to any query we generate.
|
// we will send out the tx handle to any query we generate.
|
||||||
let (query_tx, mut query_rx) = mpsc::channel::<db::QueryResult>(256);
|
let (query_tx, mut query_rx) = mpsc::channel::<db::QueryResult>(256);
|
||||||
// Create channel for receiving NOTICEs
|
// Create channel for receiving NOTICEs
|
||||||
let (notice_tx, mut notice_rx) = mpsc::channel::<String>(32);
|
let (notice_tx, mut notice_rx) = mpsc::channel::<Notice>(32);
|
||||||
|
|
||||||
// last time this client sent data (message, ping, etc.)
|
// last time this client sent data (message, ping, etc.)
|
||||||
let mut last_message_time = Instant::now();
|
let mut last_message_time = Instant::now();
|
||||||
|
@ -480,7 +486,7 @@ async fn nostr_server(
|
||||||
ws_stream.send(Message::Ping(Vec::new())).await.ok();
|
ws_stream.send(Message::Ping(Vec::new())).await.ok();
|
||||||
},
|
},
|
||||||
Some(notice_msg) = notice_rx.recv() => {
|
Some(notice_msg) = notice_rx.recv() => {
|
||||||
ws_stream.send(make_notice_message(¬ice_msg)).await.ok();
|
ws_stream.send(make_notice_message(notice_msg)).await.ok();
|
||||||
},
|
},
|
||||||
Some(query_result) = query_rx.recv() => {
|
Some(query_result) = query_rx.recv() => {
|
||||||
// database informed us of a query result we asked for
|
// database informed us of a query result we asked for
|
||||||
|
@ -528,7 +534,7 @@ async fn nostr_server(
|
||||||
},
|
},
|
||||||
Some(Ok(Message::Binary(_))) => {
|
Some(Ok(Message::Binary(_))) => {
|
||||||
ws_stream.send(
|
ws_stream.send(
|
||||||
make_notice_message("binary messages are not accepted")).await.ok();
|
make_notice_message(Notice::message("binary messages are not accepted".into()))).await.ok();
|
||||||
continue;
|
continue;
|
||||||
},
|
},
|
||||||
Some(Ok(Message::Ping(_) | Message::Pong(_))) => {
|
Some(Ok(Message::Ping(_) | Message::Pong(_))) => {
|
||||||
|
@ -538,8 +544,7 @@ async fn nostr_server(
|
||||||
},
|
},
|
||||||
Some(Err(WsError::Capacity(MessageTooLong{size, max_size}))) => {
|
Some(Err(WsError::Capacity(MessageTooLong{size, max_size}))) => {
|
||||||
ws_stream.send(
|
ws_stream.send(
|
||||||
make_notice_message(
|
make_notice_message(Notice::message(format!("message too large ({} > {})",size, max_size)))).await.ok();
|
||||||
&format!("message too large ({} > {})",size, max_size))).await.ok();
|
|
||||||
continue;
|
continue;
|
||||||
},
|
},
|
||||||
None |
|
None |
|
||||||
|
@ -581,13 +586,15 @@ async fn nostr_server(
|
||||||
} else {
|
} else {
|
||||||
info!("client: {} sent a far future-dated event", cid);
|
info!("client: {} sent a far future-dated event", cid);
|
||||||
if let Some(fut_sec) = settings.options.reject_future_seconds {
|
if let Some(fut_sec) = settings.options.reject_future_seconds {
|
||||||
ws_stream.send(make_notice_message(&format!("The event created_at field is out of the acceptable range (+{}sec) for this relay and was not stored.",fut_sec))).await.ok();
|
let msg = format!("The event created_at field is out of the acceptable range (+{}sec) for this relay.",fut_sec);
|
||||||
|
let notice = Notice::invalid(e.id, &msg);
|
||||||
|
ws_stream.send(make_notice_message(notice)).await.ok();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
info!("client: {} sent an invalid event", cid);
|
info!("client: {} sent an invalid event", cid);
|
||||||
ws_stream.send(make_notice_message("event was invalid")).await.ok();
|
ws_stream.send(make_notice_message(Notice::message("event was invalid".into()))).await.ok();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -609,7 +616,7 @@ async fn nostr_server(
|
||||||
},
|
},
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
info!("Subscription error: {}", e);
|
info!("Subscription error: {}", e);
|
||||||
ws_stream.send(make_notice_message(&e.to_string())).await.ok();
|
ws_stream.send(make_notice_message(Notice::message(format!("Subscription error: {}", e)))).await.ok();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -628,7 +635,7 @@ async fn nostr_server(
|
||||||
conn.unsubscribe(&c);
|
conn.unsubscribe(&c);
|
||||||
} else {
|
} else {
|
||||||
info!("invalid command ignored");
|
info!("invalid command ignored");
|
||||||
ws_stream.send(make_notice_message("could not parse command")).await.ok();
|
ws_stream.send(make_notice_message(Notice::message("could not parse command".into()))).await.ok();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Err(Error::ConnError) => {
|
Err(Error::ConnError) => {
|
||||||
|
@ -637,11 +644,11 @@ async fn nostr_server(
|
||||||
}
|
}
|
||||||
Err(Error::EventMaxLengthError(s)) => {
|
Err(Error::EventMaxLengthError(s)) => {
|
||||||
info!("client: {} sent event larger ({} bytes) than max size", cid, s);
|
info!("client: {} sent event larger ({} bytes) than max size", cid, s);
|
||||||
ws_stream.send(make_notice_message("event exceeded max size")).await.ok();
|
ws_stream.send(make_notice_message(Notice::message("event exceeded max size".into()))).await.ok();
|
||||||
},
|
},
|
||||||
Err(Error::ProtoParseError) => {
|
Err(Error::ProtoParseError) => {
|
||||||
info!("client {} sent event that could not be parsed", cid);
|
info!("client {} sent event that could not be parsed", cid);
|
||||||
ws_stream.send(make_notice_message("could not parse command")).await.ok();
|
ws_stream.send(make_notice_message(Notice::message("could not parse command".into()))).await.ok();
|
||||||
},
|
},
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
info!("got non-fatal error from client: {}, error: {:?}", cid, e);
|
info!("got non-fatal error from client: {}, error: {:?}", cid, e);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user