diff --git a/bitcoin/src/bip152.rs b/bitcoin/src/bip152.rs index e602a3133..979dabc5e 100644 --- a/bitcoin/src/bip152.rs +++ b/bitcoin/src/bip152.rs @@ -413,7 +413,7 @@ mod test { use crate::merkle_tree::TxMerkleNode; use crate::transaction::OutPointExt; use crate::{ - transaction, Amount, BlockChecked, CompactTarget, OutPoint, ScriptBuf, Sequence, BlockTime, + transaction, Amount, BlockChecked, BlockTime, CompactTarget, OutPoint, ScriptBuf, Sequence, TxIn, TxOut, Txid, Witness, }; diff --git a/bitcoin/src/blockdata/constants.rs b/bitcoin/src/blockdata/constants.rs index 8d48609aa..6e8ef036e 100644 --- a/bitcoin/src/blockdata/constants.rs +++ b/bitcoin/src/blockdata/constants.rs @@ -16,7 +16,7 @@ use crate::opcodes::all::*; use crate::pow::CompactTarget; use crate::transaction::{self, OutPoint, Transaction, TxIn, TxOut}; use crate::witness::Witness; -use crate::{script, Amount, BlockHash, Sequence, TestnetVersion, BlockTime}; +use crate::{script, Amount, BlockHash, BlockTime, Sequence, TestnetVersion}; /// How many seconds between blocks we expect on average. pub const TARGET_BLOCK_SPACING: u32 = 600; diff --git a/bitcoin/src/blockdata/mod.rs b/bitcoin/src/blockdata/mod.rs index 9916e56af..a6f39ba9a 100644 --- a/bitcoin/src/blockdata/mod.rs +++ b/bitcoin/src/blockdata/mod.rs @@ -21,10 +21,10 @@ pub use self::{ /// Implements `FeeRate` and assoctiated features. pub mod fee_rate { - /// Re-export everything from the [`units::fee_rate`] module. - pub use units::fee_rate::FeeRate; #[cfg(feature = "serde")] pub use units::fee_rate::serde; + /// Re-export everything from the [`units::fee_rate`] module. + pub use units::fee_rate::FeeRate; } /// Provides absolute and relative locktimes. diff --git a/bitcoin/src/blockdata/script/tests.rs b/bitcoin/src/blockdata/script/tests.rs index bbc0c43b0..3f4d56aea 100644 --- a/bitcoin/src/blockdata/script/tests.rs +++ b/bitcoin/src/blockdata/script/tests.rs @@ -8,7 +8,7 @@ use crate::address::script_pubkey::{ }; use crate::consensus::encode::{deserialize, serialize}; use crate::crypto::key::{PublicKey, XOnlyPublicKey}; -use crate::{Amount, FeeRate, opcodes}; +use crate::{opcodes, Amount, FeeRate}; #[test] #[rustfmt::skip] diff --git a/bitcoin/src/crypto/key.rs b/bitcoin/src/crypto/key.rs index 4581298b5..862ed1fc7 100644 --- a/bitcoin/src/crypto/key.rs +++ b/bitcoin/src/crypto/key.rs @@ -462,7 +462,7 @@ impl PrivateKey { /// Deserializes a private key from a byte array. pub fn from_byte_array( data: [u8; 32], - network: impl Into + network: impl Into, ) -> Result { Ok(PrivateKey::new(secp256k1::SecretKey::from_byte_array(&data)?, network)) } diff --git a/bitcoin/src/lib.rs b/bitcoin/src/lib.rs index ffb70ba54..545604397 100644 --- a/bitcoin/src/lib.rs +++ b/bitcoin/src/lib.rs @@ -164,9 +164,7 @@ pub use crate::{ blockdata::script::witness_program::{self, WitnessProgram}, blockdata::script::witness_version::{self, WitnessVersion}, // These modules also re-export all the respective `primitives` types. - blockdata::{ - block, constants, fee_rate, locktime, script, transaction, weight, witness, - }, + blockdata::{block, constants, fee_rate, locktime, script, transaction, weight, witness}, }; #[rustfmt::skip] diff --git a/bitcoin/src/psbt/mod.rs b/bitcoin/src/psbt/mod.rs index bfe528258..ffba04ea9 100644 --- a/bitcoin/src/psbt/mod.rs +++ b/bitcoin/src/psbt/mod.rs @@ -135,7 +135,7 @@ impl Psbt { /// An alias for [`extract_tx_fee_rate_limit`]. /// /// [`extract_tx_fee_rate_limit`]: Psbt::extract_tx_fee_rate_limit - #[allow(clippy::result_large_err)] // The PSBT returned in `SendingToomuch` is large. + #[allow(clippy::result_large_err)] // The PSBT returned in `SendingToomuch` is large. pub fn extract_tx(self) -> Result { self.internal_extract_tx_with_fee_rate_limit(Self::DEFAULT_MAX_FEE_RATE) } @@ -147,7 +147,7 @@ impl Psbt { /// [`ExtractTxError`] variants will contain either the [`Psbt`] itself or the [`Transaction`] /// that was extracted. These can be extracted from the Errors in order to recover. /// See the error documentation for info on the variants. In general, it covers large fees. - #[allow(clippy::result_large_err)] // The PSBT returned in `SendingToomuch` is large. + #[allow(clippy::result_large_err)] // The PSBT returned in `SendingToomuch` is large. pub fn extract_tx_fee_rate_limit(self) -> Result { self.internal_extract_tx_with_fee_rate_limit(Self::DEFAULT_MAX_FEE_RATE) } @@ -159,7 +159,7 @@ impl Psbt { /// See [`extract_tx`]. /// /// [`extract_tx`]: Psbt::extract_tx - #[allow(clippy::result_large_err)] // The PSBT returned in `SendingToomuch` is large. + #[allow(clippy::result_large_err)] // The PSBT returned in `SendingToomuch` is large. pub fn extract_tx_with_fee_rate_limit( self, max_fee_rate: FeeRate, @@ -187,7 +187,7 @@ impl Psbt { } #[inline] - #[allow(clippy::result_large_err)] // The PSBT returned in `SendingToomuch` is large. + #[allow(clippy::result_large_err)] // The PSBT returned in `SendingToomuch` is large. fn internal_extract_tx_with_fee_rate_limit( self, max_fee_rate: FeeRate, diff --git a/bitcoin/src/psbt/serialize.rs b/bitcoin/src/psbt/serialize.rs index abe0853f2..34086afab 100644 --- a/bitcoin/src/psbt/serialize.rs +++ b/bitcoin/src/psbt/serialize.rs @@ -260,10 +260,8 @@ impl Serialize for XOnlyPublicKey { impl Deserialize for XOnlyPublicKey { fn deserialize(bytes: &[u8]) -> Result { - XOnlyPublicKey::from_byte_array( - bytes.try_into().map_err(|_| Error::InvalidXOnlyPublicKey)?, - ) - .map_err(|_| Error::InvalidXOnlyPublicKey) + XOnlyPublicKey::from_byte_array(bytes.try_into().map_err(|_| Error::InvalidXOnlyPublicKey)?) + .map_err(|_| Error::InvalidXOnlyPublicKey) } } diff --git a/bitcoin/src/taproot/merkle_branch.rs b/bitcoin/src/taproot/merkle_branch.rs index 33f0330fc..5ad08cbc1 100644 --- a/bitcoin/src/taproot/merkle_branch.rs +++ b/bitcoin/src/taproot/merkle_branch.rs @@ -52,11 +52,7 @@ impl TaprootMerkleBranch { } else if node_hashes.len() > TAPROOT_CONTROL_MAX_NODE_COUNT { Err(InvalidMerkleTreeDepthError(sl.len() / TAPROOT_CONTROL_NODE_SIZE).into()) } else { - let inner = node_hashes - .iter() - .copied() - .map(TapNodeHash::from_byte_array) - .collect(); + let inner = node_hashes.iter().copied().map(TapNodeHash::from_byte_array).collect(); Ok(TaprootMerkleBranch(inner)) } diff --git a/bitcoin/tests/serde.rs b/bitcoin/tests/serde.rs index dff93b0c5..66ffc63b0 100644 --- a/bitcoin/tests/serde.rs +++ b/bitcoin/tests/serde.rs @@ -109,8 +109,7 @@ fn serde_regression_txin() { #[test] fn serde_regression_txout() { - let txout = - TxOut { value: Amount::MAX, script_pubkey: ScriptBuf::from(vec![0u8, 1u8, 2u8]) }; + let txout = TxOut { value: Amount::MAX, script_pubkey: ScriptBuf::from(vec![0u8, 1u8, 2u8]) }; let got = serialize(&txout).unwrap(); let want = include_bytes!("data/serde/txout_bincode") as &[_]; assert_eq!(got, want) diff --git a/fuzz/fuzz_targets/bitcoin/deserialize_psbt.rs b/fuzz/fuzz_targets/bitcoin/deserialize_psbt.rs index 043d19596..7b36cf697 100644 --- a/fuzz/fuzz_targets/bitcoin/deserialize_psbt.rs +++ b/fuzz/fuzz_targets/bitcoin/deserialize_psbt.rs @@ -1,6 +1,5 @@ -use honggfuzz::fuzz; - use bitcoin_fuzz::fuzz_utils::consume_random_bytes; +use honggfuzz::fuzz; fn do_test(data: &[u8]) { let mut new_data = data; diff --git a/fuzz/fuzz_targets/bitcoin/deserialize_script.rs b/fuzz/fuzz_targets/bitcoin/deserialize_script.rs index 0968b64f0..41ef80493 100644 --- a/fuzz/fuzz_targets/bitcoin/deserialize_script.rs +++ b/fuzz/fuzz_targets/bitcoin/deserialize_script.rs @@ -2,9 +2,8 @@ use bitcoin::address::Address; use bitcoin::consensus::encode; use bitcoin::script::{self, ScriptExt as _}; use bitcoin::{FeeRate, Network}; -use honggfuzz::fuzz; - use bitcoin_fuzz::fuzz_utils::{consume_random_bytes, consume_u64}; +use honggfuzz::fuzz; fn do_test(data: &[u8]) { let mut new_data = data; diff --git a/hashes/src/ripemd160/crypto.rs b/hashes/src/ripemd160/crypto.rs index 178a2f441..70e92f66d 100644 --- a/hashes/src/ripemd160/crypto.rs +++ b/hashes/src/ripemd160/crypto.rs @@ -1,6 +1,7 @@ // SPDX-License-Identifier: CC0-1.0 use internals::slice::SliceExt; + use super::{HashEngine, BLOCK_SIZE}; #[cfg(feature = "small-hash")] diff --git a/hashes/src/sha1/crypto.rs b/hashes/src/sha1/crypto.rs index 9076c9109..70d04f506 100644 --- a/hashes/src/sha1/crypto.rs +++ b/hashes/src/sha1/crypto.rs @@ -1,6 +1,7 @@ // SPDX-License-Identifier: CC0-1.0 use internals::slice::SliceExt; + use super::{HashEngine, BLOCK_SIZE}; impl HashEngine { diff --git a/hashes/src/sha256/crypto.rs b/hashes/src/sha256/crypto.rs index 34e780d7b..eea9ff7a0 100644 --- a/hashes/src/sha256/crypto.rs +++ b/hashes/src/sha256/crypto.rs @@ -6,6 +6,7 @@ use core::arch::x86::*; use core::arch::x86_64::*; use internals::slice::SliceExt; + use super::{HashEngine, Midstate, BLOCK_SIZE}; #[allow(non_snake_case)] diff --git a/hashes/src/sha256/mod.rs b/hashes/src/sha256/mod.rs index ac11002c7..a45eb0bfe 100644 --- a/hashes/src/sha256/mod.rs +++ b/hashes/src/sha256/mod.rs @@ -9,6 +9,7 @@ mod crypto; mod tests; use core::{cmp, convert, fmt}; + use internals::slice::SliceExt; use crate::{incomplete_block_len, sha256d, HashEngine as _}; @@ -80,7 +81,8 @@ impl HashEngine { /// Please see docs on [`Midstate`] before using this function. pub fn from_midstate(midstate: Midstate) -> HashEngine { let mut ret = [0; 8]; - for (ret_val, midstate_bytes) in ret.iter_mut().zip(midstate.as_ref().bitcoin_as_chunks().0) { + for (ret_val, midstate_bytes) in ret.iter_mut().zip(midstate.as_ref().bitcoin_as_chunks().0) + { *ret_val = u32::from_be_bytes(*midstate_bytes); } diff --git a/hashes/src/sha512/crypto.rs b/hashes/src/sha512/crypto.rs index 0a9d5b0da..6bf4f5ce2 100644 --- a/hashes/src/sha512/crypto.rs +++ b/hashes/src/sha512/crypto.rs @@ -1,6 +1,7 @@ // SPDX-License-Identifier: CC0-1.0 use internals::slice::SliceExt; + use super::{HashEngine, BLOCK_SIZE}; #[allow(non_snake_case)] diff --git a/internals/src/slice.rs b/internals/src/slice.rs index 38ac95878..4c32126d0 100644 --- a/internals/src/slice.rs +++ b/internals/src/slice.rs @@ -23,7 +23,9 @@ pub trait SliceExt { /// let mut slice = [1, 2, 3]; /// let fail = slice.as_chunks_mut::<0>(); /// ``` - fn bitcoin_as_chunks_mut(&mut self) -> (&mut [[Self::Item; N]], &mut [Self::Item]); + fn bitcoin_as_chunks_mut( + &mut self, + ) -> (&mut [[Self::Item; N]], &mut [Self::Item]); } impl SliceExt for [T] { @@ -40,11 +42,15 @@ impl SliceExt for [T] { // we're merely casting, so no aliasing issues here // arrays of T have same alignment as T // the resulting slice points within the obtained slice as was computed above - let left = unsafe { core::slice::from_raw_parts(left.as_ptr().cast::<[Self::Item; N]>(), chunks_count) }; + let left = unsafe { + core::slice::from_raw_parts(left.as_ptr().cast::<[Self::Item; N]>(), chunks_count) + }; (left, right) } - fn bitcoin_as_chunks_mut(&mut self) -> (&mut [[Self::Item; N]], &mut [Self::Item]) { + fn bitcoin_as_chunks_mut( + &mut self, + ) -> (&mut [[Self::Item; N]], &mut [Self::Item]) { #[allow(clippy::let_unit_value)] let _ = Hack::::IS_NONZERO; @@ -55,7 +61,12 @@ impl SliceExt for [T] { // we're merely casting, so no aliasing issues here // arrays of T have same alignment as T // the resulting slice points within the obtained slice as was computed above - let left = unsafe { core::slice::from_raw_parts_mut(left.as_mut_ptr().cast::<[Self::Item; N]>(), chunks_count) }; + let left = unsafe { + core::slice::from_raw_parts_mut( + left.as_mut_ptr().cast::<[Self::Item; N]>(), + chunks_count, + ) + }; (left, right) } } @@ -63,7 +74,9 @@ impl SliceExt for [T] { struct Hack; impl Hack { - const IS_NONZERO: () = { assert!(N != 0); }; + const IS_NONZERO: () = { + assert!(N != 0); + }; } #[cfg(test)] diff --git a/primitives/src/opcodes.rs b/primitives/src/opcodes.rs index 3a3bce70f..843844556 100644 --- a/primitives/src/opcodes.rs +++ b/primitives/src/opcodes.rs @@ -6,7 +6,7 @@ //! all of the opcodes for that language. #![allow(non_camel_case_types)] -#![allow(dead_code)] // This module is private and is duplicated in `bitcoin`. +#![allow(dead_code)] // This module is private and is duplicated in `bitcoin`. use core::fmt; diff --git a/primitives/src/transaction.rs b/primitives/src/transaction.rs index bb2f81522..764ed68c6 100644 --- a/primitives/src/transaction.rs +++ b/primitives/src/transaction.rs @@ -670,7 +670,10 @@ mod tests { witness: Witness::new(), }; - let txout = TxOut { value: Amount::from_sat(123_456_789).unwrap(), script_pubkey: ScriptBuf::new() }; + let txout = TxOut { + value: Amount::from_sat(123_456_789).unwrap(), + script_pubkey: ScriptBuf::new(), + }; let tx_orig = Transaction { version: Version::ONE, diff --git a/primitives/src/witness.rs b/primitives/src/witness.rs index d5cf27225..bf2a8f9f8 100644 --- a/primitives/src/witness.rs +++ b/primitives/src/witness.rs @@ -355,8 +355,7 @@ impl<'de> serde::Deserialize<'de> for Witness { self, mut a: A, ) -> Result { - use hex::FromHex; - use hex::HexToBytesError as E; + use hex::{FromHex, HexToBytesError as E}; use serde::de::{self, Unexpected}; let mut ret = match a.size_hint() { @@ -366,16 +365,17 @@ impl<'de> serde::Deserialize<'de> for Witness { while let Some(elem) = a.next_element::()? { let vec = Vec::::from_hex(&elem).map_err(|e| match e { - E::InvalidChar(ref e) => match core::char::from_u32(e.invalid_char().into()) { - Some(c) => de::Error::invalid_value( - Unexpected::Char(c), - &"a valid hex character", - ), - None => de::Error::invalid_value( - Unexpected::Unsigned(e.invalid_char().into()), - &"a valid hex character", - ), - }, + E::InvalidChar(ref e) => + match core::char::from_u32(e.invalid_char().into()) { + Some(c) => de::Error::invalid_value( + Unexpected::Char(c), + &"a valid hex character", + ), + None => de::Error::invalid_value( + Unexpected::Unsigned(e.invalid_char().into()), + &"a valid hex character", + ), + }, E::OddLengthString(ref e) => de::Error::invalid_length(e.length(), &"an even length string"), })?; diff --git a/units/src/amount/mod.rs b/units/src/amount/mod.rs index 9333b87e6..98e2137a8 100644 --- a/units/src/amount/mod.rs +++ b/units/src/amount/mod.rs @@ -115,7 +115,7 @@ impl Denomination { Denomination::MicroBitcoin => -2, Denomination::Bit => -2, Denomination::Satoshi => 0, - Denomination::_DoNotUse(infallible) => match infallible {} + Denomination::_DoNotUse(infallible) => match infallible {}, } } @@ -128,7 +128,7 @@ impl Denomination { Denomination::MicroBitcoin => "uBTC", Denomination::Bit => "bits", Denomination::Satoshi => "satoshi", - Denomination::_DoNotUse(infallible) => match infallible {} + Denomination::_DoNotUse(infallible) => match infallible {}, } } diff --git a/units/src/amount/tests.rs b/units/src/amount/tests.rs index b9d5518eb..fedeb78cd 100644 --- a/units/src/amount/tests.rs +++ b/units/src/amount/tests.rs @@ -345,14 +345,8 @@ fn parsing() { assert_eq!(p("1.1", den_btc), Ok(sat(1_100_000_00))); assert_eq!(p("100", den_sat), Ok(sat(100))); assert_eq!(p("55", den_sat), Ok(sat(55))); - assert_eq!( - p("2100000000000000", den_sat), - Ok(sat(21_000_000__000_000_00)) - ); - assert_eq!( - p("2100000000000000.", den_sat), - Ok(sat(21_000_000__000_000_00)) - ); + assert_eq!(p("2100000000000000", den_sat), Ok(sat(21_000_000__000_000_00))); + assert_eq!(p("2100000000000000.", den_sat), Ok(sat(21_000_000__000_000_00))); assert_eq!(p("21000000", den_btc), Ok(sat(21_000_000__000_000_00))); // exactly 50 chars. @@ -702,28 +696,15 @@ fn to_from_string_in() { assert_eq!(ua_str(&ua_sat(21_000_000).to_string_in(D::Bit), D::Bit), Ok(ua_sat(21_000_000))); assert_eq!(ua_str(&ua_sat(0).to_string_in(D::Satoshi), D::Satoshi), Ok(ua_sat(0))); - assert!( - ua_str(&ua_sat(Amount::MAX.to_sat()).to_string_in(D::Bitcoin), D::Bitcoin).is_ok() - ); - assert!(ua_str( - &ua_sat(Amount::MAX.to_sat()).to_string_in(D::CentiBitcoin), - D::CentiBitcoin - ) - .is_ok()); - assert!(ua_str( - &ua_sat(Amount::MAX.to_sat()).to_string_in(D::MilliBitcoin), - D::MilliBitcoin - ) - .is_ok()); - assert!(ua_str( - &ua_sat(Amount::MAX.to_sat()).to_string_in(D::MicroBitcoin), - D::MicroBitcoin - ) - .is_ok()); + assert!(ua_str(&ua_sat(Amount::MAX.to_sat()).to_string_in(D::Bitcoin), D::Bitcoin).is_ok()); + assert!(ua_str(&ua_sat(Amount::MAX.to_sat()).to_string_in(D::CentiBitcoin), D::CentiBitcoin) + .is_ok()); + assert!(ua_str(&ua_sat(Amount::MAX.to_sat()).to_string_in(D::MilliBitcoin), D::MilliBitcoin) + .is_ok()); + assert!(ua_str(&ua_sat(Amount::MAX.to_sat()).to_string_in(D::MicroBitcoin), D::MicroBitcoin) + .is_ok()); assert!(ua_str(&ua_sat(Amount::MAX.to_sat()).to_string_in(D::Bit), D::Bit).is_ok()); - assert!( - ua_str(&ua_sat(Amount::MAX.to_sat()).to_string_in(D::Satoshi), D::Satoshi).is_ok() - ); + assert!(ua_str(&ua_sat(Amount::MAX.to_sat()).to_string_in(D::Satoshi), D::Satoshi).is_ok()); assert_eq!( sa_str(&SignedAmount::MAX.to_string_in(D::Satoshi), D::MicroBitcoin), @@ -970,11 +951,19 @@ fn sum_amounts() { assert_eq!([].iter().sum::>(), Amount::ZERO.into()); assert_eq!([].iter().sum::>(), SignedAmount::ZERO.into()); - let results = [NumOpResult::Valid(sat(42)), NumOpResult::Valid(sat(1337)), NumOpResult::Valid(sat(21))]; + let results = + [NumOpResult::Valid(sat(42)), NumOpResult::Valid(sat(1337)), NumOpResult::Valid(sat(21))]; assert_eq!(results.iter().sum::>(), NumOpResult::Valid(sat(1400))); - let signed_results = [NumOpResult::Valid(ssat(42)), NumOpResult::Valid(ssat(1337)), NumOpResult::Valid(ssat(21))]; - assert_eq!(signed_results.iter().sum::>(), NumOpResult::Valid(ssat(1400))); + let signed_results = [ + NumOpResult::Valid(ssat(42)), + NumOpResult::Valid(ssat(1337)), + NumOpResult::Valid(ssat(21)), + ]; + assert_eq!( + signed_results.iter().sum::>(), + NumOpResult::Valid(ssat(1400)) + ); let amounts = [sat(42), sat(1337), sat(21)]; assert_eq!( @@ -1240,9 +1229,7 @@ fn unsigned_amount_div_by_amount() { #[test] #[should_panic(expected = "attempt to divide by zero")] -fn unsigned_amount_div_by_amount_zero() { - let _ = sat(1897) / Amount::ZERO; -} +fn unsigned_amount_div_by_amount_zero() { let _ = sat(1897) / Amount::ZERO; } #[test] fn signed_amount_div_by_amount() { @@ -1256,9 +1243,7 @@ fn signed_amount_div_by_amount() { #[test] #[should_panic(expected = "attempt to divide by zero")] -fn signed_amount_div_by_amount_zero() { - let _ = ssat(1897) / SignedAmount::ZERO; -} +fn signed_amount_div_by_amount_zero() { let _ = ssat(1897) / SignedAmount::ZERO; } #[test] fn check_const() { diff --git a/units/tests/api.rs b/units/tests/api.rs index b8ec96c3c..e3b89fed0 100644 --- a/units/tests/api.rs +++ b/units/tests/api.rs @@ -14,8 +14,8 @@ use arbitrary::{Arbitrary, Unstructured}; // These imports test "typical" usage by user code. use bitcoin_units::locktime::{absolute, relative}; // Typical usage is `absolute::Height`. use bitcoin_units::{ - amount, block, fee_rate, locktime, parse, weight, Amount, BlockHeight, BlockInterval, FeeRate, - SignedAmount, BlockTime, Weight, + amount, block, fee_rate, locktime, parse, weight, Amount, BlockHeight, BlockInterval, + BlockTime, FeeRate, SignedAmount, Weight, }; /// A struct that includes all public non-error enums. @@ -147,7 +147,7 @@ fn api_can_use_modules_from_crate_root() { #[test] fn api_can_use_types_from_crate_root() { use bitcoin_units::{ - Amount, BlockHeight, BlockInterval, FeeRate, SignedAmount, BlockTime, Weight, + Amount, BlockHeight, BlockInterval, BlockTime, FeeRate, SignedAmount, Weight, }; }