Bitcoin ABC  0.22.12
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 <boost/thread/shared_mutex.hpp>
15 
16 namespace {
17 
23 class CSignatureCache {
24 private:
26  uint256 nonce;
29  map_type;
30  map_type setValid;
31  boost::shared_mutex cs_sigcache;
32 
33 public:
34  CSignatureCache() { GetRandBytes(nonce.begin(), 32); }
35 
36  void ComputeEntry(uint256 &entry, const uint256 &hash,
37  const std::vector<uint8_t> &vchSig,
38  const CPubKey &pubkey) {
39  CSHA256()
40  .Write(nonce.begin(), 32)
41  .Write(hash.begin(), 32)
42  .Write(&pubkey[0], pubkey.size())
43  .Write(&vchSig[0], vchSig.size())
44  .Finalize(entry.begin());
45  }
46 
47  bool Get(const uint256 &entry, const bool erase) {
48  boost::shared_lock<boost::shared_mutex> lock(cs_sigcache);
49  return setValid.contains(entry, erase);
50  }
51 
52  void Set(uint256 &entry) {
53  boost::unique_lock<boost::shared_mutex> lock(cs_sigcache);
54  setValid.insert(entry);
55  }
56  uint32_t setup_bytes(size_t n) { return setValid.setup_bytes(n); }
57 };
58 
66 static CSignatureCache signatureCache;
67 } // namespace
68 
69 // To be called once in AppInitMain/BasicTestingSetup to initialize the
70 // signatureCache.
72  // nMaxCacheSize is unsigned. If -maxsigcachesize is set to zero,
73  // setup_bytes creates the minimum possible cache (2 elements).
74  size_t nMaxCacheSize =
75  std::min(std::max(int64_t(0), gArgs.GetArg("-maxsigcachesize",
78  (size_t(1) << 20);
79  size_t nElems = signatureCache.setup_bytes(nMaxCacheSize);
80  LogPrintf("Using %zu MiB out of %zu requested for signature cache, able to "
81  "store %zu elements\n",
82  (nElems * sizeof(uint256)) >> 20, nMaxCacheSize >> 20, nElems);
83 }
84 
85 template <typename F>
86 bool RunMemoizedCheck(const std::vector<uint8_t> &vchSig, const CPubKey &pubkey,
87  const uint256 &sighash, bool storeOrErase, const F &fun) {
88  uint256 entry;
89  signatureCache.ComputeEntry(entry, sighash, vchSig, pubkey);
90  if (signatureCache.Get(entry, !storeOrErase)) {
91  return true;
92  }
93  if (!fun()) {
94  return false;
95  }
96  if (storeOrErase) {
97  signatureCache.Set(entry);
98  }
99  return true;
100 }
101 
103  const std::vector<uint8_t> &vchSig, const CPubKey &pubkey,
104  const uint256 &sighash) const {
105  return RunMemoizedCheck(vchSig, pubkey, sighash, true,
106  [] { return false; });
107 }
108 
110  const std::vector<uint8_t> &vchSig, const CPubKey &pubkey,
111  const uint256 &sighash) const {
112  return RunMemoizedCheck(vchSig, pubkey, sighash, store, [&] {
113  return TransactionSignatureChecker::VerifySignature(vchSig, pubkey,
114  sighash);
115  });
116 }
static const unsigned int DEFAULT_MAX_SIG_CACHE_SIZE
Definition: sigcache.h:16
virtual bool VerifySignature(const std::vector< uint8_t > &vchSig, const CPubKey &vchPubKey, const uint256 &sighash) const
bool VerifySignature(const std::vector< uint8_t > &vchSig, const CPubKey &vchPubKey, const uint256 &sighash) const override
Definition: sigcache.cpp:109
cache implements a cache with properties similar to a cuckoo-set.
Definition: cuckoocache.h:162
static void LogPrintf(const char *fmt, const Args &... args)
Definition: logging.h:174
We&#39;re hashing a nonce into the entries themselves, so we don&#39;t need extra blinding in the set hash co...
Definition: sigcache.h:30
void GetRandBytes(uint8_t *buf, int num) noexcept
Overall design of the RNG and entropy sources.
Definition: random.cpp:625
An encapsulated public key.
Definition: pubkey.h:31
void InitSignatureCache()
Definition: sigcache.cpp:71
unsigned int size() const
Simple read-only vector-like interface to the pubkey data.
Definition: pubkey.h:98
uint8_t * begin()
Definition: uint256.h:76
256-bit opaque blob.
Definition: uint256.h:120
static const int64_t MAX_MAX_SIG_CACHE_SIZE
Definition: sigcache.h:18
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: system.cpp:479
CSHA256 & Write(const uint8_t *data, size_t len)
Definition: sha256.cpp:819
ArgsManager gArgs
Definition: system.cpp:76
bool RunMemoizedCheck(const std::vector< uint8_t > &vchSig, const CPubKey &pubkey, const uint256 &sighash, bool storeOrErase, const F &fun)
Definition: sigcache.cpp:86
A hasher class for SHA-256.
Definition: sha256.h:13
bool IsCached(const std::vector< uint8_t > &vchSig, const CPubKey &vchPubKey, const uint256 &sighash) const
Definition: sigcache.cpp:102