@@ -29,7 +29,14 @@ use crate::{
29
29
#[ derive( Clone , Ord , PartialOrd , Eq , PartialEq , Hash ) ]
30
30
pub enum TapTree < Pk : MiniscriptKey > {
31
31
/// A taproot tree structure
32
- Tree ( Arc < TapTree < Pk > > , Arc < TapTree < Pk > > ) ,
32
+ Tree {
33
+ /// Left tree branch.
34
+ left : Arc < TapTree < Pk > > ,
35
+ /// Right tree branch.
36
+ right : Arc < TapTree < Pk > > ,
37
+ /// Tree height, defined as `1 + max(left_height, right_height)`.
38
+ height : usize ,
39
+ } ,
33
40
/// A taproot leaf denoting a spending condition
34
41
// A new leaf version would require a new Context, therefore there is no point
35
42
// in adding a LeafVersion with Leaf type here. All Miniscripts right now
@@ -134,12 +141,17 @@ impl<Pk: MiniscriptKey> TapTree<Pk> {
134
141
T : Translator < Pk , Q , E > ,
135
142
Q : MiniscriptKey ,
136
143
{
137
- let frag = match self {
138
- TapTree :: Tree ( l, r) => TapTree :: Tree (
139
- Arc :: new ( l. translate_helper ( t) ?) ,
140
- Arc :: new ( r. translate_helper ( t) ?) ,
141
- ) ,
142
- TapTree :: Leaf ( ms) => TapTree :: Leaf ( Arc :: new ( ms. translate_pk ( t) ?) ) ,
144
+ let frag = match * self {
145
+ TapTree :: Tree {
146
+ ref left,
147
+ ref right,
148
+ ref height,
149
+ } => TapTree :: Tree {
150
+ left : Arc :: new ( left. translate_helper ( t) ?) ,
151
+ right : Arc :: new ( right. translate_helper ( t) ?) ,
152
+ height : * height,
153
+ } ,
154
+ TapTree :: Leaf ( ref ms) => TapTree :: Leaf ( Arc :: new ( ms. translate_pk ( t) ?) ) ,
143
155
} ;
144
156
Ok ( frag)
145
157
}
@@ -148,7 +160,11 @@ impl<Pk: MiniscriptKey> TapTree<Pk> {
148
160
impl < Pk : MiniscriptKey > fmt:: Display for TapTree < Pk > {
149
161
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
150
162
match self {
151
- TapTree :: Tree ( ref left, ref right) => write ! ( f, "{{{},{}}}" , * left, * right) ,
163
+ TapTree :: Tree {
164
+ ref left,
165
+ ref right,
166
+ height : _,
167
+ } => write ! ( f, "{{{},{}}}" , * left, * right) ,
152
168
TapTree :: Leaf ( ref script) => write ! ( f, "{}" , * script) ,
153
169
}
154
170
}
@@ -157,7 +173,11 @@ impl<Pk: MiniscriptKey> fmt::Display for TapTree<Pk> {
157
173
impl < Pk : MiniscriptKey > fmt:: Debug for TapTree < Pk > {
158
174
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
159
175
match self {
160
- TapTree :: Tree ( ref left, ref right) => write ! ( f, "{{{:?},{:?}}}" , * left, * right) ,
176
+ TapTree :: Tree {
177
+ ref left,
178
+ ref right,
179
+ height : _,
180
+ } => write ! ( f, "{{{:?},{:?}}}" , * left, * right) ,
161
181
TapTree :: Leaf ( ref script) => write ! ( f, "{:?}" , * script) ,
162
182
}
163
183
}
@@ -413,9 +433,13 @@ where
413
433
fn next ( & mut self ) -> Option < Self :: Item > {
414
434
while let Some ( ( depth, last) ) = self . stack . pop ( ) {
415
435
match * last {
416
- TapTree :: Tree ( ref l, ref r) => {
417
- self . stack . push ( ( depth + 1 , r) ) ;
418
- self . stack . push ( ( depth + 1 , l) ) ;
436
+ TapTree :: Tree {
437
+ ref left,
438
+ ref right,
439
+ height : _,
440
+ } => {
441
+ self . stack . push ( ( depth + 1 , right) ) ;
442
+ self . stack . push ( ( depth + 1 , left) ) ;
419
443
}
420
444
TapTree :: Leaf ( ref ms) => return Some ( ( depth, ms) ) ,
421
445
}
@@ -437,7 +461,7 @@ impl_block_str!(
437
461
expression:: Tree { name, args } if name. is_empty( ) && args. len( ) == 2 => {
438
462
let left = Self :: parse_tr_script_spend( & args[ 0 ] ) ?;
439
463
let right = Self :: parse_tr_script_spend( & args[ 1 ] ) ?;
440
- Ok ( TapTree :: Tree ( Arc :: new ( left) , Arc :: new ( right) ) )
464
+ Ok ( TapTree :: combine ( left, right) )
441
465
}
442
466
_ => Err ( Error :: Unexpected (
443
467
"unknown format for script spending paths while parsing taproot descriptor"
@@ -595,10 +619,15 @@ fn split_once(inp: &str, delim: char) -> Option<(&str, &str)> {
595
619
impl < Pk : MiniscriptKey > Liftable < Pk > for TapTree < Pk > {
596
620
fn lift ( & self ) -> Result < Policy < Pk > , Error > {
597
621
fn lift_helper < Pk : MiniscriptKey > ( s : & TapTree < Pk > ) -> Result < Policy < Pk > , Error > {
598
- match s {
599
- TapTree :: Tree ( ref l, ref r) => {
600
- Ok ( Policy :: Threshold ( 1 , vec ! [ lift_helper( l) ?, lift_helper( r) ?] ) )
601
- }
622
+ match * s {
623
+ TapTree :: Tree {
624
+ ref left,
625
+ ref right,
626
+ height : _,
627
+ } => Ok ( Policy :: Threshold (
628
+ 1 ,
629
+ vec ! [ lift_helper( left) ?, lift_helper( right) ?] ,
630
+ ) ) ,
602
631
TapTree :: Leaf ( ref leaf) => leaf. lift ( ) ,
603
632
}
604
633
}
@@ -748,6 +777,6 @@ mod tests {
748
777
fn height ( ) {
749
778
let desc = descriptor ( ) ;
750
779
let tr = Tr :: < String > :: from_str ( & desc) . unwrap ( ) ;
751
- assert_eq ! ( tr. tap_tree( ) . as_ref( ) . unwrap( ) . tap_tree_height ( ) , 2 ) ;
780
+ assert_eq ! ( tr. tap_tree( ) . as_ref( ) . unwrap( ) . height ( ) , 2 ) ;
752
781
}
753
782
}
0 commit comments