Skip to content

Commit 96db00f

Browse files
committed
Remove concrete::PolicyArc
We just added `Arc` wrappers around all the `Policy` vector elements, we no longer need the `PolicyArc` type.
1 parent 0c7575b commit 96db00f

File tree

1 file changed

+24
-130
lines changed

1 file changed

+24
-130
lines changed

src/policy/concrete.rs

+24-130
Original file line numberDiff line numberDiff line change
@@ -84,105 +84,6 @@ where
8484
}
8585
}
8686

87-
/// Lightweight repr of Concrete policy which corresponds directly to a
88-
/// Miniscript structure, and whose disjunctions are annotated with satisfaction
89-
/// probabilities to assist the compiler
90-
#[cfg(feature = "compiler")]
91-
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
92-
enum PolicyArc<Pk: MiniscriptKey> {
93-
/// Unsatisfiable
94-
Unsatisfiable,
95-
/// Trivially satisfiable
96-
Trivial,
97-
/// A public key which must sign to satisfy the descriptor
98-
Key(Pk),
99-
/// An absolute locktime restriction
100-
After(AbsLockTime),
101-
/// A relative locktime restriction
102-
Older(u32),
103-
/// A SHA256 whose preimage must be provided to satisfy the descriptor
104-
Sha256(Pk::Sha256),
105-
/// A SHA256d whose preimage must be provided to satisfy the descriptor
106-
Hash256(Pk::Hash256),
107-
/// A RIPEMD160 whose preimage must be provided to satisfy the descriptor
108-
Ripemd160(Pk::Ripemd160),
109-
/// A HASH160 whose preimage must be provided to satisfy the descriptor
110-
Hash160(Pk::Hash160),
111-
/// A list of sub-policies' references, all of which must be satisfied
112-
And(Vec<Arc<PolicyArc<Pk>>>),
113-
/// A list of sub-policies's references, one of which must be satisfied,
114-
/// along with relative probabilities for each one
115-
Or(Vec<(usize, Arc<PolicyArc<Pk>>)>),
116-
/// A set of descriptors' references, satisfactions must be provided for `k` of them
117-
Threshold(usize, Vec<Arc<PolicyArc<Pk>>>),
118-
}
119-
120-
#[cfg(feature = "compiler")]
121-
impl<Pk: MiniscriptKey> From<PolicyArc<Pk>> for Policy<Pk> {
122-
fn from(p: PolicyArc<Pk>) -> Self {
123-
match p {
124-
PolicyArc::Unsatisfiable => Policy::Unsatisfiable,
125-
PolicyArc::Trivial => Policy::Trivial,
126-
PolicyArc::Key(pk) => Policy::Key(pk),
127-
PolicyArc::After(t) => Policy::After(t),
128-
PolicyArc::Older(t) => Policy::Older(Sequence::from_consensus(t)),
129-
PolicyArc::Sha256(hash) => Policy::Sha256(hash),
130-
PolicyArc::Hash256(hash) => Policy::Hash256(hash),
131-
PolicyArc::Ripemd160(hash) => Policy::Ripemd160(hash),
132-
PolicyArc::Hash160(hash) => Policy::Hash160(hash),
133-
PolicyArc::And(subs) => Policy::And(
134-
subs.into_iter()
135-
.map(|pol| Self::from((*pol).clone()))
136-
.collect(),
137-
),
138-
PolicyArc::Or(subs) => Policy::Or(
139-
subs.into_iter()
140-
.map(|(odds, sub)| (odds, Self::from((*sub).clone())))
141-
.collect(),
142-
),
143-
PolicyArc::Threshold(k, subs) => Policy::Threshold(
144-
k,
145-
subs.into_iter()
146-
.map(|pol| Self::from((*pol).clone()))
147-
.collect(),
148-
),
149-
}
150-
}
151-
}
152-
153-
#[cfg(feature = "compiler")]
154-
impl<Pk: MiniscriptKey> From<Policy<Pk>> for PolicyArc<Pk> {
155-
fn from(p: Policy<Pk>) -> Self {
156-
match p {
157-
Policy::Unsatisfiable => PolicyArc::Unsatisfiable,
158-
Policy::Trivial => PolicyArc::Trivial,
159-
Policy::Key(pk) => PolicyArc::Key(pk),
160-
Policy::After(lock_time) => PolicyArc::After(lock_time),
161-
Policy::Older(Sequence(t)) => PolicyArc::Older(t),
162-
Policy::Sha256(hash) => PolicyArc::Sha256(hash),
163-
Policy::Hash256(hash) => PolicyArc::Hash256(hash),
164-
Policy::Ripemd160(hash) => PolicyArc::Ripemd160(hash),
165-
Policy::Hash160(hash) => PolicyArc::Hash160(hash),
166-
Policy::And(subs) => PolicyArc::And(
167-
subs.iter()
168-
.map(|sub| Arc::new(Self::from(sub.clone())))
169-
.collect(),
170-
),
171-
Policy::Or(subs) => PolicyArc::Or(
172-
subs.iter()
173-
.map(|(odds, sub)| (*odds, Arc::new(Self::from(sub.clone()))))
174-
.collect(),
175-
),
176-
Policy::Threshold(k, subs) => PolicyArc::Threshold(
177-
k,
178-
subs.iter()
179-
.map(|sub| Arc::new(Self::from(sub.clone())))
180-
.collect(),
181-
),
182-
}
183-
}
184-
}
185-
18687
/// Detailed error type for concrete policies.
18788
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
18889
pub enum PolicyError {
@@ -450,17 +351,12 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
450351
match policy {
451352
Policy::Trivial => None,
452353
policy => {
453-
let pol = PolicyArc::from(policy);
454-
let leaf_compilations: Vec<_> = pol
354+
let leaf_compilations: Vec<_> = policy
455355
.enumerate_policy_tree(1.0)
456356
.into_iter()
457-
.filter(|x| x.1 != Arc::new(PolicyArc::Unsatisfiable))
357+
.filter(|x| x.1 != Policy::Unsatisfiable)
458358
.map(|(prob, ref pol)| {
459-
let converted_pol = Policy::<Pk>::from((**pol).clone());
460-
(
461-
OrdF64(prob),
462-
compiler::best_compilation(&converted_pol).unwrap(),
463-
)
359+
(OrdF64(prob), compiler::best_compilation(&policy).unwrap())
464360
})
465361
.collect();
466362
let tap_tree = with_huffman_tree::<Pk>(leaf_compilations).unwrap();
@@ -523,7 +419,7 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
523419
}
524420

525421
#[cfg(feature = "compiler")]
526-
impl<Pk: MiniscriptKey> PolicyArc<Pk> {
422+
impl<Pk: MiniscriptKey> Policy<Pk> {
527423
/// Returns a vector of policies whose disjunction is isomorphic to the initial one.
528424
///
529425
/// This function is supposed to incrementally expand i.e. represent the policy as
@@ -532,21 +428,19 @@ impl<Pk: MiniscriptKey> PolicyArc<Pk> {
532428
#[cfg(feature = "compiler")]
533429
fn enumerate_pol(&self, prob: f64) -> Vec<(f64, Arc<Self>)> {
534430
match self {
535-
PolicyArc::Or(subs) => {
431+
Policy::Or(subs) => {
536432
let total_odds = subs.iter().fold(0, |acc, x| acc + x.0);
537433
subs.iter()
538434
.map(|(odds, pol)| (prob * *odds as f64 / total_odds as f64, pol.clone()))
539435
.collect::<Vec<_>>()
540436
}
541-
PolicyArc::Threshold(k, subs) if *k == 1 => {
437+
Policy::Threshold(k, subs) if *k == 1 => {
542438
let total_odds = subs.len();
543439
subs.iter()
544440
.map(|pol| (prob / total_odds as f64, pol.clone()))
545441
.collect::<Vec<_>>()
546442
}
547-
PolicyArc::Threshold(k, subs) if *k != subs.len() => {
548-
generate_combination(subs, prob, *k)
549-
}
443+
Policy::Threshold(k, subs) if *k != subs.len() => generate_combination(subs, prob, *k),
550444
pol => vec![(prob, Arc::new(pol.clone()))],
551445
}
552446
}
@@ -584,7 +478,7 @@ impl<Pk: MiniscriptKey> PolicyArc<Pk> {
584478
'outer: loop {
585479
//--- FIND a plausible node ---
586480
let mut prob: Reverse<OrdF64> = Reverse(OrdF64(0.0));
587-
let mut curr_policy: Arc<Self> = Arc::new(PolicyArc::Unsatisfiable);
481+
let mut curr_policy: Arc<Self> = Arc::new(Policy::Unsatisfiable);
588482
let mut curr_pol_replace_vec: Vec<(f64, Arc<Self>)> = vec![];
589483
let mut no_more_enum = false;
590484

@@ -1203,22 +1097,22 @@ fn with_huffman_tree<Pk: MiniscriptKey>(
12031097
/// any one of the conditions exclusively.
12041098
#[cfg(feature = "compiler")]
12051099
fn generate_combination<Pk: MiniscriptKey>(
1206-
policy_vec: &Vec<Arc<PolicyArc<Pk>>>,
1100+
policy_vec: &Vec<Arc<Policy<Pk>>>,
12071101
prob: f64,
12081102
k: usize,
1209-
) -> Vec<(f64, Arc<PolicyArc<Pk>>)> {
1103+
) -> Vec<(f64, Arc<Policy<Pk>>)> {
12101104
debug_assert!(k <= policy_vec.len());
12111105

1212-
let mut ret: Vec<(f64, Arc<PolicyArc<Pk>>)> = vec![];
1106+
let mut ret: Vec<(f64, Arc<Policy<Pk>>)> = vec![];
12131107
for i in 0..policy_vec.len() {
1214-
let policies: Vec<Arc<PolicyArc<Pk>>> = policy_vec
1108+
let policies: Vec<Arc<Policy<Pk>>> = policy_vec
12151109
.iter()
12161110
.enumerate()
12171111
.filter_map(|(j, sub)| if j != i { Some(Arc::clone(sub)) } else { None })
12181112
.collect();
12191113
ret.push((
12201114
prob / policy_vec.len() as f64,
1221-
Arc::new(PolicyArc::Threshold(k, policies)),
1115+
Arc::new(Policy::Threshold(k, policies)),
12221116
));
12231117
}
12241118
ret
@@ -1231,7 +1125,7 @@ mod compiler_tests {
12311125
use sync::Arc;
12321126

12331127
use super::Concrete;
1234-
use crate::policy::concrete::{generate_combination, PolicyArc};
1128+
use crate::policy::concrete::{generate_combination, Policy};
12351129
use crate::prelude::*;
12361130

12371131
#[test]
@@ -1242,46 +1136,46 @@ mod compiler_tests {
12421136
.collect();
12431137
let policy_vec = policies
12441138
.into_iter()
1245-
.map(|pol| Arc::new(PolicyArc::from(pol)))
1139+
.map(|pol| Arc::new(Policy::from(pol)))
12461140
.collect::<Vec<_>>();
12471141

12481142
let combinations = generate_combination(&policy_vec, 1.0, 2);
12491143

1250-
let comb_a: Vec<Arc<PolicyArc<String>>> = vec![
1144+
let comb_a: Vec<Arc<Policy<String>>> = vec![
12511145
policy_str!("pk(B)"),
12521146
policy_str!("pk(C)"),
12531147
policy_str!("pk(D)"),
12541148
]
12551149
.into_iter()
1256-
.map(|pol| Arc::new(PolicyArc::from(pol)))
1150+
.map(|pol| Arc::new(Policy::from(pol)))
12571151
.collect();
1258-
let comb_b: Vec<Arc<PolicyArc<String>>> = vec![
1152+
let comb_b: Vec<Arc<Policy<String>>> = vec![
12591153
policy_str!("pk(A)"),
12601154
policy_str!("pk(C)"),
12611155
policy_str!("pk(D)"),
12621156
]
12631157
.into_iter()
1264-
.map(|pol| Arc::new(PolicyArc::from(pol)))
1158+
.map(|pol| Arc::new(Policy::from(pol)))
12651159
.collect();
1266-
let comb_c: Vec<Arc<PolicyArc<String>>> = vec![
1160+
let comb_c: Vec<Arc<Policy<String>>> = vec![
12671161
policy_str!("pk(A)"),
12681162
policy_str!("pk(B)"),
12691163
policy_str!("pk(D)"),
12701164
]
12711165
.into_iter()
1272-
.map(|pol| Arc::new(PolicyArc::from(pol)))
1166+
.map(|pol| Arc::new(Policy::from(pol)))
12731167
.collect();
1274-
let comb_d: Vec<Arc<PolicyArc<String>>> = vec![
1168+
let comb_d: Vec<Arc<Policy<String>>> = vec![
12751169
policy_str!("pk(A)"),
12761170
policy_str!("pk(B)"),
12771171
policy_str!("pk(C)"),
12781172
]
12791173
.into_iter()
1280-
.map(|pol| Arc::new(PolicyArc::from(pol)))
1174+
.map(|pol| Arc::new(Policy::from(pol)))
12811175
.collect();
12821176
let expected_comb = vec![comb_a, comb_b, comb_c, comb_d]
12831177
.into_iter()
1284-
.map(|sub_pol| (0.25, Arc::new(PolicyArc::Threshold(2, sub_pol))))
1178+
.map(|sub_pol| (0.25, Arc::new(Policy::Threshold(2, sub_pol))))
12851179
.collect::<Vec<_>>();
12861180
assert_eq!(combinations, expected_comb);
12871181
}

0 commit comments

Comments
 (0)