Bitcoin ABC  0.24.7
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 // 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 <cuckoocache.h>
9 #include <pubkey.h>
10 #include <random.h>
11 #include <uint256.h>
12 #include <util/system.h>
13 
14 #include <boost/thread/shared_mutex.hpp>
15 
16 namespace {
17 
23 class CSignatureCache {
24 private:
26  CSHA256 m_salted_hasher;
29  map_type;
30  map_type setValid;
31  boost::shared_mutex cs_sigcache;
32 
33 public:
34  CSignatureCache() {
35  uint256 nonce = GetRandHash();
36  // We want the nonce to be 64 bytes long to force the hasher to process
37  // this chunk, which makes later hash computations more efficient. We
38  // just write our 32-byte entropy twice to fill the 64 bytes.
39  m_salted_hasher.Write(nonce.begin(), 32);
40  m_salted_hasher.Write(nonce.begin(), 32);
41  }
42 
43  void ComputeEntry(uint256 &entry, const uint256 &hash,
44  const std::vector<uint8_t> &vchSig,
45  const CPubKey &pubkey) {
46  CSHA256 hasher = m_salted_hasher;
47  hasher.Write(hash.begin(), 32)
48  .Write(&pubkey[0], pubkey.size())
49  .Write(&vchSig[0], vchSig.size())
50  .Finalize(entry.begin());
51  }
52 
53  bool Get(const uint256 &entry, const bool erase) {
54  boost::shared_lock<boost::shared_mutex> lock(cs_sigcache);
55  return setValid.contains(entry, erase);
56  }
57 
58  void Set(uint256 &entry) {
59  boost::unique_lock<boost::shared_mutex> lock(cs_sigcache);
60  setValid.insert(entry);
61  }
62  uint32_t setup_bytes(size_t n) { return setValid.setup_bytes(n); }
63 };
64 
72 static CSignatureCache signatureCache;
73 } // namespace
74 
75 // To be called once in AppInitMain/BasicTestingSetup to initialize the
76 // signatureCache.
78  // nMaxCacheSize is unsigned. If -maxsigcachesize is set to zero,
79  // setup_bytes creates the minimum possible cache (2 elements).
80  size_t nMaxCacheSize =
81  std::min(std::max(int64_t(0), gArgs.GetArg("-maxsigcachesize",
84  (size_t(1) << 20);
85  size_t nElems = signatureCache.setup_bytes(nMaxCacheSize);
86  LogPrintf("Using %zu MiB out of %zu requested for signature cache, able to "
87  "store %zu elements\n",
88  (nElems * sizeof(uint256)) >> 20, nMaxCacheSize >> 20, nElems);
89 }
90 
91 template <typename F>
92 bool RunMemoizedCheck(const std::vector<uint8_t> &vchSig, const CPubKey &pubkey,
93  const uint256 &sighash, bool storeOrErase, const F &fun) {
94  uint256 entry;
95  signatureCache.ComputeEntry(entry, sighash, vchSig, pubkey);
96  if (signatureCache.Get(entry, !storeOrErase)) {
97  return true;
98  }
99  if (!fun()) {
100  return false;
101  }
102  if (storeOrErase) {
103  signatureCache.Set(entry);
104  }
105  return true;
106 }
107 
109  const std::vector<uint8_t> &vchSig, const CPubKey &pubkey,
110  const uint256 &sighash) const {
111  return RunMemoizedCheck(vchSig, pubkey, sighash, true,
112  [] { return false; });
113 }
114 
116  const std::vector<uint8_t> &vchSig, const CPubKey &pubkey,
117  const uint256 &sighash) const {
118  return RunMemoizedCheck(vchSig, pubkey, sighash, store, [&] {
119  return TransactionSignatureChecker::VerifySignature(vchSig, pubkey,
120  sighash);
121  });
122 }
RunMemoizedCheck
bool RunMemoizedCheck(const std::vector< uint8_t > &vchSig, const CPubKey &pubkey, const uint256 &sighash, bool storeOrErase, const F &fun)
Definition: sigcache.cpp:92
SignatureCacheHasher
We're hashing a nonce into the entries themselves, so we don't need extra blinding in the set hash co...
Definition: sigcache.h:30
CuckooCache::cache
cache implements a cache with properties similar to a cuckoo-set.
Definition: cuckoocache.h:162
uint256.h
BaseSignatureChecker::VerifySignature
virtual bool VerifySignature(const std::vector< uint8_t > &vchSig, const CPubKey &vchPubKey, const uint256 &sighash) const
Definition: interpreter.cpp:1646
pubkey.h
DEFAULT_MAX_SIG_CACHE_SIZE
static const unsigned int DEFAULT_MAX_SIG_CACHE_SIZE
Definition: sigcache.h:16
MAX_MAX_SIG_CACHE_SIZE
static const int64_t MAX_MAX_SIG_CACHE_SIZE
Definition: sigcache.h:18
CachingTransactionSignatureChecker::IsCached
bool IsCached(const std::vector< uint8_t > &vchSig, const CPubKey &vchPubKey, const uint256 &sighash) const
Definition: sigcache.cpp:108
cuckoocache.h
random.h
CachingTransactionSignatureChecker::store
bool store
Definition: sigcache.h:44
CSHA256::Finalize
void Finalize(uint8_t hash[OUTPUT_SIZE])
Definition: sha256.cpp:844
GetRandHash
uint256 GetRandHash() noexcept
Definition: random.cpp:658
ArgsManager::GetArg
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: system.cpp:498
CPubKey::size
unsigned int size() const
Simple read-only vector-like interface to the pubkey data.
Definition: pubkey.h:98
uint256
256-bit opaque blob.
Definition: uint256.h:127
base_blob::begin
uint8_t * begin()
Definition: uint256.h:83
InitSignatureCache
void InitSignatureCache()
Definition: sigcache.cpp:77
system.h
CPubKey
An encapsulated public key.
Definition: pubkey.h:31
gArgs
ArgsManager gArgs
Definition: system.cpp:75
CSHA256
A hasher class for SHA-256.
Definition: sha256.h:13
CSHA256::Write
CSHA256 & Write(const uint8_t *data, size_t len)
Definition: sha256.cpp:819
sigcache.h
LogPrintf
static void LogPrintf(const char *fmt, const Args &... args)
Definition: logging.h:175
CachingTransactionSignatureChecker::VerifySignature
bool VerifySignature(const std::vector< uint8_t > &vchSig, const CPubKey &vchPubKey, const uint256 &sighash) const override
Definition: sigcache.cpp:115