Skip to content

Commit b60a702

Browse files
committed
Shorten Threshold variant to Thresh
We have `Terminal::Thresh` but `Policy::Threshold` (concrete and semantic). We can shorten the policy variants to mirror the `Terminal::Thresh` variant with no loss of clarity. Done in preparation for adding a `Threshold` type which will clash with the variant when we locally import all variants within functions.
1 parent f8717e4 commit b60a702

File tree

7 files changed

+83
-91
lines changed

7 files changed

+83
-91
lines changed

src/descriptor/sortedmulti.rs

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

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

src/descriptor/tr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,7 @@ impl<Pk: MiniscriptKey> Liftable<Pk> for TapTree<Pk> {
621621
fn lift_helper<Pk: MiniscriptKey>(s: &TapTree<Pk>) -> Result<Policy<Pk>, Error> {
622622
match *s {
623623
TapTree::Tree { ref left, ref right, height: _ } => {
624-
Ok(Policy::Threshold(1, vec![lift_helper(left)?, lift_helper(right)?]))
624+
Ok(Policy::Thresh(1, vec![lift_helper(left)?, lift_helper(right)?]))
625625
}
626626
TapTree::Leaf(ref leaf) => leaf.lift(),
627627
}
@@ -636,7 +636,7 @@ impl<Pk: MiniscriptKey> Liftable<Pk> for Tr<Pk> {
636636
fn lift(&self) -> Result<Policy<Pk>, Error> {
637637
match &self.tree {
638638
Some(root) => {
639-
Ok(Policy::Threshold(1, vec![Policy::Key(self.internal_key.clone()), root.lift()?]))
639+
Ok(Policy::Thresh(1, vec![Policy::Key(self.internal_key.clone()), root.lift()?]))
640640
}
641641
None => Ok(Policy::Key(self.internal_key.clone())),
642642
}

src/iter/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ impl<'a, Pk: MiniscriptKey> TreeLike for &'a policy::Concrete<Pk> {
7777
| Ripemd160(_) | Hash160(_) => Tree::Nullary,
7878
And(ref subs) => Tree::Nary(subs.iter().map(Arc::as_ref).collect()),
7979
Or(ref v) => Tree::Nary(v.iter().map(|(_, p)| p.as_ref()).collect()),
80-
Threshold(_, ref subs) => Tree::Nary(subs.iter().map(Arc::as_ref).collect()),
80+
Thresh(_, ref subs) => Tree::Nary(subs.iter().map(Arc::as_ref).collect()),
8181
}
8282
}
8383
}
@@ -90,7 +90,7 @@ impl<'a, Pk: MiniscriptKey> TreeLike for Arc<policy::Concrete<Pk>> {
9090
| Ripemd160(_) | Hash160(_) => Tree::Nullary,
9191
And(ref subs) => Tree::Nary(subs.iter().map(Arc::clone).collect()),
9292
Or(ref v) => Tree::Nary(v.iter().map(|(_, p)| Arc::clone(p)).collect()),
93-
Threshold(_, ref subs) => Tree::Nary(subs.iter().map(Arc::clone).collect()),
93+
Thresh(_, ref subs) => Tree::Nary(subs.iter().map(Arc::clone).collect()),
9494
}
9595
}
9696
}

