Files
wanderbooks/crates/ledger-package/src/books.rs
T
swaitsandClaude Fable 5.2 d4c0036599 fix(store,gui,package): every reader that derives facts reads the committed journal (P15 T1 review)
A failed call's ids are handed out again (nothing it wrote counted), so a
reader of the raw journal pins dead lines on whatever takes the id next: a
failed books.create_bill for entry 42 then a manual entry 42 showed as a
Bill. One accessor, calls::committed (lines outside a call and of calls
that closed ok, the frame left out), now feeds the viewer's books (entry
events and origin, asset trails, party events), the package feed attention
reads, and the adopt preview's settings check. The journal page and
journal_history keep every line and say in words of each line of a call
that failed, never finished or is still running that it did not count.

A call's closing line is now bounded (ANSWER_CAP 4 KiB: above it, the
answer's SHA-256 and length), a call that wrote no line leaves no line (its
key and answer live in a bounded in-memory table), and the journaled
argument digest is an HMAC under the books' own call key, so two requests
that differ only in a tax id are two requests and nothing journaled lets a
guess of one be tested.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-21 10:09:17 -07:00

177 lines
6.1 KiB
Rust

//! [`PackageBooks`]: what the package reads of the books, from the
//! server's [`Store`] or the viewer's [`Snapshot`] alike.
//!
//! ```text
//! GOAL: one package builder over either view of the books, and no way
//! for it to write to them.
//! SOURCE: ledger_store::snapshot (a Snapshot reads attachments and their
//! bytes "the same as through Store").
//! INVARIANTS:
//! 1. Every method only reads.
//! 2. A Store and a Snapshot of the same journal answer every method
//! alike: the same ledger, settings, committed journal, journal seq,
//! attachments (withdrawn ones included), bytes, statements, and
//! reconciliations, and the same entry links once the filing entry
//! is added (a store lists it in `links_of`; a snapshot only lists
//! the explicit links, so callers take the union with
//! `all_attachments`, as `build_package` and the attention module
//! do).
//! ```
use ledger_core::Ledger;
use ledger_store::{
Attachment, DiskCheck, Extraction, JournalLine, Reconciliation, Snapshot, Statement, Store,
StoreError, Subject,
};
/// The books, read-only, as the package reads them.
pub trait PackageBooks {
/// The ledger every figure comes from.
fn ledger(&self) -> &Ledger;
/// The settings journaled in the books (the same ones the server and
/// the viewer read).
fn settings(&self) -> &ledger_core::settings::Settings {
self.ledger().settings()
}
/// Sequence number of the last journal line: the journal state the
/// package reflects.
fn journal_seq(&self) -> u64;
/// The committed journal, in order: what an entry's origin and its
/// events are read from. Only the lines that are facts of the books
/// (ledger_store::calls::committed): the lines of a call that failed or
/// never finished are not here, because ids are handed out again after
/// a failed call and a reader that took them would pin dead lines on
/// whatever takes the id next (WB-format-12).
fn journal(&self) -> Result<Vec<JournalLine>, StoreError>;
/// Every attachment (receipts and documents), withdrawn ones
/// included, in id order.
fn all_attachments(&self) -> Result<Vec<Attachment>, StoreError>;
/// Every extraction record, in seq order (plan P6): what reading each
/// file found.
fn extractions(&self) -> Result<Vec<Extraction>, StoreError>;
/// The subjects attachment `attachment_id` is linked to beyond the
/// entry it is filed on (an implementation may list that entry too:
/// the package takes the union).
fn links_of(&self, attachment_id: u64) -> Result<Vec<Subject>, StoreError>;
/// An attachment's bytes, verified against its hash.
fn attachment_bytes(&self, attachment_id: u64) -> Result<Vec<u8>, StoreError>;
/// Recorded bank and card statements, by period end.
fn statements(&self) -> Result<Vec<Statement>, StoreError>;
/// Reconciliations, voided ones included, by date then id.
fn reconciliations(&self) -> Result<Vec<Reconciliation>, StoreError>;
/// How the writer's database compares with the journal. A snapshot
/// compares them; the store makes the same comparison (see its
/// implementation).
fn disk_check(&self) -> DiskCheck {
DiskCheck::Matches
}
}
impl PackageBooks for Store {
fn ledger(&self) -> &Ledger {
Store::ledger(self)
}
fn journal_seq(&self) -> u64 {
Store::journal_seq(self)
}
fn journal(&self) -> Result<Vec<JournalLine>, StoreError> {
let lines = ledger_store::journal::read_and_verify_journal(
&Store::data_dir(self).join(ledger_store::JOURNAL_FILE),
)?;
ledger_store::calls::committed(&lines)
}
fn all_attachments(&self) -> Result<Vec<Attachment>, StoreError> {
Store::list_attachments(self, None)
}
fn extractions(&self) -> Result<Vec<Extraction>, StoreError> {
Store::extractions(self)
}
fn links_of(&self, attachment_id: u64) -> Result<Vec<Subject>, StoreError> {
Store::attachment_links(self, attachment_id)
}
fn attachment_bytes(&self, attachment_id: u64) -> Result<Vec<u8>, StoreError> {
Store::attachment_bytes(self, attachment_id)
}
fn statements(&self) -> Result<Vec<Statement>, StoreError> {
Store::statements(self, None)
}
fn reconciliations(&self) -> Result<Vec<Reconciliation>, StoreError> {
Store::reconciliations(self, None)
}
/// The same comparison the viewer makes, not an assumption: the
/// store's database is the file the viewer checks, and it can be
/// damaged or changed from outside. Loading a snapshot of the data
/// directory compares it against a fresh projection of the journal.
fn disk_check(&self) -> DiskCheck {
match Snapshot::load(Store::data_dir(self)) {
Ok(s) => Snapshot::check_disk(&s),
Err(e) => DiskCheck::Unreadable(e.to_string()),
}
}
}
impl PackageBooks for Snapshot {
fn ledger(&self) -> &Ledger {
Snapshot::ledger(self)
}
fn journal_seq(&self) -> u64 {
Snapshot::journal_seq(self)
}
fn journal(&self) -> Result<Vec<JournalLine>, StoreError> {
Snapshot::committed_journal(self)
}
fn all_attachments(&self) -> Result<Vec<Attachment>, StoreError> {
Snapshot::attachments(self, None)
}
fn extractions(&self) -> Result<Vec<Extraction>, StoreError> {
Snapshot::extractions(self)
}
fn links_of(&self, attachment_id: u64) -> Result<Vec<Subject>, StoreError> {
Ok(Snapshot::attachment_links(self)?
.into_iter()
.filter(|(id, _)| *id == attachment_id)
.map(|(_, subject)| subject)
.collect())
}
fn attachment_bytes(&self, attachment_id: u64) -> Result<Vec<u8>, StoreError> {
Snapshot::attachment_bytes(self, attachment_id)
}
fn statements(&self) -> Result<Vec<Statement>, StoreError> {
Snapshot::statements(self, None)
}
fn reconciliations(&self) -> Result<Vec<Reconciliation>, StoreError> {
Snapshot::reconciliations(self, None)
}
fn disk_check(&self) -> DiskCheck {
Snapshot::check_disk(self)
}
}