Skip to main content

Module statetree

Module statetree 

Source
Expand description

State tree (SHAMap) with the root recomputed on every ledger.

The XRPL tree rule: zero keys under a prefix is empty, one key is the hash of its leaf SHA-512Half(MLN\0, object, key), two or more is an inner node SHA-512Half(MIN\0, 16 children). The root is always an inner node. There is no path compression: a chain of nodes with a single inner child is stored in full.

Two fjall keyspaces:

  • live: live state keys and their leaf hashes;
  • tree: inner nodes by prefix, a bitmask of non-empty children (u16, big-endian) and their hashes. Of the 7.4 million nodes in the mainnet tree, most sit at depth 6 with two or three children, so 16 hashes in a row would take four times as much space. A 512-byte node without a mask is the older dense format and is still readable.

On each ledger the changes are overlaid on live in memory, only the nodes on the paths of changed keys are recomputed, and the root is checked against the header’s AccountHash before writing. If it does not match, the ledger is rejected. New leaf hashes and nodes go into the same batch as the state changes.

Only the latest version of the tree is stored. For proofs against earlier ledgers each update also reports the subtrees at depth LEVEL_DEPTH it changed (4096 of them, about 1700 keys each on mainnet): with those hashes kept per ledger, the top of the tree at any ledger is a few thousand hashes, and the subtree below is rebuilt from the object versions.

Structs§

StateTree

Constants§

LEVEL_DEPTH
Depth of the subtrees whose hashes are kept per ledger.
LEVEL_SLOTS
How many subtrees there are at that depth.

Functions§

inner_hash
leaf_changes
Leaf hashes from state changes.
level_range
The key range of a subtree at LEVEL_DEPTH.
level_slot
The subtree at LEVEL_DEPTH a key belongs to: its first three nibbles.
state_leaf_hash_of
Leaf hash of an object version; None for a deletion (an empty version).

Type Aliases§

LeafChanges
Leaf changes for a ledger: key → new leaf hash, None if deleted.