Skip to content

[Taproot API Project] flatten internal representation of TapTree #808

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/descriptor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub use self::bare::{Bare, Pkh};
pub use self::segwitv0::{Wpkh, Wsh, WshInner};
pub use self::sh::{Sh, ShInner};
pub use self::sortedmulti::SortedMultiVec;
pub use self::tr::{TapTree, TapTreeIter, TapTreeIterItem, Tr};
pub use self::tr::{TapTree, TapTreeDepthError, TapTreeIter, TapTreeIterItem, Tr};

pub mod checksum;
mod key;
Expand Down Expand Up @@ -256,7 +256,7 @@ impl<Pk: MiniscriptKey> Descriptor<Pk> {
/// returned value.
pub fn tap_tree(&self) -> Option<&TapTree<Pk>> {
if let Descriptor::Tr(ref tr) = self {
tr.tap_tree().as_ref()
tr.tap_tree()
} else {
None
}
Expand All @@ -268,7 +268,7 @@ impl<Pk: MiniscriptKey> Descriptor<Pk> {
/// Taproot descriptor containing only a keyspend, returns an empty iterator.
pub fn tap_tree_iter(&self) -> tr::TapTreeIter<Pk> {
if let Descriptor::Tr(ref tr) = self {
if let Some(ref tree) = tr.tap_tree() {
if let Some(tree) = tr.tap_tree() {
return tree.leaves();
}
}
Expand Down Expand Up @@ -988,7 +988,7 @@ impl<Pk: FromStrKey> FromStr for Descriptor<Pk> {
// FIXME preserve weird/broken behavior from 12.x.
// See https://github.com/rust-bitcoin/rust-miniscript/issues/734
ret.sanity_check()?;
for item in inner.iter_scripts() {
for item in inner.leaves() {
item.miniscript()
.ext_check(&crate::miniscript::analyzable::ExtParams::sane())?;
}
Expand Down
76 changes: 22 additions & 54 deletions src/descriptor/tr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use core::{cmp, fmt, hash};
use bitcoin::secp256k1;
use bitcoin::taproot::{
LeafVersion, TaprootBuilder, TaprootSpendInfo, TAPROOT_CONTROL_BASE_SIZE,
TAPROOT_CONTROL_MAX_NODE_COUNT, TAPROOT_CONTROL_NODE_SIZE,
TAPROOT_CONTROL_NODE_SIZE,
};
use bitcoin::{opcodes, Address, Network, ScriptBuf, Weight};
use sync::Arc;
Expand All @@ -28,7 +28,7 @@ use crate::{

mod taptree;

pub use self::taptree::{TapTree, TapTreeIter, TapTreeIterItem};
pub use self::taptree::{TapTree, TapTreeDepthError, TapTreeIter, TapTreeIterItem};

/// A taproot descriptor
pub struct Tr<Pk: MiniscriptKey> {
Expand Down Expand Up @@ -98,29 +98,14 @@ impl<Pk: MiniscriptKey> Tr<Pk> {
/// Create a new [`Tr`] descriptor from internal key and [`TapTree`]
pub fn new(internal_key: Pk, tree: Option<TapTree<Pk>>) -> Result<Self, Error> {
Tap::check_pk(&internal_key)?;
let nodes = tree.as_ref().map(|t| t.height()).unwrap_or(0);

if nodes <= TAPROOT_CONTROL_MAX_NODE_COUNT {
Ok(Self { internal_key, tree, spend_info: Mutex::new(None) })
} else {
Err(Error::MaxRecursiveDepthExceeded)
}
Ok(Self { internal_key, tree, spend_info: Mutex::new(None) })
}

/// Obtain the internal key of [`Tr`] descriptor
pub fn internal_key(&self) -> &Pk { &self.internal_key }

/// Obtain the [`TapTree`] of the [`Tr`] descriptor
pub fn tap_tree(&self) -> &Option<TapTree<Pk>> { &self.tree }

/// Obtain the [`TapTree`] of the [`Tr`] descriptor
#[deprecated(since = "11.0.0", note = "use tap_tree instead")]
pub fn taptree(&self) -> &Option<TapTree<Pk>> { self.tap_tree() }

/// Iterate over all scripts in merkle tree. If there is no script path, the iterator
/// yields [`None`]
#[deprecated(since = "TBD", note = "use `leaves` instead")]
pub fn iter_scripts(&self) -> TapTreeIter<Pk> { self.leaves() }
pub fn tap_tree(&self) -> Option<&TapTree<Pk>> { self.tree.as_ref() }

/// Iterates over all the leaves of the tree in depth-first preorder.
///
Expand Down Expand Up @@ -291,7 +276,7 @@ impl<Pk: MiniscriptKey> Tr<Pk> {
T: Translator<Pk>,
{
let tree = match &self.tree {
Some(tree) => Some(tree.translate_helper(translate)?),
Some(tree) => Some(tree.translate_pk(translate)?),
None => None,
};
let translate_desc =
Expand Down Expand Up @@ -392,33 +377,6 @@ impl<Pk: FromStrKey> crate::expression::FromTree for Tr<Pk> {
fn from_tree(root: expression::TreeIterItem) -> Result<Self, Error> {
use crate::expression::{Parens, ParseTreeError};

struct TreeStack<'s, Pk: MiniscriptKey> {
inner: Vec<(expression::TreeIterItem<'s>, TapTree<Pk>)>,
}

impl<'s, Pk: MiniscriptKey> TreeStack<'s, Pk> {
fn new() -> Self { Self { inner: Vec::with_capacity(128) } }

fn push(&mut self, parent: expression::TreeIterItem<'s>, tree: TapTree<Pk>) {
let mut next_push = (parent, tree);
while let Some(top) = self.inner.pop() {
if next_push.0.index() == top.0.index() {
next_push.0 = top.0.parent().unwrap();
next_push.1 = TapTree::combine(top.1, next_push.1);
} else {
self.inner.push(top);
break;
}
}
self.inner.push(next_push);
}

fn pop_final(&mut self) -> Option<TapTree<Pk>> {
assert_eq!(self.inner.len(), 1);
self.inner.pop().map(|x| x.1)
}
}

root.verify_toplevel("tr", 1..=2)
.map_err(From::from)
.map_err(Error::Parse)?;
Expand All @@ -435,7 +393,7 @@ impl<Pk: FromStrKey> crate::expression::FromTree for Tr<Pk> {
Some(tree) => tree,
};

let mut tree_stack = TreeStack::new();
let mut tree_builder = taptree::TapTreeBuilder::new();
let mut tap_tree_iter = tap_tree.pre_order_iter();
// while let construction needed because we modify the iterator inside the loop
// (by calling skip_descendants to skip over the contents of the tapscripts).
Expand All @@ -450,18 +408,19 @@ impl<Pk: FromStrKey> crate::expression::FromTree for Tr<Pk> {
node.verify_n_children("taptree branch", 2..=2)
.map_err(From::from)
.map_err(Error::Parse)?;
tree_builder.push_inner_node()?;
} else {
let script = Miniscript::from_tree(node)?;
// FIXME hack for https://github.com/rust-bitcoin/rust-miniscript/issues/734
if script.ty.corr.base != crate::miniscript::types::Base::B {
return Err(Error::NonTopLevel(format!("{:?}", script)));
};

tree_stack.push(node.parent().unwrap(), TapTree::Leaf(Arc::new(script)));
tree_builder.push_leaf(script);
tap_tree_iter.skip_descendants();
}
}
Tr::new(internal_key, tree_stack.pop_final())
Tr::new(internal_key, Some(tree_builder.finalize()))
}
}

Expand Down Expand Up @@ -616,9 +575,18 @@ mod tests {
}

#[test]
fn height() {
let desc = descriptor();
let tr = Tr::<String>::from_str(&desc).unwrap();
assert_eq!(tr.tap_tree().as_ref().unwrap().height(), 2);
fn tr_maximum_depth() {
// Copied from integration tests
let descriptor128 = "tr(X!,{pk(X1!),{pk(X2!),{pk(X3!),{pk(X4!),{pk(X5!),{pk(X6!),{pk(X7!),{pk(X8!),{pk(X9!),{pk(X10!),{pk(X11!),{pk(X12!),{pk(X13!),{pk(X14!),{pk(X15!),{pk(X16!),{pk(X17!),{pk(X18!),{pk(X19!),{pk(X20!),{pk(X21!),{pk(X22!),{pk(X23!),{pk(X24!),{pk(X25!),{pk(X26!),{pk(X27!),{pk(X28!),{pk(X29!),{pk(X30!),{pk(X31!),{pk(X32!),{pk(X33!),{pk(X34!),{pk(X35!),{pk(X36!),{pk(X37!),{pk(X38!),{pk(X39!),{pk(X40!),{pk(X41!),{pk(X42!),{pk(X43!),{pk(X44!),{pk(X45!),{pk(X46!),{pk(X47!),{pk(X48!),{pk(X49!),{pk(X50!),{pk(X51!),{pk(X52!),{pk(X53!),{pk(X54!),{pk(X55!),{pk(X56!),{pk(X57!),{pk(X58!),{pk(X59!),{pk(X60!),{pk(X61!),{pk(X62!),{pk(X63!),{pk(X64!),{pk(X65!),{pk(X66!),{pk(X67!),{pk(X68!),{pk(X69!),{pk(X70!),{pk(X71!),{pk(X72!),{pk(X73!),{pk(X74!),{pk(X75!),{pk(X76!),{pk(X77!),{pk(X78!),{pk(X79!),{pk(X80!),{pk(X81!),{pk(X82!),{pk(X83!),{pk(X84!),{pk(X85!),{pk(X86!),{pk(X87!),{pk(X88!),{pk(X89!),{pk(X90!),{pk(X91!),{pk(X92!),{pk(X93!),{pk(X94!),{pk(X95!),{pk(X96!),{pk(X97!),{pk(X98!),{pk(X99!),{pk(X100!),{pk(X101!),{pk(X102!),{pk(X103!),{pk(X104!),{pk(X105!),{pk(X106!),{pk(X107!),{pk(X108!),{pk(X109!),{pk(X110!),{pk(X111!),{pk(X112!),{pk(X113!),{pk(X114!),{pk(X115!),{pk(X116!),{pk(X117!),{pk(X118!),{pk(X119!),{pk(X120!),{pk(X121!),{pk(X122!),{pk(X123!),{pk(X124!),{pk(X125!),{pk(X126!),{pk(X127!),{pk(X128!),pk(X129)}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}})";
descriptor128.parse::<crate::Descriptor<String>>().unwrap();

// Copied from integration tests
let descriptor129 = "tr(X!,{pk(X1!),{pk(X2!),{pk(X3!),{pk(X4!),{pk(X5!),{pk(X6!),{pk(X7!),{pk(X8!),{pk(X9!),{pk(X10!),{pk(X11!),{pk(X12!),{pk(X13!),{pk(X14!),{pk(X15!),{pk(X16!),{pk(X17!),{pk(X18!),{pk(X19!),{pk(X20!),{pk(X21!),{pk(X22!),{pk(X23!),{pk(X24!),{pk(X25!),{pk(X26!),{pk(X27!),{pk(X28!),{pk(X29!),{pk(X30!),{pk(X31!),{pk(X32!),{pk(X33!),{pk(X34!),{pk(X35!),{pk(X36!),{pk(X37!),{pk(X38!),{pk(X39!),{pk(X40!),{pk(X41!),{pk(X42!),{pk(X43!),{pk(X44!),{pk(X45!),{pk(X46!),{pk(X47!),{pk(X48!),{pk(X49!),{pk(X50!),{pk(X51!),{pk(X52!),{pk(X53!),{pk(X54!),{pk(X55!),{pk(X56!),{pk(X57!),{pk(X58!),{pk(X59!),{pk(X60!),{pk(X61!),{pk(X62!),{pk(X63!),{pk(X64!),{pk(X65!),{pk(X66!),{pk(X67!),{pk(X68!),{pk(X69!),{pk(X70!),{pk(X71!),{pk(X72!),{pk(X73!),{pk(X74!),{pk(X75!),{pk(X76!),{pk(X77!),{pk(X78!),{pk(X79!),{pk(X80!),{pk(X81!),{pk(X82!),{pk(X83!),{pk(X84!),{pk(X85!),{pk(X86!),{pk(X87!),{pk(X88!),{pk(X89!),{pk(X90!),{pk(X91!),{pk(X92!),{pk(X93!),{pk(X94!),{pk(X95!),{pk(X96!),{pk(X97!),{pk(X98!),{pk(X99!),{pk(X100!),{pk(X101!),{pk(X102!),{pk(X103!),{pk(X104!),{pk(X105!),{pk(X106!),{pk(X107!),{pk(X108!),{pk(X109!),{pk(X110!),{pk(X111!),{pk(X112!),{pk(X113!),{pk(X114!),{pk(X115!),{pk(X116!),{pk(X117!),{pk(X118!),{pk(X119!),{pk(X120!),{pk(X121!),{pk(X122!),{pk(X123!),{pk(X124!),{pk(X125!),{pk(X126!),{pk(X127!),{pk(X128!),{pk(X129),pk(X130)}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}})";
assert!(matches!(
descriptor129
.parse::<crate::Descriptor::<String>>()
.unwrap_err(),
crate::Error::TapTreeDepthError(TapTreeDepthError),
));
}
}
Loading
Loading