18#include <shared_mutex>
28class CSignatureCache {
36 std::shared_mutex cs_sigcache;
49 const std::vector<uint8_t> &vchSig,
51 CSHA256 hasher = m_salted_hasher;
54 .
Write(vchSig.data(), vchSig.size())
58 bool Get(
const uint256 &entry,
const bool erase) {
59 std::shared_lock<std::shared_mutex> lock(cs_sigcache);
60 return setValid.contains(entry, erase);
63 void Set(
const uint256 &entry) {
64 std::unique_lock<std::shared_mutex> lock(cs_sigcache);
65 setValid.insert(entry);
67 std::optional<std::pair<uint32_t, size_t>> setup_bytes(
size_t n) {
68 return setValid.setup_bytes(n);
79static CSignatureCache signatureCache;
86 auto setup_results = signatureCache.setup_bytes(max_size_bytes);
91 const auto [num_elems, approx_size_bytes] = *setup_results;
92 LogPrintf(
"Using %zu MiB out of %zu MiB requested for signature cache, "
93 "able to store %zu elements\n",
94 approx_size_bytes >> 20, max_size_bytes >> 20, num_elems);
100 const uint256 &sighash,
bool storeOrErase,
const F &fun) {
102 signatureCache.ComputeEntry(entry, sighash, vchSig, pubkey);
103 if (signatureCache.Get(entry, !storeOrErase)) {
110 signatureCache.Set(entry);
116 const std::vector<uint8_t> &vchSig,
const CPubKey &pubkey,
117 const uint256 &sighash)
const {
119 [] {
return false; });
123 const std::vector<uint8_t> &vchSig,
const CPubKey &pubkey,
124 const uint256 &sighash)
const {
virtual bool VerifySignature(const std::vector< uint8_t > &vchSig, const CPubKey &vchPubKey, const uint256 &sighash) const
An encapsulated public key.
unsigned int size() const
Simple read-only vector-like interface to the pubkey data.
const uint8_t * data() const
A hasher class for SHA-256.
CSHA256 & Write(const uint8_t *data, size_t len)
void Finalize(uint8_t hash[OUTPUT_SIZE])
bool VerifySignature(const std::vector< uint8_t > &vchSig, const CPubKey &vchPubKey, const uint256 &sighash) const override
bool IsCached(const std::vector< uint8_t > &vchSig, const CPubKey &vchPubKey, const uint256 &sighash) const
cache implements a cache with properties similar to a cuckoo-set.
We're hashing a nonce into the entries themselves, so we don't need extra blinding in the set hash co...
uint256 GetRandHash() noexcept
bool RunMemoizedCheck(const std::vector< uint8_t > &vchSig, const CPubKey &pubkey, const uint256 &sighash, bool storeOrErase, const F &fun)
bool InitSignatureCache(size_t max_size_bytes)