fuel_core/database/
database_description.rs

1use core::fmt::Debug;
2use fuel_core_storage::kv_store::StorageColumn;
3use fuel_core_types::{
4    blockchain::primitives::DaBlockHeight,
5    fuel_types::BlockHeight,
6};
7use std::collections::HashSet;
8use strum::IntoEnumIterator;
9
10pub mod compression;
11pub mod gas_price;
12pub mod off_chain;
13pub mod on_chain;
14pub mod relayer;
15
16pub trait DatabaseHeight: PartialEq + Default + Debug + Copy + Send + Sync {
17    fn as_u64(&self) -> u64;
18
19    fn advance_height(&self) -> Option<Self>;
20
21    fn rollback_height(&self) -> Option<Self>;
22}
23
24impl DatabaseHeight for BlockHeight {
25    fn as_u64(&self) -> u64 {
26        let height: u32 = (*self).into();
27        height as u64
28    }
29
30    fn advance_height(&self) -> Option<Self> {
31        self.succ()
32    }
33
34    fn rollback_height(&self) -> Option<Self> {
35        self.pred()
36    }
37}
38
39impl DatabaseHeight for DaBlockHeight {
40    fn as_u64(&self) -> u64 {
41        self.0
42    }
43
44    fn advance_height(&self) -> Option<Self> {
45        self.0.checked_add(1).map(Into::into)
46    }
47
48    fn rollback_height(&self) -> Option<Self> {
49        self.0.checked_sub(1).map(Into::into)
50    }
51}
52
53/// The description of the database that makes it unique.
54pub trait DatabaseDescription: 'static + Copy + Debug + Send + Sync {
55    /// The type of the column used by the database.
56    type Column: StorageColumn + strum::EnumCount + enum_iterator::Sequence;
57    /// The type of the height of the database used to track commits.
58    type Height: DatabaseHeight;
59
60    /// Returns the expected version of the database.
61    fn version() -> u32;
62
63    /// Returns the name of the database.
64    fn name() -> String;
65
66    /// Returns the column used to store the metadata.
67    fn metadata_column() -> Self::Column;
68
69    /// Returns the prefix for the column.
70    fn prefix(column: &Self::Column) -> Option<usize>;
71}
72
73#[derive(
74    Copy,
75    Clone,
76    Debug,
77    serde::Serialize,
78    serde::Deserialize,
79    Eq,
80    PartialEq,
81    Hash,
82    strum::EnumIter,
83)]
84pub enum IndexationKind {
85    Balances,
86    CoinsToSpend,
87    AssetMetadata,
88}
89
90impl IndexationKind {
91    pub fn all() -> impl Iterator<Item = Self> {
92        Self::iter()
93    }
94}
95
96/// The metadata of the database contains information about the version and its height.
97#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
98pub enum DatabaseMetadata<Height> {
99    V1 {
100        version: u32,
101        height: Height,
102    },
103    V2 {
104        version: u32,
105        height: Height,
106        indexation_availability: HashSet<IndexationKind>,
107    },
108}
109
110impl<Height> DatabaseMetadata<Height> {
111    /// Returns the version of the database.
112    pub fn version(&self) -> u32 {
113        match self {
114            Self::V1 { version, .. } => *version,
115            Self::V2 { version, .. } => *version,
116        }
117    }
118
119    /// Returns the height of the database.
120    pub fn height(&self) -> &Height {
121        match self {
122            Self::V1 { height, .. } => height,
123            Self::V2 { height, .. } => height,
124        }
125    }
126
127    /// Returns true if the given indexation kind is available.
128    pub fn indexation_available(&self, kind: IndexationKind) -> bool {
129        match self {
130            Self::V1 { .. } => false,
131            Self::V2 {
132                indexation_availability,
133                ..
134            } => indexation_availability.contains(&kind),
135        }
136    }
137}
138
139/// Gets the indexation availability from the metadata.
140pub fn indexation_availability<D>(
141    metadata: Option<DatabaseMetadata<D::Height>>,
142) -> HashSet<IndexationKind>
143where
144    D: DatabaseDescription,
145{
146    match metadata {
147        Some(DatabaseMetadata::V1 { .. }) => HashSet::new(),
148        Some(DatabaseMetadata::V2 {
149            indexation_availability,
150            ..
151        }) => indexation_availability.clone(),
152        // If the metadata doesn't exist, it is a new database,
153        // and we should set all indexation kinds to available.
154        None => IndexationKind::all().collect(),
155    }
156}