send queue

This commit is contained in:
yves committed 2026-11-20 23:09:05 +01:00
1 parent a520ddb557
commit 10af480218
10 files changed
+701 -60

No files matched your search

+45 -3
View File
@@ -44,7 +44,7 @@ use meow_crypto::password::KdfParams;
use rusqlite::{Connection, OptionalExtension};
use serde::{Deserialize, Serialize};
pub use model::{Batch, Content, HistoryEntry, HistoryPage, PendingAck, RetiredMailbox, SpaceId, State};
pub use model::{Batch, Content, HistoryEntry, HistoryPage, OutboxItem, PendingAck, RetiredMailbox, SpaceId, State};
#[derive(Debug, thiserror::Error)]
pub enum Error {
@@ -101,9 +101,15 @@ struct Root {
struct Index {
meta: Option<ObjectRef>,
contacts: Vec<ContactEntry>,
outbox: Vec<(Vec<u8>, ObjectRef)>,
free: Vec<BlockId>,
}
fn outbox_key(id: &[u8]) -> String {
let hex: String = id.iter().map(|b| format!("{b:02x}")).collect();
format!("outbox/{hex}")
}
#[derive(Clone, Serialize, Deserialize)]
struct ContactEntry {
id: ContactId,
@@ -453,6 +459,13 @@ impl Store {
}
state.contacts.push(c);
}
for (id, r) in &sp.index.outbox {
let bytes = blocks::get(&self.conn, &sp.key, r)?;
sp.written.insert(outbox_key(id), meow_crypto::sha256(&[&bytes]));
let item: model::OutboxItem =
postcard::from_bytes(&bytes).map_err(|e| Error::Corrupted(format!("outbox: {e}")))?;
state.outbox.push(item);
}
Ok(state)
}
@@ -466,6 +479,7 @@ impl Store {
let new_index_ref;
let new_free;
let new_contacts;
let new_outbox;
let new_meta;
{
let mut w = Writer::new(&tx, &sp.key, sp.index.free.clone());
@@ -527,6 +541,28 @@ impl Store {
}
contacts_idx = entries;
// Send queue: one object per item, rewritten only when it changed.
let mut outbox_idx = Vec::with_capacity(state.outbox.len());
for (id, r) in &sp.index.outbox {
if !state.outbox.iter().any(|o| o.id == *id) {
w.release(r);
}
}
for item in &state.outbox {
let bytes = encode(item)?;
let h = meow_crypto::sha256(&[&bytes]);
let key = outbox_key(&item.id);
let old = sp.index.outbox.iter().find(|(id, _)| *id == item.id).map(|(_, r)| r);
let r = match old {
Some(r) if sp.written.get(&key) == Some(&h) => r.clone(),
old => {
written.push((key, h));
w.put(&bytes, old)?
}
};
outbox_idx.push((item.id.clone(), r));
}
// History and files, per conversation.
type Pending = (ContactId, Vec<HistoryEntry>, Vec<(Vec<u8>, Vec<u8>)>);
let mut by_contact: Vec<Pending> = Vec::new();
@@ -565,6 +601,7 @@ impl Store {
encode(&Index {
meta: meta_ref.clone(),
contacts: contacts_idx.clone(),
outbox: outbox_idx.clone(),
free: free.to_vec(),
})
})?;
@@ -581,6 +618,7 @@ impl Store {
new_index_ref = r;
new_free = w.free;
new_contacts = contacts_idx;
new_outbox = outbox_idx;
new_meta = meta_ref;
}
tx.commit()?;
@@ -590,10 +628,14 @@ impl Store {
sp.index = Index {
meta: new_meta,
contacts: new_contacts,
outbox: new_outbox,
free: new_free,
};
sp.written
.retain(|k, _| k == "meta" || state.contacts.iter().any(|c| *k == format!("contact/{}", c.id.0)));
sp.written.retain(|k, _| {
k == "meta"
|| state.contacts.iter().any(|c| *k == format!("contact/{}", c.id.0))
|| state.outbox.iter().any(|o| *k == outbox_key(&o.id))
});
sp.written.extend(written);
Ok(())
}
+24
View File
@@ -65,6 +65,29 @@ pub struct RetiredMailbox {
pub servers: Vec<ServerInfo>,
}
/// A deposit waiting to be accepted by servers (persistent send queue).
///
/// Written in the same save as the ratchet state that produced it, so a message is
/// never encrypted without being stored for (re)delivery.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct OutboxItem {
/// Random queue id (16 bytes).
pub id: Vec<u8>,
pub contact: ContactId,
/// Id of the user message, empty for handshake messages.
pub message_id: Vec<u8>,
/// Encoded `meow.v2.Deposit`.
pub deposit: Vec<u8>,
/// Servers that have not accepted the deposit yet.
pub pending: Vec<ServerInfo>,
/// Servers that accepted it.
pub accepted: u32,
pub attempts: u32,
pub created_at: u64,
/// Unix time before which no new attempt is made.
pub next_attempt: u64,
}
/// What the client keeps in memory for one space. Saved as a whole by
/// [`crate::Store::save`]; unchanged objects are not rewritten. `nickname` and
/// `servers` are only meaningful in the main space.
@@ -75,6 +98,7 @@ pub struct State {
pub contacts: Vec<Contact>,
pub pending_acks: Vec<PendingAck>,
pub retired_mailboxes: Vec<RetiredMailbox>,
pub outbox: Vec<OutboxItem>,
}
/// Append-only data written in the same transaction as a [`State`].
+46
View File
@@ -371,3 +371,49 @@ fn schema_versions_and_foreign_files() {
));
let _ = std::fs::remove_dir_all(dir);
}
#[test]
fn outbox_items_persist_per_space() {
let (path, dir) = tmp();
let s = server();
let bob = contact("bob", &s);
let item = |n: u8, data: Vec<u8>| meow_store::OutboxItem {
id: vec![n; 16],
contact: bob.id.clone(),
message_id: vec![n; 16],
deposit: data,
pending: vec![s.clone()],
accepted: 0,
attempts: 0,
created_at: 1,
next_attempt: 1,
};
let mut store = Store::create(&path, "alice", "pw", KDF, false).unwrap();
let mut state = store.load(MAIN).unwrap();
state.contacts.push(bob.clone());
// A large deposit spans several blocks.
state.outbox = vec![item(1, vec![7; 10_000]), item(2, b"small".to_vec())];
store.save(MAIN, &state, Batch::default()).unwrap();
let h = store.create_space("hidden").unwrap();
drop(store);
let mut store = Store::open(&path, "pw").unwrap();
let mut loaded = store.load(MAIN).unwrap();
assert_eq!(loaded.outbox, state.outbox);
// Update one, drop the other.
loaded.outbox.retain(|o| o.id[0] == 1);
loaded.outbox[0].attempts = 3;
store.save(MAIN, &loaded, Batch::default()).unwrap();
drop(store);
let mut store = Store::open(&path, "pw").unwrap();
let again = store.load(MAIN).unwrap();
assert_eq!(again.outbox.len(), 1);
assert_eq!(again.outbox[0].attempts, 3);
assert_eq!(again.outbox[0].deposit, vec![7; 10_000]);
// Other spaces have their own queue.
let h2 = store.unlock("hidden").unwrap().unwrap();
assert_eq!(h2, h);
assert!(store.load(h2).unwrap().outbox.is_empty());
let _ = std::fs::remove_dir_all(dir);
}