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