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