Skip to main content

miden_node_store/state/
bootstrap.rs

1use std::path::Path;
2
3use anyhow::Context;
4use miden_node_tracing::{debug, miden_instrument};
5
6use crate::blocks::BlockStore;
7use crate::db::Db;
8use crate::genesis::GenesisBlock;
9use crate::state::State;
10use crate::{COMPONENT, DataDirectory, LOG_TARGET};
11
12impl State {
13    /// Bootstraps the store state, creating the database state and inserting the genesis block
14    /// data.
15    #[miden_instrument(
16        target = COMPONENT,
17        name = "store.bootstrap",
18        err,
19    )]
20    pub fn bootstrap(genesis: GenesisBlock, data_directory: &Path) -> anyhow::Result<()> {
21        let data_directory =
22            DataDirectory::load(data_directory.to_path_buf()).with_context(|| {
23                format!("failed to load data directory at {}", data_directory.display())
24            })?;
25        debug!(
26            target: LOG_TARGET,
27            "Data directory loaded",
28            path = data_directory
29        );
30
31        let block_store_path = data_directory.block_store_dir();
32        let _block_store =
33            BlockStore::bootstrap(block_store_path.clone(), &genesis).with_context(|| {
34                format!("failed to bootstrap block store at {}", block_store_path.display())
35            })?;
36        debug!(
37            target: LOG_TARGET,
38            "Block store created",
39            path = block_store_path
40        );
41
42        let database_filepath = data_directory.database_path();
43        Db::bootstrap(database_filepath.clone(), genesis).with_context(|| {
44            format!("failed to bootstrap database at {}", database_filepath.display())
45        })?;
46        debug!(
47            target: LOG_TARGET,
48            "Database created",
49            path = database_filepath
50        );
51
52        Ok(())
53    }
54}