Skip to content

Commit c875aa0

Browse files
committed
fix: dial less often
1 parent 7ddb54d commit c875aa0

File tree

5 files changed

+68
-52
lines changed

5 files changed

+68
-52
lines changed

src/server/p2p/network.rs

Lines changed: 53 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,7 @@ use libp2p::{
3535
ping,
3636
relay,
3737
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},
4539
Multiaddr,
4640
PeerId,
4741
Swarm,
@@ -756,9 +750,9 @@ where S: ShareChain
756750

757751
match add_status {
758752
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);
762756
return true;
763757
},
764758
AddPeerStatus::Existing => {},
@@ -781,6 +775,7 @@ where S: ShareChain
781775
error!(target: LOG_TARGET, squad = &self.config.squad; "Failed to create peer info: {error:?}");
782776
})
783777
{
778+
dbg!("here");
784779
let local_peer_id = *self.swarm.local_peer_id();
785780
if peer == &local_peer_id {
786781
return;
@@ -1187,7 +1182,7 @@ where S: ShareChain
11871182
info!(target: LOG_TARGET, squad = &self.config.squad; "Connection established: {peer_id:?} -> {endpoint:?} ({num_established:?}/{concurrent_dial_errors:?}/{established_in:?})");
11881183
},
11891184
SwarmEvent::Dialing { peer_id, .. } => {
1190-
info!(target: LOG_TARGET, squad = &self.config.squad; "Dialing: {peer_id:?}");
1185+
info!(target: LOG_TARGET, "Dialing: {peer_id:?}");
11911186
},
11921187
SwarmEvent::NewListenAddr { address, .. } => {
11931188
info!(target: LOG_TARGET, squad = &self.config.squad; "Listening on {address:?}");
@@ -1203,14 +1198,15 @@ where S: ShareChain
12031198
if !endpoint.is_dialer() {
12041199
warn!(target: LOG_TARGET, squad = &self.config.squad; "Connection closed: {peer_id:?} -> {endpoint:?} ({num_established:?}) -> {cause:?}");
12051200
}
1201+
warn!(target: LOG_TARGET, squad = &self.config.squad; "Connection closed: {peer_id:?} -> {endpoint:?} ({num_established:?}) -> {cause:?}");
12061202
},
12071203
SwarmEvent::IncomingConnectionError {
12081204
connection_id,
12091205
local_addr,
12101206
send_back_addr,
12111207
error,
12121208
} => {
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:?}");
12141210
},
12151211
SwarmEvent::ListenerError { listener_id, error } => {
12161212
error!(target: LOG_TARGET, squad = &self.config.squad; "Listener error: {listener_id:?} -> {error:?}");
@@ -1227,7 +1223,7 @@ where S: ShareChain
12271223
match error {
12281224
DialError::Transport(transport_error) => {
12291225
// 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:?}");
12311227
},
12321228
_ => {
12331229
warn!(target: LOG_TARGET, squad = &self.config.squad; "Outgoing connection error: {peer_id:?} -> {error:?}");
@@ -1966,15 +1962,15 @@ where S: ShareChain
19661962
addresses.truncate(8);
19671963

19681964
// 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+
// }
19781974

19791975
addresses.iter().for_each(|addr| {
19801976
let listen_addr = addr.clone().with(Protocol::P2pCircuit);
@@ -2172,7 +2168,7 @@ where S: ShareChain
21722168
let mut connection_stats_publish = tokio::time::interval(Duration::from_secs(10));
21732169
connection_stats_publish.set_missed_tick_behavior(MissedTickBehavior::Skip);
21742170

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));
21762172
seek_connections_interval.set_missed_tick_behavior(MissedTickBehavior::Skip);
21772173

21782174
let mut debug_chain_graph = if self.config.debug_print_chain {
@@ -2191,6 +2187,7 @@ where S: ShareChain
21912187
tokio::pin!(connection_stats_publish);
21922188
tokio::pin!(seek_connections_interval);
21932189

2190+
let uptime = Instant::now();
21942191
loop {
21952192
// info!(target: LOG_TARGET, "P2P service main loop iter");
21962193
select! {
@@ -2220,11 +2217,13 @@ where S: ShareChain
22202217
let info = self.swarm.network_info();
22212218
let counters = info.connection_counters();
22222219

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 {
22252223
continue;
22262224
}
2227-
if num_connections == 0 {
2225+
if num_connections == 0 && uptime.elapsed() < Duration::from_secs(60) {
2226+
22282227
match self.dial_seed_peers().await {
22292228
Ok(_) => {},
22302229
Err(e) => {
@@ -2236,26 +2235,44 @@ where S: ShareChain
22362235

22372236
let mut num_dialed = 0;
22382237
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![];
22412240
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);
22432241
// dbg!(&record.peer_id);
22442242
// Only dial seed peers if we have 0 connections
22452243
if !self.swarm.is_connected(&record.peer_id)
22462244
&& !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+
// }
22472250
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);
22502260
num_dialed += 1;
22512261
// We can only do 30 connections
22522262
// after 30 it starts cancelling dials
2253-
if num_dialed > 80 {
2263+
if num_dialed > 10 {
22542264
break;
22552265
}
22562266
}
2267+
2268+
22572269
}
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+
}
22592276
if timer.elapsed() > MAX_ACCEPTABLE_NETWORK_EVENT_TIMEOUT {
22602277
warn!(target: LOG_TARGET, "Seeking connections took too long: {:?}", timer.elapsed());
22612278
}
@@ -2514,12 +2531,9 @@ where S: ShareChain
25142531
// self.swarm.behaviour_mut().kademlia.add_address(peer_id, addr.clone());
25152532
self.swarm.add_peer_address(*peer_id, addr.clone());
25162533
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+
});
25232537
});
25242538
self.network_peer_store.write().await.add_seed_peers(peers_to_add);
25252539

src/server/p2p/peer_store.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ impl PeerStore {
229229
let mut peers = self.whitelist_peers.values().collect::<Vec<_>>();
230230
peers.retain(|peer| {
231231
!peer.peer_info.public_addresses().is_empty() &&
232-
(peer.last_dial_attempt.is_none() || peer.last_dial_attempt.unwrap().elapsed().as_secs() > 60)
232+
(peer.last_dial_attempt.is_none() || peer.last_dial_attempt.unwrap().elapsed().as_secs() > 120)
233233
});
234234
peers.sort_by(|a, b| {
235235
b.num_grey_listings

src/server/p2p/relay_store.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ impl RelayStore {
2222
}
2323

2424
pub fn select_random_relay(&mut self) {
25-
let Some((peer, addrs)) = self.possible_relays.iter().choose(&mut rand::thread_rng()) else {
25+
let Some((_peer, addrs)) = self.possible_relays.iter().choose(&mut rand::thread_rng()) else {
2626
return;
2727
};
2828
self.selected_relay = Some(RelayPeer {
29-
peer_id: *peer,
29+
// peer_id: *peer,
3030
addresses: addrs.iter().cloned().collect(),
3131
is_circuit_established: false,
3232
});
@@ -35,7 +35,7 @@ impl RelayStore {
3535

3636
#[derive(Debug, Clone)]
3737
pub struct RelayPeer {
38-
pub peer_id: PeerId,
38+
// pub peer_id: PeerId,
3939
pub addresses: Vec<Multiaddr>,
4040
pub is_circuit_established: bool,
4141
}

src/server/p2p/setup.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ pub(crate) async fn new_swarm(config: &config::Config) -> Result<Swarm<ServerNet
8181
let swarm = libp2p::SwarmBuilder::with_existing_identity(keypair(&config.p2p_service).await?)
8282
.with_tokio()
8383
.with_tcp(tcp::Config::default().nodelay(true), // Nodelay helps with hole punching
84-
noise::Config::new, yamux::Config::default)
85-
?
84+
noise::Config::new, yamux::Config::default)?
8685
.with_quic_config(|mut config| {
8786
config.handshake_timeout = Duration::from_secs(30);
8887
config

src/sharechain/p2chain.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,9 @@ impl<T: BlockCache> P2Chain<T> {
165165
let mut new_chain = Self::new_empty(algo, total_size, share_window, block_time, new_block_cache);
166166
for block in from_block_cache.all_blocks()? {
167167
info!(target: LOG_TARGET, "Loading block {}({:x}{:x}{:x}{:x}) into chain", block.height, block.hash[0], block.hash[1], block.hash[2], block.hash[3]);
168-
new_chain.add_block_to_chain(block)?;
168+
let _ = new_chain.add_block_to_chain(block).inspect_err(|e| {
169+
error!(target: LOG_TARGET, "Failed to load block into chain: {}", e);
170+
});
169171
}
170172
Ok(new_chain)
171173
}
@@ -466,12 +468,13 @@ impl<T: BlockCache> P2Chain<T> {
466468
let nextblock = parent_level.get_header(&current_block.prev_hash);
467469
if nextblock.is_none() {
468470
error!(target: LOG_TARGET, "FATAL: Reorging (block in chain) failed because parent block was not found and chain data is corrupted.");
469-
panic!(
470-
"FATAL: Reorging (block in chain) failed because parent block was not found and chain \
471-
data is corrupted. current_block: {:?}, current tip: {:?}",
472-
current_block.height,
473-
self.get_tip().map(|t| t.height())
474-
);
471+
return Err(ShareChainError::BlockNotFound);
472+
// panic!(
473+
// "FATAL: Reorging (block in chain) failed because parent block was not found and chain
474+
// \ data is corrupted. current_block: {:?}, current tip:
475+
// {:?}", current_block.height,
476+
// self.get_tip().map(|t| t.height())
477+
// );
475478
}
476479
// fix the main chain
477480
let mut_parent_level = self.level_at_height(current_block.height.saturating_sub(1)).unwrap();

0 commit comments

Comments
 (0)