Skip to content

Commit

Permalink
Make signature cache store CPubKeys
Browse files Browse the repository at this point in the history
  • Loading branch information
sipa committed May 30, 2013
1 parent dfa23b9 commit 896185d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
4 changes: 4 additions & 0 deletions src/allocators.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ class LockedPageManager: public LockedPageManagerBase<MemoryPageLocker>
{}
};

//
// Functions for directly locking/unlocking memory objects.
// Intended for non-dynamically allocated structures.
//
template<typename T> void LockObject(const T &t) {
LockedPageManager::instance.LockRange((void*)(&t), sizeof(T));
}
Expand Down
7 changes: 3 additions & 4 deletions src/key.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class CPubKey {
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(); }
Expand All @@ -109,12 +109,11 @@ class CPubKey {
}
template<typename Stream> 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<typename Stream> 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 {
Expand Down
20 changes: 12 additions & 8 deletions src/script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ using namespace boost;
#include "sync.h"
#include "util.h"

bool CheckSig(vector<unsigned char> vchSig, vector<unsigned char> vchPubKey, CScript scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType, int flags);
bool CheckSig(vector<unsigned char> vchSig, const vector<unsigned char> &vchPubKey, const CScript &scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType, int flags);



Expand Down Expand Up @@ -1033,13 +1033,13 @@ class CSignatureCache
{
private:
// sigdata_type is (signature hash, signature, public key):
typedef boost::tuple<uint256, std::vector<unsigned char>, std::vector<unsigned char> > sigdata_type;
typedef boost::tuple<uint256, std::vector<unsigned char>, CPubKey> sigdata_type;
std::set< sigdata_type> setValid;
boost::shared_mutex cs_sigcache;

public:
bool
Get(uint256 hash, const std::vector<unsigned char>& vchSig, const std::vector<unsigned char>& pubKey)
Get(const uint256 &hash, const std::vector<unsigned char>& vchSig, const CPubKey& pubKey)
{
boost::shared_lock<boost::shared_mutex> lock(cs_sigcache);

Expand All @@ -1050,7 +1050,7 @@ class CSignatureCache
return false;
}

void Set(uint256 hash, const std::vector<unsigned char>& vchSig, const std::vector<unsigned char>& pubKey)
void Set(const uint256 &hash, const std::vector<unsigned char>& vchSig, const CPubKey& pubKey)
{
// DoS prevention: limit cache size to less than 10MB
// (~200 bytes per cache entry times 50,000 entries)
Expand Down Expand Up @@ -1081,11 +1081,15 @@ class CSignatureCache
}
};

bool CheckSig(vector<unsigned char> vchSig, vector<unsigned char> vchPubKey, CScript scriptCode,
bool CheckSig(vector<unsigned char> vchSig, const vector<unsigned char> &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;
Expand All @@ -1097,14 +1101,14 @@ bool CheckSig(vector<unsigned char> vchSig, vector<unsigned char> 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;
}
Expand Down

0 comments on commit 896185d

Please sign in to comment.