Skip to content

Commit 4466a8b

Browse files
committed
Use lock_time instead of age
We currently use age/height as identifiers for values related to CLTV and CSV. However we get them mixed up in the `at_age`/`at_height` functions. This is a clear sign that the names are not good. Instead of 'age' use 'lock_time' - this is what the field in a transaction is called and it is easier to remember because of nLockTime and OP_CHECKLOCKTIMEVERIFY.
1 parent 0ac479f commit 4466a8b

File tree

6 files changed

+31
-31
lines changed

6 files changed

+31
-31
lines changed

examples/sign_multisig.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ fn main() {
6060
53ae"
6161
);
6262

63-
// Attempt to satisfy at age 0, height 0.
63+
// Attempt to satisfy at lock_time 0, height 0.
6464
let original_txin = tx.input[0].clone();
6565

6666
let mut sigs = HashMap::<bitcoin::PublicKey, miniscript::bitcoin::EcdsaSig>::new();

src/interpreter/mod.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub struct Interpreter<'txin> {
4848
/// For non-Taproot spends, the scriptCode; for Taproot script-spends, this
4949
/// is the leaf script; for key-spends it is `None`.
5050
script_code: Option<bitcoin::Script>,
51-
age: u32,
51+
lock_time: u32,
5252
height: u32,
5353
}
5454

@@ -169,15 +169,15 @@ impl<'txin> Interpreter<'txin> {
169169
spk: &bitcoin::Script,
170170
script_sig: &'txin bitcoin::Script,
171171
witness: &'txin Witness,
172-
age: u32,
172+
lock_time: u32,
173173
height: u32,
174174
) -> Result<Self, Error> {
175175
let (inner, stack, script_code) = inner::from_txdata(spk, script_sig, witness)?;
176176
Ok(Interpreter {
177177
inner,
178178
stack,
179179
script_code,
180-
age,
180+
lock_time,
181181
height,
182182
})
183183
}
@@ -208,7 +208,7 @@ impl<'txin> Interpreter<'txin> {
208208
// Cloning the references to elements of stack should be fine as it allows
209209
// call interpreter.iter() without mutating interpreter
210210
stack: self.stack.clone(),
211-
age: self.age,
211+
lock_time: self.lock_time,
212212
height: self.height,
213213
has_errored: false,
214214
}
@@ -527,7 +527,7 @@ pub struct Iter<'intp, 'txin: 'intp> {
527527
public_key: Option<&'intp BitcoinKey>,
528528
state: Vec<NodeEvaluationState<'intp>>,
529529
stack: Stack<'txin>,
530-
age: u32,
530+
lock_time: u32,
531531
height: u32,
532532
has_errored: bool,
533533
}
@@ -605,7 +605,7 @@ where
605605
Terminal::After(ref n) => {
606606
debug_assert_eq!(node_state.n_evaluated, 0);
607607
debug_assert_eq!(node_state.n_satisfied, 0);
608-
let res = self.stack.evaluate_after(n, self.age);
608+
let res = self.stack.evaluate_after(n, self.lock_time);
609609
if res.is_some() {
610610
return res;
611611
}
@@ -1130,7 +1130,7 @@ mod tests {
11301130
n_evaluated: 0,
11311131
n_satisfied: 0,
11321132
}],
1133-
age: 1002,
1133+
lock_time: 1002,
11341134
height: 1002,
11351135
has_errored: false,
11361136
}

