Skip to content

Commit

Permalink
Merge pull request #1581 from evoskuil/master
Browse files Browse the repository at this point in the history
Add block.spends computed count of non-null inputs.
  • Loading branch information
evoskuil authored Jan 11, 2025
2 parents bebfa0e + 00be92c commit cd5ec49
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
1 change: 1 addition & 0 deletions include/bitcoin/system/chain/block.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class BC_API block
hashes transaction_hashes(bool witness) const NOEXCEPT;

/// Computed properties.
size_t spends() const NOEXCEPT;
size_t weight() const NOEXCEPT;
uint64_t fees() const NOEXCEPT;
uint64_t claim() const NOEXCEPT;
Expand Down
2 changes: 1 addition & 1 deletion include/bitcoin/system/chain/prevout.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class BC_API prevout final
size_t height;

/// database: populated with a database identifier for the parent tx.
uint64_t parent{ zero };
uint32_t parent{ zero };
};

///************************************************************************
Expand Down
15 changes: 15 additions & 0 deletions src/chain/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,21 @@ hashes block::transaction_hashes(bool witness) const NOEXCEPT
return out;
}

// computed
size_t block::spends() const NOEXCEPT
{
if (txs_->empty())
return zero;

// Overflow returns max_size_t.
const auto ins = [](size_t total, const auto& tx) NOEXCEPT
{
return ceilinged_add(total, tx->inputs());
};

return std::accumulate(std::next(txs_->begin()), txs_->end(), zero, ins);
}

// computed
hash_digest block::hash() const NOEXCEPT
{
Expand Down
11 changes: 11 additions & 0 deletions test/chain/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,17 @@ BOOST_AUTO_TEST_CASE(block__to_data__writer__expected)
// fees
// claim

BOOST_AUTO_TEST_CASE(block__spends__genesis__zero)
{
const auto genesis = settings(selection::mainnet).genesis_block;
BOOST_REQUIRE(is_zero(genesis.spends()));
}

BOOST_AUTO_TEST_CASE(block__spends__coinbase_only__zero)
{
BOOST_REQUIRE(is_zero(get_block().spends()));
}

BOOST_AUTO_TEST_CASE(block__hash__default__matches_header_hash)
{
const block instance;
Expand Down

0 comments on commit cd5ec49

Please sign in to comment.