Skip to content

Commit c77e165

Browse files
committed
Simplify metadata and block populate(), style, comments,
1 parent 26952e7 commit c77e165

3 files changed

Lines changed: 22 additions & 87 deletions

File tree

include/bitcoin/system/chain/block.hpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,8 @@ class BC_API block
145145
code confirm(const context& ctx) const NOEXCEPT;
146146

147147
/// Populate previous outputs internal to the block.
148-
void populate() const NOEXCEPT;
149-
150-
/// Populate previous outputs and metadata.locked internal to the block.
151-
/// Execution is shortcircuited for error with that metadata.locked set.
152-
/// False if any populated prevout is immature in the block context.
153-
code populate_with_metadata(const context& ctx) const NOEXCEPT;
148+
/// Fails if any populated prevout is internally immature or locked.
149+
code populate(const context& ctx) const NOEXCEPT;
154150

155151
protected:
156152
block(stream::in::fast&& stream, bool witness) NOEXCEPT;

include/bitcoin/system/chain/prevout.hpp

Lines changed: 13 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -25,57 +25,28 @@ namespace libbitcoin {
2525
namespace system {
2626
namespace chain {
2727

28-
// Defaults are set so non-population issues usually imply invalidity.
2928
class BC_API prevout final
3029
{
3130
public:
32-
///************************************************************************
33-
/// CONSENSUS:
34-
/// A height of zero is immature (unspendable) despite unspent state.
35-
///************************************************************************
36-
union
37-
{
38-
/// Unused for coinbase.
39-
/// The confirmed height of the prevout or max_uint32.
40-
size_t prevout_height{ max_uint32 };
41-
42-
/// node: populated with database identifier for the parent tx.
43-
/// Link::terminal must derive from default of max_uint32.
44-
uint32_t parent_tx;
45-
};
31+
/// chain/node: prevout is of a coinbase tx.
32+
bool coinbase{ true };
4633

47-
///************************************************************************
48-
/// CONSENSUS:
49-
/// A mtp of max_uint32 fails locktime maturity (until time overflow).
50-
///************************************************************************
51-
union
52-
{
53-
/// Unused for coinbase.
54-
/// The median time past at confirmed prevout block or max_uint32.
55-
uint32_t median_time_past{ max_uint32 };
34+
/// chain: confirmed prevout mtp or max_uint32.
35+
uint32_t median_time_past{ max_uint32 };
5636

57-
/// node: populated with database identifier for the input/point.
58-
/// Link::terminal must derive from default of max_uint32.
59-
uint32_t point_link;
60-
};
37+
/// chain: confirmed prevout height or max_uint32.
38+
size_t prevout_height{ max_uint32 };
6139

62-
///************************************************************************
63-
/// CONSENSUS:
64-
/// An unspent coinbase collision is immature (unspendable) and spent
65-
/// collision is mature [bip30]. CB collision presumed precluded by bip34.
66-
/// This is NOT guarded by system::chain confirmation checks.
67-
///************************************************************************
68-
/// The confirmed height of the spender or max_uint32.
40+
/// chain: confirmed spender height or max_uint32.
6941
uint32_t spender_height{ max_uint32 };
7042

71-
/// node: set via block.populate() as internal spends do not require
72-
/// prevout block association for relative locktime checks. So
73-
/// median_time_past is not required as locked is determined here.
74-
bool locked{ true };
43+
/// node: database identifier for the parent tx.
44+
/// Link::terminal must align with link defaults of max_uint32.
45+
uint32_t parent_tx{ max_uint32 };
7546

76-
/// The previous output is of a coinbase transaction.
77-
/// node: populated, does not require prevout block association.
78-
bool coinbase{ true };
47+
/// node: database identifier for input/point.
48+
/// Link::terminal must align with link defaults of max_uint32.
49+
uint32_t point_link{ max_uint32 };
7950
};
8051

8152
} // namespace chain

src/chain/block.cpp

Lines changed: 7 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -751,35 +751,7 @@ bool block::is_signature_operations_limited(bool bip16,
751751
}
752752

753753
// Search is unordered, forward refs (and duplicates) caught by block.check.
754-
void block::populate() const NOEXCEPT
755-
{
756-
if (txs_->empty())
757-
return;
758-
759-
unordered_map_of_cref_point_to_output_cptr_cref points(outputs());
760-
uint32_t index{};
761-
762-
// Populate outputs hash table (coinbase included).
763-
for (auto tx = txs_->begin(); tx != txs_->end(); ++tx, index = 0)
764-
for (const auto& out: *(*tx)->outputs_ptr())
765-
points.emplace(cref_point{ (*tx)->get_hash(false), index++ }, out);
766-
767-
// Populate input prevouts from hash table.
768-
for (auto tx = std::next(txs_->begin()); tx != txs_->end(); ++tx)
769-
{
770-
for (const auto& in: *(*tx)->inputs_ptr())
771-
{
772-
// Map chain::point to cref_point for search, should optimize away.
773-
const auto point = points.find({ in->point().hash(),
774-
in->point().index() });
775-
776-
if (point != points.end())
777-
in->prevout = point->second;
778-
}
779-
}
780-
}
781-
782-
code block::populate_with_metadata(const chain::context& ctx) const NOEXCEPT
754+
code block::populate(const chain::context& ctx) const NOEXCEPT
783755
{
784756
if (txs_->empty())
785757
return error::block_success;
@@ -802,19 +774,15 @@ code block::populate_with_metadata(const chain::context& ctx) const NOEXCEPT
802774
{
803775
// Map chain::point to cref_point for search, should optimize away.
804776
const cref_point key{ in->point().hash(), in->point().index() };
805-
806777
if (const auto it = points.find(key); it != points.end())
807778
{
808-
const auto immature = (matures && in->point().hash() == self);
809-
const auto lock = (bip68 && (*tx)->is_internally_locked(*in));
810-
in->prevout = it->second;
779+
if (matures && in->point().hash() == self)
780+
return error::coinbase_maturity;
781+
782+
if (bip68 && (*tx)->is_internally_locked(*in))
783+
return error::relative_time_locked;
811784

812-
// If invalid shortcircuit population and return above error.
813-
if ((in->metadata.locked = (immature || lock)))
814-
{
815-
return immature ? error::coinbase_maturity :
816-
error::relative_time_locked;
817-
}
785+
in->prevout = it->second;
818786
}
819787
}
820788
}

0 commit comments

Comments
 (0)