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