Skip to content

Commit fc42e48

Browse files
committed
miniscript: use RelLockTime in Terminal
This gets rid of all the new conversions and unwraps :)
1 parent c490f49 commit fc42e48

File tree

8 files changed

+18
-56
lines changed

8 files changed

+18
-56
lines changed

src/interpreter/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ where
608608
Terminal::Older(ref n) => {
609609
debug_assert_eq!(node_state.n_evaluated, 0);
610610
debug_assert_eq!(node_state.n_satisfied, 0);
611-
let res = self.stack.evaluate_older(n, self.sequence);
611+
let res = self.stack.evaluate_older(&(*n).into(), self.sequence);
612612
if res.is_some() {
613613
return res;
614614
}

src/miniscript/astelem.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ use core::fmt;
1111
use core::str::FromStr;
1212

1313
use bitcoin::hashes::{hash160, Hash};
14-
use bitcoin::{absolute, opcodes, script, Sequence};
14+
use bitcoin::{absolute, opcodes, script};
1515
use sync::Arc;
1616

1717
use crate::miniscript::context::SigType;
1818
use crate::miniscript::{types, ScriptContext};
1919
use crate::prelude::*;
2020
use crate::util::MsKeyBuilder;
2121
use crate::{
22-
errstr, expression, AbsLockTime, Error, FromStrKey, Miniscript, MiniscriptKey, Terminal,
23-
ToPublicKey,
22+
errstr, expression, AbsLockTime, Error, FromStrKey, Miniscript, MiniscriptKey, RelLockTime,
23+
Terminal, ToPublicKey,
2424
};
2525

2626
impl<Pk: MiniscriptKey, Ctx: ScriptContext> Terminal<Pk, Ctx> {
@@ -261,7 +261,9 @@ impl<Pk: FromStrKey, Ctx: ScriptContext> crate::expression::FromTree for Termina
261261
.map(Terminal::After)
262262
}),
263263
("older", 1) => expression::terminal(&top.args[0], |x| {
264-
expression::parse_num(x).map(|x| Terminal::Older(Sequence::from_consensus(x)))
264+
expression::parse_num(x)
265+
.and_then(|x| RelLockTime::from_consensus(x).map_err(Error::RelativeLockTime))
266+
.map(Terminal::Older)
265267
}),
266268
("sha256", 1) => expression::terminal(&top.args[0], |x| {
267269
Pk::Sha256::from_str(x).map(Terminal::Sha256)

src/miniscript/decode.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use core::marker::PhantomData;
1111
use std::error;
1212

1313
use bitcoin::hashes::{hash160, ripemd160, sha256, Hash};
14-
use bitcoin::{Sequence, Weight};
14+
use bitcoin::Weight;
1515
use sync::Arc;
1616

1717
use crate::miniscript::lex::{Token as Tk, TokenIter};
@@ -22,7 +22,7 @@ use crate::miniscript::ScriptContext;
2222
use crate::prelude::*;
2323
#[cfg(doc)]
2424
use crate::Descriptor;
25-
use crate::{hash256, AbsLockTime, Error, Miniscript, MiniscriptKey, ToPublicKey};
25+
use crate::{hash256, AbsLockTime, Error, Miniscript, MiniscriptKey, RelLockTime, ToPublicKey};
2626

2727
/// Trait for parsing keys from byte slices
2828
pub trait ParseableKey: Sized + ToPublicKey + private::Sealed {
@@ -137,7 +137,7 @@ pub enum Terminal<Pk: MiniscriptKey, Ctx: ScriptContext> {
137137
/// `n CHECKLOCKTIMEVERIFY`
138138
After(AbsLockTime),
139139
/// `n CHECKSEQUENCEVERIFY`
140-
Older(Sequence),
140+
Older(RelLockTime),
141141
// hashlocks
142142
/// `SIZE 32 EQUALVERIFY SHA256 <hash> EQUAL`
143143
Sha256(Pk::Sha256),
@@ -357,7 +357,7 @@ pub fn parse<Ctx: ScriptContext>(
357357
},
358358
// timelocks
359359
Tk::CheckSequenceVerify, Tk::Num(n)
360-
=> term.reduce0(Terminal::Older(Sequence::from_consensus(n)))?,
360+
=> term.reduce0(Terminal::Older(RelLockTime::from_consensus(n).map_err(Error::RelativeLockTime)?))?,
361361
Tk::CheckLockTimeVerify, Tk::Num(n)
362362
=> term.reduce0(Terminal::After(AbsLockTime::from_consensus(n).map_err(Error::AbsoluteLockTime)?))?,
363363
// hashlocks

src/miniscript/satisfy.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,8 +1285,6 @@ impl<Pk: MiniscriptKey + ToPublicKey> Satisfaction<Placeholder<Pk>> {
12851285
Satisfaction { stack, has_sig: false, relative_timelock: None, absolute_timelock }
12861286
}
12871287
Terminal::Older(t) => {
1288-
// unwrap to be removed in a later commit
1289-
let t = <RelLockTime as core::convert::TryFrom<_>>::try_from(t).unwrap();
12901288
let (stack, relative_timelock) = if stfr.check_older(t.into()) {
12911289
(Witness::empty(), Some(t))
12921290
} else if root_has_sig {

src/miniscript/types/extra_props.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@
66
use core::cmp;
77
use core::iter::once;
88

9-
use bitcoin::Sequence;
10-
119
use super::{Error, ErrorKind, ScriptContext};
1210
use crate::miniscript::context::SigType;
1311
use crate::prelude::*;
14-
use crate::{script_num_size, AbsLockTime, MiniscriptKey, Terminal};
12+
use crate::{script_num_size, AbsLockTime, MiniscriptKey, RelLockTime, Terminal};
1513

1614
/// Timelock information for satisfaction of a fragment.
1715
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Default, Hash)]
@@ -365,7 +363,7 @@ impl ExtData {
365363
}
366364

367365
/// Extra properties for the `older` fragment.
368-
pub fn older(t: Sequence) -> Self {
366+
pub fn older(t: RelLockTime) -> Self {
369367
ExtData {
370368
pk_cost: script_num_size(t.to_consensus_u32() as usize) + 1,
371369
has_free_verify: false,
@@ -924,15 +922,7 @@ impl ExtData {
924922
}
925923
}
926924
Terminal::After(t) => Self::after(t),
927-
Terminal::Older(t) => {
928-
if t == Sequence::ZERO || !t.is_relative_lock_time() {
929-
return Err(Error {
930-
fragment_string: fragment.to_string(),
931-
error: ErrorKind::InvalidTime,
932-
});
933-
}
934-
Self::older(t)
935-
}
925+
Terminal::Older(t) => Self::older(t),
936926
Terminal::Sha256(..) => Self::sha256(),
937927
Terminal::Hash256(..) => Self::hash256(),
938928
Terminal::Ripemd160(..) => Self::ripemd160(),

src/miniscript/types/mod.rs

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ use core::fmt;
1414
#[cfg(feature = "std")]
1515
use std::error;
1616

17-
use bitcoin::Sequence;
18-
1917
pub use self::correctness::{Base, Correctness, Input};
2018
pub use self::extra_props::ExtData;
2119
pub use self::malleability::{Dissat, Malleability};
@@ -25,8 +23,6 @@ use crate::{MiniscriptKey, Terminal};
2523
/// Detailed type of a typechecker error
2624
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
2725
pub enum ErrorKind {
28-
/// Relative or absolute timelock had an invalid time value (either 0, or >=0x80000000)
29-
InvalidTime,
3026
/// Passed a `z` argument to a `d` wrapper when `z` was expected
3127
NonZeroDupIf,
3228
/// Multisignature or threshold policy had a `k` value of 0
@@ -79,11 +75,6 @@ pub struct Error {
7975
impl fmt::Display for Error {
8076
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
8177
match self.error {
82-
ErrorKind::InvalidTime => write!(
83-
f,
84-
"fragment «{}» represents a timelock which value is invalid (time must be in [1; 0x80000000])",
85-
self.fragment_string,
86-
),
8778
ErrorKind::NonZeroDupIf => write!(
8879
f,
8980
"fragment «{}» represents needs to be `z`, needs to consume zero elements from the stack",
@@ -479,15 +470,7 @@ impl Type {
479470
}
480471
}
481472
Terminal::After(_) => Ok(Self::time()),
482-
Terminal::Older(t) => {
483-
if t == Sequence::ZERO || !t.is_relative_lock_time() {
484-
return Err(Error {
485-
fragment_string: fragment.to_string(),
486-
error: ErrorKind::InvalidTime,
487-
});
488-
}
489-
Ok(Self::time())
490-
}
473+
Terminal::Older(_) => Ok(Self::time()),
491474
Terminal::Sha256(..) => Ok(Self::hash()),
492475
Terminal::Hash256(..) => Ok(Self::hash()),
493476
Terminal::Ripemd160(..) => Ok(Self::hash()),

src/policy/compiler.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use core::{cmp, f64, fmt, hash, mem};
99
#[cfg(feature = "std")]
1010
use std::error;
1111

12-
use bitcoin::Sequence;
1312
use sync::Arc;
1413

1514
use crate::miniscript::context::SigType;
@@ -447,15 +446,7 @@ impl CompilerExtData {
447446
}
448447
}
449448
Terminal::After(_) => Ok(Self::time()),
450-
Terminal::Older(t) => {
451-
if t == Sequence::ZERO || !t.is_relative_lock_time() {
452-
return Err(types::Error {
453-
fragment_string: fragment.to_string(),
454-
error: types::ErrorKind::InvalidTime,
455-
});
456-
}
457-
Ok(Self::time())
458-
}
449+
Terminal::Older(_) => Ok(Self::time()),
459450
Terminal::Sha256(..) => Ok(Self::hash()),
460451
Terminal::Hash256(..) => Ok(Self::hash()),
461452
Terminal::Ripemd160(..) => Ok(Self::hash()),
@@ -871,7 +862,7 @@ where
871862
insert_wrap!(AstElemExt::terminal(Terminal::PkK(pk.clone())));
872863
}
873864
Concrete::After(n) => insert_wrap!(AstElemExt::terminal(Terminal::After(n))),
874-
Concrete::Older(n) => insert_wrap!(AstElemExt::terminal(Terminal::Older(n.into()))),
865+
Concrete::Older(n) => insert_wrap!(AstElemExt::terminal(Terminal::Older(n))),
875866
Concrete::Sha256(ref hash) => {
876867
insert_wrap!(AstElemExt::terminal(Terminal::Sha256(hash.clone())))
877868
}

src/policy/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,7 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Liftable<Pk> for Terminal<Pk, Ctx> {
123123
return Err(Error::LiftError(LiftError::RawDescriptorLift))
124124
}
125125
Terminal::After(t) => Semantic::After(t),
126-
Terminal::Older(t) => Semantic::Older(
127-
<crate::RelLockTime as core::convert::TryFrom<_>>::try_from(t).unwrap(),
128-
), // unwrap to be removed in future commit
126+
Terminal::Older(t) => Semantic::Older(t),
129127
Terminal::Sha256(ref h) => Semantic::Sha256(h.clone()),
130128
Terminal::Hash256(ref h) => Semantic::Hash256(h.clone()),
131129
Terminal::Ripemd160(ref h) => Semantic::Ripemd160(h.clone()),

0 commit comments

Comments
 (0)