@@ -75,7 +75,7 @@ pub struct Extension {
7575 token_generator : TokenGenerator ,
7676 client : Arc < Client > ,
7777 api : Box < dyn Api > ,
78- last_request : u64 ,
78+ nonce : u64 ,
7979}
8080
8181impl Extension {
@@ -125,7 +125,7 @@ impl Extension {
125125 token_generator : TokenGenerator :: new ( SYNC_EXPIRE_TOKEN_BEGIN , SYNC_EXPIRE_TOKEN_END ) ,
126126 client,
127127 api,
128- last_request : Default :: default ( ) ,
128+ nonce : Default :: default ( ) ,
129129 }
130130 }
131131
@@ -138,8 +138,8 @@ impl Extension {
138138 fn send_header_request ( & mut self , id : & NodeId , request : RequestMessage ) {
139139 if let Some ( requests) = self . requests . get_mut ( id) {
140140 ctrace ! ( SYNC , "Send header request to {}" , id) ;
141- let request_id = self . last_request ;
142- self . last_request += 1 ;
141+ let request_id = self . nonce ;
142+ self . nonce += 1 ;
143143 requests. push ( ( request_id, request. clone ( ) ) ) ;
144144 self . api . send ( id, Arc :: new ( Message :: Request ( request_id, request) . rlp_bytes ( ) . into_vec ( ) ) ) ;
145145 }
@@ -161,8 +161,8 @@ impl Extension {
161161
162162 if let Some ( request) = self . body_downloader . create_request ( ) {
163163 cdebug ! ( SYNC , "Request body to {} {:?}" , id, request) ;
164- let request_id = self . last_request ;
165- self . last_request += 1 ;
164+ let request_id = self . nonce ;
165+ self . nonce += 1 ;
166166 requests. push ( ( request_id, request. clone ( ) ) ) ;
167167 self . api . send ( id, Arc :: new ( Message :: Request ( request_id, request) . rlp_bytes ( ) . into_vec ( ) ) ) ;
168168
@@ -243,14 +243,15 @@ impl NetworkExtension<Event> for Extension {
243243 id,
244244 Arc :: new (
245245 Message :: Status {
246- total_score : chain_info . best_proposal_score ,
246+ nonce : U256 :: from ( self . nonce ) ,
247247 best_hash : chain_info. best_proposal_block_hash ,
248248 genesis_hash : chain_info. genesis_hash ,
249249 }
250250 . rlp_bytes ( )
251251 . into_vec ( ) ,
252252 ) ,
253253 ) ;
254+ self . nonce += 1 ;
254255 let t = self . connected_nodes . insert ( * id) ;
255256 debug_assert ! ( t, "{} is already added to peer list" , id) ;
256257
@@ -299,10 +300,10 @@ impl NetworkExtension<Event> for Extension {
299300 if let Ok ( received_message) = UntrustedRlp :: new ( data) . as_val ( ) {
300301 match received_message {
301302 Message :: Status {
302- total_score ,
303+ nonce ,
303304 best_hash,
304305 genesis_hash,
305- } => self . on_peer_status ( id, total_score , best_hash, genesis_hash) ,
306+ } => self . on_peer_status ( id, nonce , best_hash, genesis_hash) ,
306307 Message :: Request ( request_id, request) => self . on_peer_request ( id, request_id, request) ,
307308 Message :: Response ( request_id, response) => self . on_peer_response ( id, request_id, response) ,
308309 }
@@ -328,7 +329,6 @@ impl NetworkExtension<Event> for Extension {
328329 }
329330 State :: SnapshotChunk ( ..) => unimplemented ! ( ) ,
330331 State :: Full => {
331- let best_proposal_score = self . client . chain_info ( ) . best_proposal_score ;
332332 for id in & peer_ids {
333333 let request =
334334 self . header_downloaders . get_mut ( id) . and_then ( HeaderDownloader :: create_request) ;
@@ -339,13 +339,7 @@ impl NetworkExtension<Event> for Extension {
339339 }
340340
341341 for id in peer_ids {
342- let peer_score = if let Some ( peer) = self . header_downloaders . get ( & id) {
343- peer. total_score ( )
344- } else {
345- U256 :: zero ( )
346- } ;
347-
348- if peer_score > best_proposal_score {
342+ if self . header_downloaders . get ( & id) . map_or ( false , |d| !d. is_lagging ( ) ) {
349343 self . send_body_request ( & id) ;
350344 }
351345 }
@@ -501,20 +495,21 @@ impl Extension {
501495 id,
502496 Arc :: new (
503497 Message :: Status {
504- total_score : chain_info . best_proposal_score ,
498+ nonce : U256 :: from ( self . nonce ) ,
505499 best_hash : chain_info. best_proposal_block_hash ,
506500 genesis_hash : chain_info. genesis_hash ,
507501 }
508502 . rlp_bytes ( )
509503 . into_vec ( ) ,
510504 ) ,
511505 ) ;
506+ self . nonce += 1 ;
512507 }
513508 }
514509}
515510
516511impl Extension {
517- fn on_peer_status ( & mut self , from : & NodeId , total_score : U256 , best_hash : BlockHash , genesis_hash : BlockHash ) {
512+ fn on_peer_status ( & mut self , from : & NodeId , nonce : U256 , best_hash : BlockHash , genesis_hash : BlockHash ) {
518513 // Validity check
519514 if genesis_hash != self . client . chain_info ( ) . genesis_hash {
520515 cinfo ! ( SYNC , "Genesis hash mismatch with peer {}" , from) ;
@@ -523,17 +518,17 @@ impl Extension {
523518
524519 match self . header_downloaders . entry ( * from) {
525520 Entry :: Occupied ( mut peer) => {
526- if !peer. get_mut ( ) . update ( total_score , best_hash) {
521+ if !peer. get_mut ( ) . update ( nonce , best_hash) {
527522 // FIXME: It should be an error level if the consensus is PoW.
528523 cdebug ! ( SYNC , "Peer #{} status updated but score is less than before" , from) ;
529524 return
530525 }
531526 }
532527 Entry :: Vacant ( e) => {
533- e. insert ( HeaderDownloader :: new ( self . client . clone ( ) , total_score , best_hash) ) ;
528+ e. insert ( HeaderDownloader :: new ( self . client . clone ( ) , nonce , best_hash) ) ;
534529 }
535530 }
536- cinfo ! ( SYNC , "Peer #{} status update: total_score : {}, best_hash: {}" , from, total_score , best_hash) ;
531+ cinfo ! ( SYNC , "Peer #{} status update: nonce : {}, best_hash: {}" , from, nonce , best_hash) ;
537532 }
538533
539534 fn on_peer_request ( & self , from : & NodeId , id : u64 , request : RequestMessage ) {
@@ -759,14 +754,12 @@ impl Extension {
759754 }
760755 State :: SnapshotChunk ( ..) => { }
761756 State :: Full => {
762- let ( mut completed, pivot_score_changed) = if let Some ( peer) = self . header_downloaders . get_mut ( from) {
763- let before_pivot_score = peer. pivot_score ( ) ;
757+ let ( mut completed, is_lagging) = if let Some ( peer) = self . header_downloaders . get_mut ( from) {
764758 let encoded: Vec < _ > = headers. iter ( ) . map ( |h| EncodedHeader :: new ( h. rlp_bytes ( ) . to_vec ( ) ) ) . collect ( ) ;
765759 peer. import_headers ( & encoded) ;
766- let after_pivot_score = peer. pivot_score ( ) ;
767- ( peer. downloaded ( ) , before_pivot_score != after_pivot_score)
760+ ( peer. downloaded ( ) , peer. is_lagging ( ) )
768761 } else {
769- ( Vec :: new ( ) , false )
762+ ( Vec :: new ( ) , true )
770763 } ;
771764 completed. sort_unstable_by_key ( EncodedHeader :: number) ;
772765
@@ -792,7 +785,7 @@ impl Extension {
792785 peer. mark_as_imported ( exists) ;
793786 peer. create_request ( )
794787 } ) ;
795- if pivot_score_changed {
788+ if !is_lagging {
796789 if let Some ( request) = request {
797790 self . send_header_request ( from, request) ;
798791 }
@@ -834,18 +827,11 @@ impl Extension {
834827 }
835828 }
836829
837- let total_score = self . client . chain_info ( ) . best_proposal_score ;
838830 let mut peer_ids: Vec < _ > = self . header_downloaders . keys ( ) . cloned ( ) . collect ( ) ;
839831 peer_ids. shuffle ( & mut thread_rng ( ) ) ;
840832
841833 for id in peer_ids {
842- let peer_score = if let Some ( peer) = self . header_downloaders . get ( & id) {
843- peer. total_score ( )
844- } else {
845- U256 :: zero ( )
846- } ;
847-
848- if peer_score > total_score {
834+ if self . header_downloaders . get ( & id) . map_or ( false , |d| !d. is_lagging ( ) ) {
849835 self . send_body_request ( & id) ;
850836 }
851837 }
0 commit comments