@@ -105,15 +105,16 @@ const CATCH_UP_SYNC_REQUEST_RESPONSE_PROTOCOL: &str = "/catch_up_sync/4";
105
105
const LOG_TARGET : & str = "tari::p2pool::server::p2p" ;
106
106
const SYNC_REQUEST_LOG_TARGET : & str = "sync_request" ;
107
107
const MESSAGE_LOGGING_LOG_TARGET : & str = "tari::p2pool::message_logging" ;
108
+ const PEER_INFO_LOGGING_LOG_TARGET : & str = "tari::p2pool::topics::peer_info" ;
109
+ const NEW_TIP_NOTIFY_LOGGING_LOG_TARGET : & str = "tari::p2pool::topics::new_tip_notify" ;
108
110
pub const STABLE_PRIVATE_KEY_FILE : & str = "p2pool_private.key" ;
109
111
110
112
const MAX_ACCEPTABLE_P2P_MESSAGE_TIMEOUT : Duration = Duration :: from_millis ( 500 ) ;
111
- const SYNC_TIMEOUT : Duration = Duration :: from_secs ( 60 ) ;
112
113
const MAX_ACCEPTABLE_NETWORK_EVENT_TIMEOUT : Duration = Duration :: from_millis ( 100 ) ;
113
114
const CATCH_UP_SYNC_BLOCKS_IN_I_HAVE : usize = 100 ;
114
115
const MAX_CATCH_UP_ATTEMPTS : usize = 150 ;
115
116
// Time to start up and catch up before we start processing new tip messages
116
- const STARTUP_CATCH_UP_TIME : Duration = Duration :: from_secs ( 120 ) ;
117
+ const STARTUP_CATCH_UP_TIME : Duration = Duration :: from_secs ( 1 ) ;
117
118
118
119
#[ derive( Clone , Debug , PartialEq , Eq , Hash , Serialize , Deserialize , Default ) ]
119
120
pub struct Squad {
@@ -415,14 +416,14 @@ where S: ShareChain
415
416
StreamProtocol :: new ( SHARE_CHAIN_SYNC_REQ_RESP_PROTOCOL ) ,
416
417
request_response:: ProtocolSupport :: Full ,
417
418
) ] ,
418
- request_response:: Config :: default ( ) . with_request_timeout ( Duration :: from_secs ( 30 ) ) , // 10 is the default
419
+ request_response:: Config :: default ( ) . with_request_timeout ( Duration :: from_secs ( 10 ) ) , // 10 is the default
419
420
) ,
420
421
direct_peer_exchange : cbor:: Behaviour :: < DirectPeerInfoRequest , DirectPeerInfoResponse > :: new (
421
422
[ (
422
423
StreamProtocol :: new ( DIRECT_PEER_EXCHANGE_REQ_RESP_PROTOCOL ) ,
423
424
request_response:: ProtocolSupport :: Full ,
424
425
) ] ,
425
- request_response:: Config :: default ( ) . with_request_timeout ( Duration :: from_secs ( 30 ) ) , // 10 is the default
426
+ request_response:: Config :: default ( ) . with_request_timeout ( Duration :: from_secs ( 10 ) ) , // 10 is the default
426
427
) ,
427
428
catch_up_sync : cbor:: Behaviour :: < CatchUpSyncRequest , CatchUpSyncResponse > :: new (
428
429
[ (
@@ -499,6 +500,7 @@ where S: ShareChain
499
500
let current_pow_sha3x = share_chain_sha3x. chain_pow ( ) . await . as_u128 ( ) ;
500
501
let current_pow_random_x = share_chain_random_x. chain_pow ( ) . await . as_u128 ( ) ;
501
502
let peer_info_squad_raw = PeerInfo :: new (
503
+ self . swarm . local_peer_id ( ) . clone ( ) ,
502
504
current_height_sha3x,
503
505
current_height_random_x,
504
506
current_pow_sha3x,
@@ -596,47 +598,74 @@ where S: ShareChain
596
598
#[ allow( clippy:: too_many_lines) ]
597
599
async fn handle_new_gossipsub_message ( & mut self , message : Message ) -> Result < MessageAcceptance , Error > {
598
600
debug ! ( target: MESSAGE_LOGGING_LOG_TARGET , "New gossipsub message: {message:?}" ) ;
599
- let peer = message. source ;
600
- if let Some ( peer ) = peer {
601
+ let source_peer = message. source ;
602
+ if let Some ( source_peer ) = source_peer {
601
603
let topic = message. topic . to_string ( ) ;
602
604
match topic {
603
- topic if topic == Self :: network_topic ( PEER_INFO_TOPIC ) => match messages:: PeerInfo :: try_from ( message) {
604
- Ok ( payload) => {
605
- debug ! ( target: MESSAGE_LOGGING_LOG_TARGET , "[PEERINFO_TOPIC] New peer info: {peer:?} -> {payload:?}" ) ;
606
- debug ! ( target: LOG_TARGET , squad = & self . config. squad; "[NETWORK] New peer info: {peer:?} -> {payload:?}" ) ;
607
- if payload. version != PROTOCOL_VERSION {
608
- trace ! ( target: LOG_TARGET , squad = & self . config. squad; "Peer {} has an outdated version, skipping" , peer) ;
609
- return Ok ( MessageAcceptance :: Reject ) ;
610
- }
611
- if payload. squad != self . config . squad . as_string ( ) {
612
- debug ! ( target: LOG_TARGET , squad = & self . config. squad; "Peer {} is not in the same squad, skipping. Our squad: {}, their squad:{}" , peer, self . config. squad, payload. squad) ;
613
- return Ok ( MessageAcceptance :: Ignore ) ;
614
- }
615
- if !self . config . is_seed_peer {
616
- if self . add_peer ( payload, peer. clone ( ) ) . await {
617
- self . swarm . behaviour_mut ( ) . gossipsub . add_explicit_peer ( & peer) ;
618
- }
619
- }
620
- return Ok ( MessageAcceptance :: Accept ) ;
621
- } ,
622
- Err ( error) => {
623
- debug ! ( target: LOG_TARGET , squad = & self . config. squad; "Can't deserialize peer info payload: {:?}" , error) ;
624
- return Ok ( MessageAcceptance :: Reject ) ;
625
- } ,
626
- } ,
605
+ // topic if topic == Self::network_topic(PEER_INFO_TOPIC) => match messages::PeerInfo::try_from(message)
606
+ // { Ok(payload) => {
607
+ // debug!(target: PEER_INFO_LOGGING_LOG_TARGET, "[PEERINFO_TOPIC] New peer info: {source_peer:?}
608
+ // -> {payload:?}"); debug!(target: LOG_TARGET, squad = &self.config.squad;
609
+ // "[NETWORK] New peer info: {source_peer:?} -> {payload:?}"); if
610
+ // payload.version != PROTOCOL_VERSION { trace!(target: LOG_TARGET, squad =
611
+ // &self.config.squad; "Peer {} has an outdated version, skipping", source_peer);
612
+ // return Ok(MessageAcceptance::Reject);
613
+ // }
614
+ // if payload.squad != self.config.squad.as_string() {
615
+ // debug!(target: LOG_TARGET, squad = &self.config.squad; "Peer {} is not in the same squad,
616
+ // skipping. Our squad: {}, their squad:{}", source_peer, self.config.squad, payload.squad);
617
+ // return Ok(MessageAcceptance::Ignore);
618
+ // }
619
+
620
+ // if payload.peer_id.as_ref() == Some(self.swarm.local_peer_id()) {
621
+ // return Ok(MessageAcceptance::Ignore);
622
+ // }
623
+
624
+ // if payload.peer_id != Some(source_peer) {
625
+ // warn!(target: LOG_TARGET, squad = &self.config.squad; "Peer {} sent a peer info message
626
+ // with a different peer id: {}, skipping", source_peer, payload.peer_id.as_ref().map(|p|
627
+ // p.to_string()).unwrap_or("None".to_string())); // return
628
+ // Ok(MessageAcceptance::Ignore); }
629
+ // if !self.config.is_seed_peer {
630
+ // if self.add_peer(payload, source_peer.clone()).await {
631
+ // self.swarm.behaviour_mut().gossipsub.add_explicit_peer(&source_peer);
632
+ // }
633
+ // }
634
+ // return Ok(MessageAcceptance::Accept);
635
+ // },
636
+ // Err(error) => {
637
+ // debug!(target: LOG_TARGET, squad = &self.config.squad; "Can't deserialize peer info payload:
638
+ // {:?}", error); return Ok(MessageAcceptance::Reject);
639
+ // },
640
+ // },
627
641
topic if topic == Self :: squad_topic ( & self . config . squad , PEER_INFO_TOPIC ) => {
628
642
match messages:: PeerInfo :: try_from ( message) {
629
643
Ok ( payload) => {
630
- debug ! ( target: MESSAGE_LOGGING_LOG_TARGET , "[SQUAD_PEERINFO_TOPIC] New peer info: {peer:?} -> {payload:?}" ) ;
631
-
632
- debug ! ( target: LOG_TARGET , squad = & self . config. squad; "[squad] New peer info: {peer:?} -> {payload:?}" ) ;
644
+ debug ! ( target: LOG_TARGET , squad = & self . config. squad; "[squad] New peer info: {source_peer:?} -> {payload:?}" ) ;
633
645
if payload. version != PROTOCOL_VERSION {
634
- debug ! ( target: LOG_TARGET , squad = & self . config. squad; "Peer {} has an outdated version, skipping" , peer ) ;
646
+ debug ! ( target: LOG_TARGET , squad = & self . config. squad; "Peer {} has an outdated version, skipping" , source_peer ) ;
635
647
return Ok ( MessageAcceptance :: Reject ) ;
636
648
}
649
+
650
+ // 60 seconds. TODO: make config
651
+ if payload. timestamp < EpochTime :: now ( ) . as_u64 ( ) . saturating_sub ( 60 ) {
652
+ debug ! ( target: LOG_TARGET , squad = & self . config. squad; "Peer {} sent a peer info message that is too old, skipping" , source_peer) ;
653
+ // TODO: should be punish
654
+ return Ok ( MessageAcceptance :: Ignore ) ;
655
+ }
656
+ if payload. peer_id . as_ref ( ) == Some ( self . swarm . local_peer_id ( ) ) {
657
+ return Ok ( MessageAcceptance :: Ignore ) ;
658
+ }
659
+
660
+ if payload. peer_id != Some ( source_peer) {
661
+ warn ! ( target: LOG_TARGET , squad = & self . config. squad; "Peer {} sent a peer info message with a different peer id: {}, skipping" , source_peer, payload. peer_id. as_ref( ) . map( |p| p. to_string( ) ) . unwrap_or( "None" . to_string( ) ) ) ;
662
+ // return Ok(MessageAcceptance::Ignore);
663
+ }
664
+ debug ! ( target: PEER_INFO_LOGGING_LOG_TARGET , "[SQUAD_PEERINFO_TOPIC] New peer info: {source_peer:?} -> {payload:?}" ) ;
665
+
637
666
if !self . config . is_seed_peer {
638
- if self . add_peer ( payload, peer . clone ( ) ) . await {
639
- self . swarm . behaviour_mut ( ) . gossipsub . add_explicit_peer ( & peer ) ;
667
+ if self . add_peer ( payload, source_peer . clone ( ) ) . await {
668
+ self . swarm . behaviour_mut ( ) . gossipsub . add_explicit_peer ( & source_peer ) ;
640
669
}
641
670
}
642
671
return Ok ( MessageAcceptance :: Accept ) ;
@@ -651,33 +680,35 @@ where S: ShareChain
651
680
// TODO: (sender peer's wallet address should be included always in the conibases with a fixed percent
652
681
// (like 20%))
653
682
topic if topic == Self :: squad_topic ( & self . config . squad , BLOCK_NOTIFY_TOPIC ) => {
654
- debug ! ( target: MESSAGE_LOGGING_LOG_TARGET , "[SQUAD_NEW_BLOCK_TOPIC] New block from gossip: {peer:?}" ) ;
655
-
656
683
// if self.sync_in_progress.load(Ordering::SeqCst) {
657
684
// return;
658
685
// }
659
686
match NotifyNewTipBlock :: try_from ( message) {
660
687
Ok ( payload) => {
661
688
if payload. version != PROTOCOL_VERSION {
662
- debug ! ( target: LOG_TARGET , squad = & self . config. squad; "Peer {} has an outdated version, skipping" , peer ) ;
689
+ debug ! ( target: LOG_TARGET , squad = & self . config. squad; "Peer {} has an outdated version, skipping" , source_peer ) ;
663
690
return Ok ( MessageAcceptance :: Reject ) ;
664
691
}
665
692
// lets check age
666
- if payload. timestamp < EpochTime :: now ( ) . as_u64 ( ) . saturating_sub ( 60 ) {
667
- debug ! ( target: LOG_TARGET , squad = & self . config. squad; "Peer {} sent a notify message that is too old, skipping" , peer ) ;
693
+ if payload. timestamp < EpochTime :: now ( ) . as_u64 ( ) . saturating_sub ( 10 ) {
694
+ debug ! ( target: LOG_TARGET , squad = & self . config. squad; "Peer {} sent a notify message that is too old, skipping" , source_peer ) ;
668
695
return Ok ( MessageAcceptance :: Ignore ) ;
669
696
}
670
697
let payload = Arc :: new ( payload) ;
671
- debug ! ( target: MESSAGE_LOGGING_LOG_TARGET , "[SQUAD_NEW_BLOCK_TOPIC] New block from gossip: {peer:?} -> {payload:?}" ) ;
672
- let source_peer = payload. peer_id ( ) ;
698
+ let message_peer = payload. peer_id ( ) ;
699
+ if message_peer. to_string ( ) != source_peer. to_string ( ) {
700
+ warn ! ( target: LOG_TARGET , squad = & self . config. squad; "Peer {} sent a block with a different peer id: {}, skipping" , source_peer, message_peer) ;
701
+ }
702
+ debug ! ( target: NEW_TIP_NOTIFY_LOGGING_LOG_TARGET , "[SQUAD_NEW_BLOCK_TOPIC] New block from gossip: {source_peer:?} -> {payload:?}" ) ;
703
+
673
704
// If we don't have this peer, try do peer exchange
674
- if !self . network_peer_store . exists ( source_peer ) {
675
- self . initiate_direct_peer_exchange ( source_peer ) . await ;
705
+ if !self . network_peer_store . exists ( message_peer ) {
706
+ self . initiate_direct_peer_exchange ( message_peer ) . await ;
676
707
}
677
708
678
709
// verify payload
679
710
if payload. new_blocks . is_empty ( ) {
680
- warn ! ( target: LOG_TARGET , squad = & self . config. squad; "Peer {} sent notify new tip with no blocks." , source_peer ) ;
711
+ warn ! ( target: LOG_TARGET , squad = & self . config. squad; "Peer {} sent notify new tip with no blocks." , message_peer ) ;
681
712
return Ok ( MessageAcceptance :: Reject ) ;
682
713
}
683
714
@@ -694,7 +725,14 @@ where S: ShareChain
694
725
payload. new_blocks . iter ( ) . map ( |( h, _) | * h) . max ( ) . unwrap_or ( 0 ) <=
695
726
our_tip. saturating_sub ( 4 )
696
727
{
697
- debug ! ( target: LOG_TARGET , squad = & self . config. squad; "Peer {} sent a block that is not better than ours, skipping" , source_peer) ;
728
+ debug ! ( target: LOG_TARGET , squad = & self . config. squad; "Peer {} sent a block that is not better than ours, skipping" , message_peer) ;
729
+ return Ok ( MessageAcceptance :: Ignore ) ;
730
+ }
731
+
732
+ // if payload.
733
+ if payload. new_blocks . iter ( ) . map ( |( h, _) | * h) . max ( ) . unwrap_or ( 0 ) > our_tip. saturating_add ( 2 )
734
+ {
735
+ debug ! ( target: LOG_TARGET , squad = & self . config. squad; "Peer {} sent a block that is too far ahead, skipping" , message_peer) ;
698
736
return Ok ( MessageAcceptance :: Ignore ) ;
699
737
}
700
738
@@ -706,7 +744,7 @@ where S: ShareChain
706
744
missing_blocks. push ( block. clone ( ) ) ;
707
745
}
708
746
if !missing_blocks. is_empty ( ) {
709
- self . sync_share_chain ( algo, source_peer , missing_blocks, true ) . await ;
747
+ self . sync_share_chain ( algo, message_peer , missing_blocks, true ) . await ;
710
748
}
711
749
return Ok ( MessageAcceptance :: Accept ) ;
712
750
} ,
@@ -715,7 +753,7 @@ where S: ShareChain
715
753
debug ! ( target: LOG_TARGET , squad = & self . config. squad; "Can't deserialize broadcast block payload: {:?}" , error) ;
716
754
self . network_peer_store
717
755
. move_to_grey_list (
718
- peer ,
756
+ source_peer . clone ( ) ,
719
757
format ! ( "Node sent a block that could not be deserialized: {:?}" , error) ,
720
758
)
721
759
. await ;
@@ -773,6 +811,10 @@ where S: ShareChain
773
811
} )
774
812
{
775
813
let local_peer_id = self . swarm . local_peer_id ( ) . clone ( ) ;
814
+ if peer == & local_peer_id {
815
+ return ;
816
+ }
817
+
776
818
// TODO: Should we send them our details? The problem is that if we send too many of these, libp2p
777
819
// starts dropping requests with "libp2p_relay::priv_client::handler Dropping in-flight connect
778
820
// request because we are at capacity"
@@ -1687,14 +1729,26 @@ where S: ShareChain
1687
1729
1688
1730
// dbg!(&missing_blocks);
1689
1731
1690
- let best_peers = self
1732
+ let mut best_peers = self
1691
1733
. network_peer_store
1692
1734
. best_peers_to_sync ( self . config . num_peers_to_sync , * algo) ;
1693
1735
let our_pow = match algo {
1694
1736
PowAlgorithm :: RandomX => self . share_chain_random_x . chain_pow ( ) . await ,
1695
1737
PowAlgorithm :: Sha3x => self . share_chain_sha3x . chain_pow ( ) . await ,
1696
1738
} ;
1697
1739
1740
+ let min_catch_up_difficulty = 1000 ;
1741
+ best_peers. retain ( |x| match algo {
1742
+ PowAlgorithm :: RandomX => {
1743
+ let their_pow = x. peer_info . current_random_x_pow ;
1744
+ their_pow > min_catch_up_difficulty
1745
+ } ,
1746
+ PowAlgorithm :: Sha3x => {
1747
+ let their_pow = x. peer_info . current_sha3x_pow ;
1748
+ their_pow > min_catch_up_difficulty
1749
+ } ,
1750
+ } ) ;
1751
+
1698
1752
// info!(target: LOG_TARGET, squad = &self.config.squad; "Best peers to sync: {best_peers:?}");
1699
1753
if best_peers. is_empty ( ) {
1700
1754
info ! ( target: LOG_TARGET , squad = & self . config. squad; "No peers found to try and sync to" ) ;
0 commit comments