Skip to content

Commit cd5ec49

Browse files
authored
Merge pull request libbitcoin#1581 from evoskuil/master
Add block.spends computed count of non-null inputs.
2 parents bebfa0e + 00be92c commit cd5ec49

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed

include/bitcoin/system/chain/block.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ class BC_API block
9090
hashes transaction_hashes(bool witness) const NOEXCEPT;
9191

9292
/// Computed properties.
93+
size_t spends() const NOEXCEPT;
9394
size_t weight() const NOEXCEPT;
9495
uint64_t fees() const NOEXCEPT;
9596
uint64_t claim() const NOEXCEPT;

include/bitcoin/system/chain/prevout.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class BC_API prevout final
4040
size_t height;
4141

4242
/// database: populated with a database identifier for the parent tx.
43-
uint64_t parent{ zero };
43+
uint32_t parent{ zero };
4444
};
4545

4646
///************************************************************************

src/chain/block.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,21 @@ hashes block::transaction_hashes(bool witness) const NOEXCEPT
251251
return out;
252252
}
253253

254+
// computed
255+
size_t block::spends() const NOEXCEPT
256+
{
257+
if (txs_->empty())
258+
return zero;
259+
260+
// Overflow returns max_size_t.
261+
const auto ins = [](size_t total, const auto& tx) NOEXCEPT
262+
{
263+
return ceilinged_add(total, tx->inputs());
264+
};
265+
266+
return std::accumulate(std::next(txs_->begin()), txs_->end(), zero, ins);
267+
}
268+
254269
// computed
255270
hash_digest block::hash() const NOEXCEPT
256271
{

test/chain/block.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,17 @@ BOOST_AUTO_TEST_CASE(block__to_data__writer__expected)
440440
// fees
441441
// claim
442442

443+
BOOST_AUTO_TEST_CASE(block__spends__genesis__zero)
444+
{
445+
const auto genesis = settings(selection::mainnet).genesis_block;
446+
BOOST_REQUIRE(is_zero(genesis.spends()));
447+
}
448+
449+
BOOST_AUTO_TEST_CASE(block__spends__coinbase_only__zero)
450+
{
451+
BOOST_REQUIRE(is_zero(get_block().spends()));
452+
}
453+
443454
BOOST_AUTO_TEST_CASE(block__hash__default__matches_header_hash)
444455
{
445456
const block instance;

0 commit comments

Comments
 (0)