acks
This commit is contained in:
1 parent
10af480218
commit
47b73e6935
11 files changed
+470
-28
No files matched your search
@@ -44,7 +44,9 @@ use meow_crypto::password::KdfParams;
|
||||
use rusqlite::{Connection, OptionalExtension};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
pub use model::{Batch, Content, HistoryEntry, HistoryPage, OutboxItem, PendingAck, RetiredMailbox, SpaceId, State};
|
||||
pub use model::{
|
||||
Batch, Content, HistoryEntry, HistoryPage, OutboxItem, PendingAck, RetiredMailbox, Settings, SpaceId, State, Status,
|
||||
};
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum Error {
|
||||
@@ -131,6 +133,7 @@ struct Meta {
|
||||
servers: Vec<meow_core::server::ServerInfo>,
|
||||
pending_acks: Vec<PendingAck>,
|
||||
retired_mailboxes: Vec<RetiredMailbox>,
|
||||
settings: Settings,
|
||||
}
|
||||
|
||||
// ───────────────────────────── keys ─────────────────────────────
|
||||
@@ -448,6 +451,7 @@ impl Store {
|
||||
state.servers = m.servers;
|
||||
state.pending_acks = m.pending_acks;
|
||||
state.retired_mailboxes = m.retired_mailboxes;
|
||||
state.settings = m.settings;
|
||||
}
|
||||
for e in &sp.index.contacts {
|
||||
let bytes = blocks::get(&self.conn, &sp.key, &e.state)?;
|
||||
@@ -492,6 +496,7 @@ impl Store {
|
||||
servers: state.servers.clone(),
|
||||
pending_acks: state.pending_acks.clone(),
|
||||
retired_mailboxes: state.retired_mailboxes.clone(),
|
||||
settings: state.settings,
|
||||
})?;
|
||||
let h = meow_crypto::sha256(&[&meta]);
|
||||
if sp.written.get("meta") != Some(&h) || meta_ref.is_none() {
|
||||
@@ -564,21 +569,34 @@ impl Store {
|
||||
}
|
||||
|
||||
// History and files, per conversation.
|
||||
type Pending = (ContactId, Vec<HistoryEntry>, Vec<(Vec<u8>, Vec<u8>)>);
|
||||
let mut by_contact: Vec<Pending> = Vec::new();
|
||||
#[derive(Default)]
|
||||
struct Pending {
|
||||
history: Vec<HistoryEntry>,
|
||||
files: Vec<(Vec<u8>, Vec<u8>)>,
|
||||
status: Vec<(Vec<u8>, Status)>,
|
||||
}
|
||||
fn slot<'a>(v: &'a mut Vec<(ContactId, Pending)>, c: &ContactId) -> &'a mut Pending {
|
||||
let i = match v.iter().position(|(x, _)| x == c) {
|
||||
Some(i) => i,
|
||||
None => {
|
||||
v.push((c.clone(), Pending::default()));
|
||||
v.len() - 1
|
||||
}
|
||||
};
|
||||
&mut v[i].1
|
||||
}
|
||||
let mut by_contact: Vec<(ContactId, Pending)> = Vec::new();
|
||||
for h in batch.history {
|
||||
match by_contact.iter_mut().find(|(c, _, _)| *c == h.contact) {
|
||||
Some(slot) => slot.1.push(h),
|
||||
None => by_contact.push((h.contact.clone(), vec![h], vec![])),
|
||||
}
|
||||
let c = h.contact.clone();
|
||||
slot(&mut by_contact, &c).history.push(h);
|
||||
}
|
||||
for (c, mid, data) in batch.files {
|
||||
match by_contact.iter_mut().find(|(x, _, _)| *x == c) {
|
||||
Some(slot) => slot.2.push((mid, data)),
|
||||
None => by_contact.push((c, vec![], vec![(mid, data)])),
|
||||
}
|
||||
slot(&mut by_contact, &c).files.push((mid, data));
|
||||
}
|
||||
for (cid, history, files) in by_contact {
|
||||
for (c, mid, st) in batch.status {
|
||||
slot(&mut by_contact, &c).status.push((mid, st));
|
||||
}
|
||||
for (cid, Pending { history, files, status }) in by_contact {
|
||||
let entry = contacts_idx
|
||||
.iter_mut()
|
||||
.find(|e| e.id == cid)
|
||||
@@ -591,6 +609,7 @@ impl Store {
|
||||
dir.files.push((mid, r));
|
||||
}
|
||||
}
|
||||
update_status(&tx, &sp.key, &mut w, &mut dir, status)?;
|
||||
entry.dir = w.put_value(&dir, Some(&entry.dir))?;
|
||||
}
|
||||
|
||||
@@ -758,3 +777,35 @@ fn append_history(
|
||||
dir.pages.push(r);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Apply status changes, searching segments from the latest one (where nearly all
|
||||
/// updates land). Only rewritten segments change blocks.
|
||||
fn update_status(
|
||||
conn: &Connection,
|
||||
key: &Key,
|
||||
w: &mut Writer,
|
||||
dir: &mut Dir,
|
||||
mut updates: Vec<(Vec<u8>, Status)>,
|
||||
) -> Result<()> {
|
||||
for i in (0..dir.pages.len()).rev() {
|
||||
if updates.is_empty() {
|
||||
break;
|
||||
}
|
||||
let mut page: Vec<HistoryEntry> = blocks::get_value(conn, key, &dir.pages[i])?;
|
||||
let mut changed = false;
|
||||
updates.retain(|(mid, st)| match page.iter_mut().find(|e| e.id == *mid) {
|
||||
Some(e) => {
|
||||
if *st > e.status {
|
||||
e.status = *st;
|
||||
changed = true;
|
||||
}
|
||||
false
|
||||
}
|
||||
None => true,
|
||||
});
|
||||
if changed {
|
||||
dir.pages[i] = w.put_value(&page, Some(&dir.pages[i]))?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -15,6 +15,39 @@ pub struct HistoryEntry {
|
||||
pub sent_at_ms: u64,
|
||||
pub received_at: u64,
|
||||
pub content: Content,
|
||||
pub status: Status,
|
||||
}
|
||||
|
||||
/// Delivery state of a message. Only ever moves forward.
|
||||
///
|
||||
/// Outbound: `Queued` (no server took it yet) → `Sent` (a server accepted it) →
|
||||
/// `Delivered` (the contact's device processed it) → `Read`.
|
||||
/// Inbound: `Delivered` (unread) → `Read`.
|
||||
#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub enum Status {
|
||||
#[default]
|
||||
Queued,
|
||||
Sent,
|
||||
Delivered,
|
||||
Read,
|
||||
}
|
||||
|
||||
/// Per-identity preferences (main space).
|
||||
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)]
|
||||
pub struct Settings {
|
||||
/// Tell contacts when their messages reached this device (reveals when it is online).
|
||||
pub delivered_receipts: bool,
|
||||
/// Tell contacts when their messages were read.
|
||||
pub read_receipts: bool,
|
||||
}
|
||||
|
||||
impl Default for Settings {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
delivered_receipts: true,
|
||||
read_receipts: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Displayable content of a message.
|
||||
@@ -99,6 +132,7 @@ pub struct State {
|
||||
pub pending_acks: Vec<PendingAck>,
|
||||
pub retired_mailboxes: Vec<RetiredMailbox>,
|
||||
pub outbox: Vec<OutboxItem>,
|
||||
pub settings: Settings,
|
||||
}
|
||||
|
||||
/// Append-only data written in the same transaction as a [`State`].
|
||||
@@ -107,10 +141,13 @@ pub struct Batch {
|
||||
pub history: Vec<HistoryEntry>,
|
||||
/// Received file contents: (contact, message id, data).
|
||||
pub files: Vec<(ContactId, Vec<u8>, Vec<u8>)>,
|
||||
/// Status changes of stored messages: (contact, message id, new status). Ignored
|
||||
/// when the message is unknown or already further along.
|
||||
pub status: Vec<(ContactId, Vec<u8>, Status)>,
|
||||
}
|
||||
|
||||
impl Batch {
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.history.is_empty() && self.files.is_empty()
|
||||
self.history.is_empty() && self.files.is_empty() && self.status.is_empty()
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,7 @@ use meow_core::contact::Contact;
|
||||
use meow_core::server::{ServerInfo, ServerKeys};
|
||||
use meow_crypto::password::KdfParams;
|
||||
use meow_store::blocks::BLOCK_SIZE;
|
||||
use meow_store::{Batch, Content, Error, HistoryEntry, PendingAck, SpaceId, State, Store};
|
||||
use meow_store::{Batch, Content, Error, HistoryEntry, PendingAck, SpaceId, State, Status, Store};
|
||||
|
||||
const KDF: KdfParams = KdfParams::INSECURE_TEST;
|
||||
const MAIN: SpaceId = SpaceId::MAIN;
|
||||
@@ -40,6 +40,7 @@ fn text(c: &Contact, t: &str) -> HistoryEntry {
|
||||
sent_at_ms: 1,
|
||||
received_at: 2,
|
||||
content: Content::Text(t.into()),
|
||||
status: Status::Delivered,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -141,6 +142,7 @@ fn state_segments_and_files() {
|
||||
let later = Batch {
|
||||
history: vec![text(&bob, "last")],
|
||||
files: vec![],
|
||||
status: vec![],
|
||||
};
|
||||
store.save(MAIN, &state, later).unwrap();
|
||||
drop(store);
|
||||
@@ -206,12 +208,14 @@ fn hidden_spaces() {
|
||||
let batch = Batch {
|
||||
history: vec![text(&secret, "for your eyes only")],
|
||||
files: vec![],
|
||||
status: vec![],
|
||||
};
|
||||
store.save(h, &hidden, batch).unwrap();
|
||||
// A contact of another space cannot receive history here.
|
||||
let wrong = Batch {
|
||||
history: vec![text(&secret, "x")],
|
||||
files: vec![],
|
||||
status: vec![],
|
||||
};
|
||||
assert!(store.save(MAIN, &main, wrong).is_err());
|
||||
assert!(matches!(
|
||||
@@ -254,7 +258,8 @@ fn hidden_spaces() {
|
||||
fn an_attacker_without_password_sees_only_uniform_blocks() {
|
||||
let (path, dir) = tmp();
|
||||
let s = server();
|
||||
let bob = contact("bob", &s);
|
||||
// Needles must be long: short ones (e.g. 3 bytes) occur by chance in megabytes of random filler.
|
||||
let bob = contact("bob-the-contact-name", &s);
|
||||
let mut store = Store::create(&path, "alice-the-nickname", "pw", KDF, false).unwrap();
|
||||
let state = State {
|
||||
nickname: "alice-the-nickname".into(),
|
||||
@@ -265,6 +270,7 @@ fn an_attacker_without_password_sees_only_uniform_blocks() {
|
||||
let batch = Batch {
|
||||
history: vec![text(&bob, "very secret text one"), text(&bob, "very secret text two")],
|
||||
files: vec![],
|
||||
status: vec![],
|
||||
};
|
||||
store.save(MAIN, &state, batch).unwrap();
|
||||
let h = store.create_space("hidden").unwrap();
|
||||
@@ -293,7 +299,7 @@ fn an_attacker_without_password_sees_only_uniform_blocks() {
|
||||
let wal = std::fs::metadata(format!("{}-wal", path.display())).map_or(0, |m| m.len());
|
||||
assert_eq!(wal, 0);
|
||||
let raw = std::fs::read(&path).unwrap();
|
||||
for needle in ["alice-the-nickname", "very secret", "bob", "http://s"] {
|
||||
for needle in ["alice-the-nickname", "very secret text", "bob-the-contact-name"] {
|
||||
assert!(
|
||||
!raw.windows(needle.len()).any(|w| w == needle.as_bytes()),
|
||||
"{needle:?} found in clear"
|
||||
@@ -342,6 +348,7 @@ fn rewrites_move_objects_and_wipe_old_blocks() {
|
||||
Batch {
|
||||
history: vec![text(&bob, "hello")],
|
||||
files: vec![],
|
||||
status: vec![],
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
@@ -417,3 +424,57 @@ fn outbox_items_persist_per_space() {
|
||||
assert!(store.load(h2).unwrap().outbox.is_empty());
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn status_only_moves_forward_in_any_segment() {
|
||||
let (path, dir) = tmp();
|
||||
let s = server();
|
||||
let bob = contact("bob", &s);
|
||||
let mut store = Store::create(&path, "alice", "pw", KDF, false).unwrap();
|
||||
let mut state = store.load(MAIN).unwrap();
|
||||
state.contacts.push(bob.clone());
|
||||
let filler = "x".repeat(1000);
|
||||
let msgs: Vec<HistoryEntry> = (0..30)
|
||||
.map(|i| HistoryEntry {
|
||||
outbound: true,
|
||||
status: Status::Queued,
|
||||
..text(&bob, &format!("{i}{filler}"))
|
||||
})
|
||||
.collect();
|
||||
let (first, last) = (msgs[0].id.clone(), msgs[29].id.clone());
|
||||
store
|
||||
.save(
|
||||
MAIN,
|
||||
&state,
|
||||
Batch {
|
||||
history: msgs,
|
||||
..Batch::default()
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
// First message is in the oldest segment, last one in the latest.
|
||||
let upd = |v: Vec<(Vec<u8>, Status)>| Batch {
|
||||
status: v.into_iter().map(|(m, s)| (bob.id.clone(), m, s)).collect(),
|
||||
..Batch::default()
|
||||
};
|
||||
store
|
||||
.save(
|
||||
MAIN,
|
||||
&state,
|
||||
upd(vec![
|
||||
(first.clone(), Status::Read),
|
||||
(last.clone(), Status::Delivered),
|
||||
(b"unknown".to_vec(), Status::Read),
|
||||
]),
|
||||
)
|
||||
.unwrap();
|
||||
// A late, lower status does not go back.
|
||||
store
|
||||
.save(MAIN, &state, upd(vec![(first.clone(), Status::Sent)]))
|
||||
.unwrap();
|
||||
let all = store.full_history(MAIN, &bob.id).unwrap();
|
||||
assert_eq!(all[0].status, Status::Read);
|
||||
assert_eq!(all[29].status, Status::Delivered);
|
||||
assert!(all[1..29].iter().all(|e| e.status == Status::Queued));
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
Reference in new issue
Block a user