Skip to content

Commit 8992a34

Browse files
committed
Merge bitcoin/bitcoin#28857: test, refactor: Magic bytes array followup
1e5b861 test: Add test for array serialization (TheCharlatan) d49d198 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 1e5b861 maflcko: lgtm ACK 1e5b861 Tree-SHA512: 0f58d2332dc501ca9fd419f40ed4f977c83dce0169e9a0eee1ffc9f8daa2d2ef7e7df18205ba076f55d90ae6c4a20d2b51ab303150d38470a962bcc58a66f6e7
2 parents fb85bb2 + 1e5b861 commit 8992a34

File tree

4 files changed

+15
-6
lines changed

4 files changed

+15
-6
lines changed

src/net.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -684,10 +684,8 @@ bool CNode::ReceiveMsgBytes(Span<const uint8_t> msg_bytes, bool& complete)
684684
}
685685

686686
V1Transport::V1Transport(const NodeId node_id, int nTypeIn, int nVersionIn) noexcept :
687-
m_node_id(node_id), hdrbuf(nTypeIn, nVersionIn), vRecv(nTypeIn, nVersionIn)
687+
m_magic_bytes{Params().MessageStart()}, m_node_id(node_id), hdrbuf(nTypeIn, nVersionIn), vRecv(nTypeIn, nVersionIn)
688688
{
689-
assert(std::size(Params().MessageStart()) == std::size(m_magic_bytes));
690-
m_magic_bytes = Params().MessageStart();
691689
LOCK(m_recv_mutex);
692690
Reset();
693691
}

src/net.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ class Transport {
372372
class V1Transport final : public Transport
373373
{
374374
private:
375-
MessageStartChars m_magic_bytes;
375+
const MessageStartChars m_magic_bytes;
376376
const NodeId m_node_id; // Only for logging
377377
mutable Mutex m_recv_mutex; //!< Lock for receive state
378378
mutable CHash256 hasher GUARDED_BY(m_recv_mutex);

src/protocol.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,8 @@ const static std::vector<std::string> g_all_net_message_types{
9191
};
9292

9393
CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* pszCommand, unsigned int nMessageSizeIn)
94+
: pchMessageStart{pchMessageStartIn}
9495
{
95-
pchMessageStart = pchMessageStartIn;
96-
9796
// Copy the command name
9897
size_t i = 0;
9998
for (; i < COMMAND_SIZE && pszCommand[i] != 0; ++i) pchCommand[i] = pszCommand[i];

src/test/serialize_tests.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ BOOST_AUTO_TEST_CASE(sizes)
8585
BOOST_CHECK_EQUAL(GetSerializeSize(int64_t(0), 0), 8U);
8686
BOOST_CHECK_EQUAL(GetSerializeSize(uint64_t(0), 0), 8U);
8787
BOOST_CHECK_EQUAL(GetSerializeSize(bool(0), 0), 1U);
88+
BOOST_CHECK_EQUAL(GetSerializeSize(std::array<uint8_t, 1>{0}, 0), 1U);
89+
BOOST_CHECK_EQUAL(GetSerializeSize(std::array<uint8_t, 2>{0, 0}, 0), 2U);
8890
}
8991

9092
BOOST_AUTO_TEST_CASE(varints)
@@ -179,6 +181,16 @@ BOOST_AUTO_TEST_CASE(vector_bool)
179181
BOOST_CHECK((HashWriter{} << vec1).GetHash() == (HashWriter{} << vec2).GetHash());
180182
}
181183

184+
BOOST_AUTO_TEST_CASE(array)
185+
{
186+
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};
187+
DataStream ds;
188+
ds << array1;
189+
std::array<uint8_t, 32> array2;
190+
ds >> array2;
191+
BOOST_CHECK(array1 == array2);
192+
}
193+
182194
BOOST_AUTO_TEST_CASE(noncanonical)
183195
{
184196
// Write some non-canonical CompactSize encodings, and

0 commit comments

Comments
 (0)