Skip to content

Commit 20e7463

Browse files
committed
WIP: Use new LockTime type
1 parent c80aa7c commit 20e7463

File tree

21 files changed

+125
-140
lines changed

21 files changed

+125
-140
lines changed

Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,18 @@ no-std = ["hashbrown", "bitcoin/no-std"]
1717
compiler = []
1818
trace = []
1919
unstable = []
20-
use-serde = ["serde", "bitcoin/use-serde"]
20+
use-serde = ["serde", "bitcoin/serde"]
2121
rand = ["bitcoin/rand"]
2222

2323
[dependencies]
24-
bitcoin = { version = "0.28.1", default-features = false }
24+
bitcoin = { path = "../rust-bitcoin", default-features = false }
2525
serde = { version = "1.0", optional = true }
2626
hashbrown = { version = "0.11", optional = true }
2727

2828
[dev-dependencies]
29-
bitcoind = {version = "0.26.1", features=["22_0"]}
29+
bitcoind = { path = "../../RCasatta/bitcoind", features=["22_0"] }
3030
actual-rand = { package = "rand", version = "0.8.4"}
31+
bitcoin = { path = "../rust-bitcoin", features = ["rand"] }
3132

3233
[[example]]
3334
name = "htlc"

examples/sign_multisig.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use std::collections::HashMap;
1818
use std::str::FromStr;
1919

2020
use bitcoin::blockdata::witness::Witness;
21-
use bitcoin::secp256k1;
21+
use bitcoin::{secp256k1, LockTime};
2222

2323
fn main() {
2424
let mut tx = spending_transaction();
@@ -91,7 +91,7 @@ fn main() {
9191
fn spending_transaction() -> bitcoin::Transaction {
9292
bitcoin::Transaction {
9393
version: 2,
94-
lock_time: 0,
94+
lock_time: LockTime::from(0),
9595
input: vec![bitcoin::TxIn {
9696
previous_output: Default::default(),
9797
script_sig: bitcoin::Script::new(),

examples/verify_tx.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
1717
use std::str::FromStr;
1818

19+
use bitcoin::LockTime;
1920
use bitcoin::consensus::Decodable;
2021
use bitcoin::secp256k1::{self, Secp256k1};
2122
use bitcoin::util::sighash;
@@ -34,7 +35,7 @@ fn main() {
3435
&tx.input[0].script_sig,
3536
&tx.input[0].witness,
3637
0,
37-
0,
38+
LockTime::from(0),
3839
)
3940
.unwrap();
4041

src/descriptor/tr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ impl<Pk: MiniscriptKey> Tr<Pk> {
258258
TaprootBuilderError::EmptyTree => {
259259
unreachable!("Taptree is a well formed tree with atleast 1 element")
260260
}
261+
_ => unreachable!("non_exhaustive catchall")
261262
},
262263
}
263264
};

src/interpreter/error.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ use crate::prelude::*;
2929
pub enum Error {
3030
/// Could not satisfy, absolute locktime not met
3131
AbsoluteLocktimeNotMet(u32),
32+
/// Could not satisfy, lock time values are different units
33+
AbsoluteLocktimeComparisonInvalid(u32, u32),
3234
/// Cannot Infer a taproot descriptor
3335
/// Key spends cannot infer the internal key of the descriptor
3436
/// Inferring script spends is possible, but is hidden nodes are currently
@@ -187,6 +189,7 @@ impl fmt::Display for Error {
187189
Error::VerifyFailed => {
188190
f.write_str("Expected Satisfied Boolean at stack top for VERIFY")
189191
}
192+
_ => f.write_str("Unknown error, non_exhaustive catch all"),
190193
}
191194
}
192195
}
@@ -234,6 +237,7 @@ impl error::Error for Error {
234237
Secp(e) => Some(e),
235238
SchnorrSig(e) => Some(e),
236239
SighashError(e) => Some(e),
240+
_ => None // non_exhaustive catch all.
237241
}
238242
}
239243
}

src/interpreter/mod.rs

Lines changed: 8 additions & 8 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, sha256d};
2727
use bitcoin::util::{sighash, taproot};
28-
use bitcoin::{self, secp256k1, TxOut};
28+
use bitcoin::{self, secp256k1, TxOut, LockTime};
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: u32,
52-
lock_time: u32,
52+
lock_time: LockTime,
5353
}
5454

