Changeset View
Changeset View
Standalone View
Standalone View
src/script/sigcache.cpp
Show All 17 Lines | |||||
/** | /** | ||||
* Valid signature cache, to avoid doing expensive ECDSA signature checking | * Valid signature cache, to avoid doing expensive ECDSA signature checking | ||||
* twice for every transaction (once when accepted into memory pool, and | * twice for every transaction (once when accepted into memory pool, and | ||||
* again when accepted into the block chain) | * again when accepted into the block chain) | ||||
*/ | */ | ||||
class CSignatureCache { | class CSignatureCache { | ||||
private: | private: | ||||
//! Entries are SHA256(nonce || signature hash || public key || signature): | //! Entries are SHA256(nonce || signature hash || public key || signature): | ||||
uint256 nonce; | CSHA256 m_salted_hasher; | ||||
typedef CuckooCache::cache<CuckooCache::KeyOnly<uint256>, | typedef CuckooCache::cache<CuckooCache::KeyOnly<uint256>, | ||||
SignatureCacheHasher> | SignatureCacheHasher> | ||||
map_type; | map_type; | ||||
map_type setValid; | map_type setValid; | ||||
boost::shared_mutex cs_sigcache; | boost::shared_mutex cs_sigcache; | ||||
public: | public: | ||||
CSignatureCache() { GetRandBytes(nonce.begin(), 32); } | CSignatureCache() { | ||||
uint256 nonce = GetRandHash(); | |||||
// We want the nonce to be 64 bytes long to force the hasher to process | |||||
// this chunk, which makes later hash computations more efficient. We | |||||
// just write our 32-byte entropy twice to fill the 64 bytes. | |||||
m_salted_hasher.Write(nonce.begin(), 32); | |||||
m_salted_hasher.Write(nonce.begin(), 32); | |||||
} | |||||
void ComputeEntry(uint256 &entry, const uint256 &hash, | void ComputeEntry(uint256 &entry, const uint256 &hash, | ||||
const std::vector<uint8_t> &vchSig, | const std::vector<uint8_t> &vchSig, | ||||
const CPubKey &pubkey) { | const CPubKey &pubkey) { | ||||
CSHA256() | CSHA256 hasher = m_salted_hasher; | ||||
.Write(nonce.begin(), 32) | hasher.Write(hash.begin(), 32) | ||||
.Write(hash.begin(), 32) | |||||
.Write(&pubkey[0], pubkey.size()) | .Write(&pubkey[0], pubkey.size()) | ||||
.Write(&vchSig[0], vchSig.size()) | .Write(&vchSig[0], vchSig.size()) | ||||
.Finalize(entry.begin()); | .Finalize(entry.begin()); | ||||
} | } | ||||
bool Get(const uint256 &entry, const bool erase) { | bool Get(const uint256 &entry, const bool erase) { | ||||
boost::shared_lock<boost::shared_mutex> lock(cs_sigcache); | boost::shared_lock<boost::shared_mutex> lock(cs_sigcache); | ||||
return setValid.contains(entry, erase); | return setValid.contains(entry, erase); | ||||
▲ Show 20 Lines • Show All 67 Lines • Show Last 20 Lines |