Skip to main content

ethrex_storage/
utils.rs

1/// Represents the key for each unique value of the chain data stored in the db
2//  Stores chain-specific data such as chain id and latest finalized/pending/safe block number
3#[derive(Debug, Copy, Clone)]
4pub enum ChainDataIndex {
5    ChainConfig = 0,
6    EarliestBlockNumber = 1,
7    FinalizedBlockNumber = 2,
8    SafeBlockNumber = 3,
9    LatestBlockNumber = 4,
10    PendingBlockNumber = 5,
11}
12
13impl From<u8> for ChainDataIndex {
14    fn from(value: u8) -> Self {
15        match value {
16            x if x == ChainDataIndex::ChainConfig as u8 => ChainDataIndex::ChainConfig,
17            x if x == ChainDataIndex::EarliestBlockNumber as u8 => {
18                ChainDataIndex::EarliestBlockNumber
19            }
20            x if x == ChainDataIndex::FinalizedBlockNumber as u8 => {
21                ChainDataIndex::FinalizedBlockNumber
22            }
23            x if x == ChainDataIndex::SafeBlockNumber as u8 => ChainDataIndex::SafeBlockNumber,
24            x if x == ChainDataIndex::LatestBlockNumber as u8 => ChainDataIndex::LatestBlockNumber,
25            x if x == ChainDataIndex::PendingBlockNumber as u8 => {
26                ChainDataIndex::PendingBlockNumber
27            }
28            _ => panic!("Invalid value when casting to ChainDataIndex: {value}"),
29        }
30    }
31}
32
33/// Represents the key for each unique value of the snap state stored in the db
34//  Stores the snap state from previous sync cycles. Currently stores the header & state trie download checkpoint
35//, but will later on also include the body download checkpoint and the last pivot used
36#[derive(Debug, Copy, Clone)]
37pub enum SnapStateIndex {
38    // Hash of the last downloaded header in a previous sync cycle that was aborted
39    HeaderDownloadCheckpoint = 0,
40    // Last key fetched from the state trie
41    StateTrieKeyCheckpoint = 1,
42    // Paths from the state trie in need of healing
43    StateHealPaths = 2,
44    // Trie Rebuild Checkpoint (Current State Trie Root, Last Inserted Key Per Segment)
45    StateTrieRebuildCheckpoint = 3,
46    // Storage tries awaiting rebuild (AccountHash, ExpectedRoot)
47    StorageTrieRebuildPending = 4,
48}
49
50impl From<u8> for SnapStateIndex {
51    fn from(value: u8) -> Self {
52        match value {
53            0 => SnapStateIndex::HeaderDownloadCheckpoint,
54            1 => SnapStateIndex::StateTrieKeyCheckpoint,
55            2 => SnapStateIndex::StateHealPaths,
56            3 => SnapStateIndex::StateTrieRebuildCheckpoint,
57            4 => SnapStateIndex::StorageTrieRebuildPending,
58            _ => panic!("Invalid value when casting to SnapDataIndex: {value}"),
59        }
60    }
61}