Skip to content

Commit 70841e4

Browse files
committed
Split out block::populate_with_metadata from block::populate.
1 parent 4074620 commit 70841e4

2 files changed

Lines changed: 37 additions & 6 deletions

File tree

include/bitcoin/system/chain/block.hpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,12 @@ class BC_API block
138138
code connect(const context& ctx) const NOEXCEPT;
139139
code confirm(const context& ctx) const NOEXCEPT;
140140

141-
/// Populate previous outputs (and metadata.locked) internal to the block.
142-
/// False if one or more populated prevouts is locked in the block context.
143-
bool populate(const context& ctx) const NOEXCEPT;
141+
/// Populate previous outputs internal to the block.
142+
void populate() const NOEXCEPT;
143+
144+
/// Populate previous outputs and metadata.locked internal to the block.
145+
/// False if any populated prevout is immature in the block context.
146+
bool populate_with_metadata(const context& ctx) const NOEXCEPT;
144147

145148
protected:
146149
block(const chain::header::cptr& header,

src/chain/block.cpp

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,35 @@ bool block::is_unspent_coinbase_collision() const NOEXCEPT
710710
}
711711

712712
// Search is unordered, forward refs (and duplicates) caught by block.check.
713-
bool block::populate(const chain::context& ctx) const NOEXCEPT
713+
void block::populate() const NOEXCEPT
714+
{
715+
if (txs_->empty())
716+
return;
717+
718+
unordered_map_of_cref_point_to_output_cptr_cref points{ outputs() };
719+
uint32_t index{};
720+
721+
// Populate outputs hash table (coinbase included).
722+
for (auto tx = txs_->begin(); tx != txs_->end(); ++tx, index = 0)
723+
for (const auto& out: *(*tx)->outputs_ptr())
724+
points.emplace(cref_point{ (*tx)->get_hash(false), index++ }, out);
725+
726+
// Populate input prevouts from hash table.
727+
for (auto tx = std::next(txs_->begin()); tx != txs_->end(); ++tx)
728+
{
729+
for (const auto& in: *(*tx)->inputs_ptr())
730+
{
731+
// Map chain::point to cref_point for search, should optimize away.
732+
const auto point = points.find({ in->point().hash(),
733+
in->point().index() });
734+
735+
if (point != points.end())
736+
in->prevout = point->second;
737+
}
738+
}
739+
}
740+
741+
bool block::populate_with_metadata(const chain::context& ctx) const NOEXCEPT
714742
{
715743
if (txs_->empty())
716744
return true;
@@ -724,7 +752,7 @@ bool block::populate(const chain::context& ctx) const NOEXCEPT
724752
for (const auto& out: *(*tx)->outputs_ptr())
725753
points.emplace(cref_point{ (*tx)->get_hash(false), index++ }, out);
726754

727-
// Populate input prevouts from hash table and obtain locked state.
755+
// Populate input prevouts from hash table and obtain maturity.
728756
auto locked = false;
729757
for (auto tx = std::next(txs_->begin()); tx != txs_->end(); ++tx)
730758
{
@@ -736,7 +764,7 @@ bool block::populate(const chain::context& ctx) const NOEXCEPT
736764

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

0 commit comments

Comments
 (0)