Merge #9578: Add missing mempool lock for CalculateMemPoolAncestors
[bitcoinplatinum.git] / src / script / sigcache.cpp
blob09bedc54605d46a333c5f148fc5219c37e3f147b
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.
6 #include "sigcache.h"
8 #include "memusage.h"
9 #include "pubkey.h"
10 #include "random.h"
11 #include "uint256.h"
12 #include "util.h"
14 #include "cuckoocache.h"
15 #include <boost/thread.hpp>
17 namespace {
19 /**
20 * We're hashing a nonce into the entries themselves, so we don't need extra
21 * blinding in the set hash computation.
23 * This may exhibit platform endian dependent behavior but because these are
24 * nonced hashes (random) and this state is only ever used locally it is safe.
25 * All that matters is local consistency.
27 class SignatureCacheHasher
29 public:
30 template <uint8_t hash_select>
31 uint32_t operator()(const uint256& key) const
33 static_assert(hash_select <8, "SignatureCacheHasher only has 8 hashes available.");
34 uint32_t u;
35 std::memcpy(&u, key.begin()+4*hash_select, 4);
36 return u;
40 /**
41 * Valid signature cache, to avoid doing expensive ECDSA signature checking
42 * twice for every transaction (once when accepted into memory pool, and
43 * again when accepted into the block chain)
45 class CSignatureCache
47 private:
48 //! Entries are SHA256(nonce || signature hash || public key || signature):
49 uint256 nonce;
50 typedef CuckooCache::cache<uint256, SignatureCacheHasher> map_type;
51 map_type setValid;
52 boost::shared_mutex cs_sigcache;
54 public:
55 CSignatureCache()
57 GetRandBytes(nonce.begin(), 32);
60 void
61 ComputeEntry(uint256& entry, const uint256 &hash, const std::vector<unsigned char>& vchSig, const CPubKey& pubkey)
63 CSHA256().Write(nonce.begin(), 32).Write(hash.begin(), 32).Write(&pubkey[0], pubkey.size()).Write(&vchSig[0], vchSig.size()).Finalize(entry.begin());
66 bool
67 Get(const uint256& entry, const bool erase)
69 boost::shared_lock<boost::shared_mutex> lock(cs_sigcache);
70 return setValid.contains(entry, erase);
73 void Set(uint256& entry)
75 boost::unique_lock<boost::shared_mutex> lock(cs_sigcache);
76 setValid.insert(entry);
78 uint32_t setup_bytes(size_t n)
80 return setValid.setup_bytes(n);
84 /* In previous versions of this code, signatureCache was a local static variable
85 * in CachingTransactionSignatureChecker::VerifySignature. We initialize
86 * signatureCache outside of VerifySignature to avoid the atomic operation per
87 * call overhead associated with local static variables even though
88 * signatureCache could be made local to VerifySignature.
90 static CSignatureCache signatureCache;
93 // To be called once in AppInit2/TestingSetup to initialize the signatureCache
94 void InitSignatureCache()
96 size_t nMaxCacheSize = GetArg("-maxsigcachesize", DEFAULT_MAX_SIG_CACHE_SIZE) * ((size_t) 1 << 20);
97 if (nMaxCacheSize <= 0) return;
98 size_t nElems = signatureCache.setup_bytes(nMaxCacheSize);
99 LogPrintf("Using %zu MiB out of %zu requested for signature cache, able to store %zu elements\n",
100 (nElems*sizeof(uint256)) >>20, nMaxCacheSize>>20, nElems);
103 bool CachingTransactionSignatureChecker::VerifySignature(const std::vector<unsigned char>& vchSig, const CPubKey& pubkey, const uint256& sighash) const
105 uint256 entry;
106 signatureCache.ComputeEntry(entry, sighash, vchSig, pubkey);
107 if (signatureCache.Get(entry, !store))
108 return true;
109 if (!TransactionSignatureChecker::VerifySignature(vchSig, pubkey, sighash))
110 return false;
111 if (store)
112 signatureCache.Set(entry);
113 return true;