@@ -45,6 +45,7 @@ pub struct HeaderDownloader {
45
45
pivot : Pivot ,
46
46
request_time : Option < Instant > ,
47
47
downloaded : HashMap < H256 , Header > ,
48
+ queued : HashMap < H256 , Header > ,
48
49
trial : usize ,
49
50
}
50
51
@@ -69,6 +70,7 @@ impl HeaderDownloader {
69
70
} ,
70
71
request_time : None ,
71
72
downloaded : HashMap :: new ( ) ,
73
+ queued : HashMap :: new ( ) ,
72
74
trial : 0 ,
73
75
}
74
76
}
@@ -100,12 +102,17 @@ impl HeaderDownloader {
100
102
self . request_time . map_or ( false , |time| ( Instant :: now ( ) - time) . as_secs ( ) > MAX_WAIT )
101
103
}
102
104
103
- /// Find header from download cache, and then from blockchain
105
+ /// Find header from queued headers, downloaded cache and then from blockchain
104
106
/// Panics if header dosn't exist
105
107
fn pivot_header ( & self ) -> Header {
106
- match self . downloaded . get ( & self . pivot . hash ) {
108
+ match self . queued . get ( & self . pivot . hash ) {
107
109
Some ( header) => header. clone ( ) ,
108
- None => self . client . block_header ( & BlockId :: Hash ( self . pivot . hash ) ) . unwrap ( ) ,
110
+ None => self
111
+ . downloaded
112
+ . get ( & self . pivot . hash )
113
+ . map ( Clone :: clone)
114
+ . or_else ( || self . client . block_header ( & BlockId :: Hash ( self . pivot . hash ) ) )
115
+ . unwrap ( ) ,
109
116
}
110
117
}
111
118
@@ -143,7 +150,7 @@ impl HeaderDownloader {
143
150
if self . best_hash == self . pivot . hash {
144
151
ctrace ! ( SYNC , "Ignore received headers, pivot already reached the best hash" ) ;
145
152
} else if first_header_hash == self . pivot . hash {
146
- for header in headers. iter ( ) {
153
+ for header in headers[ 1 .. ] . iter ( ) {
147
154
self . downloaded . insert ( header. hash ( ) , header. clone ( ) ) ;
148
155
}
149
156
@@ -173,7 +180,7 @@ impl HeaderDownloader {
173
180
174
181
pub fn mark_as_imported ( & mut self , hashes : Vec < H256 > ) {
175
182
for hash in hashes {
176
- self . downloaded . remove ( & hash) ;
183
+ self . queued . remove ( & hash) ;
177
184
178
185
if self . best_hash == hash {
179
186
self . pivot = Pivot {
@@ -183,4 +190,12 @@ impl HeaderDownloader {
183
190
}
184
191
}
185
192
}
193
+
194
+ pub fn mark_as_queued ( & mut self , hashes : Vec < H256 > ) {
195
+ for hash in hashes {
196
+ if let Some ( header) = self . downloaded . remove ( & hash) {
197
+ self . queued . insert ( hash, header) ;
198
+ }
199
+ }
200
+ }
186
201
}
0 commit comments