Skip to content

Commit fdd0685

Browse files
committed
Merge #20056: net: Use Span in ReceiveMsgBytes
fa5ed3b net: Use Span in ReceiveMsgBytes (MarcoFalke) Pull request description: Pass a data pointer and a size as span in `ReceiveMsgBytes` to get the benefits of a span ACKs for top commit: jonatack: ACK fa5ed3b code review, rebased to current master 12a1c3a, debug build, unit tests, ran bitcoind/-netinfo/getpeerinfo theStack: ACK fa5ed3b Tree-SHA512: 89bf111323148d6e6e50185ad20ab39f73ab3a58a27e46319e3a08bcf5dcf9d6aa84faff0fd6afb90cb892ac2f557a237c144560986063bc736a69ace353ab9d
2 parents 46f0b2f + fa5ed3b commit fdd0685

File tree

6 files changed

+43
-45
lines changed

6 files changed

+43
-45
lines changed

src/net.cpp

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -629,34 +629,21 @@ void CNode::copyStats(CNodeStats &stats, const std::vector<bool> &m_asmap)
629629
}
630630
#undef X
631631

632-
/**
633-
* Receive bytes from the buffer and deserialize them into messages.
634-
*
635-
* @param[in] pch A pointer to the raw data
636-
* @param[in] nBytes Size of the data
637-
* @param[out] complete Set True if at least one message has been
638-
* deserialized and is ready to be processed
639-
* @return True if the peer should stay connected,
640-
* False if the peer should be disconnected from.
641-
*/
642-
bool CNode::ReceiveMsgBytes(const char *pch, unsigned int nBytes, bool& complete)
632+
bool CNode::ReceiveMsgBytes(Span<const char> msg_bytes, bool& complete)
643633
{
644634
complete = false;
645635
const auto time = GetTime<std::chrono::microseconds>();
646636
LOCK(cs_vRecv);
647637
nLastRecv = std::chrono::duration_cast<std::chrono::seconds>(time).count();
648-
nRecvBytes += nBytes;
649-
while (nBytes > 0) {
638+
nRecvBytes += msg_bytes.size();
639+
while (msg_bytes.size() > 0) {
650640
// absorb network data
651-
int handled = m_deserializer->Read(pch, nBytes);
641+
int handled = m_deserializer->Read(msg_bytes);
652642
if (handled < 0) {
653643
// Serious header problem, disconnect from the peer.
654644
return false;
655645
}
656646

657-
pch += handled;
658-
nBytes -= handled;
659-
660647
if (m_deserializer->Complete()) {
661648
// decompose a transport agnostic CNetMessage from the deserializer
662649
uint32_t out_err_raw_size{0};
@@ -686,13 +673,13 @@ bool CNode::ReceiveMsgBytes(const char *pch, unsigned int nBytes, bool& complete
686673
return true;
687674
}
688675

689-
int V1TransportDeserializer::readHeader(const char *pch, unsigned int nBytes)
676+
int V1TransportDeserializer::readHeader(Span<const char> msg_bytes)
690677
{
691678
// copy data to temporary parsing buffer
692679
unsigned int nRemaining = CMessageHeader::HEADER_SIZE - nHdrPos;
693-
unsigned int nCopy = std::min(nRemaining, nBytes);
680+
unsigned int nCopy = std::min<unsigned int>(nRemaining, msg_bytes.size());
694681

695-
memcpy(&hdrbuf[nHdrPos], pch, nCopy);
682+
memcpy(&hdrbuf[nHdrPos], msg_bytes.data(), nCopy);
696683
nHdrPos += nCopy;
697684

698685
// if header incomplete, exit
@@ -726,18 +713,18 @@ int V1TransportDeserializer::readHeader(const char *pch, unsigned int nBytes)
726713
return nCopy;
727714
}
728715

729-
int V1TransportDeserializer::readData(const char *pch, unsigned int nBytes)
716+
int V1TransportDeserializer::readData(Span<const char> msg_bytes)
730717
{
731718
unsigned int nRemaining = hdr.nMessageSize - nDataPos;
732-
unsigned int nCopy = std::min(nRemaining, nBytes);
719+
unsigned int nCopy = std::min<unsigned int>(nRemaining, msg_bytes.size());
733720

734721
if (vRecv.size() < nDataPos + nCopy) {
735722
// Allocate up to 256 KiB ahead, but never more than the total message size.
736723
vRecv.resize(std::min(hdr.nMessageSize, nDataPos + nCopy + 256 * 1024));
737724
}
738725

739-
hasher.Write({(const unsigned char*)pch, nCopy});
740-
memcpy(&vRecv[nDataPos], pch, nCopy);
726+
hasher.Write(MakeUCharSpan(msg_bytes.first(nCopy)));
727+
memcpy(&vRecv[nDataPos], msg_bytes.data(), nCopy);
741728
nDataPos += nCopy;
742729

743730
return nCopy;
@@ -1487,7 +1474,7 @@ void CConnman::SocketHandler()
14871474
if (nBytes > 0)
14881475
{
14891476
bool notify = false;
1490-
if (!pnode->ReceiveMsgBytes(pchBuf, nBytes, notify))
1477+
if (!pnode->ReceiveMsgBytes(Span<const char>(pchBuf, nBytes), notify))
14911478
pnode->CloseSocketDisconnect();
14921479
RecordBytesRecv(nBytes);
14931480
if (notify) {

src/net.h

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -757,8 +757,8 @@ class TransportDeserializer {
757757
virtual bool Complete() const = 0;
758758
// set the serialization context version
759759
virtual void SetVersion(int version) = 0;
760-
// read and deserialize data
761-
virtual int Read(const char *data, unsigned int bytes) = 0;
760+
/** read and deserialize data, advances msg_bytes data pointer */
761+
virtual int Read(Span<const char>& msg_bytes) = 0;
762762
// decomposes a message from the context
763763
virtual Optional<CNetMessage> GetMessage(std::chrono::microseconds time, uint32_t& out_err) = 0;
764764
virtual ~TransportDeserializer() {}
@@ -779,8 +779,8 @@ class V1TransportDeserializer final : public TransportDeserializer
779779
unsigned int nDataPos;
780780

781781
const uint256& GetMessageHash() const;
782-
int readHeader(const char *pch, unsigned int nBytes);
783-
int readData(const char *pch, unsigned int nBytes);
782+
int readHeader(Span<const char> msg_bytes);
783+
int readData(Span<const char> msg_bytes);
784784

785785
void Reset() {
786786
vRecv.clear();
@@ -814,9 +814,14 @@ class V1TransportDeserializer final : public TransportDeserializer
814814
hdrbuf.SetVersion(nVersionIn);
815815
vRecv.SetVersion(nVersionIn);
816816
}
817-
int Read(const char *pch, unsigned int nBytes) override {
818-
int ret = in_data ? readData(pch, nBytes) : readHeader(pch, nBytes);
819-
if (ret < 0) Reset();
817+
int Read(Span<const char>& msg_bytes) override
818+
{
819+
int ret = in_data ? readData(msg_bytes) : readHeader(msg_bytes);
820+
if (ret < 0) {
821+
Reset();
822+
} else {
823+
msg_bytes = msg_bytes.subspan(ret);
824+
}
820825
return ret;
821826
}
822827
Optional<CNetMessage> GetMessage(std::chrono::microseconds time, uint32_t& out_err_raw_size) override;
@@ -1118,7 +1123,16 @@ class CNode
11181123
return nRefCount;
11191124
}
11201125

1121-
bool ReceiveMsgBytes(const char *pch, unsigned int nBytes, bool& complete);
1126+
/**
1127+
* Receive bytes from the buffer and deserialize them into messages.
1128+
*
1129+
* @param[in] msg_bytes The raw data
1130+
* @param[out] complete Set True if at least one message has been
1131+
* deserialized and is ready to be processed
1132+
* @return True if the peer should stay connected,
1133+
* False if the peer should be disconnected from.
1134+
*/
1135+
bool ReceiveMsgBytes(Span<const char> msg_bytes, bool& complete);
11221136

11231137
void SetCommonVersion(int greatest_common_version)
11241138
{

src/test/fuzz/net.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ void test_one_input(const std::vector<uint8_t>& buffer)
128128
case 11: {
129129
const std::vector<uint8_t> b = ConsumeRandomLengthByteVector(fuzzed_data_provider);
130130
bool complete;
131-
node.ReceiveMsgBytes((const char*)b.data(), b.size(), complete);
131+
node.ReceiveMsgBytes({(const char*)b.data(), b.size()}, complete);
132132
break;
133133
}
134134
}

src/test/fuzz/p2p_transport_deserializer.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,12 @@ void test_one_input(const std::vector<uint8_t>& buffer)
2121
{
2222
// Construct deserializer, with a dummy NodeId
2323
V1TransportDeserializer deserializer{Params(), (NodeId)0, SER_NETWORK, INIT_PROTO_VERSION};
24-
const char* pch = (const char*)buffer.data();
25-
size_t n_bytes = buffer.size();
26-
while (n_bytes > 0) {
27-
const int handled = deserializer.Read(pch, n_bytes);
24+
Span<const char> msg_bytes{(const char*)buffer.data(), buffer.size()};
25+
while (msg_bytes.size() > 0) {
26+
const int handled = deserializer.Read(msg_bytes);
2827
if (handled < 0) {
2928
break;
3029
}
31-
pch += handled;
32-
n_bytes -= handled;
3330
if (deserializer.Complete()) {
3431
const std::chrono::microseconds m_time{std::numeric_limits<int64_t>::max()};
3532
uint32_t out_err_raw_size{0};

src/test/util/net.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
#include <chainparams.h>
88
#include <net.h>
99

10-
void ConnmanTestMsg::NodeReceiveMsgBytes(CNode& node, const char* pch, unsigned int nBytes, bool& complete) const
10+
void ConnmanTestMsg::NodeReceiveMsgBytes(CNode& node, Span<const char> msg_bytes, bool& complete) const
1111
{
12-
assert(node.ReceiveMsgBytes(pch, nBytes, complete));
12+
assert(node.ReceiveMsgBytes(msg_bytes, complete));
1313
if (complete) {
1414
size_t nSizeAdded = 0;
1515
auto it(node.vRecvMsg.begin());
@@ -33,7 +33,7 @@ bool ConnmanTestMsg::ReceiveMsgFrom(CNode& node, CSerializedNetMsg& ser_msg) con
3333
node.m_serializer->prepareForTransport(ser_msg, ser_msg_header);
3434

3535
bool complete;
36-
NodeReceiveMsgBytes(node, (const char*)ser_msg_header.data(), ser_msg_header.size(), complete);
37-
NodeReceiveMsgBytes(node, (const char*)ser_msg.data.data(), ser_msg.data.size(), complete);
36+
NodeReceiveMsgBytes(node, {(const char*)ser_msg_header.data(), ser_msg_header.size()}, complete);
37+
NodeReceiveMsgBytes(node, {(const char*)ser_msg.data.data(), ser_msg.data.size()}, complete);
3838
return complete;
3939
}

src/test/util/net.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ struct ConnmanTestMsg : public CConnman {
2525

2626
void ProcessMessagesOnce(CNode& node) { m_msgproc->ProcessMessages(&node, flagInterruptMsgProc); }
2727

28-
void NodeReceiveMsgBytes(CNode& node, const char* pch, unsigned int nBytes, bool& complete) const;
28+
void NodeReceiveMsgBytes(CNode& node, Span<const char> msg_bytes, bool& complete) const;
2929

3030
bool ReceiveMsgFrom(CNode& node, CSerializedNetMsg& ser_msg) const;
3131
};

0 commit comments

Comments
 (0)