Skip to content

Commit

Permalink
Refactor message transport packaging
Browse files Browse the repository at this point in the history
Summary:
```
This PR factors out transport packaging logic from
CConnman::PushMessage().
It's similar to #16202 (where we refactor deserialization).

This allows implementing a new message transport protocol like BIP324.
```

Backport of core [[bitcoin/bitcoin#16562 | PR16562]].

Test Plan:
  ninja all check-all

Reviewers: #bitcoin_abc, PiRK, jasonbcox

Reviewed By: #bitcoin_abc, PiRK, jasonbcox

Differential Revision: https://reviews.bitcoinabc.org/D8374
  • Loading branch information
jonasschnelli authored and Fabcien committed Nov 13, 2020
1 parent a0384bd commit 06ce662
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
29 changes: 21 additions & 8 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,22 @@ CNetMessage V1TransportDeserializer::GetMessage(const Config &config,
return msg;
}

void V1TransportSerializer::prepareForTransport(const Config &config,
CSerializedNetMsg &msg,
std::vector<uint8_t> &header) {
// create dbl-sha256 checksum
uint256 hash = Hash(msg.data.begin(), msg.data.end());

// create header
CMessageHeader hdr(config.GetChainParams().NetMagic(), msg.command.c_str(),
msg.data.size());
memcpy(hdr.pchChecksum, hash.begin(), CMessageHeader::CHECKSUM_SIZE);

// serialize header
header.reserve(CMessageHeader::HEADER_SIZE);
CVectorWriter{SER_NETWORK, INIT_PROTO_VERSION, header, 0, hdr};
}

size_t CConnman::SocketSendData(CNode *pnode) const
EXCLUSIVE_LOCKS_REQUIRED(pnode->cs_vSend) {
size_t nSentSize = 0;
Expand Down Expand Up @@ -2932,6 +2948,8 @@ CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn,
m_deserializer = std::make_unique<V1TransportDeserializer>(
V1TransportDeserializer(GetConfig().GetChainParams().NetMagic(),
SER_NETWORK, INIT_PROTO_VERSION));
m_serializer =
std::make_unique<V1TransportSerializer>(V1TransportSerializer());
}

CNode::~CNode() {
Expand All @@ -2944,18 +2962,13 @@ bool CConnman::NodeFullyConnected(const CNode *pnode) {

void CConnman::PushMessage(CNode *pnode, CSerializedNetMsg &&msg) {
size_t nMessageSize = msg.data.size();
size_t nTotalSize = nMessageSize + CMessageHeader::HEADER_SIZE;
LogPrint(BCLog::NET, "sending %s (%d bytes) peer=%d\n",
SanitizeString(msg.command), nMessageSize, pnode->GetId());

// make sure we use the appropriate network transport format
std::vector<uint8_t> serializedHeader;
serializedHeader.reserve(CMessageHeader::HEADER_SIZE);
uint256 hash = Hash(msg.data.data(), msg.data.data() + nMessageSize);
CMessageHeader hdr(config->GetChainParams().NetMagic(), msg.command.c_str(),
nMessageSize);
memcpy(hdr.pchChecksum, hash.begin(), CMessageHeader::CHECKSUM_SIZE);

CVectorWriter{SER_NETWORK, INIT_PROTO_VERSION, serializedHeader, 0, hdr};
pnode->m_serializer->prepareForTransport(*config, msg, serializedHeader);
size_t nTotalSize = nMessageSize + serializedHeader.size();

size_t nBytesSent = 0;
{
Expand Down
20 changes: 20 additions & 0 deletions src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -756,12 +756,32 @@ class V1TransportDeserializer final : public TransportDeserializer {
CNetMessage GetMessage(const Config &config, int64_t time) override;
};

/**
* The TransportSerializer prepares messages for the network transport
*/
class TransportSerializer {
public:
// prepare message for transport (header construction, error-correction
// computation, payload encryption, etc.)
virtual void prepareForTransport(const Config &config,
CSerializedNetMsg &msg,
std::vector<uint8_t> &header) = 0;
virtual ~TransportSerializer() {}
};

class V1TransportSerializer : public TransportSerializer {
public:
void prepareForTransport(const Config &config, CSerializedNetMsg &msg,
std::vector<uint8_t> &header) override;
};

/** Information about a peer */
class CNode {
friend class CConnman;

public:
std::unique_ptr<TransportDeserializer> m_deserializer;
std::unique_ptr<TransportSerializer> m_serializer;

// socket
std::atomic<ServiceFlags> nServices{NODE_NONE};
Expand Down

0 comments on commit 06ce662

Please sign in to comment.