Rename TapTree::from_builder

This commit is contained in:
Dr Maxim Orlovsky 2022-04-01 19:46:06 +02:00
parent 219273788c
commit 410412ff01
No known key found for this signature in database
GPG Key ID: FFC0250947E5C6F7
2 changed files with 11 additions and 11 deletions

View File

@ -135,18 +135,18 @@ 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))
}
}

View File

@ -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);
}