Skip to content

Commit fa79a88

Browse files
author
MarcoFalke
committed
refactor: P2P transport without serialize version and type
1 parent fa9b5f4 commit fa79a88

File tree

6 files changed

+40
-53
lines changed

6 files changed

+40
-53
lines changed

src/net.cpp

+14-15
Original file line numberDiff line numberDiff line change
@@ -683,8 +683,8 @@ bool CNode::ReceiveMsgBytes(Span<const uint8_t> msg_bytes, bool& complete)
683683
return true;
684684
}
685685

686-
V1Transport::V1Transport(const NodeId node_id, int nTypeIn, int nVersionIn) noexcept :
687-
m_magic_bytes{Params().MessageStart()}, m_node_id(node_id), hdrbuf(nTypeIn, nVersionIn), vRecv(nTypeIn, nVersionIn)
686+
V1Transport::V1Transport(const NodeId node_id) noexcept
687+
: m_magic_bytes{Params().MessageStart()}, m_node_id{node_id}
688688
{
689689
LOCK(m_recv_mutex);
690690
Reset();
@@ -968,12 +968,12 @@ void V2Transport::StartSendingHandshake() noexcept
968968
// We cannot wipe m_send_garbage as it will still be used as AAD later in the handshake.
969969
}
970970

971-
V2Transport::V2Transport(NodeId nodeid, bool initiating, int type_in, int version_in, const CKey& key, Span<const std::byte> ent32, std::vector<uint8_t> garbage) noexcept :
972-
m_cipher{key, ent32}, m_initiating{initiating}, m_nodeid{nodeid},
973-
m_v1_fallback{nodeid, type_in, version_in}, m_recv_type{type_in}, m_recv_version{version_in},
974-
m_recv_state{initiating ? RecvState::KEY : RecvState::KEY_MAYBE_V1},
975-
m_send_garbage{std::move(garbage)},
976-
m_send_state{initiating ? SendState::AWAITING_KEY : SendState::MAYBE_V1}
971+
V2Transport::V2Transport(NodeId nodeid, bool initiating, const CKey& key, Span<const std::byte> ent32, std::vector<uint8_t> garbage) noexcept
972+
: m_cipher{key, ent32}, m_initiating{initiating}, m_nodeid{nodeid},
973+
m_v1_fallback{nodeid},
974+
m_recv_state{initiating ? RecvState::KEY : RecvState::KEY_MAYBE_V1},
975+
m_send_garbage{std::move(garbage)},
976+
m_send_state{initiating ? SendState::AWAITING_KEY : SendState::MAYBE_V1}
977977
{
978978
Assume(m_send_garbage.size() <= MAX_GARBAGE_LEN);
979979
// Start sending immediately if we're the initiator of the connection.
@@ -983,9 +983,9 @@ V2Transport::V2Transport(NodeId nodeid, bool initiating, int type_in, int versio
983983
}
984984
}
985985

986-
V2Transport::V2Transport(NodeId nodeid, bool initiating, int type_in, int version_in) noexcept :
987-
V2Transport{nodeid, initiating, type_in, version_in, GenerateRandomKey(),
988-
MakeByteSpan(GetRandHash()), GenerateRandomGarbage()} { }
986+
V2Transport::V2Transport(NodeId nodeid, bool initiating) noexcept
987+
: V2Transport{nodeid, initiating, GenerateRandomKey(),
988+
MakeByteSpan(GetRandHash()), GenerateRandomGarbage()} {}
989989

