You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sigcache.cpp 3.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2016 The Starwels developers
  3. // Distributed under the MIT software license, see the accompanying
  4. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  5. #include "sigcache.h"
  6. #include "memusage.h"
  7. #include "pubkey.h"
  8. #include "random.h"
  9. #include "uint256.h"
  10. #include "util.h"
  11. #include "cuckoocache.h"
  12. #include <boost/thread.hpp>
  13. namespace {
  14. /**
  15. * Valid signature cache, to avoid doing expensive ECDSA signature checking
  16. * twice for every transaction (once when accepted into memory pool, and
  17. * again when accepted into the block chain)
  18. */
  19. class CSignatureCache
  20. {
  21. private:
  22. //! Entries are SHA256(nonce || signature hash || public key || signature):
  23. uint256 nonce;
  24. typedef CuckooCache::cache<uint256, SignatureCacheHasher> map_type;
  25. map_type setValid;
  26. boost::shared_mutex cs_sigcache;
  27. public:
  28. CSignatureCache()
  29. {
  30. GetRandBytes(nonce.begin(), 32);
  31. }
  32. void
  33. ComputeEntry(uint256& entry, const uint256 &hash, const std::vector<unsigned char>& vchSig, const CPubKey& pubkey)
  34. {
  35. CSHA256().Write(nonce.begin(), 32).Write(hash.begin(), 32).Write(&pubkey[0], pubkey.size()).Write(&vchSig[0], vchSig.size()).Finalize(entry.begin());
  36. }
  37. bool
  38. Get(const uint256& entry, const bool erase)
  39. {
  40. boost::shared_lock<boost::shared_mutex> lock(cs_sigcache);
  41. return setValid.contains(entry, erase);
  42. }
  43. void Set(uint256& entry)
  44. {
  45. boost::unique_lock<boost::shared_mutex> lock(cs_sigcache);
  46. setValid.insert(entry);
  47. }
  48. uint32_t setup_bytes(size_t n)
  49. {
  50. return setValid.setup_bytes(n);
  51. }
  52. };
  53. /* In previous versions of this code, signatureCache was a local static variable
  54. * in CachingTransactionSignatureChecker::VerifySignature. We initialize
  55. * signatureCache outside of VerifySignature to avoid the atomic operation per
  56. * call overhead associated with local static variables even though
  57. * signatureCache could be made local to VerifySignature.
  58. */
  59. static CSignatureCache signatureCache;
  60. } // namespace
  61. // To be called once in AppInitMain/BasicTestingSetup to initialize the
  62. // signatureCache.
  63. void InitSignatureCache()
  64. {
  65. // nMaxCacheSize is unsigned. If -maxsigcachesize is set to zero,
  66. // setup_bytes creates the minimum possible cache (2 elements).
  67. 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);
  68. size_t nElems = signatureCache.setup_bytes(nMaxCacheSize);
  69. LogPrintf("Using %zu MiB out of %zu/2 requested for signature cache, able to store %zu elements\n",
  70. (nElems*sizeof(uint256)) >>20, (nMaxCacheSize*2)>>20, nElems);
  71. }
  72. bool CachingTransactionSignatureChecker::VerifySignature(const std::vector<unsigned char>& vchSig, const CPubKey& pubkey, const uint256& sighash) const
  73. {
  74. uint256 entry;
  75. signatureCache.ComputeEntry(entry, sighash, vchSig, pubkey);
  76. if (signatureCache.Get(entry, !store))
  77. return true;
  78. if (!TransactionSignatureChecker::VerifySignature(vchSig, pubkey, sighash))
  79. return false;
  80. if (store)
  81. signatureCache.Set(entry);
  82. return true;
  83. }