Skip to content

Commit 1cc2f53

Browse files
committed
Fix weight computation for funding input (multisig)
1 parent 376b5dd commit 1cc2f53

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

lightning/src/ln/chan_utils.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,27 @@ pub const HTLC_TIMEOUT_INPUT_ANCHOR_WITNESS_WEIGHT: u64 = 288;
8181
/// outputs.
8282
pub const HTLC_SUCCESS_INPUT_ANCHOR_WITNESS_WEIGHT: u64 = 327;
8383

84+
/// The size of the 2-of-2 multisig script
85+
const MULTISIG_SCRIPT_SIZE: u64 =
86+
1 + // OP_2
87+
1 + // data len
88+
33 + // pubkey1
89+
1 + // data len
90+
33 + // pubkey2
91+
1 + // OP_2
92+
1; // OP_CHECKMULTISIG
93+
/// The weight of a funding transaction input (2-of-2 P2WSH)
94+
/// See https://github.com/lightning/bolts/blob/master/03-transactions.md#expected-weight-of-the-commitment-transaction
95+
pub const FUNDING_TRANSACTION_WITNESS_WEIGHT: u64 =
96+
1 + // number_of_witness_elements
97+
1 + // nil_len
98+
1 + // sig len
99+
73 + // sig1
100+
1 + // sig len
101+
73 + // sig2
102+
1 + // witness_script_length
103+
MULTISIG_SCRIPT_SIZE;
104+
84105
/// Gets the weight for an HTLC-Success transaction.
85106
#[inline]
86107
pub fn htlc_success_tx_weight(channel_type_features: &ChannelTypeFeatures) -> u64 {

lightning/src/ln/channel.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ use crate::ln::chan_utils::{
4747
get_commitment_transaction_number_obscure_factor,
4848
ClosingTransaction, commit_tx_fee_sat,
4949
};
50+
#[cfg(splicing)]
51+
use crate::ln::chan_utils::FUNDING_TRANSACTION_WITNESS_WEIGHT;
5052
use crate::ln::chan_utils;
5153
use crate::ln::onion_utils::HTLCFailReason;
5254
use crate::chain::BestBlock;
@@ -55,8 +57,6 @@ use crate::chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, Channel
5557
use crate::chain::transaction::{OutPoint, TransactionData};
5658
use crate::sign::ecdsa::EcdsaChannelSigner;
5759
use crate::sign::{EntropySource, ChannelSigner, SignerProvider, NodeSigner, Recipient};
58-
#[cfg(splicing)]
59-
use crate::sign::P2WPKH_WITNESS_WEIGHT;
6060
use crate::events::{ClosureReason, Event};
6161
use crate::events::bump_transaction::BASE_INPUT_WEIGHT;
6262
use crate::routing::gossip::NodeId;
@@ -8443,8 +8443,7 @@ impl<SP: Deref> FundedChannel<SP> where
84438443
let funding_input_count = our_funding_inputs.len() + 1;
84448444
// Input witness weight, extended with weight for spending old funding
84458445
let total_witness_weight = Weight::from_wu(
8446-
witness_weight.to_wu()
8447-
.saturating_add(2 * P2WPKH_WITNESS_WEIGHT)
8446+
witness_weight.to_wu().saturating_add(FUNDING_TRANSACTION_WITNESS_WEIGHT)
84488447
);
84498448
let estimated_fee = estimate_v2_funding_transaction_fee(true, funding_input_count, total_witness_weight, funding_feerate_per_kw);
84508449
let available_input = sum_input.saturating_sub(estimated_fee);

0 commit comments

Comments
 (0)