Move merkle_tree hash types

Currently we are defining the two merkle tree hash types in the `block`
module, a better home for them is the `merkle_tree` module.

This is an API breaking change because the types were public in the
`block` module, however the change should/could be unnoticeable to users
if they use the crate level re-export - which is maintained.
This commit is contained in:
Andrew Poelstra 2024-06-17 19:10:23 +00:00
parent 0ae6f49cea
commit 5d8aa94069
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
6 changed files with 30 additions and 22 deletions

View File

@ -374,10 +374,10 @@ mod test {
use hex::FromHex;
use super::*;
use crate::blockdata::block::TxMerkleNode;
use crate::blockdata::locktime::absolute;
use crate::blockdata::transaction;
use crate::consensus::encode::{deserialize, serialize};
use crate::merkle_tree::TxMerkleNode;
use crate::{Amount, CompactTarget, OutPoint, ScriptBuf, Sequence, TxIn, TxOut, Txid, Witness};
fn dummy_tx(nonce: &[u8]) -> Transaction {

View File

@ -14,34 +14,21 @@ use io::{BufRead, Write};
use super::Weight;
use crate::blockdata::script;
use crate::blockdata::transaction::{Transaction, Txid, Wtxid};
use crate::blockdata::transaction::{Transaction, Wtxid};
use crate::consensus::{encode, Decodable, Encodable, Params};
use crate::internal_macros::{impl_consensus_encoding, impl_hashencode};
use crate::merkle_tree::{self, TxMerkleNode, WitnessMerkleNode};
use crate::pow::{CompactTarget, Target, Work};
use crate::prelude::*;
use crate::{merkle_tree, VarInt};
use crate::VarInt;
hashes::hash_newtype! {
/// A bitcoin block hash.
pub struct BlockHash(sha256d::Hash);
/// A hash of the Merkle tree branch or root for transactions.
pub struct TxMerkleNode(sha256d::Hash);
/// A hash corresponding to the Merkle tree root for witness data.
pub struct WitnessMerkleNode(sha256d::Hash);
/// A hash corresponding to the witness structure commitment in the coinbase transaction.
pub struct WitnessCommitment(sha256d::Hash);
}
impl_hashencode!(BlockHash);
impl_hashencode!(TxMerkleNode);
impl_hashencode!(WitnessMerkleNode);
impl From<Txid> for TxMerkleNode {
fn from(txid: Txid) -> Self { Self::from_byte_array(txid.to_byte_array()) }
}
impl From<Wtxid> for WitnessMerkleNode {
fn from(wtxid: Wtxid) -> Self { Self::from_byte_array(wtxid.to_byte_array()) }
}
/// Bitcoin block header.
///

View File

@ -23,9 +23,10 @@ use io::{BufRead, Cursor, Read, Write};
use crate::bip152::{PrefilledTransaction, ShortId};
use crate::bip158::{FilterHash, FilterHeader};
use crate::blockdata::block::{self, BlockHash, TxMerkleNode};
use crate::blockdata::block::{self, BlockHash};
use crate::blockdata::transaction::{Transaction, TxIn, TxOut};
use crate::consensus::{DecodeError, IterReader};
use crate::merkle_tree::TxMerkleNode;
#[cfg(feature = "std")]
use crate::p2p::{
address::{AddrV2Message, Address},

View File

@ -116,7 +116,7 @@ pub use crate::{
amount::{Amount, Denomination, SignedAmount},
bip158::{FilterHash, FilterHeader},
bip32::XKeyIdentifier,
blockdata::block::{self, Block, BlockHash, TxMerkleNode, WitnessMerkleNode, WitnessCommitment},
blockdata::block::{self, Block, BlockHash, WitnessCommitment},
blockdata::constants,
blockdata::fee_rate::FeeRate,
blockdata::locktime::{self, absolute, relative},
@ -132,7 +132,7 @@ pub use crate::{
crypto::ecdsa,
crypto::key::{self, PrivateKey, PubkeyHash, PublicKey, CompressedPublicKey, WPubkeyHash, XOnlyPublicKey},
crypto::sighash::{self, LegacySighash, SegwitV0Sighash, TapSighash, TapSighashTag},
merkle_tree::MerkleBlock,
merkle_tree::{MerkleBlock, TxMerkleNode, WitnessMerkleNode},
network::{Network, NetworkKind},
pow::{CompactTarget, Target, Work},
psbt::Psbt,

View File

@ -14,10 +14,11 @@ use core::fmt;
use io::{BufRead, Write};
use self::MerkleBlockError::*;
use crate::blockdata::block::{self, Block, TxMerkleNode};
use crate::blockdata::block::{self, Block};
use crate::blockdata::transaction::{Transaction, Txid};
use crate::blockdata::weight::Weight;
use crate::consensus::encode::{self, Decodable, Encodable, MAX_VEC_SIZE};
use crate::merkle_tree::TxMerkleNode;
use crate::prelude::*;
/// Data structure that represents a block header paired to a partial merkle tree.

View File

@ -18,16 +18,35 @@ mod block;
use core::cmp::min;
use core::iter;
use hashes::Hash;
use hashes::{sha256d, Hash};
use io::Write;
use crate::consensus::encode::Encodable;
use crate::internal_macros::impl_hashencode;
use crate::prelude::*;
use crate::{Txid, Wtxid};
#[rustfmt::skip]
#[doc(inline)]
pub use self::block::{MerkleBlock, MerkleBlockError, PartialMerkleTree};
hashes::hash_newtype! {
/// A hash of the Merkle tree branch or root for transactions.
pub struct TxMerkleNode(sha256d::Hash);
/// A hash corresponding to the Merkle tree root for witness data.
pub struct WitnessMerkleNode(sha256d::Hash);
}
impl_hashencode!(TxMerkleNode);
impl_hashencode!(WitnessMerkleNode);
impl From<Txid> for TxMerkleNode {
fn from(txid: Txid) -> Self { Self::from_byte_array(txid.to_byte_array()) }
}
impl From<Wtxid> for WitnessMerkleNode {
fn from(wtxid: Wtxid) -> Self { Self::from_byte_array(wtxid.to_byte_array()) }
}
/// Calculates the merkle root of a list of *hashes*, inline (in place) in `hashes`.
///
/// In most cases, you'll want to use [`calculate_root`] instead. Please note, calling this function