fuel_core/database/
database_description.rs1use 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
53pub trait DatabaseDescription: 'static + Copy + Debug + Send + Sync {
55 type Column: StorageColumn + strum::EnumCount + enum_iterator::Sequence;
57 type Height: DatabaseHeight;
59
60 fn version() -> u32;
62
63 fn name() -> String;
65
66 fn metadata_column() -> Self::Column;
68
69 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#[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 pub fn version(&self) -> u32 {
113 match self {
114 Self::V1 { version, .. } => *version,
115 Self::V2 { version, .. } => *version,
116 }
117 }
118
119 pub fn height(&self) -> &Height {
121 match self {
122 Self::V1 { height, .. } => height,
123 Self::V2 { height, .. } => height,
124 }
125 }
126
127 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
139pub 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 None => IndexationKind::all().collect(),
155 }
156}