From 125697521d8a4a7683588c90d392414cca5d68fc Mon Sep 17 00:00:00 2001 From: evoskuil Date: Sun, 9 Jun 2024 16:50:49 -0400 Subject: [PATCH] Add block/header.get_hash() to avoid hash copy when cached. --- include/bitcoin/system/chain/block.hpp | 3 +++ include/bitcoin/system/chain/header.hpp | 3 +++ include/bitcoin/system/chain/transaction.hpp | 2 +- src/chain/block.cpp | 6 ++++++ src/chain/header.cpp | 8 ++++++++ 5 files changed, 21 insertions(+), 1 deletion(-) diff --git a/include/bitcoin/system/chain/block.hpp b/include/bitcoin/system/chain/block.hpp index c8ced5e269..c63d689c85 100644 --- a/include/bitcoin/system/chain/block.hpp +++ b/include/bitcoin/system/chain/block.hpp @@ -99,6 +99,9 @@ class BC_API block size_t serialized_size(bool witness) const NOEXCEPT; size_t signature_operations(bool bip16, bool bip141) const NOEXCEPT; + /// Reference used to avoid copy, sets cache if not set (not thread safe). + const hash_digest& get_hash() const NOEXCEPT; + /// Computed malleation properties. bool is_malleable() const NOEXCEPT; bool is_malleable64() const NOEXCEPT; diff --git a/include/bitcoin/system/chain/header.hpp b/include/bitcoin/system/chain/header.hpp index b2d2a9f97f..bfc359ee92 100644 --- a/include/bitcoin/system/chain/header.hpp +++ b/include/bitcoin/system/chain/header.hpp @@ -105,6 +105,9 @@ class BC_API header /// Cache (this overrides hash() computation). void set_hash(hash_digest&& hash) const NOEXCEPT; + /// Reference used to avoid copy, sets cache if not set (not thread safe). + const hash_digest& get_hash() const NOEXCEPT; + /// Validation. /// ----------------------------------------------------------------------- /// Checkpoints and previous_block_hash are chain validation (not here). diff --git a/include/bitcoin/system/chain/transaction.hpp b/include/bitcoin/system/chain/transaction.hpp index 32c4fb41e2..db25b4bc78 100644 --- a/include/bitcoin/system/chain/transaction.hpp +++ b/include/bitcoin/system/chain/transaction.hpp @@ -119,7 +119,7 @@ class BC_API transaction /// This need not be set if the transaction is not segmented. void set_witness_hash(hash_digest&& hash) const NOEXCEPT; - /// Reference used to avoid copy, sets cache if not set. + /// Reference used to avoid copy, sets cache if not set (not thread safe). const hash_digest& get_hash(bool witness) const NOEXCEPT; /// Methods. diff --git a/src/chain/block.cpp b/src/chain/block.cpp index 455780fa60..c6d9bceeea 100644 --- a/src/chain/block.cpp +++ b/src/chain/block.cpp @@ -254,6 +254,12 @@ hash_digest block::hash() const NOEXCEPT return header_->hash(); } +// computed +const hash_digest& block::get_hash() const NOEXCEPT +{ + return header_->get_hash(); +} + // static/private block::sizes block::serialized_size( const chain::transaction_cptrs& txs) NOEXCEPT diff --git a/src/chain/header.cpp b/src/chain/header.cpp index ccbbc17c1d..1a461c886d 100644 --- a/src/chain/header.cpp +++ b/src/chain/header.cpp @@ -251,6 +251,14 @@ hash_digest header::hash() const NOEXCEPT return digest; } +const hash_digest& header::get_hash() const NOEXCEPT +{ + if (!hash_) + set_hash(hash()); + + return *hash_; +} + // static uint256_t header::proof(uint32_t bits) NOEXCEPT {