2023-12-17 automated rustfmt nightly

This commit is contained in:
Fmt Bot 2023-12-17 00:59:05 +00:00 committed by GitHub
parent eadc6a6c9e
commit 5af7727250
16 changed files with 97 additions and 148 deletions

View File

@ -8,7 +8,7 @@ use bitcoin::bip32::{ChildNumber, DerivationPath, Xpriv, Xpub};
use bitcoin::hex::FromHex;
use bitcoin::secp256k1::ffi::types::AlignedType;
use bitcoin::secp256k1::Secp256k1;
use bitcoin::{Network, NetworkKind, CompressedPublicKey};
use bitcoin::{CompressedPublicKey, Network, NetworkKind};
fn main() {
// This example derives root xprv from a 32-byte seed,

View File

@ -39,8 +39,8 @@ use bitcoin::locktime::absolute;
use bitcoin::psbt::{self, Input, Psbt, PsbtSighashType};
use bitcoin::secp256k1::{Secp256k1, Signing, Verification};
use bitcoin::{
transaction, Address, Amount, Network, OutPoint, CompressedPublicKey, ScriptBuf, Sequence, Transaction,
TxIn, TxOut, Witness,
transaction, Address, Amount, CompressedPublicKey, Network, OutPoint, ScriptBuf, Sequence,
Transaction, TxIn, TxOut, Witness,
};
type Result<T> = std::result::Result<T, Error>;

View File

@ -1,5 +1,7 @@
use bitcoin::hashes::Hash;
use bitcoin::{consensus, ecdsa, sighash, Amount, CompressedPublicKey, Script, ScriptBuf, Transaction};
use bitcoin::{
consensus, ecdsa, sighash, Amount, CompressedPublicKey, Script, ScriptBuf, Transaction,
};
use hex_lit::hex;
//These are real blockchain transactions examples of computing sighash for:

View File

@ -45,7 +45,9 @@ use crate::blockdata::constants::{
use crate::blockdata::script::witness_program::WitnessProgram;
use crate::blockdata::script::witness_version::WitnessVersion;
use crate::blockdata::script::{self, PushBytesBuf, Script, ScriptBuf, ScriptHash};
use crate::crypto::key::{PubkeyHash, PublicKey, CompressedPublicKey, TweakedPublicKey, UntweakedPublicKey};
use crate::crypto::key::{
CompressedPublicKey, PubkeyHash, PublicKey, TweakedPublicKey, UntweakedPublicKey,
};
use crate::network::{Network, NetworkKind};
use crate::prelude::*;
use crate::taproot::TapNodeHash;
@ -390,10 +392,7 @@ impl Address {
/// Creates a witness pay to public key address from a public key.
///
/// This is the native segwit address type for an output redeemable with a single signature.
pub fn p2wpkh(
pk: &CompressedPublicKey,
network: Network,
) -> Self {
pub fn p2wpkh(pk: &CompressedPublicKey, network: Network) -> Self {
let program = WitnessProgram::p2wpkh(pk);
Address::from_witness_program(program, network)
}
@ -401,10 +400,7 @@ impl Address {
/// Creates a pay to script address that embeds a witness pay to public key.
///
/// This is a segwit address type that looks familiar (as p2sh) to legacy clients.
pub fn p2shwpkh(
pk: &CompressedPublicKey,
network: impl Into<NetworkKind>,
) -> Address {
pub fn p2shwpkh(pk: &CompressedPublicKey, network: impl Into<NetworkKind>) -> Address {
let builder = script::Builder::new().push_int(0).push_slice(pk.wpubkey_hash());
let script_hash = builder.as_script().script_hash();
Address::p2sh_from_hash(script_hash, network)

View File

@ -19,7 +19,7 @@ use secp256k1::{self, Secp256k1, XOnlyPublicKey};
use serde;
use crate::base58;
use crate::crypto::key::{self, Keypair, PrivateKey, CompressedPublicKey};
use crate::crypto::key::{self, CompressedPublicKey, Keypair, PrivateKey};
use crate::internal_macros::impl_bytes_newtype;
use crate::network::NetworkKind;
use crate::prelude::*;

View File

@ -420,22 +420,22 @@ impl Script {
fn minimal_non_dust_inner(&self, dust_relay_fee: u64) -> crate::Amount {
// This must never be lower than Bitcoin Core's GetDustThreshold() (as of v0.21) as it may
// otherwise allow users to create transactions which likely can never be broadcast/confirmed.
let sats = dust_relay_fee.checked_mul(
if self.is_op_return() {
0
} else if self.is_witness_program() {
32 + 4 + 1 + (107 / 4) + 4 + // The spend cost copied from Core
let sats = dust_relay_fee
.checked_mul(if self.is_op_return() {
0
} else if self.is_witness_program() {
32 + 4 + 1 + (107 / 4) + 4 + // The spend cost copied from Core
8 + // The serialized size of the TxOut's amount field
self.consensus_encode(&mut sink()).expect("sinks don't error") as u64 // The serialized size of this script_pubkey
} else {
32 + 4 + 1 + 107 + 4 + // The spend cost copied from Core
} else {
32 + 4 + 1 + 107 + 4 + // The spend cost copied from Core
8 + // The serialized size of the TxOut's amount field
self.consensus_encode(&mut sink()).expect("sinks don't error") as u64 // The serialized size of this script_pubkey
}
).expect("dust_relay_fee or script length should not be absurdly large") /
1000; // divide by 1000 like in Core to get value as it cancels out DEFAULT_MIN_RELAY_TX_FEE
// Note: We ensure the division happens at the end, since Core performs the division at the end.
// This will make sure none of the implicit floor operations mess with the value.
})
.expect("dust_relay_fee or script length should not be absurdly large")
/ 1000; // divide by 1000 like in Core to get value as it cancels out DEFAULT_MIN_RELAY_TX_FEE
// Note: We ensure the division happens at the end, since Core performs the division at the end.
// This will make sure none of the implicit floor operations mess with the value.
crate::Amount::from_sat(sats)
}

View File

@ -6,10 +6,10 @@ use hashes::Hash;
use hex_lit::hex;
use super::*;
use crate::FeeRate;
use crate::blockdata::opcodes;
use crate::consensus::encode::{deserialize, serialize};
use crate::crypto::key::{PubkeyHash, PublicKey, WPubkeyHash, XOnlyPublicKey};
use crate::FeeRate;
#[test]
#[rustfmt::skip]
@ -651,7 +651,10 @@ fn defult_dust_value_tests() {
let script_p2wpkh = Builder::new().push_int(0).push_slice([42; 20]).into_script();
assert!(script_p2wpkh.is_p2wpkh());
assert_eq!(script_p2wpkh.minimal_non_dust(), crate::Amount::from_sat(294));
assert_eq!(script_p2wpkh.minimal_non_dust_custom(FeeRate::from_sat_per_vb_unchecked(6)), crate::Amount::from_sat(588));
assert_eq!(
script_p2wpkh.minimal_non_dust_custom(FeeRate::from_sat_per_vb_unchecked(6)),
crate::Amount::from_sat(588)
);
let script_p2pkh = Builder::new()
.push_opcode(OP_DUP)
@ -662,7 +665,10 @@ fn defult_dust_value_tests() {
.into_script();
assert!(script_p2pkh.is_p2pkh());
assert_eq!(script_p2pkh.minimal_non_dust(), crate::Amount::from_sat(546));
assert_eq!(script_p2pkh.minimal_non_dust_custom(FeeRate::from_sat_per_vb_unchecked(6)), crate::Amount::from_sat(1092));
assert_eq!(
script_p2pkh.minimal_non_dust_custom(FeeRate::from_sat_per_vb_unchecked(6)),
crate::Amount::from_sat(1092)
);
}
#[test]

View File

@ -557,10 +557,7 @@ impl TxOut {
///
/// [`minimal_non_dust_custom`]: TxOut::minimal_non_dust_custom
pub fn minimal_non_dust(script_pubkey: ScriptBuf) -> Self {
TxOut {
value: script_pubkey.minimal_non_dust(),
script_pubkey,
}
TxOut { value: script_pubkey.minimal_non_dust(), script_pubkey }
}
/// Creates a `TxOut` with given script and the smallest possible `value` that is **not** dust
@ -575,10 +572,7 @@ impl TxOut {
///
/// [`minimal_non_dust`]: TxOut::minimal_non_dust
pub fn minimal_non_dust_custom(script_pubkey: ScriptBuf, dust_relay_fee: FeeRate) -> Self {
TxOut {
value: script_pubkey.minimal_non_dust_custom(dust_relay_fee),
script_pubkey,
}
TxOut { value: script_pubkey.minimal_non_dust_custom(dust_relay_fee), script_pubkey }
}
}

View File

@ -57,7 +57,8 @@ pub fn verify_script_with_flags<F: Into<u32>>(
spending_tx,
index,
flags.into(),
).map_err(BitcoinconsensusError)
)
.map_err(BitcoinconsensusError)
}
/// Verifies that this transaction is able to spend its inputs.

View File

@ -11,9 +11,9 @@ use core::str::FromStr;
use hashes::{hash160, Hash};
use hex::FromHex;
use internals::array_vec::ArrayVec;
use internals::write_err;
use io::{Read, Write};
use internals::array_vec::ArrayVec;
use crate::crypto::ecdsa;
use crate::internal_macros::impl_asref_push_bytes;
@ -299,9 +299,7 @@ impl CompressedPublicKey {
///
/// Note that this can be used as a sort key to get BIP67-compliant sorting.
/// That's why this type doesn't have the `to_sort_key` method - it would duplicate this one.
pub fn to_bytes(&self) -> [u8; 33] {
self.0.serialize()
}
pub fn to_bytes(&self) -> [u8; 33] { self.0.serialize() }
/// Deserialize a public key from a slice
pub fn from_slice(data: &[u8]) -> Result<Self, secp256k1::Error> {
@ -354,9 +352,7 @@ impl TryFrom<PublicKey> for CompressedPublicKey {
}
impl From<CompressedPublicKey> for PublicKey {
fn from(value: CompressedPublicKey) -> Self {
PublicKey::new(value.0)
}
fn from(value: CompressedPublicKey) -> Self { PublicKey::new(value.0) }
}
impl From<CompressedPublicKey> for XOnlyPublicKey {
@ -379,7 +375,6 @@ impl From<&CompressedPublicKey> for WPubkeyHash {
fn from(key: &CompressedPublicKey) -> Self { key.wpubkey_hash() }
}
/// A Bitcoin ECDSA private key
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct PrivateKey {
@ -1284,7 +1279,8 @@ mod tests {
fn private_key_debug_is_obfuscated() {
let sk =
PrivateKey::from_str("cVt4o7BGAig1UXywgGSmARhxMdzP5qvQsxKkSsc1XEkw3tDTQFpy").unwrap();
let want = "PrivateKey { compressed: true, network: Test, inner: SecretKey(#32014e414fdce702) }";
let want =
"PrivateKey { compressed: true, network: Test, inner: SecretKey(#32014e414fdce702) }";
let got = format!("{:?}", sk);
assert_eq!(got, want)
}

View File

@ -238,7 +238,8 @@ mod tests {
assert_eq!(signature.to_base64(), signature.to_string());
let signature2 = super::MessageSignature::from_str(&signature.to_string()).unwrap();
let pubkey = signature2.recover_pubkey(&secp, msg_hash)
let pubkey = signature2
.recover_pubkey(&secp, msg_hash)
.unwrap()
.try_into()
.expect("compressed was set to true");

View File

@ -6,9 +6,13 @@ use alloc::boxed::Box;
use alloc::vec::Vec;
use core::borrow::{Borrow, BorrowMut};
use super::{TapNodeHash, TaprootError, TaprootBuilderError, TAPROOT_CONTROL_NODE_SIZE, TAPROOT_CONTROL_MAX_NODE_COUNT};
use hashes::Hash;
use super::{
TapNodeHash, TaprootBuilderError, TaprootError, TAPROOT_CONTROL_MAX_NODE_COUNT,
TAPROOT_CONTROL_NODE_SIZE,
};
/// The merkle proof for inclusion of a tree in a taptree hash.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
@ -169,9 +173,7 @@ impl IntoIterator for TaprootMerkleBranch {
type Item = TapNodeHash;
#[inline]
fn into_iter(self) -> Self::IntoIter {
IntoIter(self.0.into_iter())
}
fn into_iter(self) -> Self::IntoIter { IntoIter(self.0.into_iter()) }
}
impl<'a> IntoIterator for &'a TaprootMerkleBranch {
@ -179,9 +181,7 @@ impl<'a> IntoIterator for &'a TaprootMerkleBranch {
type Item = &'a TapNodeHash;
#[inline]
fn into_iter(self) -> Self::IntoIter {
self.0.iter()
}
fn into_iter(self) -> Self::IntoIter { self.0.iter() }
}
impl<'a> IntoIterator for &'a mut TaprootMerkleBranch {
@ -189,53 +189,39 @@ impl<'a> IntoIterator for &'a mut TaprootMerkleBranch {
type Item = &'a mut TapNodeHash;
#[inline]
fn into_iter(self) -> Self::IntoIter {
self.0.iter_mut()
}
fn into_iter(self) -> Self::IntoIter { self.0.iter_mut() }
}
impl core::ops::Deref for TaprootMerkleBranch {
type Target = [TapNodeHash];
#[inline]
fn deref(&self) -> &Self::Target {
&self.0
}
fn deref(&self) -> &Self::Target { &self.0 }
}
impl core::ops::DerefMut for TaprootMerkleBranch {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 }
}
impl AsRef<[TapNodeHash]> for TaprootMerkleBranch {
#[inline]
fn as_ref(&self) -> &[TapNodeHash] {
&self.0
}
fn as_ref(&self) -> &[TapNodeHash] { &self.0 }
}
impl AsMut<[TapNodeHash]> for TaprootMerkleBranch {
#[inline]
fn as_mut(&mut self) -> &mut [TapNodeHash] {
&mut self.0
}
fn as_mut(&mut self) -> &mut [TapNodeHash] { &mut self.0 }
}
impl Borrow<[TapNodeHash]> for TaprootMerkleBranch {
#[inline]
fn borrow(&self) -> &[TapNodeHash] {
&self.0
}
fn borrow(&self) -> &[TapNodeHash] { &self.0 }
}
impl BorrowMut<[TapNodeHash]> for TaprootMerkleBranch {
#[inline]
fn borrow_mut(&mut self) -> &mut [TapNodeHash] {
&mut self.0
}
fn borrow_mut(&mut self) -> &mut [TapNodeHash] { &mut self.0 }
}
/// Iterator over node hashes within Taproot merkle branch.
@ -247,56 +233,38 @@ pub struct IntoIter(alloc::vec::IntoIter<TapNodeHash>);
impl IntoIter {
/// Returns the remaining items of this iterator as a slice.
#[inline]
pub fn as_slice(&self) -> &[TapNodeHash] {
self.0.as_slice()
}
pub fn as_slice(&self) -> &[TapNodeHash] { self.0.as_slice() }
/// Returns the remaining items of this iterator as a mutable slice.
#[inline]
pub fn as_mut_slice(&mut self) -> &mut [TapNodeHash] {
self.0.as_mut_slice()
}
pub fn as_mut_slice(&mut self) -> &mut [TapNodeHash] { self.0.as_mut_slice() }
}
impl Iterator for IntoIter {
type Item = TapNodeHash;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.0.next()
}
fn next(&mut self) -> Option<Self::Item> { self.0.next() }
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.0.size_hint()
}
fn size_hint(&self) -> (usize, Option<usize>) { self.0.size_hint() }
#[inline]
fn nth(&mut self, n: usize) -> Option<Self::Item> {
self.0.nth(n)
}
fn nth(&mut self, n: usize) -> Option<Self::Item> { self.0.nth(n) }
#[inline]
fn last(self) -> Option<Self::Item> {
self.0.last()
}
fn last(self) -> Option<Self::Item> { self.0.last() }
#[inline]
fn count(self) -> usize {
self.0.count()
}
fn count(self) -> usize { self.0.count() }
}
impl DoubleEndedIterator for IntoIter {
#[inline]
fn next_back(&mut self) -> Option<Self::Item> {
self.0.next_back()
}
fn next_back(&mut self) -> Option<Self::Item> { self.0.next_back() }
#[inline]
fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
self.0.nth_back(n)
}
fn nth_back(&mut self, n: usize) -> Option<Self::Item> { self.0.nth_back(n) }
}
impl ExactSizeIterator for IntoIter {}

View File

@ -5,8 +5,8 @@
//! This module provides support for taproot tagged hashes.
//!
pub mod serialized_signature;
pub mod merkle_branch;
pub mod serialized_signature;
use core::cmp::Reverse;
use core::fmt;

View File

@ -11,6 +11,7 @@ pub use safety_boundary::ArrayVec;
// inside it!
mod safety_boundary {
use core::mem::MaybeUninit;
use crate::const_tools::cond_const;
/// A growable contiguous collection backed by array.
@ -100,83 +101,67 @@ mod safety_boundary {
/// memcpy.
#[allow(clippy::non_canonical_clone_impl)]
impl<T: Copy, const CAP: usize> Clone for ArrayVec<T, CAP> {
fn clone(&self) -> Self {
Self::from_slice(self)
}
fn clone(&self) -> Self { Self::from_slice(self) }
}
impl<T: Copy, const CAP: usize> core::ops::Deref for ArrayVec<T, CAP> {
type Target = [T];
fn deref(&self) -> &Self::Target {
self.as_slice()
}
fn deref(&self) -> &Self::Target { self.as_slice() }
}
impl<T: Copy, const CAP: usize> core::ops::DerefMut for ArrayVec<T, CAP> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.as_mut_slice()
}
fn deref_mut(&mut self) -> &mut Self::Target { self.as_mut_slice() }
}
impl<T: Copy + Eq, const CAP: usize> Eq for ArrayVec<T, CAP> {}
impl<T: Copy + PartialEq, const CAP1: usize, const CAP2: usize> PartialEq<ArrayVec<T, CAP2>> for ArrayVec<T, CAP1> {
fn eq(&self, other: &ArrayVec<T, CAP2>) -> bool {
**self == **other
}
impl<T: Copy + PartialEq, const CAP1: usize, const CAP2: usize> PartialEq<ArrayVec<T, CAP2>>
for ArrayVec<T, CAP1>
{
fn eq(&self, other: &ArrayVec<T, CAP2>) -> bool { **self == **other }
}
impl<T: Copy + PartialEq, const CAP: usize> PartialEq<[T]> for ArrayVec<T, CAP> {
fn eq(&self, other: &[T]) -> bool {
**self == *other
}
fn eq(&self, other: &[T]) -> bool { **self == *other }
}
impl<T: Copy + PartialEq, const CAP: usize> PartialEq<ArrayVec<T, CAP>> for [T] {
fn eq(&self, other: &ArrayVec<T, CAP>) -> bool {
*self == **other
}
fn eq(&self, other: &ArrayVec<T, CAP>) -> bool { *self == **other }
}
impl<T: Copy + PartialEq, const CAP: usize, const LEN: usize> PartialEq<[T; LEN]> for ArrayVec<T, CAP> {
fn eq(&self, other: &[T; LEN]) -> bool {
**self == *other
}
impl<T: Copy + PartialEq, const CAP: usize, const LEN: usize> PartialEq<[T; LEN]>
for ArrayVec<T, CAP>
{
fn eq(&self, other: &[T; LEN]) -> bool { **self == *other }
}
impl<T: Copy + PartialEq, const CAP: usize, const LEN: usize> PartialEq<ArrayVec<T, CAP>> for [T; LEN] {
fn eq(&self, other: &ArrayVec<T, CAP>) -> bool {
*self == **other
}
impl<T: Copy + PartialEq, const CAP: usize, const LEN: usize> PartialEq<ArrayVec<T, CAP>>
for [T; LEN]
{
fn eq(&self, other: &ArrayVec<T, CAP>) -> bool { *self == **other }
}
impl<T: Copy + Ord, const CAP: usize> Ord for ArrayVec<T, CAP> {
fn cmp(&self, other: &ArrayVec<T, CAP>) -> core::cmp::Ordering {
(**self).cmp(&**other)
}
fn cmp(&self, other: &ArrayVec<T, CAP>) -> core::cmp::Ordering { (**self).cmp(&**other) }
}
impl<T: Copy + PartialOrd, const CAP1: usize, const CAP2: usize> PartialOrd<ArrayVec<T, CAP2>> for ArrayVec<T, CAP1> {
impl<T: Copy + PartialOrd, const CAP1: usize, const CAP2: usize> PartialOrd<ArrayVec<T, CAP2>>
for ArrayVec<T, CAP1>
{
fn partial_cmp(&self, other: &ArrayVec<T, CAP2>) -> Option<core::cmp::Ordering> {
(**self).partial_cmp(&**other)
}
}
impl<T: Copy + fmt::Debug, const CAP: usize> fmt::Debug for ArrayVec<T, CAP> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(&**self, f)
}
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Debug::fmt(&**self, f) }
}
impl<T: Copy + core::hash::Hash, const CAP: usize> core::hash::Hash for ArrayVec<T, CAP> {
fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
core::hash::Hash::hash(&**self, state)
}
fn hash<H: core::hash::Hasher>(&self, state: &mut H) { core::hash::Hash::hash(&**self, state) }
}
#[cfg(test)]
mod tests {
use super::ArrayVec;

View File

@ -23,8 +23,8 @@ extern crate alloc;
mod error;
mod macros;
use core::convert::TryInto;
use core::cmp;
use core::convert::TryInto;
#[rustfmt::skip] // Keep public re-exports separate.
pub use self::error::{Error, ErrorKind};

View File

@ -940,11 +940,11 @@ impl SignedAmount {
pub fn from_str_in(s: &str, denom: Denomination) -> Result<SignedAmount, ParseAmountError> {
match parse_signed_to_satoshi(s, denom)? {
// (negative, amount)
(false, sat) if sat > i64::MAX as u64 => Err(ParseAmountError::TooBig),
(false, sat) => Ok(SignedAmount(sat as i64)),
(false, sat) if sat > i64::MAX as u64 => Err(ParseAmountError::TooBig),
(false, sat) => Ok(SignedAmount(sat as i64)),
(true, sat) if sat == i64::MAX as u64 + 1 => Ok(SignedAmount(i64::MIN)),
(true, sat) if sat > i64::MAX as u64 + 1 => Err(ParseAmountError::TooBig),
(true, sat) => Ok(SignedAmount(-(sat as i64))),
(true, sat) if sat > i64::MAX as u64 + 1 => Err(ParseAmountError::TooBig),
(true, sat) => Ok(SignedAmount(-(sat as i64))),
}
}