Skip to content

Commit 8a3b6f3

Browse files
committed
refactor: make Transport::ReceivedBytes just return success/fail
1 parent bb4aab9 commit 8a3b6f3

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

src/net.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -690,9 +690,8 @@ bool CNode::ReceiveMsgBytes(Span<const uint8_t> msg_bytes, bool& complete)
690690
nRecvBytes += msg_bytes.size();
691691
while (msg_bytes.size() > 0) {
692692
// absorb network data
693-
int handled = m_transport->ReceivedBytes(msg_bytes);
694-
if (handled < 0) {
695-
// Serious header problem, disconnect from the peer.
693+
if (!m_transport->ReceivedBytes(msg_bytes)) {
694+
// Serious transport problem, disconnect from the peer.
696695
return false;
697696
}
698697

src/net.h

+18-5
Original file line numberDiff line numberDiff line change
@@ -268,9 +268,22 @@ class Transport {
268268
virtual bool ReceivedMessageComplete() const = 0;
269269
/** Set the deserialization context version for objects returned by GetReceivedMessage. */
270270
virtual void SetReceiveVersion(int version) = 0;
271-
/** Feed wire bytes to the transport; chops off consumed bytes off front of msg_bytes. */
272-
virtual int ReceivedBytes(Span<const uint8_t>& msg_bytes) = 0;
273-
/** Retrieve a completed message from transport (only when ReceivedMessageComplete). */
271+
272+
/** Feed wire bytes to the transport.
273+
*
274+
* @return false if some bytes were invalid, in which case the transport can't be used anymore.
275+
*
276+
* Consumed bytes are chopped off the front of msg_bytes.
277+
*/
278+
virtual bool ReceivedBytes(Span<const uint8_t>& msg_bytes) = 0;
279+
280+
/** Retrieve a completed message from transport.
281+
*
282+
* This can only be called when ReceivedMessageComplete() is true.
283+
*
284+
* If reject_message=true is returned the message itself is invalid, but (other than false
285+
* returned by ReceivedBytes) the transport is not in an inconsistent state.
286+
*/
274287
virtual CNetMessage GetReceivedMessage(std::chrono::microseconds time, bool& reject_message) = 0;
275288

276289
// 2. Sending side functions, for converting messages into bytes to be sent over the wire.
@@ -387,7 +400,7 @@ class V1Transport final : public Transport
387400
vRecv.SetVersion(nVersionIn);
388401
}
389402

390-
int ReceivedBytes(Span<const uint8_t>& msg_bytes) override EXCLUSIVE_LOCKS_REQUIRED(!m_recv_mutex)
403+
bool ReceivedBytes(Span<const uint8_t>& msg_bytes) override EXCLUSIVE_LOCKS_REQUIRED(!m_recv_mutex)
391404
{
392405
AssertLockNotHeld(m_recv_mutex);
393406
LOCK(m_recv_mutex);
@@ -397,7 +410,7 @@ class V1Transport final : public Transport
397410
} else {
398411
msg_bytes = msg_bytes.subspan(ret);
399412
}
400-
return ret;
413+
return ret >= 0;
401414
}
402415

403416
CNetMessage GetReceivedMessage(std::chrono::microseconds time, bool& reject_message) override EXCLUSIVE_LOCKS_REQUIRED(!m_recv_mutex);

src/test/fuzz/p2p_transport_serialization.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ FUZZ_TARGET(p2p_transport_serialization, .init = initialize_p2p_transport_serial
7474
mutable_msg_bytes.insert(mutable_msg_bytes.end(), payload_bytes.begin(), payload_bytes.end());
7575
Span<const uint8_t> msg_bytes{mutable_msg_bytes};
7676
while (msg_bytes.size() > 0) {
77-
const int handled = recv_transport.ReceivedBytes(msg_bytes);
78-
if (handled < 0) {
77+
if (!recv_transport.ReceivedBytes(msg_bytes)) {
7978
break;
8079
}
8180
if (recv_transport.ReceivedMessageComplete()) {

0 commit comments

Comments
 (0)