From cce709ab8509fa94ebf8950b477cf6ecba4aa91c Mon Sep 17 00:00:00 2001 From: Alfredo Garcia Date: Sat, 12 Feb 2022 16:30:23 -0300 Subject: [PATCH 1/4] ignore non-verack message in handshake --- zebra-network/src/peer/handshake.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/zebra-network/src/peer/handshake.rs b/zebra-network/src/peer/handshake.rs index d4131b12c4b..2c70395ac7e 100644 --- a/zebra-network/src/peer/handshake.rs +++ b/zebra-network/src/peer/handshake.rs @@ -705,9 +705,9 @@ where .await .ok_or(HandshakeError::ConnectionClosed)??; if let Message::Verack = remote_msg { - debug!("got verack from remote peer"); + debug!("got verack message from remote peer"); } else { - Err(HandshakeError::UnexpectedMessage(Box::new(remote_msg)))?; + debug!("ignoring non-verack message from remote peer"); } Ok((remote_version, remote_services, remote_canonical_addr)) From 222dfdd69eb7f4ed82dd06d376f0adb2f6ed537c Mon Sep 17 00:00:00 2001 From: Alfredo Garcia Date: Sat, 12 Feb 2022 16:52:50 -0300 Subject: [PATCH 2/4] ignore non-version message in handshake --- zebra-network/src/peer/handshake.rs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/zebra-network/src/peer/handshake.rs b/zebra-network/src/peer/handshake.rs index 2c70395ac7e..f79296397b5 100644 --- a/zebra-network/src/peer/handshake.rs +++ b/zebra-network/src/peer/handshake.rs @@ -588,13 +588,25 @@ where debug!(?our_version, "sending initial version message"); peer_conn.send(our_version).await?; - let remote_msg = peer_conn + let mut remote_msg = peer_conn .next() .await .ok_or(HandshakeError::ConnectionClosed)??; - - // Check that we got a Version and destructure its fields into the local scope. debug!(?remote_msg, "got message from remote peer"); + + // Wait for next message if the one we have is not Version + match remote_msg { + Message::Version { .. } => (), + _ => { + remote_msg = peer_conn + .next() + .await + .ok_or(HandshakeError::ConnectionClosed)??; + debug!(?remote_msg, "got message from remote peer"); + } + } + + // If we got a Version message, destructure its fields into the local scope. let (remote_nonce, remote_services, remote_version, remote_canonical_addr, user_agent) = if let Message::Version { version, From c882606c733f67efe759ee9919bdebb45dfceb16 Mon Sep 17 00:00:00 2001 From: Alfredo Garcia Date: Mon, 14 Feb 2022 08:46:20 -0300 Subject: [PATCH 3/4] add loops for waiting --- zebra-network/src/peer/handshake.rs | 46 +++++++++++++++++++---------- 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/zebra-network/src/peer/handshake.rs b/zebra-network/src/peer/handshake.rs index f79296397b5..8fe1bf0642a 100644 --- a/zebra-network/src/peer/handshake.rs +++ b/zebra-network/src/peer/handshake.rs @@ -592,17 +592,21 @@ where .next() .await .ok_or(HandshakeError::ConnectionClosed)??; - debug!(?remote_msg, "got message from remote peer"); - // Wait for next message if the one we have is not Version - match remote_msg { - Message::Version { .. } => (), - _ => { - remote_msg = peer_conn - .next() - .await - .ok_or(HandshakeError::ConnectionClosed)??; - debug!(?remote_msg, "got message from remote peer"); + // Wait for next message if the one we got is not Version + loop { + match remote_msg { + Message::Version { .. } => { + debug!(?remote_msg, "got version message from remote peer"); + break; + } + _ => { + remote_msg = peer_conn + .next() + .await + .ok_or(HandshakeError::ConnectionClosed)??; + debug!("ignoring non-version message from remote peer"); + } } } @@ -712,14 +716,26 @@ where peer_conn.send(Message::Verack).await?; - let remote_msg = peer_conn + let mut remote_msg = peer_conn .next() .await .ok_or(HandshakeError::ConnectionClosed)??; - if let Message::Verack = remote_msg { - debug!("got verack message from remote peer"); - } else { - debug!("ignoring non-verack message from remote peer"); + + // Wait for next message if the one we got is not Verack + loop { + match remote_msg { + Message::Verack => { + debug!(?remote_msg, "got verack message from remote peer"); + break; + } + _ => { + remote_msg = peer_conn + .next() + .await + .ok_or(HandshakeError::ConnectionClosed)??; + debug!("ignoring non-verack message from remote peer"); + } + } } Ok((remote_version, remote_services, remote_canonical_addr)) From dc6679b35413fcb02069f54b10b0cfd6a2d8ea8e Mon Sep 17 00:00:00 2001 From: Alfredo Garcia Date: Tue, 15 Feb 2022 09:56:25 -0300 Subject: [PATCH 4/4] improve logging Co-authored-by: teor --- zebra-network/src/peer/handshake.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/zebra-network/src/peer/handshake.rs b/zebra-network/src/peer/handshake.rs index 8fe1bf0642a..e7107678cce 100644 --- a/zebra-network/src/peer/handshake.rs +++ b/zebra-network/src/peer/handshake.rs @@ -605,7 +605,7 @@ where .next() .await .ok_or(HandshakeError::ConnectionClosed)??; - debug!("ignoring non-version message from remote peer"); + debug!(?remote_msg, "ignoring non-version message from remote peer"); } } } @@ -733,7 +733,7 @@ where .next() .await .ok_or(HandshakeError::ConnectionClosed)??; - debug!("ignoring non-verack message from remote peer"); + debug!(?remote_msg, "ignoring non-verack message from remote peer"); } } }