Skip to content

Commit

Permalink
refactor(l1): improve error handling in RLPx modules (#1017)
Browse files Browse the repository at this point in the history
**Motivation**

Current code is full of potential `panic`s and `unwrap()`s

**Description**

Functions in RLPx modules should now return `Result<_, RLPxError>`s and
prevent panicking.

Closes #845
Fixes commented test from #843
  • Loading branch information
ElFantasma authored Nov 8, 2024
1 parent 134bc94 commit 1e1fde0
Show file tree
Hide file tree
Showing 17 changed files with 502 additions and 450 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,4 @@ jsonwebtoken = "9.3.0"
rand = "0.8.5"
cfg-if = "1.0.0"
reqwest = { version = "0.12.7", features = ["json"] }
snap = "1.1.1"
1 change: 1 addition & 0 deletions crates/common/rlp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ bytes.workspace = true
hex.workspace = true
lazy_static.workspace = true
ethereum-types.workspace = true
snap.workspace = true

[dev-dependencies]
hex-literal.workspace = true
Expand Down
4 changes: 3 additions & 1 deletion crates/common/rlp/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ pub enum RLPDecodeError {
UnexpectedList,
#[error("UnexpectedString")]
UnexpectedString,
#[error("InvalidCompression")]
InvalidCompression(#[from] snap::Error),
#[error("{0}")]
Custom(String),
}
Expand All @@ -21,7 +23,7 @@ pub enum RLPDecodeError {
#[derive(Debug, Error)]
pub enum RLPEncodeError {
#[error("InvalidCompression")]
InvalidCompression,
InvalidCompression(#[from] snap::Error),
#[error("{0}")]
Custom(String),
}
20 changes: 14 additions & 6 deletions crates/networking/p2p/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use tokio::{
sync::Mutex,
try_join,
};
use tracing::{debug, info};
use tracing::{debug, error, info};
use types::{Endpoint, Node};

pub mod bootnode;
Expand Down Expand Up @@ -774,8 +774,12 @@ async fn handle_peer_as_initiator(
.connect(SocketAddr::new(node.ip, node.tcp_port))
.await
.unwrap();
let conn = RLPxConnection::initiator(signer, msg, stream, storage).await;
handle_peer(conn, table).await;
match RLPxConnection::initiator(signer, msg, stream, storage).await {
Ok(conn) => handle_peer(conn, table).await,
Err(e) => {
error!("Error: {e}, Could not start connection with {node:?}");
}
}
}

async fn handle_peer(mut conn: RLPxConnection<TcpStream>, table: Arc<Mutex<KademliaTable>>) {
Expand All @@ -785,9 +789,13 @@ async fn handle_peer(mut conn: RLPxConnection<TcpStream>, table: Arc<Mutex<Kadem
Err(e) => info!("Error during RLPx connection: ({e})"),
},
Err(e) => {
// Discard peer from kademlia table
info!("Handshake failed, discarding peer: ({e})");
table.lock().await.replace_peer(conn.get_remote_node_id());
if let Ok(node_id) = conn.get_remote_node_id() {
// Discard peer from kademlia table
info!("Handshake failed: ({e}), discarding peer {node_id}");
table.lock().await.replace_peer(node_id);
} else {
info!("Handshake failed: ({e}), unknown peer");
}
}
}
}
Expand Down
Loading

0 comments on commit 1e1fde0

Please sign in to comment.