Skip to content

Commit 5b1576b

Browse files
committed
kernel: De-globalize signature cache
Move its ownership to the ChainstateManager class. Next to simplifying usage of the kernel library by no longer requiring manual setup of the cache prior to using validation code, it also slims down the amount of memory allocated by BasicTestingSetup. Use this opportunity to make CSignatureCache RAII styled
1 parent 737acb7 commit 5b1576b

18 files changed

+44
-147
lines changed

src/Makefile.am

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,6 @@ BITCOIN_CORE_H = \
192192
kernel/mempool_removal_reason.h \
193193
kernel/messagestartchars.h \
194194
kernel/notifications_interface.h \
195-
kernel/validation_cache_sizes.h \
196195
key.h \
197196
key_io.h \
198197
logging.h \
@@ -234,7 +233,6 @@ BITCOIN_CORE_H = \
234233
node/transaction.h \
235234
node/txreconciliation.h \
236235
node/utxo_snapshot.h \
237-
node/validation_cache_args.h \
238236
noui.h \
239237
outputtype.h \
240238
policy/v3_policy.h \
@@ -438,7 +436,6 @@ libbitcoin_node_a_SOURCES = \
438436
node/transaction.cpp \
439437
node/txreconciliation.cpp \
440438
node/utxo_snapshot.cpp \
441-
node/validation_cache_args.cpp \
442439
noui.cpp \
443440
policy/v3_policy.cpp \
444441
policy/fees.cpp \

src/bitcoin-chainstate.cpp

Lines changed: 0 additions & 7 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

2019
#include <consensus/validation.h>
2120
#include <core_io.h>
@@ -61,12 +60,6 @@ int main(int argc, char* argv[])
6160
// properly
6261
assert(kernel::SanityChecks(kernel_context));
6362

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

7265
class KernelNotifications : public kernel::Notifications

src/init.cpp

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

1010
#include <kernel/checks.h>
1111
#include <kernel/mempool_persist.h>
12-
#include <kernel/validation_cache_sizes.h>
1312

1413
#include <addrman.h>
1514
#include <banman.h>
@@ -53,7 +52,6 @@
5352
#include <node/mempool_persist_args.h>
5453
#include <node/miner.h>
5554
#include <node/peerman_args.h>
56-
#include <node/validation_cache_args.h>
5755
#include <policy/feerate.h>
5856
#include <policy/fees.h>
5957
#include <policy/fees_args.h>
@@ -117,7 +115,6 @@
117115

118116
using kernel::DumpMempool;
119117
using kernel::LoadMempool;
120-
using kernel::ValidationCacheSizes;
121118

122119
using node::ApplyArgsManOptions;
123120
using node::BlockManager;
@@ -1145,10 +1142,6 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
11451142
args.GetArg("-datadir", ""), fs::PathToString(fs::current_path()));
11461143
}
11471144

1148-
ValidationCacheSizes validation_cache_sizes{};
1149-
ApplyArgsManOptions(args, validation_cache_sizes);
1150-
(void)InitSignatureCache(validation_cache_sizes.signature_cache_bytes);
1151-
11521145
assert(!node.scheduler);
11531146
node.scheduler = std::make_unique<CScheduler>();
11541147
auto& scheduler = *node.scheduler;

src/kernel/chainstatemanager_opts.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ struct ChainstateManagerOpts {
5050
//! Number of script check worker threads. Zero means no parallel verification.
5151
int worker_threads_num{0};
5252
size_t script_execution_cache_bytes{DEFAULT_MAX_SIG_CACHE_BYTES / 2};
53+
size_t signature_cache_bytes{DEFAULT_MAX_SIG_CACHE_BYTES / 2};
5354
};
5455

5556
} // namespace kernel

src/kernel/validation_cache_sizes.h

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

src/node/chainstatemanager_args.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ util::Result<void> ApplyArgsManOptions(const ArgsManager& args, ChainstateManage
6060
// 2. Multiply first, divide after to avoid integer truncation.
6161
size_t clamped_size_each = std::max<int64_t>(*max_size, 0) * (1 << 20) / 2;
6262
opts.script_execution_cache_bytes = clamped_size_each;
63+
opts.signature_cache_bytes = clamped_size_each;
6364
}
6465

6566
return {};

src/node/validation_cache_args.cpp

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

src/node/validation_cache_args.h

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

src/script/sigcache.cpp

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include <shared_mutex>
1818
#include <vector>
1919

20-
CSignatureCache::CSignatureCache()
20+
CSignatureCache::CSignatureCache(size_t max_size_bytes)
2121
{
2222
uint256 nonce = GetRandHash();
2323
// We want the nonce to be 64 bytes long to force the hasher to process
@@ -30,6 +30,10 @@ CSignatureCache::CSignatureCache()
3030
m_salted_hasher_ecdsa.Write(PADDING_ECDSA, 32);
3131
m_salted_hasher_schnorr.Write(nonce.begin(), 32);
3232
m_salted_hasher_schnorr.Write(PADDING_SCHNORR, 32);
33+
34+
const auto [num_elems, approx_size_bytes] = setValid.setup_bytes(max_size_bytes);
35+
LogPrintf("Using %zu MiB out of %zu MiB requested for signature cache, able to store %zu elements\n",
36+
approx_size_bytes >> 20, max_size_bytes >> 20, num_elems);
3337
}
3438

