Skip to content

Commit 021d388

Browse files
committed
kernel: De-globalize script execution cache hasher
Move it to the ChainstateManager class.
1 parent 13a3661 commit 021d388

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

src/validation.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2094,17 +2094,15 @@ bool CScriptCheck::operator()() {
20942094
return VerifyScript(scriptSig, m_tx_out.scriptPubKey, witness, nFlags, CachingTransactionSignatureChecker(ptxTo, nIn, m_tx_out.nValue, cacheStore, *txdata), &error);
20952095
}
20962096

2097-
static CSHA256 g_scriptExecutionCacheHasher;
2098-
20992097
ValidationCache::ValidationCache(const size_t script_execution_cache_bytes)
21002098
{
21012099
// Setup the salted hasher
21022100
uint256 nonce = GetRandHash();
21032101
// We want the nonce to be 64 bytes long to force the hasher to process
21042102
// this chunk, which makes later hash computations more efficient. We
21052103
// just write our 32-byte entropy twice to fill the 64 bytes.
2106-
g_scriptExecutionCacheHasher.Write(nonce.begin(), 32);
2107-
g_scriptExecutionCacheHasher.Write(nonce.begin(), 32);
2104+
m_script_execution_cache_hasher.Write(nonce.begin(), 32);
2105+
m_script_execution_cache_hasher.Write(nonce.begin(), 32);
21082106

21092107
const auto [num_elems, approx_size_bytes] = m_script_execution_cache.setup_bytes(script_execution_cache_bytes);
21102108
LogPrintf("Using %zu MiB out of %zu MiB requested for script execution cache, able to store %zu elements\n",
@@ -2148,7 +2146,7 @@ bool CheckInputScripts(const CTransaction& tx, TxValidationState& state,
21482146
// properly commits to the scriptPubKey in the inputs view of that
21492147
// transaction).
21502148
uint256 hashCacheEntry;
2151-
CSHA256 hasher = g_scriptExecutionCacheHasher;
2149+
CSHA256 hasher = validation_cache.ScriptExecutionCacheHasher();
21522150
hasher.Write(UCharCast(tx.GetWitnessHash().begin()), 32).Write((unsigned char*)&flags, sizeof(flags)).Finalize(hashCacheEntry.begin());
21532151
AssertLockHeld(cs_main); //TODO: Remove this requirement by making CuckooCache not require external locks
21542152
if (validation_cache.m_script_execution_cache.contains(hashCacheEntry, !cacheFullScriptStore)) {

src/validation.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,13 +366,20 @@ static_assert(std::is_nothrow_destructible_v<CScriptCheck>);
366366
*/
367367
class ValidationCache
368368
{
369+
private:
370+
//! Pre-initialized hasher to avoid having to recreate it for every hash calculation.
371+
CSHA256 m_script_execution_cache_hasher;
372+
369373
public:
370374
CuckooCache::cache<uint256, SignatureCacheHasher> m_script_execution_cache;
371375

372376
ValidationCache(size_t script_execution_cache_bytes);
373377

374378
ValidationCache(const ValidationCache&) = delete;
375379
ValidationCache& operator=(const ValidationCache&) = delete;
380+
381+
//! Return a copy of the pre-initialized hasher.
382+
CSHA256 ScriptExecutionCacheHasher() const { return m_script_execution_cache_hasher; }
376383
};
377384

378385
/** Functions for validating blocks and updating the block tree */

0 commit comments

Comments
 (0)