Skip to content
Merged
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
108 changes: 94 additions & 14 deletions dash-spv/src/sync/block_headers/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ use crate::sync::{
};
use crate::types::HashedBlockHeader;
use crate::validation::{BlockHeaderValidator, Validator};
#[cfg(test)]
use dashcore::block::Header;
use dashcore::ephemerealdata::chain_lock::ChainLock;
use dashcore::network::message_blockdata::Inventory;
Expand Down Expand Up @@ -112,27 +111,39 @@ impl<H: BlockHeaderStorage, M: MetadataStorage> BlockHeadersManager<H, M> {
.ok_or_else(|| SyncError::MissingDependency("storage not initialized".to_string()))
}

/// Validate and store headers batch.
async fn store_headers(&mut self, headers: &[HashedBlockHeader]) -> SyncResult<BlockHeaderTip> {
debug_assert!(!headers.is_empty());

// Validate batch for internal continuity, PoW and difficulty
let tip = self.tip().await?;
/// The stored headers up to `height` the next one's difficulty is computed from.
async fn ancestors(&self, height: u32) -> SyncResult<Vec<Header>> {
let storage = self.header_storage.read().await;

// A storage started at a checkpoint holds less history than a full window.
let first = tip
.height()
let first = height
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
.saturating_sub(DGW_PAST_BLOCKS as u32 - 1)
.max(storage.get_start_height().await.unwrap_or(0));

let ancestors = storage.load_headers(first..tip.height() + 1).await?;

drop(storage);
let headers = storage.load_headers(first..height + 1).await?;
Ok(headers.iter().map(|header| *header.header()).collect())
}

let ancestors = ancestors.iter().map(|header| *header.header()).collect();
/// Height and ancestors of the block `headers` extend, a stored block or a
/// segment's tip. `None` if it's neither, as then nothing follows from them.
async fn attach_point(
&self,
headers: &[HashedBlockHeader],
) -> SyncResult<Option<(u32, Vec<Header>)>> {
let Some(first) = headers.first() else {
return Ok(None);
};
let parent = first.header().prev_blockhash;
let height = self.header_storage.read().await.get_header_height_by_hash(&parent).await?;
match height {
Some(height) => Ok(Some((height, self.ancestors(height).await?))),
None => Ok(self.pipeline.segment_ancestors(&parent)),
}
}

BlockHeaderValidator::new(self.network, ancestors, tip.height()).validate(headers)?;
/// Store an already validated headers batch.
async fn store_headers(&mut self, headers: &[HashedBlockHeader]) -> SyncResult<BlockHeaderTip> {
debug_assert!(!headers.is_empty());

// Store headers
self.header_storage.write().await.store_headers(headers).await?;
Expand Down Expand Up @@ -169,6 +180,14 @@ impl<H: BlockHeaderStorage, M: MetadataStorage> BlockHeadersManager<H, M> {
None => self.pipeline.active_tip_locator(),
};

if let Some((height, ancestors)) = self.attach_point(headers).await? {
let validator = BlockHeaderValidator::new(self.network, ancestors, height);
if let Err(e) = validator.validate(headers) {
network.ban_peer(peer).await;
return Err(e.into());
}
}

// Route headers to the pipeline, validates checkpoint match.
let matched = match self.pipeline.receive_headers(headers) {
Ok(matched) => matched,
Expand Down Expand Up @@ -514,6 +533,67 @@ mod tests {
);
}

/// Headers are validated as they arrive, on the tip or branching off below it:
/// invalid ones are neither stored nor followed, and their peer is banned.
#[test_case(4, true; "valid on the tip")]
#[test_case(4, false; "invalid on the tip")]
#[test_case(2, true; "valid fork")]
#[test_case(2, false; "invalid fork")]
#[tokio::test]
async fn test_received_headers_are_validated(parent: usize, valid: bool) {
let mut storage = DiskStorageManager::with_temp_dir().await.unwrap();
let chain: Vec<HashedBlockHeader> = Header::dummy_chain(5, BlockHash::dummy(0))
.iter()
.map(HashedBlockHeader::from)
.collect();
storage.store_headers(&chain).await.unwrap();
let mut manager = BlockHeadersManager::new(
storage.block_headers(),
storage.metadata(),
create_test_checkpoint_manager(),
Network::Regtest,
)
.await
.unwrap();
manager.pipeline.init(4, *chain[4].hash(), 4);
manager.progress.set_state(SyncState::Synced);

let parent_hash = *chain[parent].hash();
let header = if valid {
Header::dummy_chain(1, parent_hash)[0]
} else {
Header {
prev_blockhash: parent_hash,
..Header::dummy(5)
}
};
let mock = Arc::new(MockNetworkManager::new());
let network: Arc<dyn NetworkManager> = mock.clone();
let peer = test_socket_address(1);

let result = manager.handle_headers_pipeline(peer, &[header.into()], &network).await;

assert_eq!(result.is_ok(), valid);
assert_eq!(
mock.banned_peers().await,
if valid {
vec![]
} else {
vec![peer]
}
);
let forked = result.is_ok_and(|events| {
events.iter().any(|event| matches!(event, SyncEvent::ForkDetected { .. }))
});
assert_eq!(forked, valid && parent < 4);
let tip = if valid && parent == 4 {
5
} else {
4
};
assert_eq!(manager.tip().await.unwrap().height(), tip);
}

/// Create a manager in synced state with an initialized pipeline.
async fn create_synced_manager() -> TestBlockHeadersManager {
let mut manager = create_test_manager().await;
Expand Down
65 changes: 64 additions & 1 deletion dash-spv/src/sync/block_headers/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
//! downloaded in parallel from multiple peers. Each segment tracks its own
//! progress and buffers headers until ready for ordered storage.

#[cfg(test)]
use dashcore::block::Header;
use dashcore::pow::DGW_PAST_BLOCKS;
use dashcore::BlockHash;
use std::sync::Arc;

Expand Down Expand Up @@ -197,6 +197,25 @@ impl HeadersPipeline {
Ok(None)
}

/// Height and ancestors of the segment whose tip is `hash`, if any: the
/// checkpoint it starts at and the headers it has buffered since.
pub(super) fn segment_ancestors(&self, hash: &BlockHash) -> Option<(u32, Vec<Header>)> {
let segment = self.segments.iter().find(|segment| segment.matches(hash))?;
let checkpoint = self
.checkpoint_manager
.last_checkpoint_before_height(segment.start_height)
.filter(|checkpoint| checkpoint.height() == segment.start_height);
let buffered = &segment.buffered_headers;
let recent = &buffered[buffered.len().saturating_sub(DGW_PAST_BLOCKS)..];
let ancestors = checkpoint
.into_iter()
.flat_map(|checkpoint| checkpoint.headers())
.chain(recent)
.map(|header| *header.header())
.collect();
Some((segment.current_height, ancestors))
}

/// Get segments that are ready to store (complete and in order).
/// Returns tuples of (start_height, headers).
pub fn take_ready_to_store(&mut self) -> Vec<(u32, Vec<HashedBlockHeader>)> {
Expand Down Expand Up @@ -673,6 +692,50 @@ mod tests {
assert!(pipeline.segments[0].buffered_headers.is_empty());
}

/// A segment's ancestors are the checkpoint it starts at, if it starts at
/// one, followed by the last headers it buffered.
#[test]
fn test_segment_ancestors() {
let cm = create_test_checkpoint_manager(true);
let checkpoint = cm.checkpoints().next().unwrap();
let segment = |id, start_height, start_hash| {
let buffered: Vec<HashedBlockHeader> =
Header::dummy_chain(30, start_hash).iter().map(HashedBlockHeader::from).collect();
let mut segment = SegmentState::new(id, start_height, start_hash, None, None);
segment.current_height = start_height + 30;
segment.current_tip_hash = *buffered[29].hash();
segment.buffered_headers = buffered;
segment
};
let mut pipeline = HeadersPipeline::new(cm);
pipeline.initialized = true;
pipeline.segments = vec![
segment(0, checkpoint.height(), *checkpoint.hash()),
// Above the checkpoint, so it must not take its headers.
segment(1, checkpoint.height() + 100, BlockHash::dummy(10)),
];
let recent = |segment: &SegmentState| -> Vec<Header> {
segment.buffered_headers[6..].iter().map(|header| *header.header()).collect()
};

let at_checkpoint = &pipeline.segments[0];
let mut expected: Vec<Header> =
checkpoint.headers().iter().map(|header| *header.header()).collect();
expected.extend(recent(at_checkpoint));
assert_eq!(
pipeline.segment_ancestors(&at_checkpoint.current_tip_hash),
Some((checkpoint.height() + 30, expected))
);

let off_checkpoint = &pipeline.segments[1];
assert_eq!(
pipeline.segment_ancestors(&off_checkpoint.current_tip_hash),
Some((checkpoint.height() + 130, recent(off_checkpoint)))
);

assert_eq!(pipeline.segment_ancestors(&BlockHash::dummy(999)), None);
}

#[test]
fn test_disconnect_preserves_segment_chain_state() {
let shared_hash = BlockHash::dummy(42);
Expand Down
3 changes: 3 additions & 0 deletions dash-spv/src/sync/block_headers/segment_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use std::sync::Arc;
pub(super) struct SegmentState {
/// Unique segment identifier (index in segments array).
pub(super) segment_id: usize,
/// Starting height of this segment.
pub(super) start_height: u32,
/// Target height (None for tip segment).
pub(super) target_height: Option<u32>,
/// Target hash (next checkpoint hash for validation).
Expand Down Expand Up @@ -36,6 +38,7 @@ impl SegmentState {
) -> Self {
Self {
segment_id,
start_height,
target_height,
target_hash,
current_tip_hash: start_hash,
Expand Down
Loading