Skip to content

Commit

Permalink
chore: modernize rustfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Aug 17, 2024
1 parent ca8bb03 commit ad1bdb3
Show file tree
Hide file tree
Showing 21 changed files with 89 additions and 200 deletions.
10 changes: 5 additions & 5 deletions .rustfmt.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ version = "Two"
max_width = 100
array_width = 100
attr_fn_like_width = 100
comment_width = 100
chain_width = 100
fn_call_width = 100
single_line_if_else_max_width = 100

format_code_in_doc_comments = true
fn_single_line = true
format_code_in_doc_comments = true
format_macro_matchers = true
format_macro_bodues = true
format_macro_bodies = true
format_strings = true
merge_derives = false
overflow_delimited_expr = true
Expand All @@ -19,9 +22,6 @@ use_try_shorthand = true
wrap_comments = true
where_single_line = true
unstable_features = true
empty_item_single_line = true

binop_separator = "Back"

imports_granularity = "Module"
group_imports = "StdExternalCrate"
3 changes: 1 addition & 2 deletions consensus/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ impl FromStr for BlockHeader {
impl BlockHeader {
pub fn block_hash(&self) -> BlockHash {
let mut enc = Sha256::default();
self.consensus_encode(&mut enc)
.expect("engines don't error");
self.consensus_encode(&mut enc).expect("engines don't error");
let mut double = Sha256::default();
double.input_raw(&enc.finish());
BlockHash::from_byte_array(double.finish())
Expand Down
3 changes: 1 addition & 2 deletions consensus/src/coding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,7 @@ pub trait ConsensusEncode {
fn consensus_encode(&self, writer: &mut impl Write) -> Result<usize, IoError>;
fn consensus_serialize(&self) -> Vec<u8> {
let mut buf = Vec::new();
self.consensus_encode(&mut buf)
.expect("in-memory writing can't fail");
self.consensus_encode(&mut buf).expect("in-memory writing can't fail");
buf
}
}
Expand Down
8 changes: 2 additions & 6 deletions consensus/src/pubkeys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@ impl CompressedPk {
fn dumb() -> Self { Self(PublicKey::from_slice(&[2u8; 33]).unwrap()) }

pub fn from_byte_array(data: [u8; 33]) -> Result<Self, InvalidPubkey<33>> {
PublicKey::from_slice(&data)
.map(Self)
.map_err(|_| InvalidPubkey::Specified(data.into()))
PublicKey::from_slice(&data).map(Self).map_err(|_| InvalidPubkey::Specified(data.into()))
}
pub fn to_byte_array(&self) -> [u8; 33] { self.0.serialize() }

Expand Down Expand Up @@ -123,9 +121,7 @@ impl UncompressedPk {
fn dumb() -> Self { Self(PublicKey::from_slice(&[2u8; 33]).unwrap()) }

pub fn from_byte_array(data: [u8; 65]) -> Result<Self, InvalidPubkey<65>> {
PublicKey::from_slice(&data)
.map(Self)
.map_err(|_| InvalidPubkey::Specified(data.into()))
PublicKey::from_slice(&data).map(Self).map_err(|_| InvalidPubkey::Specified(data.into()))
}
pub fn to_byte_array(&self) -> [u8; 65] { self.0.serialize_uncompressed() }
}
Expand Down
24 changes: 11 additions & 13 deletions consensus/src/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,21 +130,21 @@ impl ScriptPubkey {
/// Checks whether a script pubkey is a P2PKH output.
#[inline]
pub fn is_p2pkh(&self) -> bool {
self.0.len() == 25 &&
self.0[0] == OP_DUP &&
self.0[1] == OP_HASH160 &&
self.0[2] == OP_PUSHBYTES_20 &&
self.0[23] == OP_EQUALVERIFY &&
self.0[24] == OP_CHECKSIG
self.0.len() == 25
&& self.0[0] == OP_DUP
&& self.0[1] == OP_HASH160
&& self.0[2] == OP_PUSHBYTES_20
&& self.0[23] == OP_EQUALVERIFY
&& self.0[24] == OP_CHECKSIG
}

/// Checks whether a script pubkey is a P2SH output.
#[inline]
pub fn is_p2sh(&self) -> bool {
self.0.len() == 23 &&
self.0[0] == OP_HASH160 &&
self.0[1] == OP_PUSHBYTES_20 &&
self.0[22] == OP_EQUAL
self.0.len() == 23
&& self.0[0] == OP_HASH160
&& self.0[1] == OP_PUSHBYTES_20
&& self.0[22] == OP_EQUAL
}

#[inline]
Expand Down Expand Up @@ -287,9 +287,7 @@ impl ScriptBytes {

#[inline]
pub(crate) fn extend(&mut self, data: &[u8]) {
self.0
.extend(data.iter().copied())
.expect("script exceeds 4GB")
self.0.extend(data.iter().copied()).expect("script exceeds 4GB")
}

/// Computes the sum of `len` and the length of an appropriate push
Expand Down
75 changes: 25 additions & 50 deletions consensus/src/sigcache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,7 @@ impl<Prevout: Borrow<TxOut>, Tx: Borrow<Transaction>> SighashCache<Prevout, Tx>
// hash_type (1).
match sighash_type {
None => 0u8.consensus_encode(&mut hasher)?,
Some(sighash_type) => sighash_type
.to_consensus_u8()
.consensus_encode(&mut hasher)?,
Some(sighash_type) => sighash_type.to_consensus_u8().consensus_encode(&mut hasher)?,
};

{
Expand All @@ -188,12 +186,8 @@ impl<Prevout: Borrow<TxOut>, Tx: Borrow<Transaction>> SighashCache<Prevout, Tx>
if !anyone_can_pay {
self.common_cache().prevouts.consensus_encode(&mut hasher)?;
self.taproot_cache().amounts.consensus_encode(&mut hasher)?;
self.taproot_cache()
.script_pubkeys
.consensus_encode(&mut hasher)?;
self.common_cache()
.sequences
.consensus_encode(&mut hasher)?;
self.taproot_cache().script_pubkeys.consensus_encode(&mut hasher)?;
self.common_cache().sequences.consensus_encode(&mut hasher)?;
}

// If hash_type & 3 does not equal SIGHASH_NONE or SIGHASH_SINGLE:
Expand Down Expand Up @@ -224,20 +218,15 @@ impl<Prevout: Borrow<TxOut>, Tx: Borrow<Transaction>> SighashCache<Prevout, Tx>
// output spent by this input, serialized as script inside CTxOut. Its
// size is always 35 bytes. nSequence (4): nSequence of this input.
if anyone_can_pay {
let txin = tx
.inputs
.get(input_index)
.ok_or(SighashError::InvalidInputIndex {
txid: tx.txid(),
index: input_index,
inputs: tx.inputs.len(),
})?;
let txin = tx.inputs.get(input_index).ok_or(SighashError::InvalidInputIndex {
txid: tx.txid(),
index: input_index,
inputs: tx.inputs.len(),
})?;
let previous_output = self.prevouts[input_index].borrow();
txin.prev_output.consensus_encode(&mut hasher)?;
previous_output.value.consensus_encode(&mut hasher)?;
previous_output
.script_pubkey
.consensus_encode(&mut hasher)?;
previous_output.script_pubkey.consensus_encode(&mut hasher)?;
txin.sequence.consensus_encode(&mut hasher)?;
} else {
(input_index as u32).consensus_encode(&mut hasher)?;
Expand Down Expand Up @@ -335,27 +324,22 @@ impl<Prevout: Borrow<TxOut>, Tx: Borrow<Transaction>> SighashCache<Prevout, Tx>
zero_hash.consensus_encode(&mut hasher)?;
}

if !anyone_can_pay &&
sighash_flag != SighashFlag::Single &&
sighash_flag != SighashFlag::None
if !anyone_can_pay
&& sighash_flag != SighashFlag::Single
&& sighash_flag != SighashFlag::None
{
self.segwit_cache()
.sequences
.consensus_encode(&mut hasher)?;
self.segwit_cache().sequences.consensus_encode(&mut hasher)?;
} else {
zero_hash.consensus_encode(&mut hasher)?;
}

{
let tx = self.tx.borrow();
let txin = tx
.inputs
.get(input_index)
.ok_or(SighashError::InvalidInputIndex {
txid: tx.txid(),
index: input_index,
inputs: tx.inputs.len(),
})?;
let txin = tx.inputs.get(input_index).ok_or(SighashError::InvalidInputIndex {
txid: tx.txid(),
index: input_index,
inputs: tx.inputs.len(),
})?;

txin.prev_output.consensus_encode(&mut hasher)?;
script_code.consensus_encode(&mut hasher)?;
Expand All @@ -365,8 +349,8 @@ impl<Prevout: Borrow<TxOut>, Tx: Borrow<Transaction>> SighashCache<Prevout, Tx>

if sighash_flag != SighashFlag::Single && sighash_flag != SighashFlag::None {
self.segwit_cache().outputs.consensus_encode(&mut hasher)?;
} else if sighash_flag == SighashFlag::Single &&
input_index < self.tx.borrow().outputs.len()
} else if sighash_flag == SighashFlag::Single
&& input_index < self.tx.borrow().outputs.len()
{
let mut single_enc = Sighash::engine();
self.tx.borrow().outputs[input_index].consensus_encode(&mut single_enc)?;
Expand All @@ -376,9 +360,7 @@ impl<Prevout: Borrow<TxOut>, Tx: Borrow<Transaction>> SighashCache<Prevout, Tx>
}

self.tx.borrow().lock_time.consensus_encode(&mut hasher)?;
sighash_type
.to_consensus_u32()
.consensus_encode(&mut hasher)?;
sighash_type.to_consensus_u32().consensus_encode(&mut hasher)?;

Ok(Sighash::from_engine(hasher))
}
Expand Down Expand Up @@ -430,13 +412,9 @@ impl<Prevout: Borrow<TxOut>, Tx: Borrow<Transaction>> SighashCache<Prevout, Tx>
} else {
let inputs = tx_src.inputs.iter().enumerate().map(|(n, input)| TxIn {
prev_output: input.prev_output,
sig_script: if n == input_index {
sig_script.clone()
} else {
SigScript::new()
},
sequence: if n != input_index &&
(sighash_flag == SighashFlag::Single || sighash_flag == SighashFlag::None)
sig_script: if n == input_index { sig_script.clone() } else { SigScript::new() },
sequence: if n != input_index
&& (sighash_flag == SighashFlag::Single || sighash_flag == SighashFlag::None)
{
SeqNo::ZERO
} else {
Expand Down Expand Up @@ -501,10 +479,7 @@ impl<Prevout: Borrow<TxOut>, Tx: Borrow<Transaction>> SighashCache<Prevout, Tx>
let mut enc_script_pubkeys = Sha256::default();
for prevout in &self.prevouts {
let _ = prevout.borrow().value.consensus_encode(&mut enc_amounts);
let _ = prevout
.borrow()
.script_pubkey
.consensus_encode(&mut enc_script_pubkeys);
let _ = prevout.borrow().script_pubkey.consensus_encode(&mut enc_script_pubkeys);
}
TaprootCache {
amounts: enc_amounts.finish().into(),
Expand Down
4 changes: 1 addition & 3 deletions consensus/src/stl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ pub const LIB_ID_BP_CONSENSUS: &str =
fn _bitcoin_stl() -> Result<TypeLib, CompileError> { _bp_tx_stl() }

fn _bp_tx_stl() -> Result<TypeLib, CompileError> {
LibBuilder::new(libname!(LIB_NAME_BITCOIN), None)
.transpile::<Tx>()
.compile()
LibBuilder::new(libname!(LIB_NAME_BITCOIN), None).transpile::<Tx>().compile()
}

fn _bp_consensus_stl() -> Result<TypeLib, CompileError> {
Expand Down
11 changes: 3 additions & 8 deletions consensus/src/taproot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,7 @@ impl InternalKeypair {
}
let tweak =
Scalar::from_be_bytes(engine.finish()).expect("hash value greater than curve order");
let pair = self
.0
.add_xonly_tweak(secp256k1::SECP256K1, &tweak)
.expect("hash collision");
let pair = self.0.add_xonly_tweak(secp256k1::SECP256K1, &tweak).expect("hash collision");
let (outpput_key, tweaked_parity) = pair.x_only_public_key();
debug_assert!(internal_pk.tweak_add_check(
secp256k1::SECP256K1,
Expand Down Expand Up @@ -216,10 +213,8 @@ impl InternalPk {
}
let tweak =
Scalar::from_be_bytes(engine.finish()).expect("hash value greater than curve order");
let (output_key, tweaked_parity) = self
.0
.add_tweak(secp256k1::SECP256K1, &tweak)
.expect("hash collision");
let (output_key, tweaked_parity) =
self.0.add_tweak(secp256k1::SECP256K1, &tweak).expect("hash collision");
debug_assert!(self.tweak_add_check(
secp256k1::SECP256K1,
&output_key,
Expand Down
24 changes: 4 additions & 20 deletions consensus/src/timelocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,7 @@ impl LockTime {
/// `None` is returned.
#[inline]
pub const fn from_height(height: u32) -> Option<Self> {
if height < LOCKTIME_THRESHOLD {
Some(Self(height))
} else {
None
}
if height < LOCKTIME_THRESHOLD { Some(Self(height)) } else { None }
}

/// Creates absolute time lock with the given UNIX timestamp value.
Expand All @@ -121,11 +117,7 @@ impl LockTime {
/// `None` is returned.
#[inline]
pub const fn from_unix_timestamp(timestamp: u32) -> Option<Self> {
if timestamp < LOCKTIME_THRESHOLD {
None
} else {
Some(Self(timestamp))
}
if timestamp < LOCKTIME_THRESHOLD { None } else { Some(Self(timestamp)) }
}

/// Converts into full u32 representation of `nLockTime` value as it is
Expand Down Expand Up @@ -204,11 +196,7 @@ impl LockTimestamp {
/// `None` is returned.
#[inline]
pub fn from_unix_timestamp(timestamp: u32) -> Option<Self> {
if timestamp < LOCKTIME_THRESHOLD {
None
} else {
Some(Self(timestamp))
}
if timestamp < LOCKTIME_THRESHOLD { None } else { Some(Self(timestamp)) }
}

#[inline]
Expand Down Expand Up @@ -312,11 +300,7 @@ impl LockHeight {
/// `None` is returned.
#[inline]
pub fn from_height(height: u32) -> Option<Self> {
if height < LOCKTIME_THRESHOLD {
Some(Self(height))
} else {
None
}
if height < LOCKTIME_THRESHOLD { Some(Self(height)) } else { None }
}

#[inline]
Expand Down
Loading

0 comments on commit ad1bdb3

Please sign in to comment.