Skip to content

Commit 871aec2

Browse files
committed
Move parts of the check_spends macro into a function
While we cannot move the entire `check_spends` macro into a function, we can move parts of it out, which we do here. This reduces the `--profile=test --lib` `Zpretty=expanded` code size from 316,856 LoC to 313,312 LoC.
1 parent 8c6e132 commit 871aec2

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

lightning/src/ln/functional_test_utils.rs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,6 +1149,24 @@ pub fn update_nodes_with_chan_announce<'a, 'b, 'c, 'd>(nodes: &'a Vec<Node<'b, '
11491149
}
11501150
}
11511151

1152+
pub fn do_check_spends<F: Fn(&bitcoin::blockdata::transaction::OutPoint) -> Option<TxOut>>(tx: &Transaction, get_output: F) {
1153+
for outp in tx.output.iter() {
1154+
assert!(outp.value >= outp.script_pubkey.dust_value().to_sat(), "Spending tx output didn't meet dust limit");
1155+
}
1156+
let mut total_value_in = 0;
1157+
for input in tx.input.iter() {
1158+
total_value_in += get_output(&input.previous_output).unwrap().value;
1159+
}
1160+
let mut total_value_out = 0;
1161+
for output in tx.output.iter() {
1162+
total_value_out += output.value;
1163+
}
1164+
let min_fee = (tx.weight() as u64 + 3) / 4; // One sat per vbyte (ie per weight/4, rounded up)
1165+
// Input amount - output amount = fee, so check that out + min_fee is smaller than input
1166+
assert!(total_value_out + min_fee <= total_value_in);
1167+
tx.verify(get_output).unwrap();
1168+
}
1169+
11521170
#[macro_export]
11531171
macro_rules! check_spends {
11541172
($tx: expr, $($spends_txn: expr),*) => {
@@ -1158,9 +1176,6 @@ macro_rules! check_spends {
11581176
assert!(outp.value >= outp.script_pubkey.dust_value().to_sat(), "Input tx output didn't meet dust limit");
11591177
}
11601178
)*
1161-
for outp in $tx.output.iter() {
1162-
assert!(outp.value >= outp.script_pubkey.dust_value().to_sat(), "Spending tx output didn't meet dust limit");
1163-
}
11641179
let get_output = |out_point: &bitcoin::blockdata::transaction::OutPoint| {
11651180
$(
11661181
if out_point.txid == $spends_txn.txid() {
@@ -1169,18 +1184,7 @@ macro_rules! check_spends {
11691184
)*
11701185
None
11711186
};
1172-
let mut total_value_in = 0;
1173-
for input in $tx.input.iter() {
1174-
total_value_in += get_output(&input.previous_output).unwrap().value;
1175-
}
1176-
let mut total_value_out = 0;
1177-
for output in $tx.output.iter() {
1178-
total_value_out += output.value;
1179-
}
1180-
let min_fee = ($tx.weight() as u64 + 3) / 4; // One sat per vbyte (ie per weight/4, rounded up)
1181-
// Input amount - output amount = fee, so check that out + min_fee is smaller than input
1182-
assert!(total_value_out + min_fee <= total_value_in);
1183-
$tx.verify(get_output).unwrap();
1187+
$crate::ln::functional_test_utils::do_check_spends(&$tx, get_output);
11841188
}
11851189
}
11861190
}

0 commit comments

Comments
 (0)