use std::borrow::Cow;
use std::time::Duration;
use negentropy::{Id, Negentropy, NegentropyStorageBase, NegentropyStorageVector};
use nostr_sdk::prelude::*;
use super::transport::{Diff, ReconcileFail};
const FRAME_LIMIT: u64 = 60_000;
pub(crate) enum Ended {
Fail(ReconcileFail),
Silent,
}
fn failed(why: impl std::fmt::Display) -> Ended {
Ended::Fail(ReconcileFail::Failed(why.to_string()))
}
fn refuses(notice: &str) -> bool {
let notice = notice.to_ascii_lowercase();
if ["too many", "rate", "limit", "slow down", "busy"].iter().any(|limit| notice.contains(limit)) {
return false;
}
(notice.contains("bad msg") || notice.contains("unknown") || notice.contains("unrecognized") || notice.contains("unsupported") || notice.contains("invalid"))
&& (notice.contains("neg") || notice.contains("unknown cmd") || notice.contains("unknown message") || notice.contains("unrecognized message") || notice.contains("unrecognised"))
}
pub(crate) fn open(held: &[(EventId, u64)]) -> Result<(Negentropy<'static, NegentropyStorageVector>, Vec<u8>), negentropy::Error> {
let mut storage = NegentropyStorageVector::with_capacity(held.len());
for (id, at) in held {
storage.insert(*at, Id::from_byte_array(id.to_bytes()))?;
}
storage.seal()?;
let size = storage.size()?;
if size == 0 {
let mut negentropy = Negentropy::owned(storage, FRAME_LIMIT)?;
let opening = negentropy.initiate()?;
return Ok((negentropy, opening));
}
let whole = storage.fingerprint(0, size)?.to_bytes();
let mut negentropy = Negentropy::owned(storage, FRAME_LIMIT)?;
negentropy.set_initiator();
let mut opening = vec![negentropy::PROTOCOL_VERSION as u8, 0, 0, 1];
opening.extend_from_slice(&whole);
Ok((negentropy, opening))
}
pub(crate) async fn reconcile(relay: &Relay, filter: Filter, held: &[(EventId, u64)], budget: usize, first: Duration, idle: Duration) -> Result<Diff, Ended> {
let (mut negentropy, opening) = open(held).map_err(failed)?;
let mut notifications = relay.notifications();
let sub_id = SubscriptionId::generate();
struct CloseGuard(Relay, SubscriptionId);
impl Drop for CloseGuard {
fn drop(&mut self) {
let (relay, id) = (self.0.clone(), self.1.clone());
crate::rt::spawn(async move {
let _ = relay.send_msg(ClientMessage::NegClose { subscription_id: Cow::Owned(id) }).await;
});
}
}
let _close = CloseGuard(relay.clone(), sub_id.clone());
relay
.send_msg(ClientMessage::NegOpen {
subscription_id: Cow::Borrowed(&sub_id),
filter: Cow::Owned(filter),
initial_message: Cow::Owned(crate::simd::hex::bytes_to_hex_string(&opening)),
})
.await
.map_err(|_| Ended::Fail(ReconcileFail::Unreached))?;
let mut diff = Diff::default();
let mut answered = false;
loop {
let deadline = crate::rt::time::Instant::now() + if answered { idle } else { first };
let notification = match crate::rt::time::timeout_at(deadline, notifications.next()).await {
Ok(Some(n)) => n,
Ok(None) => return Err(Ended::Fail(ReconcileFail::Unreached)),
Err(_) if answered => return Err(failed("the relay stopped answering")),
Err(_) => return Err(Ended::Silent),
};
let message = match notification {
RelayNotification::Message { message } => *message,
RelayNotification::RelayStatus { status: RelayStatus::Shutdown | RelayStatus::Terminated | RelayStatus::Banned } => {
return Err(Ended::Fail(ReconcileFail::Unreached));
}
_ => continue,
};
match message {
RelayMessage::NegMsg { subscription_id, message } if *subscription_id == sub_id => {
answered = true;
let Some(bytes) = crate::simd::hex::hex_string_to_bytes_checked(&message) else {
return Err(failed("a reconciliation message that is not hex"));
};
let (mut have, mut need) = (Vec::new(), Vec::new());
let next = negentropy.reconcile_with_ids(&bytes, &mut have, &mut need).map_err(failed)?;
diff.lack.extend(have.into_iter().map(|id| EventId::from_byte_array(id.to_bytes())));
diff.need.extend(need.into_iter().map(|id| EventId::from_byte_array(id.to_bytes())));
match next {
None => {
diff.complete = diff.need.len() <= budget;
diff.need.truncate(budget);
return Ok(diff);
}
Some(_) if diff.need.len() >= budget => {
diff.need.truncate(budget);
return Ok(diff);
}
Some(next) => {
relay
.send_msg(ClientMessage::NegMsg { subscription_id: Cow::Borrowed(&sub_id), message: Cow::Owned(crate::simd::hex::bytes_to_hex_string(&next)) })
.await
.map_err(|_| Ended::Fail(ReconcileFail::Unreached))?;
}
}
}
RelayMessage::NegErr { subscription_id, message } if *subscription_id == sub_id => {
return Err(Ended::Fail(match MachineReadablePrefix::parse(&message) {
Some(MachineReadablePrefix::AuthRequired | MachineReadablePrefix::Restricted) => ReconcileFail::Gated,
Some(MachineReadablePrefix::Blocked) if message.contains("too many") => ReconcileFail::Failed(message.into_owned()),
Some(MachineReadablePrefix::Blocked) => ReconcileFail::Gated,
_ => ReconcileFail::Failed(message.into_owned()),
}));
}
RelayMessage::Closed { subscription_id, message } if *subscription_id == sub_id => {
return Err(Ended::Fail(match MachineReadablePrefix::parse(&message) {
Some(MachineReadablePrefix::AuthRequired | MachineReadablePrefix::Restricted) => ReconcileFail::Gated,
_ => ReconcileFail::Failed(message.into_owned()),
}));
}
RelayMessage::Notice(notice) if !answered && refuses(¬ice) => {
return Err(Ended::Fail(ReconcileFail::Unsupported));
}
_ => {}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn wraps(ids: impl IntoIterator<Item = u16>) -> Vec<(EventId, u64)> {
ids.into_iter()
.map(|i| {
let id: [u8; 32] = <sha2::Sha256 as sha2::Digest>::digest(i.to_be_bytes()).into();
(EventId::from_byte_array(id), 1_000 + (i as u64 % 40))
})
.collect()
}
fn exchange(held: &[(EventId, u64)], theirs: &[(EventId, u64)]) -> (Vec<EventId>, Vec<EventId>, usize, (usize, usize)) {
let (mut ours, opening) = open(held).unwrap();
let mut storage = NegentropyStorageVector::new();
for (id, at) in theirs {
storage.insert(*at, Id::from_byte_array(id.to_bytes())).unwrap();
}
storage.seal().unwrap();
let mut relay = Negentropy::owned(storage, FRAME_LIMIT).unwrap();
let (mut need, mut lack, mut rounds, size, mut answered) = (Vec::new(), Vec::new(), 0, opening.len(), 0);
let mut message = Some(opening);
while let Some(out) = message.take() {
rounds += 1;
let answer = relay.reconcile(&out).unwrap();
answered += answer.len();
let (mut have, mut want) = (Vec::new(), Vec::new());
message = ours.reconcile_with_ids(&answer, &mut have, &mut want).unwrap();
lack.extend(have.iter().map(|id| EventId::from_byte_array(id.to_bytes())));
need.extend(want.iter().map(|id| EventId::from_byte_array(id.to_bytes())));
}
need.sort();
lack.sort();
(need, lack, rounds, (size, answered))
}
fn ids(wraps: &[(EventId, u64)]) -> Vec<EventId> {
let mut ids: Vec<EventId> = wraps.iter().map(|(id, _)| *id).collect();
ids.sort();
ids
}
#[test]
fn a_set_the_relay_holds_exactly_is_settled_by_one_fingerprint() {
for n in [1u16, 20, 400] {
let held = wraps(0..n);
assert_eq!(exchange(&held, &held), (vec![], vec![], 1, (20, 1)), "{n} wraps: twenty bytes out, one back");
}
}
#[test]
fn a_set_that_differs_names_exactly_what_each_side_lacks() {
for n in [5u16, 31, 32, 33, 400, 3_000] {
let held = wraps((0..n).filter(|i| i % 7 != 3));
let theirs = wraps((0..n).filter(|i| i % 11 != 5));
let (need, lack, ..) = exchange(&held, &theirs);
assert_eq!(need, ids(&wraps((0..n).filter(|i| i % 7 == 3 && i % 11 != 5))), "{n} wraps");
assert_eq!(lack, ids(&wraps((0..n).filter(|i| i % 11 == 5 && i % 7 != 3))), "{n} wraps");
}
let all = wraps(0..50);
assert_eq!(exchange(&[], &all).0, ids(&all));
assert_eq!(exchange(&all, &[]).1, ids(&all));
assert_eq!(exchange(&[], &[]).3 .0, 5);
}
#[test]
fn a_relay_without_nip77_is_known_by_its_notice() {
for notice in [
"ERROR: bad msg: unknown cmd",
"ERROR: bad msg: negentropy disabled",
"invalid: unknown message type NEG-OPEN",
"unrecognized message: NEG-OPEN",
"unsupported: NEG-OPEN",
] {
assert!(refuses(notice), "{notice}");
}
for notice in ["rate-limited: slow down", "ERROR: bad msg: invalid filter", "restricted: not on the allow list", "ERROR: too many open negentropy sessions", "invalid: negentropy rate limit"] {
assert!(!refuses(notice), "{notice}");
}
}
}