Files

110 lines
3.6 KiB
C++
Raw Permalink Normal View History

2017-11-09 19:34:51 +01:00
/*
* This file is part of the Flowee project
* Copyright (C) 2009-2010 Satoshi Nakamoto
* Copyright (C) 2009-2015 The Bitcoin Core developers
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2014-09-10 16:16:09 +02:00
#include "sigcache.h"
2021-01-05 22:05:25 +01:00
#include <hash.h>
2018-08-10 10:33:34 +03:00
#include "cuckoocache.h"
2015-10-30 23:14:38 +01:00
#include "memusage.h"
2018-02-12 14:15:24 +01:00
#include <SettingsDefaults.h>
2023-11-24 18:01:36 +01:00
#include <primitives/PublicKey.h>
2014-09-10 16:16:09 +02:00
#include "random.h"
#include "uint256.h"
#include "util.h"
#include <boost/thread.hpp>
namespace {
/**
* Valid signature cache, to avoid doing expensive ECDSA signature checking
* twice for every transaction (once when accepted into memory pool, and
* again when accepted into the block chain)
*/
2014-09-10 16:16:09 +02:00
class CSignatureCache
{
private:
2015-10-30 23:14:38 +01:00
//! Entries are SHA256(nonce || signature hash || public key || signature):
uint256 nonce;
2018-08-10 10:33:34 +03:00
typedef CuckooCache::cache<uint256, SignatureCacheHasher> map_type;
2015-10-30 23:14:38 +01:00
map_type setValid;
2014-09-10 16:16:09 +02:00
boost::shared_mutex cs_sigcache;
2015-10-30 23:14:38 +01:00
public:
CSignatureCache()
{
GetRandBytes(nonce.begin(), 32);
2014-09-10 16:16:09 +02:00
}
2015-10-30 23:14:38 +01:00
void
2022-07-06 21:56:34 +02:00
ComputeEntry(uint256& entry, const uint256 &hash, const std::vector<unsigned char>& vchSig, const PublicKey& pubkey)
2014-09-10 16:16:09 +02:00
{
CSHA256().write(nonce.begin(), 32).write(hash.begin(), 32).write(&pubkey[0], pubkey.size()).write(&vchSig[0], vchSig.size()).finalize(entry.begin());
2015-10-30 23:14:38 +01:00
}
bool
2018-08-10 10:33:34 +03:00
Get(const uint256& entry, bool erase)
2015-10-30 23:14:38 +01:00
{
boost::shared_lock<boost::shared_mutex> lock(cs_sigcache);
2018-08-10 10:33:34 +03:00
return setValid.contains(entry, erase);
2015-10-30 23:14:38 +01:00
}
2018-08-10 10:33:34 +03:00
void Set(uint256& entry)
{
boost::unique_lock<boost::shared_mutex> lock(cs_sigcache);
2015-10-30 23:14:38 +01:00
setValid.insert(entry);
2014-09-10 16:16:09 +02:00
}
2018-08-10 10:33:34 +03:00
uint32_t setup_bytes(size_t n)
{
return setValid.setup_bytes(n);
}
2014-09-10 16:16:09 +02:00
};
2018-08-10 10:33:34 +03:00
/* In previous versions of this code, signatureCache was a local static variable
* in CachingTransactionSignatureChecker::VerifySignature. We initialize
* signatureCache outside of VerifySignature to avoid the atomic operation per
* call overhead associated with local static variables even though
* signatureCache could be made local to VerifySignature.
*/
static CSignatureCache signatureCache;
}
// To be called once in AppInit2/TestingSetup to initialize the signatureCache
void InitSignatureCache() {
size_t nMaxCacheSize = GetArg("-maxsigcachesize", Settings::DefaultMaxSigCacheSize) * ((size_t) 1 << 20);
if (nMaxCacheSize <= 0) return;
size_t nElems = signatureCache.setup_bytes(nMaxCacheSize);
logInfo(Log::Bitcoin) << "Using" << (nElems * sizeof(uint256) >> 20) << "MiB out of" <<
(nMaxCacheSize >> 20) << "requested for signature cache, able to store" << nElems << "elements";
2014-09-10 16:16:09 +02:00
}
2022-07-06 21:56:34 +02:00
bool CachingTransactionSignatureChecker::VerifySignature(const std::vector<unsigned char>& vchSig, const PublicKey& pubkey, const uint256& sighash, uint32_t flags) const
2014-09-10 16:16:09 +02:00
{
2015-10-30 23:14:38 +01:00
uint256 entry;
signatureCache.ComputeEntry(entry, sighash, vchSig, pubkey);
2018-08-10 10:33:34 +03:00
if (signatureCache.Get(entry, !store))
2014-09-10 16:16:09 +02:00
return true;
2019-04-17 23:21:14 +02:00
if (!TransactionSignatureChecker::VerifySignature(vchSig, pubkey, sighash, flags))
2014-09-10 16:16:09 +02:00
return false;
2018-08-10 10:33:34 +03:00
if (store)
2015-10-30 23:14:38 +01:00
signatureCache.Set(entry);
2014-09-10 16:16:09 +02:00
return true;
}