|
1 | 1 | //! Header validation functionality. |
2 | 2 |
|
3 | | -use dashcore::{ |
4 | | - block::Header as BlockHeader, error::Error as DashError, network::constants::NetworkExt, |
5 | | - Network, |
6 | | -}; |
| 3 | +use dashcore::{block::Header as BlockHeader, error::Error as DashError}; |
| 4 | +use std::time::Instant; |
7 | 5 |
|
8 | 6 | use crate::error::{ValidationError, ValidationResult}; |
9 | 7 | use crate::types::ValidationMode; |
10 | 8 |
|
11 | | -/// Validates block headers. |
12 | | -pub struct HeaderValidator { |
13 | | - mode: ValidationMode, |
14 | | - network: Network, |
15 | | -} |
16 | | - |
17 | | -impl HeaderValidator { |
18 | | - /// Create a new header validator. |
19 | | - pub fn new(mode: ValidationMode) -> Self { |
20 | | - Self { |
21 | | - mode, |
22 | | - network: Network::Dash, // Default to mainnet |
23 | | - } |
| 9 | +/// Validate a chain of headers considering the validation mode. |
| 10 | +pub fn validate_headers(headers: &[BlockHeader], mode: ValidationMode) -> ValidationResult<()> { |
| 11 | + if mode == ValidationMode::None { |
| 12 | + tracing::debug!("Skipping header validation: disabled"); |
| 13 | + return Ok(()); |
24 | 14 | } |
25 | 15 |
|
26 | | - /// Set validation mode. |
27 | | - pub fn set_mode(&mut self, mode: ValidationMode) { |
28 | | - self.mode = mode; |
29 | | - } |
30 | | - |
31 | | - /// Set network. |
32 | | - pub fn set_network(&mut self, network: Network) { |
33 | | - self.network = network; |
34 | | - } |
| 16 | + let start = Instant::now(); |
35 | 17 |
|
36 | | - /// Validate a single header. |
37 | | - pub fn validate( |
38 | | - &self, |
39 | | - header: &BlockHeader, |
40 | | - prev_header: Option<&BlockHeader>, |
41 | | - ) -> ValidationResult<()> { |
42 | | - match self.mode { |
43 | | - ValidationMode::None => Ok(()), |
44 | | - ValidationMode::Basic => self.validate_basic(header, prev_header), |
45 | | - ValidationMode::Full => self.validate_full(header, prev_header), |
46 | | - } |
47 | | - } |
48 | | - |
49 | | - /// Basic header validation (structure and chain continuity). |
50 | | - fn validate_basic( |
51 | | - &self, |
52 | | - header: &BlockHeader, |
53 | | - prev_header: Option<&BlockHeader>, |
54 | | - ) -> ValidationResult<()> { |
| 18 | + let mut prev_block_hash = None; |
| 19 | + for header in headers { |
55 | 20 | // Check chain continuity if we have previous header |
56 | | - if let Some(prev) = prev_header { |
57 | | - if header.prev_blockhash != prev.block_hash() { |
| 21 | + if let Some(prev) = prev_block_hash { |
| 22 | + if header.prev_blockhash != prev { |
58 | 23 | return Err(ValidationError::InvalidHeaderChain( |
59 | 24 | "Header does not connect to previous header".to_string(), |
60 | 25 | )); |
61 | 26 | } |
62 | 27 | } |
63 | 28 |
|
64 | | - Ok(()) |
65 | | - } |
66 | | - |
67 | | - /// Full header validation (includes PoW verification). |
68 | | - fn validate_full( |
69 | | - &self, |
70 | | - header: &BlockHeader, |
71 | | - prev_header: Option<&BlockHeader>, |
72 | | - ) -> ValidationResult<()> { |
73 | | - // First do basic validation |
74 | | - self.validate_basic(header, prev_header)?; |
75 | | - |
76 | | - // Validate proof of work with X11 hashing (now enabled with core-block-hash-use-x11 feature) |
77 | | - let target = header.target(); |
78 | | - if let Err(e) = header.validate_pow(target) { |
79 | | - match e { |
80 | | - DashError::BlockBadProofOfWork => { |
81 | | - return Err(ValidationError::InvalidProofOfWork); |
82 | | - } |
83 | | - DashError::BlockBadTarget => { |
84 | | - return Err(ValidationError::InvalidHeaderChain("Invalid target".to_string())); |
85 | | - } |
86 | | - _ => { |
87 | | - return Err(ValidationError::InvalidHeaderChain(format!( |
| 29 | + if mode == ValidationMode::Full { |
| 30 | + // Validate proof of work with X11 hashing |
| 31 | + let target = header.target(); |
| 32 | + if let Err(e) = header.validate_pow(target) { |
| 33 | + return match e { |
| 34 | + DashError::BlockBadProofOfWork => Err(ValidationError::InvalidProofOfWork), |
| 35 | + DashError::BlockBadTarget => { |
| 36 | + Err(ValidationError::InvalidHeaderChain("Invalid target".to_string())) |
| 37 | + } |
| 38 | + _ => Err(ValidationError::InvalidHeaderChain(format!( |
88 | 39 | "PoW validation error: {:?}", |
89 | 40 | e |
90 | | - ))); |
91 | | - } |
92 | | - } |
93 | | - } |
94 | | - |
95 | | - Ok(()) |
96 | | - } |
97 | | - |
98 | | - /// Validate a chain of headers with basic validation. |
99 | | - pub fn validate_chain_basic(&self, headers: &[BlockHeader]) -> ValidationResult<()> { |
100 | | - // Respect ValidationMode::None |
101 | | - if self.mode == ValidationMode::None { |
102 | | - return Ok(()); |
103 | | - } |
104 | | - |
105 | | - if headers.is_empty() { |
106 | | - return Ok(()); |
107 | | - } |
108 | | - |
109 | | - // Validate chain continuity |
110 | | - for i in 1..headers.len() { |
111 | | - let header = &headers[i]; |
112 | | - let prev_header = &headers[i - 1]; |
113 | | - |
114 | | - self.validate_basic(header, Some(prev_header))?; |
115 | | - } |
116 | | - |
117 | | - tracing::debug!("Basic header chain validation passed for {} headers", headers.len()); |
118 | | - Ok(()) |
119 | | - } |
120 | | - |
121 | | - /// Validate a chain of headers with full validation. |
122 | | - pub fn validate_chain_full( |
123 | | - &self, |
124 | | - headers: &[BlockHeader], |
125 | | - validate_pow: bool, |
126 | | - ) -> ValidationResult<()> { |
127 | | - // Respect ValidationMode::None |
128 | | - if self.mode == ValidationMode::None { |
129 | | - return Ok(()); |
130 | | - } |
131 | | - |
132 | | - if headers.is_empty() { |
133 | | - return Ok(()); |
134 | | - } |
135 | | - |
136 | | - // For the first header, we might need to check it connects to genesis or our existing chain |
137 | | - // For now, we'll just validate internal chain continuity |
138 | | - |
139 | | - // Validate each header in the chain |
140 | | - for i in 0..headers.len() { |
141 | | - let header = &headers[i]; |
142 | | - let prev_header = if i > 0 { |
143 | | - Some(&headers[i - 1]) |
144 | | - } else { |
145 | | - None |
146 | | - }; |
147 | | - |
148 | | - if validate_pow { |
149 | | - self.validate_full(header, prev_header)?; |
150 | | - } else { |
151 | | - self.validate_basic(header, prev_header)?; |
| 41 | + ))), |
| 42 | + }; |
152 | 43 | } |
153 | 44 | } |
154 | 45 |
|
155 | | - tracing::debug!("Full header chain validation passed for {} headers", headers.len()); |
156 | | - Ok(()) |
| 46 | + prev_block_hash = Some(header.block_hash()); |
157 | 47 | } |
158 | 48 |
|
159 | | - /// Validate headers connect to genesis block. |
160 | | - pub fn validate_connects_to_genesis(&self, headers: &[BlockHeader]) -> ValidationResult<()> { |
161 | | - if headers.is_empty() { |
162 | | - return Ok(()); |
163 | | - } |
164 | | - |
165 | | - let genesis_hash = self.network.known_genesis_block_hash().ok_or_else(|| { |
166 | | - ValidationError::Consensus("No known genesis hash for network".to_string()) |
167 | | - })?; |
168 | | - |
169 | | - if headers[0].prev_blockhash != genesis_hash { |
170 | | - return Err(ValidationError::InvalidHeaderChain( |
171 | | - "First header doesn't connect to genesis".to_string(), |
172 | | - )); |
173 | | - } |
174 | | - |
175 | | - Ok(()) |
176 | | - } |
177 | | - |
178 | | - /// Validate difficulty adjustment (simplified for SPV). |
179 | | - pub fn validate_difficulty_adjustment( |
180 | | - &self, |
181 | | - header: &BlockHeader, |
182 | | - prev_header: &BlockHeader, |
183 | | - ) -> ValidationResult<()> { |
184 | | - // For SPV client, we trust that the network has validated difficulty properly |
185 | | - // We only check basic constraints |
186 | | - |
187 | | - // For SPV we trust the network for difficulty validation |
188 | | - // TODO: Implement proper difficulty validation if needed |
189 | | - let _prev_target = prev_header.target(); |
190 | | - let _current_target = header.target(); |
191 | | - |
192 | | - Ok(()) |
193 | | - } |
| 49 | + tracing::debug!( |
| 50 | + "Header chain validation passed for {} headers in mode: {:?}, duration: {:?}", |
| 51 | + headers.len(), |
| 52 | + mode, |
| 53 | + start.elapsed(), |
| 54 | + ); |
| 55 | + Ok(()) |
194 | 56 | } |
195 | 57 |
|
196 | 58 | #[cfg(test)] |
|
0 commit comments