Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions dash-spv/src/network/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,21 +80,18 @@ 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<BlockHash>,
}

impl Request for HeadersRequest {
fn height(&self) -> u32 {
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()))
}
}

Expand Down
102 changes: 62 additions & 40 deletions dash-spv/src/sync/block_headers/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,31 @@ impl<H: BlockHeaderStorage, M: MetadataStorage> BlockHeadersManager<H, M> {
.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<Vec<BlockHash>> {
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;
Comment thread
ZocoLini marked this conversation as resolved.
Resolved
}
}
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
Expand Down Expand Up @@ -335,11 +360,24 @@ impl<H: BlockHeaderStorage, M: MetadataStorage> BlockHeadersManager<H, M> {
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<Option<u32>> {
Comment thread
ZocoLini marked this conversation as resolved.
Resolved
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::<ChainLock>(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 {
Expand All @@ -348,40 +386,18 @@ impl<H: BlockHeaderStorage, M: MetadataStorage> BlockHeadersManager<H, M> {
}

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::<ChainLock>(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)
Expand Down Expand Up @@ -431,6 +447,12 @@ impl<H: BlockHeaderStorage, M: MetadataStorage> BlockHeadersManager<H, M> {
});
}
}
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.
Expand Down Expand Up @@ -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<u32>,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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
}
Expand All @@ -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());
Expand Down Expand Up @@ -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<HashedBlockHeader> = Header::dummy_chain(1, BlockHash::dummy(2))
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);

Expand Down
29 changes: 23 additions & 6 deletions dash-spv/src/sync/block_headers/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<BlockHash>,
target_height: u32,
) {
self.segments.clear();
self.next_to_store = 0;
self.initialized = true;
Expand Down Expand Up @@ -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 {}",
Expand Down Expand Up @@ -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<BlockHash>) {
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
Expand Down Expand Up @@ -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
Expand All @@ -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());
Expand All @@ -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<dyn NetworkManager> = mock.clone();
Expand Down Expand Up @@ -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());
}
Expand All @@ -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());
Expand Down
8 changes: 7 additions & 1 deletion dash-spv/src/sync/block_headers/segment_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -17,6 +18,8 @@ pub(super) struct SegmentState {
target_hash: Option<BlockHash>,
/// 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<BlockHash>,
/// Locator of the last declared GetHeaders that is still unanswered.
declared: Option<BlockHash>,
/// Current height reached in this segment.
Expand All @@ -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(),
Expand All @@ -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) {
Expand Down
8 changes: 6 additions & 2 deletions dash-spv/src/sync/block_headers/sync_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -58,7 +59,8 @@ impl<H: BlockHeaderStorage, M: MetadataStorage> 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)",
Expand Down Expand Up @@ -224,14 +226,16 @@ impl<H: BlockHeaderStorage, M: MetadataStorage> 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.
network
.send_to(
*address,
NetworkMessage::GetHeaders(GetHeadersMessage::new(
vec![*tip.hash()],
locator,
BlockHash::all_zeros(),
)),
)
Expand Down
Loading
Loading