Dash Core  0.12.2.1
P2P Digital Currency
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 "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 <boost/thread.hpp>
15 #include <boost/unordered_set.hpp>
16 
17 namespace {
18 
23 class CSignatureCacheHasher
24 {
25 public:
26  size_t operator()(const uint256& key) const {
27  return key.GetCheapHash();
28  }
29 };
30 
36 class CSignatureCache
37 {
38 private:
40  uint256 nonce;
41  typedef boost::unordered_set<uint256, CSignatureCacheHasher> map_type;
42  map_type setValid;
43  boost::shared_mutex cs_sigcache;
44 
45 
46 public:
47  CSignatureCache()
48  {
49  GetRandBytes(nonce.begin(), 32);
50  }
51 
52  void
53  ComputeEntry(uint256& entry, const uint256 &hash, const std::vector<unsigned char>& vchSig, const CPubKey& pubkey)
54  {
55  CSHA256().Write(nonce.begin(), 32).Write(hash.begin(), 32).Write(&pubkey[0], pubkey.size()).Write(&vchSig[0], vchSig.size()).Finalize(entry.begin());
56  }
57 
58  bool
59  Get(const uint256& entry)
60  {
61  boost::shared_lock<boost::shared_mutex> lock(cs_sigcache);
62  return setValid.count(entry);
63  }
64 
65  void Erase(const uint256& entry)
66  {
67  boost::unique_lock<boost::shared_mutex> lock(cs_sigcache);
68  setValid.erase(entry);
69  }
70 
71  void Set(const uint256& entry)
72  {
73  size_t nMaxCacheSize = GetArg("-maxsigcachesize", DEFAULT_MAX_SIG_CACHE_SIZE) * ((size_t) 1 << 20);
74  if (nMaxCacheSize <= 0) return;
75 
76  boost::unique_lock<boost::shared_mutex> lock(cs_sigcache);
77  while (memusage::DynamicUsage(setValid) > nMaxCacheSize)
78  {
79  map_type::size_type s = GetRand(setValid.bucket_count());
80  map_type::local_iterator it = setValid.begin(s);
81  if (it != setValid.end(s)) {
82  setValid.erase(*it);
83  }
84  }
85 
86  setValid.insert(entry);
87  }
88 };
89 
90 }
91 
92 bool CachingTransactionSignatureChecker::VerifySignature(const std::vector<unsigned char>& vchSig, const CPubKey& pubkey, const uint256& sighash) const
93 {
94  static CSignatureCache signatureCache;
95 
96  uint256 entry;
97  signatureCache.ComputeEntry(entry, sighash, vchSig, pubkey);
98 
99  if (signatureCache.Get(entry)) {
100  if (!store) {
101  signatureCache.Erase(entry);
102  }
103  return true;
104  }
105 
106  if (!TransactionSignatureChecker::VerifySignature(vchSig, pubkey, sighash))
107  return false;
108 
109  if (store) {
110  signatureCache.Set(entry);
111  }
112  return true;
113 }
unsigned int size() const
Simple read-only vector-like interface to the pubkey data.
Definition: pubkey.h:95
static const unsigned int DEFAULT_MAX_SIG_CACHE_SIZE
Definition: sigcache.h:15
static size_t DynamicUsage(const int8_t &v)
Definition: memusage.h:25
unsigned char * begin()
Definition: uint256.h:55
CSHA256 & Write(const unsigned char *data, size_t len)
Definition: sha256.cpp:141
virtual bool VerifySignature(const std::vector< unsigned char > &vchSig, const CPubKey &vchPubKey, const uint256 &sighash) const
void GetRandBytes(unsigned char *buf, int num)
Definition: random.cpp:86
Definition: pubkey.h:37
bool VerifySignature(const std::vector< unsigned char > &vchSig, const CPubKey &vchPubKey, const uint256 &sighash) const
Definition: sigcache.cpp:92
std::string GetArg(const std::string &strArg, const std::string &strDefault)
Definition: util.cpp:441
Definition: sha256.h:12
uint64_t GetRand(uint64_t nMax)
Definition: random.cpp:94