990990
void V2Transport::SetReceiveState(RecvState recv_state) noexcept
991991
{
@@ -1429,8 +1429,7 @@ CNetMessage V2Transport::GetReceivedMessage(std::chrono::microseconds time, bool
14291429
Assume(m_recv_state == RecvState::APP_READY);
14301430
Span<const uint8_t> contents{m_recv_decode_buffer};
14311431
auto msg_type = GetMessageType(contents);
1432-
CDataStream ret(m_recv_type, m_recv_version);
1433-
CNetMessage msg{std::move(ret)};
1432+
CNetMessage msg{DataStream{}};
14341433
// Note that BIP324Cipher::EXPANSION also includes the length descriptor size.
14351434
msg.m_raw_message_size = m_recv_decode_buffer.size() + BIP324Cipher::EXPANSION;
14361435
if (msg_type) {
@@ -3638,9 +3637,9 @@ ServiceFlags CConnman::GetLocalServices() const
36383637
static std::unique_ptr<Transport> MakeTransport(NodeId id, bool use_v2transport, bool inbound) noexcept
36393638
{
36403639
if (use_v2transport) {
3641-
return std::make_unique<V2Transport>(id, /*initiating=*/!inbound, SER_NETWORK, INIT_PROTO_VERSION);
3640+
return std::make_unique<V2Transport>(id, /*initiating=*/!inbound);
36423641
} else {
3643-
return std::make_unique<V1Transport>(id, SER_NETWORK, INIT_PROTO_VERSION);
3642+
return std::make_unique<V1Transport>(id);
36443643
}
36453644
}
36463645

src/net.h

+9-19
Original file line numberDiff line numberDiff line change
@@ -232,15 +232,16 @@ class CNodeStats
232232
* Ideally it should only contain receive time, payload,
233233
* type and size.
234234
*/
235-
class CNetMessage {
235+
class CNetMessage
236+
{
236237
public:
237-
CDataStream m_recv; //!< received message data
238+
DataStream m_recv; //!< received message data
238239
std::chrono::microseconds m_time{0}; //!< time of message receipt
239240
uint32_t m_message_size{0}; //!< size of the payload
240241
uint32_t m_raw_message_size{0}; //!< used wire size of the message (including header/checksum)
241242
std::string m_type;
242243

243-
CNetMessage(CDataStream&& recv_in) : m_recv(std::move(recv_in)) {}
244+
explicit CNetMessage(DataStream&& recv_in) : m_recv(std::move(recv_in)) {}
244245
// Only one CNetMessage object will exist for the same message on either
245246
// the receive or processing queue. For performance reasons we therefore
246247
// delete the copy constructor and assignment operator to avoid the
@@ -249,11 +250,6 @@ class CNetMessage {
249250
CNetMessage(const CNetMessage&) = delete;
250251
CNetMessage& operator=(CNetMessage&&) = default;
251252
CNetMessage& operator=(const CNetMessage&) = delete;
252-
253-
void SetVersion(int nVersionIn)
254-
{
255-
m_recv.SetVersion(nVersionIn);
256-
}
257253
};
258254

259255
/** The Transport converts one connection's sent messages to wire bytes, and received bytes back. */
@@ -379,9 +375,9 @@ class V1Transport final : public Transport
379375
mutable CHash256 hasher GUARDED_BY(m_recv_mutex);
380376
mutable uint256 data_hash GUARDED_BY(m_recv_mutex);
381377
bool in_data GUARDED_BY(m_recv_mutex); // parsing header (false) or data (true)
382-
CDataStream hdrbuf GUARDED_BY(m_recv_mutex); // partially received header
378+
DataStream hdrbuf GUARDED_BY(m_recv_mutex){}; // partially received header
383379
CMessageHeader hdr GUARDED_BY(m_recv_mutex); // complete header
384-
CDataStream vRecv GUARDED_BY(m_recv_mutex); // received message data
380+
DataStream vRecv GUARDED_BY(m_recv_mutex){}; // received message data
385381
unsigned int nHdrPos GUARDED_BY(m_recv_mutex);
386382
unsigned int nDataPos GUARDED_BY(m_recv_mutex);
387383

@@ -420,7 +416,7 @@ class V1Transport final : public Transport
420416
size_t m_bytes_sent GUARDED_BY(m_send_mutex) {0};
421417

422418
public:
423-
V1Transport(const NodeId node_id, int nTypeIn, int nVersionIn) noexcept;
419+
explicit V1Transport(const NodeId node_id) noexcept;
424420

425421
bool ReceivedMessageComplete() const override EXCLUSIVE_LOCKS_REQUIRED(!m_recv_mutex)
426422
{
@@ -598,10 +594,6 @@ class V2Transport final : public Transport
598594
std::vector<uint8_t> m_recv_aad GUARDED_BY(m_recv_mutex);
599595
/** Buffer to put decrypted contents in, for converting to CNetMessage. */
600596
std::vector<uint8_t> m_recv_decode_buffer GUARDED_BY(m_recv_mutex);
601-
/** Deserialization type. */
602-
const int m_recv_type;
603-
/** Deserialization version number. */
604-
const int m_recv_version;
605597
/** Current receiver state. */
606598
RecvState m_recv_state GUARDED_BY(m_recv_mutex);
607599

@@ -647,13 +639,11 @@ class V2Transport final : public Transport
647639
*
648640
* @param[in] nodeid the node's NodeId (only for debug log output).
649641
* @param[in] initiating whether we are the initiator side.
650-
* @param[in] type_in the serialization type of returned CNetMessages.
651-
* @param[in] version_in the serialization version of returned CNetMessages.
652642
*/
653-
V2Transport(NodeId nodeid, bool initiating, int type_in, int version_in) noexcept;
643+
V2Transport(NodeId nodeid, bool initiating) noexcept;
654644

655645
/** Construct a V2 transport with specified keys and garbage (test use only). */
656-
V2Transport(NodeId nodeid, bool initiating, int type_in, int version_in, const CKey& key, Span<const std::byte> ent32, std::vector<uint8_t> garbage) noexcept;
646+
V2Transport(NodeId nodeid, bool initiating, const CKey& key, Span<const std::byte> ent32, std::vector<uint8_t> garbage) noexcept;
657647

658648
// Receive side functions.
659649
bool ReceivedMessageComplete() const noexcept override EXCLUSIVE_LOCKS_REQUIRED(!m_recv_mutex);

src/net_processing.cpp

+8-10
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ class PeerManagerImpl final : public PeerManager
515515
void RelayTransaction(const uint256& txid, const uint256& wtxid) override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
516516
void SetBestHeight(int height) override { m_best_height = height; };
517517
void UnitTestMisbehaving(NodeId peer_id, int howmuch) override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex) { Misbehaving(*Assert(GetPeerRef(peer_id)), howmuch, ""); };
518-
void ProcessMessage(CNode& pfrom, const std::string& msg_type, CDataStream& vRecv,
518+
void ProcessMessage(CNode& pfrom, const std::string& msg_type, DataStream& vRecv,
519519
const std::chrono::microseconds time_received, const std::atomic<bool>& interruptMsgProc) override
520520
EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex, !m_recent_confirmed_transactions_mutex, !m_most_recent_block_mutex, !m_headers_presync_mutex, g_msgproc_mutex);
521521
void UpdateLastBlockAnnounceTime(NodeId node, int64_t time_in_seconds) override;
@@ -1033,7 +1033,7 @@ class PeerManagerImpl final : public PeerManager
10331033
* @param[in] peer The peer that we received the request from
10341034
* @param[in] vRecv The raw message received
10351035
*/
1036-
void ProcessGetCFilters(CNode& node, Peer& peer, CDataStream& vRecv);
1036+
void ProcessGetCFilters(CNode& node, Peer& peer, DataStream& vRecv);
10371037

10381038
/**
10391039
* Handle a cfheaders request.
@@ -1044,7 +1044,7 @@ class PeerManagerImpl final : public PeerManager
10441044
* @param[in] peer The peer that we received the request from
10451045
* @param[in] vRecv The raw message received
10461046
*/
1047-
void ProcessGetCFHeaders(CNode& node, Peer& peer, CDataStream& vRecv);
1047+
void ProcessGetCFHeaders(CNode& node, Peer& peer, DataStream& vRecv);
10481048

10491049
/**
10501050
* Handle a getcfcheckpt request.
@@ -1055,7 +1055,7 @@ class PeerManagerImpl final : public PeerManager
10551055
* @param[in] peer The peer that we received the request from
10561056
* @param[in] vRecv The raw message received
10571057
*/
1058-
void ProcessGetCFCheckPt(CNode& node, Peer& peer, CDataStream& vRecv);
1058+
void ProcessGetCFCheckPt(CNode& node, Peer& peer, DataStream& vRecv);
10591059

10601060
/** Checks if address relay is permitted with peer. If needed, initializes
10611061
* the m_addr_known bloom filter and sets m_addr_relay_enabled to true.
@@ -3130,7 +3130,7 @@ bool PeerManagerImpl::PrepareBlockFilterRequest(CNode& node, Peer& peer,
31303130
return true;
31313131
}
31323132

3133-
void PeerManagerImpl::ProcessGetCFilters(CNode& node,Peer& peer, CDataStream& vRecv)
3133+
void PeerManagerImpl::ProcessGetCFilters(CNode& node, Peer& peer, DataStream& vRecv)
31343134
{
31353135
uint8_t filter_type_ser;
31363136
uint32_t start_height;
@@ -3159,7 +3159,7 @@ void PeerManagerImpl::ProcessGetCFilters(CNode& node,Peer& peer, CDataStream& vR
31593159
}
31603160
}
31613161

3162-
void PeerManagerImpl::ProcessGetCFHeaders(CNode& node, Peer& peer, CDataStream& vRecv)
3162+
void PeerManagerImpl::ProcessGetCFHeaders(CNode& node, Peer& peer, DataStream& vRecv)
31633163
{
31643164
uint8_t filter_type_ser;
31653165
uint32_t start_height;
@@ -3201,7 +3201,7 @@ void PeerManagerImpl::ProcessGetCFHeaders(CNode& node, Peer& peer, CDataStream&
32013201
filter_hashes);
32023202
}
32033203

3204-
void PeerManagerImpl::ProcessGetCFCheckPt(CNode& node, Peer& peer, CDataStream& vRecv)
3204+
void PeerManagerImpl::ProcessGetCFCheckPt(CNode& node, Peer& peer, DataStream& vRecv)
32053205
{
32063206
uint8_t filter_type_ser;
32073207
uint256 stop_hash;
@@ -3342,7 +3342,7 @@ void PeerManagerImpl::ProcessCompactBlockTxns(CNode& pfrom, Peer& peer, const Bl
33423342
return;
33433343
}
33443344

3345-
void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type, CDataStream& vRecv,
3345+
void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type, DataStream& vRecv,
33463346
const std::chrono::microseconds time_received,
33473347
const std::atomic<bool>& interruptMsgProc)
33483348
{
@@ -5056,8 +5056,6 @@ bool PeerManagerImpl::ProcessMessages(CNode* pfrom, std::atomic<bool>& interrupt
50565056
CaptureMessage(pfrom->addr, msg.m_type, MakeUCharSpan(msg.m_recv), /*is_incoming=*/true);
50575057
}
50585058

5059-
msg.SetVersion(pfrom->GetCommonVersion());
5060-
50615059
try {
50625060
ProcessMessage(*pfrom, msg.m_type, msg.m_recv, msg.m_time, interruptMsgProc);
50635061
if (interruptMsgProc) return false;

src/net_processing.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ class PeerManager : public CValidationInterface, public NetEventsInterface
105105
virtual void CheckForStaleTipAndEvictPeers() = 0;
106106

107107
/** Process a single message from a peer. Public for fuzz testing */
108-
virtual void ProcessMessage(CNode& pfrom, const std::string& msg_type, CDataStream& vRecv,
108+
virtual void ProcessMessage(CNode& pfrom, const std::string& msg_type, DataStream& vRecv,
109109
const std::chrono::microseconds time_received, const std::atomic<bool>& interruptMsgProc) EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex) = 0;
110110

111111
/** This function is used for testing the stale tip eviction logic, see denialofservice_tests.cpp */

src/test/fuzz/p2p_transport_serialization.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ void initialize_p2p_transport_serialization()
3636
FUZZ_TARGET(p2p_transport_serialization, .init = initialize_p2p_transport_serialization)
3737
{
3838
// Construct transports for both sides, with dummy NodeIds.
39-
V1Transport recv_transport{NodeId{0}, SER_NETWORK, INIT_PROTO_VERSION};
40-
V1Transport send_transport{NodeId{1}, SER_NETWORK, INIT_PROTO_VERSION};
39+
V1Transport recv_transport{NodeId{0}};
40+
V1Transport send_transport{NodeId{1}};
4141

4242
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
4343

@@ -335,7 +335,7 @@ void SimulationTest(Transport& initiator, Transport& responder, R& rng, FuzzedDa
335335

336336
std::unique_ptr<Transport> MakeV1Transport(NodeId nodeid) noexcept
337337
{
338-
return std::make_unique<V1Transport>(nodeid, SER_NETWORK, INIT_PROTO_VERSION);
338+
return std::make_unique<V1Transport>(nodeid);
339339
}
340340

341341
template<typename RNG>
@@ -369,7 +369,7 @@ std::unique_ptr<Transport> MakeV2Transport(NodeId nodeid, bool initiator, RNG& r
369369
.Write(garb.data(), garb.size())
370370
.Finalize(UCharCast(ent.data()));
371371

372-
return std::make_unique<V2Transport>(nodeid, initiator, SER_NETWORK, INIT_PROTO_VERSION, key, ent, std::move(garb));
372+
return std::make_unique<V2Transport>(nodeid, initiator, key, ent, std::move(garb));
373373
}
374374

375375
} // namespace

src/test/net_tests.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -1046,10 +1046,10 @@ class V2TransportTester
10461046

10471047
public:
10481048
/** Construct a tester object. test_initiator: whether the tested transport is initiator. */
1049-
V2TransportTester(bool test_initiator) :
1050-
m_transport(0, test_initiator, SER_NETWORK, INIT_PROTO_VERSION),
1051-
m_cipher{GenerateRandomTestKey(), MakeByteSpan(InsecureRand256())},
1052-
m_test_initiator(test_initiator) {}
1049+
explicit V2TransportTester(bool test_initiator)
1050+
: m_transport{0, test_initiator},
1051+
m_cipher{GenerateRandomTestKey(), MakeByteSpan(InsecureRand256())},
1052+
m_test_initiator(test_initiator) {}
10531053

10541054
/** Data type returned by Interact:
10551055
*

0 commit comments

Comments
 (0)