explorerd/store/mod.rs
1/* This file is part of DarkFi (https://dark.fi)
2 *
3 * Copyright (C) 2020-2025 Dyne.org foundation
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
19use std::collections::HashMap;
20
21use lazy_static::lazy_static;
22use log::info;
23use sled_overlay::sled;
24
25use darkfi::{blockchain::Blockchain, error::Result, util::path::expand_path};
26
27use darkfi_sdk::crypto::{DAO_CONTRACT_ID, DEPLOYOOOR_CONTRACT_ID, MONEY_CONTRACT_ID};
28
29use crate::store::{contract_metadata::ContractMetaStore, metrics::MetricsStore};
30
31/// Stores, manages, and provides access to explorer metrics
32pub mod metrics;
33
34/// Stores, manages, and provides access to contract metadata
35pub mod contract_metadata;
36
37/// Represents the explorer database backed by a `sled` database connection, responsible for maintaining
38/// persistent state required for blockchain exploration. It serves as the core data layer for the Explorer application,
39/// storing and managing blockchain data, metrics, and contract-related information.
40pub struct ExplorerDb {
41 /// The main `sled` database connection used for data storage and retrieval
42 pub sled_db: sled::Db,
43 /// Local copy of the Darkfi blockchain used for block synchronization and exploration
44 pub blockchain: Blockchain,
45 /// Store for tracking chain-related metrics
46 pub metrics_store: MetricsStore,
47 /// Store for managing contract metadata, source code, and related data
48 pub contract_meta_store: ContractMetaStore,
49}
50
51impl ExplorerDb {
52 /// Creates a new `ExplorerDb` instance
53 pub fn new(db_path: String) -> Result<Self> {
54 let db_path = expand_path(db_path.as_str())?;
55 let sled_db = sled::open(&db_path)?;
56 let blockchain = Blockchain::new(&sled_db)?;
57 let metrics_store = MetricsStore::new(&sled_db)?;
58 let contract_meta_store = ContractMetaStore::new(&sled_db)?;
59 info!(target: "explorerd", "Initialized explorer database {}: block count: {}, tx count: {}", db_path.display(), blockchain.len(), blockchain.txs_len());
60 Ok(Self { sled_db, blockchain, metrics_store, contract_meta_store })
61 }
62}
63
64// Contract source archives used to bootstrap native contracts during explorer startup
65lazy_static! {
66 pub static ref NATIVE_CONTRACT_SOURCE_ARCHIVES: HashMap<String, &'static [u8]> = {
67 let mut src_map = HashMap::new();
68 src_map.insert(
69 MONEY_CONTRACT_ID.to_string(),
70 &include_bytes!("../../native_contracts_src/money_contract_src.tar")[..],
71 );
72 src_map.insert(
73 DAO_CONTRACT_ID.to_string(),
74 &include_bytes!("../../native_contracts_src/dao_contract_src.tar")[..],
75 );
76 src_map.insert(
77 DEPLOYOOOR_CONTRACT_ID.to_string(),
78 &include_bytes!("../../native_contracts_src/deployooor_contract_src.tar")[..],
79 );
80 src_map
81 };
82}