@@ -19,7 +19,7 @@ use std::collections::{HashSet, HashMap, hash_map};
19
19
use bytes:: Bytes ;
20
20
use ethereum_types:: H256 ;
21
21
use keccak_hash:: { keccak, KECCAK_NULL_RLP , KECCAK_EMPTY_LIST_RLP } ;
22
- use log:: { trace, warn} ;
22
+ use log:: { debug , trace, warn} ;
23
23
use parity_util_mem:: MallocSizeOf ;
24
24
use rlp:: { Rlp , RlpStream , DecoderError } ;
25
25
use triehash_ethereum:: ordered_trie_root;
@@ -103,7 +103,7 @@ fn unverified_from_sync(header: SyncHeader, body: Option<SyncBody>) -> Unverifie
103
103
header : header. header ,
104
104
transactions : body. transactions ,
105
105
uncles : body. uncles ,
106
- bytes : stream. out ( ) . to_vec ( ) ,
106
+ bytes : stream. out ( ) ,
107
107
}
108
108
}
109
109
@@ -196,11 +196,11 @@ impl BlockCollection {
196
196
}
197
197
198
198
/// Insert a collection of block receipts for previously downloaded headers.
199
- pub fn insert_receipts ( & mut self , receipts : Vec < Bytes > ) -> Vec < Vec < H256 > > {
199
+ pub fn insert_receipts ( & mut self , receipts : & [ & [ u8 ] ] ) -> Vec < Vec < H256 > > {
200
200
if !self . need_receipts {
201
201
return Vec :: new ( ) ;
202
202
}
203
- receipts. into_iter ( )
203
+ receipts. iter ( )
204
204
. filter_map ( |r| {
205
205
self . insert_receipt ( r)
206
206
. map_err ( |e| trace ! ( target: "sync" , "Ignored invalid receipt: {:?}" , e) )
@@ -210,24 +210,28 @@ impl BlockCollection {
210
210
}
211
211
212
212
/// Returns a set of block hashes that require a body download. The returned set is marked as being downloaded.
213
- pub fn needed_bodies ( & mut self , count : usize , _ignore_downloading : bool ) -> Vec < H256 > {
213
+ pub fn needed_bodies ( & mut self , count : usize ) -> Vec < H256 > {
214
214
if self . head . is_none ( ) {
215
215
return Vec :: new ( ) ;
216
216
}
217
- let mut needed_bodies: Vec < H256 > = Vec :: new ( ) ;
217
+ let mut needed_bodies: Vec < H256 > = Vec :: with_capacity ( count ) ;
218
218
let mut head = self . head ;
219
- while head. is_some ( ) && needed_bodies. len ( ) < count {
220
- head = self . parents . get ( & head. unwrap ( ) ) . cloned ( ) ;
221
- if let Some ( head) = head {
222
- match self . blocks . get ( & head) {
223
- Some ( block) if block. body . is_none ( ) && !self . downloading_bodies . contains ( & head) => {
224
- self . downloading_bodies . insert ( head. clone ( ) ) ;
225
- needed_bodies. push ( head. clone ( ) ) ;
219
+ while needed_bodies. len ( ) < count {
220
+ head = match head {
221
+ Some ( head) => {
222
+ match self . blocks . get ( & head) {
223
+ Some ( block) if block. body . is_none ( ) && !self . downloading_bodies . contains ( & head) => {
224
+ self . downloading_bodies . insert ( head. clone ( ) ) ;
225
+ needed_bodies. push ( head. clone ( ) ) ;
226
+ }
227
+ _ => ( ) ,
226
228
}
227
- _ => ( ) ,
228
- }
229
- }
229
+ self . parents . get ( & head) . copied ( )
230
+ } ,
231
+ None => break
232
+ } ;
230
233
}
234
+
231
235
for h in self . header_ids . values ( ) {
232
236
if needed_bodies. len ( ) >= count {
233
237
break ;
@@ -241,25 +245,28 @@ impl BlockCollection {
241
245
}
242
246
243
247
/// Returns a set of block hashes that require a receipt download. The returned set is marked as being downloaded.
244
- pub fn needed_receipts ( & mut self , count : usize , _ignore_downloading : bool ) -> Vec < H256 > {
248
+ pub fn needed_receipts ( & mut self , count : usize ) -> Vec < H256 > {
245
249
if self . head . is_none ( ) || !self . need_receipts {
246
250
return Vec :: new ( ) ;
247
251
}
248
- let mut needed_receipts: Vec < H256 > = Vec :: new ( ) ;
252
+ let mut needed_receipts: Vec < H256 > = Vec :: with_capacity ( count ) ;
249
253
let mut head = self . head ;
250
- while head. is_some ( ) && needed_receipts. len ( ) < count {
251
- head = self . parents . get ( & head. unwrap ( ) ) . cloned ( ) ;
252
- if let Some ( head) = head {
253
- match self . blocks . get ( & head) {
254
- Some ( block) => {
255
- if block. receipts . is_none ( ) && !self . downloading_receipts . contains ( & block. receipts_root ) {
256
- self . downloading_receipts . insert ( block. receipts_root ) ;
257
- needed_receipts. push ( head. clone ( ) ) ;
254
+ while needed_receipts. len ( ) < count {
255
+ head = match head {
256
+ Some ( head) => {
257
+ match self . blocks . get ( & head) {
258
+ Some ( block) => {
259
+ if block. receipts . is_none ( ) && !self . downloading_receipts . contains ( & block. receipts_root ) {
260
+ self . downloading_receipts . insert ( block. receipts_root ) ;
261
+ needed_receipts. push ( head) ;
262
+ }
258
263
}
264
+ _ => ( ) ,
259
265
}
260
- _ => ( ) ,
261
- }
262
- }
266
+ self . parents . get ( & head) . copied ( )
267
+ } ,
268
+ None => break
269
+ } ;
263
270
}
264
271
// If there are multiple blocks per receipt, only request one of them.
265
272
for ( root, h) in self . receipt_ids . iter ( ) . map ( |( root, hashes) | ( root, hashes[ 0 ] ) ) {
@@ -275,12 +282,12 @@ impl BlockCollection {
275
282
}
276
283
277
284
/// Returns a set of block hashes that require a header download. The returned set is marked as being downloaded.
278
- pub fn needed_headers ( & mut self , count : usize , ignore_downloading : bool ) -> Option < ( H256 , usize ) > {
285
+ pub fn needed_headers ( & mut self , count : usize ) -> Option < ( H256 , usize ) > {
279
286
// find subchain to download
280
287
let mut download = None ;
281
288
{
282
289
for h in & self . heads {
283
- if ignore_downloading || !self . downloading_headers . contains ( h) {
290
+ if !self . downloading_headers . contains ( h) {
284
291
self . downloading_headers . insert ( h. clone ( ) ) ;
285
292
download = Some ( h. clone ( ) ) ;
286
293
break ;
@@ -317,42 +324,40 @@ impl BlockCollection {
317
324
return Vec :: new ( ) ;
318
325
}
319
326
320
- let mut drained = Vec :: new ( ) ;
321
327
let mut hashes = Vec :: new ( ) ;
322
- {
323
- let mut blocks = Vec :: new ( ) ;
324
- let mut head = self . head ;
325
- while let Some ( h) = head {
326
- head = self . parents . get ( & h) . cloned ( ) ;
327
- if let Some ( head) = head {
328
- match self . blocks . remove ( & head) {
329
- Some ( block) => {
330
- if block. body . is_some ( ) && ( !self . need_receipts || block. receipts . is_some ( ) ) {
331
- blocks. push ( block) ;
332
- hashes. push ( head) ;
333
- self . head = Some ( head) ;
334
- } else {
335
- self . blocks . insert ( head, block) ;
336
- break ;
337
- }
338
- } ,
339
- _ => {
328
+ let mut blocks = Vec :: new ( ) ;
329
+ let mut head = self . head ;
330
+ while let Some ( h) = head {
331
+ head = self . parents . get ( & h) . copied ( ) ;
332
+ if let Some ( head) = head {
333
+ match self . blocks . remove ( & head) {
334
+ Some ( block) => {
335
+ if block. body . is_some ( ) && ( !self . need_receipts || block. receipts . is_some ( ) ) {
336
+ blocks. push ( block) ;
337
+ hashes. push ( head) ;
338
+ self . head = Some ( head) ;
339
+ } else {
340
+ self . blocks . insert ( head, block) ;
340
341
break ;
341
- } ,
342
- }
342
+ }
343
+ } ,
344
+ _ => {
345
+ break ;
346
+ } ,
343
347
}
344
348
}
349
+ }
345
350
346
- for block in blocks. into_iter ( ) {
347
- let unverified = unverified_from_sync ( block . header , block . body ) ;
348
- drained . push ( BlockAndReceipts {
349
- block : unverified ,
350
- receipts : block . receipts . clone ( ) ,
351
- } ) ;
352
- }
351
+ let mut drained = Vec :: with_capacity ( blocks. len ( ) ) ;
352
+ for block in blocks {
353
+ let unverified = unverified_from_sync ( block . header , block . body ) ;
354
+ drained . push ( BlockAndReceipts {
355
+ block : unverified ,
356
+ receipts : block . receipts ,
357
+ } ) ;
353
358
}
354
359
355
- trace ! ( target: "sync" , "Drained {} blocks, new head :{:?}" , drained. len( ) , self . head) ;
360
+ debug ! ( target: "sync" , "Drained {} blocks, new head :{:?}" , drained. len( ) , self . head) ;
356
361
drained
357
362
}
358
363
@@ -409,7 +414,7 @@ impl BlockCollection {
409
414
}
410
415
}
411
416
412
- fn insert_receipt ( & mut self , r : Bytes ) -> Result < Vec < H256 > , network:: Error > {
417
+ fn insert_receipt ( & mut self , r : & [ u8 ] ) -> Result < Vec < H256 > , network:: Error > {
413
418
let receipt_root = {
414
419
let receipts = Rlp :: new ( & r) ;
415
420
ordered_trie_root ( receipts. iter ( ) . map ( |r| r. as_raw ( ) ) )
@@ -422,7 +427,7 @@ impl BlockCollection {
422
427
match self . blocks . get_mut ( & h) {
423
428
Some ( ref mut block) => {
424
429
trace ! ( target: "sync" , "Got receipt {}" , h) ;
425
- block. receipts = Some ( r. clone ( ) ) ;
430
+ block. receipts = Some ( r. to_vec ( ) ) ;
426
431
} ,
427
432
None => {
428
433
warn ! ( "Got receipt with no header {}" , h) ;
@@ -581,11 +586,11 @@ mod test {
581
586
bc. reset_to ( heads) ;
582
587
assert ! ( !bc. is_empty( ) ) ;
583
588
assert_eq ! ( hashes[ 0 ] , bc. heads[ 0 ] ) ;
584
- assert ! ( bc. needed_bodies( 1 , false ) . is_empty( ) ) ;
589
+ assert ! ( bc. needed_bodies( 1 ) . is_empty( ) ) ;
585
590
assert ! ( !bc. contains( & hashes[ 0 ] ) ) ;
586
591
assert ! ( !bc. is_downloading( & hashes[ 0 ] ) ) ;
587
592
588
- let ( h, n) = bc. needed_headers ( 6 , false ) . unwrap ( ) ;
593
+ let ( h, n) = bc. needed_headers ( 6 ) . unwrap ( ) ;
589
594
assert ! ( bc. is_downloading( & hashes[ 0 ] ) ) ;
590
595
assert_eq ! ( hashes[ 0 ] , h) ;
591
596
assert_eq ! ( n, 6 ) ;
@@ -608,9 +613,9 @@ mod test {
608
613
assert ! ( !bc. contains( & hashes[ 0 ] ) ) ;
609
614
assert_eq ! ( hashes[ 5 ] , bc. head. unwrap( ) ) ;
610
615
611
- let ( h, _) = bc. needed_headers ( 6 , false ) . unwrap ( ) ;
616
+ let ( h, _) = bc. needed_headers ( 6 ) . unwrap ( ) ;
612
617
assert_eq ! ( hashes[ 5 ] , h) ;
613
- let ( h, _) = bc. needed_headers ( 6 , false ) . unwrap ( ) ;
618
+ let ( h, _) = bc. needed_headers ( 6 ) . unwrap ( ) ;
614
619
assert_eq ! ( hashes[ 20 ] , h) ;
615
620
bc. insert_headers ( headers[ 10 ..16 ] . into_iter ( ) . map ( Clone :: clone) . collect ( ) ) ;
616
621
assert ! ( bc. drain( ) . is_empty( ) ) ;
0 commit comments