From 97b53e0b89281d33d0a37cc268a634715812dafb Mon Sep 17 00:00:00 2001 From: sanket1729 Date: Tue, 17 Aug 2021 20:38:16 +0000 Subject: [PATCH] Cache Input/Output ScriptPubkeys sha256 --- src/script/interpreter.cpp | 31 +++++++++++++++++++++++++++++++ src/script/interpreter.h | 6 +++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/script/interpreter.cpp b/src/script/interpreter.cpp index dd9bcdb0e2..a793e5be08 100644 --- a/src/script/interpreter.cpp +++ b/src/script/interpreter.cpp @@ -1941,6 +1941,35 @@ uint256 GetSpentScriptsSHA256(const std::vector& outputs_spent) return ss.GetSHA256(); } +/** Compute the vector where each element is SHA256 of scriptPubKeys spent by a tx. */ +std::vector GetSpentScriptPubKeysSHA256(const std::vector& outputs_spent) +{ + std::vector spent_spk_single_hashes; + spent_spk_single_hashes.reserve(outputs_spent.size()); + for (const auto& txout : outputs_spent) { + // Normal serialization using the << operater would also serialize the length, therefore we directly write using CSHA256 + uint256 spent_spk_single_hash; + CSHA256().Write(txout.scriptPubKey.data(), txout.scriptPubKey.size()).Finalize(spent_spk_single_hash.data()); + spent_spk_single_hashes.push_back(std::move(spent_spk_single_hash)); + } + return spent_spk_single_hashes; +} + +/** Compute the vector where each element is SHA256 of output scriptPubKey of a tx. */ +template +std::vector GetOutputScriptPubKeysSHA256(const T& txTo) +{ + std::vector out_spk_single_hashes; + out_spk_single_hashes.reserve(txTo.vout.size()); + for (const auto& txout : txTo.vout) { + // Normal serialization using the << operater would also serialize the length, therefore we directly write using CSHA256 + uint256 out_spk_single_hash; + CSHA256().Write(txout.scriptPubKey.data(), txout.scriptPubKey.size()).Finalize(out_spk_single_hash.data()); + out_spk_single_hashes.push_back(std::move(out_spk_single_hash)); + } + return out_spk_single_hashes; +} + template uint256 GetRangeproofsHash(const T& txTo) { CHashWriter ss(SER_GETHASH, 0); @@ -2012,6 +2041,8 @@ void PrecomputedTransactionData::Init(const T& txTo, std::vector&& spent m_issuance_rangeproofs_single_hash = GetIssuanceRangeproofsSHA256(txTo); m_output_witnesses_single_hash = GetOutputWitnessesSHA256(txTo); m_spent_scripts_single_hash = GetSpentScriptsSHA256(m_spent_outputs); + m_spent_output_spk_single_hashes = GetSpentScriptPubKeysSHA256(m_spent_outputs); + m_output_spk_single_hashes = GetOutputScriptPubKeysSHA256(txTo); m_bip341_taproot_ready = true; } } diff --git a/src/script/interpreter.h b/src/script/interpreter.h index c452c5dc29..e30ebe0ce5 100644 --- a/src/script/interpreter.h +++ b/src/script/interpreter.h @@ -173,7 +173,9 @@ struct PrecomputedTransactionData uint256 m_issuances_single_hash; uint256 m_output_witnesses_single_hash; uint256 m_issuance_rangeproofs_single_hash; - //! Whether the 10 fields above are initialized. + //! transaction outputs scriptpubkey sha single hash + std::vector m_output_spk_single_hashes; + //! Whether the 11 fields above are initialized. bool m_bip341_taproot_ready = false; // BIP143 precomputed data (double-SHA256). @@ -182,6 +184,8 @@ struct PrecomputedTransactionData bool m_bip143_segwit_ready = false; std::vector m_spent_outputs; + //! ELEMENTS: spent outputs scriptpubkey sha single hash + std::vector m_spent_output_spk_single_hashes; //! Whether m_spent_outputs is initialized. bool m_spent_outputs_ready = false;