diff --git a/dash-spv/src/network/mod.rs b/dash-spv/src/network/mod.rs index feaf5e686..816dc3e4c 100644 --- a/dash-spv/src/network/mod.rs +++ b/dash-spv/src/network/mod.rs @@ -80,10 +80,10 @@ impl Request for BlockRequest { } } -/// `getheaders` from the header at `height`, whose hash is `locator`. +/// `getheaders` from the header at `height`, whose hash is the first of `locator`. pub struct HeadersRequest { pub height: u32, - pub locator: BlockHash, + pub locator: Vec, } impl Request for HeadersRequest { @@ -91,10 +91,7 @@ impl Request for HeadersRequest { self.height } fn into_message(self) -> NetworkMessage { - NetworkMessage::GetHeaders(GetHeadersMessage::new( - vec![self.locator], - BlockHash::all_zeros(), - )) + NetworkMessage::GetHeaders(GetHeadersMessage::new(self.locator, BlockHash::all_zeros())) } } diff --git a/dash-spv/src/sync/block_headers/manager.rs b/dash-spv/src/sync/block_headers/manager.rs index 7be284423..95607f38c 100644 --- a/dash-spv/src/sync/block_headers/manager.rs +++ b/dash-spv/src/sync/block_headers/manager.rs @@ -114,6 +114,31 @@ impl BlockHeadersManager { .ok_or_else(|| SyncError::MissingDependency("storage not initialized".to_string())) } + /// Stored hashes below the tip for a `getheaders` locator: ten one block apart, then + /// twice as far apart each time, down to the lowest stored header. A peer that does not + /// know the tip answers from the newest of them it knows, which is where a fork starts. + pub(super) async fn locator_ancestors(&self) -> SyncResult> { + let storage = self.header_storage.read().await; + let (Some(start), Some(tip)) = + (storage.get_start_height().await, storage.get_tip_height().await) + else { + return Ok(vec![]); + }; + let mut ancestors = Vec::new(); + let mut height = tip; + let mut step = 1; + while height > start { + height = height.saturating_sub(step).max(start); + if let Some(header) = storage.get_header(height).await? { + ancestors.push(*header.hash()); + } + if ancestors.len() >= 10 { + step *= 2; + } + } + Ok(ancestors) + } + /// Sync the headers below the lowest stored one, down to [`Self::floor`]. /// /// Splits the range at the checkpoints below the lowest stored header, down to the @@ -335,11 +360,24 @@ impl BlockHeadersManager { Ok(events) } - /// Height where `headers` branch off the stored chain below its tip, if they - /// do. A branch off below the best ChainLock, or below the newest checkpoint - /// the stored chain reaches, is not followed: the blocks they fix are final. + /// Height to drop the stored chain to when `headers` do not extend its tip: the + /// best ChainLock, or the newest checkpoint the tip reaches without one. Those blocks + /// are final, so a branch off below them is not followed. async fn fork_height(&self, headers: &[HashedBlockHeader]) -> SyncResult> { let storage = self.header_storage.read().await; + let Some(tip) = storage.get_tip_height().await.filter(|&tip| tip > 0) else { + return Ok(None); + }; + let chain_lock = self + .metadata_storage + .read() + .await + .load_metadata::(BEST_CHAINLOCK_METADATA_STORAGE_KEY) + .await?; + let checkpoint = self.checkpoint_manager.last_checkpoint_before_height(tip); + let final_height = chain_lock + .map_or(0, |cl| cl.block_height) + .max(checkpoint.map_or(0, Checkpoint::height)); // Skip the headers we already store: a peer may resend part of our chain. for header in headers { @@ -348,40 +386,18 @@ impl BlockHeadersManager { } let parent = storage.get_header_height_by_hash(&header.header().prev_blockhash).await?; - let tip = storage.get_tip_height().await; - let Some(fork_height) = parent.filter(|&parent| tip.is_some_and(|tip| parent < tip)) - else { + let Some(parent) = parent.filter(|&parent| parent < tip) else { return Ok(None); }; - - if let Some(checkpoint) = tip - .and_then(|tip| self.checkpoint_manager.last_checkpoint_before_height(tip)) - .filter(|checkpoint| fork_height < checkpoint.height()) - { + if parent < final_height { tracing::warn!( - "Ignoring a fork at height {}, below the checkpoint at {}", - fork_height, - checkpoint.height() + "Ignoring a fork at height {}, below the final height {}", + parent, + final_height ); return Ok(None); } - - let chain_lock = self - .metadata_storage - .read() - .await - .load_metadata::(BEST_CHAINLOCK_METADATA_STORAGE_KEY) - .await?; - if let Some(chain_lock) = chain_lock.filter(|cl| fork_height < cl.block_height) { - tracing::warn!( - "Ignoring a fork at height {}, below the ChainLock at {}", - fork_height, - chain_lock.block_height - ); - return Ok(None); - } - - return Ok(Some(fork_height)); + return Ok(Some(final_height)); } Ok(None) @@ -431,6 +447,12 @@ impl BlockHeadersManager { }); } } + if !events.is_empty() { + match self.locator_ancestors().await { + Ok(ancestors) => self.pipeline.set_tip_ancestors(ancestors), + Err(e) => tracing::warn!("Failed to refresh the tip locator: {}", e), + } + } // Last, and without failing, as the batches above already left the pipeline // and their events would otherwise be dropped. @@ -570,10 +592,10 @@ mod tests { .expect("Failed to create BlockHeadersManager") } - #[test_case(None, 5, Some(5); "no ChainLock")] + #[test_case(None, 5, Some(0); "no ChainLock")] #[test_case(Some(6), 5, None; "below the ChainLock")] #[test_case(Some(5), 5, Some(5); "at the ChainLock")] - #[test_case(Some(3), 5, Some(5); "above the ChainLock")] + #[test_case(Some(3), 5, Some(3); "above the ChainLock")] #[tokio::test] async fn a_branch_off_below_the_best_chain_lock_is_no_fork( chain_locked: Option, @@ -615,7 +637,7 @@ mod tests { /// or no fork could ever be followed before the chain reached it. #[test_case(49_999, None; "below the checkpoint")] #[test_case(50_000, Some(50_000); "at the checkpoint")] - #[test_case(50_001, Some(50_001); "above the checkpoint")] + #[test_case(50_001, Some(50_000); "above the checkpoint")] #[tokio::test] async fn a_branch_off_below_the_newest_reached_checkpoint_is_no_fork( branch_off: u32, @@ -668,7 +690,7 @@ mod tests { ) .await .unwrap(); - manager.pipeline.init(4, *chain[4].hash(), 4); + manager.pipeline.init(4, *chain[4].hash(), vec![], 4); manager.progress.set_state(SyncState::Synced); let parent_hash = *chain[parent].hash(); @@ -711,7 +733,7 @@ mod tests { async fn create_synced_manager() -> TestBlockHeadersManager { let mut manager = create_test_manager().await; let tip = manager.tip().await.unwrap(); - manager.pipeline.init(tip.height(), *tip.hash(), tip.height()); + manager.pipeline.init(tip.height(), *tip.hash(), vec![], tip.height()); manager.progress.set_state(SyncState::Synced); manager } @@ -734,7 +756,7 @@ mod tests { let tip = manager.tip().await.unwrap(); let start_height = tip.height(); - manager.pipeline.init(start_height, *tip.hash(), start_height + 2); + manager.pipeline.init(start_height, *tip.hash(), vec![], start_height + 2); manager.progress.set_state(SyncState::Syncing); let mock = Arc::new(MockNetworkManager::new()); @@ -812,7 +834,7 @@ mod tests { .await .unwrap(); let tip = manager.tip().await.unwrap(); - manager.pipeline.init(tip.height(), *tip.hash(), tip.height() + 2); + manager.pipeline.init(tip.height(), *tip.hash(), vec![], tip.height() + 2); // Headers below that join neither the anchor nor the stored ones. let below: Vec = Header::dummy_chain(1, BlockHash::dummy(2)) @@ -860,7 +882,7 @@ mod tests { ) .await .unwrap(); - manager.pipeline.init(129, *chain[129].hash(), 129); + manager.pipeline.init(129, *chain[129].hash(), vec![], 129); manager.floor = Some(30); manager.extend_down().await.unwrap(); @@ -955,7 +977,7 @@ mod tests { let tip_hash = *tip.hash(); // Simulate completed sync: pipeline initialized with tip segment marked complete - manager.pipeline.init(0, tip_hash, 0); + manager.pipeline.init(0, tip_hash, vec![], 0); manager.pipeline.mark_tip_complete(); manager.progress.set_state(SyncState::Synced); diff --git a/dash-spv/src/sync/block_headers/pipeline.rs b/dash-spv/src/sync/block_headers/pipeline.rs index e7241f5f3..bc10bbe3b 100644 --- a/dash-spv/src/sync/block_headers/pipeline.rs +++ b/dash-spv/src/sync/block_headers/pipeline.rs @@ -60,7 +60,14 @@ impl HeadersPipeline { } /// Initialize the pipeline for downloading from current_height to target_height. - pub fn init(&mut self, current_height: u32, current_hash: BlockHash, target_height: u32) { + /// `ancestors` are the stored hashes below `current_hash` its locator also carries. + pub fn init( + &mut self, + current_height: u32, + current_hash: BlockHash, + ancestors: Vec, + target_height: u32, + ) { self.segments.clear(); self.next_to_store = 0; self.initialized = true; @@ -107,6 +114,9 @@ impl HeadersPipeline { self.segments.push(segment); } + if let Some(first) = self.segments.first_mut() { + first.ancestors = ancestors; + } tracing::info!( "HeadersPipeline initialized with {} segments for heights {} to {}", @@ -344,6 +354,13 @@ impl HeadersPipeline { .map(|s| s.current_tip_hash) } + /// Sets the stored hashes the tip segment's locator carries below its tip. + pub(super) fn set_tip_ancestors(&mut self, ancestors: Vec) { + if let Some(tip) = self.segments.iter_mut().find(|s| s.target_height.is_none()) { + tip.ancestors = ancestors; + } + } + /// Check if pipeline is initialized. pub fn is_initialized(&self) -> bool { self.initialized @@ -460,7 +477,7 @@ mod tests { let cm = create_test_checkpoint_manager(true); let mut pipeline = HeadersPipeline::new(cm); - pipeline.init(0, testnet_genesis_hash(), 1_200_000); + pipeline.init(0, testnet_genesis_hash(), vec![], 1_200_000); assert!(pipeline.is_initialized()); // Should have segments: 0->500k, 500k->800k, 800k->1.1M, 1.1M->tip @@ -474,7 +491,7 @@ mod tests { // Start from checkpoint 500k let cp_500k = cm.last_checkpoint_before_height(500_000).unwrap(); - pipeline.init(500_000, *cp_500k.hash(), 1_200_000); + pipeline.init(500_000, *cp_500k.hash(), vec![], 1_200_000); // Should have fewer segments since we're starting from 500k assert!(pipeline.is_initialized()); @@ -487,7 +504,7 @@ mod tests { let cm = create_test_checkpoint_manager(true); let mut pipeline = HeadersPipeline::new(cm); - pipeline.init(0, testnet_genesis_hash(), 1_200_000); + pipeline.init(0, testnet_genesis_hash(), vec![], 1_200_000); let mock = Arc::new(MockNetworkManager::new()); let network: Arc = mock.clone(); @@ -520,7 +537,7 @@ mod tests { let cm = create_test_checkpoint_manager(true); let mut pipeline = HeadersPipeline::new(cm); - pipeline.init(0, testnet_genesis_hash(), 1_200_000); + pipeline.init(0, testnet_genesis_hash(), vec![], 1_200_000); assert!(!pipeline.is_complete()); } @@ -530,7 +547,7 @@ mod tests { let cm = create_test_checkpoint_manager(true); let mut pipeline = HeadersPipeline::new(cm); - pipeline.init(0, testnet_genesis_hash(), 1_200_000); + pipeline.init(0, testnet_genesis_hash(), vec![], 1_200_000); let ready = pipeline.take_ready_to_store(); assert!(ready.is_empty()); diff --git a/dash-spv/src/sync/block_headers/segment_state.rs b/dash-spv/src/sync/block_headers/segment_state.rs index bdaed0bb9..f7cb60454 100644 --- a/dash-spv/src/sync/block_headers/segment_state.rs +++ b/dash-spv/src/sync/block_headers/segment_state.rs @@ -2,6 +2,7 @@ use crate::error::{SyncError, SyncResult}; use crate::network::{HeadersRequest, NetworkManager, SendRequest}; use crate::types::HashedBlockHeader; use dashcore::BlockHash; +use std::iter; use std::sync::Arc; /// State for a single download segment between two checkpoints. @@ -17,6 +18,8 @@ pub(super) struct SegmentState { target_hash: Option, /// Current tip hash for GetHeaders locator. pub(super) current_tip_hash: BlockHash, + /// Stored hashes below the start, sent after `current_tip_hash` in the locator. + pub(super) ancestors: Vec, /// Locator of the last declared GetHeaders that is still unanswered. declared: Option, /// Current height reached in this segment. @@ -42,6 +45,7 @@ impl SegmentState { target_height, target_hash, current_tip_hash: start_hash, + ancestors: Vec::new(), declared: None, current_height: start_height, buffered_headers: Vec::new(), @@ -60,7 +64,9 @@ impl SegmentState { network .send(HeadersRequest { height: self.current_height, - locator: self.current_tip_hash, + locator: iter::once(self.current_tip_hash) + .chain(self.ancestors.iter().copied()) + .collect(), }) .await; if self.declared == Some(self.current_tip_hash) { diff --git a/dash-spv/src/sync/block_headers/sync_manager.rs b/dash-spv/src/sync/block_headers/sync_manager.rs index 8efb16ab7..915c1f129 100644 --- a/dash-spv/src/sync/block_headers/sync_manager.rs +++ b/dash-spv/src/sync/block_headers/sync_manager.rs @@ -10,6 +10,7 @@ use async_trait::async_trait; use dashcore::network::message::NetworkMessage; use dashcore::network::message_blockdata::GetHeadersMessage; use dashcore::BlockHash; +use std::iter; use std::net::SocketAddr; use std::sync::Arc; use std::time::{Duration, Instant}; @@ -58,7 +59,8 @@ impl SyncManager for BlockHeadersMana let target_height = self.progress.target_height(); // Initialize the pipeline with checkpoint-based segments - self.pipeline.init(tip.height(), *tip.hash(), target_height); + let ancestors = self.locator_ancestors().await?; + self.pipeline.init(tip.height(), *tip.hash(), ancestors, target_height); tracing::info!( "Starting parallel header sync from {} to {} ({} segments)", @@ -224,6 +226,8 @@ impl SyncManager for BlockHeadersMana && !self.pipeline.tip_segment_has_pending_request() { let tip = self.tip().await?; + let locator = + iter::once(*tip.hash()).chain(self.locator_ancestors().await?).collect(); tracing::info!("Announcing tip {} to new peer {}", tip.height(), address); // Peer-pinned send: this must reach THIS peer so it learns our // tip, not whichever peer the router would otherwise pick. @@ -231,7 +235,7 @@ impl SyncManager for BlockHeadersMana .send_to( *address, NetworkMessage::GetHeaders(GetHeadersMessage::new( - vec![*tip.hash()], + locator, BlockHash::all_zeros(), )), ) diff --git a/dash-spv/src/sync/filter_headers/manager.rs b/dash-spv/src/sync/filter_headers/manager.rs index 229f7da03..ee198a383 100644 --- a/dash-spv/src/sync/filter_headers/manager.rs +++ b/dash-spv/src/sync/filter_headers/manager.rs @@ -276,7 +276,7 @@ impl FilterHeadersManager ) -> SyncResult> { // Get current filter tip let filter_headers_tip = - self.filter_header_storage.read().await.get_filter_tip_height().await?.unwrap_or(0); + self.filter_header_storage.read().await.get_filter_tip_height().await?; // Get header start height (for checkpoint sync) let header_start_height = @@ -284,11 +284,11 @@ impl FilterHeadersManager // Calculate start height let start_height = match filter_headers_tip { - 0 => header_start_height, - n => (n + 1).max(header_start_height), + None => header_start_height, + Some(n) => (n + 1).max(header_start_height), }; - self.progress.update_current_height(filter_headers_tip); + self.progress.update_current_height(filter_headers_tip.unwrap_or(0)); // Check if already at target (nothing to download) if start_height > self.progress.block_header_tip_height() { @@ -307,7 +307,7 @@ impl FilterHeadersManager // Track checkpoint start height for storing prev header on first batch. // Only needed on fresh checkpoint sync (no existing filter headers). // On resume, start_height-1 is already stored so re-inserting would panic in debug builds. - if start_height > 0 && filter_headers_tip == 0 { + if start_height > 0 && filter_headers_tip.is_none() { self.checkpoint_start_height = Some(start_height); } diff --git a/dash-spv/tests/dashd_sync/tests_fork.rs b/dash-spv/tests/dashd_sync/tests_fork.rs index 0ffe024d1..6f1c757d1 100644 --- a/dash-spv/tests/dashd_sync/tests_fork.rs +++ b/dash-spv/tests/dashd_sync/tests_fork.rs @@ -1,7 +1,7 @@ use std::collections::HashSet; use std::time::Duration; -use dash_spv::network::PeerNetworkManager; +use dash_spv::network::{NetworkEvent, PeerNetworkManager}; use dash_spv::test_utils::{create_test_wallet, next_unused_receive_address, TestChain}; use dash_spv::{DashSpvClient, Network}; use dashcore::{Address, Amount}; @@ -9,8 +9,8 @@ use key_wallet_manager::WalletEvent; use serde_json::json; use super::helpers::{ - build_and_sign, count_wallet_transactions, wait_for_sync, wait_for_wallet_synced, PeerTracker, - EMPTY_MNEMONIC, + build_and_sign, count_wallet_transactions, wait_for_network_event, wait_for_sync, + wait_for_wallet_synced, PeerTracker, EMPTY_MNEMONIC, }; use super::setup::{create_and_start_client, TestContext}; @@ -38,7 +38,7 @@ async fn test_sync_follows_reorg() { let miner_address = node.get_new_address(); let funding_amount = 500_000_000; let address = next_unused_receive_address(&wallet, &wallet_id).await; - let funding_txid = node.send_to_address(&address, Amount::from_sat(funding_amount)); + node.send_to_address(&address, Amount::from_sat(funding_amount)); node.generate_blocks(1, &miner_address); let fork_height = ctx.dashd.initial_height + 1; wait_for_sync(&mut client_handle.progress_receiver, fork_height).await; @@ -112,12 +112,6 @@ async fn test_sync_follows_reorg() { assert_eq!(balance.confirmed(), funding_amount); assert_eq!(count_wallet_transactions(&wallet, &wallet_id).await, 1); - let funding_outpoint = spend - .input - .iter() - .map(|input| input.previous_output) - .find(|outpoint| outpoint.txid == funding_txid) - .expect("the spend takes the funding output"); let mut truncations = Vec::new(); while let Ok(event) = events.try_recv() { if let WalletEvent::ChainTruncated { @@ -135,9 +129,10 @@ async fn test_sync_follows_reorg() { )); } } + // Without a ChainLock the fork drops the chain down to genesis, funding included. assert_eq!( truncations, - vec![(fork_height, 2, true, vec![funding_outpoint])], + vec![(0, 3, true, vec![])], "the persister is told what to drop and what to restore" ); @@ -153,3 +148,94 @@ async fn test_sync_follows_reorg() { .await .expect("the storage must be released once stop returns"); } + +/// Verify a client restarted on a tip the network replaced while it was stopped +/// follows the network. The chain is longer than one `headers` response, so a +/// peer that does not know the stored tip answers with headers the client stores. +#[tokio::test] +async fn test_restart_follows_reorg_while_stopped() { + let Some(ctx) = TestContext::new(TestChain::Full).await else { + return; + }; + if !ctx.dashd.supports_mining { + eprintln!("Skipping test (dashd RPC miner not available)"); + return; + } + let mut client_handle = ctx.spawn_new_client().await; + wait_for_sync(&mut client_handle.progress_receiver, ctx.dashd.initial_height).await; + client_handle.stop().await; + drop(client_handle); + + let node = &ctx.dashd.node; + node.invalidate_block(&node.get_best_block_hash()); + node.generate_blocks(2, &node.get_new_address_from_wallet("default")); + let new_tip = ctx.dashd.initial_height + 1; + + let mut client_handle = ctx.spawn_new_client().await; + tokio::time::timeout( + Duration::from_secs(60), + wait_for_sync(&mut client_handle.progress_receiver, new_tip), + ) + .await + .expect("client did not follow the reorg it missed while stopped"); + let tip = client_handle.client.tip_header().await.expect("tip"); + assert_eq!(*tip.hash(), node.get_best_block_hash()); +} + +/// Verify a synced client follows a reorg the peer announces with `inv`, which it does +/// for a branch of more than 8 blocks. The client asks for headers from its tip, so its +/// locator must reach the fork point even though the tip moved since the sync started. +#[tokio::test] +async fn test_sync_follows_reorg_announced_by_inv() { + let Some(ctx) = TestContext::new(TestChain::Full).await else { + return; + }; + if !ctx.dashd.supports_mining { + eprintln!("Skipping test (dashd RPC miner not available)"); + return; + } + let mut client_handle = ctx.spawn_new_client().await; + wait_for_sync(&mut client_handle.progress_receiver, ctx.dashd.initial_height).await; + + // Build the branch offline and set it aside, so the client hears of it only + // when the whole branch becomes active at once. + let node = &ctx.dashd.node; + node.set_network_active(false); + assert!( + wait_for_network_event( + &mut client_handle.network_event_receiver, + |e| matches!(e, NetworkEvent::PeerDisconnected { .. }), + Duration::from_secs(30), + ) + .await, + "the client did not disconnect" + ); + let replaced_tip = node.get_best_block_hash(); + node.invalidate_block(&replaced_tip); + let branch = node.generate_blocks(10, &node.get_new_address_from_wallet("default")); + node.invalidate_block(&branch[0]); + node.try_rpc_call("reconsiderblock", &[json!(replaced_tip.to_string())]) + .expect("reconsiderblock failed"); + node.set_network_active(true); + assert!( + wait_for_network_event( + &mut client_handle.network_event_receiver, + |e| matches!(e, NetworkEvent::PeerConnected { .. }), + Duration::from_secs(30), + ) + .await, + "the client did not reconnect" + ); + + node.try_rpc_call("reconsiderblock", &[json!(branch[0].to_string())]) + .expect("reconsiderblock failed"); + let new_tip = ctx.dashd.initial_height + 9; + tokio::time::timeout( + Duration::from_secs(60), + wait_for_sync(&mut client_handle.progress_receiver, new_tip), + ) + .await + .expect("client did not follow the reorg announced by inv"); + let tip = client_handle.client.tip_header().await.expect("tip"); + assert_eq!(*tip.hash(), node.get_best_block_hash()); +}