Skip to main content

miden_validator/
data_directory.rs

1use std::ops::Not;
2use std::path::{Path, PathBuf};
3
4/// Represents the validator's directories and their content paths.
5///
6/// Used to keep our filepath assumptions in one location.
7#[derive(Clone)]
8pub enum DataDirectory {
9    /// Runtime mode: just the data directory.
10    Server { data: PathBuf },
11    /// Bootstrap mode: genesis block, accounts, and data directories.
12    Bootstrap {
13        genesis_block: PathBuf,
14        accounts: PathBuf,
15        data: PathBuf,
16    },
17}
18
19impl DataDirectory {
20    /// Loads a data directory for use by the `start` and `migrate` commands.
21    pub fn load_server(data: PathBuf) -> std::io::Result<Self> {
22        verify_is_dir(&data)?;
23        Ok(Self::Server { data })
24    }
25
26    /// Loads a data directory for use by the `bootstrap` command.
27    pub fn load_bootstrap(
28        genesis_block: PathBuf,
29        accounts: PathBuf,
30        data: PathBuf,
31    ) -> std::io::Result<Self> {
32        for dir in [&genesis_block, &accounts, &data] {
33            verify_is_dir(dir)?;
34        }
35        Ok(Self::Bootstrap { genesis_block, accounts, data })
36    }
37
38    pub fn database_path(&self) -> PathBuf {
39        self.data().join("validator.sqlite3")
40    }
41
42    pub fn block_store_dir(&self) -> PathBuf {
43        self.data().join("blocks")
44    }
45
46    pub fn genesis_block_path(&self) -> Option<PathBuf> {
47        match self {
48            Self::Bootstrap { genesis_block, .. } => Some(genesis_block.join("genesis.dat")),
49            Self::Server { .. } => None,
50        }
51    }
52
53    pub fn accounts_dir(&self) -> Option<&Path> {
54        match self {
55            Self::Bootstrap { accounts, .. } => Some(accounts),
56            Self::Server { .. } => None,
57        }
58    }
59
60    pub fn display(&self) -> std::path::Display<'_> {
61        self.data().display()
62    }
63
64    fn data(&self) -> &PathBuf {
65        match self {
66            Self::Server { data } | Self::Bootstrap { data, .. } => data,
67        }
68    }
69}
70
71fn verify_is_dir(path: &PathBuf) -> std::io::Result<()> {
72    if fs_err::metadata(path)?.is_dir().not() {
73        return Err(std::io::ErrorKind::NotConnected.into());
74    }
75    Ok(())
76}