[relay] Wake-up: a push endpoint per mailbox, and a constant wake to it when a blob arrives
ci / check (pull_request) Has been cancelled
ci / check (pull_request) Has been cancelled
This commit is contained in:
@@ -32,8 +32,8 @@ use sha2::Sha256;
|
||||
use snapshot::Bytes;
|
||||
use std::fmt;
|
||||
use tabellarius_protocol::{
|
||||
blob_hash, CreateInvite, CreateMailbox, InviteId, KeyPackageBytes, MailboxId, Sha256Hash,
|
||||
Token, MAX_BLOB_BYTES,
|
||||
blob_hash, CreateInvite, CreateMailbox, InviteId, KeyPackageBytes, MailboxId, PushEndpoint,
|
||||
Sha256Hash, Token, MAX_BLOB_BYTES,
|
||||
};
|
||||
pub use transport::{MemoryTransport, Transport, TransportError};
|
||||
|
||||
@@ -523,6 +523,35 @@ impl<T: Transport> Client<T> {
|
||||
out.clone()
|
||||
}
|
||||
|
||||
/// The mailboxes this client fetches: each contact's inbox (a blocked contact's too, which
|
||||
/// nothing reaches) and each open invite's welcome mailbox. For wake-ups (`PROTOCOL.md` §6): an
|
||||
/// app registers one push endpoint per mailbox, so the relay cannot link them by endpoint.
|
||||
pub fn inboxes(&self) -> Vec<MailboxId> {
|
||||
self.contacts
|
||||
.iter()
|
||||
.filter_map(|c| c.inbox.map(|m| m.id))
|
||||
.chain(self.welcome_boxes.iter().map(|w| w.mailbox.id))
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Where the relay wakes us when this mailbox receives (`Some`), or nowhere (`None`). Only a
|
||||
/// mailbox from [`Client::inboxes`]; another is [`ClientError::UnknownContact`].
|
||||
pub fn set_push(
|
||||
&self,
|
||||
mailbox: &MailboxId,
|
||||
endpoint: Option<&PushEndpoint>,
|
||||
) -> Result<(), ClientError> {
|
||||
let fetch = self
|
||||
.contacts
|
||||
.iter()
|
||||
.filter_map(|c| c.inbox)
|
||||
.chain(self.welcome_boxes.iter().map(|w| w.mailbox))
|
||||
.find(|m| m.id == *mailbox)
|
||||
.ok_or(ClientError::UnknownContact)?
|
||||
.fetch;
|
||||
Ok(self.transport.set_push(mailbox, &fetch, endpoint)?)
|
||||
}
|
||||
|
||||
/// Blocks a contact, staged: they are marked, and their way into our mailbox is closed by a
|
||||
/// rotate to the hash of a fresh token that nobody gets. Nothing is sent to them. Save the
|
||||
/// snapshot, then deliver the result.
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
use std::collections::HashMap;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use tabellarius_protocol::{
|
||||
blob_hash, CreateInvite, CreateMailbox, InviteId, MailboxId, Sha256Hash, Token,
|
||||
blob_hash, CreateInvite, CreateMailbox, InviteId, MailboxId, PushEndpoint, Sha256Hash, Token,
|
||||
};
|
||||
|
||||
/// Why the relay said no. Carries nothing of the request.
|
||||
@@ -41,12 +41,21 @@ pub trait Transport {
|
||||
) -> Result<(), TransportError>;
|
||||
/// The KeyPackage, once.
|
||||
fn claim_invite(&self, id: &InviteId, secret: &Token) -> Result<Vec<u8>, TransportError>;
|
||||
/// Where the relay wakes the mailbox's owner when a blob arrives (`Some`), or nowhere (`None`).
|
||||
/// `PROTOCOL.md` §6, `.agents/features/wake-up.md`.
|
||||
fn set_push(
|
||||
&self,
|
||||
id: &MailboxId,
|
||||
fetch: &Token,
|
||||
endpoint: Option<&PushEndpoint>,
|
||||
) -> Result<(), TransportError>;
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct Boxes {
|
||||
mailboxes: HashMap<MailboxId, (Sha256Hash, Sha256Hash, Vec<Vec<u8>>)>,
|
||||
invites: HashMap<InviteId, (Sha256Hash, Vec<u8>)>,
|
||||
push: HashMap<MailboxId, PushEndpoint>,
|
||||
}
|
||||
|
||||
/// An in-memory relay with the same rules as the real one (no expiry: there is no clock here).
|
||||
@@ -68,7 +77,12 @@ impl MemoryTransport {
|
||||
.sum()
|
||||
}
|
||||
|
||||
/// Every byte the relay holds, for privacy tests: ids, hashes, blobs and KeyPackages.
|
||||
/// The push endpoint a mailbox holds, if any.
|
||||
pub fn push_endpoint(&self, id: &MailboxId) -> Option<String> {
|
||||
self.boxes().push.get(id).map(|e| e.as_str().to_string())
|
||||
}
|
||||
|
||||
/// Every byte the relay holds, for privacy tests: ids, hashes, blobs, KeyPackages, endpoints.
|
||||
pub fn everything_stored(&self) -> Vec<u8> {
|
||||
let b = self.boxes();
|
||||
let mut out = Vec::new();
|
||||
@@ -83,6 +97,10 @@ impl MemoryTransport {
|
||||
out.extend_from_slice(hash.as_bytes());
|
||||
out.extend_from_slice(kp);
|
||||
}
|
||||
for (id, endpoint) in &b.push {
|
||||
out.extend_from_slice(id.as_bytes());
|
||||
out.extend_from_slice(endpoint.as_str().as_bytes());
|
||||
}
|
||||
out
|
||||
}
|
||||
}
|
||||
@@ -172,4 +190,22 @@ impl Transport for MemoryTransport {
|
||||
_ => Err(TransportError::NotFound),
|
||||
}
|
||||
}
|
||||
|
||||
fn set_push(
|
||||
&self,
|
||||
id: &MailboxId,
|
||||
fetch: &Token,
|
||||
endpoint: Option<&PushEndpoint>,
|
||||
) -> Result<(), TransportError> {
|
||||
let mut b = self.boxes();
|
||||
match b.mailboxes.get(id) {
|
||||
Some((_, f, _)) if *f == fetch.hash() => {}
|
||||
_ => return Err(TransportError::NotFound),
|
||||
}
|
||||
match endpoint {
|
||||
Some(e) => b.push.insert(*id, e.clone()),
|
||||
None => b.push.remove(id),
|
||||
};
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
//! Wake-up (`.agents/features/wake-up.md` §2.4) on the in-memory relay: which mailboxes a client
|
||||
//! fetches, and an endpoint per mailbox, set only with that mailbox's fetch token.
|
||||
|
||||
use tabellarius_client::*;
|
||||
use tabellarius_protocol::PushEndpoint;
|
||||
|
||||
fn endpoint(path: &str) -> PushEndpoint {
|
||||
PushEndpoint::parse(&format!("https://push.invalid/{path}")).expect("endpoint")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inboxes_are_the_open_welcome_mailboxes_then_each_contacts_inbox() {
|
||||
let relay = MemoryTransport::default();
|
||||
let mut eva = Client::new("Eva", relay.clone()).expect("eva");
|
||||
let mut jan = Client::new("Jan", relay.clone()).expect("jan");
|
||||
assert!(eva.inboxes().is_empty());
|
||||
let invite = eva.create_invite().expect("invite");
|
||||
let welcome = eva.inboxes();
|
||||
assert_eq!(
|
||||
welcome.len(),
|
||||
1,
|
||||
"the welcome mailbox, while the invite is open"
|
||||
);
|
||||
// Someone who joins wakes the inviter through it.
|
||||
eva.set_push(&welcome[0], Some(&endpoint("upWelcome")))
|
||||
.expect("set");
|
||||
assert_eq!(
|
||||
relay.push_endpoint(&welcome[0]).as_deref(),
|
||||
Some("https://push.invalid/upWelcome")
|
||||
);
|
||||
jan.accept_invite(&invite).expect("accept");
|
||||
assert_eq!(jan.inboxes().len(), 1);
|
||||
eva.poll().expect("poll");
|
||||
// Joined: the welcome mailbox is dropped, Jan's inbox at Eva's is there.
|
||||
let after = eva.inboxes();
|
||||
assert_eq!(after.len(), 1);
|
||||
assert_ne!(after[0], welcome[0]);
|
||||
// Every mailbox is different: one endpoint each never links them.
|
||||
let mut all = eva.inboxes();
|
||||
all.extend(jan.inboxes());
|
||||
all.sort_by_key(|m| m.to_wire());
|
||||
all.dedup();
|
||||
assert_eq!(all.len(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn only_a_mailbox_of_ones_own_takes_an_endpoint_and_clearing_removes_it() {
|
||||
let relay = MemoryTransport::default();
|
||||
let mut eva = Client::new("Eva", relay.clone()).expect("eva");
|
||||
let mut jan = Client::new("Jan", relay.clone()).expect("jan");
|
||||
let invite = eva.create_invite().expect("invite");
|
||||
jan.accept_invite(&invite).expect("accept");
|
||||
eva.poll().expect("poll");
|
||||
let (mine, his) = (eva.inboxes()[0], jan.inboxes()[0]);
|
||||
assert_eq!(
|
||||
eva.set_push(&his, Some(&endpoint("upX"))),
|
||||
Err(ClientError::UnknownContact)
|
||||
);
|
||||
assert_eq!(relay.push_endpoint(&his), None);
|
||||
eva.set_push(&mine, Some(&endpoint("upEva"))).expect("set");
|
||||
eva.set_push(&mine, Some(&endpoint("upEva2")))
|
||||
.expect("replace");
|
||||
assert_eq!(
|
||||
relay.push_endpoint(&mine).as_deref(),
|
||||
Some("https://push.invalid/upEva2")
|
||||
);
|
||||
eva.set_push(&mine, None).expect("clear");
|
||||
assert_eq!(relay.push_endpoint(&mine), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_blocked_contacts_inbox_is_still_listed() {
|
||||
let relay = MemoryTransport::default();
|
||||
let mut eva = Client::new("Eva", relay.clone()).expect("eva");
|
||||
let mut jan = Client::new("Jan", relay.clone()).expect("jan");
|
||||
let invite = eva.create_invite().expect("invite");
|
||||
let jan_side = jan.accept_invite(&invite).expect("accept");
|
||||
eva.poll().expect("poll");
|
||||
let out = jan.block(jan_side).expect("block");
|
||||
jan.deliver(&out).expect("deliver");
|
||||
// Listed, so turning wake-ups off clears it too; nothing reaches it to wake anyone.
|
||||
assert_eq!(jan.inboxes().len(), 1);
|
||||
}
|
||||
@@ -129,13 +129,32 @@ impl Transport for Http {
|
||||
serde_json::from_slice(&body).map_err(|_| TransportError::Failed)?;
|
||||
Ok(claimed.key_package.bytes().to_vec())
|
||||
}
|
||||
fn set_push(
|
||||
&self,
|
||||
id: &MailboxId,
|
||||
fetch: &Token,
|
||||
endpoint: Option<&PushEndpoint>,
|
||||
) -> Result<(), TransportError> {
|
||||
let path = format!("/v0/mailboxes/{}/push", id.to_wire());
|
||||
match endpoint {
|
||||
Some(e) => {
|
||||
let body = serde_json::to_vec(&SetPush {
|
||||
endpoint: e.clone(),
|
||||
})
|
||||
.map_err(|_| TransportError::Failed)?;
|
||||
self.call("PUT", &path, Some(fetch), &body, true)
|
||||
}
|
||||
None => self.call("DELETE", &path, Some(fetch), &[], false),
|
||||
}
|
||||
.map(|_| ())
|
||||
}
|
||||
}
|
||||
|
||||
/// Starts the real relay on 127.0.0.1:<free port> with its database in `db`.
|
||||
fn start_relay(db: &std::path::Path) -> SocketAddr {
|
||||
let store = Arc::new(Store::open(db).expect("store"));
|
||||
let clock: Clock = Arc::new(|| 1_794_000_000);
|
||||
let app = router(AppState { store, clock });
|
||||
let app = router(AppState::new(store, clock));
|
||||
let (tx, rx) = std::sync::mpsc::channel();
|
||||
std::thread::spawn(move || {
|
||||
let rt = tokio::runtime::Builder::new_current_thread()
|
||||
@@ -222,3 +241,37 @@ fn pair_and_talk_through_a_real_relay_which_never_sees_a_name_or_a_word() {
|
||||
// The messages were there, as ciphertext: the database grew while they waited.
|
||||
assert!(waiting.len() >= 4096, "the relay stored something");
|
||||
}
|
||||
|
||||
// Wake-up (`.agents/features/wake-up.md` §2.4): an endpoint per mailbox, set and cleared through the
|
||||
// real relay with the mailbox's fetch token.
|
||||
#[test]
|
||||
fn each_inbox_takes_its_own_push_endpoint_through_the_real_relay() {
|
||||
let dir = tempfile::tempdir().expect("tempdir");
|
||||
let db = dir.path().join("relay.db");
|
||||
let relay = Http(start_relay(&db));
|
||||
let mut eva = Client::new("Eva", relay.clone()).expect("eva");
|
||||
let mut jan = Client::new("Jan", relay.clone()).expect("jan");
|
||||
let invite = eva.create_invite().expect("invite");
|
||||
jan.accept_invite(&invite).expect("accept");
|
||||
eva.poll().expect("poll");
|
||||
let (eva_boxes, jan_boxes) = (eva.inboxes(), jan.inboxes());
|
||||
assert_eq!((eva_boxes.len(), jan_boxes.len()), (1, 1));
|
||||
let endpoint = |path: &str| PushEndpoint::parse(&format!("https://push.invalid/{path}"));
|
||||
let a = endpoint("upEvaOnly1").expect("endpoint");
|
||||
eva.set_push(&eva_boxes[0], Some(&a)).expect("set");
|
||||
jan.set_push(
|
||||
&jan_boxes[0],
|
||||
Some(&endpoint("upJanOnly2").expect("endpoint")),
|
||||
)
|
||||
.expect("set");
|
||||
let stored = std::fs::read(&db).expect("db");
|
||||
assert!(contains(&stored, b"upEvaOnly1") && contains(&stored, b"upJanOnly2"));
|
||||
// Not Jan's mailbox: Eva cannot set it.
|
||||
assert_eq!(
|
||||
eva.set_push(&jan_boxes[0], Some(&a)),
|
||||
Err(ClientError::UnknownContact)
|
||||
);
|
||||
eva.set_push(&eva_boxes[0], None).expect("clear");
|
||||
// Nothing is posted after this: a post would make the relay try to wake `push.invalid`, and no
|
||||
// test reaches for the network.
|
||||
}
|
||||
|
||||
@@ -323,6 +323,14 @@ impl Transport for Flaky {
|
||||
fn create_invite(&self, r: &tabellarius_protocol::CreateInvite) -> Result<(), TransportError> {
|
||||
self.inner.create_invite(r)
|
||||
}
|
||||
fn set_push(
|
||||
&self,
|
||||
id: &tabellarius_protocol::MailboxId,
|
||||
fetch: &tabellarius_protocol::Token,
|
||||
endpoint: Option<&tabellarius_protocol::PushEndpoint>,
|
||||
) -> Result<(), TransportError> {
|
||||
self.inner.set_push(id, fetch, endpoint)
|
||||
}
|
||||
fn rotate_post_token(
|
||||
&self,
|
||||
id: &tabellarius_protocol::MailboxId,
|
||||
|
||||
Reference in New Issue
Block a user