Skip to main content

Module history

Module history 

Source
Expand description

Ledger history, format 2: transactions and state changes in separate streams.

The layout was chosen by measuring the alternatives:

  • transactions: one frame per ledger (header and transactions with metadata), zstd with a dictionary, so a single transaction or an account_tx page is read without decompressing neighbouring ledgers (page p99 ~20 ms versus 383 ms with a shared block, a single tx 0.2 ms versus 5.8);
  • state changes: blocks of changes_group ledgers, zstd-19, since they are read only when rebuilding and verifying state.

Dictionaries are immutable files dicts/<hash>.zdict, named by the SHA-512Half of their content. A transaction segment names its dictionary by hash in its header, and the reader checks the file’s hash. Changing the dictionary means a new file and new segments; old segments are read with their own dictionary. Tampering with dictionary bytes is detected.

Files of a segment starting at ledger F:

  • txl-F.xwlt: XWLT\0\1, dictionary hash (32 bytes, zeros for no dictionary), then frames back to back; a frame is zstd of (118-byte header, u32 n, n pairs (u32 length, transaction, u32 length, metadata));
  • txl-F.xwlt.idx: XWTI\0\1, per frame u32 ledger | u64 offset | u32 length;
  • chg-F.xwlc: XWLC\0\1, then blocks; a block is zstd of (u32 n, n pairs (u32 ledger, u32 length), each ledger’s changes in Ledger format);
  • chg-F.xwlc.idx: XWCI\0\1, per block u32 first | u32 n | u64 offset | u32 length.

ASTJ5 cold segments (module cold, files cold-*.{astj,asix,kinds,json}) in the same directory are read when a ledger is not in the hot segments; with with_cold_first, before the hot ones (for checking the cold history).

Writing a group: transaction frames first, fsync, index, fsync; then the changes block the same way. A ledger is in the history only if both streams are written: a transaction tail without changes left by a crash is not read and gets rewritten in the next segment.

Structs§

Dict
An immutable zstd dictionary.
HistoryReader
History reader. New segments and groups become visible after refresh; get calls it itself when the ledger is past the known end.
HistoryWriter
Writes history in groups of changes_group ledgers.

Functions§

tx_frame
Transaction frame: header, transaction count, pairs.

Type Aliases§

PrevAt
Previous version of an object as of a ledger: (key, ledger) → object.