@@ -84,105 +84,6 @@ where
84
84
}
85
85
}
86
86
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
-
186
87
/// Detailed error type for concrete policies.
187
88
#[ derive( Copy , Clone , PartialEq , Eq , Debug ) ]
188
89
pub enum PolicyError {
@@ -450,17 +351,12 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
450
351
match policy {
451
352
Policy :: Trivial => None ,
452
353
policy => {
453
- let pol = PolicyArc :: from ( policy) ;
454
- let leaf_compilations: Vec < _ > = pol
354
+ let leaf_compilations: Vec < _ > = policy
455
355
. enumerate_policy_tree ( 1.0 )
456
356
. into_iter ( )
457
- . filter ( |x| x. 1 != Arc :: new ( PolicyArc :: Unsatisfiable ) )
357
+ . filter ( |x| x. 1 != Policy :: Unsatisfiable )
458
358
. 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 ( ) )
464
360
} )
465
361
. collect ( ) ;
466
362
let tap_tree = with_huffman_tree :: < Pk > ( leaf_compilations) . unwrap ( ) ;
@@ -523,7 +419,7 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
523
419
}
524
420
525
421
#[ cfg( feature = "compiler" ) ]
526
- impl < Pk : MiniscriptKey > PolicyArc < Pk > {
422
+ impl < Pk : MiniscriptKey > Policy < Pk > {
527
423
/// Returns a vector of policies whose disjunction is isomorphic to the initial one.
528
424
///
529
425
/// This function is supposed to incrementally expand i.e. represent the policy as
@@ -532,21 +428,19 @@ impl<Pk: MiniscriptKey> PolicyArc<Pk> {
532
428
#[ cfg( feature = "compiler" ) ]
533
429
fn enumerate_pol ( & self , prob : f64 ) -> Vec < ( f64 , Arc < Self > ) > {
534
430
match self {
535
- PolicyArc :: Or ( subs) => {
431
+ Policy :: Or ( subs) => {
536
432
let total_odds = subs. iter ( ) . fold ( 0 , |acc, x| acc + x. 0 ) ;
537
433
subs. iter ( )
538
434
. map ( |( odds, pol) | ( prob * * odds as f64 / total_odds as f64 , pol. clone ( ) ) )
539
435
. collect :: < Vec < _ > > ( )
540
436
}
541
- PolicyArc :: Threshold ( k, subs) if * k == 1 => {
437
+ Policy :: Threshold ( k, subs) if * k == 1 => {
542
438
let total_odds = subs. len ( ) ;
543
439
subs. iter ( )
544
440
. map ( |pol| ( prob / total_odds as f64 , pol. clone ( ) ) )
545
441
. collect :: < Vec < _ > > ( )
546
442
}
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) ,
550
444
pol => vec ! [ ( prob, Arc :: new( pol. clone( ) ) ) ] ,
551
445
}
552
446
}
@@ -584,7 +478,7 @@ impl<Pk: MiniscriptKey> PolicyArc<Pk> {
584
478
' outer: loop {
585
479
//--- FIND a plausible node ---
586
480
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 ) ;
588
482
let mut curr_pol_replace_vec: Vec < ( f64 , Arc < Self > ) > = vec ! [ ] ;
589
483
let mut no_more_enum = false ;
590
484
@@ -1203,22 +1097,22 @@ fn with_huffman_tree<Pk: MiniscriptKey>(
1203
1097
/// any one of the conditions exclusively.
1204
1098
#[ cfg( feature = "compiler" ) ]
1205
1099
fn generate_combination < Pk : MiniscriptKey > (
1206
- policy_vec : & Vec < Arc < PolicyArc < Pk > > > ,
1100
+ policy_vec : & Vec < Arc < Policy < Pk > > > ,
1207
1101
prob : f64 ,
1208
1102
k : usize ,
1209
- ) -> Vec < ( f64 , Arc < PolicyArc < Pk > > ) > {
1103
+ ) -> Vec < ( f64 , Arc < Policy < Pk > > ) > {
1210
1104
debug_assert ! ( k <= policy_vec. len( ) ) ;
1211
1105
1212
- let mut ret: Vec < ( f64 , Arc < PolicyArc < Pk > > ) > = vec ! [ ] ;
1106
+ let mut ret: Vec < ( f64 , Arc < Policy < Pk > > ) > = vec ! [ ] ;
1213
1107
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
1215
1109
. iter ( )
1216
1110
. enumerate ( )
1217
1111
. filter_map ( |( j, sub) | if j != i { Some ( Arc :: clone ( sub) ) } else { None } )
1218
1112
. collect ( ) ;
1219
1113
ret. push ( (
1220
1114
prob / policy_vec. len ( ) as f64 ,
1221
- Arc :: new ( PolicyArc :: Threshold ( k, policies) ) ,
1115
+ Arc :: new ( Policy :: Threshold ( k, policies) ) ,
1222
1116
) ) ;
1223
1117
}
1224
1118
ret
@@ -1231,7 +1125,7 @@ mod compiler_tests {
1231
1125
use sync:: Arc ;
1232
1126
1233
1127
use super :: Concrete ;
1234
- use crate :: policy:: concrete:: { generate_combination, PolicyArc } ;
1128
+ use crate :: policy:: concrete:: { generate_combination, Policy } ;
1235
1129
use crate :: prelude:: * ;
1236
1130
1237
1131
#[ test]
@@ -1242,46 +1136,46 @@ mod compiler_tests {
1242
1136
. collect ( ) ;
1243
1137
let policy_vec = policies
1244
1138
. into_iter ( )
1245
- . map ( |pol| Arc :: new ( PolicyArc :: from ( pol) ) )
1139
+ . map ( |pol| Arc :: new ( Policy :: from ( pol) ) )
1246
1140
. collect :: < Vec < _ > > ( ) ;
1247
1141
1248
1142
let combinations = generate_combination ( & policy_vec, 1.0 , 2 ) ;
1249
1143
1250
- let comb_a: Vec < Arc < PolicyArc < String > > > = vec ! [
1144
+ let comb_a: Vec < Arc < Policy < String > > > = vec ! [
1251
1145
policy_str!( "pk(B)" ) ,
1252
1146
policy_str!( "pk(C)" ) ,
1253
1147
policy_str!( "pk(D)" ) ,
1254
1148
]
1255
1149
. into_iter ( )
1256
- . map ( |pol| Arc :: new ( PolicyArc :: from ( pol) ) )
1150
+ . map ( |pol| Arc :: new ( Policy :: from ( pol) ) )
1257
1151
. collect ( ) ;
1258
- let comb_b: Vec < Arc < PolicyArc < String > > > = vec ! [
1152
+ let comb_b: Vec < Arc < Policy < String > > > = vec ! [
1259
1153
policy_str!( "pk(A)" ) ,
1260
1154
policy_str!( "pk(C)" ) ,
1261
1155
policy_str!( "pk(D)" ) ,
1262
1156
]
1263
1157
. into_iter ( )
1264
- . map ( |pol| Arc :: new ( PolicyArc :: from ( pol) ) )
1158
+ . map ( |pol| Arc :: new ( Policy :: from ( pol) ) )
1265
1159
. collect ( ) ;
1266
- let comb_c: Vec < Arc < PolicyArc < String > > > = vec ! [
1160
+ let comb_c: Vec < Arc < Policy < String > > > = vec ! [
1267
1161
policy_str!( "pk(A)" ) ,
1268
1162
policy_str!( "pk(B)" ) ,
1269
1163
policy_str!( "pk(D)" ) ,
1270
1164
]
1271
1165
. into_iter ( )
1272
- . map ( |pol| Arc :: new ( PolicyArc :: from ( pol) ) )
1166
+ . map ( |pol| Arc :: new ( Policy :: from ( pol) ) )
1273
1167
. collect ( ) ;
1274
- let comb_d: Vec < Arc < PolicyArc < String > > > = vec ! [
1168
+ let comb_d: Vec < Arc < Policy < String > > > = vec ! [
1275
1169
policy_str!( "pk(A)" ) ,
1276
1170
policy_str!( "pk(B)" ) ,
1277
1171
policy_str!( "pk(C)" ) ,
1278
1172
]
1279
1173
. into_iter ( )
1280
- . map ( |pol| Arc :: new ( PolicyArc :: from ( pol) ) )
1174
+ . map ( |pol| Arc :: new ( Policy :: from ( pol) ) )
1281
1175
. collect ( ) ;
1282
1176
let expected_comb = vec ! [ comb_a, comb_b, comb_c, comb_d]
1283
1177
. 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) ) ) )
1285
1179
. collect :: < Vec < _ > > ( ) ;
1286
1180
assert_eq ! ( combinations, expected_comb) ;
1287
1181
}
0 commit comments