store: persist per-peer ratchet session state in the SQLCipher store #86
Loading…
Reference in a new issue
No description provided.
Delete branch "wt/t_657d57c1"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
What
Persistent per-peer Double Ratchet state, so a chat session survives a restart. Follows on from #85 (merged as
5a36338), which shippedsrc/ratchet.rs.New additive table in the schema block of
src/store.rs:Keyed by
friend_idlikeoutbox, not by raw pubkey.Store::load_ratchet(&self, friend_pk) -> Result<Option<Vec<u8>>>—Ok(None)means no session yet, not an error.Store::save_ratchet(&self, friend_pk, state) -> Result<()>— upsert, stampsupdated_at.Store::clear_ratchet(&self, friend_pk) -> Result<()>— prekey change / explicit reset; a missing row is a no-op.The blob is the opaque, versioned
RatchetState::to_bytesoutput. The store does not parse, re-serialize or encrypt it (that format belongs tosrc/ratchet.rs, and the db is SQLCipher-encrypted wholesale); the new API mirrors the existing outbox helpers and uses the sameError/Result.Friend deletion: which statement deletes the ratchet row
DELETE FROM friends WHERE pubkey = ?1— the ratchet row goes via theON DELETE CASCADEforeign key; no explicit delete was needed. The test assertsPRAGMA foreign_keysis 1 first, and I confirmed the test has teeth by building the table without the cascade clause: the delete then failed withFOREIGN KEY constraint failed(extended_code 787) and the test failed. It passes onceON DELETE CASCADEis in place.wipe_messagesdoes not touch ratchet state, and the method carries a comment explaining why: wiping chat history is not a session reset, and resetting would break decryption against a peer that still holds the old state. The cascade test asserts the row surviveswipe_messagesand then disappears with the friend.Tests
tests/store.rs(new, public API only):ratchet_save_load_round_trips_exact_bytes— exact bytes back, and the blob is not in the db files in the clear after drop.ratchet_load_for_friend_without_a_row_is_none—Ok(None)for a known friend with no row and for an unknown peer.ratchet_sessions_are_per_friend— two friends, independent blobs; clearing one leaves the other.ratchet_clear_on_missing_row_is_a_no_opratchet_state_survives_reopening_the_store— save, drop, reopen the same path, load.src/store.rs,mod at_rest(needs the keyed connection / private field view):removing_a_friend_cascades_to_its_ratchet_row_and_wipe_messages_keeps_it(cascade + wipe).ratchet_save_overwrites_the_row_and_bumps_updated_at— the row count stays 1 andupdated_atis rewritten (clock parked at 0 first, so the assertion cannot pass on an untouched row).store_predating_the_ratchet_table_gains_it_on_reopen— a store written with the table dropped gains it fromCREATE TABLE IF NOT EXISTSon the next open, with message history intact and the ratchet API usable.RED/GREEN: the first run of the new tests failed to compile with
error[E0599]: no method named save_ratchet/load_ratchet/clear_ratchet found for struct Storein both test targets; the cascade test then failed for real (FOREIGN KEY constraint failed) before theON DELETE CASCADEclause was added, then passed.Deviations from the card (and why)
tests/store.rs. Three tests need the store's keyed connection (privateconn) or the rawDELETE FROM friendsstatement, because the crate has no public friend-delete API and no public way to readupdated_at—git grep -n 'DELETE FROM friends' src/is empty. Those three live in the existing#[cfg(test)] mod at_restinsrc/store.rs(precedent: the WAL-checkpoint, zeroize and legacy-fixture tests are already there). The five behaviours expressible through the public API are intests/store.rs, sopubvisibility of the new methods is exercised from outside the crate.CREATE TABLE friendsblock intests/store.rs; the only fixture isLEGACY_SCHEMAinsrc/store.rs(mod at_rest), documented as "the exact v0.2.1 on-disk layout … pin it here so the migration test cannot drift with the production schema". It already omitsoutbox, which postdates v0.2.1. Adding a post-v0.2.1 table to it would falsify the pin, so I left it byte-identical and covered the upgrade path with test 8 instead (build a db,DROP TABLE ratchet_sessions, reopen, use the API).src/ratchet.rschange.to_bytes/from_bytesalready exist there (v1 layout); this card only stores the bytes.Verification
cargo test --locked --target x86_64-unknown-linux-gnu: 266 passed; 0 failed (was 258; +8). The 4#[ignore = "needs live Tor network"]tests are unchanged.cargo clippy --locked --all-targets --target x86_64-unknown-linux-gnu -- -D warnings: clean, exit 0.rustfmt --edition 2024 --check:tests/store.rsclean;src/store.rsreports the same 8 inherited hunks asHEAD(that file was not rustfmt-clean before this branch) and no new ones.src/store.rsandtests/store.rs; no wire-protocol change (src/node.rs/src/dispatch.rsuntouched);Cargo.toml/Cargo.lockuntouched.