From 277045bad7c4e9339d55e57ec425b0d0b46a5b58 Mon Sep 17 00:00:00 2001 From: Martin Habovstiak Date: Sat, 15 Mar 2025 19:28:43 +0100 Subject: [PATCH] Add `Buf` suffix to `TaprootMerkleBranch` This type actually contains a `Vec` but we would prefer to have an unsized type. Rename it first so that we can reuse the name later. --- bitcoin/src/taproot/merkle_branch.rs | 42 ++++++++++++++-------------- bitcoin/src/taproot/mod.rs | 22 +++++++-------- 2 files changed, 32 insertions(+), 32 deletions(-) diff --git a/bitcoin/src/taproot/merkle_branch.rs b/bitcoin/src/taproot/merkle_branch.rs index 5ad08cbc1..6a523ba8d 100644 --- a/bitcoin/src/taproot/merkle_branch.rs +++ b/bitcoin/src/taproot/merkle_branch.rs @@ -1,6 +1,6 @@ // SPDX-License-Identifier: CC0-1.0 -//! Contains `TaprootMerkleBranch` and its associated types. +//! Contains `TaprootMerkleBranchBuf` and its associated types. use hashes::Hash; @@ -15,9 +15,9 @@ use crate::prelude::{Borrow, BorrowMut, Box, Vec}; #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", serde(into = "Vec"))] #[cfg_attr(feature = "serde", serde(try_from = "Vec"))] -pub struct TaprootMerkleBranch(Vec); +pub struct TaprootMerkleBranchBuf(Vec); -impl TaprootMerkleBranch { +impl TaprootMerkleBranchBuf { /// Returns a reference to the slice of hashes. #[deprecated(since = "0.32.0", note = "use `as_slice` instead")] #[inline] @@ -54,7 +54,7 @@ impl TaprootMerkleBranch { } else { let inner = node_hashes.iter().copied().map(TapNodeHash::from_byte_array).collect(); - Ok(TaprootMerkleBranch(inner)) + Ok(TaprootMerkleBranchBuf(inner)) } } @@ -70,7 +70,7 @@ impl TaprootMerkleBranch { if collection.as_ref().len() > TAPROOT_CONTROL_MAX_NODE_COUNT { Err(InvalidMerkleTreeDepthError(collection.as_ref().len())) } else { - Ok(TaprootMerkleBranch(collection.into())) + Ok(TaprootMerkleBranchBuf(collection.into())) } } @@ -113,7 +113,7 @@ impl TaprootMerkleBranch { macro_rules! impl_try_from { ($from:ty) => { - impl TryFrom<$from> for TaprootMerkleBranch { + impl TryFrom<$from> for TaprootMerkleBranchBuf { type Error = InvalidMerkleTreeDepthError; /// Constructs a new Merkle proof from list of hashes. @@ -123,7 +123,7 @@ macro_rules! impl_try_from { /// If inner proof length is more than [`TAPROOT_CONTROL_MAX_NODE_COUNT`] (128). #[inline] fn try_from(v: $from) -> Result { - TaprootMerkleBranch::from_collection(v) + TaprootMerkleBranchBuf::from_collection(v) } } }; @@ -135,7 +135,7 @@ impl_try_from!(Box<[TapNodeHash]>); macro_rules! impl_try_from_array { ($($len:expr),* $(,)?) => { $( - impl From<[TapNodeHash; $len]> for TaprootMerkleBranch { + impl From<[TapNodeHash; $len]> for TaprootMerkleBranchBuf { #[inline] fn from(a: [TapNodeHash; $len]) -> Self { Self(a.to_vec()) @@ -146,7 +146,7 @@ macro_rules! impl_try_from_array { } // Implement for all values [0, 128] inclusive. // -// The reason zero is included is that `TaprootMerkleBranch` doesn't contain the hash of the node +// The reason zero is included is that `TaprootMerkleBranchBuf` doesn't contain the hash of the node // that's being proven - it's not needed because the script is already right before control block. impl_try_from_array!( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, @@ -157,12 +157,12 @@ impl_try_from_array!( 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128 ); -impl From for Vec { +impl From for Vec { #[inline] - fn from(branch: TaprootMerkleBranch) -> Self { branch.0 } + fn from(branch: TaprootMerkleBranchBuf) -> Self { branch.0 } } -impl IntoIterator for TaprootMerkleBranch { +impl IntoIterator for TaprootMerkleBranchBuf { type IntoIter = IntoIter; type Item = TapNodeHash; @@ -170,7 +170,7 @@ impl IntoIterator for TaprootMerkleBranch { fn into_iter(self) -> Self::IntoIter { IntoIter(self.0.into_iter()) } } -impl<'a> IntoIterator for &'a TaprootMerkleBranch { +impl<'a> IntoIterator for &'a TaprootMerkleBranchBuf { type IntoIter = core::slice::Iter<'a, TapNodeHash>; type Item = &'a TapNodeHash; @@ -178,7 +178,7 @@ impl<'a> IntoIterator for &'a TaprootMerkleBranch { fn into_iter(self) -> Self::IntoIter { self.0.iter() } } -impl<'a> IntoIterator for &'a mut TaprootMerkleBranch { +impl<'a> IntoIterator for &'a mut TaprootMerkleBranchBuf { type IntoIter = core::slice::IterMut<'a, TapNodeHash>; type Item = &'a mut TapNodeHash; @@ -186,41 +186,41 @@ impl<'a> IntoIterator for &'a mut TaprootMerkleBranch { fn into_iter(self) -> Self::IntoIter { self.0.iter_mut() } } -impl core::ops::Deref for TaprootMerkleBranch { +impl core::ops::Deref for TaprootMerkleBranchBuf { type Target = [TapNodeHash]; #[inline] fn deref(&self) -> &Self::Target { &self.0 } } -impl core::ops::DerefMut for TaprootMerkleBranch { +impl core::ops::DerefMut for TaprootMerkleBranchBuf { #[inline] fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 } } -impl AsRef<[TapNodeHash]> for TaprootMerkleBranch { +impl AsRef<[TapNodeHash]> for TaprootMerkleBranchBuf { #[inline] fn as_ref(&self) -> &[TapNodeHash] { &self.0 } } -impl AsMut<[TapNodeHash]> for TaprootMerkleBranch { +impl AsMut<[TapNodeHash]> for TaprootMerkleBranchBuf { #[inline] fn as_mut(&mut self) -> &mut [TapNodeHash] { &mut self.0 } } -impl Borrow<[TapNodeHash]> for TaprootMerkleBranch { +impl Borrow<[TapNodeHash]> for TaprootMerkleBranchBuf { #[inline] fn borrow(&self) -> &[TapNodeHash] { &self.0 } } -impl BorrowMut<[TapNodeHash]> for TaprootMerkleBranch { +impl BorrowMut<[TapNodeHash]> for TaprootMerkleBranchBuf { #[inline] fn borrow_mut(&mut self) -> &mut [TapNodeHash] { &mut self.0 } } /// Iterator over node hashes within Taproot Merkle branch. /// -/// This is created by `into_iter` method on `TaprootMerkleBranch` (via `IntoIterator` trait). +/// This is created by `into_iter` method on `TaprootMerkleBranchBuf` (via `IntoIterator` trait). #[derive(Clone, Debug)] pub struct IntoIter(alloc::vec::IntoIter); diff --git a/bitcoin/src/taproot/mod.rs b/bitcoin/src/taproot/mod.rs index 78616db36..2c0a29096 100644 --- a/bitcoin/src/taproot/mod.rs +++ b/bitcoin/src/taproot/mod.rs @@ -27,7 +27,7 @@ use crate::{Script, ScriptBuf}; #[doc(inline)] pub use crate::crypto::taproot::{SigFromSliceError, Signature}; #[doc(inline)] -pub use merkle_branch::TaprootMerkleBranch; +pub use merkle_branch::TaprootMerkleBranchBuf; // Taproot test vectors from BIP-341 state the hashes without any reversing sha256t_tag! { @@ -190,7 +190,7 @@ pub struct LeafScript { } // type alias for versioned tap script corresponding Merkle proof -type ScriptMerkleProofMap = BTreeMap<(ScriptBuf, LeafVersion), BTreeSet>; +type ScriptMerkleProofMap = BTreeMap<(ScriptBuf, LeafVersion), BTreeSet>; /// Represents Taproot spending information. /// @@ -221,7 +221,7 @@ pub struct TaprootSpendInfo { output_key_parity: secp256k1::Parity, /// The tweaked output key. output_key: TweakedPublicKey, - /// Map from (script, leaf_version) to (sets of) [`TaprootMerkleBranch`]. More than one control + /// Map from (script, leaf_version) to (sets of) [`TaprootMerkleBranchBuf`]. More than one control /// block for a given script is only possible if it appears in multiple branches of the tree. In /// all cases, keeping one should be enough for spending funds, but we keep all of the paths so /// that a full tree can be constructed again from spending data if required. @@ -1037,7 +1037,7 @@ pub struct LeafNode { /// The [`TapLeaf`] leaf: TapLeaf, /// The Merkle proof (hashing partners) to get this node. - merkle_branch: TaprootMerkleBranch, + merkle_branch: TaprootMerkleBranchBuf, } impl LeafNode { @@ -1091,9 +1091,9 @@ impl LeafNode { pub fn leaf_version(&self) -> Option { self.leaf.as_script().map(|x| x.1) } /// Returns reference to the Merkle proof (hashing partners) to get this - /// node in form of [`TaprootMerkleBranch`]. + /// node in form of [`TaprootMerkleBranchBuf`]. #[inline] - pub fn merkle_branch(&self) -> &TaprootMerkleBranch { &self.merkle_branch } + pub fn merkle_branch(&self) -> &TaprootMerkleBranchBuf { &self.merkle_branch } /// Returns a reference to the leaf of this [`ScriptLeaf`]. #[inline] @@ -1109,7 +1109,7 @@ pub struct ScriptLeaf<'leaf> { /// The script. script: &'leaf Script, /// The Merkle proof (hashing partners) to get this node. - merkle_branch: &'leaf TaprootMerkleBranch, + merkle_branch: &'leaf TaprootMerkleBranchBuf, } impl<'leaf> ScriptLeaf<'leaf> { @@ -1120,7 +1120,7 @@ impl<'leaf> ScriptLeaf<'leaf> { pub fn script(&self) -> &Script { self.script } /// Obtains a reference to the Merkle proof of the leaf. - pub fn merkle_branch(&self) -> &TaprootMerkleBranch { self.merkle_branch } + pub fn merkle_branch(&self) -> &TaprootMerkleBranchBuf { self.merkle_branch } /// Obtains a script leaf from the leaf node if the leaf is not hidden. pub fn from_leaf_node(leaf_node: &'leaf LeafNode) -> Option { @@ -1140,7 +1140,7 @@ pub struct ControlBlock { /// The internal key. pub internal_key: UntweakedPublicKey, /// The Merkle proof of a script associated with this leaf. - pub merkle_branch: TaprootMerkleBranch, + pub merkle_branch: TaprootMerkleBranchBuf, } impl ControlBlock { @@ -1172,7 +1172,7 @@ impl ControlBlock { &sl[1..TAPROOT_CONTROL_BASE_SIZE].try_into().expect("Slice should be exactly 32 bytes"), ) .map_err(TaprootError::InvalidInternalKey)?; - let merkle_branch = TaprootMerkleBranch::decode(&sl[TAPROOT_CONTROL_BASE_SIZE..])?; + let merkle_branch = TaprootMerkleBranchBuf::decode(&sl[TAPROOT_CONTROL_BASE_SIZE..])?; Ok(ControlBlock { leaf_version, output_key_parity, internal_key, merkle_branch }) } @@ -1923,7 +1923,7 @@ mod test { .unwrap() .to_byte_array(), ); - let merkle_branch = TaprootMerkleBranch::from([hash1, hash2]); + let merkle_branch = TaprootMerkleBranchBuf::from([hash1, hash2]); // use serde_test to test serialization and deserialization serde_test::assert_tokens( &merkle_branch.readable(),