casper_storage/block_store/lmdb/
mod.rs

1mod lmdb_ext;
2mod temp_map;
3mod versioned_databases;
4
5mod indexed_lmdb_block_store;
6mod lmdb_block_store;
7
8use core::convert::TryFrom;
9pub use indexed_lmdb_block_store::IndexedLmdbBlockStore;
10pub use lmdb_block_store::LmdbBlockStore;
11
12#[cfg(test)]
13use rand::Rng;
14use serde::Serialize;
15
16#[cfg(test)]
17use casper_types::testing::TestRng;
18
19/// An identifier of db tables.
20#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Serialize)]
21#[repr(u16)]
22pub enum DbTableId {
23    /// Refers to `BlockHeader` db table.
24    BlockHeader = 0,
25    /// Refers to `BlockBody` db table.
26    BlockBody = 1,
27    /// Refers to `ApprovalsHashes` db table.
28    ApprovalsHashes = 2,
29    /// Refers to `BlockMetadata` db table.
30    BlockMetadata = 3,
31    /// Refers to `Transaction` db table.
32    Transaction = 4,
33    /// Refers to `ExecutionResult` db table.
34    ExecutionResult = 5,
35    /// Refers to `Transfer` db table.
36    Transfer = 6,
37    /// Refers to `FinalizedTransactionApprovals` db table.
38    FinalizedTransactionApprovals = 7,
39}
40
41impl DbTableId {
42    /// Returns a random `DbTableId`.
43    #[cfg(test)]
44    pub fn random(rng: &mut TestRng) -> Self {
45        match rng.gen_range(0..8) {
46            0 => DbTableId::BlockHeader,
47            1 => DbTableId::BlockBody,
48            2 => DbTableId::ApprovalsHashes,
49            3 => DbTableId::BlockMetadata,
50            4 => DbTableId::Transaction,
51            5 => DbTableId::ExecutionResult,
52            6 => DbTableId::Transfer,
53            7 => DbTableId::FinalizedTransactionApprovals,
54            _ => unreachable!(),
55        }
56    }
57}
58
59impl TryFrom<u16> for DbTableId {
60    type Error = UnknownDbTableId;
61
62    fn try_from(value: u16) -> Result<Self, Self::Error> {
63        match value {
64            0 => Ok(DbTableId::BlockHeader),
65            1 => Ok(DbTableId::BlockBody),
66            2 => Ok(DbTableId::ApprovalsHashes),
67            3 => Ok(DbTableId::BlockMetadata),
68            4 => Ok(DbTableId::Transaction),
69            5 => Ok(DbTableId::ExecutionResult),
70            6 => Ok(DbTableId::Transfer),
71            7 => Ok(DbTableId::FinalizedTransactionApprovals),
72            _ => Err(UnknownDbTableId(value)),
73        }
74    }
75}
76
77impl From<DbTableId> for u16 {
78    fn from(value: DbTableId) -> Self {
79        value as u16
80    }
81}
82
83impl core::fmt::Display for DbTableId {
84    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
85        match self {
86            DbTableId::BlockHeader => write!(f, "BlockHeader"),
87            DbTableId::BlockBody => write!(f, "BlockBody"),
88            DbTableId::ApprovalsHashes => write!(f, "ApprovalsHashes"),
89            DbTableId::BlockMetadata => write!(f, "BlockMetadata"),
90            DbTableId::Transaction => write!(f, "Transaction"),
91            DbTableId::ExecutionResult => write!(f, "ExecutionResult"),
92            DbTableId::Transfer => write!(f, "Transfer"),
93            DbTableId::FinalizedTransactionApprovals => write!(f, "FinalizedTransactionApprovals"),
94        }
95    }
96}
97
98/// Error returned when trying to convert a `u16` into a `DbTableId`.
99#[derive(Debug, PartialEq, Eq)]
100pub struct UnknownDbTableId(u16);
101
102#[cfg(test)]
103mod tests {
104    use super::*;
105    use casper_types::testing::TestRng;
106
107    #[test]
108    fn tag_roundtrip() {
109        let rng = &mut TestRng::new();
110
111        let val = DbTableId::random(rng);
112        let tag = u16::from(val);
113        assert_eq!(DbTableId::try_from(tag), Ok(val));
114    }
115}