diff --git a/crates/net/network-devp2p/src/session.rs b/crates/net/network-devp2p/src/session.rs index bece885fb..117f0736f 100644 --- a/crates/net/network-devp2p/src/session.rs +++ b/crates/net/network-devp2p/src/session.rs @@ -648,7 +648,9 @@ impl Session { pub(crate) fn update_token_id(&mut self, token: StreamToken) -> Result<(), Error> { match self.state { - State::Handshake(ref _h) => return Err(ErrorKind::HostCacheInconsistency.into()), + State::Handshake(ref _h) => { + return Err(ErrorKind::SessionStateInconsistency(token).into()); + } State::Session(ref mut s) => { s.connection.token = token; return Ok(()); diff --git a/crates/net/network-devp2p/src/session_container.rs b/crates/net/network-devp2p/src/session_container.rs index 6e2b514e3..21ee3fe69 100644 --- a/crates/net/network-devp2p/src/session_container.rs +++ b/crates/net/network-devp2p/src/session_container.rs @@ -198,7 +198,7 @@ impl SessionContainer { if let Some(existing_session_mutex) = existing_session_mutex_o { let session = existing_session_mutex.lock(); if let Some(id_from_session) = &session.info.id { - if session.info.id == Some(*node_id) { + if id_from_session == node_id { // we got already got a session for the specified node. // maybe the old session is already scheduled for getting deleted. if !session.expired() { @@ -208,13 +208,22 @@ impl SessionContainer { ) .into()); } + // if the session it expired, we will create a Handshake, so it can get restablished. } else { - error!(target: "network", "host cache inconsistency: Session node id mismatch. expected: {} is {}.", existing_peer_id, id_from_session); - return Err(ErrorKind::HostCacheInconsistency.into()); + return Err(ErrorKind::HostCacheInconsistencySessionMissmatch( + node_id.clone(), + existing_peer_id.clone(), + id_from_session.clone(), + ) + .into()); } } else { - error!(target: "network", "host cache inconsistency: Session has no Node_id defined where it should for {}", existing_peer_id); - return Err(ErrorKind::HostCacheInconsistency.into()); + error!(target: "network", "Host cache inconsistency: Session has no Node_id defined where it should for {}", existing_peer_id); + return Err(ErrorKind::HostCacheInconsistencyNodeIDMissing( + node_id.clone(), + existing_peer_id.clone(), + ) + .into()); } // session guard is dropped here } @@ -310,11 +319,10 @@ impl SessionContainer { let node_id = match id { Some(id) => id.clone(), None => { - error!(target: "network", "Tried to register finalized handshake without node id"); // We have no Node ID, so we can't promote it to a full session mapped by Node ID. // This might indicate an error state, or a handshake that failed to yield a Node ID. // For now, we'll just log and return. - return Err(ErrorKind::HostCacheInconsistency.into()); + return Err(ErrorKind::HandshakeFinalisationMissingNodeId(token).into()); } }; @@ -361,7 +369,7 @@ impl SessionContainer { } return Ok(upgraded_token); } else { - return Err(ErrorKind::HostCacheInconsistency.into()); + return Err(ErrorKind::HandshakeNotRemoved(token).into()); } } diff --git a/crates/net/network/src/error.rs b/crates/net/network/src/error.rs index 00eab4f68..84022d2ef 100644 --- a/crates/net/network/src/error.rs +++ b/crates/net/network/src/error.rs @@ -173,9 +173,33 @@ error_chain! { } #[doc = "A connection to the specified NodeId exists, but there is a mismatch in the host cache."] - HostCacheInconsistency { - description("A connection to the specified nodeId already exists."), - display("A connection to the specified NodeId exists, but there is a mismatch in the host cache."), + HostCacheInconsistencySessionMissmatch(wanted_node: crate::NodeId, peer_id_from_cache: usize, actual_session_node: crate::NodeId) { + description("A HostCache mapping points to a wrong session"), + display("A connection to the specified NodeId {wanted_node:?} exists, but the real session on peer ID {peer_id_from_cache} points to NodeId: {actual_session_node:?}"), + } + + #[doc = "A connection to the specified NodeId exists, but there is no NodeId defined on the session."] + HostCacheInconsistencyNodeIDMissing(wanted_node: crate::NodeId, peer_id_from_cache: usize) { + description("A HostCache mapping points to a session, that does not have a NodeID defined."), + display("A connection to the specified NodeId {wanted_node:?} exists with peer_id {peer_id_from_cache}, but the Session information does not have NodeID Information."), + } + + #[doc = "A handshake can not get finalized."] + HandshakeFinalisationMissingNodeId(peer_id: usize) { + description("Tried to register finalized handshake without a node id"), + display("Tried to register finalized handshake for peer {peer_id} without a node id"), + } + + #[doc = "A handshake state is wrong."] + SessionStateInconsistency(stream_token: usize) { + description("A Session state is wrong."), + display("A Session state is wrong for stream id {stream_token}"), + } + + #[doc = "A handshake could not get removed."] + HandshakeNotRemoved(stream_token: usize) { + description("Tried to remove a Handshake, but the session was not found."), + display("Tried to remove a Handshake for id {stream_token}, but the session was not found."), } #[doc = "An unknown IO error occurred."]