send queue
This commit is contained in:
1 parent
a520ddb557
commit
10af480218
10 files changed
+701
-60
No files matched your search
@@ -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(())
|
||||
}
|
||||
|
||||
@@ -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`].
|
||||
|
||||
Reference in new issue
Block a user