3539
void CSignatureCache::ComputeEntryECDSA(uint256& entry, const uint256 &hash, const std::vector<unsigned char>& vchSig, const CPubKey& pubkey) const
@@ -56,48 +60,25 @@ void CSignatureCache::Set(const uint256& entry)
5660
setValid.insert(entry);
5761
}
5862

59-
std::pair<uint32_t, size_t> CSignatureCache::setup_bytes(size_t n)
60-
{
61-
return setValid.setup_bytes(n);
62-
}
63-
64-
/* In previous versions of this code, signatureCache was a local static variable
65-
* in CachingTransactionSignatureChecker::VerifySignature. We initialize
66-
* signatureCache outside of VerifySignature to avoid the atomic operation per
67-
* call overhead associated with local static variables even though
68-
* signatureCache could be made local to VerifySignature.
69-
*/
70-
static CSignatureCache signatureCache;
71-
72-
// To be called once in AppInitMain/BasicTestingSetup to initialize the
73-
// signatureCache.
74-
bool InitSignatureCache(size_t max_size_bytes)
75-
{
76-
const auto [num_elems, approx_size_bytes] = signatureCache.setup_bytes(max_size_bytes);
77-
LogPrintf("Using %zu MiB out of %zu MiB requested for signature cache, able to store %zu elements\n",
78-
approx_size_bytes >> 20, max_size_bytes >> 20, num_elems);
79-
return true;
80-
}
81-
8263
bool CachingTransactionSignatureChecker::VerifyECDSASignature(const std::vector<unsigned char>& vchSig, const CPubKey& pubkey, const uint256& sighash) const
8364
{
8465
uint256 entry;
85-
signatureCache.ComputeEntryECDSA(entry, sighash, vchSig, pubkey);
86-
if (signatureCache.Get(entry, !store))
66+
m_signature_cache.ComputeEntryECDSA(entry, sighash, vchSig, pubkey);
67+
if (m_signature_cache.Get(entry, !store))
8768
return true;
8869
if (!TransactionSignatureChecker::VerifyECDSASignature(vchSig, pubkey, sighash))
8970
return false;
9071
if (store)
91-
signatureCache.Set(entry);
72+
m_signature_cache.Set(entry);
9273
return true;
9374
}
9475

9576
bool CachingTransactionSignatureChecker::VerifySchnorrSignature(Span<const unsigned char> sig, const XOnlyPubKey& pubkey, const uint256& sighash) const
9677
{
9778
uint256 entry;
98-
signatureCache.ComputeEntrySchnorr(entry, sighash, sig, pubkey);
99-
if (signatureCache.Get(entry, !store)) return true;
79+
m_signature_cache.ComputeEntrySchnorr(entry, sighash, sig, pubkey);
80+
if (m_signature_cache.Get(entry, !store)) return true;
10081
if (!TransactionSignatureChecker::VerifySchnorrSignature(sig, pubkey, sighash)) return false;
101-
if (store) signatureCache.Set(entry);
82+
if (store) m_signature_cache.Set(entry);
10283
return true;
10384
}

src/script/sigcache.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class CSignatureCache
4646
std::shared_mutex cs_sigcache;
4747

4848
public:
49-
CSignatureCache();
49+
CSignatureCache(size_t max_size_bytes);
5050

5151
void ComputeEntryECDSA(uint256& entry, const uint256 &hash, const std::vector<unsigned char>& vchSig, const CPubKey& pubkey) const;
5252

@@ -55,22 +55,19 @@ class CSignatureCache
5555
bool Get(const uint256& entry, const bool erase);
5656

5757
void Set(const uint256& entry);
58-
59-
std::pair<uint32_t, size_t> setup_bytes(size_t n);
6058
};
6159

6260
class CachingTransactionSignatureChecker : public TransactionSignatureChecker
6361
{
6462
private:
6563
bool store;
64+
CSignatureCache& m_signature_cache;
6665

6766
public:
68-
CachingTransactionSignatureChecker(const CTransaction* txToIn, unsigned int nInIn, const CAmount& amountIn, bool storeIn, PrecomputedTransactionData& txdataIn) : TransactionSignatureChecker(txToIn, nInIn, amountIn, txdataIn, MissingDataBehavior::ASSERT_FAIL), store(storeIn) {}
67+
CachingTransactionSignatureChecker(const CTransaction* txToIn, unsigned int nInIn, const CAmount& amountIn, bool storeIn, CSignatureCache& signature_cache, PrecomputedTransactionData& txdataIn) : TransactionSignatureChecker(txToIn, nInIn, amountIn, txdataIn, MissingDataBehavior::ASSERT_FAIL), store(storeIn), m_signature_cache(signature_cache) {}
6968

7069
bool VerifyECDSASignature(const std::vector<unsigned char>& vchSig, const CPubKey& vchPubKey, const uint256& sighash) const override;
7170
bool VerifySchnorrSignature(Span<const unsigned char> sig, const XOnlyPubKey& pubkey, const uint256& sighash) const override;
7271
};
7372

74-
[[nodiscard]] bool InitSignatureCache(size_t max_size_bytes);
75-
7673
#endif // BITCOIN_SCRIPT_SIGCACHE_H

0 commit comments

Comments
 (0)