Bitcoin ABC 0.32.4
P2P Digital Currency
sigcache.h
Go to the documentation of this file.
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-2016 The Bitcoin Core developers
3// Distributed under the MIT software license, see the accompanying
4// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6#ifndef BITCOIN_SCRIPT_SIGCACHE_H
7#define BITCOIN_SCRIPT_SIGCACHE_H
8
9#include <consensus/amount.h>
10#include <crypto/sha256.h>
11#include <cuckoocache.h>
12#include <script/interpreter.h>
13#include <uint256.h>
14#include <util/hasher.h>
15
16#include <cstddef>
17#include <shared_mutex>
18#include <vector>
19
20class CPubKey;
21class CTransaction;
22
23// DoS prevention: limit cache size to 32MiB (over 1000000 entries on 64-bit
24// systems). Due to how we count cache size, actual memory usage is slightly
25// more (~32.25 MiB)
26static constexpr size_t DEFAULT_SIGNATURE_CACHE_BYTES{32 << 20};
27
34private:
41 std::shared_mutex cs_sigcache;
42
43public:
44 SignatureCache(size_t max_size_bytes);
45
46 SignatureCache(const SignatureCache &) = delete;
48
49 void ComputeEntry(uint256 &entry, const uint256 &hash,
50 const std::vector<uint8_t> &vchSig,
51 const CPubKey &pubkey) const;
52
53 bool Get(const uint256 &entry, const bool erase);
54
55 void Set(const uint256 &entry);
56};
57
59private:
60 bool store;
62
63 bool IsCached(const std::vector<uint8_t> &vchSig, const CPubKey &vchPubKey,
64 const uint256 &sighash) const;
65
66public:
68 unsigned int nInIn,
69 const Amount amountIn, bool storeIn,
70 SignatureCache &signature_cache,
72 : TransactionSignatureChecker(txToIn, nInIn, amountIn, txdataIn),
73 store(storeIn), m_signature_cache(signature_cache) {}
74
75 bool VerifySignature(const std::vector<uint8_t> &vchSig,
76 const CPubKey &vchPubKey,
77 const uint256 &sighash) const override;
78
80};
81
82#endif // BITCOIN_SCRIPT_SIGCACHE_H
An encapsulated public key.
Definition: pubkey.h:31
A hasher class for SHA-256.
Definition: sha256.h:13
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:192
friend class TestCachingTransactionSignatureChecker
Definition: sigcache.h:79
bool VerifySignature(const std::vector< uint8_t > &vchSig, const CPubKey &vchPubKey, const uint256 &sighash) const override
Definition: sigcache.cpp:80
bool IsCached(const std::vector< uint8_t > &vchSig, const CPubKey &vchPubKey, const uint256 &sighash) const
Definition: sigcache.cpp:73
SignatureCache & m_signature_cache
Definition: sigcache.h:61
CachingTransactionSignatureChecker(const CTransaction *txToIn, unsigned int nInIn, const Amount amountIn, bool storeIn, SignatureCache &signature_cache, PrecomputedTransactionData &txdataIn)
Definition: sigcache.h:67
cache implements a cache with properties similar to a cuckoo-set.
Definition: cuckoocache.h:166
We're hashing a nonce into the entries themselves, so we don't need extra blinding in the set hash co...
Definition: hasher.h:80
Valid signature cache, to avoid doing expensive ECDSA signature checking twice for every transaction ...
Definition: sigcache.h:33
SignatureCache(size_t max_size_bytes)
Definition: sigcache.cpp:20
void ComputeEntry(uint256 &entry, const uint256 &hash, const std::vector< uint8_t > &vchSig, const CPubKey &pubkey) const
Definition: sigcache.cpp:35
map_type setValid
Definition: sigcache.h:40
CSHA256 m_salted_hasher
Entries are SHA256(nonce || signature hash || public key || signature):
Definition: sigcache.h:36
std::shared_mutex cs_sigcache
Definition: sigcache.h:41
SignatureCache & operator=(const SignatureCache &)=delete
SignatureCache(const SignatureCache &)=delete
CuckooCache::cache< CuckooCache::KeyOnly< uint256 >, SignatureCacheHasher > map_type
Definition: sigcache.h:39
bool Get(const uint256 &entry, const bool erase)
Definition: sigcache.cpp:45
void Set(const uint256 &entry)
Definition: sigcache.cpp:50
256-bit opaque blob.
Definition: uint256.h:129
static constexpr size_t DEFAULT_SIGNATURE_CACHE_BYTES
Definition: sigcache.h:26
Definition: amount.h:19
Precompute sighash midstate to avoid quadratic hashing.
Definition: transaction.h:325