Skip to content

Commit 103fac3

Browse files
authored
Merge pull request #1766 from evoskuil/master
Fix block.fees() (use cb.spend, not cb.value).
2 parents a2c40ad + c8542e4 commit 103fac3

File tree

4 files changed

+49
-50
lines changed

4 files changed

+49
-50
lines changed

include/bitcoin/system/chain/transaction.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ class BC_API transaction
105105
/// Computed properties.
106106
size_t weight() const NOEXCEPT;
107107
uint64_t fee() const NOEXCEPT;
108-
uint64_t claim() const NOEXCEPT;
108+
uint64_t spend() const NOEXCEPT;
109109
uint64_t value() const NOEXCEPT;
110110
bool is_coinbase() const NOEXCEPT;
111111
bool is_segregated() const NOEXCEPT;

src/chain/block.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -669,26 +669,25 @@ static uint64_t block_subsidy(size_t height, uint64_t subsidy_interval,
669669

670670
// Prevouts required.
671671

672+
// The fees() is the sum of all transaction fees (coinbase is zero).
672673
uint64_t block::fees() const NOEXCEPT
673674
{
674-
if (txs_->empty())
675-
return {};
676-
677675
// Overflow returns max_uint64.
678676
const auto value = [](uint64_t total, const auto& tx) NOEXCEPT
679677
{
680678
return ceilinged_add(total, tx->fee());
681679
};
682680

683-
return std::accumulate(std::next(txs_->begin()), txs_->end(),
684-
uint64_t{}, value);
681+
return std::accumulate(txs_->begin(), txs_->end(), 0_u64, value);
685682
}
686683

684+
// The claim() is the spend of the coinbase transaction.
687685
uint64_t block::claim() const NOEXCEPT
688686
{
689-
return txs_->empty() ? zero : txs_->front()->value();
687+
return txs_->empty() ? zero : txs_->front()->spend();
690688
}
691689

690+
// The reward() is the sum of all transaction.fee() and the block subsidy.
692691
uint64_t block::reward(size_t height, uint64_t subsidy_interval,
693692
uint64_t initial_block_subsidy_satoshi, bool bip42) const NOEXCEPT
694693
{

src/chain/transaction.cpp

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -347,8 +347,8 @@ const outputs_cptr& transaction::outputs_ptr() const NOEXCEPT
347347
uint64_t transaction::fee() const NOEXCEPT
348348
{
349349
// Underflow returns zero (and is_overspent() will be true).
350-
// This is value of prevouts spent by inputs minus that claimed by outputs.
351-
return floored_subtract(value(), claim());
350+
// The value of prevouts referenced by inputs minus that spent by outputs.
351+
return floored_subtract(value(), spend());
352352
}
353353

354354
// Methods.
@@ -650,36 +650,39 @@ bool transaction::is_missing_prevouts() const NOEXCEPT
650650
return std::any_of(inputs_->begin(), inputs_->end(), missing);
651651
}
652652

653-
uint64_t transaction::claim() const NOEXCEPT
653+
// The value() is the sum of own inputs.
654+
uint64_t transaction::value() const NOEXCEPT
654655
{
655-
// Overflow returns max_uint64.
656-
const auto sum = [](uint64_t total, const auto& output) NOEXCEPT
656+
// Overflow returns max_uint64. Not populated/coinbase return zero.
657+
const auto sum = [](uint64_t total, const auto& input) NOEXCEPT
657658
{
658-
return ceilinged_add(total, output->value());
659+
const auto value = input->prevout ? input->prevout->value() : zero;
660+
return ceilinged_add(total, value);
659661
};
660662

661-
// The amount claimed by outputs.
662-
return std::accumulate(outputs_->begin(), outputs_->end(), 0_u64, sum);
663+
// The amount referenced by inputs.
664+
return std::accumulate(inputs_->begin(), inputs_->end(), 0_u64, sum);
663665
}
664666

665-
uint64_t transaction::value() const NOEXCEPT
667+
// The spend() is the sum of own outputs.
668+
uint64_t transaction::spend() const NOEXCEPT
666669
{
667-
// Overflow, not populated, and coinbase (default) return max_uint64.
668-
const auto sum = [](uint64_t total, const auto& input) NOEXCEPT
670+
// Overflow returns max_uint64.
671+
const auto sum = [](uint64_t total, const auto& output) NOEXCEPT
669672
{
670-
const auto value = input->prevout ? input->prevout->value() : max_uint64;
671-
return ceilinged_add(total, value);
673+
return ceilinged_add(total, output->value());
672674
};
673675

674-
// The amount of prevouts (referenced by inputs).
675-
return std::accumulate(inputs_->begin(), inputs_->end(), 0_u64, sum);
676+
// The amount spent by outputs.
677+
return std::accumulate(outputs_->begin(), outputs_->end(), 0_u64, sum);
676678
}
677679

680+
// Overspent is invalid (spend exceeds value), while underspent is the fee().
678681
bool transaction::is_overspent() const NOEXCEPT
679682
{
680683
BC_ASSERT(!is_coinbase());
681684

682-
return claim() > value();
685+
return spend() > value();
683686
}
684687

685688
constexpr bool is_non_coinbase_mature(size_t tx_height, size_t height) NOEXCEPT

test/chain/transaction.cpp

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ BOOST_AUTO_TEST_CASE(transaction__fee__empty__zero)
425425
BOOST_REQUIRE_EQUAL(instance.fee(), 0u);
426426
}
427427

