stratus/eth/storage/permanent/rocks/
mod.rs

1//! RocksDB layers (top-to-bottom): permanent -> state -> rest.
2
3use std::fmt::Debug;
4
5use anyhow::Context;
6pub use rocks_cf_cache_config::RocksCfCacheConfig;
7pub use rocks_permanent::RocksPermanentStorage;
8pub use rocks_state::RocksStorageState;
9use serde::Deserialize;
10use serde::Serialize;
11
12use crate::eth::storage::permanent::rocks::types::AddressRocksdb;
13use crate::eth::storage::permanent::rocks::types::BlockNumberRocksdb;
14use crate::eth::storage::permanent::rocks::types::SlotIndexRocksdb;
15
16/// Exposed API.
17mod rocks_permanent;
18
19/// State handler for DB and column families.
20pub mod rocks_state;
21
22/// CFs versionated by value variant.
23pub mod cf_versions;
24
25/// Data manipulation for column families.
26mod rocks_cf;
27
28/// Settings and tweaks for the database and column families.
29pub mod rocks_config;
30
31/// Column Family cache configuration.
32pub mod rocks_cf_cache_config;
33
34/// Functionalities related to the whole database.
35pub mod rocks_db;
36
37/// All types to be serialized and desserialized in the db.
38pub mod types;
39
40/// Utilities for testing.
41#[cfg(test)]
42pub mod test_utils;
43
44// Tuple implementations for composite keys
45impl SerializeDeserializeWithContext for (AddressRocksdb, BlockNumberRocksdb) {}
46impl SerializeDeserializeWithContext for (AddressRocksdb, SlotIndexRocksdb) {}
47impl SerializeDeserializeWithContext for (AddressRocksdb, SlotIndexRocksdb, BlockNumberRocksdb) {}
48
49pub trait SerializeDeserializeWithContext {
50    fn deserialize_with_context(bytes: &[u8]) -> anyhow::Result<Self>
51    where
52        Self: for<'de> Deserialize<'de> + bincode::Decode<()>,
53    {
54        use crate::rocks_bincode_config;
55        let (result, _) = bincode::decode_from_slice(bytes, rocks_bincode_config())
56            .with_context(|| format!("failed to deserialize '{}'", hex_fmt::HexFmt(bytes)))
57            .with_context(|| format!("failed to deserialize to type '{}'", std::any::type_name::<Self>()))?;
58        Ok(result)
59    }
60
61    fn serialize_with_context(input: &Self) -> anyhow::Result<Vec<u8>>
62    where
63        Self: Serialize + Debug + bincode::Encode,
64    {
65        use crate::rocks_bincode_config;
66        bincode::encode_to_vec(input, rocks_bincode_config()).with_context(|| format!("failed to serialize '{input:?}'"))
67    }
68}