diff options
author | Pieter Wuille <pieter@wuille.net> | 2023-07-27 15:35:41 -0400 |
---|---|---|
committer | Pieter Wuille <pieter@wuille.net> | 2023-08-23 19:56:24 -0400 |
commit | fb2c5edb79656a0b3b04ded6419928102ad696d6 (patch) | |
tree | 6551e671a86f9dbd7bcb058ba8e24fd4770d03d8 /src | |
parent | 0de48fe858a1ffcced340eef2c849165216141c8 (diff) | |
download | bitcoin-fb2c5edb79656a0b3b04ded6419928102ad696d6.tar.xz |
net: make V1Transport implicitly use current chainparams
The rest of net.cpp already uses Params() to determine chainparams in many
places (and even V1Transport itself does so in some places).
Since the only chainparams dependency is through the message start characters,
just store those directly in the transport.
Diffstat (limited to 'src')
-rw-r--r-- | src/net.cpp | 15 | ||||
-rw-r--r-- | src/net.h | 12 | ||||
-rw-r--r-- | src/test/fuzz/p2p_transport_serialization.cpp | 4 |
3 files changed, 16 insertions, 15 deletions
diff --git a/src/net.cpp b/src/net.cpp index 1545e36e68..3a70f3690b 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -717,6 +717,15 @@ bool CNode::ReceiveMsgBytes(Span<const uint8_t> msg_bytes, bool& complete) return true; } +V1Transport::V1Transport(const NodeId node_id, int nTypeIn, int nVersionIn) noexcept : + m_node_id(node_id), hdrbuf(nTypeIn, nVersionIn), vRecv(nTypeIn, nVersionIn) +{ + assert(std::size(Params().MessageStart()) == std::size(m_magic_bytes)); + std::copy(std::begin(Params().MessageStart()), std::end(Params().MessageStart()), m_magic_bytes); + LOCK(m_recv_mutex); + Reset(); +} + int V1Transport::readHeader(Span<const uint8_t> msg_bytes) { AssertLockHeld(m_recv_mutex); @@ -741,7 +750,7 @@ int V1Transport::readHeader(Span<const uint8_t> msg_bytes) } // Check start string, network magic - if (memcmp(hdr.pchMessageStart, m_chain_params.MessageStart(), CMessageHeader::MESSAGE_START_SIZE) != 0) { + if (memcmp(hdr.pchMessageStart, m_magic_bytes, CMessageHeader::MESSAGE_START_SIZE) != 0) { LogPrint(BCLog::NET, "Header error: Wrong MessageStart %s received, peer=%d\n", HexStr(hdr.pchMessageStart), m_node_id); return -1; } @@ -835,7 +844,7 @@ bool V1Transport::SetMessageToSend(CSerializedNetMsg& msg) noexcept uint256 hash = Hash(msg.data); // create header - CMessageHeader hdr(Params().MessageStart(), msg.m_type.c_str(), msg.data.size()); + CMessageHeader hdr(m_magic_bytes, msg.m_type.c_str(), msg.data.size()); memcpy(hdr.pchChecksum, hash.begin(), CMessageHeader::CHECKSUM_SIZE); // serialize header @@ -2874,7 +2883,7 @@ CNode::CNode(NodeId idIn, ConnectionType conn_type_in, bool inbound_onion, CNodeOptions&& node_opts) - : m_transport{std::make_unique<V1Transport>(Params(), idIn, SER_NETWORK, INIT_PROTO_VERSION)}, + : m_transport{std::make_unique<V1Transport>(idIn, SER_NETWORK, INIT_PROTO_VERSION)}, m_permission_flags{node_opts.permission_flags}, m_sock{sock}, m_connected{GetTime<std::chrono::seconds>()}, @@ -318,7 +318,7 @@ public: class V1Transport final : public Transport { private: - const CChainParams& m_chain_params; + CMessageHeader::MessageStartChars m_magic_bytes; const NodeId m_node_id; // Only for logging mutable Mutex m_recv_mutex; //!< Lock for receive state mutable CHash256 hasher GUARDED_BY(m_recv_mutex); @@ -365,15 +365,7 @@ private: size_t m_bytes_sent GUARDED_BY(m_send_mutex) {0}; public: - V1Transport(const CChainParams& chain_params, const NodeId node_id, int nTypeIn, int nVersionIn) - : m_chain_params(chain_params), - m_node_id(node_id), - hdrbuf(nTypeIn, nVersionIn), - vRecv(nTypeIn, nVersionIn) - { - LOCK(m_recv_mutex); - Reset(); - } + V1Transport(const NodeId node_id, int nTypeIn, int nVersionIn) noexcept; bool ReceivedMessageComplete() const 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 d96215e8e0..25e370bbf9 100644 --- a/src/test/fuzz/p2p_transport_serialization.cpp +++ b/src/test/fuzz/p2p_transport_serialization.cpp @@ -25,8 +25,8 @@ void initialize_p2p_transport_serialization() FUZZ_TARGET(p2p_transport_serialization, .init = initialize_p2p_transport_serialization) { // Construct transports for both sides, with dummy NodeIds. - V1Transport recv_transport{Params(), NodeId{0}, SER_NETWORK, INIT_PROTO_VERSION}; - V1Transport send_transport{Params(), NodeId{1}, SER_NETWORK, INIT_PROTO_VERSION}; + V1Transport recv_transport{NodeId{0}, SER_NETWORK, INIT_PROTO_VERSION}; + V1Transport send_transport{NodeId{1}, SER_NETWORK, INIT_PROTO_VERSION}; FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()}; |