File tree Expand file tree Collapse file tree 3 files changed +20
-9
lines changed
Expand file tree Collapse file tree 3 files changed +20
-9
lines changed Original file line number Diff line number Diff line change @@ -44,6 +44,9 @@ rand = "0.8"
4444hex = " 0.4"
4545indexmap = " 2.0"
4646
47+ # Parallelization
48+ rayon = " 1.11"
49+
4750# Terminal UI (optional)
4851crossterm = { version = " 0.27" , optional = true }
4952
Original file line number Diff line number Diff line change @@ -16,6 +16,7 @@ use crate::sync::headers::validate_headers;
1616use crate :: sync:: headers2:: Headers2StateManager ;
1717use crate :: types:: { CachedHeader , ChainState } ;
1818use std:: sync:: Arc ;
19+ use rayon:: iter:: { IntoParallelRefIterator , ParallelIterator } ;
1920use tokio:: sync:: RwLock ;
2021
2122/// Configuration for reorg handling
@@ -247,8 +248,15 @@ impl<S: StorageManager + Send + Sync + 'static, N: NetworkManager + Send + Sync
247248
248249 // Wrap headers in CachedHeader to avoid redundant X11 hashing
249250 // This prevents recomputing hashes during validation, logging, and storage
250- let cached_headers: Vec < CachedHeader > =
251- headers. iter ( ) . map ( |h| CachedHeader :: new ( * h) ) . collect ( ) ;
251+ // Precalculate the block hashes in parallel
252+ let cached_headers: Vec < CachedHeader > = headers
253+ . par_iter ( )
254+ . map ( |h| {
255+ let cached = CachedHeader :: new ( * h) ;
256+ let _ = cached. block_hash ( ) ;
257+ cached
258+ } )
259+ . collect ( ) ;
252260
253261 // Step 2: Validate Batch
254262 let first_cached = & cached_headers[ 0 ] ;
Original file line number Diff line number Diff line change 11//! Header validation functionality.
22
33use dashcore:: { block:: Header as BlockHeader , error:: Error as DashError , BlockHash } ;
4+ use rayon:: iter:: { IntoParallelRefIterator , ParallelIterator } ;
45use std:: time:: Instant ;
56
67use crate :: error:: { ValidationError , ValidationResult } ;
@@ -58,7 +59,7 @@ impl ValidationHeader for CachedHeader {
5859/// Validate a chain of headers considering the validation mode.
5960pub fn validate_headers < H > ( headers : & [ H ] , mode : ValidationMode ) -> ValidationResult < ( ) >
6061where
61- H : ValidationHeader ,
62+ H : ValidationHeader + Send + Sync ,
6263{
6364 if mode == ValidationMode :: None {
6465 tracing:: debug!( "Skipping header validation: disabled" ) ;
@@ -82,15 +83,14 @@ where
8283 ) ) ;
8384 }
8485 }
85-
86- if mode == ValidationMode :: Full {
87- // Validate proof of work with X11 hashing
88- header. validate_pow ( ) ?;
89- }
90-
9186 prev_block_hash = Some ( header. block_hash ( ) ) ;
9287 }
9388
89+ if mode == ValidationMode :: Full {
90+ // Parallelized proof of work validation
91+ headers. par_iter ( ) . try_for_each ( |header| header. validate_pow ( ) ) ?;
92+ }
93+
9494 tracing:: debug!(
9595 "Header chain validation passed for {} headers in mode: {:?}, duration: {:?}" ,
9696 headers. len( ) ,
You can’t perform that action at this time.
0 commit comments