Skip to content

Commit 7161382

Browse files
committed
Change block.populate() to bool (locked), set in.metadata.locked.
1 parent a96803c commit 7161382

5 files changed

Lines changed: 33 additions & 15 deletions

File tree

include/bitcoin/system/chain/block.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ class BC_API block
136136
code connect(const context& ctx) const NOEXCEPT;
137137
code confirm(const context& ctx) const NOEXCEPT;
138138

139-
/// Populate previous outputs (only, no metadata) internal to the block.
140-
size_t populate() const NOEXCEPT;
139+
/// Populate previous outputs (and metadata.locked) internal to the block.
140+
bool populate() const NOEXCEPT;
141141

142142
protected:
143143
block(const chain::header::cptr& header,

include/bitcoin/system/chain/input.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ class BC_API input
118118
/// Requires metadata.height and median_time_past (otherwise returns true).
119119
bool is_locked(size_t height, uint32_t median_time_past) const NOEXCEPT;
120120

121+
/// Any non-zero relative locktime value locks internally-spent input.
122+
bool is_internally_locked() const NOEXCEPT;
123+
121124
protected:
122125
input(const chain::point::cptr& point, const chain::script::cptr& script,
123126
const chain::witness::cptr& witness, uint32_t sequence,

include/bitcoin/system/chain/prevout.hpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ class BC_API prevout final
3232
/// CONSENSUS:
3333
/// A height of zero is immature (unspendable) despite unspent state.
3434
///************************************************************************
35-
3635
union
3736
{
3837
/// The confirmed chain height of the prevout (zero if not found).
3938
/// Unused if the input owning this prevout is null (coinbase).
39+
/// database: unused as validation precedes prevout block association.
4040
size_t height;
4141

4242
/// database: populated with a database identifier for the parent tx.
@@ -47,25 +47,33 @@ class BC_API prevout final
4747
/// CONSENSUS:
4848
/// A mtp of max_uint32 fails locktime maturity (until time overflow).
4949
///************************************************************************
50-
/// The median time past at height (max_uint32 if not found/confirmed).
51-
/// Unused if the input owning this prevout is null (coinbase).
52-
/// database: unused as validation precedes prevout block association.
53-
uint32_t median_time_past{ max_uint32 };
50+
union
51+
{
52+
/// The median time past at height (max_uint32 if not found/confirmed).
53+
/// Unused if the input owning this prevout is null (coinbase).
54+
/// database: unused as validation precedes prevout block association.
55+
uint32_t median_time_past{ max_uint32 };
56+
57+
/// Populated by block.populate() call as internal spends do not
58+
/// require prevout block association for relative locktime checks.
59+
/// So median_time_past is not required as locked is determined here.
60+
bool locked;
61+
};
5462

5563
///************************************************************************
5664
/// CONSENSUS:
5765
/// An unspent coinbase collision is immature (unspendable) and spent
5866
/// collision is mature (bip30). CB collision presumed precluded by bip34.
5967
///************************************************************************
60-
/// If the input owning this prevout is null (coinbase), this implies that
68+
/// If input owning this prevout is null (coinbase), this implies that
6169
/// all outputs of any duplicate txs are fully spent at height.
6270
/// If the input owning this prevout is not null (not coinbase), this
6371
/// indicates whether the prevout is spent at height (double spend).
64-
/// database: unused as validation precedes potential spend block assocs.
72+
/// database: unused as validation precedes prevout block association.
6573
bool spent{ true };
6674

6775
/// The previous output is of a coinbase transaction.
68-
/// database: populated as does not require prevout block association.
76+
/// database: populated, does not require prevout block association.
6977
bool coinbase{ false };
7078
};
7179

src/chain/block.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,7 @@ bool block::is_unspent_coinbase_collision() const NOEXCEPT
688688
}
689689

690690
// Search is not ordered, forward references are caught by block.check.
691-
size_t block::populate() const NOEXCEPT
691+
bool block::populate() const NOEXCEPT
692692
{
693693
std::unordered_map<point, output::cptr> points{};
694694
uint32_t index{};
@@ -699,22 +699,23 @@ size_t block::populate() const NOEXCEPT
699699
points.emplace(std::pair{ point{ (*tx)->hash(false), index++ },
700700
out });
701701

702-
// Populate input prevouts from hash table and obtain count.
703-
size_t count{};
702+
// Populate input prevouts from hash table and obtain locked state.
703+
bool locked{};
704704
for (auto tx = txs_->begin(); tx != txs_->end(); ++tx)
705705
{
706706
for (const auto& in: *(*tx)->inputs_ptr())
707707
{
708708
const auto point = points.find(in->point());
709709
if (point != points.end())
710710
{
711-
++count;
712711
in->prevout = point->second;
712+
in->metadata.locked = in->is_internally_locked();
713+
locked |= in->metadata.locked;
713714
}
714715
}
715716
}
716717

717-
return count;
718+
return !locked;
718719
}
719720

720721
// Delegated.

src/chain/input.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,12 @@ bool input::is_locked(size_t height, uint32_t median_time_past) const NOEXCEPT
395395
metadata.median_time_past);
396396
}
397397

398+
bool input::is_internally_locked() const NOEXCEPT
399+
{
400+
// Internal spends have zero relative height/mtp.
401+
return is_locked(sequence_, {}, {}, {}, {});
402+
}
403+
398404
bool input::reserved_hash(hash_digest& out) const NOEXCEPT
399405
{
400406
if (!witness::is_reserved_pattern(get_witness().stack()))

0 commit comments

Comments
 (0)