cnidarium/lib.rs
1//! Storage and management of chain state, backed by Jellyfish Merkle Trees and RocksDB.
2//!
3//! This crate provides a versioned, verifiable key-value store that also
4//! supports lightweight, copy-on-write snapshots and transactional semantics.
5//! The [`Storage`] type is a handle for an instance of a backing store,
6//! implemented using RocksDB. The storage records a sequence of versioned
7//! [`Snapshot`]s. The [`Snapshot`] type is a lightweight snapshot of a particular
8//! version of the chain state.
9//!
10//! Each [`Snapshot`] instance can also be used as the basis for a copy-on-write
11//! fork to build up changes before committing them to persistent storage. The
12//! [`StateDelta`] type collects a group of writes, which can then be applied to
13//! the (in-memory) [`StateDelta`] overlay. Finally, the changes accumulated in the
14//! [`StateDelta`] instance can be committed to the persistent [`Storage`].
15//!
16//! Reads are performed with the [`StateRead`] trait, implemented by both
17//! [`Snapshot`] and [`StateDelta`], and reflect any currently cached writes.
18//! Writes are performed with the [`StateWrite`] trait, which is only
19//! implemented for [`StateDelta`].
20//!
21//! The storage system provides three data stores:
22//!
23//! * A verifiable key-value store, with UTF-8 keys and byte values, backed by
24//! the Jellyfish Merkle Tree. The JMT is a sparse merkle tree that records
25//! hashed keys, so we also record an index of the keys themselves to allow
26//! range queries on keys rather than key hashes. This index, however, is not
27//! part of the verifiable consensus state.
28//!
29//! * A secondary, non-verifiable key-value store with byte keys and byte
30//! values, backed directly by RocksDB. This is intended for use building
31//! application-specific indexes of the verifiable consensus state.
32//!
33//! * A tertiary, in-memory object store. This is intended for use implementing
34//! accumulators, like lists of data to be batch-processed at the end of the
35//! block. The object store clones on read to prevent violations of
36//! transactional semantics, so it should be used with immutable data structures
37//! like those in the `im` crate that implement copy-on-write behavior
38//! internally.
39//!
40//! The storage system also supports prefixed "substores", somewhat similar to
41//! the Cosmos SDK's multistore design. Each substore has a separate JMT, whose
42//! root hash is written into the base store under the prefix. This allows use
43//! cases like storing IBC data in a subtree. The substore's non-verifiable
44//! store is also stored in a separate RocksDB column family, allowing storage
45//! optimizations.
46//!
47//! Remember that the chain state is a public API. Mapping from raw byte values
48//! to typed data should be accomplished by means of extension traits. For
49//! instance, the `penumbra_proto` crate provides an extension trait to
50//! automatically (de)serialize into proto or domain types, allowing its use as
51//! an object store.
52//!
53//! With the `rpc` feature enabled, this crate also provides a GRPC interface to
54//! the key-value store using Tonic.
55#![deny(clippy::unwrap_used)]
56// Requires nightly.
57#![cfg_attr(docsrs, feature(doc_auto_cfg))]
58// We use `HashMap`s opportunistically.
59#![allow(clippy::disallowed_types)]
60
61mod cache;
62mod delta;
63mod escaped_byte_slice;
64mod metrics;
65mod read;
66mod snapshot;
67mod snapshot_cache;
68mod storage;
69mod store;
70#[cfg(test)]
71mod tests;
72mod utils;
73mod write;
74mod write_batch;
75
76#[cfg(feature = "metrics")]
77pub use crate::metrics::register_metrics;
78pub use cache::Cache;
79pub use delta::{ArcStateDeltaExt, StateDelta};
80pub use escaped_byte_slice::EscapedByteSlice;
81pub use jmt::{ics23_spec, RootHash};
82pub use read::StateRead;
83pub use snapshot::Snapshot;
84pub use storage::{Storage, TempStorage};
85pub use write::StateWrite;
86pub use write_batch::StagedWriteBatch;
87
88pub mod future;
89
90#[cfg(feature = "rpc")]
91pub mod rpc;
92
93#[cfg(feature = "proto")]
94pub mod proto;