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 <cory-nospam-@coryfields.com>
This commit is contained in:
TheCharlatan 2023-09-06 15:55:14 +02:00
parent 9be330b654
commit 534b314a74
No known key found for this signature in database
GPG key ID: 9B79B45691DB4173
12 changed files with 31 additions and 13 deletions

View file

@ -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 \

View file

@ -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()) {

View file

@ -10,6 +10,7 @@
#include <consensus/merkle.h>
#include <consensus/params.h>
#include <hash.h>
#include <kernel/messagestartchars.h>
#include <logging.h>
#include <primitives/block.h>
#include <primitives/transaction.h>

View file

@ -7,9 +7,9 @@
#define BITCOIN_KERNEL_CHAINPARAMS_H
#include <consensus/params.h>
#include <kernel/messagestartchars.h>
#include <netaddress.h>
#include <primitives/block.h>
#include <protocol.h>
#include <uint256.h>
#include <util/chaintype.h>
#include <util/hash_type.h>
@ -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;

View file

@ -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 <array>
#include <cstdint>
using MessageStartChars = std::array<uint8_t, 4>;
#endif // BITCOIN_KERNEL_MESSAGESTARTCHARS_H

View file

@ -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<uint8_t, 12> MATCH = {'v', 'e', 'r', 's', 'i', 'o', 'n', 0, 0, 0, 0, 0};
static constexpr size_t OFFSET = std::tuple_size_v<CMessageHeader::MessageStartChars>;
static constexpr size_t OFFSET = std::tuple_size_v<MessageStartChars>;
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",

View file

@ -10,15 +10,16 @@
#include <chainparams.h>
#include <common/bloom.h>
#include <compat/compat.h>
#include <node/connection_types.h>
#include <consensus/amount.h>
#include <crypto/siphash.h>
#include <hash.h>
#include <i2p.h>
#include <kernel/messagestartchars.h>
#include <net_permissions.h>
#include <netaddress.h>
#include <netbase.h>
#include <netgroup.h>
#include <node/connection_types.h>
#include <policy/feerate.h>
#include <protocol.h>
#include <random.h>
@ -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);

View file

@ -11,6 +11,7 @@
#include <flatfile.h>
#include <hash.h>
#include <kernel/chainparams.h>
#include <kernel/messagestartchars.h>
#include <logging.h>
#include <pow.h>
#include <reverse_iterator.h>
@ -927,7 +928,7 @@ bool BlockManager::ReadRawBlockFromDisk(std::vector<uint8_t>& block, const FlatF
}
try {
CMessageHeader::MessageStartChars blk_start;
MessageStartChars blk_start;
unsigned int blk_size;
filein >> blk_start >> blk_size;

View file

@ -11,7 +11,7 @@
#include <kernel/blockmanager_opts.h>
#include <kernel/chainparams.h>
#include <kernel/cs_main.h>
#include <protocol.h>
#include <kernel/messagestartchars.h>
#include <sync.h>
#include <util/fs.h>
#include <util/hasher.h>
@ -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<CMessageHeader::MessageStartChars> + sizeof(unsigned int);
static constexpr size_t BLOCK_SERIALIZATION_HEADER_SIZE = std::tuple_size_v<MessageStartChars> + sizeof(unsigned int);
extern std::atomic_bool fReindex;

View file

@ -6,6 +6,7 @@
#ifndef BITCOIN_PROTOCOL_H
#define BITCOIN_PROTOCOL_H
#include <kernel/messagestartchars.h> // IWYU pragma: export
#include <netaddress.h>
#include <primitives/transaction.h>
#include <serialize.h>
@ -27,7 +28,6 @@
class CMessageHeader
{
public:
using MessageStartChars = std::array<uint8_t, 4>;
static constexpr size_t COMMAND_SIZE = 12;
static constexpr size_t MESSAGE_SIZE_SIZE = 4;
static constexpr size_t CHECKSUM_SIZE = 4;

View file

@ -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);

View file

@ -23,6 +23,7 @@
#include <hash.h>
#include <kernel/chainparams.h>
#include <kernel/mempool_entry.h>
#include <kernel/messagestartchars.h>
#include <kernel/notifications_interface.h>
#include <logging.h>
#include <logging/timer.h>
@ -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;