Skip to content

Commit f8aa659

Browse files
committed
Use the new relative::LockTime module
Implement `check_older` using the new `relative::LockTime` API. This code seems upside down, I'm not sure I have the naming correct, i.e., I don't know which (`self` or arg) is doing the satisfying and which is satisfied? I believe the `<=` are going the correct direction, its the naming and which sequence value to call the 'lock' that I'm struggling with.
1 parent 4a0a87b commit f8aa659

File tree

11 files changed

+84
-94
lines changed

11 files changed

+84
-94
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ use-serde = ["serde", "bitcoin/serde"]
2222
rand = ["bitcoin/rand"]
2323

2424
[dependencies]
25-
bitcoin = { version = "0.29.0", default-features = false }
25+
bitcoin = { path = "/home/tobin/build/github.com/tcharding/rust-bitcoin", default-features = false }
2626
serde = { version = "1.0", optional = true }
2727
hashbrown = { version = "0.11", optional = true }
2828

2929
[dev-dependencies]
3030
bitcoind = { git = "https://github.com/tcharding/bitcoind", branch = "dev-build", features=["22_0"] }
3131
actual-rand = { package = "rand", version = "0.8.4"}
3232
secp256k1 = {version = "0.24.0", features = ["rand-std"]}
33-
bitcoin = { version = "0.29.0", features = ["rand"] }
33+
bitcoin = { path = "/home/tobin/build/github.com/tcharding/rust-bitcoin", features = ["rand"] }
3434

3535
[[example]]
3636
name = "htlc"

src/interpreter/mod.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use core::str::FromStr;
2525
use bitcoin::blockdata::witness::Witness;
2626
use bitcoin::hashes::{hash160, ripemd160, sha256};
2727
use bitcoin::util::{sighash, taproot};
28-
use bitcoin::{self, secp256k1, LockTime, Sequence, TxOut};
28+
use bitcoin::{self, secp256k1, absolute, Sequence, TxOut};
2929

3030
use crate::miniscript::context::NoChecks;
3131
use crate::miniscript::ScriptContext;
@@ -49,7 +49,7 @@ pub struct Interpreter<'txin> {
4949
/// is the leaf script; for key-spends it is `None`.
5050
script_code: Option<bitcoin::Script>,
5151
age: Sequence,
52-
lock_time: LockTime,
52+
lock_time: absolute::LockTime,
5353
}
5454

