Skip to content

Commit

Permalink
Merge pull request #1478 from evoskuil/master
Browse files Browse the repository at this point in the history
Add block/header.get_hash() to avoid hash copy when cached.
  • Loading branch information
evoskuil authored Jun 9, 2024
2 parents b70f885 + 1256975 commit 3eb6c84
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 1 deletion.
3 changes: 3 additions & 0 deletions include/bitcoin/system/chain/block.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions include/bitcoin/system/chain/header.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
2 changes: 1 addition & 1 deletion include/bitcoin/system/chain/transaction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions src/chain/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions src/chain/header.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down

0 comments on commit 3eb6c84

Please sign in to comment.