Bitcoin Core  0.18.99
P2P Digital Currency
sigcache.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2018 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 <pubkey.h>
9 #include <random.h>
10 #include <uint256.h>
11 #include <util/system.h>
12 
13 #include <cuckoocache.h>
14 #include <boost/thread.hpp>
15 
16 namespace {
22 class CSignatureCache
23 {
24 private:
26  uint256 nonce;
28  map_type setValid;
29  boost::shared_mutex cs_sigcache;
30 
31 public:
32  CSignatureCache()
33  {
34  GetRandBytes(nonce.begin(), 32);
35  }
36 
37  void
38  ComputeEntry(uint256& entry, const uint256 &hash, const std::vector<unsigned char>& vchSig, const CPubKey& pubkey)
39  {
40  CSHA256().Write(nonce.begin(), 32).Write(hash.begin(), 32).Write(&pubkey[0], pubkey.size()).Write(&vchSig[0], vchSig.size()).Finalize(entry.begin());
41  }
42 
43  bool
44  Get(const uint256& entry, const bool erase)
45  {
46  boost::shared_lock<boost::shared_mutex> lock(cs_sigcache);
47  return setValid.contains(entry, erase);
48  }
49 
50  void Set(uint256& entry)
51  {
52  boost::unique_lock<boost::shared_mutex> lock(cs_sigcache);
53  setValid.insert(entry);
54  }
55  uint32_t setup_bytes(size_t n)
56  {
57  return setValid.setup_bytes(n);
58  }
59 };
60 
61 /* In previous versions of this code, signatureCache was a local static variable
62  * in CachingTransactionSignatureChecker::VerifySignature. We initialize
63  * signatureCache outside of VerifySignature to avoid the atomic operation per
64  * call overhead associated with local static variables even though
65  * signatureCache could be made local to VerifySignature.
66 */
67 static CSignatureCache signatureCache;
68 } // namespace
69 
70 // To be called once in AppInitMain/BasicTestingSetup to initialize the
71 // signatureCache.
73 {
74  // nMaxCacheSize is unsigned. If -maxsigcachesize is set to zero,
75  // setup_bytes creates the minimum possible cache (2 elements).
76  size_t nMaxCacheSize = std::min(std::max((int64_t)0, gArgs.GetArg("-maxsigcachesize", DEFAULT_MAX_SIG_CACHE_SIZE) / 2), MAX_MAX_SIG_CACHE_SIZE) * ((size_t) 1 << 20);
77  size_t nElems = signatureCache.setup_bytes(nMaxCacheSize);
78  LogPrintf("Using %zu MiB out of %zu/2 requested for signature cache, able to store %zu elements\n",
79  (nElems*sizeof(uint256)) >>20, (nMaxCacheSize*2)>>20, nElems);
80 }
81 
82 bool CachingTransactionSignatureChecker::VerifySignature(const std::vector<unsigned char>& vchSig, const CPubKey& pubkey, const uint256& sighash) const
83 {
84  uint256 entry;
85  signatureCache.ComputeEntry(entry, sighash, vchSig, pubkey);
86  if (signatureCache.Get(entry, !store))
87  return true;
88  if (!TransactionSignatureChecker::VerifySignature(vchSig, pubkey, sighash))
89  return false;
90  if (store)
91  signatureCache.Set(entry);
92  return true;
93 }
CSHA256 & Write(const unsigned char *data, size_t len)
Definition: sha256.cpp:648
bool VerifySignature(const std::vector< unsigned char > &vchSig, const CPubKey &vchPubKey, const uint256 &sighash) const override
Definition: sigcache.cpp:82
static const unsigned int DEFAULT_MAX_SIG_CACHE_SIZE
Definition: sigcache.h:16
cache implements a cache with properties similar to a cuckoo-set
Definition: cuckoocache.h:160
static void LogPrintf(const char *fmt, const Args &... args)
Definition: logging.h:144
void GetRandBytes(unsigned char *buf, int num) noexcept
Overall design of the RNG and entropy sources.
Definition: random.cpp:659
unsigned char * begin()
Definition: uint256.h:55
virtual bool VerifySignature(const std::vector< unsigned char > &vchSig, const CPubKey &vchPubKey, const uint256 &sighash) const
An encapsulated public key.
Definition: pubkey.h:30
void InitSignatureCache()
Definition: sigcache.cpp:72
unsigned int size() const
Simple read-only vector-like interface to the pubkey data.
Definition: pubkey.h:109
256-bit opaque blob.
Definition: uint256.h:121
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:496
ArgsManager gArgs
Definition: system.cpp:73
A hasher class for SHA-256.
Definition: sha256.h:13