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
use crate::database::{
    Column,
    Database,
    Error as DatabaseError,
    Result as DatabaseResult,
};
use fuel_core_chain_config::ChainConfig;

pub(crate) const DB_VERSION_KEY: &[u8] = b"version";
pub(crate) const CHAIN_NAME_KEY: &[u8] = b"chain_name";

/// Can be used to perform migrations in the future.
pub(crate) const DB_VERSION: u32 = 0x00;

impl Database {
    /// Ensures the database is initialized and that the database version is correct
    pub fn init(&self, config: &ChainConfig) -> DatabaseResult<()> {
        // initialize chain name if not set
        if self.get_chain_name()?.is_none() {
            self.insert(CHAIN_NAME_KEY, Column::Metadata, &config.chain_name)
                .and_then(|v: Option<String>| {
                    if v.is_some() {
                        Err(DatabaseError::ChainAlreadyInitialized)
                    } else {
                        Ok(())
                    }
                })?;
        }

        // Ensure the database version is correct
        if let Some(version) = self.get::<u32>(DB_VERSION_KEY, Column::Metadata)? {
            if version != DB_VERSION {
                return Err(DatabaseError::InvalidDatabaseVersion {
                    found: version,
                    expected: DB_VERSION,
                })?
            }
        } else {
            let _: Option<u32> =
                self.insert(DB_VERSION_KEY, Column::Metadata, &DB_VERSION)?;
        }
        Ok(())
    }

    pub fn get_chain_name(&self) -> DatabaseResult<Option<String>> {
        self.get(CHAIN_NAME_KEY, Column::Metadata)
    }
}