From 475b1c64bb10b5ff3518582ab7a129bf55086b55 Mon Sep 17 00:00:00 2001 From: Cory Fields Date: Tue, 16 Nov 2021 18:09:45 -0300 Subject: [PATCH] net: require a verack before responding to anything else 7a8c251901 made this logic hard to follow. After that change, messages would not be sent to a peer via SendMessages() before the handshake was complete, but messages could still be sent as a response to an incoming message. For example, if a peer had not yet sent a verack, we wouldn't notify it about new blocks, but we would respond to a PING with a PONG. This change makes the behavior straightforward: until we've received a verack, never send any message other than version/verack/reject. The behavior until a VERACK is received has always been undefined, this change just tightens our policy. This also makes testing much easier, because we can now connect but not send version/verack, and anything sent to us is an error. --- src/net_processing.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/net_processing.cpp b/src/net_processing.cpp index c6b5c44340290..ca761f136ecf1 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -1290,6 +1290,14 @@ bool static ProcessMessage(CNode* pfrom, std::string strCommand, CDataStream& vR (fLogIPs ? strprintf(", peeraddr=%s", pfrom->addr.ToString()) : "")); } + else if (!pfrom->fSuccessfullyConnected) + { + // Must have a verack message before anything else + LOCK(cs_main); + Misbehaving(pfrom->GetId(), 1); + return false; + } + else if (strCommand == NetMsgType::ADDR || strCommand == NetMsgType::ADDRV2) { int stream_version = vRecv.GetVersion();