Skip to content

Commit bc3b8dc

Browse files
committed
policy: rename Threshold variant to Thresh
This matches the same variant in Terminal, matches the string serialization "thresh", and will avoid weird compiler errors related to collisions with the upcoming `Threshold` type. This will be an annoying API-breaking change, but fortunately(?) this PR is already breaking the API for thresholds in an annoying way.
1 parent 78db616 commit bc3b8dc

File tree

6 files changed

+82
-97
lines changed

6 files changed

+82
-97
lines changed

src/descriptor/sortedmulti.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> SortedMultiVec<Pk, Ctx> {
198198

199199
impl<Pk: MiniscriptKey, Ctx: ScriptContext> policy::Liftable<Pk> for SortedMultiVec<Pk, Ctx> {
200200
fn lift(&self) -> Result<policy::semantic::Policy<Pk>, Error> {
201-
let ret = policy::semantic::Policy::Threshold(
201+
let ret = policy::semantic::Policy::Thresh(
202202
self.k,
203203
self.pks
204204
.iter()

src/descriptor/tr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ impl<Pk: MiniscriptKey> Liftable<Pk> for TapTree<Pk> {
616616
fn lift(&self) -> Result<Policy<Pk>, Error> {
617617
fn lift_helper<Pk: MiniscriptKey>(s: &TapTree<Pk>) -> Result<Policy<Pk>, Error> {
618618
match *s {
619-
TapTree::Tree { ref left, ref right, height: _ } => Ok(Policy::Threshold(
619+
TapTree::Tree { ref left, ref right, height: _ } => Ok(Policy::Thresh(
620620
1,
621621
vec![Arc::new(lift_helper(left)?), Arc::new(lift_helper(right)?)],
622622
)),
@@ -632,7 +632,7 @@ impl<Pk: MiniscriptKey> Liftable<Pk> for TapTree<Pk> {
632632
impl<Pk: MiniscriptKey> Liftable<Pk> for Tr<Pk> {
633633
fn lift(&self) -> Result<Policy<Pk>, Error> {
634634
match &self.tree {
635-
Some(root) => Ok(Policy::Threshold(
635+
Some(root) => Ok(Policy::Thresh(
636636
1,
637637
vec![
638638
Arc::new(Policy::Key(self.internal_key.clone())),

src/policy/compiler.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,7 +1008,7 @@ where
10081008
compile_binary!(&mut l_comp[3], &mut r_comp[2], [lw, rw], Terminal::OrI);
10091009
compile_binary!(&mut r_comp[3], &mut l_comp[2], [rw, lw], Terminal::OrI);
10101010
}
1011-
Concrete::Threshold(k, ref subs) => {
1011+
Concrete::Thresh(k, ref subs) => {
10121012
let n = subs.len();
10131013
let k_over_n = k as f64 / n as f64;
10141014

@@ -1389,7 +1389,7 @@ mod tests {
13891389
let policy: BPolicy = Concrete::Or(vec![
13901390
(
13911391
127,
1392-
Arc::new(Concrete::Threshold(
1392+
Arc::new(Concrete::Thresh(
13931393
3,
13941394
key_pol[0..5].iter().map(|p| (p.clone()).into()).collect(),
13951395
)),
@@ -1398,7 +1398,7 @@ mod tests {
13981398
1,
13991399
Arc::new(Concrete::And(vec![
14001400
Arc::new(Concrete::Older(RelLockTime::from_height(10000))),
1401-
Arc::new(Concrete::Threshold(
1401+
Arc::new(Concrete::Thresh(
14021402
2,
14031403
key_pol[5..8].iter().map(|p| (p.clone()).into()).collect(),
14041404
)),
@@ -1519,7 +1519,7 @@ mod tests {
15191519
.iter()
15201520
.map(|pubkey| Arc::new(Concrete::Key(*pubkey)))
15211521
.collect();
1522-
let big_thresh = Concrete::Threshold(*k, pubkeys);
1522+
let big_thresh = Concrete::Thresh(*k, pubkeys);
15231523
let big_thresh_ms: SegwitMiniScript = big_thresh.compile().unwrap();
15241524
if *k == 21 {
15251525
// N * (PUSH + pubkey + CHECKSIGVERIFY)
@@ -1555,8 +1555,8 @@ mod tests {
15551555
.collect();
15561556

15571557
let thresh_res: Result<SegwitMiniScript, _> = Concrete::Or(vec![
1558-
(1, Arc::new(Concrete::Threshold(keys_a.len(), keys_a))),
1559-
(1, Arc::new(Concrete::Threshold(keys_b.len(), keys_b))),
1558+
(1, Arc::new(Concrete::Thresh(keys_a.len(), keys_a))),
1559+
(1, Arc::new(Concrete::Thresh(keys_b.len(), keys_b))),
15601560
])
15611561
.compile();
15621562
let script_size = thresh_res.clone().and_then(|m| Ok(m.script_size()));
@@ -1573,8 +1573,7 @@ mod tests {
15731573
.iter()
15741574
.map(|pubkey| Arc::new(Concrete::Key(*pubkey)))
15751575
.collect();
1576-
let thresh_res: Result<SegwitMiniScript, _> =
1577-
Concrete::Threshold(keys.len(), keys).compile();
1576+
let thresh_res: Result<SegwitMiniScript, _> = Concrete::Thresh(keys.len(), keys).compile();
15781577
let n_elements = thresh_res
15791578
.clone()
15801579
.and_then(|m| Ok(m.max_satisfaction_witness_elements()));
@@ -1595,7 +1594,7 @@ mod tests {
15951594
.map(|pubkey| Arc::new(Concrete::Key(*pubkey)))
15961595
.collect();
15971596
let thresh_res: Result<SegwitMiniScript, _> =
1598-
Concrete::Threshold(keys.len() - 1, keys).compile();
1597+
Concrete::Thresh(keys.len() - 1, keys).compile();
15991598
let ops_count = thresh_res.clone().and_then(|m| Ok(m.ext.ops.op_count()));
16001599
assert_eq!(
16011600
thresh_res,
@@ -1609,7 +1608,7 @@ mod tests {
16091608
.iter()
16101609
.map(|pubkey| Arc::new(Concrete::Key(*pubkey)))
16111610
.collect();
1612-
let thresh_res = Concrete::Threshold(keys.len() - 1, keys).compile::<Legacy>();
1611+
let thresh_res = Concrete::Thresh(keys.len() - 1, keys).compile::<Legacy>();
16131612
let ops_count = thresh_res.clone().and_then(|m| Ok(m.ext.ops.op_count()));
16141613
assert_eq!(
16151614
thresh_res,

src/policy/concrete.rs

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ pub enum Policy<Pk: MiniscriptKey> {
6969
/// relative probabilities for each one.
7070
Or(Vec<(usize, Arc<Policy<Pk>>)>),
7171
/// A set of descriptors, satisfactions must be provided for `k` of them.
72-
Threshold(usize, Vec<Arc<Policy<Pk>>>),
72+
Thresh(usize, Vec<Arc<Policy<Pk>>>),
7373
}
7474

7575
/// Detailed error type for concrete policies.
@@ -187,7 +187,7 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
187187
})
188188
.collect::<Vec<_>>()
189189
}
190-
Policy::Threshold(k, ref subs) if *k == 1 => {
190+
Policy::Thresh(k, ref subs) if *k == 1 => {
191191
let total_odds = subs.len();
192192
subs.iter()
193193
.flat_map(|policy| policy.to_tapleaf_prob_vec(prob / total_odds as f64))
@@ -242,7 +242,7 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
242242
/// ### TapTree compilation
243243
///
244244
/// The policy tree constructed by root-level disjunctions over [`Policy::Or`] and
245-
/// [`Policy::Threshold`](1, ..) which is flattened into a vector (with respective
245+
/// [`Policy::Thresh`](1, ..) which is flattened into a vector (with respective
246246
/// probabilities derived from odds) of policies.
247247
///
248248
/// For example, the policy `thresh(1,or(pk(A),pk(B)),and(or(pk(C),pk(D)),pk(E)))` gives the
@@ -294,7 +294,7 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
294294
/// ### TapTree compilation
295295
///
296296
/// The policy tree constructed by root-level disjunctions over [`Policy::Or`] and
297-
/// [`Policy::Threshold`](k, ..n..) which is flattened into a vector (with respective
297+
/// [`Policy::Thresh`](k, ..n..) which is flattened into a vector (with respective
298298
/// probabilities derived from odds) of policies. For example, the policy
299299
/// `thresh(1,or(pk(A),pk(B)),and(or(pk(C),pk(D)),pk(E)))` gives the vector
300300
/// `[pk(A),pk(B),and(or(pk(C),pk(D)),pk(E)))]`.
@@ -407,13 +407,13 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
407407
.map(|(odds, pol)| (prob * *odds as f64 / total_odds as f64, pol.clone()))
408408
.collect::<Vec<_>>()
409409
}
410-
Policy::Threshold(k, subs) if *k == 1 => {
410+
Policy::Thresh(k, subs) if *k == 1 => {
411411
let total_odds = subs.len();
412412
subs.iter()
413413
.map(|pol| (prob / total_odds as f64, pol.clone()))
414414
.collect::<Vec<_>>()
415415
}
416-
Policy::Threshold(k, subs) if *k != subs.len() => generate_combination(subs, prob, *k),
416+
Policy::Thresh(k, subs) if *k != subs.len() => generate_combination(subs, prob, *k),
417417
pol => vec![(prob, Arc::new(pol.clone()))],
418418
}
419419
}
@@ -562,7 +562,7 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
562562
.enumerate()
563563
.map(|(i, (prob, _))| (*prob, child_n(i)))
564564
.collect()),
565-
Threshold(ref k, ref subs) => Threshold(*k, (0..subs.len()).map(child_n).collect()),
565+
Thresh(ref k, ref subs) => Thresh(*k, (0..subs.len()).map(child_n).collect()),
566566
};
567567
translated.push(Arc::new(new_policy));
568568
}
@@ -588,9 +588,7 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
588588
.enumerate()
589589
.map(|(i, (prob, _))| (*prob, child_n(i)))
590590
.collect())),
591-
Threshold(k, ref subs) => {
592-
Some(Threshold(*k, (0..subs.len()).map(child_n).collect()))
593-
}
591+
Thresh(k, ref subs) => Some(Thresh(*k, (0..subs.len()).map(child_n).collect())),
594592
_ => None,
595593
};
596594
match new_policy {
@@ -615,7 +613,7 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
615613
}
616614

617615
/// Gets the number of [TapLeaf](`TapTree::Leaf`)s considering exhaustive root-level [`Policy::Or`]
618-
/// and [`Policy::Threshold`] disjunctions for the `TapTree`.
616+
/// and [`Policy::Thresh`] disjunctions for the `TapTree`.
619617
#[cfg(feature = "compiler")]
620618
fn num_tap_leaves(&self) -> usize {
621619
use Policy::*;
@@ -626,7 +624,7 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
626624

627625
let num = match data.node {
628626
Or(subs) => (0..subs.len()).map(num_for_child_n).sum(),
629-
Threshold(k, subs) if *k == 1 => (0..subs.len()).map(num_for_child_n).sum(),
627+
Thresh(k, subs) if *k == 1 => (0..subs.len()).map(num_for_child_n).sum(),
630628
_ => 1,
631629
};
632630
nums.push(num);
@@ -709,7 +707,7 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
709707
let iter = (0..subs.len()).map(info_for_child_n);
710708
TimelockInfo::combine_threshold(1, iter)
711709
}
712-
Threshold(ref k, subs) => {
710+
Thresh(ref k, subs) => {
713711
let iter = (0..subs.len()).map(info_for_child_n);
714712
TimelockInfo::combine_threshold(*k, iter)
715713
}
@@ -745,7 +743,7 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
745743
return Err(PolicyError::NonBinaryArgOr);
746744
}
747745
}
748-
Threshold(k, ref subs) => {
746+
Thresh(k, ref subs) => {
749747
if k == 0 || k > subs.len() {
750748
return Err(PolicyError::IncorrectThresh);
751749
}
@@ -789,7 +787,7 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
789787
});
790788
(all_safe, atleast_one_safe && all_non_mall)
791789
}
792-
Threshold(k, ref subs) => {
790+
Thresh(k, ref subs) => {
793791
let (safe_count, non_mall_count) = (0..subs.len()).map(acc_for_child_n).fold(
794792
(0, 0),
795793
|(safe_count, non_mall_count), (safe, non_mall)| {
@@ -841,7 +839,7 @@ impl<Pk: MiniscriptKey> fmt::Debug for Policy<Pk> {
841839
}
842840
f.write_str(")")
843841
}
844-
Policy::Threshold(k, ref subs) => {
842+
Policy::Thresh(k, ref subs) => {
845843
write!(f, "thresh({}", k)?;
846844
for sub in subs {
847845
write!(f, ",{:?}", sub)?;
@@ -884,7 +882,7 @@ impl<Pk: MiniscriptKey> fmt::Display for Policy<Pk> {
884882
}
885883
f.write_str(")")
886884
}
887-
Policy::Threshold(k, ref subs) => {
885+
Policy::Thresh(k, ref subs) => {
888886
write!(f, "thresh({}", k)?;
889887
for sub in subs {
890888
write!(f, ",{}", sub)?;
@@ -999,7 +997,7 @@ impl<Pk: FromStrKey> Policy<Pk> {
999997
for arg in &top.args[1..] {
1000998
subs.push(Policy::from_tree(arg)?);
1001999
}
1002-
Ok(Policy::Threshold(thresh as usize, subs.into_iter().map(Arc::new).collect()))
1000+
Ok(Policy::Thresh(thresh as usize, subs.into_iter().map(Arc::new).collect()))
10031001
}
10041002
_ => Err(errstr(top.name)),
10051003
}
@@ -1041,7 +1039,7 @@ fn with_huffman_tree<Pk: MiniscriptKey>(
10411039
Ok(node)
10421040
}
10431041

1044-
/// Enumerates a [`Policy::Threshold(k, ..n..)`] into `n` different thresh's.
1042+
/// Enumerates a [`Policy::Thresh(k, ..n..)`] into `n` different thresh's.
10451043
///
10461044
/// ## Strategy
10471045
///
@@ -1063,7 +1061,7 @@ fn generate_combination<Pk: MiniscriptKey>(
10631061
.enumerate()
10641062
.filter_map(|(j, sub)| if j != i { Some(Arc::clone(sub)) } else { None })
10651063
.collect();
1066-
ret.push((prob / policy_vec.len() as f64, Arc::new(Policy::Threshold(k, policies))));
1064+
ret.push((prob / policy_vec.len() as f64, Arc::new(Policy::Thresh(k, policies))));
10671065
}
10681066
ret
10691067
}
@@ -1077,7 +1075,7 @@ impl<'a, Pk: MiniscriptKey> TreeLike for &'a Policy<Pk> {
10771075
| Ripemd160(_) | Hash160(_) => Tree::Nullary,
10781076
And(ref subs) => Tree::Nary(subs.iter().map(Arc::as_ref).collect()),
10791077
Or(ref v) => Tree::Nary(v.iter().map(|(_, p)| p.as_ref()).collect()),
1080-
Threshold(_, ref subs) => Tree::Nary(subs.iter().map(Arc::as_ref).collect()),
1078+
Thresh(_, ref subs) => Tree::Nary(subs.iter().map(Arc::as_ref).collect()),
10811079
}
10821080
}
10831081
}
@@ -1091,7 +1089,7 @@ impl<Pk: MiniscriptKey> TreeLike for Arc<Policy<Pk>> {
10911089
| Ripemd160(_) | Hash160(_) => Tree::Nullary,
10921090
And(ref subs) => Tree::Nary(subs.iter().map(Arc::clone).collect()),
10931091
Or(ref v) => Tree::Nary(v.iter().map(|(_, p)| Arc::clone(p)).collect()),
1094-
Threshold(_, ref subs) => Tree::Nary(subs.iter().map(Arc::clone).collect()),
1092+
Thresh(_, ref subs) => Tree::Nary(subs.iter().map(Arc::clone).collect()),
10951093
}
10961094
}
10971095
}
@@ -1137,10 +1135,7 @@ mod compiler_tests {
11371135
.map(|sub_pol| {
11381136
(
11391137
0.25,
1140-
Arc::new(Policy::Threshold(
1141-
2,
1142-
sub_pol.into_iter().map(|p| Arc::new(p)).collect(),
1143-
)),
1138+
Arc::new(Policy::Thresh(2, sub_pol.into_iter().map(|p| Arc::new(p)).collect())),
11441139
)
11451140
})
11461141
.collect::<Vec<_>>();

src/policy/mod.rs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -138,15 +138,12 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Liftable<Pk> for Terminal<Pk, Ctx> {
138138
| Terminal::NonZero(ref sub)
139139
| Terminal::ZeroNotEqual(ref sub) => sub.node.lift()?,
140140
Terminal::AndV(ref left, ref right) | Terminal::AndB(ref left, ref right) => {
141-
Semantic::Threshold(
142-
2,
143-
vec![Arc::new(left.node.lift()?), Arc::new(right.node.lift()?)],
144-
)
141+
Semantic::Thresh(2, vec![Arc::new(left.node.lift()?), Arc::new(right.node.lift()?)])
145142
}
146-
Terminal::AndOr(ref a, ref b, ref c) => Semantic::Threshold(
143+
Terminal::AndOr(ref a, ref b, ref c) => Semantic::Thresh(
147144
1,
148145
vec![
149-
Arc::new(Semantic::Threshold(
146+
Arc::new(Semantic::Thresh(
150147
2,
151148
vec![Arc::new(a.node.lift()?), Arc::new(b.node.lift()?)],
152149
)),
@@ -156,17 +153,16 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Liftable<Pk> for Terminal<Pk, Ctx> {
156153
Terminal::OrB(ref left, ref right)
157154
| Terminal::OrD(ref left, ref right)
158155
| Terminal::OrC(ref left, ref right)
159-
| Terminal::OrI(ref left, ref right) => Semantic::Threshold(
160-
1,
161-
vec![Arc::new(left.node.lift()?), Arc::new(right.node.lift()?)],
162-
),
156+
| Terminal::OrI(ref left, ref right) => {
157+
Semantic::Thresh(1, vec![Arc::new(left.node.lift()?), Arc::new(right.node.lift()?)])
158+
}
163159
Terminal::Thresh(k, ref subs) => {
164160
let semantic_subs: Result<Vec<Semantic<Pk>>, Error> =
165161
subs.iter().map(|s| s.node.lift()).collect();
166162
let semantic_subs = semantic_subs?.into_iter().map(Arc::new).collect();
167-
Semantic::Threshold(k, semantic_subs)
163+
Semantic::Thresh(k, semantic_subs)
168164
}
169-
Terminal::Multi(k, ref keys) | Terminal::MultiA(k, ref keys) => Semantic::Threshold(
165+
Terminal::Multi(k, ref keys) | Terminal::MultiA(k, ref keys) => Semantic::Thresh(
170166
k,
171167
keys.iter()
172168
.map(|k| Arc::new(Semantic::Key(k.clone())))
@@ -214,19 +210,19 @@ impl<Pk: MiniscriptKey> Liftable<Pk> for Concrete<Pk> {
214210
let semantic_subs: Result<Vec<Semantic<Pk>>, Error> =
215211
subs.iter().map(Liftable::lift).collect();
216212
let semantic_subs = semantic_subs?.into_iter().map(Arc::new).collect();
217-
Semantic::Threshold(2, semantic_subs)
213+
Semantic::Thresh(2, semantic_subs)
218214
}
219215
Concrete::Or(ref subs) => {
220216
let semantic_subs: Result<Vec<Semantic<Pk>>, Error> =
221217
subs.iter().map(|(_p, sub)| sub.lift()).collect();
222218
let semantic_subs = semantic_subs?.into_iter().map(Arc::new).collect();
223-
Semantic::Threshold(1, semantic_subs)
219+
Semantic::Thresh(1, semantic_subs)
224220
}
225-
Concrete::Threshold(k, ref subs) => {
221+
Concrete::Thresh(k, ref subs) => {
226222
let semantic_subs: Result<Vec<Semantic<Pk>>, Error> =
227223
subs.iter().map(Liftable::lift).collect();
228224
let semantic_subs = semantic_subs?.into_iter().map(Arc::new).collect();
229-
Semantic::Threshold(k, semantic_subs)
225+
Semantic::Thresh(k, semantic_subs)
230226
}
231227
}
232228
.normalized();
@@ -362,10 +358,10 @@ mod tests {
362358
.parse()
363359
.unwrap();
364360
assert_eq!(
365-
Semantic::Threshold(
361+
Semantic::Thresh(
366362
1,
367363
vec![
368-
Arc::new(Semantic::Threshold(
364+
Arc::new(Semantic::Thresh(
369365
2,
370366
vec![
371367
Arc::new(Semantic::Key(key_a)),

0 commit comments

Comments
 (0)