Merge branch 'main' into feat/tui-msgid-join
All checks were successful
ci / test (pull_request) Successful in 4m10s
All checks were successful
ci / test (pull_request) Successful in 4m10s
This commit is contained in:
commit
18ccab5464
16 changed files with 897 additions and 195 deletions
54
src/store.rs
54
src/store.rs
|
|
@ -1054,6 +1054,31 @@ impl Store {
|
|||
Ok(out)
|
||||
}
|
||||
|
||||
/// Fold the WAL into the main db file and truncate it.
|
||||
///
|
||||
/// Runs on this store's own keyed connection: a second, unkeyed connection
|
||||
/// cannot even read a SQLCipher store (`SQLITE_NOTADB`), so the checkpoint
|
||||
/// has to happen here, not from `node.rs`.
|
||||
///
|
||||
/// Best effort on purpose: WAL replay on open recovers everything committed,
|
||||
/// so this is about leaving less to replay (and less on disk) if the process
|
||||
/// is killed right after teardown — not about correctness.
|
||||
pub fn checkpoint_wal(&self) -> Result<()> {
|
||||
let (busy, log, checkpointed): (i64, i64, i64) =
|
||||
self.conn
|
||||
.query_row("PRAGMA wal_checkpoint(TRUNCATE)", [], |row| {
|
||||
Ok((row.get(0)?, row.get(1)?, row.get(2)?))
|
||||
})?;
|
||||
if busy != 0 {
|
||||
// Another connection is reading/writing: pages are still safe in the
|
||||
// WAL, they just were not moved this time.
|
||||
return Err(Error(format!(
|
||||
"busy ({checkpointed}/{log} pages moved, another connection active)"
|
||||
)));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Drop chat + payments history. Identity + friends stay.
|
||||
/// Not a forensic erase: SSD wear-leveling can keep copies.
|
||||
pub fn wipe_messages(&self) -> Result<()> {
|
||||
|
|
@ -1065,7 +1090,7 @@ impl Store {
|
|||
self.conn.execute("DELETE FROM messages", [])?;
|
||||
self.conn.execute("DELETE FROM payments", [])?;
|
||||
self.conn.execute_batch("VACUUM")?;
|
||||
self.conn.execute_batch("PRAGMA wal_checkpoint(TRUNCATE)")?;
|
||||
self.checkpoint_wal()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
@ -1379,6 +1404,33 @@ mod at_rest {
|
|||
use super::*;
|
||||
use zeroize::Zeroize;
|
||||
|
||||
/// `Node::shutdown`/`flush` checkpoint the WAL through this method, so it has
|
||||
/// to run on the store's own keyed connection: a second, unkeyed one cannot
|
||||
/// read a SQLCipher file at all (`SQLITE_NOTADB`) and the truncation silently
|
||||
/// never happened.
|
||||
#[test]
|
||||
fn checkpoint_wal_truncates_an_encrypted_store_wal() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let store = Store::open_at_with_passphrase(dir.path(), "checkpoint-pass").unwrap();
|
||||
let pk = [3u8; 32];
|
||||
store.upsert_friend(&pk, "peer.onion", None).unwrap();
|
||||
for _ in 0..64 {
|
||||
store.append_message(&pk, "out", &[7u8; 512]).unwrap();
|
||||
}
|
||||
|
||||
let wal = dir.path().join("onionwire.db-wal");
|
||||
let before = std::fs::metadata(&wal).expect("wal file").len();
|
||||
assert!(before > 0, "expected pending WAL frames, got {before}");
|
||||
|
||||
store.checkpoint_wal().expect("checkpoint");
|
||||
|
||||
let after = std::fs::metadata(&wal).map(|m| m.len()).unwrap_or(0);
|
||||
assert_eq!(after, 0, "WAL not truncated ({before} bytes before)");
|
||||
|
||||
// A checkpoint moves pages into the db; it never drops committed data.
|
||||
assert_eq!(store.list_messages(&pk).unwrap().len(), 64);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn store_data_key_is_a_zeroizing_wrapper_that_wipes() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
|
|
|
|||
Loading…
Reference in a new issue