Skip to content

Commit 9ea2e79

Browse files
committed
f use const for witness flag bytes
1 parent 044afd9 commit 9ea2e79

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

lightning/src/util/transaction_utils.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ pub fn sort_outputs<T, C : Fn(&T, &T) -> Ordering>(outputs: &mut Vec<(TxOut, T)>
3636
pub(crate) fn maybe_add_change_output(tx: &mut Transaction, input_value: u64, witness_max_weight: usize, feerate_sat_per_1000_weight: u32, change_destination_script: Script) -> Result<usize, ()> {
3737
if input_value > MAX_VALUE_MSAT / 1000 { return Err(()); }
3838

39+
let const WITNESS_FLAG_BYTES: i64 = 2;
40+
3941
let mut output_value = 0;
4042
for output in tx.output.iter() {
4143
output_value += output.value;
@@ -48,7 +50,7 @@ pub(crate) fn maybe_add_change_output(tx: &mut Transaction, input_value: u64, wi
4850
value: 0,
4951
};
5052
let change_len = change_output.consensus_encode(&mut sink()).unwrap();
51-
let mut weight_with_change: i64 = tx.get_weight() as i64 + 2 + witness_max_weight as i64 + change_len as i64 * 4;
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;
5254
// Include any extra bytes required to push an extra output.
5355
weight_with_change += (VarInt(tx.output.len() as u64 + 1).len() - VarInt(tx.output.len() as u64).len()) as i64 * 4;
5456
// When calculating weight, add two for the flag bytes
@@ -60,7 +62,7 @@ pub(crate) fn maybe_add_change_output(tx: &mut Transaction, input_value: u64, wi
6062
} 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 {
6163
Err(())
6264
} else {
63-
Ok(tx.get_weight() + 2 + witness_max_weight)
65+
Ok(tx.get_weight() + WITNESS_FLAG_BYTES as usize + witness_max_weight)
6466
}
6567
}
6668

0 commit comments

Comments
 (0)