src/interpreter/stack.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,9 @@ impl<'txin> Stack<'txin> {
231231
pub(super) fn evaluate_after(
232232
&mut self,
233233
n: &u32,
234-
age: u32,
234+
lock_time: u32,
235235
) -> Option<Result<SatisfiedConstraint, Error>> {
236-
if age >= *n {
236+
if lock_time >= *n {
237237
self.push(Element::Satisfied);
238238
Some(Ok(SatisfiedConstraint::AbsoluteTimelock { time: *n }))
239239
} else {

src/miniscript/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -854,7 +854,7 @@ mod tests {
854854
let mut abs = miniscript.lift().unwrap();
855855
assert_eq!(abs.n_keys(), 5);
856856
assert_eq!(abs.minimum_n_keys(), Some(2));
857-
abs = abs.at_age(10000);
857+
abs = abs.at_lock_time(10000);
858858
assert_eq!(abs.n_keys(), 5);
859859
assert_eq!(abs.minimum_n_keys(), Some(2));
860860
abs = abs.at_age(9999);

src/policy/compiler.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1382,13 +1382,13 @@ mod tests {
13821382
let mut abs = policy.lift().unwrap();
13831383
assert_eq!(abs.n_keys(), 8);
13841384
assert_eq!(abs.minimum_n_keys(), Some(2));
1385-
abs = abs.at_age(10000);
1385+
abs = abs.at_lock_time(10000);
13861386
assert_eq!(abs.n_keys(), 8);
13871387
assert_eq!(abs.minimum_n_keys(), Some(2));
1388-
abs = abs.at_age(9999);
1388+
abs = abs.at_lock_time(9999);
13891389
assert_eq!(abs.n_keys(), 5);
13901390
assert_eq!(abs.minimum_n_keys(), Some(3));
1391-
abs = abs.at_age(0);
1391+
abs = abs.at_lock_time(0);
13921392
assert_eq!(abs.n_keys(), 5);
13931393
assert_eq!(abs.minimum_n_keys(), Some(3));
13941394

src/policy/semantic.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -538,21 +538,21 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
538538
self.normalized()
539539
}
540540

541-
/// Filter a policy by eliminating absolute timelock constraints
542-
/// that are not satisfied at the given age.
543-
pub fn at_height(mut self, time: u32) -> Policy<Pk> {
541+
/// Filter a policy by eliminating absolute timelock constraints that are not satisfied at the
542+
/// given age `n` (n comes from `n OP_CHECKLOCKTIMEVERIFY`).
543+
pub fn at_lock_time(mut self, n: u32) -> Policy<Pk> {
544544
self = match self {
545545
Policy::After(t) => {
546-
if !timelock::absolute_timelocks_are_same_unit(t, time) {
546+
if !timelock::absolute_timelocks_are_same_unit(t, n) {
547547
Policy::Unsatisfiable
548-
} else if t > time {
548+
} else if t > n {
549549
Policy::Unsatisfiable
550550
} else {
551551
Policy::After(t)
552552
}
553553
}
554554
Policy::Threshold(k, subs) => {
555-
Policy::Threshold(k, subs.into_iter().map(|sub| sub.at_height(time)).collect())
555+
Policy::Threshold(k, subs.into_iter().map(|sub| sub.at_lock_time(n)).collect())
556556
}
557557
x => x,
558558
};
@@ -655,19 +655,19 @@ mod tests {
655655
assert_eq!(policy, Policy::KeyHash("".to_owned()));
656656
assert_eq!(policy.relative_timelocks(), vec![]);
657657
assert_eq!(policy.absolute_timelocks(), vec![]);
658-
assert_eq!(policy.clone().at_age(0), policy.clone());
659-
assert_eq!(policy.clone().at_age(10000), policy.clone());
658+
assert_eq!(policy.clone().at_lock_time(0), policy.clone());
659+
assert_eq!(policy.clone().at_lock_time(10000), policy.clone());
660660
assert_eq!(policy.n_keys(), 1);
661661
assert_eq!(policy.minimum_n_keys(), Some(1));
662662

663663
let policy = StringPolicy::from_str("older(1000)").unwrap();
664664
assert_eq!(policy, Policy::Older(1000));
665665
assert_eq!(policy.absolute_timelocks(), vec![]);
666666
assert_eq!(policy.relative_timelocks(), vec![1000]);
667-
assert_eq!(policy.clone().at_age(0), Policy::Unsatisfiable);
668-
assert_eq!(policy.clone().at_age(999), Policy::Unsatisfiable);
669-
assert_eq!(policy.clone().at_age(1000), policy.clone());
670-
assert_eq!(policy.clone().at_age(10000), policy.clone());
667+
assert_eq!(policy.clone().at_lock_time(0), Policy::Unsatisfiable);
668+
assert_eq!(policy.clone().at_lock_time(999), Policy::Unsatisfiable);
669+
assert_eq!(policy.clone().at_lock_time(1000), policy.clone());
670+
assert_eq!(policy.clone().at_lock_time(10000), policy.clone());
671671
assert_eq!(policy.n_keys(), 0);
672672
assert_eq!(policy.minimum_n_keys(), Some(0));
673673

@@ -681,10 +681,10 @@ mod tests {
681681
);
682682
assert_eq!(policy.relative_timelocks(), vec![1000]);
683683
assert_eq!(policy.absolute_timelocks(), vec![]);
684-
assert_eq!(policy.clone().at_age(0), Policy::KeyHash("".to_owned()));
685-
assert_eq!(policy.clone().at_age(999), Policy::KeyHash("".to_owned()));
686-
assert_eq!(policy.clone().at_age(1000), policy.clone().normalized());
687-
assert_eq!(policy.clone().at_age(10000), policy.clone().normalized());
684+
assert_eq!(policy.clone().at_lock_time(0), Policy::KeyHash("".to_owned()));
685+
assert_eq!(policy.clone().at_lock_time(999), Policy::KeyHash("".to_owned()));
686+
assert_eq!(policy.clone().at_lock_time(1000), policy.clone().normalized());
687+
assert_eq!(policy.clone().at_lock_time(10000), policy.clone().normalized());
688688
assert_eq!(policy.n_keys(), 1);
689689
assert_eq!(policy.minimum_n_keys(), Some(0));
690690

@@ -813,7 +813,7 @@ mod tests {
813813
let backup_policy = StringPolicy::from_str("thresh(2,pkh(A),pkh(B),pkh(C))").unwrap();
814814
assert!(!backup_policy
815815
.clone()
816-
.entails(liquid_pol.clone().at_age(4095))
816+
.entails(liquid_pol.clone().at_lock_time(4095))
817817
.unwrap());
818818

819819
// Finally test both spending paths

0 commit comments

Comments
 (0)