Bitcoin Core  0.20.99
P2P Digital Currency
sigcache.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2019 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/shared_mutex.hpp>
15 
16 namespace {
22 class CSignatureCache
23 {
24 private:
26  CSHA256 m_salted_hasher;
28  map_type setValid;
29  boost::shared_mutex cs_sigcache;
30 
31 public:
32  CSignatureCache()
33  {
34  uint256 nonce = GetRandHash();
35  // We want the nonce to be 64 bytes long to force the hasher to process
36  // this chunk, which makes later hash computations more efficient. We
37  // just write our 32-byte entropy twice to fill the 64 bytes.
38  m_salted_hasher.Write(nonce.begin(), 32);
39  m_salted_hasher.Write(nonce.begin(), 32);
40  }
41 
42  void
43  ComputeEntry(uint256& entry, const uint256 &hash, const std::vector<unsigned char>& vchSig, const CPubKey& pubkey)
44  {
45  CSHA256 hasher = m_salted_hasher;
46  hasher.Write(hash.begin(), 32).Write(&pubkey[0], pubkey.size()).Write(&vchSig[0], vchSig.size()).Finalize(entry.begin());
47  }
48 
49  bool
50  Get(const uint256& entry, const bool erase)
51  {
52  boost::shared_lock<boost::shared_mutex> lock(cs_sigcache);
53  return setValid.contains(entry, erase);
54  }
55 
56  void Set(uint256& entry)
57  {
58  boost::unique_lock<boost::shared_mutex> lock(cs_sigcache);
59  setValid.insert(entry);
60  }
61  uint32_t setup_bytes(size_t n)
62  {
63  return setValid.setup_bytes(n);
64  }
65 };
66 
67 /* In previous versions of this code, signatureCache was a local static variable
68  * in CachingTransactionSignatureChecker::VerifySignature. We initialize
69  * signatureCache outside of VerifySignature to avoid the atomic operation per
70  * call overhead associated with local static variables even though
71  * signatureCache could be made local to VerifySignature.
72 */
73 static CSignatureCache signatureCache;
74 } // namespace
75 
76 // To be called once in AppInitMain/BasicTestingSetup to initialize the
77 // signatureCache.
79 {
80  // nMaxCacheSize is unsigned. If -maxsigcachesize is set to zero,
81  // setup_bytes creates the minimum possible cache (2 elements).
82  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);
83  size_t nElems = signatureCache.setup_bytes(nMaxCacheSize);
84  LogPrintf("Using %zu MiB out of %zu/2 requested for signature cache, able to store %zu elements\n",
85  (nElems*sizeof(uint256)) >>20, (nMaxCacheSize*2)>>20, nElems);
86 }
87 
88 bool CachingTransactionSignatureChecker::VerifySignature(const std::vector<unsigned char>& vchSig, const CPubKey& pubkey, const uint256& sighash) const
89 {
90  uint256 entry;
91  signatureCache.ComputeEntry(entry, sighash, vchSig, pubkey);
92  if (signatureCache.Get(entry, !store))
93  return true;
94  if (!TransactionSignatureChecker::VerifySignature(vchSig, pubkey, sighash))
95  return false;
96  if (store)
97  signatureCache.Set(entry);
98  return true;
99 }
CSHA256 & Write(const unsigned char *data, size_t len)
Definition: sha256.cpp:637
bool VerifySignature(const std::vector< unsigned char > &vchSig, const CPubKey &vchPubKey, const uint256 &sighash) const override
Definition: sigcache.cpp:88
static const unsigned int DEFAULT_MAX_SIG_CACHE_SIZE
Definition: sigcache.h:16
uint256 GetRandHash() noexcept
Definition: random.cpp:602
cache implements a cache with properties similar to a cuckoo-set.
Definition: cuckoocache.h:158
static void LogPrintf(const char *fmt, const Args &... args)
Definition: logging.h:166
unsigned char * begin()
Definition: uint256.h:57
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:78
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:123
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:463
ArgsManager gArgs
Definition: system.cpp:82
A hasher class for SHA-256.
Definition: sha256.h:13