Skip to main content

miden_node_store/state/
bootstrap.rs

1use std::path::Path;
2
3use anyhow::Context;
4use miden_node_utils::tracing::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        tracing::debug!(target: LOG_TARGET, path=%data_directory.display(), "Data directory loaded");
26
27        let block_store_path = data_directory.block_store_dir();
28        let block_store =
29            BlockStore::bootstrap(block_store_path.clone(), &genesis).with_context(|| {
30                format!("failed to bootstrap block store at {}", block_store_path.display())
31            })?;
32        tracing::debug!(target: LOG_TARGET, path=%block_store.display(), "Block store created");
33
34        let database_filepath = data_directory.database_path();
35        Db::bootstrap(database_filepath.clone(), genesis).with_context(|| {
36            format!("failed to bootstrap database at {}", database_filepath.display())
37        })?;
38        tracing::debug!(target: LOG_TARGET, path=%database_filepath.display(), "Database created");
39
40        Ok(())
41    }
42}