Skip to main content

miden_node_store/state/
bootstrap.rs

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