Merge rust-bitcoin/rust-bitcoin#927: Trivial improvements for TapTree type
4cdff06b1e
Add convenience method TapTree:to_builder (Dr Maxim Orlovsky)a12e7c73b6
Implement From<TapTree> for TaprootBuilder (Dr Maxim Orlovsky)410412ff01
Rename TapTree::from_builder (Dr Maxim Orlovsky)219273788c
Rename TapTree::into_builder (Dr Maxim Orlovsky)f9d8d0d968
Make TapTree::node_info public (Dr Maxim Orlovsky) Pull request description: These are trivial fixes from extracted from now closed #922 ACKs for top commit: Kixunil: ACK4cdff06b1e
sanket1729: ACK4cdff06b1e
apoelstra: ACK4cdff06b1e
Tree-SHA512: 6132e8c214edc6f199a5550309daf4ed5035f24f545c793d6396c393bd2f55940dc418af62aed9aff25c0c90b74ee384ace986f7201db4018c6fd52710006126
This commit is contained in:
commit
6b57a02b1f
|
@ -125,9 +125,16 @@ impl PartialEq for TapTree {
|
|||
|
||||
impl Eq for TapTree {}
|
||||
|
||||
impl From<TapTree> for TaprootBuilder {
|
||||
#[inline]
|
||||
fn from(tree: TapTree) -> Self {
|
||||
tree.into_builder()
|
||||
}
|
||||
}
|
||||
|
||||
impl TapTree {
|
||||
/// Gets the inner node info as the builder is finalized.
|
||||
fn node_info(&self) -> &NodeInfo {
|
||||
pub fn node_info(&self) -> &NodeInfo {
|
||||
// The builder algorithm invariant guarantees that is_complete builder
|
||||
// have only 1 element in branch and that is not None.
|
||||
// We make sure that we only allow is_complete builders via the from_inner
|
||||
|
@ -135,26 +142,32 @@ impl TapTree {
|
|||
self.0.branch()[0].as_ref().expect("from_inner only parses is_complete builders")
|
||||
}
|
||||
|
||||
/// Converts a [`TaprootBuilder`] into a tree if it is complete binary tree.
|
||||
/// Constructs [`TapTree`] from a [`TaprootBuilder`] if it is complete binary tree.
|
||||
///
|
||||
/// # Returns
|
||||
/// A [`TapTree`] iff the `inner` builder is complete, otherwise return [`IncompleteTapTree`]
|
||||
/// error with the content of incomplete builder `inner` instance.
|
||||
pub fn from_inner(inner: TaprootBuilder) -> Result<Self, IncompleteTapTree> {
|
||||
if !inner.is_finalized() {
|
||||
Err(IncompleteTapTree::NotFinalized(inner))
|
||||
} else if inner.has_hidden_nodes() {
|
||||
Err(IncompleteTapTree::HiddenParts(inner))
|
||||
/// A [`TapTree`] iff the `builder` is complete, otherwise return [`IncompleteTapTree`]
|
||||
/// error with the content of incomplete `builder` instance.
|
||||
pub fn from_builder(builder: TaprootBuilder) -> Result<Self, IncompleteTapTree> {
|
||||
if !builder.is_finalized() {
|
||||
Err(IncompleteTapTree::NotFinalized(builder))
|
||||
} else if builder.has_hidden_nodes() {
|
||||
Err(IncompleteTapTree::HiddenParts(builder))
|
||||
} else {
|
||||
Ok(TapTree(inner))
|
||||
Ok(TapTree(builder))
|
||||
}
|
||||
}
|
||||
|
||||
/// Converts self into builder [`TaprootBuilder`]. The builder is guaranteed to be finalized.
|
||||
pub fn into_inner(self) -> TaprootBuilder {
|
||||
pub fn into_builder(self) -> TaprootBuilder {
|
||||
self.0
|
||||
}
|
||||
|
||||
/// Constructs [`TaprootBuilder`] by internally cloning the `self`. The builder is guaranteed
|
||||
/// to be finalized.
|
||||
pub fn to_builder(&self) -> TaprootBuilder {
|
||||
self.0.clone()
|
||||
}
|
||||
|
||||
/// Returns [`TapTreeIter`] iterator for a taproot script tree, operating in DFS order over
|
||||
/// tree [`ScriptLeaf`]s.
|
||||
pub fn script_leaves(&self) -> TapTreeIter {
|
||||
|
|
|
@ -393,14 +393,14 @@ mod tests {
|
|||
let mut builder = compose_taproot_builder(0x51, &[2, 2, 2]);
|
||||
builder = builder.add_leaf_with_ver(3, Script::from_hex("b9").unwrap(), LeafVersion::from_consensus(0xC2).unwrap()).unwrap();
|
||||
builder = builder.add_hidden_node(3, sha256::Hash::default()).unwrap();
|
||||
assert!(TapTree::from_inner(builder.clone()).is_err());
|
||||
assert!(TapTree::from_builder(builder.clone()).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn taptree_roundtrip() {
|
||||
let mut builder = compose_taproot_builder(0x51, &[2, 2, 2, 3]);
|
||||
builder = builder.add_leaf_with_ver(3, Script::from_hex("b9").unwrap(), LeafVersion::from_consensus(0xC2).unwrap()).unwrap();
|
||||
let tree = TapTree::from_inner(builder).unwrap();
|
||||
let tree = TapTree::from_builder(builder).unwrap();
|
||||
let tree_prime = TapTree::deserialize(&tree.serialize()).unwrap();
|
||||
assert_eq!(tree, tree_prime);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue