Skip to content

Commit b463007

Browse files
committed
tr: move TapTree into taptree module
This completes the move of all the taptree-related stuff into its own module, where it will be easier to mess around with this stuff.
1 parent 82f58ce commit b463007

File tree

2 files changed

+87
-83
lines changed

2 files changed

+87
-83
lines changed

src/descriptor/tr/mod.rs

Lines changed: 1 addition & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -28,28 +28,7 @@ use crate::{
2828

2929
mod taptree;
3030

31-
pub use self::taptree::{TapTreeIter, TapTreeIterItem};
32-
33-
/// A Taproot Tree representation.
34-
// Hidden leaves are not yet supported in descriptor spec. Conceptually, it should
35-
// be simple to integrate those here, but it is best to wait on core for the exact syntax.
36-
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
37-
pub enum TapTree<Pk: MiniscriptKey> {
38-
/// A taproot tree structure
39-
Tree {
40-
/// Left tree branch.
41-
left: Arc<TapTree<Pk>>,
42-
/// Right tree branch.
43-
right: Arc<TapTree<Pk>>,
44-
/// Tree height, defined as `1 + max(left_height, right_height)`.
45-
height: usize,
46-
},
47-
/// A taproot leaf denoting a spending condition
48-
// A new leaf version would require a new Context, therefore there is no point
49-
// in adding a LeafVersion with Leaf type here. All Miniscripts right now
50-
// are of Leafversion::default
51-
Leaf(Arc<Miniscript<Pk, Tap>>),
52-
}
31+
pub use self::taptree::{TapTree, TapTreeIter, TapTreeIterItem};
5332

5433
/// A taproot descriptor
5534
pub struct Tr<Pk: MiniscriptKey> {
@@ -115,64 +94,6 @@ impl<Pk: MiniscriptKey> hash::Hash for Tr<Pk> {
11594
}
11695
}
11796

118-
impl<Pk: MiniscriptKey> TapTree<Pk> {
119-
/// Creates a `TapTree` by combining `left` and `right` tree nodes.
120-
pub fn combine(left: TapTree<Pk>, right: TapTree<Pk>) -> Self {
121-
let height = 1 + cmp::max(left.height(), right.height());
122-
TapTree::Tree { left: Arc::new(left), right: Arc::new(right), height }
123-
}
124-
125-
/// Returns the height of this tree.
126-
pub fn height(&self) -> usize {
127-
match *self {
128-
TapTree::Tree { left: _, right: _, height } => height,
129-
TapTree::Leaf(..) => 0,
130-
}
131-
}
132-
133-
/// Iterates over all miniscripts in DFS walk order compatible with the
134-
/// PSBT requirements (BIP 371).
135-
pub fn iter(&self) -> TapTreeIter<Pk> { TapTreeIter::from_tree(self) }
136-
137-
// Helper function to translate keys
138-
fn translate_helper<T>(&self, t: &mut T) -> Result<TapTree<T::TargetPk>, TranslateErr<T::Error>>
139-
where
140-
T: Translator<Pk>,
141-
{
142-
let frag = match *self {
143-
TapTree::Tree { ref left, ref right, ref height } => TapTree::Tree {
144-
left: Arc::new(left.translate_helper(t)?),
145-
right: Arc::new(right.translate_helper(t)?),
146-
height: *height,
147-
},
148-
TapTree::Leaf(ref ms) => TapTree::Leaf(Arc::new(ms.translate_pk(t)?)),
149-
};
150-
Ok(frag)
151-
}
152-
}
153-
154-
impl<Pk: MiniscriptKey> fmt::Display for TapTree<Pk> {
155-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
156-
match self {
157-
TapTree::Tree { ref left, ref right, height: _ } => {
158-
write!(f, "{{{},{}}}", *left, *right)
159-
}
160-
TapTree::Leaf(ref script) => write!(f, "{}", *script),
161-
}
162-
}
163-
}
164-
165-
impl<Pk: MiniscriptKey> fmt::Debug for TapTree<Pk> {
166-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
167-
match self {
168-
TapTree::Tree { ref left, ref right, height: _ } => {
169-
write!(f, "{{{:?},{:?}}}", *left, *right)
170-
}
171-
TapTree::Leaf(ref script) => write!(f, "{:?}", *script),
172-
}
173-
}
174-
}
175-
17697
impl<Pk: MiniscriptKey> Tr<Pk> {
17798
/// Create a new [`Tr`] descriptor from internal key and [`TapTree`]
17899
pub fn new(internal_key: Pk, tree: Option<TapTree<Pk>>) -> Result<Self, Error> {

src/descriptor/tr/taptree.rs

Lines changed: 86 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,95 @@
11
// SPDX-License-Identifier: CC0-1.0
22

3+
use core::{cmp, fmt};
4+
35
use bitcoin::taproot::{LeafVersion, TapLeafHash};
46

5-
use super::TapTree;
67
use crate::miniscript::context::Tap;
78
use crate::prelude::Vec;
89
use crate::sync::Arc;
9-
use crate::{Miniscript, MiniscriptKey, ToPublicKey};
10+
use crate::{Miniscript, MiniscriptKey, ToPublicKey, TranslateErr, Translator};
11+
12+
/// A Taproot Tree representation.
13+
// Hidden leaves are not yet supported in descriptor spec. Conceptually, it should
14+
// be simple to integrate those here, but it is best to wait on core for the exact syntax.
15+
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
16+
pub enum TapTree<Pk: MiniscriptKey> {
17+
/// A taproot tree structure
18+
Tree {
19+
/// Left tree branch.
20+
left: Arc<TapTree<Pk>>,
21+
/// Right tree branch.
22+
right: Arc<TapTree<Pk>>,
23+
/// Tree height, defined as `1 + max(left_height, right_height)`.
24+
height: usize,
25+
},
26+
/// A taproot leaf denoting a spending condition
27+
// A new leaf version would require a new Context, therefore there is no point
28+
// in adding a LeafVersion with Leaf type here. All Miniscripts right now
29+
// are of Leafversion::default
30+
Leaf(Arc<Miniscript<Pk, Tap>>),
31+
}
32+
33+
impl<Pk: MiniscriptKey> TapTree<Pk> {
34+
/// Creates a `TapTree` by combining `left` and `right` tree nodes.
35+
pub fn combine(left: TapTree<Pk>, right: TapTree<Pk>) -> Self {
36+
let height = 1 + cmp::max(left.height(), right.height());
37+
TapTree::Tree { left: Arc::new(left), right: Arc::new(right), height }
38+
}
39+
40+
/// Returns the height of this tree.
41+
pub fn height(&self) -> usize {
42+
match *self {
43+
TapTree::Tree { left: _, right: _, height } => height,
44+
TapTree::Leaf(..) => 0,
45+
}
46+
}
47+
48+
/// Iterates over all miniscripts in DFS walk order compatible with the
49+
/// PSBT requirements (BIP 371).
50+
pub fn iter(&self) -> TapTreeIter<Pk> { TapTreeIter::from_tree(self) }
51+
52+
// Helper function to translate keys
53+
pub(super) fn translate_helper<T>(
54+
&self,
55+
t: &mut T,
56+
) -> Result<TapTree<T::TargetPk>, TranslateErr<T::Error>>
57+
where
58+
T: Translator<Pk>,
59+
{
60+
let frag = match *self {
61+
TapTree::Tree { ref left, ref right, ref height } => TapTree::Tree {
62+
left: Arc::new(left.translate_helper(t)?),
63+
right: Arc::new(right.translate_helper(t)?),
64+
height: *height,
65+
},
66+
TapTree::Leaf(ref ms) => TapTree::Leaf(Arc::new(ms.translate_pk(t)?)),
67+
};
68+
Ok(frag)
69+
}
70+
}
71+
72+
impl<Pk: MiniscriptKey> fmt::Display for TapTree<Pk> {
73+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
74+
match self {
75+
TapTree::Tree { ref left, ref right, height: _ } => {
76+
write!(f, "{{{},{}}}", *left, *right)
77+
}
78+
TapTree::Leaf(ref script) => write!(f, "{}", *script),
79+
}
80+
}
81+
}
82+
83+
impl<Pk: MiniscriptKey> fmt::Debug for TapTree<Pk> {
84+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
85+
match self {
86+
TapTree::Tree { ref left, ref right, height: _ } => {
87+
write!(f, "{{{:?},{:?}}}", *left, *right)
88+
}
89+
TapTree::Leaf(ref script) => write!(f, "{:?}", *script),
90+
}
91+
}
92+
}
1093

1194
/// Iterator over the leaves of a Taptree.
1295
///
@@ -31,7 +114,7 @@ impl<'tr, Pk: MiniscriptKey> TapTreeIter<'tr, Pk> {
31114
pub fn empty() -> Self { Self { stack: vec![] } }
32115

33116
/// An iterator over a given tree.
34-
pub(super) fn from_tree(tree: &'tr TapTree<Pk>) -> Self { Self { stack: vec![(0, tree)] } }
117+
fn from_tree(tree: &'tr TapTree<Pk>) -> Self { Self { stack: vec![(0, tree)] } }
35118
}
36119

37120
impl<'a, Pk> Iterator for TapTreeIter<'a, Pk>

0 commit comments

Comments
 (0)