@@ -605,7 +605,17 @@ impl<N: NetworkPrimitives> NetworkManager<N> {
605605 }
606606 PeerMessage :: NewBlock ( block) => {
607607 self . within_pow_or_disconnect ( peer_id, move |this| {
608- this. swarm . state_mut ( ) . on_new_block ( peer_id, block. hash ) ;
608+ // Extract TD from the NewBlock message
609+ let td = block. td ( ) ;
610+
611+ // Update the session's current TD (important for BSC and other chains)
612+ if let Some ( session) = this. swarm . sessions ( ) . active_sessions ( ) . get ( & peer_id) {
613+ session. update_td ( td) ;
614+ }
615+
616+ // Update NetworkState's ActivePeer TD
617+ this. swarm . state_mut ( ) . on_new_block_with_td ( peer_id, block. hash , td) ;
618+
609619 // start block import process
610620 this. block_import . on_new_block ( peer_id, NewBlockEvent :: Block ( block) ) ;
611621 } ) ;
@@ -641,13 +651,15 @@ impl<N: NetworkPrimitives> NetworkManager<N> {
641651 NetworkHandleMessage :: DiscoveryListener ( tx) => {
642652 self . swarm . state_mut ( ) . discovery_mut ( ) . add_listener ( tx) ;
643653 }
644- NetworkHandleMessage :: AnnounceBlock ( block, hash) => {
654+ NetworkHandleMessage :: AnnounceBlock ( block, hash, total_difficulty ) => {
645655 if self . handle . mode ( ) . is_stake ( ) {
646656 // See [EIP-3675](https://eips.ethereum.org/EIPS/eip-3675#devp2p)
647657 warn ! ( target: "net" , "Peer performed block propagation, but it is not supported in proof of stake (EIP-3675)" ) ;
648- return
658+ return ;
649659 }
650- let msg = NewBlockMessage { hash, block : Arc :: new ( block) } ;
660+ // Include the total_difficulty when announcing our own block
661+ // This is essential for BSC and other chains that rely on TD
662+ let msg = NewBlockMessage { hash, block : Arc :: new ( block) , td : total_difficulty } ;
651663 self . swarm . state_mut ( ) . announce_new_block ( msg) ;
652664 }
653665 NetworkHandleMessage :: EthRequest { peer_id, request } => {
@@ -989,11 +1001,20 @@ impl<N: NetworkPrimitives> NetworkManager<N> {
9891001 . active_sessions ( )
9901002 . iter ( )
9911003 . filter_map ( |( & peer_id, session) | {
992- self . swarm
993- . state ( )
994- . peers ( )
995- . peer_by_id ( peer_id)
996- . map ( |( record, kind) | session. peer_info ( & record, kind) )
1004+ let ( record, kind) = self . swarm . state ( ) . peers ( ) . peer_by_id ( peer_id) ?;
1005+
1006+ // Get real-time block info from NetworkState
1007+ let ( best_hash, best_number, best_td) =
1008+ self . swarm . state ( ) . get_peer_best_block ( & peer_id) . unwrap_or_else ( || {
1009+ // Fallback to status from handshake if no real-time data
1010+ (
1011+ session. status . blockhash ,
1012+ session. status . latest_block ,
1013+ session. status . total_difficulty ,
1014+ )
1015+ } ) ;
1016+
1017+ Some ( session. peer_info ( & record, kind, best_hash, best_number, best_td) )
9971018 } )
9981019 . collect ( )
9991020 }
@@ -1002,13 +1023,21 @@ impl<N: NetworkPrimitives> NetworkManager<N> {
10021023 ///
10031024 /// Returns `None` if there's no active session to the peer.
10041025 fn get_peer_info_by_id ( & self , peer_id : PeerId ) -> Option < PeerInfo > {
1005- self . swarm . sessions ( ) . active_sessions ( ) . get ( & peer_id) . and_then ( |session| {
1006- self . swarm
1007- . state ( )
1008- . peers ( )
1009- . peer_by_id ( peer_id)
1010- . map ( |( record, kind) | session. peer_info ( & record, kind) )
1011- } )
1026+ let session = self . swarm . sessions ( ) . active_sessions ( ) . get ( & peer_id) ?;
1027+ let ( record, kind) = self . swarm . state ( ) . peers ( ) . peer_by_id ( peer_id) ?;
1028+
1029+ // Get real-time block info from NetworkState
1030+ let ( best_hash, best_number, best_td) =
1031+ self . swarm . state ( ) . get_peer_best_block ( & peer_id) . unwrap_or_else ( || {
1032+ // Fallback to status from handshake if no real-time data
1033+ (
1034+ session. status . blockhash ,
1035+ session. status . latest_block ,
1036+ session. status . total_difficulty ,
1037+ )
1038+ } ) ;
1039+
1040+ Some ( session. peer_info ( & record, kind, best_hash, best_number, best_td) )
10121041 }
10131042
10141043 /// Returns [`PeerInfo`] for a given peers.
@@ -1142,7 +1171,7 @@ impl<N: NetworkPrimitives> Future for NetworkManager<N> {
11421171 if maybe_more_handle_messages || maybe_more_swarm_events {
11431172 // make sure we're woken up again
11441173 cx. waker ( ) . wake_by_ref ( ) ;
1145- return Poll :: Pending
1174+ return Poll :: Pending ;
11461175 }
11471176
11481177 this. update_poll_metrics ( start, poll_durations) ;
0 commit comments