@@ -35,13 +35,7 @@ use libp2p::{
35
35
ping,
36
36
relay,
37
37
request_response:: { self , cbor, OutboundFailure , OutboundRequestId , ResponseChannel } ,
38
- swarm:: {
39
- behaviour:: toggle:: Toggle ,
40
- dial_opts:: { DialOpts , PeerCondition } ,
41
- DialError ,
42
- NetworkBehaviour ,
43
- SwarmEvent ,
44
- } ,
38
+ swarm:: { behaviour:: toggle:: Toggle , dial_opts:: DialOpts , DialError , NetworkBehaviour , SwarmEvent } ,
45
39
Multiaddr ,
46
40
PeerId ,
47
41
Swarm ,
@@ -756,9 +750,9 @@ where S: ShareChain
756
750
757
751
match add_status {
758
752
AddPeerStatus :: NewPeer => {
759
- self . initiate_direct_peer_exchange ( & peer) . await ;
760
- self . swarm . behaviour_mut ( ) . gossipsub . add_explicit_peer ( & peer) ;
761
- let _unused = self . swarm . dial ( peer) ;
753
+ // self.initiate_direct_peer_exchange(&peer).await;
754
+ // self.swarm.behaviour_mut().gossipsub.add_explicit_peer(&peer);
755
+ // let _unused = self.swarm.dial(peer);
762
756
return true ;
763
757
} ,
764
758
AddPeerStatus :: Existing => { } ,
@@ -781,6 +775,7 @@ where S: ShareChain
781
775
error ! ( target: LOG_TARGET , squad = & self . config. squad; "Failed to create peer info: {error:?}" ) ;
782
776
} )
783
777
{
778
+ dbg ! ( "here" ) ;
784
779
let local_peer_id = * self . swarm . local_peer_id ( ) ;
785
780
if peer == & local_peer_id {
786
781
return ;
@@ -1187,7 +1182,7 @@ where S: ShareChain
1187
1182
info ! ( target: LOG_TARGET , squad = & self . config. squad; "Connection established: {peer_id:?} -> {endpoint:?} ({num_established:?}/{concurrent_dial_errors:?}/{established_in:?})" ) ;
1188
1183
} ,
1189
1184
SwarmEvent :: Dialing { peer_id, .. } => {
1190
- info ! ( target: LOG_TARGET , squad = & self . config . squad ; "Dialing: {peer_id:?}" ) ;
1185
+ info ! ( target: LOG_TARGET , "Dialing: {peer_id:?}" ) ;
1191
1186
} ,
1192
1187
SwarmEvent :: NewListenAddr { address, .. } => {
1193
1188
info ! ( target: LOG_TARGET , squad = & self . config. squad; "Listening on {address:?}" ) ;
@@ -1203,14 +1198,15 @@ where S: ShareChain
1203
1198
if !endpoint. is_dialer ( ) {
1204
1199
warn ! ( target: LOG_TARGET , squad = & self . config. squad; "Connection closed: {peer_id:?} -> {endpoint:?} ({num_established:?}) -> {cause:?}" ) ;
1205
1200
}
1201
+ warn ! ( target: LOG_TARGET , squad = & self . config. squad; "Connection closed: {peer_id:?} -> {endpoint:?} ({num_established:?}) -> {cause:?}" ) ;
1206
1202
} ,
1207
1203
SwarmEvent :: IncomingConnectionError {
1208
1204
connection_id,
1209
1205
local_addr,
1210
1206
send_back_addr,
1211
1207
error,
1212
1208
} => {
1213
- info ! ( target: LOG_TARGET , squad = & self . config . squad ; "Incoming connection error: {connection_id:?} -> {local_addr:?} -> {send_back_addr:?} -> {error:?}" ) ;
1209
+ info ! ( target: LOG_TARGET , "Incoming connection error: {connection_id:?} -> {local_addr:?} -> {send_back_addr:?} -> {error:?}" ) ;
1214
1210
} ,
1215
1211
SwarmEvent :: ListenerError { listener_id, error } => {
1216
1212
error ! ( target: LOG_TARGET , squad = & self . config. squad; "Listener error: {listener_id:?} -> {error:?}" ) ;
@@ -1227,7 +1223,7 @@ where S: ShareChain
1227
1223
match error {
1228
1224
DialError :: Transport ( transport_error) => {
1229
1225
// There are a lot of cancelled errors, so ignore them
1230
- debug ! ( target: LOG_TARGET , "Outgoing connection error, ignoring: {peer_id:?} -> {transport_error:?}" ) ;
1226
+ warn ! ( target: LOG_TARGET , "Outgoing connection error, ignoring: {peer_id:?} -> {transport_error:?}" ) ;
1231
1227
} ,
1232
1228
_ => {
1233
1229
warn ! ( target: LOG_TARGET , squad = & self . config. squad; "Outgoing connection error: {peer_id:?} -> {error:?}" ) ;
@@ -1966,15 +1962,15 @@ where S: ShareChain
1966
1962
addresses. truncate ( 8 ) ;
1967
1963
1968
1964
// Try dial, this should already be happening though
1969
- if let Err ( err) = self . swarm . dial (
1970
- DialOpts :: peer_id ( relay. peer_id )
1971
- . addresses ( relay. addresses . clone ( ) )
1972
- // .condition(PeerCondition::NotDialing)
1973
- . build ( ) ,
1974
- ) {
1975
- debug ! ( target: LOG_TARGET , "🚨 Failed to dial relay: {}" , err) ;
1976
- // return;
1977
- }
1965
+ // if let Err(err) = self.swarm.dial(
1966
+ // DialOpts::peer_id(relay.peer_id)
1967
+ // .addresses(relay.addresses.clone())
1968
+ // // .condition(PeerCondition::NotDialing)
1969
+ // .build(),
1970
+ // ) {
1971
+ // debug!(target: LOG_TARGET, "🚨 Failed to dial relay: {}", err);
1972
+ // // return;
1973
+ // }
1978
1974
1979
1975
addresses. iter ( ) . for_each ( |addr| {
1980
1976
let listen_addr = addr. clone ( ) . with ( Protocol :: P2pCircuit ) ;
@@ -2172,7 +2168,7 @@ where S: ShareChain
2172
2168
let mut connection_stats_publish = tokio:: time:: interval ( Duration :: from_secs ( 10 ) ) ;
2173
2169
connection_stats_publish. set_missed_tick_behavior ( MissedTickBehavior :: Skip ) ;
2174
2170
2175
- let mut seek_connections_interval = tokio:: time:: interval ( Duration :: from_secs ( 5 ) ) ;
2171
+ let mut seek_connections_interval = tokio:: time:: interval ( Duration :: from_secs ( 20 ) ) ;
2176
2172
seek_connections_interval. set_missed_tick_behavior ( MissedTickBehavior :: Skip ) ;
2177
2173
2178
2174
let mut debug_chain_graph = if self . config . debug_print_chain {
@@ -2191,6 +2187,7 @@ where S: ShareChain
2191
2187
tokio:: pin!( connection_stats_publish) ;
2192
2188
tokio:: pin!( seek_connections_interval) ;
2193
2189
2190
+ let uptime = Instant :: now ( ) ;
2194
2191
loop {
2195
2192
// info!(target: LOG_TARGET, "P2P service main loop iter");
2196
2193
select ! {
@@ -2220,11 +2217,13 @@ where S: ShareChain
2220
2217
let info = self . swarm. network_info( ) ;
2221
2218
let counters = info. connection_counters( ) ;
2222
2219
2223
- let num_connections = counters. num_established_incoming( ) + counters. num_established_outgoing( ) ;
2224
- if num_connections > 20 {
2220
+ // let num_connections = counters.num_established_incoming() + counters.num_established_outgoing();
2221
+ let num_connections = counters. num_established_outgoing( ) ;
2222
+ if num_connections > 8 {
2225
2223
continue ;
2226
2224
}
2227
- if num_connections == 0 {
2225
+ if num_connections == 0 && uptime. elapsed( ) < Duration :: from_secs( 60 ) {
2226
+
2228
2227
match self . dial_seed_peers( ) . await {
2229
2228
Ok ( _) => { } ,
2230
2229
Err ( e) => {
@@ -2236,26 +2235,44 @@ where S: ShareChain
2236
2235
2237
2236
let mut num_dialed = 0 ;
2238
2237
let mut store_write_lock = self . network_peer_store. write( ) . await ;
2239
- // Rather try and search good peers rather than randomly dialing
2240
- // 1000 peers will take a long time to get through
2238
+
2239
+ let mut peers_to_dial = vec! [ ] ;
2241
2240
for record in store_write_lock. best_peers_to_dial( 100 ) {
2242
- debug!( target: LOG_TARGET , "Dialing peer: {:?} with height(rx/sha) {}/{}" , record. peer_id, record. peer_info. current_random_x_height, record. peer_info. current_sha3x_height) ;
2243
2241
// dbg!(&record.peer_id);
2244
2242
// Only dial seed peers if we have 0 connections
2245
2243
if !self . swarm. is_connected( & record. peer_id)
2246
2244
&& !store_write_lock. is_seed_peer( & record. peer_id) {
2245
+ // if &record.peer_id.to_string() != "12D3KooWD6GY3c8cz6AwKaDaqmqGCbmewhjKT5ULN9JUB5oUgWjS" {
2246
+ // store_write_lock.update_last_dial_attempt(&record.peer_id);
2247
+ // info!(target: LOG_TARGET, "Skipping dialing peer: {:?} with height(rx/sha) {}/{} on {}", record.peer_id, record.peer_info.current_random_x_height, record.peer_info.current_sha3x_height, record.peer_info.public_addresses().iter().map(|a| a.to_string()).collect::<Vec<String>>().join(", "));
2248
+ // continue;
2249
+ // }
2247
2250
store_write_lock. update_last_dial_attempt( & record. peer_id) ;
2248
- let dial_opts= DialOpts :: peer_id( record. peer_id) . condition( PeerCondition :: Always ) . addresses( record. peer_info. public_addresses( ) . clone( ) ) . extend_addresses_through_behaviour( ) . build( ) ;
2249
- let _unused = self . swarm. dial( dial_opts) ;
2251
+ info!( target: LOG_TARGET , "Dialing peer: {:?} with height(rx/sha) {}/{} on {}" , record. peer_id, record. peer_info. current_random_x_height, record. peer_info. current_sha3x_height, record. peer_info. public_addresses( ) . iter( ) . map( |a| a. to_string( ) ) . collect:: <Vec <String >>( ) . join( ", " ) ) ;
2252
+ let dial_opts= DialOpts :: peer_id( record. peer_id) . addresses( record. peer_info. public_addresses( ) . clone( ) ) . extend_addresses_through_behaviour( ) . build( ) ;
2253
+ // let dial_opts= DialOpts::peer_id(record.peer_id).addresses(vec!["/ip4/152.228.210.16/tcp/19001/p2p/12D3KooWD6GY3c8cz6AwKaDaqmqGCbmewhjKT5ULN9JUB5oUgWjS".parse().unwrap(), "/ip4/152.228.210.16/udp/19001/quic-v1/p2p/12D3KooWD6GY3c8cz6AwKaDaqmqGCbmewhjKT5ULN9JUB5oUgWjS".parse().unwrap()]).build();
2254
+ // let dial_opts = DialOpts::unknown_peer_id().address("/ip4/152.228.210.16/tcp/19001/p2p/12D3KooWD6GY3c8cz6AwKaDaqmqGCbmewhjKT5ULN9JUB5oUgWjS".parse().unwrap()).build();
2255
+ let _unused = self . swarm. dial( dial_opts) . map_err( |e| {
2256
+ warn!( target: LOG_TARGET , "Failed to dial peer: {e:?}" ) ;
2257
+ } ) ;
2258
+ // self.initiate_direct_peer_exchange(&record.peer_id).await;
2259
+ peers_to_dial. push( record. peer_id) ;
2250
2260
num_dialed += 1 ;
2251
2261
// We can only do 30 connections
2252
2262
// after 30 it starts cancelling dials
2253
- if num_dialed > 80 {
2263
+ if num_dialed > 10 {
2254
2264
break ;
2255
2265
}
2256
2266
}
2267
+
2268
+
2257
2269
}
2258
- }
2270
+ drop( store_write_lock) ;
2271
+ // for peer in peers_to_dial {
2272
+ // dbg!("trying");
2273
+ // self.initiate_direct_peer_exchange(&peer).await;
2274
+ // }
2275
+ }
2259
2276
if timer. elapsed( ) > MAX_ACCEPTABLE_NETWORK_EVENT_TIMEOUT {
2260
2277
warn!( target: LOG_TARGET , "Seeking connections took too long: {:?}" , timer. elapsed( ) ) ;
2261
2278
}
@@ -2514,12 +2531,9 @@ where S: ShareChain
2514
2531
// self.swarm.behaviour_mut().kademlia.add_address(peer_id, addr.clone());
2515
2532
self . swarm . add_peer_address ( * peer_id, addr. clone ( ) ) ;
2516
2533
peers_to_add. push ( * peer_id) ;
2517
- let _unused = self
2518
- . swarm
2519
- . dial ( DialOpts :: peer_id ( * peer_id) . condition ( PeerCondition :: Always ) . build ( ) )
2520
- . inspect_err ( |e| {
2521
- warn ! ( target: LOG_TARGET , squad = & self . config. squad; "Failed to dial seed peer: {e:?}" ) ;
2522
- } ) ;
2534
+ let _unused = self . swarm . dial ( DialOpts :: peer_id ( * peer_id) . build ( ) ) . inspect_err ( |e| {
2535
+ warn ! ( target: LOG_TARGET , squad = & self . config. squad; "Failed to dial seed peer: {e:?}" ) ;
2536
+ } ) ;
2523
2537
} ) ;
2524
2538
self . network_peer_store . write ( ) . await . add_seed_peers ( peers_to_add) ;
2525
2539
0 commit comments