From 2c40b4f4ec7d71c796506adb0d2069e49a76c40a Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Thu, 10 Oct 2024 10:39:36 +1100 Subject: [PATCH 1/3] Configure formmater to skip read_compact_size --- bitcoin/src/consensus/encode.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/bitcoin/src/consensus/encode.rs b/bitcoin/src/consensus/encode.rs index feeea2248..7beb9d457 100644 --- a/bitcoin/src/consensus/encode.rs +++ b/bitcoin/src/consensus/encode.rs @@ -323,6 +323,7 @@ impl ReadExt for R { self.read_exact(slice).map_err(Error::Io) } #[inline] + #[rustfmt::skip] // Formatter munges code comments below. fn read_compact_size(&mut self) -> Result { match self.read_u8()? { 0xFF => { From 2e4179ed0ff0680ab7f9807c98246f233c1f6370 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Thu, 10 Oct 2024 10:39:53 +1100 Subject: [PATCH 2/3] Run the formatter Run `just fmt`, no other manual changes. --- bitcoin/examples/taproot-psbt.rs | 16 +++++++++++----- bitcoin/src/bip152.rs | 2 +- bitcoin/src/bip32.rs | 7 ++----- bitcoin/src/blockdata/transaction.rs | 15 +++++++++------ bitcoin/src/blockdata/witness.rs | 14 +++++--------- bitcoin/src/consensus/encode.rs | 16 ++++------------ bitcoin/src/merkle_tree/block.rs | 2 +- bitcoin/src/p2p/message.rs | 2 +- internals/src/compact_size.rs | 4 +--- primitives/src/lib.rs | 2 +- primitives/src/transaction.rs | 5 +---- 11 files changed, 37 insertions(+), 48 deletions(-) diff --git a/bitcoin/examples/taproot-psbt.rs b/bitcoin/examples/taproot-psbt.rs index 7c72fcaba..677e7ad91 100644 --- a/bitcoin/examples/taproot-psbt.rs +++ b/bitcoin/examples/taproot-psbt.rs @@ -297,7 +297,8 @@ fn generate_bip86_key_spend_tx( .get(&input.tap_internal_key.ok_or("internal key missing in PSBT")?) .ok_or("missing Taproot key origin")?; - let secret_key = master_xpriv.derive_xpriv(secp, &derivation_path).to_private_key().inner; + let secret_key = + master_xpriv.derive_xpriv(secp, &derivation_path).to_private_key().inner; sign_psbt_taproot( secret_key, input.tap_internal_key.unwrap(), @@ -484,8 +485,10 @@ impl BenefactorWallet { .master_xpriv .derive_xpriv(&self.secp, &new_derivation_path) .to_keypair(&self.secp); - let beneficiary_key = - self.beneficiary_xpub.derive_xpub(&self.secp, &new_derivation_path)?.to_x_only_public_key(); + let beneficiary_key = self + .beneficiary_xpub + .derive_xpub(&self.secp, &new_derivation_path)? + .to_x_only_public_key(); // Build up the leaf script and combine with internal key into a Taproot commitment let lock_time = absolute::LockTime::from_height( @@ -530,8 +533,11 @@ impl BenefactorWallet { .tap_key_origins .get(&input.tap_internal_key.ok_or("internal key missing in PSBT")?) .ok_or("missing Taproot key origin")?; - let secret_key = - self.master_xpriv.derive_xpriv(&self.secp, &derivation_path).to_private_key().inner; + let secret_key = self + .master_xpriv + .derive_xpriv(&self.secp, &derivation_path) + .to_private_key() + .inner; sign_psbt_taproot( secret_key, spend_info.internal_key(), diff --git a/bitcoin/src/bip152.rs b/bitcoin/src/bip152.rs index ff3e59c14..028724f78 100644 --- a/bitcoin/src/bip152.rs +++ b/bitcoin/src/bip152.rs @@ -12,7 +12,7 @@ use hashes::{sha256, siphash24}; use internals::{impl_array_newtype, ToU64 as _}; use io::{BufRead, Write}; -use crate::consensus::encode::{self, Decodable, Encodable, WriteExt, ReadExt}; +use crate::consensus::encode::{self, Decodable, Encodable, ReadExt, WriteExt}; use crate::internal_macros::{impl_array_newtype_stringify, impl_consensus_encoding}; use crate::prelude::Vec; use crate::transaction::TxIdentifier; diff --git a/bitcoin/src/bip32.rs b/bitcoin/src/bip32.rs index 1b15a1022..fbf02b464 100644 --- a/bitcoin/src/bip32.rs +++ b/bitcoin/src/bip32.rs @@ -593,9 +593,7 @@ impl Xpriv { /// Constructs ECDSA compressed private key matching internal secret key representation. #[deprecated(since = "TBD", note = "use `to_private_key()`")] - pub fn to_priv(self) -> PrivateKey { - self.to_private_key() - } + pub fn to_priv(self) -> PrivateKey { self.to_private_key() } /// Constructs ECDSA compressed private key matching internal secret key representation. pub fn to_private_key(self) -> PrivateKey { @@ -603,7 +601,7 @@ impl Xpriv { } /// Creates new extended public key from this extended private key. - pub fn to_xpub(&self, secp: &Secp256k1,) -> Xpub { + pub fn to_xpub(&self, secp: &Secp256k1) -> Xpub { Xpub::from_xpriv(secp, self) } @@ -775,7 +773,6 @@ impl Xpub { secp: &Secp256k1, path: &P, ) -> Result { - self.derive_xpub(secp, path) } diff --git a/bitcoin/src/blockdata/transaction.rs b/bitcoin/src/blockdata/transaction.rs index 8fc677957..b0c2a5a67 100644 --- a/bitcoin/src/blockdata/transaction.rs +++ b/bitcoin/src/blockdata/transaction.rs @@ -88,7 +88,6 @@ crate::internal_macros::define_extension_trait! { } } - /// Bitcoin transaction input. /// /// It contains the location of the previous transaction's output, @@ -1267,13 +1266,16 @@ impl InputWeightPrediction { T: IntoIterator, T::Item: Borrow, { - let (count, total_size) = - witness_element_lengths.into_iter().fold((0usize, 0), |(count, total_size), elem_len| { + let (count, total_size) = witness_element_lengths.into_iter().fold( + (0usize, 0), + |(count, total_size), elem_len| { let elem_len = *elem_len.borrow(); let elem_size = elem_len + compact_size::encoded_size(elem_len); (count + 1, total_size + elem_size) - }); - let witness_size = if count > 0 { total_size + compact_size::encoded_size(count) } else { 0 }; + }, + ); + let witness_size = + if count > 0 { total_size + compact_size::encoded_size(count) } else { 0 }; let script_size = input_script_len + compact_size::encoded_size(input_script_len); InputWeightPrediction { script_size, witness_size } @@ -1299,7 +1301,8 @@ impl InputWeightPrediction { } else { 0 }; - let script_size = input_script_len + compact_size::encoded_size_const(input_script_len as u64); + let script_size = + input_script_len + compact_size::encoded_size_const(input_script_len as u64); InputWeightPrediction { script_size, witness_size } } diff --git a/bitcoin/src/blockdata/witness.rs b/bitcoin/src/blockdata/witness.rs index d51fd14c4..61cd757b3 100644 --- a/bitcoin/src/blockdata/witness.rs +++ b/bitcoin/src/blockdata/witness.rs @@ -12,7 +12,7 @@ use arbitrary::{Arbitrary, Unstructured}; use internals::compact_size; use io::{BufRead, Write}; -use crate::consensus::encode::{self, Error, MAX_VEC_SIZE, ReadExt, WriteExt}; +use crate::consensus::encode::{self, Error, ReadExt, WriteExt, MAX_VEC_SIZE}; use crate::consensus::{Decodable, Encodable}; use crate::crypto::ecdsa; use crate::prelude::Vec; @@ -185,7 +185,8 @@ impl Decodable for Witness { encode_cursor(&mut content, 0, i, cursor - witness_index_space); resize_if_needed(&mut content, required_len); - cursor += (&mut content[cursor..cursor + element_size_len]).emit_compact_size(element_size)?; + cursor += (&mut content[cursor..cursor + element_size_len]) + .emit_compact_size(element_size)?; r.read_exact(&mut content[cursor..cursor + element_size])?; cursor += element_size; } @@ -629,7 +630,7 @@ mod test { use hex::test_hex_unwrap as hex; use super::*; - use crate::consensus::{encode, deserialize, serialize}; + use crate::consensus::{deserialize, encode, serialize}; use crate::hex::DisplayHex; use crate::sighash::EcdsaSighashType; use crate::Transaction; @@ -649,11 +650,7 @@ mod test { // The last four bytes represent start at index 0. let content = [0_u8; 5]; - Witness { - witness_elements: 1, - content: content.to_vec(), - indices_start: 1, - } + Witness { witness_elements: 1, content: content.to_vec(), indices_start: 1 } } #[test] @@ -960,7 +957,6 @@ mod test { let ser = serde_json::to_string(&original).unwrap(); let rinsed: Witness = serde_json::from_str(&ser).unwrap(); assert_eq!(rinsed, original); - } #[test] diff --git a/bitcoin/src/consensus/encode.rs b/bitcoin/src/consensus/encode.rs index 7beb9d457..7a952c718 100644 --- a/bitcoin/src/consensus/encode.rs +++ b/bitcoin/src/consensus/encode.rs @@ -323,7 +323,7 @@ impl ReadExt for R { self.read_exact(slice).map_err(Error::Io) } #[inline] - #[rustfmt::skip] // Formatter munges code comments below. + #[rustfmt::skip] // Formatter munges code comments below. fn read_compact_size(&mut self) -> Result { match self.read_u8()? { 0xFF => { @@ -493,9 +493,7 @@ pub const fn varint_size_u64(v: u64) -> usize { /// Returns 1 for 0..=0xFC, 3 for 0xFD..=(2^16-1), 5 for 0x10000..=(2^32-1), and 9 otherwise. #[inline] -pub fn varint_size(v: impl ToU64) -> usize { - varint_size_u64(v.to_u64()) -} +pub fn varint_size(v: impl ToU64) -> usize { varint_size_u64(v.to_u64()) } impl Encodable for bool { #[inline] @@ -935,14 +933,8 @@ mod tests { assert_eq!(encode(0xFD), [0xFDu8, 0xFD, 0]); assert_eq!(encode(0xFFF), [0xFDu8, 0xFF, 0xF]); assert_eq!(encode(0xF0F0F0F), [0xFEu8, 0xF, 0xF, 0xF, 0xF]); - assert_eq!( - encode(0xF0F0F0F0F0E0), - vec![0xFFu8, 0xE0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0, 0], - ); - assert_eq!( - test_varint_encode(0xFF, &0x100000000_u64.to_le_bytes()).unwrap(), - 0x100000000, - ); + assert_eq!(encode(0xF0F0F0F0F0E0), vec![0xFFu8, 0xE0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0, 0],); + assert_eq!(test_varint_encode(0xFF, &0x100000000_u64.to_le_bytes()).unwrap(), 0x100000000,); assert_eq!(test_varint_encode(0xFE, &0x10000_u64.to_le_bytes()).unwrap(), 0x10000); assert_eq!(test_varint_encode(0xFD, &0xFD_u64.to_le_bytes()).unwrap(), 0xFD); diff --git a/bitcoin/src/merkle_tree/block.rs b/bitcoin/src/merkle_tree/block.rs index c54b7f299..4147d84a3 100644 --- a/bitcoin/src/merkle_tree/block.rs +++ b/bitcoin/src/merkle_tree/block.rs @@ -16,7 +16,7 @@ use io::{BufRead, Write}; use self::MerkleBlockError::*; use crate::block::{self, Block}; -use crate::consensus::encode::{self, Decodable, Encodable, MAX_VEC_SIZE, WriteExt, ReadExt}; +use crate::consensus::encode::{self, Decodable, Encodable, ReadExt, WriteExt, MAX_VEC_SIZE}; use crate::merkle_tree::{MerkleNode as _, TxMerkleNode}; use crate::prelude::Vec; use crate::transaction::{Transaction, Txid}; diff --git a/bitcoin/src/p2p/message.rs b/bitcoin/src/p2p/message.rs index 3dccfb85c..76fd1f75b 100644 --- a/bitcoin/src/p2p/message.rs +++ b/bitcoin/src/p2p/message.rs @@ -11,7 +11,7 @@ use hashes::sha256d; use internals::ToU64 as _; use io::{BufRead, Write}; -use crate::consensus::encode::{self, CheckedData, Decodable, Encodable, WriteExt, ReadExt}; +use crate::consensus::encode::{self, CheckedData, Decodable, Encodable, ReadExt, WriteExt}; use crate::merkle_tree::MerkleBlock; use crate::p2p::address::{AddrV2Message, Address}; use crate::p2p::{ diff --git a/internals/src/compact_size.rs b/internals/src/compact_size.rs index 0e8a0daa2..a343f3c16 100644 --- a/internals/src/compact_size.rs +++ b/internals/src/compact_size.rs @@ -29,9 +29,7 @@ const MAX_ENCODING_SIZE: usize = 9; /// - 5 for 0x10000..=(2^32-1) /// - 9 otherwise. #[inline] -pub fn encoded_size(value: impl ToU64) -> usize { - encoded_size_const(value.to_u64()) -} +pub fn encoded_size(value: impl ToU64) -> usize { encoded_size_const(value.to_u64()) } /// Returns the number of bytes used to encode this `CompactSize` value (in const context). /// diff --git a/primitives/src/lib.rs b/primitives/src/lib.rs index e649bdf4e..5fedfd5d9 100644 --- a/primitives/src/lib.rs +++ b/primitives/src/lib.rs @@ -32,8 +32,8 @@ extern crate serde; pub mod block; #[cfg(feature = "alloc")] pub mod locktime; -pub mod opcodes; pub mod merkle_tree; +pub mod opcodes; pub mod pow; pub mod sequence; pub mod transaction; diff --git a/primitives/src/transaction.rs b/primitives/src/transaction.rs index ee54ac297..576729996 100644 --- a/primitives/src/transaction.rs +++ b/primitives/src/transaction.rs @@ -197,10 +197,7 @@ impl fmt::Display for Version { #[cfg(feature = "arbitrary")] impl<'a> Arbitrary<'a> for OutPoint { fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result { - Ok(OutPoint{ - txid: Txid::arbitrary(u)?, - vout: u32::arbitrary(u)? - }) + Ok(OutPoint { txid: Txid::arbitrary(u)?, vout: u32::arbitrary(u)? }) } } From 323e70611358c7db5b7e7be0c064df69725fafc1 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Thu, 10 Oct 2024 10:41:16 +1100 Subject: [PATCH 3/3] Add rustfmt config option style_edition rustfmt is emitting: Warning: the version option is deprecated. Use style_edition instead. As suggested add a config option and set it to 2021. --- rustfmt.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rustfmt.toml b/rustfmt.toml index c8a2256ec..08c331b51 100644 --- a/rustfmt.toml +++ b/rustfmt.toml @@ -58,7 +58,7 @@ match_block_trailing_comma = false blank_lines_upper_bound = 1 blank_lines_lower_bound = 0 edition = "2021" -version = "One" +style_edition = "2021" inline_attribute_width = 0 format_generated_files = true merge_derives = true