Skip to content

Commit 376b5dd

Browse files
committed
Include input weight in splice_channel; test util to return weight as well
1 parent 9426b37 commit 376b5dd

File tree

5 files changed

+39
-17
lines changed

5 files changed

+39
-17
lines changed

lightning/src/ln/channel.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ use crate::chain::chaininterface::{FeeEstimator, ConfirmationTarget, LowerBounde
5454
use crate::chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateStep, LATENCY_GRACE_PERIOD_BLOCKS};
5555
use crate::chain::transaction::{OutPoint, TransactionData};
5656
use crate::sign::ecdsa::EcdsaChannelSigner;
57-
use crate::sign::{EntropySource, ChannelSigner, SignerProvider, NodeSigner, Recipient, P2WPKH_WITNESS_WEIGHT};
57+
use crate::sign::{EntropySource, ChannelSigner, SignerProvider, NodeSigner, Recipient};
58+
#[cfg(splicing)]
59+
use crate::sign::P2WPKH_WITNESS_WEIGHT;
5860
use crate::events::{ClosureReason, Event};
5961
use crate::events::bump_transaction::BASE_INPUT_WEIGHT;
6062
use crate::routing::gossip::NodeId;
@@ -8391,10 +8393,12 @@ impl<SP: Deref> FundedChannel<SP> where
83918393
}
83928394
}
83938395

