Storage
The Ledger Engine keeps recent history and state in a hot store and older history in cold segments. How ledgers get there is in Loader.
§Hot store
Recent history and state live in the hot store under <data-dir>:
- state: a snapshot of every ledger object plus every later version of each object, in a fjall LSM tree. Any object can be read at any ledger since the snapshot. This is what
account_info,account_lines,account_objects,ledger_entry,ledger_data,mpt_holders,account_nftsand the NFT offer methods use. Next to the object versions it keeps the NFT index, Clio’s four NFT tables: a token’s owner and its URI at each ledger they changed, the tokens of each issuer by taxon, and each token’s transactions. New ledgers are indexed in the same batch as their state; a store that adds the index fills earlier ledgers in the background, from the history, and keeps its progress, so a restart continues where it stopped. The same store keeps the state tree (SHAMap) of the latest ledger and its history: for each ledger, the hashes of the subtrees at depth 3 (4096 of them) that it changed, and all of them every 256 ledgers. A path at an earlier ledger takes the top of the tree from there and rebuilds the key’s subtree, about 1700 objects on mainnet, from the object versions; the root has to match the ledger’sAccountHash. A store that started keeping the history after its snapshot rebuilds the missing part at startup, in the background. The object versions of one subtree are one key range, so each subtree is replayed on its own in memory, on several threads, and a second pass puts the top of the tree together for every ledger and checks it againstAccountHash. On mainnet this takes under two minutes per 50 thousand ledgers. After a stop, subtrees already replayed are not read again. - history: segments with one zstd frame of transactions per ledger, plus state-change blocks kept for rebuilds. Indexes map transaction hash to position, ledger hash to sequence, and account to its transactions.
§Cold store
Ledgers older than the state snapshot have transactions but no queryable state. They live in cold segments written in the ASTJ6 format (crates/astj5), which is ASTJ5 with its blocks cut into parts; older ASTJ5 segments are read as before. A segment with first ledger F is four files, plus an optional seal:
| File | Contents |
|---|---|
cold-F.astj | independent blocks of up to 1024 ledgers or 4 MiB, each with a SHA-512Half of its exact bytes; an ASTJ6 block is cut into parts of up to 1 MiB |
cold-F.asix | ASIX2 indexes; only in segments written before 0.3, and nothing reads them (astraea-state history-cold-drop-index removes them) |
cold-F.kinds | kind of each state change, when the source recorded it |
cold-F.seal | digests of each block’s stored bytes (of an ASTJ6 block, its head and every part), recorded after the block passed the full check |
cold-F.json | manifest: range, profile, sizes and hashes of the other files; written last |
A ledger’s state patches are stored only where its metadata doesn’t give them (coldpatch.rs). The metadata names every object a transaction touched, and the object after the last such transaction follows from that node: FinalFields or NewFields, the entry type, and this transaction as PreviousTxnID and PreviousTxnLgrSeq if the object is threaded. xrpld leaves a field at its default value out of NewFields, so those are filled in, and a new directory page gets the object its transaction created. What remains is stored per ledger, each patch as nothing at all, as the fields that differ, or whole. The largest remainder is Indexes of modified directory pages, which metadata never lists. The first entry of a stored ledger says how to read it, so a segment being rewritten can’t be read with the wrong rule, and a ledger that wouldn’t come back exactly is stored as it is. Reading transactions doesn’t touch any of this; only reading a ledger with its patches does, at about 1 ms per ledger. Segments in the older format, astraea-cold/1, hold every patch and are read as before.
Every block gets a full check: its hash, the transaction roots of its ledgers and the header chain. The check costs about as much as decoding the block, so it runs once, when the segment is written, and the seal records it. When a block is loaded for reading, the server hashes its stored bytes and compares them with the seal. If they match, the block is decoded without repeating the check. If they don’t, or the segment has no seal, the block is checked in full again. A damaged block becomes an error, never a wrong answer. Segments written before seals existed can be sealed with astraea-state history-cold-seal DIR.
An ASTJ5 block decodes only as a whole, so a page of account_tx that finds one transaction in each of 200 blocks decodes 200 blocks of 4 MiB, about 7 ms each. An ASTJ6 block keeps the codec’s dictionary and templates in a shared section and cuts the ledgers into parts of up to 1 MiB of exact bytes. Each part has its own columns, its own dictionary for the values no other part uses, and its own zstd frame compressed against the shared section. Repeats and transaction hashes derived from other transactions don’t reach outside a part. Reading one transaction then decodes the shared section and one part, about 2 ms on recent history, and only those bytes are hashed against the seal. Parts cost 8% in size on recent history and 17% around ledger 98 million. astraea-state history-cold-recode DIR rewrites ASTJ5 segments as ASTJ6 in place while the server runs.
Cold segments are grouped into index partitions (coldidx-F.*) for transaction-by-hash, account history and ledger-by-hash lookups. These are read straight from disk with positioned reads, so memory use doesn’t grow with history.
On the 5.18 billion transactions of the whole mainnet history the cold store takes 208 bytes per transaction with all indexes: about 199 for the segments and 9 for the index partitions.