From 61c02ff20282356757f1a51f4c880dfae0bd7527 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Tue, 7 Nov 2023 13:34:13 +1100 Subject: [PATCH 1/6] 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). --- bitcoin/src/bip152.rs | 2 +- bitcoin/src/bip158.rs | 6 +++--- bitcoin/src/blockdata/block.rs | 30 +++++++++++++++++++++------- bitcoin/src/consensus/encode.rs | 4 ++-- bitcoin/src/hash_types.rs | 29 ++++----------------------- bitcoin/src/lib.rs | 4 ++-- bitcoin/src/merkle_tree/block.rs | 6 ++---- bitcoin/src/p2p/message_blockdata.rs | 3 ++- bitcoin/src/p2p/message_filter.rs | 3 ++- bitcoin/src/pow.rs | 2 +- 10 files changed, 42 insertions(+), 47 deletions(-) diff --git a/bitcoin/src/bip152.rs b/bitcoin/src/bip152.rs index 2ba70d73..8a6cc296 100644 --- a/bitcoin/src/bip152.rs +++ b/bitcoin/src/bip152.rs @@ -373,10 +373,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::hash_types::TxMerkleNode; use crate::{ Amount, CompactTarget, OutPoint, ScriptBuf, Sequence, Transaction, TxIn, TxOut, Txid, Witness, diff --git a/bitcoin/src/bip158.rs b/bitcoin/src/bip158.rs index 13097b7b..b8f83d85 100644 --- a/bitcoin/src/bip158.rs +++ b/bitcoin/src/bip158.rs @@ -44,12 +44,12 @@ use core::fmt::{self, Display, Formatter}; use hashes::{siphash24, Hash}; use internals::write_err; -use crate::blockdata::block::Block; +use crate::blockdata::block::{Block, BlockHash}; use crate::blockdata::script::Script; use crate::blockdata::transaction::OutPoint; use crate::consensus::encode::VarInt; use crate::consensus::{Decodable, Encodable}; -use crate::hash_types::{BlockHash, FilterHash, FilterHeader}; +use crate::hash_types::{FilterHash, FilterHeader}; use crate::prelude::*; /// 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 super::*; + use crate::blockdata::block::BlockHash; use crate::consensus::encode::deserialize; - use crate::hash_types::BlockHash; use crate::ScriptBuf; #[test] diff --git a/bitcoin/src/blockdata/block.rs b/bitcoin/src/blockdata/block.rs index 18135412..671be43b 100644 --- a/bitcoin/src/blockdata/block.rs +++ b/bitcoin/src/blockdata/block.rs @@ -10,23 +10,39 @@ use core::fmt; -use hashes::{Hash, HashEngine}; +use hashes::{sha256d, Hash, HashEngine}; use super::Weight; use crate::blockdata::script; use crate::blockdata::transaction::Transaction; 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::pow::{CompactTarget, Target, Work}; use crate::prelude::*; use crate::{merkle_tree, Network, VarInt}; -#[rustfmt::skip] // Keep public re-exports separate. -#[doc(inline)] -pub use crate::{ - hash_types::BlockHash, -}; +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 for TxMerkleNode { + fn from(txid: Txid) -> Self { Self::from_byte_array(txid.to_byte_array()) } +} + +impl From for WitnessMerkleNode { + fn from(wtxid: Wtxid) -> Self { Self::from_byte_array(wtxid.to_byte_array()) } +} /// Bitcoin block header. /// diff --git a/bitcoin/src/consensus/encode.rs b/bitcoin/src/consensus/encode.rs index 7b6a39cb..ce3d4a7b 100644 --- a/bitcoin/src/consensus/encode.rs +++ b/bitcoin/src/consensus/encode.rs @@ -23,9 +23,9 @@ use internals::write_err; use io::{Cursor, Read}; 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::hash_types::{BlockHash, FilterHash, FilterHeader, TxMerkleNode}; +use crate::hash_types::{FilterHash, FilterHeader}; #[cfg(feature = "std")] use crate::p2p::{ address::{AddrV2Message, Address}, diff --git a/bitcoin/src/hash_types.rs b/bitcoin/src/hash_types.rs index 7e2c5f87..99b7f298 100644 --- a/bitcoin/src/hash_types.rs +++ b/bitcoin/src/hash_types.rs @@ -25,6 +25,7 @@ macro_rules! impl_hashencode { } }; } +pub(crate) use impl_hashencode; #[rustfmt::skip] macro_rules! impl_asref_push_bytes { @@ -69,15 +70,6 @@ mod newtypes { /// A bitcoin witness transaction ID. 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 pub struct FilterHash(sha256d::Hash); @@ -87,23 +79,10 @@ mod newtypes { impl_hashencode!(Txid); impl_hashencode!(Wtxid); - impl_hashencode!(BlockHash); - - impl_hashencode!(TxMerkleNode); - impl_hashencode!(WitnessMerkleNode); impl_hashencode!(FilterHash); impl_hashencode!(FilterHeader); - - impl From for TxMerkleNode { - fn from(txid: Txid) -> Self { - Self::from(txid.0) - } - } - - impl From 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}; diff --git a/bitcoin/src/lib.rs b/bitcoin/src/lib.rs index 38d77248..beca4f12 100644 --- a/bitcoin/src/lib.rs +++ b/bitcoin/src/lib.rs @@ -122,7 +122,7 @@ pub use crate::{ address::{Address, AddressType}, amount::{Amount, Denomination, SignedAmount}, bip32::XKeyIdentifier, - blockdata::block::{self, Block}, + blockdata::block::{self, Block, BlockHash, TxMerkleNode, WitnessMerkleNode, WitnessCommitment}, blockdata::constants, blockdata::fee_rate::FeeRate, blockdata::locktime::{self, absolute, relative}, @@ -137,7 +137,7 @@ pub use crate::{ crypto::ecdsa, crypto::key::{self, PrivateKey, PubkeyHash, PublicKey, WPubkeyHash, XOnlyPublicKey}, 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, network::Network, pow::{CompactTarget, Target, Work}, diff --git a/bitcoin/src/merkle_tree/block.rs b/bitcoin/src/merkle_tree/block.rs index caafa4a7..5f0cf041 100644 --- a/bitcoin/src/merkle_tree/block.rs +++ b/bitcoin/src/merkle_tree/block.rs @@ -43,11 +43,11 @@ use core::fmt; use hashes::Hash; use self::MerkleBlockError::*; -use crate::blockdata::block::{self, Block}; +use crate::blockdata::block::{self, Block, TxMerkleNode}; use crate::blockdata::transaction::Transaction; use crate::blockdata::weight::Weight; use crate::consensus::encode::{self, Decodable, Encodable}; -use crate::hash_types::{TxMerkleNode, Txid}; +use crate::hash_types::Txid; use crate::prelude::*; /// Data structure that represents a block header paired to a partial merkle tree. @@ -540,8 +540,6 @@ mod tests { use super::*; use crate::consensus::encode::{deserialize, serialize}; - #[cfg(feature = "rand-std")] - use crate::hash_types::TxMerkleNode; use crate::{Block, Txid}; #[cfg(feature = "rand-std")] diff --git a/bitcoin/src/p2p/message_blockdata.rs b/bitcoin/src/p2p/message_blockdata.rs index 224887d8..cae8884a 100644 --- a/bitcoin/src/p2p/message_blockdata.rs +++ b/bitcoin/src/p2p/message_blockdata.rs @@ -8,8 +8,9 @@ use hashes::{sha256d, Hash as _}; +use crate::blockdata::block::BlockHash; 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::prelude::*; use crate::{io, p2p}; diff --git a/bitcoin/src/p2p/message_filter.rs b/bitcoin/src/p2p/message_filter.rs index e2231bca..e17c0904 100644 --- a/bitcoin/src/p2p/message_filter.rs +++ b/bitcoin/src/p2p/message_filter.rs @@ -5,7 +5,8 @@ //! 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; /// getcfilters message diff --git a/bitcoin/src/pow.rs b/bitcoin/src/pow.rs index b59f35e1..24772b68 100644 --- a/bitcoin/src/pow.rs +++ b/bitcoin/src/pow.rs @@ -13,10 +13,10 @@ use io::{Read, Write}; #[cfg(all(test, mutate))] use mutagen::mutate; +use crate::blockdata::block::BlockHash; use crate::consensus::encode::{self, Decodable, Encodable}; #[cfg(doc)] use crate::consensus::Params; -use crate::hash_types::BlockHash; use crate::prelude::String; use crate::string::FromHexStr; use crate::Network; From 3107f80aac7f2b9c8e96b90d387bafec99b3eb8e Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Tue, 7 Nov 2023 13:40:01 +1100 Subject: [PATCH 2/6] Move transaction 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 transaction hash types to the `transaction` module. --- bitcoin/src/blockdata/block.rs | 4 ++-- bitcoin/src/blockdata/transaction.rs | 17 ++++++++++++++++- bitcoin/src/hash_types.rs | 17 +---------------- bitcoin/src/lib.rs | 4 ++-- bitcoin/src/merkle_tree/block.rs | 3 +-- bitcoin/src/p2p/message_blockdata.rs | 2 +- 6 files changed, 23 insertions(+), 24 deletions(-) diff --git a/bitcoin/src/blockdata/block.rs b/bitcoin/src/blockdata/block.rs index 671be43b..be216d00 100644 --- a/bitcoin/src/blockdata/block.rs +++ b/bitcoin/src/blockdata/block.rs @@ -14,9 +14,9 @@ use hashes::{sha256d, Hash, HashEngine}; use super::Weight; use crate::blockdata::script; -use crate::blockdata::transaction::Transaction; +use crate::blockdata::transaction::{Transaction, Txid, Wtxid}; use crate::consensus::{encode, Decodable, Encodable}; -use crate::hash_types::{impl_hashencode, Txid, Wtxid}; +use crate::hash_types::impl_hashencode; use crate::internal_macros::impl_consensus_encoding; use crate::pow::{CompactTarget, Target, Work}; use crate::prelude::*; diff --git a/bitcoin/src/blockdata/transaction.rs b/bitcoin/src/blockdata/transaction.rs index 20146c1e..a481a789 100644 --- a/bitcoin/src/blockdata/transaction.rs +++ b/bitcoin/src/blockdata/transaction.rs @@ -23,7 +23,7 @@ use crate::blockdata::locktime::relative; use crate::blockdata::script::{Script, ScriptBuf}; use crate::blockdata::witness::Witness; use crate::consensus::{encode, Decodable, Encodable}; -use crate::hash_types::{Txid, Wtxid}; +use crate::hash_types::impl_hashencode; use crate::internal_macros::impl_consensus_encoding; use crate::parse::impl_parse_str_from_int_infallible; use crate::prelude::*; @@ -38,6 +38,21 @@ use crate::{Amount, VarInt}; #[doc(inline)] pub use crate::consensus::validation::TxVerifyError; +hashes::hash_newtype! { + /// A bitcoin transaction hash/transaction ID. + /// + /// For compatibility with the existing Bitcoin infrastructure and historical and current + /// versions of the Bitcoin Core software itself, this and other [`sha256d::Hash`] types, are + /// serialized in reverse byte order when converted to a hex string via [`std::fmt::Display`] + /// trait operations. See [`hashes::Hash::DISPLAY_BACKWARD`] for more details. + pub struct Txid(sha256d::Hash); + + /// A bitcoin witness transaction ID. + pub struct Wtxid(sha256d::Hash); +} +impl_hashencode!(Txid); +impl_hashencode!(Wtxid); + /// The marker MUST be a 1-byte zero value: 0x00. (BIP-141) const SEGWIT_MARKER: u8 = 0x00; /// The flag MUST be a 1-byte non-zero value. Currently, 0x01 MUST be used. (BIP-141) diff --git a/bitcoin/src/hash_types.rs b/bitcoin/src/hash_types.rs index 99b7f298..cbdb6eed 100644 --- a/bitcoin/src/hash_types.rs +++ b/bitcoin/src/hash_types.rs @@ -59,30 +59,15 @@ mod newtypes { use hashes::{sha256d, hash_newtype}; hash_newtype! { - /// A bitcoin transaction hash/transaction ID. - /// - /// For compatibility with the existing Bitcoin infrastructure and historical - /// and current versions of the Bitcoin Core software itself, this and - /// other [`sha256d::Hash`] types, are serialized in reverse - /// byte order when converted to a hex string via [`std::fmt::Display`] trait operations. - /// See [`hashes::Hash::DISPLAY_BACKWARD`] for more details. - pub struct Txid(sha256d::Hash); - - /// A bitcoin witness transaction ID. - pub struct Wtxid(sha256d::Hash); - /// Filter hash, as defined in BIP-157 pub struct FilterHash(sha256d::Hash); /// Filter header, as defined in BIP-157 pub struct FilterHeader(sha256d::Hash); } - impl_hashencode!(Txid); - impl_hashencode!(Wtxid); - impl_hashencode!(FilterHash); impl_hashencode!(FilterHeader); } #[deprecated(since = "0.0.0-NEXT-RELEASE", note = "use crate::T instead")] -pub use crate::{BlockHash, TxMerkleNode, WitnessCommitment, WitnessMerkleNode}; +pub use crate::{BlockHash, TxMerkleNode, Txid, WitnessCommitment, WitnessMerkleNode, Wtxid}; diff --git a/bitcoin/src/lib.rs b/bitcoin/src/lib.rs index beca4f12..0cb4d4a6 100644 --- a/bitcoin/src/lib.rs +++ b/bitcoin/src/lib.rs @@ -130,14 +130,14 @@ pub use crate::{ blockdata::script::witness_program::{self, WitnessProgram}, blockdata::script::witness_version::{self, WitnessVersion}, blockdata::script::{self, Script, ScriptBuf, ScriptHash, WScriptHash}, - blockdata::transaction::{self, OutPoint, Sequence, Transaction, TxIn, TxOut}, + blockdata::transaction::{self, OutPoint, Sequence, Transaction, TxIn, TxOut, Txid, Wtxid}, blockdata::weight::Weight, blockdata::witness::{self, Witness}, consensus::encode::VarInt, crypto::ecdsa, crypto::key::{self, PrivateKey, PubkeyHash, PublicKey, WPubkeyHash, XOnlyPublicKey}, crypto::sighash::{self, LegacySighash, SegwitV0Sighash, TapSighash, TapSighashTag}, - hash_types::{FilterHash, FilterHeader, Txid, Wtxid}, + hash_types::{FilterHash, FilterHeader}, merkle_tree::MerkleBlock, network::Network, pow::{CompactTarget, Target, Work}, diff --git a/bitcoin/src/merkle_tree/block.rs b/bitcoin/src/merkle_tree/block.rs index 5f0cf041..79fedf02 100644 --- a/bitcoin/src/merkle_tree/block.rs +++ b/bitcoin/src/merkle_tree/block.rs @@ -44,10 +44,9 @@ use hashes::Hash; use self::MerkleBlockError::*; use crate::blockdata::block::{self, Block, TxMerkleNode}; -use crate::blockdata::transaction::Transaction; +use crate::blockdata::transaction::{Transaction, Txid}; use crate::blockdata::weight::Weight; use crate::consensus::encode::{self, Decodable, Encodable}; -use crate::hash_types::Txid; use crate::prelude::*; /// Data structure that represents a block header paired to a partial merkle tree. diff --git a/bitcoin/src/p2p/message_blockdata.rs b/bitcoin/src/p2p/message_blockdata.rs index cae8884a..e4995f3e 100644 --- a/bitcoin/src/p2p/message_blockdata.rs +++ b/bitcoin/src/p2p/message_blockdata.rs @@ -9,8 +9,8 @@ use hashes::{sha256d, Hash as _}; use crate::blockdata::block::BlockHash; +use crate::blockdata::transaction::{Txid, Wtxid}; use crate::consensus::encode::{self, Decodable, Encodable}; -use crate::hash_types::{Txid, Wtxid}; use crate::internal_macros::impl_consensus_encoding; use crate::prelude::*; use crate::{io, p2p}; From 2a0ac1258a534c93198445c9243f30d02c7eaa0e Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Tue, 7 Nov 2023 14:01:14 +1100 Subject: [PATCH 3/6] Move the bip158 filter 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 BIP-158 filter hash types to the `bip158` module. --- bitcoin/src/bip158.rs | 14 ++++++++++++-- bitcoin/src/consensus/encode.rs | 2 +- bitcoin/src/hash_types.rs | 25 ++++--------------------- bitcoin/src/lib.rs | 2 +- bitcoin/src/p2p/message_filter.rs | 2 +- 5 files changed, 19 insertions(+), 26 deletions(-) diff --git a/bitcoin/src/bip158.rs b/bitcoin/src/bip158.rs index b8f83d85..1b6f03da 100644 --- a/bitcoin/src/bip158.rs +++ b/bitcoin/src/bip158.rs @@ -41,7 +41,7 @@ use core::cmp::{self, Ordering}; use core::fmt::{self, Display, Formatter}; -use hashes::{siphash24, Hash}; +use hashes::{sha256d, siphash24, Hash}; use internals::write_err; use crate::blockdata::block::{Block, BlockHash}; @@ -49,13 +49,23 @@ use crate::blockdata::script::Script; use crate::blockdata::transaction::OutPoint; use crate::consensus::encode::VarInt; use crate::consensus::{Decodable, Encodable}; -use crate::hash_types::{FilterHash, FilterHeader}; +use crate::hash_types::impl_hashencode; use crate::prelude::*; /// Golomb encoding parameter as in BIP-158, see also https://gist.github.com/sipa/576d5f09c3b86c3b1b75598d799fc845 const P: u8 = 19; const M: u64 = 784931; +hashes::hash_newtype! { + /// Filter hash, as defined in BIP-157 + pub struct FilterHash(sha256d::Hash); + /// Filter header, as defined in BIP-157 + pub struct FilterHeader(sha256d::Hash); +} + +impl_hashencode!(FilterHash); +impl_hashencode!(FilterHeader); + /// Errors for blockfilter. #[derive(Debug)] #[non_exhaustive] diff --git a/bitcoin/src/consensus/encode.rs b/bitcoin/src/consensus/encode.rs index ce3d4a7b..8253eaa7 100644 --- a/bitcoin/src/consensus/encode.rs +++ b/bitcoin/src/consensus/encode.rs @@ -23,9 +23,9 @@ use internals::write_err; use io::{Cursor, Read}; use crate::bip152::{PrefilledTransaction, ShortId}; +use crate::bip158::{FilterHash, FilterHeader}; use crate::blockdata::block::{self, BlockHash, TxMerkleNode}; use crate::blockdata::transaction::{Transaction, TxIn, TxOut}; -use crate::hash_types::{FilterHash, FilterHeader}; #[cfg(feature = "std")] use crate::p2p::{ address::{AddrV2Message, Address}, diff --git a/bitcoin/src/hash_types.rs b/bitcoin/src/hash_types.rs index cbdb6eed..531eadc4 100644 --- a/bitcoin/src/hash_types.rs +++ b/bitcoin/src/hash_types.rs @@ -49,25 +49,8 @@ macro_rules! impl_asref_push_bytes { } pub(crate) use impl_asref_push_bytes; -// newtypes module is solely here so we can rustfmt::skip. -#[rustfmt::skip] -#[doc(inline)] -pub use newtypes::*; - -#[rustfmt::skip] -mod newtypes { - use hashes::{sha256d, hash_newtype}; - - hash_newtype! { - /// Filter hash, as defined in BIP-157 - pub struct FilterHash(sha256d::Hash); - /// Filter header, as defined in BIP-157 - pub struct FilterHeader(sha256d::Hash); - } - - impl_hashencode!(FilterHash); - impl_hashencode!(FilterHeader); -} - #[deprecated(since = "0.0.0-NEXT-RELEASE", note = "use crate::T instead")] -pub use crate::{BlockHash, TxMerkleNode, Txid, WitnessCommitment, WitnessMerkleNode, Wtxid}; +pub use crate::{ + BlockHash, FilterHash, FilterHeader, TxMerkleNode, Txid, WitnessCommitment, WitnessMerkleNode, + Wtxid, +}; diff --git a/bitcoin/src/lib.rs b/bitcoin/src/lib.rs index 0cb4d4a6..16a5394e 100644 --- a/bitcoin/src/lib.rs +++ b/bitcoin/src/lib.rs @@ -121,6 +121,7 @@ pub mod taproot; pub use crate::{ address::{Address, AddressType}, amount::{Amount, Denomination, SignedAmount}, + bip158::{FilterHash, FilterHeader}, bip32::XKeyIdentifier, blockdata::block::{self, Block, BlockHash, TxMerkleNode, WitnessMerkleNode, WitnessCommitment}, blockdata::constants, @@ -137,7 +138,6 @@ pub use crate::{ crypto::ecdsa, crypto::key::{self, PrivateKey, PubkeyHash, PublicKey, WPubkeyHash, XOnlyPublicKey}, crypto::sighash::{self, LegacySighash, SegwitV0Sighash, TapSighash, TapSighashTag}, - hash_types::{FilterHash, FilterHeader}, merkle_tree::MerkleBlock, network::Network, pow::{CompactTarget, Target, Work}, diff --git a/bitcoin/src/p2p/message_filter.rs b/bitcoin/src/p2p/message_filter.rs index e17c0904..82e6e7ea 100644 --- a/bitcoin/src/p2p/message_filter.rs +++ b/bitcoin/src/p2p/message_filter.rs @@ -5,8 +5,8 @@ //! This module describes BIP157 Client Side Block Filtering network messages. //! +use crate::bip158::{FilterHash, FilterHeader}; use crate::blockdata::block::BlockHash; -use crate::hash_types::{FilterHash, FilterHeader}; use crate::internal_macros::impl_consensus_encoding; /// getcfilters message From 2b4b66dee30c3a1d7ccbc101ae9b87084c44ff71 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Wed, 8 Nov 2023 08:53:41 +1100 Subject: [PATCH 4/6] Move impl_hashencode to internal_macros We are emptying the `hash_types` module. `impl_hashencode!` is an internal macro, as such it can live in the `internal_macros` module. --- bitcoin/src/bip158.rs | 2 +- bitcoin/src/blockdata/block.rs | 3 +-- bitcoin/src/blockdata/transaction.rs | 3 +-- bitcoin/src/hash_types.rs | 19 ------------------- bitcoin/src/internal_macros.rs | 19 +++++++++++++++++++ 5 files changed, 22 insertions(+), 24 deletions(-) diff --git a/bitcoin/src/bip158.rs b/bitcoin/src/bip158.rs index 1b6f03da..88875966 100644 --- a/bitcoin/src/bip158.rs +++ b/bitcoin/src/bip158.rs @@ -49,7 +49,7 @@ use crate::blockdata::script::Script; use crate::blockdata::transaction::OutPoint; use crate::consensus::encode::VarInt; use crate::consensus::{Decodable, Encodable}; -use crate::hash_types::impl_hashencode; +use crate::internal_macros::impl_hashencode; use crate::prelude::*; /// Golomb encoding parameter as in BIP-158, see also https://gist.github.com/sipa/576d5f09c3b86c3b1b75598d799fc845 diff --git a/bitcoin/src/blockdata/block.rs b/bitcoin/src/blockdata/block.rs index be216d00..50a66f41 100644 --- a/bitcoin/src/blockdata/block.rs +++ b/bitcoin/src/blockdata/block.rs @@ -16,8 +16,7 @@ use super::Weight; use crate::blockdata::script; use crate::blockdata::transaction::{Transaction, Txid, Wtxid}; use crate::consensus::{encode, Decodable, Encodable}; -use crate::hash_types::impl_hashencode; -use crate::internal_macros::impl_consensus_encoding; +use crate::internal_macros::{impl_consensus_encoding, impl_hashencode}; use crate::pow::{CompactTarget, Target, Work}; use crate::prelude::*; use crate::{merkle_tree, Network, VarInt}; diff --git a/bitcoin/src/blockdata/transaction.rs b/bitcoin/src/blockdata/transaction.rs index a481a789..02a34055 100644 --- a/bitcoin/src/blockdata/transaction.rs +++ b/bitcoin/src/blockdata/transaction.rs @@ -23,8 +23,7 @@ use crate::blockdata::locktime::relative; use crate::blockdata::script::{Script, ScriptBuf}; use crate::blockdata::witness::Witness; use crate::consensus::{encode, Decodable, Encodable}; -use crate::hash_types::impl_hashencode; -use crate::internal_macros::impl_consensus_encoding; +use crate::internal_macros::{impl_consensus_encoding, impl_hashencode}; use crate::parse::impl_parse_str_from_int_infallible; use crate::prelude::*; use crate::script::Push; diff --git a/bitcoin/src/hash_types.rs b/bitcoin/src/hash_types.rs index 531eadc4..f63fb540 100644 --- a/bitcoin/src/hash_types.rs +++ b/bitcoin/src/hash_types.rs @@ -8,25 +8,6 @@ //! hash). //! -#[rustfmt::skip] -macro_rules! impl_hashencode { - ($hashtype:ident) => { - impl $crate::consensus::Encodable for $hashtype { - fn consensus_encode(&self, w: &mut W) -> Result { - self.0.consensus_encode(w) - } - } - - impl $crate::consensus::Decodable for $hashtype { - fn consensus_decode(r: &mut R) -> Result { - use $crate::hashes::Hash; - Ok(Self::from_byte_array(<<$hashtype as $crate::hashes::Hash>::Bytes>::consensus_decode(r)?)) - } - } - }; -} -pub(crate) use impl_hashencode; - #[rustfmt::skip] macro_rules! impl_asref_push_bytes { ($($hashtype:ident),*) => { diff --git a/bitcoin/src/internal_macros.rs b/bitcoin/src/internal_macros.rs index db46f944..bfad3d6a 100644 --- a/bitcoin/src/internal_macros.rs +++ b/bitcoin/src/internal_macros.rs @@ -191,3 +191,22 @@ macro_rules! impl_bytes_newtype { }; } pub(crate) use impl_bytes_newtype; + +#[rustfmt::skip] +macro_rules! impl_hashencode { + ($hashtype:ident) => { + impl $crate::consensus::Encodable for $hashtype { + fn consensus_encode(&self, w: &mut W) -> Result { + self.0.consensus_encode(w) + } + } + + impl $crate::consensus::Decodable for $hashtype { + fn consensus_decode(r: &mut R) -> Result { + use $crate::hashes::Hash; + Ok(Self::from_byte_array(<<$hashtype as $crate::hashes::Hash>::Bytes>::consensus_decode(r)?)) + } + } + }; +} +pub(crate) use impl_hashencode; From 61351c917f1fb72c46f53e392a556b831bc1edf1 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Wed, 8 Nov 2023 08:58:06 +1100 Subject: [PATCH 5/6] Move impl_asref_push_bytes to internal_macros We are emptying the `hash_types` module. `impl_asref_push_bytes!` is an internal macro, as such it can live in the `internal_macros` module. While we are at it import the macro and call it without any qualifying path, this is typical for our usage of other internals/internal_macros usage. --- bitcoin/src/blockdata/script/mod.rs | 3 ++- bitcoin/src/crypto/key.rs | 3 ++- bitcoin/src/hash_types.rs | 22 ---------------------- bitcoin/src/internal_macros.rs | 22 ++++++++++++++++++++++ 4 files changed, 26 insertions(+), 24 deletions(-) diff --git a/bitcoin/src/blockdata/script/mod.rs b/bitcoin/src/blockdata/script/mod.rs index 413f06cc..05cf2957 100644 --- a/bitcoin/src/blockdata/script/mod.rs +++ b/bitcoin/src/blockdata/script/mod.rs @@ -72,6 +72,7 @@ use serde; use crate::blockdata::opcodes::all::*; use crate::blockdata::opcodes::{self, Opcode}; use crate::consensus::{encode, Decodable, Encodable}; +use crate::internal_macros::impl_asref_push_bytes; use crate::prelude::*; use crate::{io, OutPoint}; @@ -91,7 +92,7 @@ hashes::hash_newtype! { /// SegWit version of a Bitcoin Script bytecode hash. pub struct WScriptHash(sha256::Hash); } -crate::hash_types::impl_asref_push_bytes!(ScriptHash, WScriptHash); +impl_asref_push_bytes!(ScriptHash, WScriptHash); impl From for ScriptHash { fn from(script: ScriptBuf) -> ScriptHash { script.script_hash() } diff --git a/bitcoin/src/crypto/key.rs b/bitcoin/src/crypto/key.rs index 6c4d354c..91fe9cb7 100644 --- a/bitcoin/src/crypto/key.rs +++ b/bitcoin/src/crypto/key.rs @@ -14,6 +14,7 @@ use hex::FromHex; use internals::write_err; use crate::crypto::ecdsa; +use crate::internal_macros::impl_asref_push_bytes; use crate::network::Network; use crate::prelude::*; use crate::taproot::{TapNodeHash, TapTweakHash}; @@ -252,7 +253,7 @@ hashes::hash_newtype! { /// SegWit version of a public key hash. pub struct WPubkeyHash(hash160::Hash); } -crate::hash_types::impl_asref_push_bytes!(PubkeyHash, WPubkeyHash); +impl_asref_push_bytes!(PubkeyHash, WPubkeyHash); impl From for PubkeyHash { fn from(key: PublicKey) -> PubkeyHash { key.pubkey_hash() } diff --git a/bitcoin/src/hash_types.rs b/bitcoin/src/hash_types.rs index f63fb540..db0fbc9e 100644 --- a/bitcoin/src/hash_types.rs +++ b/bitcoin/src/hash_types.rs @@ -8,28 +8,6 @@ //! hash). //! -#[rustfmt::skip] -macro_rules! impl_asref_push_bytes { - ($($hashtype:ident),*) => { - $( - impl AsRef<$crate::blockdata::script::PushBytes> for $hashtype { - fn as_ref(&self) -> &$crate::blockdata::script::PushBytes { - use $crate::hashes::Hash; - self.as_byte_array().into() - } - } - - impl From<$hashtype> for $crate::blockdata::script::PushBytesBuf { - fn from(hash: $hashtype) -> Self { - use $crate::hashes::Hash; - hash.as_byte_array().into() - } - } - )* - }; -} -pub(crate) use impl_asref_push_bytes; - #[deprecated(since = "0.0.0-NEXT-RELEASE", note = "use crate::T instead")] pub use crate::{ BlockHash, FilterHash, FilterHeader, TxMerkleNode, Txid, WitnessCommitment, WitnessMerkleNode, diff --git a/bitcoin/src/internal_macros.rs b/bitcoin/src/internal_macros.rs index bfad3d6a..669c3ff6 100644 --- a/bitcoin/src/internal_macros.rs +++ b/bitcoin/src/internal_macros.rs @@ -210,3 +210,25 @@ macro_rules! impl_hashencode { }; } pub(crate) use impl_hashencode; + +#[rustfmt::skip] +macro_rules! impl_asref_push_bytes { + ($($hashtype:ident),*) => { + $( + impl AsRef<$crate::blockdata::script::PushBytes> for $hashtype { + fn as_ref(&self) -> &$crate::blockdata::script::PushBytes { + use $crate::hashes::Hash; + self.as_byte_array().into() + } + } + + impl From<$hashtype> for $crate::blockdata::script::PushBytesBuf { + fn from(hash: $hashtype) -> Self { + use $crate::hashes::Hash; + hash.as_byte_array().into() + } + } + )* + }; +} +pub(crate) use impl_asref_push_bytes; From 801c72e0566e38f98fe93a43074f4a6db8b20958 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Wed, 8 Nov 2023 09:00:53 +1100 Subject: [PATCH 6/6] Add deprecation comment to hash_types module The `hash_types` module has been emptied, it now contains only deprecated re-exports. Add a rustdoc comment stating as such. --- bitcoin/src/hash_types.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/bitcoin/src/hash_types.rs b/bitcoin/src/hash_types.rs index db0fbc9e..37d8c76f 100644 --- a/bitcoin/src/hash_types.rs +++ b/bitcoin/src/hash_types.rs @@ -2,11 +2,7 @@ //! Bitcoin hash types. //! -//! This module defines types for hashes used throughout the library. These -//! types are needed in order to avoid mixing data of the same hash format -//! (e.g. `SHA256d`) but of different meaning (such as transaction id, block -//! hash). -//! +//! This module is deprecated. You can find hash types in their respective, hopefully obvious, modules. #[deprecated(since = "0.0.0-NEXT-RELEASE", note = "use crate::T instead")] pub use crate::{