diff options
author | Pieter Wuille <pieter@wuille.net> | 2023-07-29 13:59:35 -0400 |
---|---|---|
committer | Pieter Wuille <pieter@wuille.net> | 2023-08-23 20:13:49 -0400 |
commit | 8a3b6f33873a1075f932f5d9feb6d82e50d83c0c (patch) | |
tree | 4baf7a53ca7f60f6752675983e6cd5caea23e354 /src | |
parent | bb4aab90fd046f2fff61e082a0c0d01c5ee31297 (diff) | |
download | bitcoin-8a3b6f33873a1075f932f5d9feb6d82e50d83c0c.tar.xz |
refactor: make Transport::ReceivedBytes just return success/fail
Diffstat (limited to 'src')
-rw-r--r-- | src/net.cpp | 5 | ||||
-rw-r--r-- | src/net.h | 23 | ||||
-rw-r--r-- | src/test/fuzz/p2p_transport_serialization.cpp | 3 |
3 files changed, 21 insertions, 10 deletions
diff --git a/src/net.cpp b/src/net.cpp index 9fce585b81..e66c0ec7f8 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -690,9 +690,8 @@ bool CNode::ReceiveMsgBytes(Span<const uint8_t> msg_bytes, bool& complete) nRecvBytes += msg_bytes.size(); while (msg_bytes.size() > 0) { // absorb network data - int handled = m_transport->ReceivedBytes(msg_bytes); - if (handled < 0) { - // Serious header problem, disconnect from the peer. + if (!m_transport->ReceivedBytes(msg_bytes)) { + // Serious transport problem, disconnect from the peer. return false; } @@ -268,9 +268,22 @@ public: virtual bool ReceivedMessageComplete() const = 0; /** Set the deserialization context version for objects returned by GetReceivedMessage. */ virtual void SetReceiveVersion(int version) = 0; - /** Feed wire bytes to the transport; chops off consumed bytes off front of msg_bytes. */ - virtual int ReceivedBytes(Span<const uint8_t>& msg_bytes) = 0; - /** Retrieve a completed message from transport (only when ReceivedMessageComplete). */ + + /** Feed wire bytes to the transport. + * + * @return false if some bytes were invalid, in which case the transport can't be used anymore. + * + * Consumed bytes are chopped off the front of msg_bytes. + */ + virtual bool ReceivedBytes(Span<const uint8_t>& msg_bytes) = 0; + + /** Retrieve a completed message from transport. + * + * This can only be called when ReceivedMessageComplete() is true. + * + * If reject_message=true is returned the message itself is invalid, but (other than false + * returned by ReceivedBytes) the transport is not in an inconsistent state. + */ virtual CNetMessage GetReceivedMessage(std::chrono::microseconds time, bool& reject_message) = 0; // 2. Sending side functions, for converting messages into bytes to be sent over the wire. @@ -387,7 +400,7 @@ public: vRecv.SetVersion(nVersionIn); } - int ReceivedBytes(Span<const uint8_t>& msg_bytes) override EXCLUSIVE_LOCKS_REQUIRED(!m_recv_mutex) + bool ReceivedBytes(Span<const uint8_t>& msg_bytes) override EXCLUSIVE_LOCKS_REQUIRED(!m_recv_mutex) { AssertLockNotHeld(m_recv_mutex); LOCK(m_recv_mutex); @@ -397,7 +410,7 @@ public: } else { msg_bytes = msg_bytes.subspan(ret); } - return ret; + return ret >= 0; } CNetMessage GetReceivedMessage(std::chrono::microseconds time, bool& reject_message) override EXCLUSIVE_LOCKS_REQUIRED(!m_recv_mutex); diff --git a/src/test/fuzz/p2p_transport_serialization.cpp b/src/test/fuzz/p2p_transport_serialization.cpp index 8363779916..2fa5de5008 100644 --- a/src/test/fuzz/p2p_transport_serialization.cpp +++ b/src/test/fuzz/p2p_transport_serialization.cpp @@ -74,8 +74,7 @@ FUZZ_TARGET(p2p_transport_serialization, .init = initialize_p2p_transport_serial mutable_msg_bytes.insert(mutable_msg_bytes.end(), payload_bytes.begin(), payload_bytes.end()); Span<const uint8_t> msg_bytes{mutable_msg_bytes}; while (msg_bytes.size() > 0) { - const int handled = recv_transport.ReceivedBytes(msg_bytes); - if (handled < 0) { + if (!recv_transport.ReceivedBytes(msg_bytes)) { break; } if (recv_transport.ReceivedMessageComplete()) { |