Skip to content

Commit 75b7e80

Browse files
committed
Account for shared input EMPTY_SCRIPT_SIG_WEIGHT
When splicing a channel, the previous funding output is spent and fees for it are paid by the splice initiator. However, the witness weight was not including EMPTY_SCRIPT_SIG_WEIGHT. Fix this and update the variable name to make clear the weight needed is the input satisfaction.
1 parent 3327382 commit 75b7e80

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

lightning/src/ln/channel.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ use crate::chain::channelmonitor::{
3838
use crate::chain::transaction::{OutPoint, TransactionData};
3939
use crate::chain::BestBlock;
4040
use crate::events::bump_transaction::BASE_INPUT_WEIGHT;
41+
#[cfg(splicing)]
42+
use crate::events::bump_transaction::EMPTY_SCRIPT_SIG_WEIGHT;
4143
use crate::events::ClosureReason;
4244
use crate::ln::chan_utils;
4345
#[cfg(splicing)]
@@ -5879,18 +5881,18 @@ fn get_v2_channel_reserve_satoshis(channel_value_satoshis: u64, dust_limit_satos
58795881

58805882
/// Estimate our part of the fee of the new funding transaction.
58815883
/// input_count: Number of contributed inputs.
5882-
/// witness_weight: The witness weight for contributed inputs.
5884+
/// input_satisfaction_weight: The satisfaction weight for contributed inputs.
58835885
#[allow(dead_code)] // TODO(dual_funding): TODO(splicing): Remove allow once used.
58845886
#[rustfmt::skip]
58855887
fn estimate_v2_funding_transaction_fee(
5886-
is_initiator: bool, input_count: usize, witness_weight: Weight,
5888+
is_initiator: bool, input_count: usize, input_satisfaction_weight: Weight,
58875889
funding_feerate_sat_per_1000_weight: u32,
58885890
) -> u64 {
58895891
// Inputs
58905892
let mut weight = (input_count as u64) * BASE_INPUT_WEIGHT;
58915893

58925894
// Witnesses
5893-
weight = weight.saturating_add(witness_weight.to_wu());
5895+
weight = weight.saturating_add(input_satisfaction_weight.to_wu());
58945896

58955897
// If we are the initiator, we must pay for weight of all common fields in the funding transaction.
58965898
if is_initiator {
@@ -5919,14 +5921,15 @@ fn check_v2_funding_inputs_sufficient(
59195921
contribution_amount: i64, funding_inputs: &[(TxIn, Transaction, Weight)], is_initiator: bool,
59205922
is_splice: bool, funding_feerate_sat_per_1000_weight: u32,
59215923
) -> Result<u64, ChannelError> {
5922-
let mut total_input_witness_weight = Weight::from_wu(funding_inputs.iter().map(|(_, _, w)| w.to_wu()).sum());
5924+
let mut total_input_satisfaction_weight = Weight::from_wu(funding_inputs.iter().map(|(_, _, w)| w.to_wu()).sum());
59235925
let mut funding_inputs_len = funding_inputs.len();
59245926
if is_initiator && is_splice {
59255927
// consider the weight of the input and witness needed for spending the old funding transaction
59265928
funding_inputs_len += 1;
5927-
total_input_witness_weight += Weight::from_wu(FUNDING_TRANSACTION_WITNESS_WEIGHT);
5929+
total_input_satisfaction_weight +=
5930+
Weight::from_wu(EMPTY_SCRIPT_SIG_WEIGHT + FUNDING_TRANSACTION_WITNESS_WEIGHT);
59285931
}
5929-
let estimated_fee = estimate_v2_funding_transaction_fee(is_initiator, funding_inputs_len, total_input_witness_weight, funding_feerate_sat_per_1000_weight);
5932+
let estimated_fee = estimate_v2_funding_transaction_fee(is_initiator, funding_inputs_len, total_input_satisfaction_weight, funding_feerate_sat_per_1000_weight);
59305933

59315934
let mut total_input_sats = 0u64;
59325935
for (idx, input) in funding_inputs.iter().enumerate() {
@@ -15931,7 +15934,7 @@ mod tests {
1593115934
true,
1593215935
2000,
1593315936
).unwrap(),
15934-
2268,
15937+
2276,
1593515938
);
1593615939

1593715940
// negative case, inputs clearly insufficient
@@ -15947,13 +15950,13 @@ mod tests {
1594715950
);
1594815951
assert_eq!(
1594915952
format!("{:?}", res.err().unwrap()),
15950-
"Warn: Total input amount 100000 is lower than needed for contribution 220000, considering fees of 1730. Need more inputs.",
15953+
"Warn: Total input amount 100000 is lower than needed for contribution 220000, considering fees of 1738. Need more inputs.",
1595115954
);
1595215955
}
1595315956

1595415957
// barely covers
1595515958
{
15956-
let expected_fee: u64 = 2268;
15959+
let expected_fee: u64 = 2276;
1595715960
assert_eq!(
1595815961
check_v2_funding_inputs_sufficient(
1595915962
(300_000 - expected_fee - 20) as i64,
@@ -15983,7 +15986,7 @@ mod tests {
1598315986
);
1598415987
assert_eq!(
1598515988
format!("{:?}", res.err().unwrap()),
15986-
"Warn: Total input amount 300000 is lower than needed for contribution 298032, considering fees of 2495. Need more inputs.",
15989+
"Warn: Total input amount 300000 is lower than needed for contribution 298032, considering fees of 2504. Need more inputs.",
1598715990
);
1598815991
}
1598915992

0 commit comments

Comments
 (0)