mod.rs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. use std::collections::HashMap;
  19. use lazy_static::lazy_static;
  20. use log::info;
  21. use sled_overlay::sled;
  22. use darkfi::{blockchain::Blockchain, error::Result, util::path::expand_path};
  23. use darkfi_sdk::crypto::{DAO_CONTRACT_ID, DEPLOYOOOR_CONTRACT_ID, MONEY_CONTRACT_ID};
  24. use crate::store::{contract_metadata::ContractMetaStore, metrics::MetricsStore};
  25. /// Stores, manages, and provides access to explorer metrics
  26. pub mod metrics;
  27. /// Stores, manages, and provides access to contract metadata
  28. pub mod contract_metadata;
  29. /// Represents the explorer database backed by a `sled` database connection, responsible for maintaining
  30. /// persistent state required for blockchain exploration. It serves as the core data layer for the Explorer application,
  31. /// storing and managing blockchain data, metrics, and contract-related information.
  32. pub struct ExplorerDb {
  33. /// The main `sled` database connection used for data storage and retrieval
  34. pub sled_db: sled::Db,
  35. /// Local copy of the Darkfi blockchain used for block synchronization and exploration
  36. pub blockchain: Blockchain,
  37. /// Store for tracking chain-related metrics
  38. pub metrics_store: MetricsStore,
  39. /// Store for managing contract metadata, source code, and related data
  40. pub contract_meta_store: ContractMetaStore,
  41. }
  42. impl ExplorerDb {
  43. /// Creates a new `ExplorerDb` instance
  44. pub fn new(db_path: String) -> Result<Self> {
  45. let db_path = expand_path(db_path.as_str())?;
  46. let sled_db = sled::open(&db_path)?;
  47. let blockchain = Blockchain::new(&sled_db)?;
  48. let metrics_store = MetricsStore::new(&sled_db)?;
  49. let contract_meta_store = ContractMetaStore::new(&sled_db)?;
  50. info!(target: "explorerd", "Initialized explorer database {}: block count: {}, tx count: {}", db_path.display(), blockchain.len(), blockchain.txs_len());
  51. Ok(Self { sled_db, blockchain, metrics_store, contract_meta_store })
  52. }
  53. }
  54. // Contract source archives used to bootstrap native contracts during explorer startup
  55. lazy_static! {
  56. pub static ref NATIVE_CONTRACT_SOURCE_ARCHIVES: HashMap<String, &'static [u8]> = {
  57. let mut src_map = HashMap::new();
  58. src_map.insert(
  59. MONEY_CONTRACT_ID.to_string(),
  60. &include_bytes!("../../native_contracts_src/money_contract_src.tar")[..],
  61. );
  62. src_map.insert(
  63. DAO_CONTRACT_ID.to_string(),
  64. &include_bytes!("../../native_contracts_src/dao_contract_src.tar")[..],
  65. );
  66. src_map.insert(
  67. DEPLOYOOOR_CONTRACT_ID.to_string(),
  68. &include_bytes!("../../native_contracts_src/deployooor_contract_src.tar")[..],
  69. );
  70. src_map
  71. };
  72. }