Skip to content

Commit c252a0f

Browse files
committed
Merge bitcoin#28892: refactor: P2P transport without serialize version and type
fa79a88 refactor: P2P transport without serialize version and type (MarcoFalke) fa9b5f4 refactor: NetMsg::Make() without nVersion (MarcoFalke) 66669da Remove unused Make() overload in netmessagemaker.h (MarcoFalke) fa0ed07 refactor: VectorWriter without nVersion (MarcoFalke) Pull request description: Now that the serialize framework ignores the serialize version and serialize type, everything related to it can be removed from the code. This is the first step, removing dead code from the P2P stack. A different pull will remove it from the wallet and other parts. ACKs for top commit: ajtowns: reACK fa79a88 Tree-SHA512: 785b413580d980f51f0d4f70ea5e0a99ce14cd12cb065393de2f5254891be94a14f4266110c8b87bd2dbc37467676655bce13bdb295ab139749fcd8b61bd5110
2 parents dc369af + fa79a88 commit c252a0f

15 files changed

+145
-197
lines changed

src/blockfilter.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,15 @@ GCSFilter::GCSFilter(const Params& params, const ElementSet& elements)
8181
}
8282
m_F = static_cast<uint64_t>(m_N) * static_cast<uint64_t>(m_params.m_M);
8383

84-
CVectorWriter stream(GCS_SER_VERSION, m_encoded, 0);
84+
VectorWriter stream{m_encoded, 0};
8585

8686
WriteCompactSize(stream, m_N);
8787

8888
if (elements.empty()) {
8989
return;
9090
}
9191

92-
BitStreamWriter<CVectorWriter> bitwriter(stream);
92+
BitStreamWriter bitwriter{stream};
9393

9494
uint64_t last_value = 0;
9595
for (uint64_t value : BuildHashedSet(elements)) {

src/net.cpp

+15-16
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();
@@ -818,7 +818,7 @@ bool V1Transport::SetMessageToSend(CSerializedNetMsg& msg) noexcept
818818

819819
// serialize header
820820
m_header_to_send.clear();
821-
CVectorWriter{INIT_PROTO_VERSION, m_header_to_send, 0, hdr};
821+
VectorWriter{m_header_to_send, 0, hdr};
822822

823823
// update state
824824
m_message_to_send = std::move(msg);
@@ -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) {
@@ -3660,9 +3659,9 @@ ServiceFlags CConnman::GetLocalServices() const
36603659
static std::unique_ptr<Transport> MakeTransport(NodeId id, bool use_v2transport, bool inbound) noexcept
36613660
{
36623661
if (use_v2transport) {
3663-
return std::make_unique<V2Transport>(id, /*initiating=*/!inbound, SER_NETWORK, INIT_PROTO_VERSION);
3662+
return std::make_unique<V2Transport>(id, /*initiating=*/!inbound);
36643663
} else {
3665-
return std::make_unique<V1Transport>(id, SER_NETWORK, INIT_PROTO_VERSION);
3664+
return std::make_unique<V1Transport>(id);
36663665
}
36673666
}
36683667

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);

0 commit comments

Comments
 (0)