5555
// A type representing functions for checking signatures that accept both
@@ -170,8 +170,8 @@ impl<'txin> Interpreter<'txin> {
170170
spk: &bitcoin::Script,
171171
script_sig: &'txin bitcoin::Script,
172172
witness: &'txin Witness,
173-
age: u32, // CSV, relative lock time.
174-
lock_time: u32, // CLTV, absolute lock time.
173+
age: u32, // CSV, relative lock time.
174+
lock_time: LockTime, // CLTV, absolute lock time.
175175
) -> Result<Self, Error> {
176176
let (inner, stack, script_code) = inner::from_txdata(spk, script_sig, witness)?;
177177
Ok(Interpreter {
@@ -493,7 +493,7 @@ pub enum SatisfiedConstraint {
493493
///Absolute Timelock for CLTV.
494494
AbsoluteTimelock {
495495
/// The value of Absolute timelock
496-
time: u32,
496+
n: LockTime,
497497
},
498498
}
499499

@@ -529,7 +529,7 @@ pub struct Iter<'intp, 'txin: 'intp> {
529529
state: Vec<NodeEvaluationState<'intp>>,
530530
stack: Stack<'txin>,
531531
age: u32,
532-
lock_time: u32,
532+
lock_time: LockTime,
533533
has_errored: bool,
534534
}
535535

@@ -1132,7 +1132,7 @@ mod tests {
11321132
n_satisfied: 0,
11331133
}],
11341134
age: 1002,
1135-
lock_time: 1002,
1135+
lock_time: LockTime::from(1002),
11361136
has_errored: false,
11371137
}
11381138
}
@@ -1198,7 +1198,7 @@ mod tests {
11981198
let after_satisfied: Result<Vec<SatisfiedConstraint>, Error> = constraints.collect();
11991199
assert_eq!(
12001200
after_satisfied.unwrap(),
1201-
vec![SatisfiedConstraint::AbsoluteTimelock { time: 1000 }]
1201+
vec![SatisfiedConstraint::AbsoluteTimelock { n: LockTime::from(1000) }]
12021202
);
12031203

12041204
//Check Older

src/interpreter/stack.rs

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

2122
use super::error::PkEvalErrInner;
2223
use super::{
@@ -231,14 +232,16 @@ impl<'txin> Stack<'txin> {
231232
/// booleans
232233
pub(super) fn evaluate_after(
233234
&mut self,
234-
n: &u32,
235-
lock_time: u32,
235+
n: &LockTime,
236+
lock_time: LockTime,
236237
) -> Option<Result<SatisfiedConstraint, Error>> {
237-
if lock_time >= *n {
238-
self.push(Element::Satisfied);
239-
Some(Ok(SatisfiedConstraint::AbsoluteTimelock { time: *n }))
240-
} else {
241-
Some(Err(Error::AbsoluteLocktimeNotMet(*n)))
238+
match n.is_satisfied(lock_time) {
239+
Ok(true) => {
240+
self.push(Element::Satisfied);
241+
Some(Ok(SatisfiedConstraint::AbsoluteTimelock { n: *n }))
242+
},
243+
Ok(false) => Some(Err(Error::AbsoluteLocktimeNotMet(n.to_raw_u32()))),
244+
Err(_) => Some(Err(Error::AbsoluteLocktimeComparisonInvalid(n.to_raw_u32(), lock_time.to_raw_u32()))),
242245
}
243246
}
244247

src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ pub mod interpreter;
116116
pub mod miniscript;
117117
pub mod policy;
118118
pub mod psbt;
119-
pub mod timelock;
120119

121120
#[cfg(test)]
122121
mod test_utils;

src/miniscript/astelem.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use core::fmt;
2323
use core::str::FromStr;
2424

25+
use bitcoin::LockTime;
2526
use bitcoin::blockdata::{opcodes, script};
2627
use bitcoin::hashes::hex::FromHex;
2728
use bitcoin::hashes::{hash160, ripemd160, sha256d, Hash};
@@ -464,7 +465,7 @@ impl_from_tree!(
464465
expression::terminal(&top.args[0], |x| Pk::Hash::from_str(x).map(Terminal::PkH))
465466
}
466467
("after", 1) => expression::terminal(&top.args[0], |x| {
467-
expression::parse_num(x).map(Terminal::After)
468+
expression::parse_num(x).map(|x| Terminal::After(LockTime::from(x)))
468469
}),
469470
("older", 1) => expression::terminal(&top.args[0], |x| {
470471
expression::parse_num(x).map(Terminal::Older)
@@ -626,7 +627,7 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Terminal<Pk, Ctx> {
626627
.push_slice(&Pk::hash_to_hash160(hash)[..])
627628
.push_opcode(opcodes::all::OP_EQUALVERIFY),
628629
Terminal::After(t) => builder
629-
.push_int(t as i64)
630+
.push_int(t.to_raw_u32() as i64)
630631
.push_opcode(opcodes::all::OP_CLTV),
631632
Terminal::Older(t) => builder.push_int(t as i64).push_opcode(opcodes::all::OP_CSV),
632633
Terminal::Sha256(ref h) => builder
@@ -761,7 +762,7 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Terminal<Pk, Ctx> {
761762
match *self {
762763
Terminal::PkK(ref pk) => Ctx::pk_len(pk),
763764
Terminal::PkH(..) => 24,
764-
Terminal::After(n) => script_num_size(n as usize) + 1,
765+
Terminal::After(n) => script_num_size(n.to_raw_u32() as usize) + 1,
765766
Terminal::Older(n) => script_num_size(n as usize) + 1,
766767
Terminal::Sha256(..) => 33 + 6,
767768
Terminal::Hash256(..) => 33 + 6,

src/miniscript/decode.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use sync::Arc;
2828

2929
use crate::miniscript::lex::{Token as Tk, TokenIter};
3030
use crate::miniscript::limits::MAX_PUBKEYS_PER_MULTISIG;
31+
use crate::bitcoin::locktime::LockTime;
3132
use crate::miniscript::types::extra_props::ExtData;
3233
use crate::miniscript::types::{Property, Type};
3334
use crate::miniscript::ScriptContext;
@@ -137,7 +138,7 @@ pub enum Terminal<Pk: MiniscriptKey, Ctx: ScriptContext> {
137138
PkH(Pk::Hash),
138139
// timelocks
139140
/// `n CHECKLOCKTIMEVERIFY`
140-
After(u32),
141+
After(LockTime),
141142
/// `n CHECKSEQUENCEVERIFY`
142143
Older(u32),
143144
// hashlocks
@@ -392,7 +393,7 @@ pub fn parse<Ctx: ScriptContext>(
392393
Tk::CheckSequenceVerify, Tk::Num(n)
393394
=> term.reduce0(Terminal::Older(n))?,
394395
Tk::CheckLockTimeVerify, Tk::Num(n)
395-
=> term.reduce0(Terminal::After(n))?,
396+
=> term.reduce0(Terminal::After(LockTime::from(n)))?,
396397
// hashlocks
397398
Tk::Equal => match_token!(
398399
tokens,

0 commit comments

Comments
 (0)