@@ -25,7 +25,7 @@ use crate::miniscript::{Miniscript, ScriptContext};
2525use crate :: sync:: Arc ;
2626#[ cfg( all( not( feature = "std" ) , not( test) ) ) ]
2727use crate :: Vec ;
28- use crate :: { Error , MiniscriptKey , Terminal } ;
28+ use crate :: { Error , MiniscriptKey , Terminal , Threshold } ;
2929
3030/// Policy entailment algorithm maximum number of terminals allowed.
3131const ENTAILMENT_MAX_TERMINALS : usize = 20 ;
@@ -138,35 +138,40 @@ 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 :: Thresh ( 2 , vec ! [ Arc :: new( left. node. lift( ) ?) , Arc :: new( right. node. lift( ) ?) ] )
141+ Semantic :: Thresh ( Threshold :: and (
142+ Arc :: new ( left. node . lift ( ) ?) ,
143+ Arc :: new ( right. node . lift ( ) ?) ,
144+ ) )
142145 }
143- Terminal :: AndOr ( ref a, ref b, ref c) => Semantic :: Thresh (
144- 1 ,
145- vec ! [
146- Arc :: new( Semantic :: Thresh (
147- 2 ,
148- vec![ Arc :: new( a. node. lift( ) ?) , Arc :: new( b. node. lift( ) ?) ] ,
149- ) ) ,
150- Arc :: new( c. node. lift( ) ?) ,
151- ] ,
152- ) ,
146+ Terminal :: AndOr ( ref a, ref b, ref c) => Semantic :: Thresh ( Threshold :: or (
147+ Arc :: new ( Semantic :: Thresh ( Threshold :: and (
148+ Arc :: new ( a. node . lift ( ) ?) ,
149+ Arc :: new ( b. node . lift ( ) ?) ,
150+ ) ) ) ,
151+ Arc :: new ( c. node . lift ( ) ?) ,
152+ ) ) ,
153153 Terminal :: OrB ( ref left, ref right)
154154 | Terminal :: OrD ( ref left, ref right)
155155 | Terminal :: OrC ( ref left, ref right)
156- | Terminal :: OrI ( ref left, ref right) => {
157- Semantic :: Thresh ( 1 , vec ! [ Arc :: new( left. node. lift( ) ?) , Arc :: new( right. node. lift( ) ?) ] )
158- }
156+ | Terminal :: OrI ( ref left, ref right) => Semantic :: Thresh ( Threshold :: or (
157+ Arc :: new ( left. node . lift ( ) ?) ,
158+ Arc :: new ( right. node . lift ( ) ?) ,
159+ ) ) ,
159160 Terminal :: Thresh ( k, ref subs) => {
160161 let semantic_subs: Result < Vec < Semantic < Pk > > , Error > =
161162 subs. iter ( ) . map ( |s| s. node . lift ( ) ) . collect ( ) ;
162163 let semantic_subs = semantic_subs?. into_iter ( ) . map ( Arc :: new) . collect ( ) ;
163- Semantic :: Thresh ( k, semantic_subs)
164+ // unwrap to be removed in a later commit
165+ Semantic :: Thresh ( Threshold :: new ( k, semantic_subs) . unwrap ( ) )
164166 }
165167 Terminal :: Multi ( k, ref keys) | Terminal :: MultiA ( k, ref keys) => Semantic :: Thresh (
166- k,
167- keys. iter ( )
168- . map ( |k| Arc :: new ( Semantic :: Key ( k. clone ( ) ) ) )
169- . collect ( ) ,
168+ Threshold :: new (
169+ k,
170+ keys. iter ( )
171+ . map ( |k| Arc :: new ( Semantic :: Key ( k. clone ( ) ) ) )
172+ . collect ( ) ,
173+ )
174+ . unwrap ( ) , // unwrap to be removed in a later commit
170175 ) ,
171176 }
172177 . normalized ( ) ;
@@ -210,19 +215,19 @@ impl<Pk: MiniscriptKey> Liftable<Pk> for Concrete<Pk> {
210215 let semantic_subs: Result < Vec < Semantic < Pk > > , Error > =
211216 subs. iter ( ) . map ( Liftable :: lift) . collect ( ) ;
212217 let semantic_subs = semantic_subs?. into_iter ( ) . map ( Arc :: new) . collect ( ) ;
213- Semantic :: Thresh ( 2 , semantic_subs)
218+ Semantic :: Thresh ( Threshold :: new ( 2 , semantic_subs) . unwrap ( ) )
214219 }
215220 Concrete :: Or ( ref subs) => {
216221 let semantic_subs: Result < Vec < Semantic < Pk > > , Error > =
217222 subs. iter ( ) . map ( |( _p, sub) | sub. lift ( ) ) . collect ( ) ;
218223 let semantic_subs = semantic_subs?. into_iter ( ) . map ( Arc :: new) . collect ( ) ;
219- Semantic :: Thresh ( 1 , semantic_subs)
224+ Semantic :: Thresh ( Threshold :: new ( 1 , semantic_subs) . unwrap ( ) )
220225 }
221226 Concrete :: Thresh ( k, ref subs) => {
222227 let semantic_subs: Result < Vec < Semantic < Pk > > , Error > =
223228 subs. iter ( ) . map ( Liftable :: lift) . collect ( ) ;
224229 let semantic_subs = semantic_subs?. into_iter ( ) . map ( Arc :: new) . collect ( ) ;
225- Semantic :: Thresh ( k, semantic_subs)
230+ Semantic :: Thresh ( Threshold :: new ( k, semantic_subs) . unwrap ( ) )
226231 }
227232 }
228233 . normalized ( ) ;
@@ -358,19 +363,13 @@ mod tests {
358363 . parse ( )
359364 . unwrap ( ) ;
360365 assert_eq ! (
361- Semantic :: Thresh (
362- 1 ,
363- vec![
364- Arc :: new( Semantic :: Thresh (
365- 2 ,
366- vec![
367- Arc :: new( Semantic :: Key ( key_a) ) ,
368- Arc :: new( Semantic :: Older ( RelLockTime :: from_height( 42 ) ) )
369- ]
370- ) ) ,
371- Arc :: new( Semantic :: Key ( key_b) )
372- ]
373- ) ,
366+ Semantic :: Thresh ( Threshold :: or(
367+ Arc :: new( Semantic :: Thresh ( Threshold :: and(
368+ Arc :: new( Semantic :: Key ( key_a) ) ,
369+ Arc :: new( Semantic :: Older ( RelLockTime :: from_height( 42 ) ) )
370+ ) ) ) ,
371+ Arc :: new( Semantic :: Key ( key_b) )
372+ ) ) ,
374373 ms_str. lift( ) . unwrap( )
375374 ) ;
376375 }
0 commit comments