Changeset View
Changeset View
Standalone View
Standalone View
src/script/sigcache.cpp
Show First 20 Lines • Show All 51 Lines • ▼ Show 20 Lines | void Set(uint256 &entry) { | ||||
boost::unique_lock<boost::shared_mutex> lock(cs_sigcache); | boost::unique_lock<boost::shared_mutex> lock(cs_sigcache); | ||||
setValid.insert(entry); | setValid.insert(entry); | ||||
} | } | ||||
uint32_t setup_bytes(size_t n) { return setValid.setup_bytes(n); } | uint32_t setup_bytes(size_t n) { return setValid.setup_bytes(n); } | ||||
}; | }; | ||||
/** | /** | ||||
* In previous versions of this code, signatureCache was a local static variable | * In previous versions of this code, signatureCache was a local static variable | ||||
* in CachingTransactionSignatureChecker::VerifySignature. We initialize | * in CachingTransactionSignatureChecker::VerifySignatureECDSA(). We initialize | ||||
* signatureCache outside of VerifySignature to avoid the atomic operation per | * signatureCache outside of VerifySignatureECDSA() to avoid the atomic | ||||
* call overhead associated with local static variables even though | * operation per call overhead associated with local static variables even | ||||
* signatureCache could be made local to VerifySignature. | * though signatureCache could be made local to VerifySignatureECDSA(). | ||||
*/ | */ | ||||
static CSignatureCache signatureCache; | static CSignatureCache signatureCache; | ||||
} // namespace | } // namespace | ||||
// To be called once in AppInit2/TestingSetup to initialize the signatureCache | // To be called once in AppInit2/TestingSetup to initialize the signatureCache | ||||
void InitSignatureCache() { | void InitSignatureCache() { | ||||
// nMaxCacheSize is unsigned. If -maxsigcachesize is set to zero, | // nMaxCacheSize is unsigned. If -maxsigcachesize is set to zero, | ||||
// setup_bytes creates the minimum possible cache (2 elements). | // setup_bytes creates the minimum possible cache (2 elements). | ||||
size_t nMaxCacheSize = | size_t nMaxCacheSize = | ||||
std::min(std::max(int64_t(0), gArgs.GetArg("-maxsigcachesize", | std::min(std::max(int64_t(0), gArgs.GetArg("-maxsigcachesize", | ||||
DEFAULT_MAX_SIG_CACHE_SIZE)), | DEFAULT_MAX_SIG_CACHE_SIZE)), | ||||
MAX_MAX_SIG_CACHE_SIZE) * | MAX_MAX_SIG_CACHE_SIZE) * | ||||
(size_t(1) << 20); | (size_t(1) << 20); | ||||
size_t nElems = signatureCache.setup_bytes(nMaxCacheSize); | size_t nElems = signatureCache.setup_bytes(nMaxCacheSize); | ||||
LogPrintf("Using %zu MiB out of %zu requested for signature cache, able to " | LogPrintf("Using %zu MiB out of %zu requested for signature cache, able to " | ||||
"store %zu elements\n", | "store %zu elements\n", | ||||
(nElems * sizeof(uint256)) >> 20, nMaxCacheSize >> 20, nElems); | (nElems * sizeof(uint256)) >> 20, nMaxCacheSize >> 20, nElems); | ||||
} | } | ||||
bool CachingTransactionSignatureChecker::VerifySignature( | bool CachingTransactionSignatureChecker::VerifySignatureECDSA( | ||||
const std::vector<uint8_t> &vchSig, const CPubKey &pubkey, | const std::vector<uint8_t> &vchSig, const CPubKey &pubkey, | ||||
const uint256 &sighash) const { | const uint256 &sighash) const { | ||||
uint256 entry; | uint256 entry; | ||||
signatureCache.ComputeEntry(entry, sighash, vchSig, pubkey); | signatureCache.ComputeEntry(entry, sighash, vchSig, pubkey); | ||||
if (signatureCache.Get(entry, !store)) { | if (signatureCache.Get(entry, !store)) { | ||||
return true; | return true; | ||||
} | } | ||||
if (!TransactionSignatureChecker::VerifySignature(vchSig, pubkey, | if (!TransactionSignatureChecker::VerifySignatureECDSA(vchSig, pubkey, | ||||
sighash)) { | sighash)) { | ||||
return false; | return false; | ||||
} | } | ||||
if (store) { | if (store) { | ||||
signatureCache.Set(entry); | signatureCache.Set(entry); | ||||
} | } | ||||
return true; | return true; | ||||
} | } |