fuel_core/database/
database_description.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
use core::fmt::Debug;
use fuel_core_storage::kv_store::StorageColumn;
use fuel_core_types::{
    blockchain::primitives::DaBlockHeight,
    fuel_types::BlockHeight,
};
use std::collections::HashSet;
use strum::IntoEnumIterator;

pub mod gas_price;
pub mod off_chain;
pub mod on_chain;
pub mod relayer;

pub trait DatabaseHeight: PartialEq + Default + Debug + Copy + Send + Sync {
    fn as_u64(&self) -> u64;

    fn advance_height(&self) -> Option<Self>;

    fn rollback_height(&self) -> Option<Self>;
}

impl DatabaseHeight for BlockHeight {
    fn as_u64(&self) -> u64 {
        let height: u32 = (*self).into();
        height as u64
    }

    fn advance_height(&self) -> Option<Self> {
        self.succ()
    }

    fn rollback_height(&self) -> Option<Self> {
        self.pred()
    }
}

impl DatabaseHeight for DaBlockHeight {
    fn as_u64(&self) -> u64 {
        self.0
    }

    fn advance_height(&self) -> Option<Self> {
        self.0.checked_add(1).map(Into::into)
    }

    fn rollback_height(&self) -> Option<Self> {
        self.0.checked_sub(1).map(Into::into)
    }
}

/// The description of the database that makes it unique.
pub trait DatabaseDescription: 'static + Copy + Debug + Send + Sync {
    /// The type of the column used by the database.
    type Column: StorageColumn + strum::EnumCount + enum_iterator::Sequence;
    /// The type of the height of the database used to track commits.
    type Height: DatabaseHeight;

    /// Returns the expected version of the database.
    fn version() -> u32;

    /// Returns the name of the database.
    fn name() -> String;

    /// Returns the column used to store the metadata.
    fn metadata_column() -> Self::Column;

    /// Returns the prefix for the column.
    fn prefix(column: &Self::Column) -> Option<usize>;
}

#[derive(
    Copy,
    Clone,
    Debug,
    serde::Serialize,
    serde::Deserialize,
    Eq,
    PartialEq,
    Hash,
    strum::EnumIter,
)]
pub enum IndexationKind {
    Balances,
    CoinsToSpend,
    AssetMetadata,
}

impl IndexationKind {
    pub fn all() -> impl Iterator<Item = Self> {
        Self::iter()
    }
}

/// The metadata of the database contains information about the version and its height.
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub enum DatabaseMetadata<Height> {
    V1 {
        version: u32,
        height: Height,
    },
    V2 {
        version: u32,
        height: Height,
        indexation_availability: HashSet<IndexationKind>,
    },
}

impl<Height> DatabaseMetadata<Height> {
    /// Returns the version of the database.
    pub fn version(&self) -> u32 {
        match self {
            Self::V1 { version, .. } => *version,
            Self::V2 { version, .. } => *version,
        }
    }

    /// Returns the height of the database.
    pub fn height(&self) -> &Height {
        match self {
            Self::V1 { height, .. } => height,
            Self::V2 { height, .. } => height,
        }
    }

    /// Returns true if the given indexation kind is available.
    pub fn indexation_available(&self, kind: IndexationKind) -> bool {
        match self {
            Self::V1 { .. } => false,
            Self::V2 {
                indexation_availability,
                ..
            } => indexation_availability.contains(&kind),
        }
    }
}

/// Gets the indexation availability from the metadata.
pub fn indexation_availability<D>(
    metadata: Option<DatabaseMetadata<D::Height>>,
) -> HashSet<IndexationKind>
where
    D: DatabaseDescription,
{
    match metadata {
        Some(DatabaseMetadata::V1 { .. }) => HashSet::new(),
        Some(DatabaseMetadata::V2 {
            indexation_availability,
            ..
        }) => indexation_availability.clone(),
        // If the metadata doesn't exist, it is a new database,
        // and we should set all indexation kinds to available.
        None => IndexationKind::all().collect(),
    }
}