Changeset View
Changeset View
Standalone View
Standalone View
src/script/scriptcache.h
// Copyright (c) 2017 - The Bitcoin Developers | // Copyright (c) 2017 - The Bitcoin Developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#ifndef BITCOIN_SCRIPT_SCRIPTCACHE_H | #ifndef BITCOIN_SCRIPT_SCRIPTCACHE_H | ||||
#define BITCOIN_SCRIPT_SCRIPTCACHE_H | #define BITCOIN_SCRIPT_SCRIPTCACHE_H | ||||
#include <uint256.h> | #include <array> | ||||
#include <cstdint> | #include <cstdint> | ||||
class CTransaction; | class CTransaction; | ||||
/** | |||||
* The script cache is a map using a key/value element. | |||||
* The key is slightly shorter than a power-of-two size to make room for | |||||
* the value. | |||||
*/ | |||||
struct ScriptCacheKey { | |||||
std::array<uint8_t, 30> data; | |||||
deadalnix: make that private. You can make it friend with ScriptCacheHasher if you need the data or simply… | |||||
markblundebergAuthorUnsubmitted Done Inline ActionsYeah I would have to make GetScriptCacheKey a friend too so I made it into a constructor instead. markblundeberg: Yeah I would have to make GetScriptCacheKey a friend too so I made it into a constructor… | |||||
deadalnixUnsubmitted Not Done Inline ActionsVery good call. deadalnix: Very good call. | |||||
ScriptCacheKey() = default; | |||||
ScriptCacheKey(const ScriptCacheKey &rhs) = default; | |||||
bool operator==(const ScriptCacheKey &rhs) const { | |||||
return rhs.data == data; | |||||
} | |||||
}; | |||||
// DoS prevention: limit cache size to 32MB (over 1000000 entries on 64-bit | // DoS prevention: limit cache size to 32MB (over 1000000 entries on 64-bit | ||||
// systems). Due to how we count cache size, actual memory usage is slightly | // systems). Due to how we count cache size, actual memory usage is slightly | ||||
// more (~32.25 MB) | // more (~32.25 MB) | ||||
static const unsigned int DEFAULT_MAX_SCRIPT_CACHE_SIZE = 32; | static const unsigned int DEFAULT_MAX_SCRIPT_CACHE_SIZE = 32; | ||||
// Maximum sig cache size allowed | // Maximum sig cache size allowed | ||||
static const int64_t MAX_MAX_SCRIPT_CACHE_SIZE = 16384; | static const int64_t MAX_MAX_SCRIPT_CACHE_SIZE = 16384; | ||||
/** Initializes the script-execution cache */ | /** Initializes the script-execution cache */ | ||||
void InitScriptExecutionCache(); | void InitScriptExecutionCache(); | ||||
/** Compute the cache key for a given transaction and flags. */ | /** Compute the cache key for a given transaction and flags. */ | ||||
uint256 GetScriptCacheKey(const CTransaction &tx, uint32_t flags); | ScriptCacheKey GetScriptCacheKey(const CTransaction &tx, uint32_t flags); | ||||
/** Check if a given key is in the cache, and if so, return its values. */ | |||||
bool IsKeyInScriptCache(ScriptCacheKey key, bool erase, int &nSigChecksRet); | |||||
markblundebergAuthorUnsubmitted Done Inline ActionsMaybe this should return a struct { bool, val1, val2, val3 ... } instead. Thoughts? markblundeberg: Maybe this should return a struct { bool, val1, val2, val3 ... } instead. Thoughts? | |||||
/** Check if a given key is in the cache. */ | constexpr int SCRIPT_CACHE_MAX_SIGCHECKS = UINT16_MAX; | ||||
bool IsKeyInScriptCache(uint256 key, bool erase); | |||||
/** Add an entry in the cache. */ | /** Add an entry in the cache. The sigchecks count must be in the range | ||||
void AddKeyInScriptCache(uint256 key); | * 0-SCRIPT_CACHE_MAX_SIGCHECKS inclusive, otherwise a cache entry must not | ||||
* be added (if successfully added, it would be corrupt data, and possibly | |||||
* lead to consensus failure when the cache is consulted during block | |||||
* validation). | |||||
*/ | |||||
deadalnixUnsubmitted Done Inline ActionsFormating deadalnix: Formating | |||||
void AddKeyInScriptCache(ScriptCacheKey key, int nSigChecks); | |||||
#endif // BITCOIN_SCRIPT_SCRIPTCACHE_H | #endif // BITCOIN_SCRIPT_SCRIPTCACHE_H |
make that private. You can make it friend with ScriptCacheHasher if you need the data or simply have a const accessor to the data. There are no reason for anyone to modify the key provided by GetScriptCacheKey under any circumstance, so allowing it is just allowing bugs.