mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-12 23:18:14 +01:00
refactor: merge transport serializer and deserializer into Transport class
This allows state that is shared between both directions to be encapsulated into a single object. Specifically the v2 transport protocol introduced by BIP324 has sending state (the encryption keys) that depends on received messages (the DH key exchange). Having a single object for both means it can hide logic from callers related to that key exchange and other interactions.
This commit is contained in:
21
src/net.cpp
21
src/net.cpp
@@ -681,16 +681,16 @@ bool CNode::ReceiveMsgBytes(Span<const uint8_t> msg_bytes, bool& complete)
|
||||
nRecvBytes += msg_bytes.size();
|
||||
while (msg_bytes.size() > 0) {
|
||||
// absorb network data
|
||||
int handled = m_deserializer->Read(msg_bytes);
|
||||
int handled = m_transport->Read(msg_bytes);
|
||||
if (handled < 0) {
|
||||
// Serious header problem, disconnect from the peer.
|
||||
return false;
|
||||
}
|
||||
|
||||
if (m_deserializer->Complete()) {
|
||||
if (m_transport->Complete()) {
|
||||
// decompose a transport agnostic CNetMessage from the deserializer
|
||||
bool reject_message{false};
|
||||
CNetMessage msg = m_deserializer->GetMessage(time, reject_message);
|
||||
CNetMessage msg = m_transport->GetMessage(time, reject_message);
|
||||
if (reject_message) {
|
||||
// Message deserialization failed. Drop the message but don't disconnect the peer.
|
||||
// store the size of the corrupt message
|
||||
@@ -717,7 +717,7 @@ bool CNode::ReceiveMsgBytes(Span<const uint8_t> msg_bytes, bool& complete)
|
||||
return true;
|
||||
}
|
||||
|
||||
int V1TransportDeserializer::readHeader(Span<const uint8_t> msg_bytes)
|
||||
int V1Transport::readHeader(Span<const uint8_t> msg_bytes)
|
||||
{
|
||||
// copy data to temporary parsing buffer
|
||||
unsigned int nRemaining = CMessageHeader::HEADER_SIZE - nHdrPos;
|
||||
@@ -757,7 +757,7 @@ int V1TransportDeserializer::readHeader(Span<const uint8_t> msg_bytes)
|
||||
return nCopy;
|
||||
}
|
||||
|
||||
int V1TransportDeserializer::readData(Span<const uint8_t> msg_bytes)
|
||||
int V1Transport::readData(Span<const uint8_t> msg_bytes)
|
||||
{
|
||||
unsigned int nRemaining = hdr.nMessageSize - nDataPos;
|
||||
unsigned int nCopy = std::min<unsigned int>(nRemaining, msg_bytes.size());
|
||||
@@ -774,7 +774,7 @@ int V1TransportDeserializer::readData(Span<const uint8_t> msg_bytes)
|
||||
return nCopy;
|
||||
}
|
||||
|
||||
const uint256& V1TransportDeserializer::GetMessageHash() const
|
||||
const uint256& V1Transport::GetMessageHash() const
|
||||
{
|
||||
assert(Complete());
|
||||
if (data_hash.IsNull())
|
||||
@@ -782,7 +782,7 @@ const uint256& V1TransportDeserializer::GetMessageHash() const
|
||||
return data_hash;
|
||||
}
|
||||
|
||||
CNetMessage V1TransportDeserializer::GetMessage(const std::chrono::microseconds time, bool& reject_message)
|
||||
CNetMessage V1Transport::GetMessage(const std::chrono::microseconds time, bool& reject_message)
|
||||
{
|
||||
// Initialize out parameter
|
||||
reject_message = false;
|
||||
@@ -819,7 +819,7 @@ CNetMessage V1TransportDeserializer::GetMessage(const std::chrono::microseconds
|
||||
return msg;
|
||||
}
|
||||
|
||||
void V1TransportSerializer::prepareForTransport(CSerializedNetMsg& msg, std::vector<unsigned char>& header) const
|
||||
void V1Transport::prepareForTransport(CSerializedNetMsg& msg, std::vector<unsigned char>& header) const
|
||||
{
|
||||
// create dbl-sha256 checksum
|
||||
uint256 hash = Hash(msg.data);
|
||||
@@ -2822,8 +2822,7 @@ CNode::CNode(NodeId idIn,
|
||||
ConnectionType conn_type_in,
|
||||
bool inbound_onion,
|
||||
CNodeOptions&& node_opts)
|
||||
: m_deserializer{std::make_unique<V1TransportDeserializer>(V1TransportDeserializer(Params(), idIn, SER_NETWORK, INIT_PROTO_VERSION))},
|
||||
m_serializer{std::make_unique<V1TransportSerializer>(V1TransportSerializer())},
|
||||
: m_transport{std::make_unique<V1Transport>(Params(), idIn, SER_NETWORK, INIT_PROTO_VERSION)},
|
||||
m_permission_flags{node_opts.permission_flags},
|
||||
m_sock{sock},
|
||||
m_connected{GetTime<std::chrono::seconds>()},
|
||||
@@ -2908,7 +2907,7 @@ void CConnman::PushMessage(CNode* pnode, CSerializedNetMsg&& msg)
|
||||
|
||||
// make sure we use the appropriate network transport format
|
||||
std::vector<unsigned char> serializedHeader;
|
||||
pnode->m_serializer->prepareForTransport(msg, serializedHeader);
|
||||
pnode->m_transport->prepareForTransport(msg, serializedHeader);
|
||||
size_t nTotalSize = nMessageSize + serializedHeader.size();
|
||||
|
||||
size_t nBytesSent = 0;
|
||||
|
||||
Reference in New Issue
Block a user