hopr_db_node/
lib.rs

1mod db;
2pub mod errors;
3mod tickets;
4
5use std::path::PathBuf;
6
7pub use db::{HoprNodeDb, HoprNodeDbConfig};
8pub use hopr_api::{chain::RedeemableTicket, db::*};
9
10/// Convenience function to initialize the HOPR node database.
11pub async fn init_hopr_node_db(
12    db_data_path: &str,
13    initialize: bool,
14    force_initialize: bool,
15) -> anyhow::Result<HoprNodeDb> {
16    let db_path: PathBuf = [db_data_path, "node_db"].iter().collect();
17    tracing::info!(path = ?db_path, "initiating DB");
18
19    let mut create_if_missing = initialize;
20
21    if force_initialize {
22        tracing::info!("Force cleaning up existing database");
23        std::fs::remove_dir_all(db_path.as_path())?;
24        create_if_missing = true;
25    }
26
27    // create DB dir if it does not exist
28    if let Some(parent_dir_path) = db_path.as_path().parent() {
29        if !parent_dir_path.is_dir() {
30            std::fs::create_dir_all(parent_dir_path)
31                .map_err(|e| anyhow::anyhow!("Failed to create DB parent directory at '{parent_dir_path:?}': {e}"))?
32        }
33    }
34
35    let db_cfg = HoprNodeDbConfig {
36        create_if_missing,
37        force_create: force_initialize,
38        log_slow_queries: std::time::Duration::from_millis(150),
39    };
40    let node_db = HoprNodeDb::new(db_path.as_path(), db_cfg).await?;
41
42    Ok(node_db)
43}