diff --git a/src/script/scriptcache.h b/src/script/scriptcache.h --- a/src/script/scriptcache.h +++ b/src/script/scriptcache.h @@ -5,12 +5,31 @@ #ifndef BITCOIN_SCRIPT_SCRIPTCACHE_H #define BITCOIN_SCRIPT_SCRIPTCACHE_H -#include - +#include #include 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. + */ +class ScriptCacheKey { + std::array data; + +public: + ScriptCacheKey() = default; + ScriptCacheKey(const ScriptCacheKey &rhs) = default; + ScriptCacheKey(const CTransaction &tx, uint32_t flags); + + bool operator==(const ScriptCacheKey &rhs) const { + return rhs.data == data; + } + + friend class ScriptCacheHasher; +}; + // 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 // more (~32.25 MB) @@ -21,13 +40,20 @@ /** Initializes the script-execution cache */ void InitScriptExecutionCache(); -/** Compute the cache key for a given transaction and flags. */ -uint256 GetScriptCacheKey(const CTransaction &tx, uint32_t flags); - -/** Check if a given key is in the cache. */ -bool IsKeyInScriptCache(uint256 key, bool erase); - -/** Add an entry in the cache. */ -void AddKeyInScriptCache(uint256 key); +/** Check if a given key is in the cache, and if so, return its values. + * (if not found nSigChecksRet may or may not be set to an arbitrary value) + */ +bool IsKeyInScriptCache(ScriptCacheKey key, bool erase, int &nSigChecksRet); + +constexpr int SCRIPT_CACHE_MAX_SIGCHECKS = UINT16_MAX; + +/** + * Add an entry in the cache. The sigchecks count must be in the range + * 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). + */ +void AddKeyInScriptCache(ScriptCacheKey key, int nSigChecks); #endif // BITCOIN_SCRIPT_SCRIPTCACHE_H diff --git a/src/script/scriptcache.cpp b/src/script/scriptcache.cpp --- a/src/script/scriptcache.cpp +++ b/src/script/scriptcache.cpp @@ -10,10 +10,71 @@ #include #include