Skip to content

Commit 7e6f514

Browse files
committed
Merge rust-bitcoin/rust-bitcoin#806: Use rustfmt to hint at clean ups for the codebase
a77907d Remove unnecessary explicit type annotation (Tobin Harding) 71cf00a Use less vertical lines (Tobin Harding) a5c06e0 Refactor vector initialisation (Tobin Harding) aabf2d1 Use brace not parenth fo macro arm (Tobin Harding) b021415 Use block stlye function call (Tobin Harding) d6462ba Refactor usage of + (Tobin Harding) 702e8bf Refactor consensus_encode (Tobin Harding) a8ed95e Refactor where statements (Tobin Harding) 6d84998 Improve braces usage (Tobin Harding) 39ec596 Fix unusual indentation (Tobin Harding) b9b6e7e Remove unneeded braces (Tobin Harding) 5d68ad8 Remove unneeded return statement (Tobin Harding) bf4f563 Refactor whitespace (Tobin Harding) 1c50239 Remove trailing whitespace (Tobin Harding) Pull request description: `rustfmt` is still under discussion, while researching the topic I came across a maintainer of another project that does not use `rustfmt` who mentioned that he manually implemented the `rusfmt` suggestions that he liked ever month or so. This seemed like a good idea so I did it. This was extremely painful but I believe I have put together a PR that is non-controversial with well separated patches. Totally non urgent. ACKs for top commit: apoelstra: ACK a77907d sanket1729: ACK a77907d. Tree-SHA512: 27aa10d1c6d02d0e5bc335a5cda9cf2664b968c298d2ea6c653b8074abf18764a9d0f19c36222852fc23b887ab64144901dae059088e61478e9a90a042221e61
2 parents efa9555 + a77907d commit 7e6f514

30 files changed

+301
-453
lines changed

src/blockdata/block.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ impl Block {
175175
}
176176

