diff options
author | Pieter Wuille <pieter@wuille.net> | 2023-07-05 16:22:52 -0400 |
---|---|---|
committer | Pieter Wuille <pieter@wuille.net> | 2023-08-23 19:56:24 -0400 |
commit | 93594e42c3f92d82427d2b284ff0f94cdbebe99c (patch) | |
tree | 2f7d4f12f650accebc2a82ae06a5f99ab081980b /src/net.h | |
parent | 23f3f402fca346302fe424427ae4077d8a458cbb (diff) |
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.
Diffstat (limited to 'src/net.h')
-rw-r--r-- | src/net.h | 41 |
1 files changed, 18 insertions, 23 deletions
@@ -253,24 +253,31 @@ public: } }; -/** The TransportDeserializer takes care of holding and deserializing the - * network receive buffer. It can deserialize the network buffer into a - * transport protocol agnostic CNetMessage (message type & payload) - */ -class TransportDeserializer { +/** The Transport converts one connection's sent messages to wire bytes, and received bytes back. */ +class Transport { public: + virtual ~Transport() {} + + // 1. Receiver side functions, for decoding bytes received on the wire into transport protocol + // agnostic CNetMessage (message type & payload) objects. Callers must guarantee that none of + // these functions are called concurrently w.r.t. one another. + // returns true if the current deserialization is complete virtual bool Complete() const = 0; - // set the serialization context version + // set the deserialization context version virtual void SetVersion(int version) = 0; /** read and deserialize data, advances msg_bytes data pointer */ virtual int Read(Span<const uint8_t>& msg_bytes) = 0; // decomposes a message from the context virtual CNetMessage GetMessage(std::chrono::microseconds time, bool& reject_message) = 0; - virtual ~TransportDeserializer() {} + + // 2. Sending side functions: + + // prepare message for transport (header construction, error-correction computation, payload encryption, etc.) + virtual void prepareForTransport(CSerializedNetMsg& msg, std::vector<unsigned char>& header) const = 0; }; -class V1TransportDeserializer final : public TransportDeserializer +class V1Transport final : public Transport { private: const CChainParams& m_chain_params; @@ -300,7 +307,7 @@ private: } public: - V1TransportDeserializer(const CChainParams& chain_params, const NodeId node_id, int nTypeIn, int nVersionIn) + 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), @@ -331,19 +338,7 @@ public: return ret; } CNetMessage GetMessage(std::chrono::microseconds time, bool& reject_message) 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(CSerializedNetMsg& msg, std::vector<unsigned char>& header) const = 0; - virtual ~TransportSerializer() {} -}; - -class V1TransportSerializer : public TransportSerializer { -public: void prepareForTransport(CSerializedNetMsg& msg, std::vector<unsigned char>& header) const override; }; @@ -359,8 +354,8 @@ struct CNodeOptions class CNode { public: - const std::unique_ptr<TransportDeserializer> m_deserializer; // Used only by SocketHandler thread - const std::unique_ptr<const TransportSerializer> m_serializer; + /** Transport serializer/deserializer. The receive side functions are only called under cs_vRecv. */ + const std::unique_ptr<Transport> m_transport; const NetPermissionFlags m_permission_flags; |