src/policy/compiler.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -920,7 +920,7 @@ where
920920
compile_binary!(&mut l_comp[3], &mut r_comp[2], [lw, rw], Terminal::OrI);
921921
compile_binary!(&mut r_comp[3], &mut l_comp[2], [rw, lw], Terminal::OrI);
922922
}
923-
Concrete::Threshold(k, ref subs) => {
923+
Concrete::Thresh(k, ref subs) => {
924924
let n = subs.len();
925925
let k_over_n = k as f64 / n as f64;
926926

@@ -1301,7 +1301,7 @@ mod tests {
13011301
let policy: BPolicy = Concrete::Or(vec![
13021302
(
13031303
127,
1304-
Arc::new(Concrete::Threshold(
1304+
Arc::new(Concrete::Thresh(
13051305
3,
13061306
key_pol[0..5].iter().map(|p| (p.clone()).into()).collect(),
13071307
)),
@@ -1310,7 +1310,7 @@ mod tests {
13101310
1,
13111311
Arc::new(Concrete::And(vec![
13121312
Arc::new(Concrete::Older(Sequence::from_height(10000))),
1313-
Arc::new(Concrete::Threshold(
1313+
Arc::new(Concrete::Thresh(
13141314
2,
13151315
key_pol[5..8].iter().map(|p| (p.clone()).into()).collect(),
13161316
)),
@@ -1430,7 +1430,7 @@ mod tests {
14301430
.iter()
14311431
.map(|pubkey| Arc::new(Concrete::Key(*pubkey)))
14321432
.collect();
1433-
let big_thresh = Concrete::Threshold(*k, pubkeys);
1433+
let big_thresh = Concrete::Thresh(*k, pubkeys);
14341434
let big_thresh_ms: SegwitMiniScript = big_thresh.compile().unwrap();
14351435
if *k == 21 {
14361436
// N * (PUSH + pubkey + CHECKSIGVERIFY)
@@ -1466,8 +1466,8 @@ mod tests {
14661466
.collect();
14671467

14681468
let thresh_res: Result<SegwitMiniScript, _> = Concrete::Or(vec![
1469-
(1, Arc::new(Concrete::Threshold(keys_a.len(), keys_a))),
1470-
(1, Arc::new(Concrete::Threshold(keys_b.len(), keys_b))),
1469+
(1, Arc::new(Concrete::Thresh(keys_a.len(), keys_a))),
1470+
(1, Arc::new(Concrete::Thresh(keys_b.len(), keys_b))),
14711471
])
14721472
.compile();
14731473
let script_size = thresh_res.clone().and_then(|m| Ok(m.script_size()));
@@ -1484,8 +1484,7 @@ mod tests {
14841484
.iter()
14851485
.map(|pubkey| Arc::new(Concrete::Key(*pubkey)))
14861486
.collect();
1487-
let thresh_res: Result<SegwitMiniScript, _> =
1488-
Concrete::Threshold(keys.len(), keys).compile();
1487+
let thresh_res: Result<SegwitMiniScript, _> = Concrete::Thresh(keys.len(), keys).compile();
14891488
let n_elements = thresh_res
14901489
.clone()
14911490
.and_then(|m| Ok(m.max_satisfaction_witness_elements()));
@@ -1506,7 +1505,7 @@ mod tests {
15061505
.map(|pubkey| Arc::new(Concrete::Key(*pubkey)))
15071506
.collect();
15081507
let thresh_res: Result<SegwitMiniScript, _> =
1509-
Concrete::Threshold(keys.len() - 1, keys).compile();
1508+
Concrete::Thresh(keys.len() - 1, keys).compile();
15101509
let ops_count = thresh_res.clone().and_then(|m| Ok(m.ext.ops.op_count()));
15111510
assert_eq!(
15121511
thresh_res,
@@ -1520,7 +1519,7 @@ mod tests {
15201519
.iter()
15211520
.map(|pubkey| Arc::new(Concrete::Key(*pubkey)))
15221521
.collect();
1523-
let thresh_res = Concrete::Threshold(keys.len() - 1, keys).compile::<Legacy>();
1522+
let thresh_res = Concrete::Thresh(keys.len() - 1, keys).compile::<Legacy>();
15241523
let ops_count = thresh_res.clone().and_then(|m| Ok(m.ext.ops.op_count()));
15251524
assert_eq!(
15261525
thresh_res,

src/policy/concrete.rs

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ pub enum Policy<Pk: MiniscriptKey> {
6767
/// relative probabilities for each one.
6868
Or(Vec<(usize, Arc<Policy<Pk>>)>),
6969
/// A set of descriptors, satisfactions must be provided for `k` of them.
70-
Threshold(usize, Vec<Arc<Policy<Pk>>>),
70+
Thresh(usize, Vec<Arc<Policy<Pk>>>),
7171
}
7272

7373
impl<Pk> Policy<Pk>
@@ -210,7 +210,7 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
210210
})
211211
.collect::<Vec<_>>()
212212
}
213-
Policy::Threshold(k, ref subs) if *k == 1 => {
213+
Policy::Thresh(k, ref subs) if *k == 1 => {
214214
let total_odds = subs.len();
215215
subs.iter()
216216
.flat_map(|policy| policy.to_tapleaf_prob_vec(prob / total_odds as f64))
@@ -265,7 +265,7 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
265265
/// ### TapTree compilation
266266
///
267267
/// The policy tree constructed by root-level disjunctions over [`Policy::Or`] and
268-
/// [`Policy::Threshold`](1, ..) which is flattened into a vector (with respective
268+
/// [`Policy::Thresh`](1, ..) which is flattened into a vector (with respective
269269
/// probabilities derived from odds) of policies.
270270
///
271271
/// For example, the policy `thresh(1,or(pk(A),pk(B)),and(or(pk(C),pk(D)),pk(E)))` gives the
@@ -317,7 +317,7 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
317317
/// ### TapTree compilation
318318
///
319319
/// The policy tree constructed by root-level disjunctions over [`Policy::Or`] and
320-
/// [`Policy::Threshold`](k, ..n..) which is flattened into a vector (with respective
320+
/// [`Policy::Thresh`](k, ..n..) which is flattened into a vector (with respective
321321
/// probabilities derived from odds) of policies. For example, the policy
322322
/// `thresh(1,or(pk(A),pk(B)),and(or(pk(C),pk(D)),pk(E)))` gives the vector
323323
/// `[pk(A),pk(B),and(or(pk(C),pk(D)),pk(E)))]`.
@@ -430,13 +430,13 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
430430
.map(|(odds, pol)| (prob * *odds as f64 / total_odds as f64, pol.clone()))
431431
.collect::<Vec<_>>()
432432
}
433-
Policy::Threshold(k, subs) if *k == 1 => {
433+
Policy::Thresh(k, subs) if *k == 1 => {
434434
let total_odds = subs.len();
435435
subs.iter()
436436
.map(|pol| (prob / total_odds as f64, pol.clone()))
437437
.collect::<Vec<_>>()
438438
}
439-
Policy::Threshold(k, subs) if *k != subs.len() => generate_combination(subs, prob, *k),
439+
Policy::Thresh(k, subs) if *k != subs.len() => generate_combination(subs, prob, *k),
440440
pol => vec![(prob, Arc::new(pol.clone()))],
441441
}
442442
}
@@ -579,7 +579,7 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
579579
Hash160(ref h) => t.hash160(h).map(Hash160)?,
580580
Older(ref n) => Older(*n),
581581
After(ref n) => After(*n),
582-
Threshold(ref k, ref subs) => Threshold(*k, (0..subs.len()).map(child_n).collect()),
582+
Thresh(ref k, ref subs) => Thresh(*k, (0..subs.len()).map(child_n).collect()),
583583
And(ref subs) => And((0..subs.len()).map(child_n).collect()),
584584
Or(ref subs) => Or(subs
585585
.iter()
@@ -605,9 +605,7 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
605605

606606
let new_policy = match data.node.as_ref() {
607607
Policy::Key(ref k) if k.clone() == *key => Some(Policy::Unsatisfiable),
608-
Threshold(k, ref subs) => {
609-
Some(Threshold(*k, (0..subs.len()).map(child_n).collect()))
610-
}
608+
Thresh(k, ref subs) => Some(Thresh(*k, (0..subs.len()).map(child_n).collect())),
611609
And(ref subs) => Some(And((0..subs.len()).map(child_n).collect())),
612610
Or(ref subs) => Some(Or(subs
613611
.iter()
@@ -638,7 +636,7 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
638636
}
639637

640638
/// Gets the number of [TapLeaf](`TapTree::Leaf`)s considering exhaustive root-level [`Policy::Or`]
641-
/// and [`Policy::Threshold`] disjunctions for the `TapTree`.
639+
/// and [`Policy::Thresh`] disjunctions for the `TapTree`.
642640
#[cfg(feature = "compiler")]
643641
fn num_tap_leaves(&self) -> usize {
644642
use Policy::*;
@@ -649,7 +647,7 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
649647

650648
let num = match data.node {
651649
Or(subs) => (0..subs.len()).map(num_for_child_n).sum(),
652-
Threshold(k, subs) if *k == 1 => (0..subs.len()).map(num_for_child_n).sum(),
650+
Thresh(k, subs) if *k == 1 => (0..subs.len()).map(num_for_child_n).sum(),
653651
_ => 1,
654652
};
655653
nums.push(num);
@@ -724,7 +722,7 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
724722
cltv_with_time: false,
725723
contains_combination: false,
726724
},
727-
Threshold(ref k, subs) => {
725+
Thresh(ref k, subs) => {
728726
let iter = (0..subs.len()).map(info_for_child_n);
729727
TimelockInfo::combine_threshold(*k, iter)
730728
}
@@ -772,7 +770,7 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
772770
Ok(())
773771
}
774772
}
775-
Policy::Threshold(k, ref subs) => {
773+
Policy::Thresh(k, ref subs) => {
776774
if k == 0 || k > subs.len() {
777775
Err(PolicyError::IncorrectThresh)
778776
} else {
@@ -821,7 +819,7 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
821819
| Policy::Hash160(_)
822820
| Policy::After(_)
823821
| Policy::Older(_) => (false, true),
824-
Policy::Threshold(k, ref subs) => {
822+
Policy::Thresh(k, ref subs) => {
825823
let (safe_count, non_mall_count) = subs
826824
.iter()
827825
.map(|sub| sub.is_safe_nonmalleable())
@@ -884,7 +882,7 @@ impl<Pk: MiniscriptKey> fmt::Debug 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)?;
@@ -927,7 +925,7 @@ impl<Pk: MiniscriptKey> fmt::Display for Policy<Pk> {
927925
}
928926
f.write_str(")")
929927
}
930-
Policy::Threshold(k, ref subs) => {
928+
Policy::Thresh(k, ref subs) => {
931929
write!(f, "thresh({}", k)?;
932930
for sub in subs {
933931
write!(f, ",{}", sub)?;
@@ -1052,7 +1050,7 @@ impl_block_str!(
10521050
for arg in &top.args[1..] {
10531051
subs.push(Policy::from_tree(arg)?);
10541052
}
1055-
Ok(Policy::Threshold(thresh as usize, subs.into_iter().map(Arc::new).collect()))
1053+
Ok(Policy::Thresh(thresh as usize, subs.into_iter().map(Arc::new).collect()))
10561054
}
10571055
_ => Err(errstr(top.name)),
10581056
}
@@ -1095,7 +1093,7 @@ fn with_huffman_tree<Pk: MiniscriptKey>(
10951093
Ok(node)
10961094
}
10971095

1098-
/// Enumerates a [`Policy::Threshold(k, ..n..)`] into `n` different thresh's.
1096+
/// Enumerates a [`Policy::Thresh(k, ..n..)`] into `n` different thresh's.
10991097
///
11001098
/// ## Strategy
11011099
///
@@ -1117,7 +1115,7 @@ fn generate_combination<Pk: MiniscriptKey>(
11171115
.enumerate()
11181116
.filter_map(|(j, sub)| if j != i { Some(Arc::clone(sub)) } else { None })
11191117
.collect();
1120-
ret.push((prob / policy_vec.len() as f64, Arc::new(Policy::Threshold(k, policies))));
1118+
ret.push((prob / policy_vec.len() as f64, Arc::new(Policy::Thresh(k, policies))));
11211119
}
11221120
ret
11231121
}
@@ -1165,10 +1163,7 @@ mod compiler_tests {
11651163
.map(|sub_pol| {
11661164
(
11671165
0.25,
1168-
Arc::new(Policy::Threshold(
1169-
2,
1170-
sub_pol.into_iter().map(|p| Arc::new(p)).collect(),
1171-
)),
1166+
Arc::new(Policy::Thresh(2, sub_pol.into_iter().map(|p| Arc::new(p)).collect())),
11721167
)
11731168
})
11741169
.collect::<Vec<_>>();

src/policy/mod.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -136,27 +136,27 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Liftable<Pk> for Terminal<Pk, Ctx> {
136136
| Terminal::NonZero(ref sub)
137137
| Terminal::ZeroNotEqual(ref sub) => sub.node.lift()?,
138138
Terminal::AndV(ref left, ref right) | Terminal::AndB(ref left, ref right) => {
139-
Semantic::Threshold(2, vec![left.node.lift()?, right.node.lift()?])
139+
Semantic::Thresh(2, vec![left.node.lift()?, right.node.lift()?])
140140
}
141-
Terminal::AndOr(ref a, ref b, ref c) => Semantic::Threshold(
141+
Terminal::AndOr(ref a, ref b, ref c) => Semantic::Thresh(
142142
1,
143143
vec![
144-
Semantic::Threshold(2, vec![a.node.lift()?, b.node.lift()?]),
144+
Semantic::Thresh(2, vec![a.node.lift()?, b.node.lift()?]),
145145
c.node.lift()?,
146146
],
147147
),
148148
Terminal::OrB(ref left, ref right)
149149
| Terminal::OrD(ref left, ref right)
150150
| Terminal::OrC(ref left, ref right)
151151
| Terminal::OrI(ref left, ref right) => {
152-
Semantic::Threshold(1, vec![left.node.lift()?, right.node.lift()?])
152+
Semantic::Thresh(1, vec![left.node.lift()?, right.node.lift()?])
153153
}
154154
Terminal::Thresh(k, ref subs) => {
155155
let semantic_subs: Result<_, Error> = subs.iter().map(|s| s.node.lift()).collect();
156-
Semantic::Threshold(k, semantic_subs?)
156+
Semantic::Thresh(k, semantic_subs?)
157157
}
158158
Terminal::Multi(k, ref keys) | Terminal::MultiA(k, ref keys) => {
159-
Semantic::Threshold(k, keys.iter().map(|k| Semantic::Key(k.clone())).collect())
159+
Semantic::Thresh(k, keys.iter().map(|k| Semantic::Key(k.clone())).collect())
160160
}
161161
}
162162
.normalized();
@@ -198,16 +198,16 @@ impl<Pk: MiniscriptKey> Liftable<Pk> for Concrete<Pk> {
198198
Concrete::Hash160(ref h) => Semantic::Hash160(h.clone()),
199199
Concrete::And(ref subs) => {
200200
let semantic_subs: Result<_, Error> = subs.iter().map(Liftable::lift).collect();
201-
Semantic::Threshold(2, semantic_subs?)
201+
Semantic::Thresh(2, semantic_subs?)
202202
}
203203
Concrete::Or(ref subs) => {
204204
let semantic_subs: Result<_, Error> =
205205
subs.iter().map(|(_p, sub)| sub.lift()).collect();
206-
Semantic::Threshold(1, semantic_subs?)
206+
Semantic::Thresh(1, semantic_subs?)
207207
}
208-
Concrete::Threshold(k, ref subs) => {
208+
Concrete::Thresh(k, ref subs) => {
209209
let semantic_subs: Result<_, Error> = subs.iter().map(Liftable::lift).collect();
210-
Semantic::Threshold(k, semantic_subs?)
210+
Semantic::Thresh(k, semantic_subs?)
211211
}
212212
}
213213
.normalized();
@@ -345,10 +345,10 @@ mod tests {
345345
.parse()
346346
.unwrap();
347347
assert_eq!(
348-
Semantic::Threshold(
348+
Semantic::Thresh(
349349
1,
350350
vec![
351-
Semantic::Threshold(
351+
Semantic::Thresh(
352352
2,
353353
vec![
354354
Semantic::Key(key_a),

0 commit comments

Comments
 (0)