0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-03 09:56:38 -05:00

Merge bitcoin/bitcoin#28857: test, refactor: Magic bytes array followup

1e5b86171e test: Add test for array serialization (TheCharlatan)
d49d198840 refactor: Initialize magic bytes in constructor initializer (TheCharlatan)

Pull request description:

  This is a followup-PR for #28423

  * Initialize magic bytes in constructor
  * Add a small unit test for serializing arrays.

ACKs for top commit:
  sipa:
    utACK 1e5b86171e
  maflcko:
    lgtm ACK 1e5b86171e

Tree-SHA512: 0f58d2332dc501ca9fd419f40ed4f977c83dce0169e9a0eee1ffc9f8daa2d2ef7e7df18205ba076f55d90ae6c4a20d2b51ab303150d38470a962bcc58a66f6e7
This commit is contained in:
fanquake 2023-11-14 15:38:29 +00:00
commit 8992a34ee4
No known key found for this signature in database
GPG key ID: 2EEB9F5CC09526C1
4 changed files with 15 additions and 6 deletions

View file

@ -684,10 +684,8 @@ bool CNode::ReceiveMsgBytes(Span<const uint8_t> msg_bytes, bool& complete)
} }
V1Transport::V1Transport(const NodeId node_id, int nTypeIn, int nVersionIn) noexcept : 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); LOCK(m_recv_mutex);
Reset(); Reset();
} }

View file

@ -372,7 +372,7 @@ public:
class V1Transport final : public Transport class V1Transport final : public Transport
{ {
private: private:
MessageStartChars m_magic_bytes; const MessageStartChars m_magic_bytes;
const NodeId m_node_id; // Only for logging const NodeId m_node_id; // Only for logging
mutable Mutex m_recv_mutex; //!< Lock for receive state mutable Mutex m_recv_mutex; //!< Lock for receive state
mutable CHash256 hasher GUARDED_BY(m_recv_mutex); mutable CHash256 hasher GUARDED_BY(m_recv_mutex);

View file

@ -91,9 +91,8 @@ const static std::vector<std::string> g_all_net_message_types{
}; };
CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* pszCommand, unsigned int nMessageSizeIn) CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* pszCommand, unsigned int nMessageSizeIn)
: pchMessageStart{pchMessageStartIn}
{ {
pchMessageStart = pchMessageStartIn;
// Copy the command name // Copy the command name
size_t i = 0; size_t i = 0;
for (; i < COMMAND_SIZE && pszCommand[i] != 0; ++i) pchCommand[i] = pszCommand[i]; for (; i < COMMAND_SIZE && pszCommand[i] != 0; ++i) pchCommand[i] = pszCommand[i];

View file

@ -85,6 +85,8 @@ BOOST_AUTO_TEST_CASE(sizes)
BOOST_CHECK_EQUAL(GetSerializeSize(int64_t(0), 0), 8U); BOOST_CHECK_EQUAL(GetSerializeSize(int64_t(0), 0), 8U);
BOOST_CHECK_EQUAL(GetSerializeSize(uint64_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(bool(0), 0), 1U);
BOOST_CHECK_EQUAL(GetSerializeSize(std::array<uint8_t, 1>{0}, 0), 1U);
BOOST_CHECK_EQUAL(GetSerializeSize(std::array<uint8_t, 2>{0, 0}, 0), 2U);
} }
BOOST_AUTO_TEST_CASE(varints) BOOST_AUTO_TEST_CASE(varints)
@ -179,6 +181,16 @@ BOOST_AUTO_TEST_CASE(vector_bool)
BOOST_CHECK((HashWriter{} << vec1).GetHash() == (HashWriter{} << vec2).GetHash()); BOOST_CHECK((HashWriter{} << vec1).GetHash() == (HashWriter{} << vec2).GetHash());
} }
BOOST_AUTO_TEST_CASE(array)
{
std::array<uint8_t, 32> 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<uint8_t, 32> array2;
ds >> array2;
BOOST_CHECK(array1 == array2);
}
BOOST_AUTO_TEST_CASE(noncanonical) BOOST_AUTO_TEST_CASE(noncanonical)
{ {
// Write some non-canonical CompactSize encodings, and // Write some non-canonical CompactSize encodings, and