17 #include <shared_mutex>
27 class CSignatureCache {
35 std::shared_mutex cs_sigcache;
48 const std::vector<uint8_t> &vchSig,
50 CSHA256 hasher = m_salted_hasher;
53 .
Write(vchSig.data(), vchSig.size())
57 bool Get(
const uint256 &entry,
const bool erase) {
58 std::shared_lock<std::shared_mutex> lock(cs_sigcache);
59 return setValid.contains(entry, erase);
62 void Set(
const uint256 &entry) {
63 std::unique_lock<std::shared_mutex> lock(cs_sigcache);
64 setValid.insert(entry);
66 std::optional<std::pair<uint32_t, size_t>> setup_bytes(
size_t n) {
67 return setValid.setup_bytes(n);
78 static CSignatureCache signatureCache;
85 auto setup_results = signatureCache.setup_bytes(max_size_bytes);
90 const auto [num_elems, approx_size_bytes] = *setup_results;
91 LogPrintf(
"Using %zu MiB out of %zu MiB requested for signature cache, "
92 "able to store %zu elements\n",
93 approx_size_bytes >> 20, max_size_bytes >> 20, num_elems);
99 const uint256 &sighash,
bool storeOrErase,
const F &fun) {
101 signatureCache.ComputeEntry(entry, sighash, vchSig, pubkey);
102 if (signatureCache.Get(entry, !storeOrErase)) {
109 signatureCache.Set(entry);
115 const std::vector<uint8_t> &vchSig,
const CPubKey &pubkey,
116 const uint256 &sighash)
const {
118 [] {
return false; });
122 const std::vector<uint8_t> &vchSig,
const CPubKey &pubkey,
123 const uint256 &sighash)
const {
virtual bool VerifySignature(const std::vector< uint8_t > &vchSig, const CPubKey &vchPubKey, const uint256 &sighash) const
An encapsulated public key.
const uint8_t * data() const
unsigned int size() const
Simple read-only vector-like interface to the pubkey data.
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)