Skip to content
Open
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,6 @@ add_library(bitcoin_node STATIC EXCLUDE_FROM_ALL
kernel/context.cpp
kernel/cs_main.cpp
kernel/disconnected_transactions.cpp
kernel/mempool_persist.cpp
kernel/mempool_removal_reason.cpp
instantsend/db.cpp
instantsend/instantsend.cpp
Expand Down Expand Up @@ -394,6 +393,7 @@ add_library(bitcoin_node STATIC EXCLUDE_FROM_ALL
node/interfaces.cpp
node/kernel_notifications.cpp
node/mempool_args.cpp
node/mempool_persist.cpp
node/mempool_persist_args.cpp
node/miner.cpp
node/minisketchwrapper.cpp
Expand Down
3 changes: 3 additions & 0 deletions src/bench/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ add_executable(bench_bitcoin
ccoins_caching.cpp
chacha20.cpp
checkblock.cpp
checkblockindex.cpp
checkqueue.cpp
disconnected_transactions.cpp
crypto_hash.cpp
Expand All @@ -41,11 +42,13 @@ add_executable(bench_bitcoin
mempool_stress.cpp
merkle_root.cpp
net_processing.cpp
parse_hex.cpp
peer_eviction.cpp
poly1305.cpp
pool.cpp
pow_hash.cpp
prevector.cpp
readblock.cpp
rollingbloom.cpp
rpc_blockchain.cpp
rpc_mempool.cpp
Expand Down
20 changes: 20 additions & 0 deletions src/bench/checkblockindex.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) 2023-present The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <bench/bench.h>
#include <test/util/setup_common.h>
#include <validation.h>

static void CheckBlockIndex(benchmark::Bench& bench)
{
auto testing_setup{MakeNoLogFileContext<TestChain100Setup>()};
// Mine some more blocks
testing_setup->mineBlocks(1000);
bench.run([&] {
testing_setup->m_node.chainman->CheckBlockIndex();
});
}


BENCHMARK(CheckBlockIndex, benchmark::PriorityLevel::HIGH);
36 changes: 36 additions & 0 deletions src/bench/parse_hex.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) 2024- The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <bench/bench.h>
#include <random.h>
#include <stddef.h>
#include <util/strencodings.h>
#include <cassert>
#include <optional>
#include <vector>

std::string generateHexString(size_t length) {
const auto hex_digits = "0123456789ABCDEF";
FastRandomContext rng(/*fDeterministic=*/true);

std::string data;
while (data.size() < length) {
auto digit = hex_digits[rng.randbits(4)];
data.push_back(digit);
}
return data;
}

static void HexParse(benchmark::Bench& bench)
{
auto data = generateHexString(130); // Generates 678B0EDA0A1FD30904D5A65E3568DB82DB2D918B0AD8DEA18A63FECCB877D07CAD1495C7157584D877420EF38B8DA473A6348B4F51811AC13C786B962BEE5668F9 by default

bench.batch(data.size()).unit("base16").run([&] {
auto result = TryParseHex(data);
assert(result != std::nullopt); // make sure we're measuring the successful case
ankerl::nanobench::doNotOptimizeAway(result);
});
}

BENCHMARK(HexParse, benchmark::PriorityLevel::HIGH);
38 changes: 38 additions & 0 deletions src/bench/readblock.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// 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.

#include <bench/bench.h>
#include <bench/data.h>

#include <consensus/validation.h>
#include <node/blockstorage.h>
#include <streams.h>
#include <test/util/setup_common.h>
#include <util/chaintype.h>
#include <validation.h>

static FlatFilePos WriteBlockToDisk(ChainstateManager& chainman)
{
DataStream stream{benchmark::data::block813851};
CBlock block;
stream >> block;

return chainman.m_blockman.SaveBlockToDisk(block, 0, nullptr);
}

static void ReadBlockFromDiskTest(benchmark::Bench& bench)
{
const auto testing_setup{MakeNoLogFileContext<const TestingSetup>(ChainType::MAIN)};
ChainstateManager& chainman{*testing_setup->m_node.chainman};

CBlock block;
const auto pos{WriteBlockToDisk(chainman)};

bench.run([&] {
const auto success{chainman.m_blockman.ReadBlockFromDisk(block, pos)};
assert(success);
});
}

BENCHMARK(ReadBlockFromDiskTest, benchmark::PriorityLevel::HIGH);
8 changes: 4 additions & 4 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

#include <kernel/checks.h>
#include <kernel/mempool_options.h>
#include <kernel/mempool_persist.h>
#include <kernel/validation_cache_sizes.h>

#include <addrman.h>
Expand Down Expand Up @@ -63,6 +62,7 @@
#include <node/interface_ui.h>
#include <node/kernel_notifications.h>
#include <node/mempool_args.h>
#include <node/mempool_persist.h>
#include <node/mempool_persist_args.h>
#include <node/miner.h>
#include <node/peerman_args.h>
Expand Down Expand Up @@ -173,8 +173,6 @@

using kernel::CoinStatsHashType;
using kernel::DEFAULT_STOPAFTERBLOCKIMPORT;
using kernel::DumpMempool;
using kernel::LoadMempool;
using kernel::ValidationCacheSizes;

using node::ApplyArgsManOptions;
Expand All @@ -183,6 +181,8 @@ using node::CacheSizes;
using node::CalculateCacheSizes;
using node::DEFAULT_PERSIST_MEMPOOL;
using node::DEFAULT_PRINTPRIORITY;
using node::DumpMempool;
using node::LoadMempool;
using node::g_indexes_ready_to_sync;
using node::KernelNotifications;
using node::LoadChainstate;
Expand Down Expand Up @@ -821,7 +821,7 @@ void SetupServerArgs(ArgsManager& argsman)
hidden_args.emplace_back("-zmqpubsequencehwm=<n>");
#endif

argsman.AddArg("-checkblockindex", strprintf("Do a consistency check for the block tree, and occasionally. (default: %u, regtest: %u)", defaultChainParams->DefaultConsistencyChecks(), regtestChainParams->DefaultConsistencyChecks()), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST);
argsman.AddArg("-checkblockindex", strprintf("Do a consistency check for the block tree, chainstate, and other validation data structures every <n> operations. Use 0 to disable. (default: %u, regtest: %u)", defaultChainParams->DefaultConsistencyChecks(), regtestChainParams->DefaultConsistencyChecks()), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST);
argsman.AddArg("-checkblocks=<n>", strprintf("How many blocks to check at startup (default: %u, 0 = all)", DEFAULT_CHECKBLOCKS), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST);
argsman.AddArg("-checklevel=<n>", strprintf("How thorough the block verification of -checkblocks is: %s (0-4, default: %u)", Join(CHECKLEVEL_DOC, ", "), DEFAULT_CHECKLEVEL), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST);
argsman.AddArg("-checkaddrman=<n>", strprintf("Run addrman consistency checks every <n> operations. Use 0 to disable. (default: %u)", DEFAULT_ADDRMAN_CONSISTENCY_CHECKS), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST);
Expand Down
108 changes: 108 additions & 0 deletions src/ipc/capnp/common-types.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// 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_IPC_CAPNP_COMMON_TYPES_H
#define BITCOIN_IPC_CAPNP_COMMON_TYPES_H

#include <clientversion.h>
#include <streams.h>
#include <univalue.h>

#include <cstddef>
#include <mp/proxy-types.h>
#include <type_traits>
#include <utility>

namespace ipc {
namespace capnp {
//! Use SFINAE to define Serializeable<T> trait which is true if type T has a
//! Serialize(stream) method, false otherwise.
template <typename T>
struct Serializable {
private:
template <typename C>
static std::true_type test(decltype(std::declval<C>().Serialize(std::declval<std::nullptr_t&>()))*);
template <typename>
static std::false_type test(...);

public:
static constexpr bool value = decltype(test<T>(nullptr))::value;
};

//! Use SFINAE to define Unserializeable<T> trait which is true if type T has
//! an Unserialize(stream) method, false otherwise.
template <typename T>
struct Unserializable {
private:
template <typename C>
static std::true_type test(decltype(std::declval<C>().Unserialize(std::declval<std::nullptr_t&>()))*);
template <typename>
static std::false_type test(...);

public:
static constexpr bool value = decltype(test<T>(nullptr))::value;
};
} // namespace capnp
} // namespace ipc

//! Functions to serialize / deserialize common bitcoin types.
namespace mp {
//! Overload multiprocess library's CustomBuildField hook to allow any
//! serializable object to be stored in a capnproto Data field or passed to a
//! canproto interface. Use Priority<1> so this hook has medium priority, and
//! higher priority hooks could take precedence over this one.
template <typename LocalType, typename Value, typename Output>
void CustomBuildField(
TypeList<LocalType>, Priority<1>, InvokeContext& invoke_context, Value&& value, Output&& output,
// Enable if serializeable and if LocalType is not cv or reference
// qualified. If LocalType is cv or reference qualified, it is important to
// fall back to lower-priority Priority<0> implementation of this function
// that strips cv references, to prevent this CustomBuildField overload from
// taking precedence over more narrow overloads for specific LocalTypes.
std::enable_if_t<ipc::capnp::Serializable<LocalType>::value &&
std::is_same_v<LocalType, std::remove_cv_t<std::remove_reference_t<LocalType>>>>* enable = nullptr)
{
DataStream stream;
value.Serialize(stream);
auto result = output.init(stream.size());
memcpy(result.begin(), stream.data(), stream.size());
}

//! Overload multiprocess library's CustomReadField hook to allow any object
//! with an Unserialize method to be read from a capnproto Data field or
//! returned from canproto interface. Use Priority<1> so this hook has medium
//! priority, and higher priority hooks could take precedence over this one.
template <typename LocalType, typename Input, typename ReadDest>
decltype(auto)
CustomReadField(TypeList<LocalType>, Priority<1>, InvokeContext& invoke_context, Input&& input, ReadDest&& read_dest,
std::enable_if_t<ipc::capnp::Unserializable<LocalType>::value>* enable = nullptr)
{
return read_dest.update([&](auto& value) {
if (!input.has()) return;
auto data = input.get();
SpanReader stream{SER_NETWORK, CLIENT_VERSION, {data.begin(), data.end()}};
value.Unserialize(stream);
});
}

template <typename Value, typename Output>
void CustomBuildField(TypeList<UniValue>, Priority<1>, InvokeContext& invoke_context, Value&& value, Output&& output)
{
std::string str = value.write();
auto result = output.init(str.size());
memcpy(result.begin(), str.data(), str.size());
}

template <typename Input, typename ReadDest>
decltype(auto) CustomReadField(TypeList<UniValue>, Priority<1>, InvokeContext& invoke_context, Input&& input,
ReadDest&& read_dest)
{
return read_dest.update([&](auto& value) {
auto data = input.get();
value.read(std::string_view{data.begin(), data.size()});
});
}
} // namespace mp

#endif // BITCOIN_IPC_CAPNP_COMMON_TYPES_H
1 change: 0 additions & 1 deletion src/kernel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ add_library(bitcoinkernel STATIC EXCLUDE_FROM_ALL
context.cpp
cs_main.cpp
disconnected_transactions.cpp
mempool_persist.cpp
mempool_removal_reason.cpp
../key.cpp
../key_io.cpp
Expand Down
2 changes: 1 addition & 1 deletion src/kernel/chainstatemanager_opts.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace kernel {
struct ChainstateManagerOpts {
const CChainParams& chainparams;
fs::path datadir;
std::optional<bool> check_block_index{};
std::optional<int32_t> check_block_index{};
bool checkpoints_enabled{DEFAULT_CHECKPOINTS_ENABLED};
//! If set, it will override the minimum work we will assume exists on some valid chain.
std::optional<arith_uint256> minimum_chain_work{};
Expand Down
5 changes: 4 additions & 1 deletion src/node/chainstatemanager_args.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
namespace node {
std::optional<bilingual_str> ApplyArgsManOptions(const ArgsManager& args, ChainstateManager::Options& opts)
{
if (auto value{args.GetBoolArg("-checkblockindex")}) opts.check_block_index = *value;
if (auto value{args.GetIntArg("-checkblockindex")}) {
// Interpret bare -checkblockindex argument as 1 instead of 0.
opts.check_block_index = args.GetArg("-checkblockindex")->empty() ? 1 : *value;
}

if (auto value{args.GetBoolArg("-checkpoints")}) opts.checkpoints_enabled = *value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <kernel/mempool_persist.h>
#include <node/mempool_persist.h>

#include <clientversion.h>
#include <consensus/amount.h>
Expand Down Expand Up @@ -32,7 +32,7 @@

using fsbridge::FopenFn;

namespace kernel {
namespace node {

static const uint64_t MEMPOOL_DUMP_VERSION = 1;

Expand Down Expand Up @@ -203,4 +203,4 @@ bool DumpMempool(const CTxMemPool& pool, const fs::path& dump_path, FopenFn mock
return true;
}

} // namespace kernel
} // namespace node
10 changes: 5 additions & 5 deletions src/kernel/mempool_persist.h → src/node/mempool_persist.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef BITCOIN_KERNEL_MEMPOOL_PERSIST_H
#define BITCOIN_KERNEL_MEMPOOL_PERSIST_H
#ifndef BITCOIN_NODE_MEMPOOL_PERSIST_H
#define BITCOIN_NODE_MEMPOOL_PERSIST_H

#include <util/fs.h>

class Chainstate;
class CTxMemPool;

namespace kernel {
namespace node {

/** Dump the mempool to a file. */
bool DumpMempool(const CTxMemPool& pool, const fs::path& dump_path,
Expand All @@ -28,7 +28,7 @@ bool LoadMempool(CTxMemPool& pool, const fs::path& load_path,
Chainstate& active_chainstate,
ImportMempoolOptions&& opts);

} // namespace kernel
} // namespace node


#endif // BITCOIN_KERNEL_MEMPOOL_PERSIST_H
#endif // BITCOIN_NODE_MEMPOOL_PERSIST_H
Loading
Loading