Skip to content

Commit 7a24ed3

Browse files
committed
f clean up weight calculation
1 parent cbc3827 commit 7a24ed3

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

lightning/src/util/transaction_utils.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ pub(crate) fn maybe_add_change_output(tx: &mut Transaction, input_value: u64, wi
5050
value: 0,
5151
};
5252
let change_len = change_output.consensus_encode(&mut sink()).unwrap();
53-
let mut weight_with_change: i64 = tx.get_weight() as i64 + WITNESS_FLAG_BYTES + witness_max_weight as i64 + change_len as i64 * 4;
53+
let starting_weight = tx.get_weight() + WITNESS_FLAG_BYTES as usize + witness_max_weight;
54+
let mut weight_with_change: i64 = starting_weight as i64 + change_len as i64 * 4;
5455
// Include any extra bytes required to push an extra output.
5556
weight_with_change += (VarInt(tx.output.len() as u64 + 1).len() - VarInt(tx.output.len() as u64).len()) as i64 * 4;
5657
// When calculating weight, add two for the flag bytes
@@ -59,10 +60,10 @@ pub(crate) fn maybe_add_change_output(tx: &mut Transaction, input_value: u64, wi
5960
change_output.value = change_value as u64;
6061
tx.output.push(change_output);
6162
Ok(weight_with_change as usize)
62-
} else if (input_value - output_value) as i64 - (tx.get_weight() as i64 + 2 + witness_max_weight as i64) * feerate_sat_per_1000_weight as i64 / 1000 < 0 {
63+
} else if (input_value - output_value) as i64 - (starting_weight as i64) * feerate_sat_per_1000_weight as i64 / 1000 < 0 {
6364
Err(())
6465
} else {
65-
Ok(tx.get_weight() + WITNESS_FLAG_BYTES as usize + witness_max_weight)
66+
Ok(starting_weight)
6667
}
6768
}
6869

0 commit comments

Comments
 (0)