@@ -45,6 +45,7 @@ pub struct HeaderDownloader {
4545 pivot : Pivot ,
4646 request_time : Option < Instant > ,
4747 downloaded : HashMap < H256 , Header > ,
48+ queued : HashMap < H256 , Header > ,
4849 trial : usize ,
4950}
5051
@@ -69,6 +70,7 @@ impl HeaderDownloader {
6970 } ,
7071 request_time : None ,
7172 downloaded : HashMap :: new ( ) ,
73+ queued : HashMap :: new ( ) ,
7274 trial : 0 ,
7375 }
7476 }
@@ -100,12 +102,15 @@ impl HeaderDownloader {
100102 self . request_time . map_or ( false , |time| ( Instant :: now ( ) - time) . as_secs ( ) > MAX_WAIT )
101103 }
102104
103- /// Find header from download cache, and then from blockchain
105+ /// Find header from queued headers, downloaded cache and then from blockchain
104106 /// Panics if header dosn't exist
105107 fn pivot_header ( & self ) -> Header {
106- match self . downloaded . get ( & self . pivot . hash ) {
108+ match self . queued . get ( & self . pivot . hash ) {
107109 Some ( header) => header. clone ( ) ,
108- None => self . client . block_header ( & BlockId :: Hash ( self . pivot . hash ) ) . unwrap ( ) ,
110+ None => match self . downloaded . get ( & self . pivot . hash ) {
111+ Some ( header) => header. clone ( ) ,
112+ None => self . client . block_header ( & BlockId :: Hash ( self . pivot . hash ) ) . unwrap ( ) ,
113+ } ,
109114 }
110115 }
111116
@@ -143,7 +148,7 @@ impl HeaderDownloader {
143148 if self . best_hash == self . pivot . hash {
144149 ctrace ! ( SYNC , "Ignore received headers, pivot already reached the best hash" ) ;
145150 } else if first_header_hash == self . pivot . hash {
146- for header in headers. iter ( ) {
151+ for header in headers[ 1 .. ] . iter ( ) {
147152 self . downloaded . insert ( header. hash ( ) , header. clone ( ) ) ;
148153 }
149154
@@ -173,7 +178,7 @@ impl HeaderDownloader {
173178
174179 pub fn mark_as_imported ( & mut self , hashes : Vec < H256 > ) {
175180 for hash in hashes {
176- self . downloaded . remove ( & hash) ;
181+ self . queued . remove ( & hash) ;
177182
178183 if self . best_hash == hash {
179184 self . pivot = Pivot {
@@ -183,4 +188,12 @@ impl HeaderDownloader {
183188 }
184189 }
185190 }
191+
192+ pub fn mark_as_queued ( & mut self , hashes : Vec < H256 > ) {
193+ for hash in hashes {
194+ if let Some ( header) = self . downloaded . remove ( & hash) {
195+ self . queued . insert ( hash, header) ;
196+ }
197+ }
198+ }
186199}
0 commit comments