428-
BOOST_AUTO_TEST_CASE(transaction__fee__default_input__max_uint64)
428+
BOOST_AUTO_TEST_CASE(transaction__fee__default_input__zero)
429429
{
430430
const transaction instance
431431
{
@@ -435,8 +435,7 @@ BOOST_AUTO_TEST_CASE(transaction__fee__default_input__max_uint64)
435435
0
436436
};
437437

438-
// floored_subtract(max_uint64, 0)
439-
BOOST_REQUIRE_EQUAL(instance.fee(), max_uint64);
438+
BOOST_REQUIRE_EQUAL(instance.fee(), zero);
440439
}
441440

442441
BOOST_AUTO_TEST_CASE(transaction__fee__default_output__zero)
@@ -449,17 +448,16 @@ BOOST_AUTO_TEST_CASE(transaction__fee__default_output__zero)
449448
0
450449
};
451450

452-
// floored_subtract(0, max_uint64)
453451
BOOST_REQUIRE_EQUAL(instance.fee(), 0u);
454452
}
455453

456-
BOOST_AUTO_TEST_CASE(transaction__fee__nonempty__outputs_minus_inputs)
454+
BOOST_AUTO_TEST_CASE(transaction__fee__nonempty__prevouts_minus_outputs)
457455
{
458456
constexpr uint64_t value0 = 123;
459457
constexpr uint64_t value1 = 321;
460-
constexpr uint64_t claim0 = 11;
461-
constexpr uint64_t claim1 = 11;
462-
constexpr uint64_t claim2 = 22;
458+
constexpr uint64_t spend0 = 11;
459+
constexpr uint64_t spend1 = 11;
460+
constexpr uint64_t spend2 = 22;
463461

464462
input input0;
465463
input input1;
@@ -471,41 +469,41 @@ BOOST_AUTO_TEST_CASE(transaction__fee__nonempty__outputs_minus_inputs)
471469
0,
472470
{ input0, input1 },
473471
{
474-
{ claim0, script{} },
475-
{ claim1, script{} },
476-
{ claim2, script{} }
472+
{ spend0, script{} },
473+
{ spend1, script{} },
474+
{ spend2, script{} }
477475
},
478476
0
479477
};
480478

481-
// floored_subtract(values, claims)
482-
BOOST_REQUIRE_EQUAL(instance.fee(), value0 + value1 - claim0 - claim1 - claim2);
479+
// floored_subtract(values, spend)
480+
BOOST_REQUIRE_EQUAL(instance.fee(), value0 + value1 - spend0 - spend1 - spend2);
483481
}
484482

485-
BOOST_AUTO_TEST_CASE(transaction__claim__empty_outputs__zero)
483+
BOOST_AUTO_TEST_CASE(transaction__spend__empty_outputs__zero)
486484
{
487485
transaction instance;
488-
BOOST_REQUIRE_EQUAL(instance.claim(), 0u);
486+
BOOST_REQUIRE_EQUAL(instance.spend(), 0u);
489487
}
490488

491-
BOOST_AUTO_TEST_CASE(transaction__claim__two_outputs__sum)
489+
BOOST_AUTO_TEST_CASE(transaction__spend__two_outputs__sum)
492490
{
493-
const uint64_t claim0 = 123;
494-
const uint64_t claim1 = 321;
491+
constexpr uint64_t spend0{ 123 };
492+
constexpr uint64_t spend1{ 321 };
495493
const transaction instance
496494
{
497495
0,
498496
{},
499497
{
500-
{ claim0, script{} },
501-
{ claim1, script{} }
498+
{ spend0, script{} },
499+
{ spend1, script{} }
502500
},
503501
0
504502
};
505503

506504

507-
// ceilinged_add(claim0, claim1)
508-
BOOST_REQUIRE_EQUAL(instance.claim(), claim0 + claim1);
505+
// ceilinged_add(spend0, spend1)
506+
BOOST_REQUIRE_EQUAL(instance.spend(), spend0 + spend1);
509507
}
510508

511509
BOOST_AUTO_TEST_CASE(transaction__value__no_inputs__zero)
@@ -514,7 +512,7 @@ BOOST_AUTO_TEST_CASE(transaction__value__no_inputs__zero)
514512
BOOST_REQUIRE_EQUAL(instance.value(), 0u);
515513
}
516514

517-
BOOST_AUTO_TEST_CASE(transaction__value__default_input2__max_uint64)
515+
BOOST_AUTO_TEST_CASE(transaction__value__default_input2__zero)
518516
{
519517
transaction instance
520518
{
@@ -524,14 +522,13 @@ BOOST_AUTO_TEST_CASE(transaction__value__default_input2__max_uint64)
524522
0
525523
};
526524

527-
// ceilinged_add(max_uint64, max_uint64)
528-
BOOST_REQUIRE_EQUAL(instance.value(), max_uint64);
525+
BOOST_REQUIRE_EQUAL(instance.value(), zero);
529526
}
530527

531528
BOOST_AUTO_TEST_CASE(transaction__value__two_prevouts__sum)
532529
{
533-
constexpr uint64_t value0 = 123;
534-
constexpr uint64_t value1 = 321;
530+
constexpr uint64_t value0{ 123 };
531+
constexpr uint64_t value1{ 321 };
535532

536533
const input input0;
537534
const input input1;

0 commit comments

Comments
 (0)