Skip to content

Commit b9fc8ba

Browse files
committed
rustfmt: Run on util/transaction_utils.rs
1 parent 2be14b3 commit b9fc8ba

File tree

1 file changed

+40
-41
lines changed

1 file changed

+40
-41
lines changed

lightning/src/util/transaction_utils.rs

Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,21 @@
88
// licenses.
99

1010
use bitcoin::amount::Amount;
11-
use bitcoin::transaction::{Transaction, TxOut};
12-
use bitcoin::script::ScriptBuf;
13-
use bitcoin::consensus::Encodable;
1411
use bitcoin::consensus::encode::VarInt;
12+
use bitcoin::consensus::Encodable;
13+
use bitcoin::script::ScriptBuf;
14+
use bitcoin::transaction::{Transaction, TxOut};
1515

1616
#[allow(unused_imports)]
1717
use crate::prelude::*;
1818

1919
use crate::io_extras::sink;
2020
use core::cmp::Ordering;
2121

22-
pub fn sort_outputs<T, C : Fn(&T, &T) -> Ordering>(outputs: &mut Vec<(TxOut, T)>, tie_breaker: C) {
22+
pub fn sort_outputs<T, C: Fn(&T, &T) -> Ordering>(outputs: &mut Vec<(TxOut, T)>, tie_breaker: C) {
2323
outputs.sort_unstable_by(|a, b| {
2424
a.0.value.cmp(&b.0.value).then_with(|| {
25-
a.0.script_pubkey[..].cmp(&b.0.script_pubkey[..]).then_with(|| {
26-
tie_breaker(&a.1, &b.1)
27-
})
25+
a.0.script_pubkey[..].cmp(&b.0.script_pubkey[..]).then_with(|| tie_breaker(&a.1, &b.1))
2826
})
2927
});
3028
}
@@ -34,22 +32,26 @@ pub fn sort_outputs<T, C : Fn(&T, &T) -> Ordering>(outputs: &mut Vec<(TxOut, T)>
3432
/// Assumes at least one input will have a witness (ie spends a segwit output).
3533
/// Returns an Err(()) if the requested feerate cannot be met.
3634
/// Returns the expected maximum weight of the fully signed transaction on success.
37-
pub(crate) fn maybe_add_change_output(tx: &mut Transaction, input_value: Amount, witness_max_weight: u64, feerate_sat_per_1000_weight: u32, change_destination_script: ScriptBuf) -> Result<u64, ()> {
38-
if input_value > Amount::MAX_MONEY { return Err(()); }
35+
pub(crate) fn maybe_add_change_output(
36+
tx: &mut Transaction, input_value: Amount, witness_max_weight: u64,
37+
feerate_sat_per_1000_weight: u32, change_destination_script: ScriptBuf,
38+
) -> Result<u64, ()> {
39+
if input_value > Amount::MAX_MONEY {
40+
return Err(());
41+
}
3942

4043
const WITNESS_FLAG_BYTES: u64 = 2;
4144

4245
let mut output_value = Amount::ZERO;
4346
for output in tx.output.iter() {
4447
output_value += output.value;
45-
if output_value >= input_value { return Err(()); }
48+
if output_value >= input_value {
49+
return Err(());
50+
}
4651
}
4752

4853
let dust_value = change_destination_script.minimal_non_dust();
49-
let mut change_output = TxOut {
50-
script_pubkey: change_destination_script,
51-
value: Amount::ZERO,
52-
};
54+
let mut change_output = TxOut { script_pubkey: change_destination_script, value: Amount::ZERO };
5355
let change_len = change_output.consensus_encode(&mut sink()).unwrap();
5456
let starting_weight = tx.weight().to_wu() + WITNESS_FLAG_BYTES + witness_max_weight as u64;
5557
let starting_fees = (starting_weight as i64) * feerate_sat_per_1000_weight as i64 / 1000;
@@ -76,60 +78,58 @@ mod tests {
7678
use super::*;
7779

7880
use bitcoin::amount::Amount;
79-
use bitcoin::locktime::absolute::LockTime;
80-
use bitcoin::transaction::{TxIn, OutPoint, Version};
81-
use bitcoin::script::Builder;
8281
use bitcoin::hash_types::Txid;
8382
use bitcoin::hashes::Hash;
8483
use bitcoin::hex::FromHex;
84+
use bitcoin::locktime::absolute::LockTime;
85+
use bitcoin::script::Builder;
86+
use bitcoin::transaction::{OutPoint, TxIn, Version};
8587
use bitcoin::{PubkeyHash, Sequence, Witness};
8688

8789
use alloc::vec;
8890

8991
#[test]
9092
fn sort_output_by_value() {
9193
let txout1 = TxOut {
92-
value: Amount::from_sat(100),
93-
script_pubkey: Builder::new().push_int(0).into_script()
94+
value: Amount::from_sat(100),
95+
script_pubkey: Builder::new().push_int(0).into_script(),
9496
};
9597
let txout1_ = txout1.clone();
9698

9799
let txout2 = TxOut {
98100
value: Amount::from_sat(99),
99-
script_pubkey: Builder::new().push_int(0).into_script()
101+
script_pubkey: Builder::new().push_int(0).into_script(),
100102
};
101103
let txout2_ = txout2.clone();
102104

103105
let mut outputs = vec![(txout1, "ignore"), (txout2, "ignore")];
104-
sort_outputs(&mut outputs, |_, _| { unreachable!(); });
106+
sort_outputs(&mut outputs, |_, _| {
107+
unreachable!();
108+
});
105109

106-
assert_eq!(
107-
&outputs,
108-
&vec![(txout2_, "ignore"), (txout1_, "ignore")]
109-
);
110+
assert_eq!(&outputs, &vec![(txout2_, "ignore"), (txout1_, "ignore")]);
110111
}
111112

112113
#[test]
113114
fn sort_output_by_script_pubkey() {
114115
let txout1 = TxOut {
115-
value: Amount::from_sat(100),
116+
value: Amount::from_sat(100),
116117
script_pubkey: Builder::new().push_int(3).into_script(),
117118
};
118119
let txout1_ = txout1.clone();
119120

120121
let txout2 = TxOut {
121122
value: Amount::from_sat(100),
122-
script_pubkey: Builder::new().push_int(1).push_int(2).into_script()
123+
script_pubkey: Builder::new().push_int(1).push_int(2).into_script(),
123124
};
124125
let txout2_ = txout2.clone();
125126

126127
let mut outputs = vec![(txout1, "ignore"), (txout2, "ignore")];
127-
sort_outputs(&mut outputs, |_, _| { unreachable!(); });
128+
sort_outputs(&mut outputs, |_, _| {
129+
unreachable!();
130+
});
128131

129-
assert_eq!(
130-
&outputs,
131-
&vec![(txout2_, "ignore"), (txout1_, "ignore")]
132-
);
132+
assert_eq!(&outputs, &vec![(txout2_, "ignore"), (txout1_, "ignore")]);
133133
}
134134

135135
#[test]
@@ -148,29 +148,28 @@ mod tests {
148148
let txout2_ = txout2.clone();
149149

150150
let mut outputs = vec![(txout1, "ignore"), (txout2, "ignore")];
151-
sort_outputs(&mut outputs, |_, _| { unreachable!(); });
151+
sort_outputs(&mut outputs, |_, _| {
152+
unreachable!();
153+
});
152154

153155
assert_eq!(&outputs, &vec![(txout1_, "ignore"), (txout2_, "ignore")]);
154156
}
155157

156158
#[test]
157159
fn sort_output_tie_breaker_test() {
158160
let txout1 = TxOut {
159-
value: Amount::from_sat(100),
160-
script_pubkey: Builder::new().push_int(1).push_int(2).into_script()
161+
value: Amount::from_sat(100),
162+
script_pubkey: Builder::new().push_int(1).push_int(2).into_script(),
161163
};
162164
let txout1_ = txout1.clone();
163165

164166
let txout2 = txout1.clone();
165167
let txout2_ = txout1.clone();
166168

167169
let mut outputs = vec![(txout1, 420), (txout2, 69)];
168-
sort_outputs(&mut outputs, |a, b| { a.cmp(b) });
170+
sort_outputs(&mut outputs, |a, b| a.cmp(b));
169171

170-
assert_eq!(
171-
&outputs,
172-
&vec![(txout2_, 69), (txout1_, 420)]
173-
);
172+
assert_eq!(&outputs, &vec![(txout2_, 69), (txout1_, 420)]);
174173
}
175174

176175
fn script_from_hex(hex_str: &str) -> ScriptBuf {
@@ -276,7 +275,7 @@ mod tests {
276275
assert_eq!(tx.output[0].value.to_sat(), 546);
277276
assert_eq!(tx.output[0].script_pubkey, output_spk);
278277
// New weight is exactly the fee we wanted.
279-
assert_eq!(tx.weight().to_wu() / 4, 590-546);
278+
assert_eq!(tx.weight().to_wu() / 4, 590 - 546);
280279

281280
tx.output.pop();
282281
// The only change is the addition of one output.

0 commit comments

Comments
 (0)