8394-
/// Initiate splicing
8396+
/// Initiate splicing.
8397+
/// - witness_weight: The witness weight for contributed inputs.
83958398
#[cfg(splicing)]
83968399
pub fn splice_channel(&mut self, our_funding_contribution_satoshis: i64,
8397-
our_funding_inputs: Vec<(TxIn, Transaction)>, funding_feerate_per_kw: u32, locktime: u32,
8400+
our_funding_inputs: Vec<(TxIn, Transaction)>, witness_weight: Weight,
8401+
funding_feerate_per_kw: u32, locktime: u32,
83988402
) -> Result<msgs::SpliceInit, ChannelError> {
83998403
// Check if a splice has been initiated already.
84008404
// Note: only a single outstanding splice is supported (per spec)
@@ -8437,10 +8441,10 @@ impl<SP: Deref> FundedChannel<SP> where
84378441

84388442
// The +1 is to include the input of the old funding
84398443
let funding_input_count = our_funding_inputs.len() + 1;
8440-
// Add weight for inputs (estimated as P2WPKH) *and* spending old funding
8444+
// Input witness weight, extended with weight for spending old funding
84418445
let total_witness_weight = Weight::from_wu(
8442-
our_funding_inputs.len() as u64 * P2WPKH_WITNESS_WEIGHT +
8443-
2 * P2WPKH_WITNESS_WEIGHT
8446+
witness_weight.to_wu()
8447+
.saturating_add(2 * P2WPKH_WITNESS_WEIGHT)
84448448
);
84458449
let estimated_fee = estimate_v2_funding_transaction_fee(true, funding_input_count, total_witness_weight, funding_feerate_per_kw);
84468450
let available_input = sum_input.saturating_sub(estimated_fee);

lightning/src/ln/channelmanager.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use bitcoin::hash_types::{BlockHash, Txid};
3030

3131
use bitcoin::secp256k1::{SecretKey,PublicKey};
3232
use bitcoin::secp256k1::Secp256k1;
33-
use bitcoin::{secp256k1, Sequence, TxIn};
33+
use bitcoin::{secp256k1, Sequence, TxIn, Weight};
3434

3535
use crate::events::FundingInfo;
3636
use crate::blinded_path::message::{AsyncPaymentsContext, MessageContext, OffersContext};
@@ -4285,10 +4285,12 @@ where
42854285
/// Note: Currently only splice-in is supported (increase in channel capacity), splice-out is not.
42864286
/// - our_funding_contribution_satoshis: the amount contributed by us to the channel. This will increase our channel balance.
42874287
/// - our_funding_inputs: the funding inputs provided by us. If our contribution is positive, our funding inputs must cover at least that amount.
4288+
/// - witness_weight: The witness weight for contributed inputs.
42884289
#[cfg(splicing)]
42894290
pub fn splice_channel(
42904291
&self, channel_id: &ChannelId, counterparty_node_id: &PublicKey, our_funding_contribution_satoshis: i64,
4291-
our_funding_inputs: Vec<(TxIn, Transaction)>, funding_feerate_per_kw: u32, locktime: u32,
4292+
our_funding_inputs: Vec<(TxIn, Transaction)>, witness_weight: Weight,
4293+
funding_feerate_per_kw: u32, locktime: u32,
42924294
) -> Result<(), APIError> {
42934295
let per_peer_state = self.per_peer_state.read().unwrap();
42944296

@@ -4302,7 +4304,7 @@ where
43024304
match peer_state.channel_by_id.entry(*channel_id) {
43034305
hash_map::Entry::Occupied(mut chan_phase_entry) => {
43044306
if let Some(chan) = chan_phase_entry.get_mut().as_funded_mut() {
4305-
let msg = chan.splice_channel(our_funding_contribution_satoshis, our_funding_inputs, funding_feerate_per_kw, locktime)
4307+
let msg = chan.splice_channel(our_funding_contribution_satoshis, our_funding_inputs, witness_weight, funding_feerate_per_kw, locktime)
43064308
.map_err(|err| APIError::APIMisuseError {
43074309
err: format!(
43084310
"Cannot initiate Splicing, {}, channel ID {}", err, channel_id

lightning/src/ln/dual_funding_tests.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ fn do_test_v2_channel_establishment(
4949
let logger_a = test_utils::TestLogger::with_id("node a".to_owned());
5050

5151
// Create a funding input for the new channel along with its previous transaction.
52-
let initiator_funding_inputs: Vec<_> = create_dual_funding_utxos_with_prev_txs(
52+
let (initiator_funding_inputs, total_weight) = create_dual_funding_utxos_with_prev_txs(
5353
&nodes[0],
5454
&[session.initiator_input_value_satoshis],
5555
)
@@ -58,7 +58,17 @@ fn do_test_v2_channel_establishment(
5858
.collect();
5959

6060
// Alice creates a dual-funded channel as initiator.
61-
let funding_satoshis = session.funding_input_sats;
61+
let funding_feerate = node_cfgs[0]
62+
.fee_estimator
63+
.get_est_sat_per_1000_weight(ConfirmationTarget::NonAnchorChannelFee);
64+
let funding_satoshis = calculate_our_funding_satoshis(
65+
true,
66+
&initiator_funding_inputs[..],
67+
total_weight,
68+
funding_feerate,
69+
MIN_CHAN_DUST_LIMIT_SATOSHIS,
70+
)
71+
.unwrap();
6272
let mut channel = PendingV2Channel::new_outbound(
6373
&LowerBoundedFeeEstimator(node_cfgs[0].fee_estimator),
6474
&nodes[0].node.entropy_source,

lightning/src/ln/functional_test_utils.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use crate::util::test_utils;
3838
use crate::util::test_utils::{TestChainMonitor, TestScorer, TestKeysInterface};
3939
use crate::util::ser::{ReadableArgs, Writeable};
4040

41-
use bitcoin::WPubkeyHash;
41+
use bitcoin::{Weight, WPubkeyHash};
4242
use bitcoin::amount::Amount;
4343
use bitcoin::block::{Block, Header, Version as BlockVersion};
4444
use bitcoin::locktime::absolute::{LockTime, LOCK_TIME_THRESHOLD};
@@ -60,6 +60,7 @@ use core::mem;
6060
use core::ops::Deref;
6161
use crate::io;
6262
use crate::prelude::*;
63+
use crate::sign::P2WPKH_WITNESS_WEIGHT;
6364
use crate::sync::{Arc, Mutex, LockTestExt, RwLock};
6465

6566
pub const CHAN_CONFIRM_DEPTH: u32 = 10;
@@ -1236,9 +1237,11 @@ fn internal_create_funding_transaction<'a, 'b, 'c>(node: &Node<'a, 'b, 'c>,
12361237
}
12371238
}
12381239

1240+
/// Create test inputs for a funding transaction.
1241+
/// Return the inputs (with prev tx), and the total witness weight for these inputs
12391242
pub fn create_dual_funding_utxos_with_prev_txs(
12401243
node: &Node<'_, '_, '_>, utxo_values_in_satoshis: &[u64],
1241-
) -> Vec<(TxIn, Transaction)> {
1244+
) -> (Vec<(TxIn, Transaction)>, Weight) {
12421245
// Ensure we have unique transactions per node by using the locktime.
12431246
let tx = Transaction {
12441247
version: TxVersion::TWO,
@@ -1251,9 +1254,9 @@ pub fn create_dual_funding_utxos_with_prev_txs(
12511254
}).collect()
12521255
};
12531256

1254-
let mut result = vec![];
1257+
let mut inputs = vec![];
12551258
for i in 0..utxo_values_in_satoshis.len() {
1256-
result.push(
1259+
inputs.push(
12571260
(TxIn {
12581261
previous_output: OutPoint {
12591262
txid: tx.compute_txid(),
@@ -1264,7 +1267,9 @@ pub fn create_dual_funding_utxos_with_prev_txs(
12641267
witness: Witness::new(),
12651268
}, tx.clone()));
12661269
}
1267-
result
1270+
let total_weight = Weight::from_wu(utxo_values_in_satoshis.len() as u64 * P2WPKH_WITNESS_WEIGHT);
1271+
1272+
(inputs, total_weight)
12681273
}
12691274

12701275
pub fn sign_funding_transaction<'a, 'b, 'c>(node_a: &Node<'a, 'b, 'c>, node_b: &Node<'a, 'b, 'c>, channel_value: u64, expected_temporary_channel_id: ChannelId) -> Transaction {

lightning/src/ln/functional_tests_splice.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ fn test_v1_splice_in() {
231231

232232
// Create additional inputs
233233
let extra_splice_funding_input_sats = 35_000;
234-
let funding_inputs = create_dual_funding_utxos_with_prev_txs(
234+
let (funding_inputs, total_weight) = create_dual_funding_utxos_with_prev_txs(
235235
&initiator_node,
236236
&[extra_splice_funding_input_sats],
237237
);
@@ -243,6 +243,7 @@ fn test_v1_splice_in() {
243243
&acceptor_node.node.get_our_node_id(),
244244
splice_in_sats as i64,
245245
funding_inputs,
246+
total_weight,
246247
funding_feerate_per_kw,
247248
locktime,
248249
)

0 commit comments

Comments
 (0)