Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

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