From 2085b209ea218fdac000ebdca8ac069200c232a5 Mon Sep 17 00:00:00 2001 From: Borja Castellano Date: Thu, 18 Dec 2025 18:19:14 +0000 Subject: [PATCH] removed fork detector and fork structs since they are no longer used after las removal --- dash-spv/src/chain/fork_detector.rs | 80 ---------------------------- dash-spv/src/chain/mod.rs | 24 --------- dash-spv/src/chain/reorg.rs | 40 -------------- dash-spv/src/sync/headers/manager.rs | 9 +--- 4 files changed, 1 insertion(+), 152 deletions(-) delete mode 100644 dash-spv/src/chain/fork_detector.rs delete mode 100644 dash-spv/src/chain/reorg.rs diff --git a/dash-spv/src/chain/fork_detector.rs b/dash-spv/src/chain/fork_detector.rs deleted file mode 100644 index 6dfe92822..000000000 --- a/dash-spv/src/chain/fork_detector.rs +++ /dev/null @@ -1,80 +0,0 @@ -//! Fork detection logic for identifying blockchain forks -//! -//! This module detects when incoming headers create a fork in the blockchain -//! rather than extending the current chain tip. - -use super::Fork; -use dashcore::BlockHash; -use std::collections::HashMap; - -/// Detects and manages blockchain forks -pub struct ForkDetector { - /// Currently known forks indexed by their tip hash - forks: HashMap, -} - -impl ForkDetector { - pub fn new(max_forks: usize) -> Result { - if max_forks == 0 { - return Err("max_forks must be greater than 0"); - } - Ok(Self { - forks: HashMap::new(), - }) - } - - /// Get all known forks - pub fn get_forks(&self) -> Vec<&Fork> { - self.forks.values().collect() - } - - /// Get a specific fork by its tip hash - pub fn get_fork(&self, tip_hash: &BlockHash) -> Option<&Fork> { - self.forks.get(tip_hash) - } - - /// Remove a fork (e.g., after it's been processed) - pub fn remove_fork(&mut self, tip_hash: &BlockHash) -> Option { - self.forks.remove(tip_hash) - } - - /// Check if we have any forks - pub fn has_forks(&self) -> bool { - !self.forks.is_empty() - } - - /// Get the strongest fork (most cumulative work) - pub fn get_strongest_fork(&self) -> Option<&Fork> { - self.forks.values().max_by_key(|fork| &fork.chain_work) - } - - /// Clear all forks - pub fn clear_forks(&mut self) { - self.forks.clear(); - } -} - -/// Result of fork detection for a header -#[derive(Debug, Clone)] -pub enum ForkDetectionResult { - /// Header extends the current main chain tip - ExtendsMainChain, - /// Header extends an existing fork - ExtendsFork(Fork), - /// Header creates a new fork from the main chain - CreatesNewFork(Fork), - /// Header doesn't connect to any known chain - Orphan, -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_fork_detector_zero_max_forks() { - let result = ForkDetector::new(0); - assert!(result.is_err()); - assert_eq!(result.err(), Some("max_forks must be greater than 0")); - } -} diff --git a/dash-spv/src/chain/mod.rs b/dash-spv/src/chain/mod.rs index 61be1f963..e533b7be3 100644 --- a/dash-spv/src/chain/mod.rs +++ b/dash-spv/src/chain/mod.rs @@ -1,7 +1,6 @@ //! Chain management module with reorganization support //! //! This module provides functionality for managing blockchain state including: -//! - Fork detection and handling //! - Chain reorganization //! - Multiple chain tip tracking //! - Chain work calculation @@ -11,9 +10,7 @@ pub mod chain_tip; pub mod chain_work; pub mod chainlock_manager; pub mod checkpoints; -pub mod fork_detector; pub mod orphan_pool; -pub mod reorg; #[cfg(test)] mod checkpoint_test; @@ -24,25 +21,4 @@ pub use chain_tip::{ChainTip, ChainTipManager}; pub use chain_work::ChainWork; pub use chainlock_manager::{ChainLockEntry, ChainLockManager, ChainLockStats}; pub use checkpoints::{Checkpoint, CheckpointManager}; -pub use fork_detector::{ForkDetectionResult, ForkDetector}; pub use orphan_pool::{OrphanBlock, OrphanPool, OrphanPoolStats}; -pub use reorg::ReorgEvent; - -use dashcore::{BlockHash, Header as BlockHeader}; - -/// Represents a potential chain fork -#[derive(Debug, Clone)] -pub struct Fork { - /// The block hash where the fork diverges from the main chain - pub fork_point: BlockHash, - /// The height of the fork point - pub fork_height: u32, - /// The tip of the forked chain - pub tip_hash: BlockHash, - /// The height of the fork tip - pub tip_height: u32, - /// Headers in the fork (from fork point to tip) - pub headers: Vec, - /// Cumulative chain work of this fork - pub chain_work: ChainWork, -} diff --git a/dash-spv/src/chain/reorg.rs b/dash-spv/src/chain/reorg.rs deleted file mode 100644 index 026f7ccd0..000000000 --- a/dash-spv/src/chain/reorg.rs +++ /dev/null @@ -1,40 +0,0 @@ -//! Chain reorganization handling -//! -//! This module implements the core logic for handling blockchain reorganizations, -//! including finding common ancestors, rolling back transactions, and switching chains. - -use dashcore::{BlockHash, Header as BlockHeader, Transaction, Txid}; - -/// Event emitted when a reorganization occurs -#[derive(Debug, Clone)] -pub struct ReorgEvent { - /// The common ancestor where chains diverged - pub common_ancestor: BlockHash, - /// Height of the common ancestor - pub common_height: u32, - /// Headers that were removed from the main chain - pub disconnected_headers: Vec, - /// Headers that were added to the main chain - pub connected_headers: Vec, - /// Transactions that may have changed confirmation status - pub affected_transactions: Vec, -} - -/// Data collected during the read phase of reorganization -#[allow(dead_code)] -#[derive(Debug)] -#[cfg_attr(test, derive(Clone))] -pub(crate) struct ReorgData { - /// The common ancestor where chains diverged - pub(crate) common_ancestor: BlockHash, - /// Height of the common ancestor - pub(crate) common_height: u32, - /// Headers that need to be disconnected from the main chain - disconnected_headers: Vec, - /// Block hashes and heights for disconnected blocks - disconnected_blocks: Vec<(BlockHash, u32)>, - /// Transaction IDs from disconnected blocks that affect the wallet - affected_tx_ids: Vec, - /// Actual transactions that were affected (if available) - affected_transactions: Vec, -} diff --git a/dash-spv/src/sync/headers/manager.rs b/dash-spv/src/sync/headers/manager.rs index 2db5fda4f..8e4a2f41b 100644 --- a/dash-spv/src/sync/headers/manager.rs +++ b/dash-spv/src/sync/headers/manager.rs @@ -7,7 +7,7 @@ use dashcore::{ use dashcore_hashes::Hash; use crate::chain::checkpoints::{mainnet_checkpoints, testnet_checkpoints, CheckpointManager}; -use crate::chain::{ChainTip, ChainTipManager, ChainWork, ForkDetector}; +use crate::chain::{ChainTip, ChainTipManager, ChainWork}; use crate::client::ClientConfig; use crate::error::{SyncError, SyncResult}; use crate::network::NetworkManager; @@ -47,7 +47,6 @@ pub struct HeaderSyncManager { _phantom_s: std::marker::PhantomData, _phantom_n: std::marker::PhantomData, config: ClientConfig, - fork_detector: ForkDetector, tip_manager: ChainTipManager, checkpoint_manager: CheckpointManager, reorg_config: ReorgConfig, @@ -83,8 +82,6 @@ impl