Skip to content

Commit 62b929f

Browse files
committed
Be explicit about time locks, drop is_internal_lock()/friend tx.
1 parent f9f309c commit 62b929f

4 files changed

Lines changed: 25 additions & 30 deletions

File tree

include/bitcoin/system/chain/input.hpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class BC_API input
4444
typedef std::shared_ptr<const input> cptr;
4545

4646
static bool is_relative_locktime_applied(uint32_t sequence) NOEXCEPT;
47-
static bool is_locked(uint32_t sequence, size_t height,
47+
static bool is_relative_locked(uint32_t sequence, size_t height,
4848
uint32_t median_time_past, size_t prevout_height,
4949
uint32_t prevout_median_time_past) NOEXCEPT;
5050

@@ -117,17 +117,14 @@ class BC_API input
117117
size_t signature_operations(bool bip16, bool bip141) const NOEXCEPT;
118118

119119
/// Requires metadata.height and median_time_past (otherwise returns true).
120-
bool is_locked(size_t height, uint32_t median_time_past) const NOEXCEPT;
120+
bool is_relative_locked(size_t height,
121+
uint32_t median_time_past) const NOEXCEPT;
121122

122123
protected:
123124
input(const chain::point::cptr& point, const chain::script::cptr& script,
124125
const chain::witness::cptr& witness, uint32_t sequence,
125126
bool valid) NOEXCEPT;
126127

127-
/// Any non-zero relative locktime value locks internally-spent input.
128-
friend class transaction;
129-
bool is_internal_lock() const NOEXCEPT;
130-
131128
private:
132129
typedef struct { size_t nominal; size_t witnessed; } sizes;
133130

include/bitcoin/system/chain/transaction.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ class BC_API transaction
134134
bool is_dusty(uint64_t minimum_output_value) const NOEXCEPT;
135135

136136
/// Requires no metadata, true if spend in own block would be locked.
137-
bool is_internal_lock(const input& in) const NOEXCEPT;
137+
bool is_internally_locked(const input& in) const NOEXCEPT;
138138

139139
/// Assumes coinbase if prevout not populated (returns only legacy sigops).
140140
size_t signature_operations(bool bip16, bool bip141) const NOEXCEPT;
@@ -204,7 +204,7 @@ class BC_API transaction
204204
/// Check (requires context).
205205
/// -----------------------------------------------------------------------
206206

207-
bool is_non_final(size_t height, uint32_t timestamp,
207+
bool is_absolute_locked(size_t height, uint32_t timestamp,
208208
uint32_t median_time_past, bool bip113) const NOEXCEPT;
209209

210210
/// Accept (requires prevouts).
@@ -220,7 +220,8 @@ class BC_API transaction
220220
/// -----------------------------------------------------------------------
221221

222222
/// Requires input.metadata.height/median_time_past (prevout confirmation).
223-
bool is_locked(size_t height, uint32_t median_time_past) const NOEXCEPT;
223+
bool is_relative_locked(size_t height,
224+
uint32_t median_time_past) const NOEXCEPT;
224225

225226
/// Requires input.metadata.height (prevout confirmation).
226227
bool is_immature(size_t height) const NOEXCEPT;

src/chain/input.cpp

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ bool input::is_relative_locktime_applied(uint32_t sequence) NOEXCEPT
399399
}
400400

401401
// static
402-
bool input::is_locked(uint32_t sequence, size_t height,
402+
bool input::is_relative_locked(uint32_t sequence, size_t height,
403403
uint32_t median_time_past, size_t prevout_height,
404404
uint32_t prevout_median_time_past) NOEXCEPT
405405
{
@@ -412,29 +412,26 @@ bool input::is_locked(uint32_t sequence, size_t height,
412412
// BIP68: bit 22 determines if relative lock is time or block based.
413413
if (get_right(sequence, relative_locktime_time_locked_bit))
414414
{
415+
// BIP68: references to median time past are as defined by bip113.
415416
// BIP68: change sequence to seconds by shift up by 9 bits (x 512).
416417
auto time = shift_left(blocks, relative_locktime_seconds_shift_left);
417418
auto age = floored_subtract(median_time_past, prevout_median_time_past);
418419
return age < time;
419420
}
420421

422+
// BIP68: when the relative lock time is block based, it is interpreted as
423+
// a minimum block height constraint over the age of the input.
421424
const auto age = floored_subtract(height, prevout_height);
422425
return age < blocks;
423426
}
424427

425-
bool input::is_locked(size_t height, uint32_t median_time_past) const NOEXCEPT
428+
bool input::is_relative_locked(size_t height,
429+
uint32_t median_time_past) const NOEXCEPT
426430
{
427431
// Prevout must be found and height/median_time_past metadata populated.
428432
////BC_ASSERT(!is_zero(metadata.height));
429-
return is_locked(sequence_, height, median_time_past, metadata.height,
430-
metadata.median_time_past);
431-
}
432-
433-
// protected (tx friend)
434-
bool input::is_internal_lock() const NOEXCEPT
435-
{
436-
// Internal spends have no relative height/mtp (any metadata values work).
437-
return is_locked(metadata.height, metadata.median_time_past);
433+
return is_relative_locked(sequence_, height, median_time_past,
434+
metadata.height, metadata.median_time_past);
438435
}
439436

440437
bool input::reserved_hash(hash_digest& out) const NOEXCEPT

src/chain/transaction.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,7 +1047,7 @@ bool transaction::is_invalid_coinbase_size() const NOEXCEPT
10471047
// Accept (contextual).
10481048
// ----------------------------------------------------------------------------
10491049

1050-
bool transaction::is_non_final(size_t height, uint32_t timestamp,
1050+
bool transaction::is_absolute_locked(size_t height, uint32_t timestamp,
10511051
uint32_t median_time_past, bool bip113) const NOEXCEPT
10521052
{
10531053
// BIP113: comparing the locktime against the median of the past 11 block
@@ -1153,7 +1153,7 @@ bool transaction::is_relative_locktime_applied(bool coinbase, uint32_t version,
11531153
(version >= relative_locktime_min_version);
11541154
}
11551155

1156-
bool transaction::is_internal_lock(const input& in) const NOEXCEPT
1156+
bool transaction::is_internally_locked(const input& in) const NOEXCEPT
11571157
{
11581158
// BIP68: not applied to the sequence of the input of a coinbase.
11591159
BC_ASSERT(!is_coinbase());
@@ -1162,10 +1162,12 @@ bool transaction::is_internal_lock(const input& in) const NOEXCEPT
11621162
if (version_ < relative_locktime_min_version)
11631163
return false;
11641164

1165-
return in.is_internal_lock();
1165+
// Internal spends have no relative height/mtp (own metadata vs. itself).
1166+
return in.is_relative_locked(in.metadata.height,
1167+
in.metadata.median_time_past);
11661168
}
11671169

1168-
bool transaction::is_locked(size_t height,
1170+
bool transaction::is_relative_locked(size_t height,
11691171
uint32_t median_time_past) const NOEXCEPT
11701172
{
11711173
// BIP68: not applied to the sequence of the input of a coinbase.
@@ -1178,11 +1180,9 @@ bool transaction::is_locked(size_t height,
11781180
// BIP68: references to median time past are as defined by bip113.
11791181
const auto locked = [=](const auto& input) NOEXCEPT
11801182
{
1181-
return input->is_locked(height, median_time_past);
1183+
return input->is_relative_locked(height, median_time_past);
11821184
};
11831185

1184-
// BIP68: when the relative lock time is block based, it is interpreted as
1185-
// a minimum block height constraint over the age of the input.
11861186
return std::any_of(inputs_->begin(), inputs_->end(), locked);
11871187
}
11881188

@@ -1289,8 +1289,8 @@ code transaction::check(const context& ctx) const NOEXCEPT
12891289
{
12901290
const auto bip113 = ctx.is_enabled(bip113_rule);
12911291

1292-
if (is_non_final(ctx.height, ctx.timestamp, ctx.median_time_past, bip113))
1293-
return error::transaction_non_final;
1292+
if (is_absolute_locked(ctx.height, ctx.timestamp, ctx.median_time_past, bip113))
1293+
return error::absolute_time_locked;
12941294

12951295
return error::transaction_success;
12961296
}
@@ -1325,7 +1325,7 @@ code transaction::confirm(const context& ctx) const NOEXCEPT
13251325

13261326
if (is_coinbase())
13271327
return error::transaction_success;
1328-
if (bip68 && is_locked(ctx.height, ctx.median_time_past))
1328+
if (bip68 && is_relative_locked(ctx.height, ctx.median_time_past))
13291329
return error::relative_time_locked;
13301330
if (is_immature(ctx.height))
13311331
return error::coinbase_maturity;

0 commit comments

Comments
 (0)