From d49d1988406f2f0d350bc5b552625f9823090130 Mon Sep 17 00:00:00 2001 From: TheCharlatan Date: Sat, 11 Nov 2023 13:04:18 +0100 Subject: [PATCH 1/2] refactor: Initialize magic bytes in constructor initializer Also remove an assert that is already enforced by the compiler checking that the length of the std::array matches. --- src/net.cpp | 4 +--- src/net.h | 2 +- src/protocol.cpp | 3 +-- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/net.cpp b/src/net.cpp index 4aee78846a..a2f80cbcf7 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -684,10 +684,8 @@ bool CNode::ReceiveMsgBytes(Span msg_bytes, bool& complete) } V1Transport::V1Transport(const NodeId node_id, int nTypeIn, int nVersionIn) noexcept : - m_node_id(node_id), hdrbuf(nTypeIn, nVersionIn), vRecv(nTypeIn, nVersionIn) + m_magic_bytes{Params().MessageStart()}, m_node_id(node_id), hdrbuf(nTypeIn, nVersionIn), vRecv(nTypeIn, nVersionIn) { - assert(std::size(Params().MessageStart()) == std::size(m_magic_bytes)); - m_magic_bytes = Params().MessageStart(); LOCK(m_recv_mutex); Reset(); } diff --git a/src/net.h b/src/net.h index dde65fd5f0..16312cf72d 100644 --- a/src/net.h +++ b/src/net.h @@ -372,7 +372,7 @@ public: class V1Transport final : public Transport { private: - MessageStartChars m_magic_bytes; + const 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); diff --git a/src/protocol.cpp b/src/protocol.cpp index f956728af2..27a0a2ffc1 100644 --- a/src/protocol.cpp +++ b/src/protocol.cpp @@ -91,9 +91,8 @@ const static std::vector g_all_net_message_types{ }; CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* pszCommand, unsigned int nMessageSizeIn) + : pchMessageStart{pchMessageStartIn} { - pchMessageStart = pchMessageStartIn; - // Copy the command name size_t i = 0; for (; i < COMMAND_SIZE && pszCommand[i] != 0; ++i) pchCommand[i] = pszCommand[i]; From 1e5b86171e81ab4b022b9746bb06e1968ecf4086 Mon Sep 17 00:00:00 2001 From: TheCharlatan Date: Sat, 11 Nov 2023 13:21:01 +0100 Subject: [PATCH 2/2] test: Add test for array serialization --- src/test/serialize_tests.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/test/serialize_tests.cpp b/src/test/serialize_tests.cpp index 49827be559..ec4ffbfb7c 100644 --- a/src/test/serialize_tests.cpp +++ b/src/test/serialize_tests.cpp @@ -85,6 +85,8 @@ BOOST_AUTO_TEST_CASE(sizes) BOOST_CHECK_EQUAL(GetSerializeSize(int64_t(0), 0), 8U); BOOST_CHECK_EQUAL(GetSerializeSize(uint64_t(0), 0), 8U); BOOST_CHECK_EQUAL(GetSerializeSize(bool(0), 0), 1U); + BOOST_CHECK_EQUAL(GetSerializeSize(std::array{0}, 0), 1U); + BOOST_CHECK_EQUAL(GetSerializeSize(std::array{0, 0}, 0), 2U); } BOOST_AUTO_TEST_CASE(varints) @@ -179,6 +181,16 @@ BOOST_AUTO_TEST_CASE(vector_bool) BOOST_CHECK((HashWriter{} << vec1).GetHash() == (HashWriter{} << vec2).GetHash()); } +BOOST_AUTO_TEST_CASE(array) +{ + std::array array1{1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1}; + DataStream ds; + ds << array1; + std::array array2; + ds >> array2; + BOOST_CHECK(array1 == array2); +} + BOOST_AUTO_TEST_CASE(noncanonical) { // Write some non-canonical CompactSize encodings, and