From bbec32c9ad2fe213314db9d39aa1eacff2e0bc23 Mon Sep 17 00:00:00 2001 From: Anthony Towns Date: Sat, 5 Mar 2022 04:09:35 +1000 Subject: [PATCH] net: mark TransportSerializer/m_serializer as const The (V1)TransportSerializer instance CNode::m_serializer is used from multiple threads via PushMessage without protection by a mutex. This is only thread safe because the class does not have any mutable state, so document that by marking the methods and the object as "const". --- src/net.cpp | 3 ++- src/net.h | 8 ++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/net.cpp b/src/net.cpp index c2e45898e80..cf0c9aef7ba 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -797,7 +797,8 @@ CNetMessage V1TransportDeserializer::GetMessage(const std::chrono::microseconds return msg; } -void V1TransportSerializer::prepareForTransport(CSerializedNetMsg& msg, std::vector& header) { +void V1TransportSerializer::prepareForTransport(CSerializedNetMsg& msg, std::vector& header) const +{ // create dbl-sha256 checksum uint256 hash = Hash(msg.data); diff --git a/src/net.h b/src/net.h index 064c448264c..97389314b2b 100644 --- a/src/net.h +++ b/src/net.h @@ -325,13 +325,13 @@ public: class TransportSerializer { public: // prepare message for transport (header construction, error-correction computation, payload encryption, etc.) - virtual void prepareForTransport(CSerializedNetMsg& msg, std::vector& header) = 0; + virtual void prepareForTransport(CSerializedNetMsg& msg, std::vector& header) const = 0; virtual ~TransportSerializer() {} }; -class V1TransportSerializer : public TransportSerializer { +class V1TransportSerializer : public TransportSerializer { public: - void prepareForTransport(CSerializedNetMsg& msg, std::vector& header) override; + void prepareForTransport(CSerializedNetMsg& msg, std::vector& header) const override; }; /** Information about a peer */ @@ -342,7 +342,7 @@ class CNode public: std::unique_ptr m_deserializer; - std::unique_ptr m_serializer; + std::unique_ptr m_serializer; NetPermissionFlags m_permissionFlags{NetPermissionFlags::None};