From 4865d60258cd0ba7e8a200cfc169f8726fbe81ee Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Mon, 11 Nov 2024 14:10:33 +1100 Subject: [PATCH 1/3] bitcoin: Improve the re-exports ... again These re-exports are hard to get right. We (I) recently made an attempt to make our stack of crates easier to navigate using the idea that exporting a type from the crate it is defined in adds some additional information without any loss of clarity. Note however that the module must be re-exported from the "highest" place possible because we at times add additional functionality as we move up the stack e.g., `bitcoin::merkle_tree` has logic in it that `primitives::merkle_tree` does not. In order to future proof the codebase default to always using the highest module up the stack even when that module adds no additional code e.g., re-export `blockdata::fee_rate` as opposed to `units::fee_rate` in the event that we later add logic to `bitcoin::blockdata::fee_rate`. This patch adds an additional re-export: `sequence` (previously missing). --- bitcoin/src/lib.rs | 50 +++++++++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/bitcoin/src/lib.rs b/bitcoin/src/lib.rs index 5685414b4..9d9dc9628 100644 --- a/bitcoin/src/lib.rs +++ b/bitcoin/src/lib.rs @@ -114,7 +114,27 @@ pub mod psbt; pub mod sign_message; pub mod taproot; -#[rustfmt::skip] // Keep public re-exports separate. +// Re-export the type from where it is defined but the module from the highest place up the stack +// that it is available in the event that we add some functionality there. +#[doc(inline)] +pub use primitives::{ + block::{BlockHash, WitnessCommitment, Header as BlockHeader}, + merkle_tree::{TxMerkleNode, WitnessMerkleNode}, + opcodes::Opcode, + pow::CompactTarget, // No `pow` module outside of `primitives`. + script::{Script, ScriptBuf}, + sequence::{self, Sequence}, // No `sequence` module outside of `primitives`. + transaction::{OutPoint, Transaction, TxIn, TxOut, Txid, Wtxid}, + witness::Witness, +}; +#[doc(inline)] +pub use units::{ + amount::{Amount, Denomination, SignedAmount}, + block::{BlockHeight, BlockInterval}, + fee_rate::FeeRate, + weight::Weight, +}; + #[doc(inline)] pub use crate::{ address::{Address, AddressType, KnownHrp}, @@ -134,32 +154,16 @@ pub use crate::{ // Re-export all modules from `blockdata`, users should never need to use `blockdata` directly. #[doc(inline)] pub use crate::{ - // These modules also re-export all the respective `primitives` types. - blockdata::{block, constants, fee_rate, locktime, opcodes, script, transaction, weight, witness}, - // And re-export types and modules from `blockdata` that don't come from `primitives`. - blockdata::block::Block, // TODO: Move this down below after it is in primitives. + // Also, re-export types and modules from `blockdata` that don't come from `primitives`. + blockdata::block::Block, // TODO: Move this after `Block` is in primitives. blockdata::locktime::{absolute, relative}, blockdata::script::witness_program::{self, WitnessProgram}, blockdata::script::witness_version::{self, WitnessVersion}, blockdata::script::{ScriptHash, WScriptHash}, // TODO: Move these down below after they are in primitives. -}; -#[doc(inline)] -pub use primitives::{ - block::{BlockHash, WitnessCommitment, Header as BlockHeader}, - merkle_tree::{TxMerkleNode, WitnessMerkleNode}, - opcodes::Opcode, - pow::CompactTarget, - script::{Script, ScriptBuf}, - transaction::{OutPoint, Transaction, TxIn, TxOut, Txid, Wtxid}, - witness::Witness, - sequence::Sequence, -}; -#[doc(inline)] -pub use units::{ - amount::{Amount, Denomination, SignedAmount}, - block::{BlockHeight, BlockInterval}, - fee_rate::FeeRate, - weight::Weight + // These modules also re-export all the respective `primitives` types. + blockdata::{ + block, constants, fee_rate, locktime, opcodes, script, transaction, weight, witness, + }, }; #[rustfmt::skip] From 84ede349b0b1b7ef00526594750f86d547e2070b Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Mon, 11 Nov 2024 14:19:17 +1100 Subject: [PATCH 2/3] Run the formatter Run `just fmt`, no manual changes. --- bitcoin/src/blockdata/block.rs | 7 +++++-- bitcoin/src/blockdata/script/push_bytes.rs | 4 +--- bitcoin/src/lib.rs | 8 +++++--- chacha20_poly1305/src/lib.rs | 1 - units/src/amount/signed.rs | 5 +---- units/src/amount/unsigned.rs | 5 +---- 6 files changed, 13 insertions(+), 17 deletions(-) diff --git a/bitcoin/src/blockdata/block.rs b/bitcoin/src/blockdata/block.rs index 391b0992a..c4825b601 100644 --- a/bitcoin/src/blockdata/block.rs +++ b/bitcoin/src/blockdata/block.rs @@ -249,12 +249,15 @@ impl Block { let cb = self.coinbase().ok_or(Bip34Error::NotPresent)?; let input = cb.input.first().ok_or(Bip34Error::NotPresent)?; - let push = input.script_sig.instructions_minimal() + let push = input + .script_sig + .instructions_minimal() .next() .ok_or(Bip34Error::NotPresent)? .map_err(to_bip34_error)?; match (push.script_num(), push.push_bytes().map(|b| b.read_scriptint())) { - (Some(num), Some(Ok(_)) | None) => Ok(num.try_into().map_err(|_| Bip34Error::NegativeHeight)?), + (Some(num), Some(Ok(_)) | None) => + Ok(num.try_into().map_err(|_| Bip34Error::NegativeHeight)?), (_, Some(Err(err))) => Err(to_bip34_error(err)), (None, _) => Err(Bip34Error::NotPresent), } diff --git a/bitcoin/src/blockdata/script/push_bytes.rs b/bitcoin/src/blockdata/script/push_bytes.rs index d597cb79d..59f651314 100644 --- a/bitcoin/src/blockdata/script/push_bytes.rs +++ b/bitcoin/src/blockdata/script/push_bytes.rs @@ -64,9 +64,7 @@ mod primitive { } /// Constructs an empty `&PushBytes`. - pub fn empty() -> &'static Self { - Self::from_slice_unchecked(&[]) - } + pub fn empty() -> &'static Self { Self::from_slice_unchecked(&[]) } /// Returns the underlying bytes. pub fn as_bytes(&self) -> &[u8] { &self.0 } diff --git a/bitcoin/src/lib.rs b/bitcoin/src/lib.rs index 9d9dc9628..0a26fa1c2 100644 --- a/bitcoin/src/lib.rs +++ b/bitcoin/src/lib.rs @@ -118,7 +118,7 @@ pub mod taproot; // that it is available in the event that we add some functionality there. #[doc(inline)] pub use primitives::{ - block::{BlockHash, WitnessCommitment, Header as BlockHeader}, + block::{BlockHash, Header as BlockHeader, WitnessCommitment}, merkle_tree::{TxMerkleNode, WitnessMerkleNode}, opcodes::Opcode, pow::CompactTarget, // No `pow` module outside of `primitives`. @@ -141,11 +141,13 @@ pub use crate::{ bip158::{FilterHash, FilterHeader}, bip32::XKeyIdentifier, crypto::ecdsa, - crypto::key::{self, PrivateKey, PubkeyHash, PublicKey, CompressedPublicKey, WPubkeyHash, XOnlyPublicKey}, + crypto::key::{ + self, CompressedPublicKey, PrivateKey, PubkeyHash, PublicKey, WPubkeyHash, XOnlyPublicKey, + }, crypto::sighash::{self, LegacySighash, SegwitV0Sighash, TapSighash, TapSighashTag}, merkle_tree::MerkleBlock, - network::{Network, NetworkKind, TestnetVersion}, network::params::{self, Params}, + network::{Network, NetworkKind, TestnetVersion}, pow::{Target, Work}, psbt::Psbt, sighash::{EcdsaSighashType, TapSighashType}, diff --git a/chacha20_poly1305/src/lib.rs b/chacha20_poly1305/src/lib.rs index 04d7f3771..330f5389e 100644 --- a/chacha20_poly1305/src/lib.rs +++ b/chacha20_poly1305/src/lib.rs @@ -3,7 +3,6 @@ //! Combine the ChaCha20 stream cipher with the Poly1305 message authentication code //! to form an authenticated encryption with additional data (AEAD) algorithm. #![no_std] - // Experimental features we need. #![cfg_attr(docsrs, feature(doc_auto_cfg))] // Coding conventions. diff --git a/units/src/amount/signed.rs b/units/src/amount/signed.rs index 2d763335f..d9c9efe97 100644 --- a/units/src/amount/signed.rs +++ b/units/src/amount/signed.rs @@ -67,10 +67,7 @@ impl SignedAmount { pub fn from_int_btc(btc: i64) -> Result { match btc.checked_mul(100_000_000) { Some(amount) => Ok(SignedAmount::from_sat(amount)), - None => Err(OutOfRangeError { - is_signed: true, - is_greater_than_max: true, - }) + None => Err(OutOfRangeError { is_signed: true, is_greater_than_max: true }), } } diff --git a/units/src/amount/unsigned.rs b/units/src/amount/unsigned.rs index 28cbf235e..f217c2371 100644 --- a/units/src/amount/unsigned.rs +++ b/units/src/amount/unsigned.rs @@ -77,10 +77,7 @@ impl Amount { pub fn from_int_btc(btc: u64) -> Result { match btc.checked_mul(100_000_000) { Some(amount) => Ok(Amount::from_sat(amount)), - None => Err(OutOfRangeError { - is_signed: false, - is_greater_than_max: true, - }) + None => Err(OutOfRangeError { is_signed: false, is_greater_than_max: true }), } } From 727c519efa771d3ec6a21350e4638b9f1536bfe1 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Mon, 11 Nov 2024 14:21:47 +1100 Subject: [PATCH 3/3] Re-export amount from primitives We are trying to make it so that what ever crate a user uses they see the same module/type tree (if the module or type exists). E.g., one can do either of these. If they use `bitcoin`: ``` use bitcoin::{ amount, block, fee_rate, weight, merkle_tree, opcodes, pow, script, sequence, transaction, witness, }; ``` Or if they use `primitives`: ``` use bitcoin_primitives::{amount, block, fee_rate, weight}; ``` The above imports were tested and `amount` was found to be missing. --- primitives/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/primitives/src/lib.rs b/primitives/src/lib.rs index b8f09ac8b..a6e41b35e 100644 --- a/primitives/src/lib.rs +++ b/primitives/src/lib.rs @@ -44,7 +44,7 @@ pub mod transaction; pub mod witness; #[doc(inline)] -pub use units::amount::{Amount, SignedAmount}; +pub use units::amount::{self, Amount, SignedAmount}; #[cfg(feature = "alloc")] #[doc(inline)] pub use units::{