@@ -13,14 +13,10 @@ use super::{
1313 error:: { CFHeaderSyncError , CFilterSyncError , HeaderSyncError } ,
1414 graph:: { AcceptHeaderChanges , BlockTree , HeaderRejection } ,
1515 CFHeaderChanges , ChainState , Filter , FilterCheck , FilterHeaderRequest , FilterRequest ,
16- FilterRequestState , HeightMonitor , PeerId ,
16+ FilterRequestState , HeaderValidationExt , HeightMonitor , PeerId ,
1717} ;
1818use crate :: IndexedFilter ;
19- use crate :: {
20- chain:: { header_batch:: HeadersBatch , BlockHeaderChanges } ,
21- messages:: Event ,
22- Dialog , Info , Progress ,
23- } ;
19+ use crate :: { chain:: BlockHeaderChanges , messages:: Event , Dialog , Info , Progress } ;
2420
2521const FILTER_BASIC : u8 = 0x00 ;
2622const CF_HEADER_BATCH_SIZE : u32 = 1_999 ;
@@ -91,11 +87,18 @@ impl Chain {
9187 // Sync the chain with headers from a peer, adjusting to reorgs if needed
9288 pub ( crate ) fn sync_chain (
9389 & mut self ,
94- message : Vec < Header > ,
90+ header_batch : Vec < Header > ,
9591 ) -> Result < Vec < BlockHash > , HeaderSyncError > {
96- let header_batch = HeadersBatch :: new ( message) . map_err ( |_| HeaderSyncError :: EmptyMessage ) ?;
92+ if header_batch. is_empty ( ) {
93+ return Err ( HeaderSyncError :: EmptyMessage ) ;
94+ }
9795 // If our chain already has the last header in the message there is no new information
98- if self . header_chain . contains ( header_batch. last ( ) . block_hash ( ) ) {
96+ if self . header_chain . contains (
97+ header_batch
98+ . last ( )
99+ . expect ( "non-empty check above." )
100+ . block_hash ( ) ,
101+ ) {
99102 return Ok ( Vec :: new ( ) ) ;
100103 }
101104 // We check first if the peer is sending us nonsense
@@ -153,21 +156,16 @@ impl Chain {
153156 }
154157
155158 // These are invariants in all batches of headers we receive
156- fn sanity_check ( & mut self , header_batch : & HeadersBatch ) -> Result < ( ) , HeaderSyncError > {
157- // All the headers connect with each other and is the difficulty adjustment not absurd
159+ fn sanity_check ( & mut self , header_batch : & [ Header ] ) -> Result < ( ) , HeaderSyncError > {
158160 if !header_batch. connected ( ) {
159161 return Err ( HeaderSyncError :: HeadersNotConnected ) ;
160162 }
161-
162- // All headers pass their own proof of work and the network minimum
163- if !header_batch. individually_valid_pow ( ) {
163+ if !header_batch. passes_own_pow ( ) {
164164 return Err ( HeaderSyncError :: InvalidHeaderWork ) ;
165165 }
166-
167- if !header_batch. bits_adhere_transition ( self . network ) {
166+ if !header_batch. bits_adhere_transition_threshold ( self . network ) {
168167 return Err ( HeaderSyncError :: InvalidBits ) ;
169168 }
170-
171169 Ok ( ( ) )
172170 }
173171
0 commit comments