Raven Core  3.0.0
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 // Copyright (c) 2017-2019 The Raven Core developers
4 // Distributed under the MIT software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
7 #include "sigcache.h"
8 
9 #include "memusage.h"
10 #include "pubkey.h"
11 #include "random.h"
12 #include "uint256.h"
13 #include "util.h"
14 
15 #include "cuckoocache.h"
16 #include <boost/thread.hpp>
17 
18 namespace {
24 class CSignatureCache
25 {
26 private:
28  uint256 nonce;
30  map_type setValid;
31  boost::shared_mutex cs_sigcache;
32 
33 public:
34  CSignatureCache()
35  {
36  GetRandBytes(nonce.begin(), 32);
37  }
38 
39  void
40  ComputeEntry(uint256& entry, const uint256 &hash, const std::vector<unsigned char>& vchSig, const CPubKey& pubkey)
41  {
42  CSHA256().Write(nonce.begin(), 32).Write(hash.begin(), 32).Write(&pubkey[0], pubkey.size()).Write(&vchSig[0], vchSig.size()).Finalize(entry.begin());
43  }
44 
45  bool
46  Get(const uint256& entry, const bool erase)
47  {
48  boost::shared_lock<boost::shared_mutex> lock(cs_sigcache);
49  return setValid.contains(entry, erase);
50  }
51 
52  void Set(uint256& entry)
53  {
54  boost::unique_lock<boost::shared_mutex> lock(cs_sigcache);
55  setValid.insert(entry);
56  }
57  uint32_t setup_bytes(size_t n)
58  {
59  return setValid.setup_bytes(n);
60  }
61 };
62 
63 /* In previous versions of this code, signatureCache was a local static variable
64  * in CachingTransactionSignatureChecker::VerifySignature. We initialize
65  * signatureCache outside of VerifySignature to avoid the atomic operation per
66  * call overhead associated with local static variables even though
67  * signatureCache could be made local to VerifySignature.
68 */
69 static CSignatureCache signatureCache;
70 } // namespace
71 
72 // To be called once in AppInitMain/BasicTestingSetup to initialize the
73 // signatureCache.
75 {
76  // nMaxCacheSize is unsigned. If -maxsigcachesize is set to zero,
77  // setup_bytes creates the minimum possible cache (2 elements).
78  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);
79  size_t nElems = signatureCache.setup_bytes(nMaxCacheSize);
80  LogPrintf("Using %zu MiB out of %zu/2 requested for signature cache, able to store %zu elements\n",
81  (nElems*sizeof(uint256)) >>20, (nMaxCacheSize*2)>>20, nElems);
82 }
83 
84 bool CachingTransactionSignatureChecker::VerifySignature(const std::vector<unsigned char>& vchSig, const CPubKey& pubkey, const uint256& sighash) const
85 {
86  uint256 entry;
87  signatureCache.ComputeEntry(entry, sighash, vchSig, pubkey);
88  if (signatureCache.Get(entry, !store))
89  return true;
90  if (!TransactionSignatureChecker::VerifySignature(vchSig, pubkey, sighash))
91  return false;
92  if (store)
93  signatureCache.Set(entry);
94  return true;
95 }
CSHA256 & Write(const unsigned char *data, size_t len)
Definition: sha256.cpp:202
bool VerifySignature(const std::vector< unsigned char > &vchSig, const CPubKey &vchPubKey, const uint256 &sighash) const override
Definition: sigcache.cpp:84
virtual bool VerifySignature(const std::vector< unsigned char > &vchSig, const CPubKey &vchPubKey, const uint256 &sighash) const
cache implements a cache with properties similar to a cuckoo-set
Definition: cuckoocache.h:160
unsigned char * begin()
Definition: uint256.h:57
#define LogPrintf(...)
Definition: util.h:149
An encapsulated public key.
Definition: pubkey.h:40
void InitSignatureCache()
Definition: sigcache.cpp:74
unsigned int size() const
Simple read-only vector-like interface to the pubkey data.
Definition: pubkey.h:98
256-bit opaque blob.
Definition: uint256.h:123
ArgsManager gArgs
Definition: util.cpp:94
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: util.cpp:454
void GetRandBytes(unsigned char *buf, int num)
Functions to gather random data via the OpenSSL PRNG.
Definition: random.cpp:274
A hasher class for SHA-256.
Definition: sha256.h:14