Skip to content

Commit 94d56b9

Browse files
committed
Merge bitcoin/bitcoin#30141: kernel: De-globalize validation caches
606a7ab kernel: De-globalize signature cache (TheCharlatan) 66d74bf Expose CSignatureCache class in header (TheCharlatan) 021d388 kernel: De-globalize script execution cache hasher (TheCharlatan) 13a3661 kernel: De-globalize script execution cache (TheCharlatan) ab14d1d validation: Don't error if maxsigcachesize exceeds uint32::max (TheCharlatan) Pull request description: The validation caches are currently setup independently from where the rest of the validation code is initialized. This makes their ownership semantics unclear. There is also no clear enforcement on when and in what order they need to be initialized. The caches are always initialized in the `BasicTestingSetup` although a number of tests don't actually need them. Solve this by moving the caches from global scope into the `ChainstateManager` class. This simplifies the usage of the kernel library by no longer requiring manual setup of the caches prior to using the `ChainstateManager`. Tests that need to access the caches can instantiate them independently. --- This pull request is part of the [libbitcoinkernel project](bitcoin/bitcoin#27587). ACKs for top commit: stickies-v: re-ACK 606a7ab glozow: reACK 606a7ab ryanofsky: Code review ACK 606a7ab. Just small formatting, include, and static_assert changes since last review. Tree-SHA512: e7f3ee41406e3b233832bb67dc3a63c4203b5367e5daeed383df9cb590f227fcc62eae31311029c077d5e81b273a37a88a364db3dee2efe91bb3b9c9ddc8a42e
2 parents 1c11089 + 606a7ab commit 94d56b9

19 files changed

+201
-261
lines changed

src/Makefile.am

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,6 @@ BITCOIN_CORE_H = \
195195
kernel/mempool_removal_reason.h \
196196
kernel/messagestartchars.h \
197197
kernel/notifications_interface.h \
198-
kernel/validation_cache_sizes.h \
199198
kernel/warning.h \
200199
key.h \
201200
key_io.h \
@@ -240,7 +239,6 @@ BITCOIN_CORE_H = \
240239
node/txreconciliation.h \
241240
node/types.h \
242241
node/utxo_snapshot.h \
243-
node/validation_cache_args.h \
244242
node/warnings.h \
245243
noui.h \
246244
outputtype.h \
@@ -445,7 +443,6 @@ libbitcoin_node_a_SOURCES = \
445443
node/transaction.cpp \
446444
node/txreconciliation.cpp \
447445
node/utxo_snapshot.cpp \
448-
node/validation_cache_args.cpp \
449446
node/warnings.cpp \
450447
noui.cpp \
451448
policy/fees.cpp \

src/bitcoin-chainstate.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#include <kernel/chainstatemanager_opts.h>
1616
#include <kernel/checks.h>
1717
#include <kernel/context.h>
18-
#include <kernel/validation_cache_sizes.h>
1918
#include <kernel/warning.h>
2019

2120
#include <consensus/validation.h>
@@ -63,13 +62,6 @@ int main(int argc, char* argv[])
6362
// properly
6463
assert(kernel::SanityChecks(kernel_context));
6564

66-
// Necessary for CheckInputScripts (eventually called by ProcessNewBlock),
67-
// which will try the script cache first and fall back to actually
68-
// performing the check with the signature cache.
69-
kernel::ValidationCacheSizes validation_cache_sizes{};
70-
Assert(InitSignatureCache(validation_cache_sizes.signature_cache_bytes));
71-
Assert(InitScriptExecutionCache(validation_cache_sizes.script_execution_cache_bytes));
72-
7365
ValidationSignals validation_signals{std::make_unique<util::ImmediateTaskRunner>()};
7466

7567
class KernelNotifications : public kernel::Notifications

src/cuckoocache.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#include <cstring>
1515
#include <limits>
1616
#include <memory>
17-
#include <optional>
1817
#include <utility>
1918
#include <vector>
2019

@@ -360,16 +359,15 @@ class cache
360359
* structure
361360
* @returns A pair of the maximum number of elements storable (see setup()
362361
* documentation for more detail) and the approximate total size of these
363-
* elements in bytes or std::nullopt if the size requested is too large.
362+
* elements in bytes.
364363
*/
365-
std::optional<std::pair<uint32_t, size_t>> setup_bytes(size_t bytes)
364+
std::pair<uint32_t, size_t> setup_bytes(size_t bytes)
366365
{
367-
size_t requested_num_elems = bytes / sizeof(Element);
368-
if (std::numeric_limits<uint32_t>::max() < requested_num_elems) {
369-
return std::nullopt;
370-
}
366+
uint32_t requested_num_elems(std::min<size_t>(
367+
bytes / sizeof(Element),
368+
std::numeric_limits<uint32_t>::max()));
371369

372-
auto num_elems = setup(bytes/sizeof(Element));
370+
auto num_elems = setup(requested_num_elems);
373371

374372
size_t approx_size_bytes = num_elems * sizeof(Element);
375373
return std::make_pair(num_elems, approx_size_bytes);

src/init.cpp

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include <init.h>
99

1010
#include <kernel/checks.h>
11-
#include <kernel/validation_cache_sizes.h>
1211

1312
#include <addrman.h>
1413
#include <banman.h>
@@ -54,7 +53,6 @@
5453
#include <node/mempool_persist_args.h>
5554
#include <node/miner.h>
5655
#include <node/peerman_args.h>
57-
#include <node/validation_cache_args.h>
5856
#include <policy/feerate.h>
5957
#include <policy/fees.h>
6058
#include <policy/fees_args.h>
@@ -119,7 +117,6 @@
119117
using common::AmountErrMsg;
120118
using common::InvalidPortErrMsg;
121119
using common::ResolveErrMsg;
122-
using kernel::ValidationCacheSizes;
123120

124121
using node::ApplyArgsManOptions;
125122
using node::BlockManager;
@@ -619,7 +616,7 @@ void SetupServerArgs(ArgsManager& argsman)
619616
argsman.AddArg("-test=<option>", "Pass a test-only option. Options include : " + Join(TEST_OPTIONS_DOC, ", ") + ".", ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST);
620617
argsman.AddArg("-capturemessages", "Capture all P2P messages to disk", ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST);
621618
argsman.AddArg("-mocktime=<n>", "Replace actual time with " + UNIX_EPOCH_TIME + " (default: 0)", ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST);
622-
argsman.AddArg("-maxsigcachesize=<n>", strprintf("Limit sum of signature cache and script execution cache sizes to <n> MiB (default: %u)", DEFAULT_MAX_SIG_CACHE_BYTES >> 20), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST);
619+
argsman.AddArg("-maxsigcachesize=<n>", strprintf("Limit sum of signature cache and script execution cache sizes to <n> MiB (default: %u)", DEFAULT_VALIDATION_CACHE_BYTES >> 20), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST);
623620
argsman.AddArg("-maxtipage=<n>",
624621
strprintf("Maximum tip age in seconds to consider node in initial block download (default: %u)",
625622
Ticks<std::chrono::seconds>(DEFAULT_MAX_TIP_AGE)),
@@ -1154,14 +1151,6 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
11541151
args.GetArg("-datadir", ""), fs::PathToString(fs::current_path()));
11551152
}
11561153

1157-
ValidationCacheSizes validation_cache_sizes{};
1158-
ApplyArgsManOptions(args, validation_cache_sizes);
1159-
if (!InitSignatureCache(validation_cache_sizes.signature_cache_bytes)
1160-
|| !InitScriptExecutionCache(validation_cache_sizes.script_execution_cache_bytes))
1161-
{
1162-
return InitError(strprintf(_("Unable to allocate memory for -maxsigcachesize: '%s' MiB"), args.GetIntArg("-maxsigcachesize", DEFAULT_MAX_SIG_CACHE_BYTES >> 20)));
1163-
}
1164-
11651154
assert(!node.scheduler);
11661155
node.scheduler = std::make_unique<CScheduler>();
11671156
auto& scheduler = *node.scheduler;

src/kernel/chainstatemanager_opts.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <arith_uint256.h>
1111
#include <dbwrapper.h>
12+
#include <script/sigcache.h>
1213
#include <txdb.h>
1314
#include <uint256.h>
1415
#include <util/time.h>
@@ -48,6 +49,8 @@ struct ChainstateManagerOpts {
4849
ValidationSignals* signals{nullptr};
4950
//! Number of script check worker threads. Zero means no parallel verification.
5051
int worker_threads_num{0};
52+
size_t script_execution_cache_bytes{DEFAULT_SCRIPT_EXECUTION_CACHE_BYTES};
53+
size_t signature_cache_bytes{DEFAULT_SIGNATURE_CACHE_BYTES};
5154
};
5255

5356
} // namespace kernel

src/kernel/validation_cache_sizes.h

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/node/chainstatemanager_args.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@ util::Result<void> ApplyArgsManOptions(const ArgsManager& args, ChainstateManage
5656
opts.worker_threads_num = std::clamp(script_threads - 1, 0, MAX_SCRIPTCHECK_THREADS);
5757
LogPrintf("Script verification uses %d additional threads\n", opts.worker_threads_num);
5858

59+
if (auto max_size = args.GetIntArg("-maxsigcachesize")) {
60+
// 1. When supplied with a max_size of 0, both the signature cache and
61+
// script execution cache create the minimum possible cache (2
62+
// elements). Therefore, we can use 0 as a floor here.
63+
// 2. Multiply first, divide after to avoid integer truncation.
64+
size_t clamped_size_each = std::max<int64_t>(*max_size, 0) * (1 << 20) / 2;
65+
opts.script_execution_cache_bytes = clamped_size_each;
66+
opts.signature_cache_bytes = clamped_size_each;
67+
}
68+
5969
return {};
6070
}
6171
} // namespace node

src/node/validation_cache_args.cpp

Lines changed: 0 additions & 34 deletions
This file was deleted.

src/node/validation_cache_args.h

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)