mod.rs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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::sync::Arc;
  19. use log::debug;
  20. use darkfi::Result;
  21. use crate::{rpc::DarkfidRpcClient, store::ExplorerDb};
  22. /// Handles core block-related functionality
  23. pub mod blocks;
  24. /// Implements functionality for smart contracts
  25. pub mod contracts;
  26. /// Powers metrics gathering and analytical capabilities
  27. pub mod statistics;
  28. /// Manages transaction data processing
  29. pub mod transactions;
  30. /// Manages synchronization with darkfid
  31. pub mod sync;
  32. /// Represents the service layer for the Explorer application, bridging the RPC layer and the database.
  33. /// It encapsulates explorer business logic and provides a unified interface for core functionalities,
  34. /// providing a clear separation of concerns between RPC handling and data management layers.
  35. ///
  36. /// Core functionalities include:
  37. ///
  38. /// - Data Transformation: Converting database data into structured responses suitable for RPC callers.
  39. /// - Blocks: Synchronization, retrieval, counting, and management.
  40. /// - Contracts: Handling native and user contract data, source code, tar files, and metadata.
  41. /// - Metrics: Providing metric-related data over the life of the chain.
  42. /// - Transactions: Synchronization, calculating gas data, retrieval, counting, and related block information.
  43. pub struct ExplorerService {
  44. /// Explorer database instance
  45. pub db: ExplorerDb,
  46. /// JSON-RPC client used to execute requests to Darkfi blockchain nodes
  47. pub darkfid_client: Arc<DarkfidRpcClient>,
  48. }
  49. impl ExplorerService {
  50. /// Creates a new `ExplorerService` instance.
  51. pub fn new(db_path: String, darkfid_client: Arc<DarkfidRpcClient>) -> Result<Self> {
  52. // Initialize explorer database
  53. let db = ExplorerDb::new(db_path)?;
  54. Ok(Self { db, darkfid_client })
  55. }
  56. /// Initializes the explorer service by deploying native contracts and loading native contract
  57. /// source code and metadata required for its operation.
  58. pub async fn init(&self) -> Result<()> {
  59. self.deploy_native_contracts().await?;
  60. self.load_native_contract_sources()?;
  61. self.load_native_contract_metadata()?;
  62. Ok(())
  63. }
  64. /// Resets the explorer state to the specified height. If a genesis block height is provided,
  65. /// all blocks and transactions are purged from the database. Otherwise, the state is reverted
  66. /// to the given height. The explorer metrics are updated to reflect the updated blocks and
  67. /// transactions up to the reset height, ensuring consistency. Returns a result indicating
  68. /// success or an error if the operation fails.
  69. pub fn reset_explorer_state(&self, height: u32) -> Result<()> {
  70. debug!(target: "explorerd::reset_explorer_state", "Resetting explorer state to height: {height}");
  71. // Check if a genesis block reset or to a specific height
  72. match height {
  73. // Reset for genesis height 0, purge blocks and transactions
  74. 0 => {
  75. self.reset_blocks()?;
  76. self.reset_transactions()?;
  77. debug!(target: "explorerd::reset_explorer_state", "Reset explorer state to accept a new genesis block");
  78. }
  79. // Reset for all other heights
  80. _ => {
  81. self.reset_to_height(height)?;
  82. debug!(target: "explorerd::reset_explorer_state", "Reset blocks to height: {height}");
  83. }
  84. }
  85. // Reset gas metrics to the specified height to reflect the updated blockchain state
  86. self.db.metrics_store.reset_gas_metrics(height)?;
  87. debug!(target: "explorerd::reset_explorer_state", "Reset metrics store to height: {height}");
  88. Ok(())
  89. }
  90. }