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 <boost/thread/lock_types.hpp>
15 #include <boost/thread/shared_mutex.hpp>
16 
17 namespace {
18 
24 class CSignatureCache {
25 private:
27  CSHA256 m_salted_hasher;
30  map_type;
31  map_type setValid;
32  boost::shared_mutex cs_sigcache;
33 
34 public:
35  CSignatureCache() {
36  uint256 nonce = GetRandHash();
37  // We want the nonce to be 64 bytes long to force the hasher to process
38  // this chunk, which makes later hash computations more efficient. We
39  // just write our 32-byte entropy twice to fill the 64 bytes.
40  m_salted_hasher.Write(nonce.begin(), 32);
41  m_salted_hasher.Write(nonce.begin(), 32);
42  }
43 
44  void ComputeEntry(uint256 &entry, const uint256 &hash,
45  const std::vector<uint8_t> &vchSig,
46  const CPubKey &pubkey) {
47  CSHA256 hasher = m_salted_hasher;
48  hasher.Write(hash.begin(), 32)
49  .Write(&pubkey[0], pubkey.size())
50  .Write(&vchSig[0], vchSig.size())
51  .Finalize(entry.begin());
52  }
53 
54  bool Get(const uint256 &entry, const bool erase) {
55  boost::shared_lock<boost::shared_mutex> lock(cs_sigcache);
56  return setValid.contains(entry, erase);
57  }
58 
59  void Set(const uint256 &entry) {
60  boost::unique_lock<boost::shared_mutex> lock(cs_sigcache);
61  setValid.insert(entry);
62  }
63  uint32_t setup_bytes(size_t n) { return setValid.setup_bytes(n); }
64 };
65 
73 static CSignatureCache signatureCache;
74 } // namespace
75 
76 // To be called once in AppInitMain/BasicTestingSetup to initialize the
77 // signatureCache.
79  // nMaxCacheSize is unsigned. If -maxsigcachesize is set to zero,
80  // setup_bytes creates the minimum possible cache (2 elements).
81  size_t nMaxCacheSize =
82  std::min(
83  std::max(int64_t(0), gArgs.GetIntArg("-maxsigcachesize",
86  (size_t(1) << 20);
87  size_t nElems = signatureCache.setup_bytes(nMaxCacheSize);
88  LogPrintf("Using %zu MiB out of %zu requested for signature cache, able to "
89  "store %zu elements\n",
90  (nElems * sizeof(uint256)) >> 20, nMaxCacheSize >> 20, nElems);
91 }
92 
93 template <typename F>
94 bool RunMemoizedCheck(const std::vector<uint8_t> &vchSig, const CPubKey &pubkey,
95  const uint256 &sighash, bool storeOrErase, const F &fun) {
96  uint256 entry;
97  signatureCache.ComputeEntry(entry, sighash, vchSig, pubkey);
98  if (signatureCache.Get(entry, !storeOrErase)) {
99  return true;
100  }
101  if (!fun()) {
102  return false;
103  }
104  if (storeOrErase) {
105  signatureCache.Set(entry);
106  }
107  return true;
108 }
109 
111  const std::vector<uint8_t> &vchSig, const CPubKey &pubkey,
112  const uint256 &sighash) const {
113  return RunMemoizedCheck(vchSig, pubkey, sighash, true,
114  [] { return false; });
115 }
116 
118  const std::vector<uint8_t> &vchSig, const CPubKey &pubkey,
119  const uint256 &sighash) const {
120  return RunMemoizedCheck(vchSig, pubkey, sighash, store, [&] {
121  return TransactionSignatureChecker::VerifySignature(vchSig, pubkey,
122  sighash);
123  });
124 }
int64_t GetIntArg(const std::string &strArg, int64_t nDefault) const
Return integer argument or default value.
Definition: system.cpp:593
virtual bool VerifySignature(const std::vector< uint8_t > &vchSig, const CPubKey &vchPubKey, const uint256 &sighash) const
An encapsulated public key.
Definition: pubkey.h:31
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:117
bool IsCached(const std::vector< uint8_t > &vchSig, const CPubKey &vchPubKey, const uint256 &sighash) const
Definition: sigcache.cpp:110
cache implements a cache with properties similar to a cuckoo-set.
Definition: cuckoocache.h:162
We're hashing a nonce into the entries themselves, so we don't need extra blinding in the set hash co...
Definition: hasher.h:70
uint8_t * begin()
Definition: uint256.h:83
256-bit opaque blob.
Definition: uint256.h:127
#define LogPrintf(...)
Definition: logging.h:204
uint256 GetRandHash() noexcept
Definition: random.cpp:658
bool RunMemoizedCheck(const std::vector< uint8_t > &vchSig, const CPubKey &pubkey, const uint256 &sighash, bool storeOrErase, const F &fun)
Definition: sigcache.cpp:94
void InitSignatureCache()
Definition: sigcache.cpp:78
static const unsigned int DEFAULT_MAX_SIG_CACHE_SIZE
Definition: sigcache.h:17
static const int64_t MAX_MAX_SIG_CACHE_SIZE
Definition: sigcache.h:19
ArgsManager gArgs
Definition: system.cpp:77