kernel: De-globalize script execution cache hasher

Move it to the ChainstateManager class.
This commit is contained in:
TheCharlatan 2024-05-18 21:06:16 +02:00
parent 13a3661aba
commit 021d38822c
No known key found for this signature in database
GPG Key ID: 9B79B45691DB4173
2 changed files with 10 additions and 5 deletions

View File

@ -2094,8 +2094,6 @@ bool CScriptCheck::operator()() {
return VerifyScript(scriptSig, m_tx_out.scriptPubKey, witness, nFlags, CachingTransactionSignatureChecker(ptxTo, nIn, m_tx_out.nValue, cacheStore, *txdata), &error);
}
static CSHA256 g_scriptExecutionCacheHasher;
ValidationCache::ValidationCache(const size_t script_execution_cache_bytes)
{
// Setup the salted hasher
@ -2103,8 +2101,8 @@ ValidationCache::ValidationCache(const size_t script_execution_cache_bytes)
// We want the nonce to be 64 bytes long to force the hasher to process
// this chunk, which makes later hash computations more efficient. We
// just write our 32-byte entropy twice to fill the 64 bytes.
g_scriptExecutionCacheHasher.Write(nonce.begin(), 32);
g_scriptExecutionCacheHasher.Write(nonce.begin(), 32);
m_script_execution_cache_hasher.Write(nonce.begin(), 32);
m_script_execution_cache_hasher.Write(nonce.begin(), 32);
const auto [num_elems, approx_size_bytes] = m_script_execution_cache.setup_bytes(script_execution_cache_bytes);
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,
// properly commits to the scriptPubKey in the inputs view of that
// transaction).
uint256 hashCacheEntry;
CSHA256 hasher = g_scriptExecutionCacheHasher;
CSHA256 hasher = validation_cache.ScriptExecutionCacheHasher();
hasher.Write(UCharCast(tx.GetWitnessHash().begin()), 32).Write((unsigned char*)&flags, sizeof(flags)).Finalize(hashCacheEntry.begin());
AssertLockHeld(cs_main); //TODO: Remove this requirement by making CuckooCache not require external locks
if (validation_cache.m_script_execution_cache.contains(hashCacheEntry, !cacheFullScriptStore)) {

View File

@ -366,6 +366,10 @@ static_assert(std::is_nothrow_destructible_v<CScriptCheck>);
*/
class ValidationCache
{
private:
//! Pre-initialized hasher to avoid having to recreate it for every hash calculation.
CSHA256 m_script_execution_cache_hasher;
public:
CuckooCache::cache<uint256, SignatureCacheHasher> m_script_execution_cache;
@ -373,6 +377,9 @@ public:
ValidationCache(const ValidationCache&) = delete;
ValidationCache& operator=(const ValidationCache&) = delete;
//! Return a copy of the pre-initialized hasher.
CSHA256 ScriptExecutionCacheHasher() const { return m_script_execution_cache_hasher; }
};
/** Functions for validating blocks and updating the block tree */