Skip to content

Commit

Permalink
Merge pull request #1589 from evoskuil/master
Browse files Browse the repository at this point in the history
Treat internal cb spend (immature) as locked in block.prevout().
  • Loading branch information
evoskuil authored Jan 22, 2025
2 parents d9ea76b + 8ccb445 commit 9775634
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/chain/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -698,25 +698,24 @@ bool block::is_unspent_coinbase_collision() const NOEXCEPT
return !txs_->front()->inputs_ptr()->front()->metadata.spent;
}

// Search is not ordered, forward references are caught by block.check.
// Search is unordered, forward refs (and duplicates) caught by block.check.
bool block::populate(const chain::context& ctx) const NOEXCEPT
{
if (txs_->empty())
return true;

const auto start = std::next(txs_->begin());
const auto bip68 = ctx.is_enabled(chain::flags::bip68_rule);
unordered_map_of_cref_point_to_output_cptr_cref points{ outputs() };
uint32_t index{};

// Populate outputs hash table.
for (auto tx = start; tx != txs_->end(); ++tx, index = 0)
// Populate outputs hash table (coinbase included).
for (auto tx = txs_->begin(); tx != txs_->end(); ++tx, index = 0)
for (const auto& out: *(*tx)->outputs_ptr())
points.emplace(cref_point{ (*tx)->get_hash(false), index++ }, out);

// Populate input prevouts from hash table and obtain locked state.
auto locked = false;
for (auto tx = start; tx != txs_->end(); ++tx)
for (auto tx = std::next(txs_->begin()); tx != txs_->end(); ++tx)
{
for (const auto& in: *(*tx)->inputs_ptr())
{
Expand All @@ -726,8 +725,13 @@ bool block::populate(const chain::context& ctx) const NOEXCEPT

if (point != points.end())
{
// Zero maturity coinbase spend is treated as locked.
const auto lock = (bip68 && (*tx)->is_internal_lock(*in));
const auto immature = !is_zero(coinbase_maturity) &&
(in->point().hash() == txs_->front()->get_hash(false));

in->prevout = point->second;
in->metadata.locked = bip68 && (*tx)->is_internal_lock(*in);
in->metadata.locked = immature || lock;
locked |= in->metadata.locked;
}
}
Expand Down

0 comments on commit 9775634

Please sign in to comment.