Skip to content

Commit 776183e

Browse files
committed
Include input weight in splice_channel; test util to return weight as well
1 parent d07a289 commit 776183e

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;
@@ -8216,10 +8218,12 @@ impl<SP: Deref> FundedChannel<SP> where
82168218
}
82178219
}
82188220

8219-
/// Initiate splicing
8221+
/// Initiate splicing.
8222+
/// - witness_weight: The witness weight for contributed inputs.
82208223
#[cfg(splicing)]
82218224
pub fn splice_channel(&mut self, our_funding_contribution_satoshis: i64,
8222-
our_funding_inputs: Vec<(TxIn, Transaction)>, funding_feerate_per_kw: u32, locktime: u32,
8225+
our_funding_inputs: Vec<(TxIn, Transaction)>, witness_weight: Weight,
8226+
funding_feerate_per_kw: u32, locktime: u32,
82238227
) -> Result<msgs::SpliceInit, ChannelError> {
82248228
// Check if a splice has been initiated already.
82258229
// Note: only a single outstanding splice is supported (per spec)
@@ -8262,10 +8266,10 @@ impl<SP: Deref> FundedChannel<SP> where
82628266

82638267
// The +1 is to include the input of the old funding
82648268
let funding_input_count = our_funding_inputs.len() + 1;
8265-
// Add weight for inputs (estimated as P2WPKH) *and* spending old funding
8269+
// Input witness weight, extended with weight for spending old funding
82668270
let total_witness_weight = Weight::from_wu(
8267-
our_funding_inputs.len() as u64 * P2WPKH_WITNESS_WEIGHT +
8268-
2 * P2WPKH_WITNESS_WEIGHT
8271+
witness_weight.to_wu()
8272+
.saturating_add(2 * P2WPKH_WITNESS_WEIGHT)
82698273
);
82708274
let estimated_fee = estimate_funding_transaction_fee(true, funding_input_count, total_witness_weight, funding_feerate_per_kw);
82718275
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
@@ -4238,10 +4238,12 @@ where
42384238
/// Note: Currently only splice-in is supported (increase in channel capacity), splice-out is not.
42394239
/// - our_funding_contribution_satoshis: the amount contributed by us to the channel. This will increase our channel balance.
42404240
/// - our_funding_inputs: the funding inputs provided by us. If our contribution is positive, our funding inputs must cover at least that amount.
4241+
/// - witness_weight: The witness weight for contributed inputs.
42414242
#[cfg(splicing)]
42424243
pub fn splice_channel(
42434244
&self, channel_id: &ChannelId, counterparty_node_id: &PublicKey, our_funding_contribution_satoshis: i64,
4244-
our_funding_inputs: Vec<(TxIn, Transaction)>, funding_feerate_per_kw: u32, locktime: u32,
4245+
our_funding_inputs: Vec<(TxIn, Transaction)>, witness_weight: Weight,
4246+
funding_feerate_per_kw: u32, locktime: u32,
42454247
) -> Result<(), APIError> {
42464248
let per_peer_state = self.per_peer_state.read().unwrap();
42474249

@@ -4255,7 +4257,7 @@ where
42554257
match peer_state.channel_by_id.entry(*channel_id) {
42564258
hash_map::Entry::Occupied(mut chan_phase_entry) => {
42574259
if let Some(chan) = chan_phase_entry.get_mut().as_funded_mut() {
4258-
let msg = chan.splice_channel(our_funding_contribution_satoshis, our_funding_inputs, funding_feerate_per_kw, locktime)
4260+
let msg = chan.splice_channel(our_funding_contribution_satoshis, our_funding_inputs, witness_weight, funding_feerate_per_kw, locktime)
42594261
.map_err(|err| APIError::APIMisuseError {
42604262
err: format!(
42614263
"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;
@@ -1245,9 +1246,11 @@ fn internal_create_funding_transaction<'a, 'b, 'c>(node: &Node<'a, 'b, 'c>,
12451246
}
12461247
}
12471248

1249+
/// Create test inputs for a funding transaction.
1250+
/// Return the inputs (with prev tx), and the total witness weight for these inputs
12481251
pub fn create_dual_funding_utxos_with_prev_txs(
12491252
node: &Node<'_, '_, '_>, utxo_values_in_satoshis: &[u64],
1250-
) -> Vec<(TxIn, Transaction)> {
1253+
) -> (Vec<(TxIn, Transaction)>, Weight) {
12511254
// Ensure we have unique transactions per node by using the locktime.
12521255
let tx = Transaction {
12531256
version: TxVersion::TWO,
@@ -1260,9 +1263,9 @@ pub fn create_dual_funding_utxos_with_prev_txs(
12601263
}).collect()
12611264
};
12621265

1263-
let mut result = vec![];
1266+
let mut inputs = vec![];
12641267
for i in 0..utxo_values_in_satoshis.len() {
1265-
result.push(
1268+
inputs.push(
12661269
(TxIn {
12671270
previous_output: OutPoint {
12681271
txid: tx.compute_txid(),
@@ -1273,7 +1276,9 @@ pub fn create_dual_funding_utxos_with_prev_txs(
12731276
witness: Witness::new(),
12741277
}, tx.clone()));
12751278
}
1276-
result
1279+
let total_weight = Weight::from_wu(utxo_values_in_satoshis.len() as u64 * P2WPKH_WITNESS_WEIGHT);
1280+
1281+
(inputs, total_weight)
12771282
}
12781283

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