Move block hash types

We would like all the various hash types to be defined where they
rightly live instead of in the `hash_types` module.

Move the block hash types to the `block` module. While moving, add full
stops to the rustdoc of each hash.

Re-export _all four_ types from lib.rs (previously `WitnessMerkleNode`
was not re-exported).
This commit is contained in:
Tobin C. Harding 2023-11-07 13:34:13 +11:00
parent 83cefefa45
commit 61c02ff202
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
10 changed files with 42 additions and 47 deletions

View File

@ -373,10 +373,10 @@ mod test {
use hex::FromHex; use hex::FromHex;
use super::*; use super::*;
use crate::blockdata::block::TxMerkleNode;
use crate::blockdata::locktime::absolute; use crate::blockdata::locktime::absolute;
use crate::blockdata::transaction; use crate::blockdata::transaction;
use crate::consensus::encode::{deserialize, serialize}; use crate::consensus::encode::{deserialize, serialize};
use crate::hash_types::TxMerkleNode;
use crate::{ use crate::{
Amount, CompactTarget, OutPoint, ScriptBuf, Sequence, Transaction, TxIn, TxOut, Txid, Amount, CompactTarget, OutPoint, ScriptBuf, Sequence, Transaction, TxIn, TxOut, Txid,
Witness, Witness,

View File

@ -44,12 +44,12 @@ use core::fmt::{self, Display, Formatter};
use hashes::{siphash24, Hash}; use hashes::{siphash24, Hash};
use internals::write_err; use internals::write_err;
use crate::blockdata::block::Block; use crate::blockdata::block::{Block, BlockHash};
use crate::blockdata::script::Script; use crate::blockdata::script::Script;
use crate::blockdata::transaction::OutPoint; use crate::blockdata::transaction::OutPoint;
use crate::consensus::encode::VarInt; use crate::consensus::encode::VarInt;
use crate::consensus::{Decodable, Encodable}; use crate::consensus::{Decodable, Encodable};
use crate::hash_types::{BlockHash, FilterHash, FilterHeader}; use crate::hash_types::{FilterHash, FilterHeader};
use crate::prelude::*; use crate::prelude::*;
/// Golomb encoding parameter as in BIP-158, see also https://gist.github.com/sipa/576d5f09c3b86c3b1b75598d799fc845 /// Golomb encoding parameter as in BIP-158, see also https://gist.github.com/sipa/576d5f09c3b86c3b1b75598d799fc845
@ -554,8 +554,8 @@ mod test {
use serde_json::Value; use serde_json::Value;
use super::*; use super::*;
use crate::blockdata::block::BlockHash;
use crate::consensus::encode::deserialize; use crate::consensus::encode::deserialize;
use crate::hash_types::BlockHash;
use crate::ScriptBuf; use crate::ScriptBuf;
#[test] #[test]

View File

@ -10,23 +10,39 @@
use core::fmt; use core::fmt;
use hashes::{Hash, HashEngine}; use hashes::{sha256d, Hash, HashEngine};
use super::Weight; use super::Weight;
use crate::blockdata::script; use crate::blockdata::script;
use crate::blockdata::transaction::Transaction; use crate::blockdata::transaction::Transaction;
use crate::consensus::{encode, Decodable, Encodable}; use crate::consensus::{encode, Decodable, Encodable};
use crate::hash_types::{TxMerkleNode, WitnessCommitment, WitnessMerkleNode, Wtxid}; use crate::hash_types::{impl_hashencode, Txid, Wtxid};
use crate::internal_macros::impl_consensus_encoding; use crate::internal_macros::impl_consensus_encoding;
use crate::pow::{CompactTarget, Target, Work}; use crate::pow::{CompactTarget, Target, Work};
use crate::prelude::*; use crate::prelude::*;
use crate::{merkle_tree, Network, VarInt}; use crate::{merkle_tree, Network, VarInt};
#[rustfmt::skip] // Keep public re-exports separate. hashes::hash_newtype! {
#[doc(inline)] /// A bitcoin block hash.
pub use crate::{ pub struct BlockHash(sha256d::Hash);
hash_types::BlockHash, /// 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. /// Bitcoin block header.
/// ///

View File

@ -23,9 +23,9 @@ use internals::write_err;
use io::{Cursor, Read}; use io::{Cursor, Read};
use crate::bip152::{PrefilledTransaction, ShortId}; use crate::bip152::{PrefilledTransaction, ShortId};
use crate::blockdata::block; use crate::blockdata::block::{self, BlockHash, TxMerkleNode};
use crate::blockdata::transaction::{Transaction, TxIn, TxOut}; use crate::blockdata::transaction::{Transaction, TxIn, TxOut};
use crate::hash_types::{BlockHash, FilterHash, FilterHeader, TxMerkleNode}; use crate::hash_types::{FilterHash, FilterHeader};
#[cfg(feature = "std")] #[cfg(feature = "std")]
use crate::p2p::{ use crate::p2p::{
address::{AddrV2Message, Address}, address::{AddrV2Message, Address},

View File

@ -25,6 +25,7 @@ macro_rules! impl_hashencode {
} }
}; };
} }
pub(crate) use impl_hashencode;
#[rustfmt::skip] #[rustfmt::skip]
macro_rules! impl_asref_push_bytes { macro_rules! impl_asref_push_bytes {
@ -69,15 +70,6 @@ mod newtypes {
/// A bitcoin witness transaction ID. /// A bitcoin witness transaction ID.
pub struct Wtxid(sha256d::Hash); pub struct Wtxid(sha256d::Hash);
/// 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);
/// Filter hash, as defined in BIP-157 /// Filter hash, as defined in BIP-157
pub struct FilterHash(sha256d::Hash); pub struct FilterHash(sha256d::Hash);
@ -87,23 +79,10 @@ mod newtypes {
impl_hashencode!(Txid); impl_hashencode!(Txid);
impl_hashencode!(Wtxid); impl_hashencode!(Wtxid);
impl_hashencode!(BlockHash);
impl_hashencode!(TxMerkleNode);
impl_hashencode!(WitnessMerkleNode);
impl_hashencode!(FilterHash); impl_hashencode!(FilterHash);
impl_hashencode!(FilterHeader); impl_hashencode!(FilterHeader);
impl From<Txid> for TxMerkleNode {
fn from(txid: Txid) -> Self {
Self::from(txid.0)
}
}
impl From<Wtxid> for WitnessMerkleNode {
fn from(wtxid: Wtxid) -> Self {
Self::from(wtxid.0)
}
}
} }
#[deprecated(since = "0.0.0-NEXT-RELEASE", note = "use crate::T instead")]
pub use crate::{BlockHash, TxMerkleNode, WitnessCommitment, WitnessMerkleNode};

View File

@ -122,7 +122,7 @@ pub use crate::{
address::{Address, AddressType}, address::{Address, AddressType},
amount::{Amount, Denomination, SignedAmount}, amount::{Amount, Denomination, SignedAmount},
bip32::XKeyIdentifier, bip32::XKeyIdentifier,
blockdata::block::{self, Block}, blockdata::block::{self, Block, BlockHash, TxMerkleNode, WitnessMerkleNode, WitnessCommitment},
blockdata::constants, blockdata::constants,
blockdata::fee_rate::FeeRate, blockdata::fee_rate::FeeRate,
blockdata::locktime::{self, absolute, relative}, blockdata::locktime::{self, absolute, relative},
@ -137,7 +137,7 @@ pub use crate::{
crypto::ecdsa, crypto::ecdsa,
crypto::key::{self, PrivateKey, PubkeyHash, PublicKey, WPubkeyHash, XOnlyPublicKey}, crypto::key::{self, PrivateKey, PubkeyHash, PublicKey, WPubkeyHash, XOnlyPublicKey},
crypto::sighash::{self, LegacySighash, SegwitV0Sighash, TapSighash, TapSighashTag}, crypto::sighash::{self, LegacySighash, SegwitV0Sighash, TapSighash, TapSighashTag},
hash_types::{BlockHash, FilterHash, FilterHeader, TxMerkleNode, Txid, WitnessCommitment, Wtxid}, hash_types::{FilterHash, FilterHeader, Txid, Wtxid},
merkle_tree::MerkleBlock, merkle_tree::MerkleBlock,
network::Network, network::Network,
pow::{CompactTarget, Target, Work}, pow::{CompactTarget, Target, Work},

View File

@ -43,11 +43,11 @@ use core::fmt;
use hashes::Hash; use hashes::Hash;
use self::MerkleBlockError::*; use self::MerkleBlockError::*;
use crate::blockdata::block::{self, Block}; use crate::blockdata::block::{self, Block, TxMerkleNode};
use crate::blockdata::transaction::Transaction; use crate::blockdata::transaction::Transaction;
use crate::blockdata::weight::Weight; use crate::blockdata::weight::Weight;
use crate::consensus::encode::{self, Decodable, Encodable}; use crate::consensus::encode::{self, Decodable, Encodable};
use crate::hash_types::{TxMerkleNode, Txid}; use crate::hash_types::Txid;
use crate::prelude::*; use crate::prelude::*;
/// Data structure that represents a block header paired to a partial merkle tree. /// Data structure that represents a block header paired to a partial merkle tree.
@ -540,8 +540,6 @@ mod tests {
use super::*; use super::*;
use crate::consensus::encode::{deserialize, serialize}; use crate::consensus::encode::{deserialize, serialize};
#[cfg(feature = "rand-std")]
use crate::hash_types::TxMerkleNode;
use crate::{Block, Txid}; use crate::{Block, Txid};
#[cfg(feature = "rand-std")] #[cfg(feature = "rand-std")]

View File

@ -8,8 +8,9 @@
use hashes::{sha256d, Hash as _}; use hashes::{sha256d, Hash as _};
use crate::blockdata::block::BlockHash;
use crate::consensus::encode::{self, Decodable, Encodable}; use crate::consensus::encode::{self, Decodable, Encodable};
use crate::hash_types::{BlockHash, Txid, Wtxid}; use crate::hash_types::{Txid, Wtxid};
use crate::internal_macros::impl_consensus_encoding; use crate::internal_macros::impl_consensus_encoding;
use crate::prelude::*; use crate::prelude::*;
use crate::{io, p2p}; use crate::{io, p2p};

View File

@ -5,7 +5,8 @@
//! This module describes BIP157 Client Side Block Filtering network messages. //! This module describes BIP157 Client Side Block Filtering network messages.
//! //!
use crate::hash_types::{BlockHash, FilterHash, FilterHeader}; use crate::blockdata::block::BlockHash;
use crate::hash_types::{FilterHash, FilterHeader};
use crate::internal_macros::impl_consensus_encoding; use crate::internal_macros::impl_consensus_encoding;
/// getcfilters message /// getcfilters message

View File

@ -13,10 +13,10 @@ use io::{Read, Write};
#[cfg(all(test, mutate))] #[cfg(all(test, mutate))]
use mutagen::mutate; use mutagen::mutate;
use crate::blockdata::block::BlockHash;
use crate::consensus::encode::{self, Decodable, Encodable}; use crate::consensus::encode::{self, Decodable, Encodable};
#[cfg(doc)] #[cfg(doc)]
use crate::consensus::Params; use crate::consensus::Params;
use crate::hash_types::BlockHash;
use crate::prelude::String; use crate::prelude::String;
use crate::string::FromHexStr; use crate::string::FromHexStr;
use crate::Network; use crate::Network;