Skip to content

Commit 8e5d809

Browse files
committed
tr: rename iter_scripts to leaves
The name `iter_scripts` is dumb and undiscoverable. More importantly, it's misleading -- this iterator does not yield scripts. It yields Miniscripts, which can be converted to scripts if you have ToPublicKey and are willing to pay a cost.
1 parent 86bee15 commit 8e5d809

File tree

6 files changed

+25
-16
lines changed

6 files changed

+25
-16
lines changed

bitcoind-tests/tests/test_desc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ pub fn test_desc_satisfy(
185185
}
186186
// ------------------ script spend -------------
187187
let x_only_keypairs_reqd: Vec<(secp256k1::Keypair, TapLeafHash)> = tr
188-
.iter_scripts()
188+
.leaves()
189189
.flat_map(|leaf| {
190190
let leaf_hash = TapLeafHash::from_script(&leaf.compute_script(), LeafVersion::TapScript);
191191
leaf.miniscript().iter_pk().filter_map(move |pk| {

examples/taproot.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ fn main() {
6666
assert_eq!(p.internal_key(), "Ca");
6767

6868
// Iterate through scripts
69-
let mut iter = p.iter_scripts();
69+
let mut iter = p.leaves();
7070
let mut next = iter.next().unwrap();
7171
assert_eq!(
7272
(next.depth(), next.miniscript().as_ref()),

src/descriptor/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ impl<Pk: MiniscriptKey> Descriptor<Pk> {
252252

253253
/// For a Taproot descriptor, returns the [`TapTree`] describing the Taproot tree.
254254
///
255-
/// To obtain the individual leaves of the tree, call [`TapTree::iter`] on the
255+
/// To obtain the individual leaves of the tree, call [`TapTree::leaves`] on the
256256
/// returned value.
257257
pub fn tap_tree(&self) -> Option<&TapTree<Pk>> {
258258
if let Descriptor::Tr(ref tr) = self {
@@ -269,7 +269,7 @@ impl<Pk: MiniscriptKey> Descriptor<Pk> {
269269
pub fn tap_tree_iter(&self) -> tr::TapTreeIter<Pk> {
270270
if let Descriptor::Tr(ref tr) = self {
271271
if let Some(ref tree) = tr.tap_tree() {
272-
return tree.iter();
272+
return tree.leaves();
273273
}
274274
}
275275
tr::TapTreeIter::empty()

src/descriptor/tr/mod.rs

+15-8
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,16 @@ impl<Pk: MiniscriptKey> Tr<Pk> {
119119

120120
/// Iterate over all scripts in merkle tree. If there is no script path, the iterator
121121
/// yields [`None`]
122-
pub fn iter_scripts(&self) -> TapTreeIter<Pk> {
122+
#[deprecated(since = "TBD", note = "use `leaves` instead")]
123+
pub fn iter_scripts(&self) -> TapTreeIter<Pk> { self.leaves() }
124+
125+
/// Iterates over all the leaves of the tree in depth-first preorder.
126+
///
127+
/// The yielded elements include the Miniscript for each leave as well as its depth
128+
/// in the tree, which is the data required by PSBT (BIP 371).
129+
pub fn leaves(&self) -> TapTreeIter<Pk> {
123130
match self.tree {
124-
Some(ref t) => t.iter(),
131+
Some(ref t) => t.leaves(),
125132
None => TapTreeIter::empty(),
126133
}
127134
}
@@ -151,7 +158,7 @@ impl<Pk: MiniscriptKey> Tr<Pk> {
151158
TaprootSpendInfo::new_key_spend(&secp, self.internal_key.to_x_only_pubkey(), None)
152159
} else {
153160
let mut builder = TaprootBuilder::new();
154-
for leaf in self.iter_scripts() {
161+
for leaf in self.leaves() {
155162
let script = leaf.miniscript().encode();
156163
builder = builder
157164
.add_leaf(leaf.depth(), script)
@@ -170,7 +177,7 @@ impl<Pk: MiniscriptKey> Tr<Pk> {
170177

171178
/// Checks whether the descriptor is safe.
172179
pub fn sanity_check(&self) -> Result<(), Error> {
173-
for leaf in self.iter_scripts() {
180+
for leaf in self.leaves() {
174181
leaf.miniscript().sanity_check()?;
175182
}
176183
Ok(())
@@ -200,7 +207,7 @@ impl<Pk: MiniscriptKey> Tr<Pk> {
200207
};
201208

202209
let wu = tree
203-
.iter()
210+
.leaves()
204211
.filter_map(|leaf| {
205212
let script_size = leaf.miniscript().script_size();
206213
let max_sat_elems = leaf.miniscript().max_satisfaction_witness_elements().ok()?;
@@ -250,7 +257,7 @@ impl<Pk: MiniscriptKey> Tr<Pk> {
250257
Some(tree) => tree,
251258
};
252259

253-
tree.iter()
260+
tree.leaves()
254261
.filter_map(|leaf| {
255262
let script_size = leaf.miniscript().script_size();
256263
let max_sat_elems = leaf.miniscript().max_satisfaction_witness_elements().ok()?;
@@ -495,7 +502,7 @@ impl<Pk: MiniscriptKey> Liftable<Pk> for Tr<Pk> {
495502
impl<Pk: MiniscriptKey> ForEachKey<Pk> for Tr<Pk> {
496503
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, mut pred: F) -> bool {
497504
let script_keys_res = self
498-
.iter_scripts()
505+
.leaves()
499506
.all(|leaf| leaf.miniscript().for_each_key(&mut pred));
500507
script_keys_res && pred(&self.internal_key)
501508
}
@@ -540,7 +547,7 @@ where
540547
absolute_timelock: None,
541548
};
542549
let mut min_wit_len = None;
543-
for leaf in desc.iter_scripts() {
550+
for leaf in desc.leaves() {
544551
let mut satisfaction = if allow_mall {
545552
match leaf.miniscript().build_template(provider) {
546553
s @ Satisfaction { stack: Witness::Stack(_), .. } => s,

src/descriptor/tr/taptree.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,11 @@ impl<Pk: MiniscriptKey> TapTree<Pk> {
4646
}
4747
}
4848

49-
/// Iterates over all miniscripts in DFS walk order compatible with the
50-
/// PSBT requirements (BIP 371).
51-
pub fn iter(&self) -> TapTreeIter<Pk> { TapTreeIter::from_tree(self) }
49+
/// Iterates over all the leaves of the tree in depth-first preorder.
50+
///
51+
/// The yielded elements include the Miniscript for each leave as well as its depth
52+
/// in the tree, which is the data required by PSBT (BIP 371).
53+
pub fn leaves(&self) -> TapTreeIter<Pk> { TapTreeIter::from_tree(self) }
5254

5355
// Helper function to translate keys
5456
pub(super) fn translate_helper<T>(

src/psbt/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1112,7 +1112,7 @@ fn update_item_with_descriptor_helper<F: PsbtFields>(
11121112

11131113
let mut builder = taproot::TaprootBuilder::new();
11141114

1115-
for (leaf_derived, leaf) in tr_derived.iter_scripts().zip(tr_xpk.iter_scripts()) {
1115+
for (leaf_derived, leaf) in tr_derived.leaves().zip(tr_xpk.leaves()) {
11161116
debug_assert_eq!(leaf_derived.depth(), leaf.depth());
11171117
let leaf_script = (leaf_derived.compute_script(), leaf_derived.leaf_version());
11181118
let tapleaf_hash = TapLeafHash::from_script(&leaf_script.0, leaf_script.1);

0 commit comments

Comments
 (0)