@@ -21,8 +21,8 @@ use std::time::Duration;
2121
2222use ccore:: encoded:: Header as EncodedHeader ;
2323use ccore:: {
24- Block , BlockChainClient , BlockChainTrait , BlockId , BlockImportError , ChainNotify , Client , ImportBlock , ImportError ,
25- UnverifiedTransaction ,
24+ Block , BlockChainClient , BlockChainTrait , BlockId , BlockImportError , BlockStatus , ChainNotify , Client , ImportBlock ,
25+ ImportError , UnverifiedTransaction ,
2626} ;
2727use cmerkle:: TrieFactory ;
2828use cnetwork:: { Api , EventSender , NetworkExtension , NodeId } ;
@@ -74,6 +74,7 @@ pub struct Extension {
7474 client : Arc < Client > ,
7575 api : Box < dyn Api > ,
7676 last_request : u64 ,
77+ nonce : u64 ,
7778}
7879
7980impl Extension {
@@ -125,6 +126,7 @@ impl Extension {
125126 client,
126127 api,
127128 last_request : Default :: default ( ) ,
129+ nonce : Default :: default ( ) ,
128130 }
129131 }
130132
@@ -140,13 +142,14 @@ impl Extension {
140142 id,
141143 Arc :: new (
142144 Message :: Status {
143- total_score : chain_info . best_proposal_score ,
145+ nonce : U256 :: from ( self . nonce ) ,
144146 best_hash : chain_info. best_proposal_block_hash ,
145147 genesis_hash : chain_info. genesis_hash ,
146148 }
147149 . rlp_bytes ( ) ,
148150 ) ,
149151 ) ;
152+ self . nonce += 1 ;
150153 }
151154
152155 fn send_status_broadcast ( & mut self ) {
@@ -156,13 +159,14 @@ impl Extension {
156159 id,
157160 Arc :: new (
158161 Message :: Status {
159- total_score : chain_info . best_proposal_score ,
162+ nonce : U256 :: from ( self . nonce ) ,
160163 best_hash : chain_info. best_proposal_block_hash ,
161164 genesis_hash : chain_info. genesis_hash ,
162165 }
163166 . rlp_bytes ( ) ,
164167 ) ,
165168 ) ;
169+ self . nonce += 1 ;
166170 }
167171 }
168172
@@ -177,6 +181,14 @@ impl Extension {
177181 }
178182
179183 fn send_body_request ( & mut self , id : & NodeId ) {
184+ if let Some ( downloader) = self . header_downloaders . get ( & id) {
185+ if self . client . block_status ( & BlockId :: Hash ( downloader. best_hash ( ) ) ) == BlockStatus :: InChain {
186+ // Peer is lagging behind the local blockchain.
187+ // We don't need to request block bodies to this peer
188+ return
189+ }
190+ }
191+
180192 self . check_sync_variable ( ) ;
181193 if let Some ( requests) = self . requests . get_mut ( id) {
182194 let have_body_request = {
@@ -319,10 +331,10 @@ impl NetworkExtension<Event> for Extension {
319331 if let Ok ( received_message) = Rlp :: new ( data) . as_val ( ) {
320332 match received_message {
321333 Message :: Status {
322- total_score ,
334+ nonce ,
323335 best_hash,
324336 genesis_hash,
325- } => self . on_peer_status ( id, total_score , best_hash, genesis_hash) ,
337+ } => self . on_peer_status ( id, nonce , best_hash, genesis_hash) ,
326338 Message :: Request ( request_id, request) => self . on_peer_request ( id, request_id, request) ,
327339 Message :: Response ( request_id, response) => self . on_peer_response ( id, request_id, response) ,
328340 }
@@ -348,7 +360,6 @@ impl NetworkExtension<Event> for Extension {
348360 }
349361 State :: SnapshotChunk ( ..) => unimplemented ! ( ) ,
350362 State :: Full => {
351- let best_proposal_score = self . client . chain_info ( ) . best_proposal_score ;
352363 for id in & peer_ids {
353364 let request =
354365 self . header_downloaders . get_mut ( id) . and_then ( HeaderDownloader :: create_request) ;
@@ -359,15 +370,7 @@ impl NetworkExtension<Event> for Extension {
359370 }
360371
361372 for id in peer_ids {
362- let peer_score = if let Some ( peer) = self . header_downloaders . get ( & id) {
363- peer. total_score ( )
364- } else {
365- U256 :: zero ( )
366- } ;
367-
368- if peer_score > best_proposal_score {
369- self . send_body_request ( & id) ;
370- }
373+ self . send_body_request ( & id) ;
371374 }
372375 }
373376 }
@@ -516,7 +519,7 @@ impl Extension {
516519}
517520
518521impl Extension {
519- fn on_peer_status ( & mut self , from : & NodeId , total_score : U256 , best_hash : BlockHash , genesis_hash : BlockHash ) {
522+ fn on_peer_status ( & mut self , from : & NodeId , nonce : U256 , best_hash : BlockHash , genesis_hash : BlockHash ) {
520523 // Validity check
521524 if genesis_hash != self . client . chain_info ( ) . genesis_hash {
522525 cinfo ! ( SYNC , "Genesis hash mismatch with peer {}" , from) ;
@@ -525,17 +528,17 @@ impl Extension {
525528
526529 match self . header_downloaders . entry ( * from) {
527530 Entry :: Occupied ( mut peer) => {
528- if !peer. get_mut ( ) . update ( total_score , best_hash) {
531+ if !peer. get_mut ( ) . update ( nonce , best_hash) {
529532 // FIXME: It should be an error level if the consensus is PoW.
530533 cdebug ! ( SYNC , "Peer #{} status updated but score is less than before" , from) ;
531534 return
532535 }
533536 }
534537 Entry :: Vacant ( e) => {
535- e. insert ( HeaderDownloader :: new ( self . client . clone ( ) , total_score , best_hash) ) ;
538+ e. insert ( HeaderDownloader :: new ( self . client . clone ( ) , nonce , best_hash) ) ;
536539 }
537540 }
538- cinfo ! ( SYNC , "Peer #{} status update: total_score : {}, best_hash: {}" , from, total_score , best_hash) ;
541+ cinfo ! ( SYNC , "Peer #{} status update: nonce : {}, best_hash: {}" , from, nonce , best_hash) ;
539542 }
540543
541544 fn on_peer_request ( & self , from : & NodeId , id : u64 , request : RequestMessage ) {
@@ -750,14 +753,12 @@ impl Extension {
750753 } ,
751754 State :: SnapshotChunk ( ..) => { }
752755 State :: Full => {
753- let ( mut completed, pivot_score_changed) = if let Some ( peer) = self . header_downloaders . get_mut ( from) {
754- let before_pivot_score = peer. pivot_score ( ) ;
756+ let ( mut completed, peer_is_caught_up) = if let Some ( peer) = self . header_downloaders . get_mut ( from) {
755757 let encoded: Vec < _ > = headers. iter ( ) . map ( |h| EncodedHeader :: new ( h. rlp_bytes ( ) . to_vec ( ) ) ) . collect ( ) ;
756758 peer. import_headers ( & encoded) ;
757- let after_pivot_score = peer. pivot_score ( ) ;
758- ( peer. downloaded ( ) , before_pivot_score != after_pivot_score)
759+ ( peer. downloaded ( ) , peer. is_caught_up ( ) )
759760 } else {
760- ( Vec :: new ( ) , false )
761+ ( Vec :: new ( ) , true )
761762 } ;
762763 completed. sort_unstable_by_key ( EncodedHeader :: number) ;
763764
@@ -783,7 +784,7 @@ impl Extension {
783784 peer. mark_as_imported ( exists) ;
784785 peer. create_request ( )
785786 } ) ;
786- if pivot_score_changed {
787+ if !peer_is_caught_up {
787788 if let Some ( request) = request {
788789 self . send_header_request ( from, request) ;
789790 }
@@ -825,20 +826,11 @@ impl Extension {
825826 }
826827 }
827828
828- let total_score = self . client . chain_info ( ) . best_proposal_score ;
829829 let mut peer_ids: Vec < _ > = self . header_downloaders . keys ( ) . cloned ( ) . collect ( ) ;
830830 peer_ids. shuffle ( & mut thread_rng ( ) ) ;
831831
832832 for id in peer_ids {
833- let peer_score = if let Some ( peer) = self . header_downloaders . get ( & id) {
834- peer. total_score ( )
835- } else {
836- U256 :: zero ( )
837- } ;
838-
839- if peer_score > total_score {
840- self . send_body_request ( & id) ;
841- }
833+ self . send_body_request ( & id) ;
842834 }
843835 }
844836}
0 commit comments