From 534b314a7401d44f51aabd4565f97be9ee411740 Mon Sep 17 00:00:00 2001 From: TheCharlatan Date: Wed, 6 Sep 2023 15:55:14 +0200 Subject: kernel: Move MessageStartChars to its own file The protocol.h file contains many non-consensus related definitions and should thus not be part of the libbitcoinkernel. This commit makes protocol.h no longer a required include for users of the libbitcoinkernel. This commit is part of the libbitcoinkernel project, namely its stage 1 step 3: Decouple most non-consensus headers from libbitcoinkernel. Co-Authored-By: Cory Fields --- src/Makefile.am | 1 + src/addrdb.cpp | 2 +- src/kernel/chainparams.cpp | 1 + src/kernel/chainparams.h | 6 +++--- src/kernel/messagestartchars.h | 13 +++++++++++++ src/net.cpp | 2 +- src/net.h | 5 +++-- src/node/blockstorage.cpp | 3 ++- src/node/blockstorage.h | 4 ++-- src/protocol.h | 2 +- src/test/net_tests.cpp | 2 +- src/validation.cpp | 3 ++- 12 files changed, 31 insertions(+), 13 deletions(-) create mode 100644 src/kernel/messagestartchars.h (limited to 'src') diff --git a/src/Makefile.am b/src/Makefile.am index feed4a0061..e542a067a4 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -191,6 +191,7 @@ BITCOIN_CORE_H = \ kernel/mempool_options.h \ kernel/mempool_persist.h \ kernel/mempool_removal_reason.h \ + kernel/messagestartchars.h \ kernel/notifications_interface.h \ kernel/validation_cache_sizes.h \ key.h \ diff --git a/src/addrdb.cpp b/src/addrdb.cpp index 6b21d3a1ed..50f576624c 100644 --- a/src/addrdb.cpp +++ b/src/addrdb.cpp @@ -90,7 +90,7 @@ void DeserializeDB(Stream& stream, Data&& data, bool fCheckSum = true) { HashVerifier verifier{stream}; // de-serialize file header (network specific magic number) and .. - CMessageHeader::MessageStartChars pchMsgTmp; + MessageStartChars pchMsgTmp; verifier >> pchMsgTmp; // ... verify the network matches ours if (pchMsgTmp != Params().MessageStart()) { diff --git a/src/kernel/chainparams.cpp b/src/kernel/chainparams.cpp index c41559ad98..6cee379faf 100644 --- a/src/kernel/chainparams.cpp +++ b/src/kernel/chainparams.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include diff --git a/src/kernel/chainparams.h b/src/kernel/chainparams.h index 2d38af609c..f1df878f60 100644 --- a/src/kernel/chainparams.h +++ b/src/kernel/chainparams.h @@ -7,9 +7,9 @@ #define BITCOIN_KERNEL_CHAINPARAMS_H #include +#include #include #include -#include #include #include #include @@ -87,7 +87,7 @@ public: }; const Consensus::Params& GetConsensus() const { return consensus; } - const CMessageHeader::MessageStartChars& MessageStart() const { return pchMessageStart; } + const MessageStartChars& MessageStart() const { return pchMessageStart; } uint16_t GetDefaultPort() const { return nDefaultPort; } uint16_t GetDefaultPort(Network net) const { @@ -165,7 +165,7 @@ protected: CChainParams() {} Consensus::Params consensus; - CMessageHeader::MessageStartChars pchMessageStart; + MessageStartChars pchMessageStart; uint16_t nDefaultPort; uint64_t nPruneAfterHeight; uint64_t m_assumed_blockchain_size; diff --git a/src/kernel/messagestartchars.h b/src/kernel/messagestartchars.h new file mode 100644 index 0000000000..829616dc8b --- /dev/null +++ b/src/kernel/messagestartchars.h @@ -0,0 +1,13 @@ +// Copyright (c) 2023 The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#ifndef BITCOIN_KERNEL_MESSAGESTARTCHARS_H +#define BITCOIN_KERNEL_MESSAGESTARTCHARS_H + +#include +#include + +using MessageStartChars = std::array; + +#endif // BITCOIN_KERNEL_MESSAGESTARTCHARS_H diff --git a/src/net.cpp b/src/net.cpp index d091db74c4..ddbf9a6e3c 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -1144,7 +1144,7 @@ bool V2Transport::ProcessReceivedKeyBytes() noexcept // they receive our uniformly random key and garbage, but detecting this case specially // means we can log it. static constexpr std::array MATCH = {'v', 'e', 'r', 's', 'i', 'o', 'n', 0, 0, 0, 0, 0}; - static constexpr size_t OFFSET = std::tuple_size_v; + static constexpr size_t OFFSET = std::tuple_size_v; if (!m_initiating && m_recv_buffer.size() >= OFFSET + MATCH.size()) { if (std::equal(MATCH.begin(), MATCH.end(), m_recv_buffer.begin() + OFFSET)) { LogPrint(BCLog::NET, "V2 transport error: V1 peer with wrong MessageStart %s\n", diff --git a/src/net.h b/src/net.h index e1d8995a8e..669ff68cb4 100644 --- a/src/net.h +++ b/src/net.h @@ -10,15 +10,16 @@ #include #include #include -#include #include #include #include #include +#include #include #include #include #include +#include #include #include #include @@ -360,7 +361,7 @@ public: class V1Transport final : public Transport { private: - CMessageHeader::MessageStartChars m_magic_bytes; + 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/node/blockstorage.cpp b/src/node/blockstorage.cpp index 2e4e077f18..253dee088e 100644 --- a/src/node/blockstorage.cpp +++ b/src/node/blockstorage.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -927,7 +928,7 @@ bool BlockManager::ReadRawBlockFromDisk(std::vector& block, const FlatF } try { - CMessageHeader::MessageStartChars blk_start; + MessageStartChars blk_start; unsigned int blk_size; filein >> blk_start >> blk_size; diff --git a/src/node/blockstorage.h b/src/node/blockstorage.h index 4b57637427..b251ece31f 100644 --- a/src/node/blockstorage.h +++ b/src/node/blockstorage.h @@ -11,7 +11,7 @@ #include #include #include -#include +#include #include #include #include @@ -73,7 +73,7 @@ static const unsigned int UNDOFILE_CHUNK_SIZE = 0x100000; // 1 MiB static const unsigned int MAX_BLOCKFILE_SIZE = 0x8000000; // 128 MiB /** Size of header written by WriteBlockToDisk before a serialized CBlock */ -static constexpr size_t BLOCK_SERIALIZATION_HEADER_SIZE = std::tuple_size_v + sizeof(unsigned int); +static constexpr size_t BLOCK_SERIALIZATION_HEADER_SIZE = std::tuple_size_v + sizeof(unsigned int); extern std::atomic_bool fReindex; diff --git a/src/protocol.h b/src/protocol.h index cb7f9666e1..22e2108afb 100644 --- a/src/protocol.h +++ b/src/protocol.h @@ -6,6 +6,7 @@ #ifndef BITCOIN_PROTOCOL_H #define BITCOIN_PROTOCOL_H +#include // IWYU pragma: export #include #include #include @@ -27,7 +28,6 @@ class CMessageHeader { public: - using MessageStartChars = std::array; static constexpr size_t COMMAND_SIZE = 12; static constexpr size_t MESSAGE_SIZE_SIZE = 4; static constexpr size_t CHECKSUM_SIZE = 4; diff --git a/src/test/net_tests.cpp b/src/test/net_tests.cpp index 3eb7bdec62..34d7867079 100644 --- a/src/test/net_tests.cpp +++ b/src/test/net_tests.cpp @@ -1114,7 +1114,7 @@ public: } /** Send V1 version message header to the transport. */ - void SendV1Version(const CMessageHeader::MessageStartChars& magic) + void SendV1Version(const MessageStartChars& magic) { CMessageHeader hdr(magic, "version", 126 + InsecureRandRange(11)); CDataStream ser(SER_NETWORK, CLIENT_VERSION); diff --git a/src/validation.cpp b/src/validation.cpp index 4bb648d1e5..8d4b8386e8 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -4533,7 +4534,7 @@ void ChainstateManager::LoadExternalBlockFile( unsigned int nSize = 0; try { // locate a header - CMessageHeader::MessageStartChars buf; + MessageStartChars buf; blkdat.FindByte(std::byte(params.MessageStart()[0])); nRewind = blkdat.GetPos() + 1; blkdat >> buf; -- cgit v1.2.3