From 896185d7ed3fa23415424c608f0897d6139640f4 Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Sat, 4 May 2013 16:10:09 +0200 Subject: [PATCH] Make signature cache store CPubKeys --- src/allocators.h | 4 ++++ src/key.h | 7 +++---- src/script.cpp | 20 ++++++++++++-------- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/allocators.h b/src/allocators.h index b4490ce27..85af8fe37 100644 --- a/src/allocators.h +++ b/src/allocators.h @@ -176,6 +176,10 @@ private: {} }; +// +// Functions for directly locking/unlocking memory objects. +// Intended for non-dynamically allocated structures. +// template void LockObject(const T &t) { LockedPageManager::instance.LockRange((void*)(&t), sizeof(T)); } diff --git a/src/key.h b/src/key.h index b9ecd4857..ce469ad29 100644 --- a/src/key.h +++ b/src/key.h @@ -84,7 +84,7 @@ public: Set(vch.begin(), vch.end()); } - // Simply read-only vector-like interface to the pubkey data. + // Simple read-only vector-like interface to the pubkey data. unsigned int size() const { return GetLen(vch[0]); } const unsigned char *begin() const { return vch; } const unsigned char *end() const { return vch+size(); } @@ -109,12 +109,11 @@ public: } template void Serialize(Stream &s, int nType, int nVersion) const { unsigned int len = size(); - ::Serialize(s, VARINT(len), nType, nVersion); + ::WriteCompactSize(s, len); s.write((char*)vch, len); } template void Unserialize(Stream &s, int nType, int nVersion) { - unsigned int len; - ::Unserialize(s, VARINT(len), nType, nVersion); + unsigned int len = ::ReadCompactSize(s); if (len <= 65) { s.read((char*)vch, len); } else { diff --git a/src/script.cpp b/src/script.cpp index 2c7fd5987..b41166635 100644 --- a/src/script.cpp +++ b/src/script.cpp @@ -16,7 +16,7 @@ using namespace boost; #include "sync.h" #include "util.h" -bool CheckSig(vector vchSig, vector vchPubKey, CScript scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType, int flags); +bool CheckSig(vector vchSig, const vector &vchPubKey, const CScript &scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType, int flags); @@ -1033,13 +1033,13 @@ class CSignatureCache { private: // sigdata_type is (signature hash, signature, public key): - typedef boost::tuple, std::vector > sigdata_type; + typedef boost::tuple, CPubKey> sigdata_type; std::set< sigdata_type> setValid; boost::shared_mutex cs_sigcache; public: bool - Get(uint256 hash, const std::vector& vchSig, const std::vector& pubKey) + Get(const uint256 &hash, const std::vector& vchSig, const CPubKey& pubKey) { boost::shared_lock lock(cs_sigcache); @@ -1050,7 +1050,7 @@ public: return false; } - void Set(uint256 hash, const std::vector& vchSig, const std::vector& pubKey) + void Set(const uint256 &hash, const std::vector& vchSig, const CPubKey& pubKey) { // DoS prevention: limit cache size to less than 10MB // (~200 bytes per cache entry times 50,000 entries) @@ -1081,11 +1081,15 @@ public: } }; -bool CheckSig(vector vchSig, vector vchPubKey, CScript scriptCode, +bool CheckSig(vector vchSig, const vector &vchPubKey, const CScript &scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType, int flags) { static CSignatureCache signatureCache; + CPubKey pubkey(vchPubKey); + if (!pubkey.IsValid()) + return false; + // Hash type is one byte tacked on to the end of the signature if (vchSig.empty()) return false; @@ -1097,14 +1101,14 @@ bool CheckSig(vector vchSig, vector vchPubKey, CSc uint256 sighash = SignatureHash(scriptCode, txTo, nIn, nHashType); - if (signatureCache.Get(sighash, vchSig, vchPubKey)) + if (signatureCache.Get(sighash, vchSig, pubkey)) return true; - if (!CPubKey(vchPubKey).Verify(sighash, vchSig)) + if (!pubkey.Verify(sighash, vchSig)) return false; if (!(flags & SCRIPT_VERIFY_NOCACHE)) - signatureCache.Set(sighash, vchSig, vchPubKey); + signatureCache.Set(sighash, vchSig, pubkey); return true; }