177177
/// check if merkle root of header matches merkle root of the transaction list
178-
pub fn check_merkle_root (&self) -> bool {
178+
pub fn check_merkle_root(&self) -> bool {
179179
match self.compute_merkle_root() {
180180
Some(merkle_root) => self.header.merkle_root == merkle_root,
181181
None => false,
@@ -229,7 +229,7 @@ impl Block {
229229
}
230230

231231
/// Computes the witness commitment for the block's transaction list.
232-
pub fn compute_witness_commitment (witness_root: &WitnessMerkleNode, witness_reserved_value: &[u8]) -> WitnessCommitment {
232+
pub fn compute_witness_commitment(witness_root: &WitnessMerkleNode, witness_reserved_value: &[u8]) -> WitnessCommitment {
233233
let mut encoder = WitnessCommitment::engine();
234234
witness_root.consensus_encode(&mut encoder).expect("engines don't error");
235235
encoder.input(witness_reserved_value);
@@ -238,14 +238,14 @@ impl Block {
238238

239239
/// Computes the merkle root of transactions hashed for witness.
240240
pub fn witness_root(&self) -> Option<WitnessMerkleNode> {
241-
let hashes = self.txdata.iter().enumerate().map(|(i, t)|
241+
let hashes = self.txdata.iter().enumerate().map(|(i, t)| {
242242
if i == 0 {
243243
// Replace the first hash with zeroes.
244244
Wtxid::default().as_hash()
245245
} else {
246246
t.wtxid().as_hash()
247247
}
248-
);
248+
});
249249
bitcoin_merkle_root(hashes).map(|h| h.into())
250250
}
251251

src/blockdata/opcodes.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -632,10 +632,10 @@ impl fmt::Debug for All {
632632
all::OP_NUMEQUAL => write!(f, "NUMEQUAL"),
633633
all::OP_NUMEQUALVERIFY => write!(f, "NUMEQUALVERIFY"),
634634
all::OP_NUMNOTEQUAL => write!(f, "NUMNOTEQUAL"),
635-
all::OP_LESSTHAN => write!(f, "LESSTHAN"),
636-
all::OP_GREATERTHAN => write!(f, "GREATERTHAN"),
637-
all::OP_LESSTHANOREQUAL => write!(f, "LESSTHANOREQUAL"),
638-
all::OP_GREATERTHANOREQUAL => write!(f, "GREATERTHANOREQUAL"),
635+
all::OP_LESSTHAN => write!(f, "LESSTHAN"),
636+
all::OP_GREATERTHAN => write!(f, "GREATERTHAN"),
637+
all::OP_LESSTHANOREQUAL => write!(f, "LESSTHANOREQUAL"),
638+
all::OP_GREATERTHANOREQUAL => write!(f, "GREATERTHANOREQUAL"),
639639
all::OP_MIN => write!(f, "MIN"),
640640
all::OP_MAX => write!(f, "MAX"),
641641
all::OP_WITHIN => write!(f, "WITHIN"),
@@ -860,8 +860,8 @@ impl Ordinary {
860860
/// Encode as a byte
861861
#[inline]
862862
pub fn into_u8(self) -> u8 {
863-
self as u8
864-
}
863+
self as u8
864+
}
865865
}
866866

867867
#[cfg(test)]

src/blockdata/script.rs

Lines changed: 24 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,8 @@ impl fmt::UpperHex for Script {
8989

9090
impl hex::FromHex for Script {
9191
fn from_byte_iter<I>(iter: I) -> Result<Self, hex::Error>
92-
where I: Iterator<Item=Result<u8, hex::Error>> +
93-
ExactSizeIterator +
94-
DoubleEndedIterator,
92+
where
93+
I: Iterator<Item=Result<u8, hex::Error>> + ExactSizeIterator + DoubleEndedIterator,
9594
{
9695
Vec::from_byte_iter(iter).map(|v| Script(Box::<[u8]>::from(v)))
9796
}
@@ -141,11 +140,11 @@ impl fmt::Display for Error {
141140
Error::NonMinimalPush => "non-minimal datapush",
142141
Error::EarlyEndOfScript => "unexpected end of script",
143142
Error::NumericOverflow => "numeric overflow (number on stack larger than 4 bytes)",
144-
#[cfg(feature="bitcoinconsensus")]
143+
#[cfg(feature = "bitcoinconsensus")]
145144
Error::BitcoinConsensus(ref _n) => "bitcoinconsensus verification failed",
146-
#[cfg(feature="bitcoinconsensus")]
145+
#[cfg(feature = "bitcoinconsensus")]
147146
Error::UnknownSpentOutput(ref _point) => "unknown spent output Transaction::verify()",
148-
#[cfg(feature="bitcoinconsensus")]
147+
#[cfg(feature = "bitcoinconsensus")]
149148
Error::SerializationError => "can not serialize the spending transaction in Transaction::verify()",
150149
};
151150
f.write_str(str)
@@ -717,7 +716,7 @@ impl<'a> Iterator for Instructions<'a> {
717716
opcodes::Class::PushBytes(n) => {
718717
let n = n as usize;
719718
if self.data.len() < n + 1 {
720-
self.data = &[]; // Kill iterator so that it does not return an infinite stream of errors
719+
self.data = &[]; // Kill iterator so that it does not return an infinite stream of errors
721720
return Some(Err(Error::EarlyEndOfScript));
722721
}
723722
if self.enforce_minimal {
@@ -949,7 +948,8 @@ impl_index_newtype!(Builder, u8);
949948
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
950949
impl<'de> serde::Deserialize<'de> for Script {
951950
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
952-
where D: serde::Deserializer<'de>,
951+
where
952+
D: serde::Deserializer<'de>,
953953
{
954954
use core::fmt::Formatter;
955955
use hashes::hex::FromHex;
@@ -965,20 +965,23 @@ impl<'de> serde::Deserialize<'de> for Script {
965965
}
966966

967967
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
968-
where E: serde::de::Error,
968+
where
969+
E: serde::de::Error,
969970
{
970971
let v = Vec::from_hex(v).map_err(E::custom)?;
971972
Ok(Script::from(v))
972973
}
973974

974975
fn visit_borrowed_str<E>(self, v: &'de str) -> Result<Self::Value, E>
975-
where E: serde::de::Error,
976+
where
977+
E: serde::de::Error,
976978
{
977979
self.visit_str(v)
978980
}
979981

980982
fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
981-
where E: serde::de::Error,
983+
where
984+
E: serde::de::Error,
982985
{
983986
self.visit_str(&v)
984987
}
@@ -995,7 +998,8 @@ impl<'de> serde::Deserialize<'de> for Script {
995998
}
996999

9971000
fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
998-
where E: serde::de::Error,
1001+
where
1002+
E: serde::de::Error,
9991003
{
10001004
Ok(Script::from(v.to_vec()))
10011005
}
@@ -1023,10 +1027,7 @@ impl serde::Serialize for Script {
10231027

10241028
impl Encodable for Script {
10251029
#[inline]
1026-
fn consensus_encode<S: io::Write>(
1027-
&self,
1028-
s: S,
1029-
) -> Result<usize, io::Error> {
1030+
fn consensus_encode<S: io::Write>(&self, s: S) -> Result<usize, io::Error> {
10301031
self.0.consensus_encode(s)
10311032
}
10321033
}
@@ -1354,38 +1355,19 @@ mod test {
13541355
let slop_v_nonmin: Result<Vec<Instruction>, Error> = nonminimal.instructions().collect();
13551356
let slop_v_nonmin_alt: Result<Vec<Instruction>, Error> = nonminimal_alt.instructions().collect();
13561357

1357-
assert_eq!(
1358-
v_zero.unwrap(),
1359-
vec![
1360-
Instruction::PushBytes(&[]),
1361-
]
1362-
);
1363-
assert_eq!(
1364-
v_zeropush.unwrap(),
1365-
vec![
1366-
Instruction::PushBytes(&[0]),
1367-
]
1368-
);
1358+
assert_eq!(v_zero.unwrap(), vec![Instruction::PushBytes(&[])]);
1359+
assert_eq!(v_zeropush.unwrap(), vec![Instruction::PushBytes(&[0])]);
13691360

13701361
assert_eq!(
13711362
v_min.clone().unwrap(),
1372-
vec![
1373-
Instruction::PushBytes(&[105]),
1374-
Instruction::Op(opcodes::OP_NOP3),
1375-
]
1363+
vec![Instruction::PushBytes(&[105]), Instruction::Op(opcodes::OP_NOP3)]
13761364
);
13771365

1378-
assert_eq!(
1379-
v_nonmin.err().unwrap(),
1380-
Error::NonMinimalPush
1381-
);
1366+
assert_eq!(v_nonmin.err().unwrap(), Error::NonMinimalPush);
13821367

13831368
assert_eq!(
13841369
v_nonmin_alt.clone().unwrap(),
1385-
vec![
1386-
Instruction::PushBytes(&[105, 0]),
1387-
Instruction::Op(opcodes::OP_NOP3),
1388-
]
1370+
vec![Instruction::PushBytes(&[105, 0]), Instruction::Op(opcodes::OP_NOP3)]
13891371
);
13901372

13911373
assert_eq!(v_min.clone().unwrap(), slop_v_min.unwrap());
@@ -1395,7 +1377,7 @@ mod test {
13951377

13961378
#[test]
13971379
fn script_ord() {
1398-
let script_1 = Builder::new().push_slice(&[1,2,3,4]).into_script();
1380+
let script_1 = Builder::new().push_slice(&[1, 2, 3, 4]).into_script();
13991381
let script_2 = Builder::new().push_int(10).into_script();
14001382
let script_3 = Builder::new().push_int(15).into_script();
14011383
let script_4 = Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script();
@@ -1413,7 +1395,7 @@ mod test {
14131395
}
14141396

14151397
#[test]
1416-
#[cfg(feature="bitcoinconsensus")]
1398+
#[cfg(feature = "bitcoinconsensus")]
14171399
fn test_bitcoinconsensus () {
14181400
// a random segwit transaction from the blockchain using native segwit
14191401
let spent = Builder::from(Vec::from_hex("0020701a8d401c84fb13e6baf169d59684e17abd9fa216c8cc5b9fc63d622ff8c58d").unwrap()).into_script();

src/blockdata/transaction.rs

Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,7 @@ impl OutPoint {
5959
/// Creates a new [`OutPoint`].
6060
#[inline]
6161
pub fn new(txid: Txid, vout: u32) -> OutPoint {
62-
OutPoint {
63-
txid,
64-
vout,
65-
}
62+
OutPoint { txid, vout }
6663
}
6764

6865
/// Creates a "null" `OutPoint`.
@@ -137,7 +134,7 @@ impl fmt::Display for ParseOutPointError {
137134

138135
#[cfg(feature = "std")]
139136
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
140-
impl error::Error for ParseOutPointError {
137+
impl error::Error for ParseOutPointError {
141138
fn cause(&self) -> Option<&dyn error::Error> {
142139
match *self {
143140
ParseOutPointError::Txid(ref e) => Some(e),
@@ -504,7 +501,9 @@ impl Transaction {
504501
#[cfg(feature="bitcoinconsensus")]
505502
#[cfg_attr(docsrs, doc(cfg(feature = "bitcoinconsensus")))]
506503
pub fn verify<S>(&self, spent: S) -> Result<(), script::Error>
507-
where S: FnMut(&OutPoint) -> Option<TxOut> {
504+
where
505+
S: FnMut(&OutPoint) -> Option<TxOut>
506+
{
508507
self.verify_with_flags(spent, ::bitcoinconsensus::VERIFY_ALL)
509508
}
510509

@@ -513,7 +512,10 @@ impl Transaction {
513512
#[cfg(feature="bitcoinconsensus")]
514513
#[cfg_attr(docsrs, doc(cfg(feature = "bitcoinconsensus")))]
515514
pub fn verify_with_flags<S, F>(&self, mut spent: S, flags: F) -> Result<(), script::Error>
516-
where S: FnMut(&OutPoint) -> Option<TxOut>, F : Into<u32> {
515+
where
516+
S: FnMut(&OutPoint) -> Option<TxOut>,
517+
F: Into<u32>
518+
{
517519
let tx = encode::serialize(&*self);
518520
let flags: u32 = flags.into();
519521
for (idx, input) in self.input.iter().enumerate() {
@@ -542,10 +544,7 @@ impl Transaction {
542544
impl_consensus_encoding!(TxOut, value, script_pubkey);
543545

544546
impl Encodable for OutPoint {
545-
fn consensus_encode<S: io::Write>(
546-
&self,
547-
mut s: S,
548-
) -> Result<usize, io::Error> {
547+
fn consensus_encode<S: io::Write>(&self, mut s: S) -> Result<usize, io::Error> {
549548
let len = self.txid.consensus_encode(&mut s)?;
550549
Ok(len + self.vout.consensus_encode(s)?)
551550
}
@@ -560,10 +559,7 @@ impl Decodable for OutPoint {
560559
}
561560

562561
impl Encodable for TxIn {
563-
fn consensus_encode<S: io::Write>(
564-
&self,
565-
mut s: S,
566-
) -> Result<usize, io::Error> {
562+
fn consensus_encode<S: io::Write>(&self, mut s: S) -> Result<usize, io::Error> {
567563
let mut len = 0;
568564
len += self.previous_output.consensus_encode(&mut s)?;
569565
len += self.script_sig.consensus_encode(&mut s)?;
@@ -583,10 +579,7 @@ impl Decodable for TxIn {
583579
}
584580

585581
impl Encodable for Transaction {
586-
fn consensus_encode<S: io::Write>(
587-
&self,
588-
mut s: S,
589-
) -> Result<usize, io::Error> {
582+
fn consensus_encode<S: io::Write>(&self, mut s: S) -> Result<usize, io::Error> {
590583
let mut len = 0;
591584
len += self.version.consensus_encode(&mut s)?;
592585
// To avoid serialization ambiguity, no inputs means we use BIP141 serialization (see
@@ -643,9 +636,7 @@ impl Decodable for Transaction {
643636
}
644637
}
645638
// We don't support anything else
646-
x => {
647-
Err(encode::Error::UnsupportedSegwitFlag(x))
648-
}
639+
x => Err(encode::Error::UnsupportedSegwitFlag(x)),
649640
}
650641
// non-segwit
651642
} else {
@@ -674,8 +665,8 @@ impl fmt::Display for NonStandardSigHashType {
674665
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
675666
impl error::Error for NonStandardSigHashType {}
676667

677-
/// Legacy Hashtype of an input's signature.
678-
#[deprecated(since="0.28.0", note="Please use [`EcdsaSigHashType`] instead")]
668+
/// Legacy Hashtype of an input's signature
669+
#[deprecated(since = "0.28.0", note = "Please use [`EcdsaSigHashType`] instead")]
679670
pub type SigHashType = EcdsaSigHashType;
680671

681672
/// Hashtype of an input's signature, encoded in the last byte of the signature.
@@ -736,17 +727,17 @@ impl EcdsaSigHashType {
736727
/// Splits the sighash flag into the "real" sighash flag and the ANYONECANPAY boolean.
737728
pub(crate) fn split_anyonecanpay_flag(self) -> (EcdsaSigHashType, bool) {
738729
match self {
739-
EcdsaSigHashType::All => (EcdsaSigHashType::All, false),
740-
EcdsaSigHashType::None => (EcdsaSigHashType::None, false),
741-
EcdsaSigHashType::Single => (EcdsaSigHashType::Single, false),
742-
EcdsaSigHashType::AllPlusAnyoneCanPay => (EcdsaSigHashType::All, true),
743-
EcdsaSigHashType::NonePlusAnyoneCanPay => (EcdsaSigHashType::None, true),
744-
EcdsaSigHashType::SinglePlusAnyoneCanPay => (EcdsaSigHashType::Single, true)
730+
EcdsaSigHashType::All => (EcdsaSigHashType::All, false),
731+
EcdsaSigHashType::None => (EcdsaSigHashType::None, false),
732+
EcdsaSigHashType::Single => (EcdsaSigHashType::Single, false),
733+
EcdsaSigHashType::AllPlusAnyoneCanPay => (EcdsaSigHashType::All, true),
734+
EcdsaSigHashType::NonePlusAnyoneCanPay => (EcdsaSigHashType::None, true),
735+
EcdsaSigHashType::SinglePlusAnyoneCanPay => (EcdsaSigHashType::Single, true)
745736
}
746737
}
747738

748739
/// Reads a 4-byte uint32 as a sighash type.
749-
#[deprecated(since="0.26.1", note="please use `from_u32_consensus` or `from_u32_standard` instead")]
740+
#[deprecated(since = "0.26.1", note = "please use `from_u32_consensus` or `from_u32_standard` instead")]
750741
pub fn from_u32(n: u32) -> EcdsaSigHashType {
751742
Self::from_u32_consensus(n)
752743
}
@@ -1117,12 +1108,14 @@ mod tests {
11171108

11181109
#[test]
11191110
fn test_sighashtype_fromstr_display() {
1120-
let sighashtypes = vec![("SIGHASH_ALL", EcdsaSigHashType::All),
1111+
let sighashtypes = vec![
1112+
("SIGHASH_ALL", EcdsaSigHashType::All),
11211113
("SIGHASH_NONE", EcdsaSigHashType::None),
11221114
("SIGHASH_SINGLE", EcdsaSigHashType::Single),
11231115
("SIGHASH_ALL|SIGHASH_ANYONECANPAY", EcdsaSigHashType::AllPlusAnyoneCanPay),
11241116
("SIGHASH_NONE|SIGHASH_ANYONECANPAY", EcdsaSigHashType::NonePlusAnyoneCanPay),
1125-
("SIGHASH_SINGLE|SIGHASH_ANYONECANPAY", EcdsaSigHashType::SinglePlusAnyoneCanPay)];
1117+
("SIGHASH_SINGLE|SIGHASH_ANYONECANPAY", EcdsaSigHashType::SinglePlusAnyoneCanPay)
1118+
];
11261119
for (s, sht) in sighashtypes {
11271120
assert_eq!(sht.to_string(), s);
11281121
assert_eq!(EcdsaSigHashType::from_str(s).unwrap(), sht);
@@ -1486,7 +1479,7 @@ mod tests {
14861479
// test that we fail with repeated use of same input
14871480
let mut double_spending = spending.clone();
14881481
let re_use = double_spending.input[0].clone();
1489-
double_spending.input.push (re_use);
1482+
double_spending.input.push(re_use);
14901483

14911484
assert!(double_spending.verify(|point: &OutPoint| {
14921485
if let Some(tx) = spent2.remove(&point.txid) {

0 commit comments

Comments
 (0)