Bitcoin ABC 0.32.4
P2P Digital Currency
scriptcache.h
Go to the documentation of this file.
1// Copyright (c) 2017 The Bitcoin developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#ifndef BITCOIN_SCRIPT_SCRIPTCACHE_H
6#define BITCOIN_SCRIPT_SCRIPTCACHE_H
7
8#include <array>
9#include <cstdint>
10
11#include <cuckoocache.h>
12#include <uint256.h>
13#include <util/hasher.h>
14
15class CTransaction;
16
27 std::array<uint8_t, 28> data;
28
29public:
30 ScriptCacheKey() = default;
31 ScriptCacheKey(const ScriptCacheKey &rhs) = default;
32 ScriptCacheKey(const CTransaction &tx, uint32_t flags, CSHA256 &&hasher);
33
34 ScriptCacheKey &operator=(const ScriptCacheKey &rhs) = default;
35
36 bool operator==(const ScriptCacheKey &rhs) const {
37 return rhs.data == data;
38 }
39
40 friend class ScriptCacheHasher;
41};
42
54
57
58 ScriptCacheElement() = default;
59
60 ScriptCacheElement(const KeyType &keyIn, int nSigChecksIn)
61 : key(keyIn), nSigChecks(nSigChecksIn) {}
62
63 const KeyType &getKey() const { return key; }
64};
65
66static_assert(sizeof(ScriptCacheElement) == 32,
67 "ScriptCacheElement should be 32 bytes");
68
70public:
71 template <uint8_t hash_select>
72 uint32_t operator()(const ScriptCacheKey &k) const {
73 static_assert(hash_select < 8, "only has 8 hashes available.");
74
75 const auto &d = k.data;
76
77 static_assert(sizeof(d) == 28,
78 "modify the following if key size changes");
79
80 uint32_t u;
81 static_assert(sizeof(u) == 4 && sizeof(d[0]) == 1, "basic assumptions");
82 if (hash_select < 7) {
83 std::memcpy(&u, d.data() + 4 * hash_select, 4);
84 } else {
85 // We are required to produce 8 subhashes, and all bytes have
86 // been used once. We re-use the bytes but mix together different
87 // entries (and flip the order) to get a new, distinct value.
88 uint8_t arr[4];
89 arr[0] = d[3] ^ d[7] ^ d[11] ^ d[15];
90 arr[1] = d[6] ^ d[10] ^ d[14] ^ d[18];
91 arr[2] = d[9] ^ d[13] ^ d[17] ^ d[21];
92 arr[3] = d[12] ^ d[16] ^ d[20] ^ d[24];
93 std::memcpy(&u, arr, 4);
94 }
95 return u;
96 }
97};
98
99// DoS prevention: limit cache size to 32MiB (over 1000000 entries on 64-bit
100// systems). Due to how we count cache size, actual memory usage is slightly
101// more (~32.25 MiB)
102static constexpr size_t DEFAULT_SCRIPT_EXECUTION_CACHE_BYTES{32 << 20};
103
104#endif // BITCOIN_SCRIPT_SCRIPTCACHE_H
int flags
Definition: bitcoin-tx.cpp:542
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
uint32_t operator()(const ScriptCacheKey &k) const
Definition: scriptcache.h:72
The script cache is a map using a key/value element, that caches the success of executing a specific ...
Definition: scriptcache.h:26
ScriptCacheKey & operator=(const ScriptCacheKey &rhs)=default
std::array< uint8_t, 28 > data
Definition: scriptcache.h:27
ScriptCacheKey(const ScriptCacheKey &rhs)=default
bool operator==(const ScriptCacheKey &rhs) const
Definition: scriptcache.h:36
ScriptCacheKey()=default
static constexpr size_t DEFAULT_SCRIPT_EXECUTION_CACHE_BYTES
Definition: scriptcache.h:102
In future if many more values are added, it should be considered to expand the element size to 64 byt...
Definition: scriptcache.h:52
const KeyType & getKey() const
Definition: scriptcache.h:63
ScriptCacheElement(const KeyType &keyIn, int nSigChecksIn)
Definition: scriptcache.h:60
ScriptCacheElement()=default