Skip to content
This repository has been archived by the owner on Dec 17, 2024. It is now read-only.

Handle P2P discovery connection error #139

Merged
merged 3 commits into from
Mar 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion crates/node/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,12 +266,21 @@ async fn run(config: Arc<Config>) -> Result<()> {
let p2p_listen_addr = p2p.node().start_listening().await?;
tracing::info!("listening for p2p at {}", p2p_listen_addr);

let mut connected_nodes = 0;
for addr in config.p2p_discovery_addrs.clone() {
tracing::info!("connecting to p2p peer {}", addr);
match addr.to_socket_addrs() {
Ok(mut socket_iter) => {
if let Some(peer) = socket_iter.next() {
p2p.node().connect(peer).await?;
match p2p.node().connect(peer).await {
Ok(_) => {
connected_nodes += 1;
continue;
}
Err(err) => {
tracing::warn!("failed to connect to {}: {}", peer, err);
}
}
break;
}
}
Expand All @@ -281,6 +290,13 @@ async fn run(config: Arc<Config>) -> Result<()> {
}
}

// If we couldn't connect to any P2P nodes, we'll be left out alone forever.
// Useless to run at that point.
if !config.p2p_discovery_addrs.is_empty() && connected_nodes == 0 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the P2P bootstrap node, the discovery addresses are empty as that's the beacon of the network. In that case it's fine that connected_nodes == 0. For the others: It's useless to continue when no friends are playing with. 🙂

tracing::error!("Failed to connect to any P2P node. Quitting.");
std::process::exit(1);
}

// Start JSON-RPC server.
let rpc_server = rpc_server::RpcServer::run(
config.clone(),
Expand Down
Loading