5555
// A type representing functions for checking signatures that accept both
@@ -174,7 +174,7 @@ impl<'txin> Interpreter<'txin> {
174174
script_sig: &'txin bitcoin::Script,
175175
witness: &'txin Witness,
176176
age: Sequence, // CSV, relative lock time.
177-
lock_time: LockTime, // CLTV, absolute lock time.
177+
lock_time: absolute::LockTime, // CLTV, absolute lock time.
178178
) -> Result<Self, Error> {
179179
let (inner, stack, script_code) = inner::from_txdata(spk, script_sig, witness)?;
180180
Ok(Interpreter {
@@ -496,7 +496,7 @@ pub enum SatisfiedConstraint {
496496
///Absolute Timelock for CLTV.
497497
AbsoluteTimelock {
498498
/// The value of Absolute timelock
499-
n: LockTime,
499+
n: absolute::LockTime,
500500
},
501501
}
502502

@@ -532,7 +532,7 @@ pub struct Iter<'intp, 'txin: 'intp> {
532532
state: Vec<NodeEvaluationState<'intp>>,
533533
stack: Stack<'txin>,
534534
age: Sequence,
535-
lock_time: LockTime,
535+
lock_time: absolute::LockTime,
536536
has_errored: bool,
537537
}
538538

@@ -1146,7 +1146,7 @@ mod tests {
11461146
n_satisfied: 0,
11471147
}],
11481148
age: Sequence::from_height(1002),
1149-
lock_time: LockTime::from_height(1002).unwrap(),
1149+
lock_time: absolute::LockTime::from_height(1002).unwrap(),
11501150
has_errored: false,
11511151
}
11521152
}
@@ -1210,7 +1210,7 @@ mod tests {
12101210
assert_eq!(
12111211
after_satisfied.unwrap(),
12121212
vec![SatisfiedConstraint::AbsoluteTimelock {
1213-
n: LockTime::from_height(1000).unwrap()
1213+
n: absolute::LockTime::from_height(1000).unwrap()
12141214
}]
12151215
);
12161216

src/interpreter/stack.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use bitcoin;
1818
use bitcoin::blockdata::{opcodes, script};
1919
use bitcoin::hashes::{hash160, ripemd160, sha256, Hash};
20-
use bitcoin::{LockTime, Sequence};
20+
use bitcoin::{absolute, Sequence};
2121

2222
use super::error::PkEvalErrInner;
2323
use super::{
@@ -231,10 +231,10 @@ impl<'txin> Stack<'txin> {
231231
/// booleans
232232
pub(super) fn evaluate_after(
233233
&mut self,
234-
n: &LockTime,
235-
lock_time: LockTime,
234+
n: &absolute::LockTime,
235+
lock_time: absolute::LockTime,
236236
) -> Option<Result<SatisfiedConstraint, Error>> {
237-
use LockTime::*;
237+
use absolute::LockTime::*;
238238

239239
let is_satisfied = match (*n, lock_time) {
240240
(Blocks(n), Blocks(lock_time)) => n <= lock_time,

src/miniscript/astelem.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use core::fmt;
2323
use core::str::FromStr;
2424

2525
use bitcoin::blockdata::{opcodes, script};
26-
use bitcoin::{LockTime, Sequence};
26+
use bitcoin::{absolute, Sequence};
2727
use sync::Arc;
2828

2929
use crate::miniscript::context::SigType;
@@ -460,7 +460,7 @@ impl_from_tree!(
460460
}
461461
("pk_h", 1) => expression::terminal(&top.args[0], |x| Pk::from_str(x).map(Terminal::PkH)),
462462
("after", 1) => expression::terminal(&top.args[0], |x| {
463-
expression::parse_num(x).map(|x| Terminal::After(LockTime::from_consensus(x).into()))
463+
expression::parse_num(x).map(|x| Terminal::After(absolute::LockTime::from_consensus(x).into()))
464464
}),
465465
("older", 1) => expression::terminal(&top.args[0], |x| {
466466
expression::parse_num(x).map(|x| Terminal::Older(Sequence::from_consensus(x)))

src/miniscript/decode.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use bitcoin::blockdata::constants::MAX_BLOCK_WEIGHT;
2626
use bitcoin::hashes::{hash160, ripemd160, sha256, Hash};
2727
use sync::Arc;
2828

29-
use crate::bitcoin::{LockTime, PackedLockTime, Sequence};
29+
use crate::bitcoin::{absolute, Sequence};
3030
use crate::miniscript::lex::{Token as Tk, TokenIter};
3131
use crate::miniscript::limits::MAX_PUBKEYS_PER_MULTISIG;
3232
use crate::miniscript::types::extra_props::ExtData;
@@ -140,7 +140,7 @@ pub enum Terminal<Pk: MiniscriptKey, Ctx: ScriptContext> {
140140
RawPkH(Pk::RawPkHash),
141141
// timelocks
142142
/// `n CHECKLOCKTIMEVERIFY`
143-
After(PackedLockTime),
143+
After(absolute::PackedLockTime),
144144
/// `n CHECKSEQUENCEVERIFY`
145145
Older(Sequence),
146146
// hashlocks
@@ -395,7 +395,7 @@ pub fn parse<Ctx: ScriptContext>(
395395
Tk::CheckSequenceVerify, Tk::Num(n)
396396
=> term.reduce0(Terminal::Older(Sequence::from_consensus(n)))?,
397397
Tk::CheckLockTimeVerify, Tk::Num(n)
398-
=> term.reduce0(Terminal::After(LockTime::from_consensus(n).into()))?,
398+
=> term.reduce0(Terminal::After(absolute::LockTime::from_consensus(n).into()))?,
399399
// hashlocks
400400
Tk::Equal => match_token!(
401401
tokens,

src/miniscript/satisfy.rs

Lines changed: 38 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use core::{cmp, i64, mem};
2222

2323
use bitcoin::secp256k1::XOnlyPublicKey;
2424
use bitcoin::util::taproot::{ControlBlock, LeafVersion, TapLeafHash};
25-
use bitcoin::{LockTime, Sequence};
25+
use bitcoin::{absolute, relative, Sequence};
2626
use sync::Arc;
2727

2828
use crate::prelude::*;
@@ -106,12 +106,12 @@ pub trait Satisfier<Pk: MiniscriptKey + ToPublicKey> {
106106
}
107107

108108
/// Assert whether an relative locktime is satisfied
109-
fn check_older(&self, _: Sequence) -> bool {
109+
fn check_older(&self, _: relative::LockTime) -> bool {
110110
false
111111
}
112112

113113
/// Assert whether a absolute locktime is satisfied
114-
fn check_after(&self, _: LockTime) -> bool {
114+
fn check_after(&self, _: absolute::LockTime) -> bool {
115115
false
116116
}
117117
}
@@ -120,32 +120,18 @@ pub trait Satisfier<Pk: MiniscriptKey + ToPublicKey> {
120120
impl<Pk: MiniscriptKey + ToPublicKey> Satisfier<Pk> for () {}
121121

122122
impl<Pk: MiniscriptKey + ToPublicKey> Satisfier<Pk> for Sequence {
123-
fn check_older(&self, n: Sequence) -> bool {
123+
fn check_older(&self, n: relative::LockTime) -> bool {
124124
if !self.is_relative_lock_time() {
125125
return false;
126126
}
127127

128-
// We need a relative lock time type in rust-bitcoin to clean this up.
129-
130-
/* If nSequence encodes a relative lock-time, this mask is
131-
* applied to extract that lock-time from the sequence field. */
132-
const SEQUENCE_LOCKTIME_MASK: u32 = 0x0000ffff;
133-
const SEQUENCE_LOCKTIME_TYPE_FLAG: u32 = 0x00400000;
134-
135-
let mask = SEQUENCE_LOCKTIME_MASK | SEQUENCE_LOCKTIME_TYPE_FLAG;
136-
let masked_n = n.to_consensus_u32() & mask;
137-
let masked_seq = self.to_consensus_u32() & mask;
138-
if masked_n < SEQUENCE_LOCKTIME_TYPE_FLAG && masked_seq >= SEQUENCE_LOCKTIME_TYPE_FLAG {
139-
false
140-
} else {
141-
masked_n <= masked_seq
142-
}
128+
n.is_satisfied_by(*self)
143129
}
144130
}
145131

146-
impl<Pk: MiniscriptKey + ToPublicKey> Satisfier<Pk> for LockTime {
147-
fn check_after(&self, n: LockTime) -> bool {
148-
use LockTime::*;
132+
impl<Pk: MiniscriptKey + ToPublicKey> Satisfier<Pk> for absolute::LockTime {
133+
fn check_after(&self, n: absolute::LockTime) -> bool {
134+
use absolute::LockTime::*;
149135

150136
match (n, *self) {
151137
(Blocks(n), Blocks(lock_time)) => n <= lock_time,
@@ -265,11 +251,11 @@ impl<'a, Pk: MiniscriptKey + ToPublicKey, S: Satisfier<Pk>> Satisfier<Pk> for &'
265251
(**self).lookup_hash160(h)
266252
}
267253

268-
fn check_older(&self, t: Sequence) -> bool {
254+
fn check_older(&self, t: relative::LockTime) -> bool {
269255
(**self).check_older(t)
270256
}
271257

272-
fn check_after(&self, n: LockTime) -> bool {
258+
fn check_after(&self, n: absolute::LockTime) -> bool {
273259
(**self).check_after(n)
274260
}
275261
}
@@ -327,11 +313,11 @@ impl<'a, Pk: MiniscriptKey + ToPublicKey, S: Satisfier<Pk>> Satisfier<Pk> for &'
327313
(**self).lookup_hash160(h)
328314
}
329315

330-
fn check_older(&self, t: Sequence) -> bool {
316+
fn check_older(&self, t: relative::LockTime) -> bool {
331317
(**self).check_older(t)
332318
}
333319

334-
fn check_after(&self, n: LockTime) -> bool {
320+
fn check_after(&self, n: absolute::LockTime) -> bool {
335321
(**self).check_after(n)
336322
}
337323
}
@@ -465,7 +451,7 @@ macro_rules! impl_tuple_satisfier {
465451
None
466452
}
467453

468-
fn check_older(&self, n: Sequence) -> bool {
454+
fn check_older(&self, n: relative::LockTime) -> bool {
469455
let &($(ref $ty,)*) = self;
470456
$(
471457
if $ty.check_older(n) {
@@ -475,7 +461,7 @@ macro_rules! impl_tuple_satisfier {
475461
false
476462
}
477463

478-
fn check_after(&self, n: LockTime) -> bool {
464+
fn check_after(&self, n: absolute::LockTime) -> bool {
479465
let &($(ref $ty,)*) = self;
480466
$(
481467
if $ty.check_after(n) {
@@ -953,21 +939,31 @@ impl Satisfaction {
953939
},
954940
has_sig: false,
955941
},
956-
Terminal::Older(t) => Satisfaction {
957-
stack: if stfr.check_older(t) {
958-
Witness::empty()
959-
} else if root_has_sig {
960-
// If the root terminal has signature, the
961-
// signature covers the nLockTime and nSequence
962-
// values. The sender of the transaction should
963-
// take care that it signs the value such that the
964-
// timelock is not met
965-
Witness::Impossible
966-
} else {
967-
Witness::Unavailable
968-
},
942+
Terminal::Older(t) => {
943+
let older = {
944+
match t.to_relative_lock_time() {
945+
// https://github.com/bitcoin/bips/blob/master/bip-0112.mediawiki
946+
// Disable flag set => return true.
947+
None => true,
948+
Some(lock) => stfr.check_older(lock),
949+
}
950+
};
951+
Satisfaction {
952+
stack: if older {
953+
Witness::empty()
954+
} else if root_has_sig {
955+
// If the root terminal has signature, the
956+
// signature covers the nLockTime and nSequence
957+
// values. The sender of the transaction should
958+
// take care that it signs the value such that the
959+
// timelock is not met
960+
Witness::Impossible
961+
} else {
962+
Witness::Unavailable
963+
},
969964

970-
has_sig: false,
965+
has_sig: false,
966+
}
971967
},
972968
Terminal::Ripemd160(ref h) => Satisfaction {
973969
stack: Witness::ripemd160_preimage(stfr, h),

src/miniscript/types/extra_props.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use core::cmp;
55
use core::iter::once;
66

7-
use bitcoin::{LockTime, PackedLockTime, Sequence};
7+
use bitcoin::{absolute, Sequence};
88

99
use super::{Error, ErrorKind, Property, ScriptContext};
1010
use crate::miniscript::context::SigType;
@@ -337,7 +337,7 @@ impl Property for ExtData {
337337
unreachable!()
338338
}
339339

340-
fn from_after(t: LockTime) -> Self {
340+
fn from_after(t: absolute::LockTime) -> Self {
341341
ExtData {
342342
pk_cost: script_num_size(t.to_consensus_u32() as usize) + 1,
343343
has_free_verify: false,
@@ -933,7 +933,7 @@ impl Property for ExtData {
933933
// Note that for CLTV this is a limitation not of Bitcoin but Miniscript. The
934934
// number on the stack would be a 5 bytes signed integer but Miniscript's B type
935935
// only consumes 4 bytes from the stack.
936-
if t == PackedLockTime::ZERO {
936+
if t == absolute::PackedLockTime::ZERO {
937937
return Err(Error {
938938
fragment: fragment.clone(),
939939
error: ErrorKind::InvalidTime,

src/miniscript/types/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use core::fmt;
2424
#[cfg(feature = "std")]
2525
use std::error;
2626

27-
use bitcoin::{LockTime, PackedLockTime, Sequence};
27+
use bitcoin::{absolute, Sequence};
2828

2929
pub use self::correctness::{Base, Correctness, Input};
3030
pub use self::extra_props::ExtData;
@@ -304,7 +304,7 @@ pub trait Property: Sized {
304304

305305
/// Type property of an absolute timelock. Default implementation simply
306306
/// passes through to `from_time`
307-
fn from_after(t: LockTime) -> Self {
307+
fn from_after(t: absolute::LockTime) -> Self {
308308
Self::from_time(t.to_consensus_u32())
309309
}
310310

@@ -438,7 +438,7 @@ pub trait Property: Sized {
438438
// Note that for CLTV this is a limitation not of Bitcoin but Miniscript. The
439439
// number on the stack would be a 5 bytes signed integer but Miniscript's B type
440440
// only consumes 4 bytes from the stack.
441-
if t == PackedLockTime::ZERO {
441+
if t == absolute::PackedLockTime::ZERO {
442442
return Err(Error {
443443
fragment: fragment.clone(),
444444
error: ErrorKind::InvalidTime,
@@ -625,7 +625,7 @@ impl Property for Type {
625625
}
626626
}
627627

628-
fn from_after(t: LockTime) -> Self {
628+
fn from_after(t: absolute::LockTime) -> Self {
629629
Type {
630630
corr: Property::from_after(t),
631631
mall: Property::from_after(t),
@@ -821,7 +821,7 @@ impl Property for Type {
821821
// Note that for CLTV this is a limitation not of Bitcoin but Miniscript. The
822822
// number on the stack would be a 5 bytes signed integer but Miniscript's B type
823823
// only consumes 4 bytes from the stack.
824-
if t == PackedLockTime::ZERO {
824+
if t == absolute::PackedLockTime::ZERO {
825825
return Err(Error {
826826
fragment: fragment.clone(),
827827
error: ErrorKind::InvalidTime,

0 commit comments

Comments
 (0)