Skip to content

Commit 2cc49bc

Browse files
committed
Include input weight in splice_channel; test util to return weight as well
1 parent 8b5dcd2 commit 2cc49bc

File tree

5 files changed

+28
-16
lines changed

5 files changed

+28
-16
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;
@@ -8128,10 +8130,12 @@ impl<SP: Deref> FundedChannel<SP> where
81288130
}
81298131
}
81308132

8131-
/// Initiate splicing
8133+
/// Initiate splicing.
8134+
/// - witness_weight: The witness weight for contributed inputs.
81328135
#[cfg(splicing)]
81338136
pub fn splice_channel(&mut self, our_funding_contribution_satoshis: i64,
8134-
our_funding_inputs: Vec<(TxIn, Transaction)>, funding_feerate_per_kw: u32, locktime: u32,
8137+
our_funding_inputs: Vec<(TxIn, Transaction)>, witness_weight: Weight,
8138+
funding_feerate_per_kw: u32, locktime: u32,
81358139
) -> Result<msgs::SpliceInit, ChannelError> {
81368140
// Check if a splice has been initiated already.
81378141
// Note: only a single outstanding splice is supported (per spec)
@@ -8174,10 +8178,10 @@ impl<SP: Deref> FundedChannel<SP> where
81748178

81758179
// The +1 is to include the input of the old funding
81768180
let funding_input_count = our_funding_inputs.len() + 1;
8177-
// Add weight for inputs (estimated as P2WPKH) *and* spending old funding
8181+
// Input witness weight, extended with weight for spending old funding
81788182
let total_witness_weight = Weight::from_wu(
8179-
our_funding_inputs.len() as u64 * P2WPKH_WITNESS_WEIGHT +
8180-
2 * P2WPKH_WITNESS_WEIGHT
8183+
witness_weight.to_wu()
8184+
.saturating_add(2 * P2WPKH_WITNESS_WEIGHT)
81818185
);
81828186
let estimated_fee = estimate_funding_transaction_fee(true, funding_input_count, total_witness_weight, funding_feerate_per_kw);
81838187
let available_input = sum_input.saturating_sub(estimated_fee);

lightning/src/ln/channelmanager.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4213,10 +4213,12 @@ where
42134213
/// Note: Currently only splice-in is supported (increase in channel capacity), splice-out is not.
42144214
/// - our_funding_contribution_satoshis: the amount contributed by us to the channel. This will increase our channel balance.
42154215
/// - our_funding_inputs: the funding inputs provided by us. If our contribution is positive, our funding inputs must cover at least that amount.
4216+
/// - witness_weight: The witness weight for contributed inputs.
42164217
#[cfg(splicing)]
42174218
pub fn splice_channel(
42184219
&self, channel_id: &ChannelId, counterparty_node_id: &PublicKey, our_funding_contribution_satoshis: i64,
4219-
our_funding_inputs: Vec<(TxIn, Transaction)>, funding_feerate_per_kw: u32, locktime: u32,
4220+
our_funding_inputs: Vec<(TxIn, Transaction)>, witness_weight: Weight,
4221+
funding_feerate_per_kw: u32, locktime: u32,
42204222
) -> Result<(), APIError> {
42214223
let per_peer_state = self.per_peer_state.read().unwrap();
42224224

@@ -4230,7 +4232,7 @@ where
42304232
match peer_state.channel_by_id.entry(*channel_id) {
42314233
hash_map::Entry::Occupied(mut chan_phase_entry) => {
42324234
if let Some(chan) = chan_phase_entry.get_mut().as_funded_mut() {
4233-
let msg = chan.splice_channel(our_funding_contribution_satoshis, our_funding_inputs, funding_feerate_per_kw, locktime)
4235+
let msg = chan.splice_channel(our_funding_contribution_satoshis, our_funding_inputs, witness_weight, funding_feerate_per_kw, locktime)
42344236
.map_err(|err| APIError::APIMisuseError {
42354237
err: format!(
42364238
"Cannot initiate Splicing, {}, channel ID {}", err, channel_id

lightning/src/ln/dual_funding_tests.rs

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

5353
// Create a funding input for the new channel along with its previous transaction.
54-
let initiator_funding_inputs: Vec<_> = create_dual_funding_utxos_with_prev_txs(
54+
let (initiator_funding_inputs, total_weight) = create_dual_funding_utxos_with_prev_txs(
5555
&nodes[0],
5656
&[session.initiator_input_value_satoshis],
5757
)
@@ -66,7 +66,7 @@ fn do_test_v2_channel_establishment(
6666
let funding_satoshis = calculate_our_funding_satoshis(
6767
true,
6868
&initiator_funding_inputs[..],
69-
Weight::from_wu(P2WPKH_WITNESS_WEIGHT),
69+
total_weight,
7070
funding_feerate,
7171
MIN_CHAN_DUST_LIMIT_SATOSHIS,
7272
)

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;
@@ -1243,9 +1244,11 @@ fn internal_create_funding_transaction<'a, 'b, 'c>(node: &Node<'a, 'b, 'c>,
12431244
}
12441245
}
12451246

1247+
/// Create test inputs for a funding transaction.
1248+
/// Return the inputs (with prev tx), and the total witness weight for these inputs
12461249
pub fn create_dual_funding_utxos_with_prev_txs(
12471250
node: &Node<'_, '_, '_>, utxo_values_in_satoshis: &[u64],
1248-
) -> Vec<(TxIn, Transaction)> {
1251+
) -> (Vec<(TxIn, Transaction)>, Weight) {
12491252
// Ensure we have unique transactions per node by using the locktime.
12501253
let tx = Transaction {
12511254
version: TxVersion::TWO,
@@ -1258,9 +1261,9 @@ pub fn create_dual_funding_utxos_with_prev_txs(
12581261
}).collect()
12591262
};
12601263

1261-
let mut result = vec![];
1264+
let mut inputs = vec![];
12621265
for i in 0..utxo_values_in_satoshis.len() {
1263-
result.push(
1266+
inputs.push(
12641267
(TxIn {
12651268
previous_output: OutPoint {
12661269
txid: tx.compute_txid(),
@@ -1271,7 +1274,9 @@ pub fn create_dual_funding_utxos_with_prev_txs(
12711274
witness: Witness::new(),
12721275
}, tx.clone()));
12731276
}
1274-
result
1277+
let total_weight = Weight::from_wu(utxo_values_in_satoshis.len() as u64 * P2WPKH_WITNESS_WEIGHT);
1278+
1279+
(inputs, total_weight)
12751280
}
12761281

12771282
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)