Skip to content

Commit a00f9e8

Browse files
committed
fix(p2p-service): implement a periodic re-establish connections
1 parent 5fccc34 commit a00f9e8

File tree

1 file changed

+39
-4
lines changed

1 file changed

+39
-4
lines changed

crates/p2p-service/src/bootstrap.rs

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,25 @@
22
33
use std::time::Duration;
44

5-
use strata_p2p::swarm::{
6-
self, handle::P2PHandle, P2PConfig, DEFAULT_CONNECTION_CHECK_INTERVAL, DEFAULT_DIAL_TIMEOUT,
7-
DEFAULT_GENERAL_TIMEOUT, P2P,
5+
use strata_p2p::{
6+
commands::{Command, ConnectToPeerCommand},
7+
swarm::{
8+
self, handle::P2PHandle, P2PConfig, DEFAULT_CONNECTION_CHECK_INTERVAL,
9+
DEFAULT_DIAL_TIMEOUT, DEFAULT_GENERAL_TIMEOUT, P2P,
10+
},
811
};
912
use tokio::task::JoinHandle;
1013
use tokio_util::sync::CancellationToken;
11-
use tracing::info;
14+
use tracing::{debug, info, warn};
1215

1316
use crate::{config::Configuration, constants::DEFAULT_IDLE_CONNECTION_TIMEOUT};
1417

1518
/// Bootstrap the p2p node by hooking up all the required services.
1619
pub async fn bootstrap(
1720
config: &Configuration,
1821
) -> anyhow::Result<(P2PHandle, CancellationToken, JoinHandle<()>)> {
22+
let allowlist_len = config.allowlist.len();
23+
1924
let p2p_config = P2PConfig {
2025
keypair: config.keypair.clone(),
2126
idle_connection_timeout: config
@@ -48,5 +53,35 @@ pub async fn bootstrap(
4853
info!("listening for network events and commands from handles");
4954
let listen_task = tokio::spawn(p2p.listen());
5055

56+
let connect_handle = handle.clone();
57+
let connect_to = config.connect_to.clone();
58+
let allowlist = config.allowlist.clone();
59+
let _connect_task = tokio::spawn(async move {
60+
loop {
61+
let connected_peers = connect_handle.get_connected_peers().await;
62+
if connected_peers.len() < allowlist_len {
63+
debug!(
64+
connected_peers=%connected_peers.len(),
65+
allowlist=%allowlist_len,
66+
"initializing period re-establishing connections"
67+
);
68+
for (addr, peer_id) in connect_to.iter().zip(allowlist.iter()) {
69+
warn!(
70+
%peer_id,
71+
"re-connecting to peer"
72+
);
73+
let command = Command::ConnectToPeer(ConnectToPeerCommand {
74+
peer_id: *peer_id,
75+
peer_addr: addr.clone(),
76+
});
77+
let _ = connect_handle.send_command(command).await;
78+
debug!(%peer_id, "command sent");
79+
}
80+
}
81+
// TODO(@storopoli): make this configurable
82+
tokio::time::sleep(Duration::from_secs(10)).await;
83+
}
84+
});
85+
5186
Ok((handle, cancel, listen_task))
5287
}

0 commit comments

Comments
 (0)