Bitcoin ABC  0.26.3
P2P Digital Currency
sigcache.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include <script/sigcache.h>
7 
8 #include <cuckoocache.h>
9 #include <pubkey.h>
10 #include <random.h>
11 #include <uint256.h>
12 #include <util/system.h>
13 
14 #include <algorithm>
15 #include <mutex>
16 #include <optional>
17 #include <shared_mutex>
18 #include <vector>
19 
20 namespace {
21 
27 class CSignatureCache {
28 private:
30  CSHA256 m_salted_hasher;
33  map_type;
34  map_type setValid;
35  std::shared_mutex cs_sigcache;
36 
37 public:
38  CSignatureCache() {
39  uint256 nonce = GetRandHash();
40  // We want the nonce to be 64 bytes long to force the hasher to process
41  // this chunk, which makes later hash computations more efficient. We
42  // just write our 32-byte entropy twice to fill the 64 bytes.
43  m_salted_hasher.Write(nonce.begin(), 32);
44  m_salted_hasher.Write(nonce.begin(), 32);
45  }
46 
47  void ComputeEntry(uint256 &entry, const uint256 &hash,
48  const std::vector<uint8_t> &vchSig,
49  const CPubKey &pubkey) {
50  CSHA256 hasher = m_salted_hasher;
51  hasher.Write(hash.begin(), 32)
52  .Write(pubkey.data(), pubkey.size())
53  .Write(vchSig.data(), vchSig.size())
54  .Finalize(entry.begin());
55  }
56 
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);
60  }
61 
62  void Set(const uint256 &entry) {
63  std::unique_lock<std::shared_mutex> lock(cs_sigcache);
64  setValid.insert(entry);
65  }
66  std::optional<std::pair<uint32_t, size_t>> setup_bytes(size_t n) {
67  return setValid.setup_bytes(n);
68  }
69 };
70 
78 static CSignatureCache signatureCache;
79 } // namespace
80 
81 // To be called once in AppInitMain/BasicTestingSetup to initialize the
82 // signatureCache.
83 
84 bool InitSignatureCache(size_t max_size_bytes) {
85  auto setup_results = signatureCache.setup_bytes(max_size_bytes);
86  if (!setup_results) {
87  return false;
88  }
89 
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);
94  return true;
95 }
96 
97 template <typename F>
98 bool RunMemoizedCheck(const std::vector<uint8_t> &vchSig, const CPubKey &pubkey,
99  const uint256 &sighash, bool storeOrErase, const F &fun) {
100  uint256 entry;
101  signatureCache.ComputeEntry(entry, sighash, vchSig, pubkey);
102  if (signatureCache.Get(entry, !storeOrErase)) {
103  return true;
104  }
105  if (!fun()) {
106  return false;
107  }
108  if (storeOrErase) {
109  signatureCache.Set(entry);
110  }
111  return true;
112 }
113 
115  const std::vector<uint8_t> &vchSig, const CPubKey &pubkey,
116  const uint256 &sighash) const {
117  return RunMemoizedCheck(vchSig, pubkey, sighash, true,
118  [] { return false; });
119 }
120 
122  const std::vector<uint8_t> &vchSig, const CPubKey &pubkey,
123  const uint256 &sighash) const {
124  return RunMemoizedCheck(vchSig, pubkey, sighash, store, [&] {
125  return TransactionSignatureChecker::VerifySignature(vchSig, pubkey,
126  sighash);
127  });
128 }
virtual bool VerifySignature(const std::vector< uint8_t > &vchSig, const CPubKey &vchPubKey, const uint256 &sighash) const
An encapsulated public key.
Definition: pubkey.h:31
const uint8_t * data() const
Definition: pubkey.h:99
unsigned int size() const
Simple read-only vector-like interface to the pubkey data.
Definition: pubkey.h:98
A hasher class for SHA-256.
Definition: sha256.h:13
CSHA256 & Write(const uint8_t *data, size_t len)
Definition: sha256.cpp:819
void Finalize(uint8_t hash[OUTPUT_SIZE])
Definition: sha256.cpp:844
bool VerifySignature(const std::vector< uint8_t > &vchSig, const CPubKey &vchPubKey, const uint256 &sighash) const override
Definition: sigcache.cpp:121
bool IsCached(const std::vector< uint8_t > &vchSig, const CPubKey &vchPubKey, const uint256 &sighash) const
Definition: sigcache.cpp:114
cache implements a cache with properties similar to a cuckoo-set.
Definition: cuckoocache.h:167
We're hashing a nonce into the entries themselves, so we don't need extra blinding in the set hash co...
Definition: hasher.h:80
uint8_t * begin()
Definition: uint256.h:85
256-bit opaque blob.
Definition: uint256.h:129
#define LogPrintf(...)
Definition: logging.h:206
uint256 GetRandHash() noexcept
Definition: random.cpp:659
bool RunMemoizedCheck(const std::vector< uint8_t > &vchSig, const CPubKey &pubkey, const uint256 &sighash, bool storeOrErase, const F &fun)
Definition: sigcache.cpp:98
bool InitSignatureCache(size_t max_size_bytes)
Definition: sigcache.cpp:84