Skip to content

Commit b99da94

Browse files
committed
Include witness weights in FundingNegotiationContext
ChannelManager::splice_channel takes witness weights with the funding inputs. Storing these in FundingNegotiationContext allows us to use them when calculating the change output and include them in a common struct used for initiating a splice-in. In preparation for having ChannelManager::splice_channel take FundingTxContributions, add a weight to the FundingTxContributions::InputsOnly, which supports the splice-in use case.
1 parent 229086d commit b99da94

File tree

3 files changed

+19
-18
lines changed

3 files changed

+19
-18
lines changed

lightning/src/ln/channel.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5979,7 +5979,7 @@ pub(super) struct FundingNegotiationContext {
59795979
pub shared_funding_input: Option<SharedOwnedInput>,
59805980
/// The funding inputs we will be contributing to the channel.
59815981
#[allow(dead_code)] // TODO(dual_funding): Remove once contribution to V2 channels is enabled.
5982-
pub our_funding_inputs: Vec<(TxIn, Transaction)>,
5982+
pub our_funding_inputs: Vec<(TxIn, Transaction, Weight)>,
59835983
/// The change output script. This will be used if needed or -- if not set -- generated using
59845984
/// `SignerProvider::get_destination_script`.
59855985
#[allow(dead_code)] // TODO(splicing): Remove once splicing is enabled.
@@ -6051,6 +6051,9 @@ impl FundingNegotiationContext {
60516051
}
60526052
}
60536053

6054+
let funding_inputs =
6055+
self.our_funding_inputs.into_iter().map(|(txin, tx, _)| (txin, tx)).collect();
6056+
60546057
let constructor_args = InteractiveTxConstructorArgs {
60556058
entropy_source,
60566059
holder_node_id,
@@ -6059,7 +6062,7 @@ impl FundingNegotiationContext {
60596062
feerate_sat_per_kw: self.funding_feerate_sat_per_1000_weight,
60606063
is_initiator: self.is_initiator,
60616064
funding_tx_locktime: self.funding_tx_locktime,
6062-
inputs_to_contribute: self.our_funding_inputs,
6065+
inputs_to_contribute: funding_inputs,
60636066
shared_funding_input: self.shared_funding_input,
60646067
shared_funding_output: SharedOwnedOutput::new(
60656068
shared_funding_output,
@@ -10676,9 +10679,8 @@ where
1067610679
err,
1067710680
),
1067810681
})?;
10679-
// Convert inputs
10680-
let mut funding_inputs = Vec::new();
10681-
for (txin, tx, _) in our_funding_inputs.into_iter() {
10682+
10683+
for (_, tx, _) in our_funding_inputs.iter() {
1068210684
const MESSAGE_TEMPLATE: msgs::TxAddInput = msgs::TxAddInput {
1068310685
channel_id: ChannelId([0; 32]),
1068410686
serial_id: 0,
@@ -10696,8 +10698,6 @@ where
1069610698
),
1069710699
});
1069810700
}
10699-
10700-
funding_inputs.push((txin, tx));
1070110701
}
1070210702

1070310703
let prev_funding_input = self.funding.to_splice_funding_input();
@@ -10707,7 +10707,7 @@ where
1070710707
funding_tx_locktime: LockTime::from_consensus(locktime),
1070810708
funding_feerate_sat_per_1000_weight: funding_feerate_per_kw,
1070910709
shared_funding_input: Some(prev_funding_input),
10710-
our_funding_inputs: funding_inputs,
10710+
our_funding_inputs,
1071110711
change_script,
1071210712
};
1071310713

@@ -12474,7 +12474,7 @@ where
1247412474
pub fn new_outbound<ES: Deref, F: Deref, L: Deref>(
1247512475
fee_estimator: &LowerBoundedFeeEstimator<F>, entropy_source: &ES, signer_provider: &SP,
1247612476
counterparty_node_id: PublicKey, their_features: &InitFeatures, funding_satoshis: u64,
12477-
funding_inputs: Vec<(TxIn, Transaction)>, user_id: u128, config: &UserConfig,
12477+
funding_inputs: Vec<(TxIn, Transaction, Weight)>, user_id: u128, config: &UserConfig,
1247812478
current_chain_height: u32, outbound_scid_alias: u64, funding_confirmation_target: ConfirmationTarget,
1247912479
logger: L,
1248012480
) -> Result<Self, APIError>
@@ -12688,6 +12688,8 @@ where
1268812688
value: Amount::from_sat(funding.get_value_satoshis()),
1268912689
script_pubkey: funding.get_funding_redeemscript().to_p2wsh(),
1269012690
};
12691+
let inputs_to_contribute =
12692+
our_funding_inputs.into_iter().map(|(txin, tx, _)| (txin, tx)).collect();
1269112693

1269212694
let interactive_tx_constructor = Some(InteractiveTxConstructor::new(
1269312695
InteractiveTxConstructorArgs {
@@ -12698,7 +12700,7 @@ where
1269812700
feerate_sat_per_kw: funding_negotiation_context.funding_feerate_sat_per_1000_weight,
1269912701
funding_tx_locktime: funding_negotiation_context.funding_tx_locktime,
1270012702
is_initiator: false,
12701-
inputs_to_contribute: our_funding_inputs,
12703+
inputs_to_contribute,
1270212704
shared_funding_input: None,
1270312705
shared_funding_output: SharedOwnedOutput::new(shared_funding_output, our_funding_contribution_sats),
1270412706
outputs_to_contribute: Vec::new(),

lightning/src/ln/dual_funding_tests.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,7 @@ fn do_test_v2_channel_establishment(session: V2ChannelEstablishmentTestSession)
4848
let initiator_funding_inputs: Vec<_> = create_dual_funding_utxos_with_prev_txs(
4949
&nodes[0],
5050
&[session.initiator_input_value_satoshis],
51-
)
52-
.into_iter()
53-
.map(|(txin, tx, _)| (txin, tx))
54-
.collect();
51+
);
5552

5653
// Alice creates a dual-funded channel as initiator.
5754
let funding_satoshis = session.funding_input_sats;

lightning/src/ln/interactivetxs.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1884,7 +1884,7 @@ pub(super) fn calculate_change_output_value(
18841884

18851885
let mut total_input_satoshis = 0u64;
18861886
let mut our_funding_inputs_weight = 0u64;
1887-
for (txin, tx) in context.our_funding_inputs.iter() {
1887+
for (txin, tx, _) in context.our_funding_inputs.iter() {
18881888
let txid = tx.compute_txid();
18891889
if txin.previous_output.txid != txid {
18901890
return Err(AbortReason::PrevTxOutInvalid);
@@ -1894,6 +1894,7 @@ pub(super) fn calculate_change_output_value(
18941894
.get(txin.previous_output.vout as usize)
18951895
.ok_or(AbortReason::PrevTxOutInvalid)?;
18961896
total_input_satoshis = total_input_satoshis.saturating_add(output.value.to_sat());
1897+
// FIXME: Can we use the Weight from context.our_funding_inputs?
18971898
let weight = estimate_input_weight(output).to_wu();
18981899
our_funding_inputs_weight = our_funding_inputs_weight.saturating_add(weight);
18991900
}
@@ -1956,7 +1957,7 @@ mod tests {
19561957
use bitcoin::transaction::Version;
19571958
use bitcoin::{
19581959
OutPoint, PubkeyHash, ScriptBuf, Sequence, SignedAmount, Transaction, TxIn, TxOut,
1959-
WPubkeyHash, Witness,
1960+
WPubkeyHash, Weight, Witness,
19601961
};
19611962
use core::ops::Deref;
19621963

@@ -2967,9 +2968,10 @@ mod tests {
29672968
sequence: Sequence::ZERO,
29682969
witness: Witness::new(),
29692970
};
2970-
(txin, tx)
2971+
let weight = Weight::ZERO;
2972+
(txin, tx, weight)
29712973
})
2972-
.collect::<Vec<(TxIn, Transaction)>>();
2974+
.collect::<Vec<(TxIn, Transaction, Weight)>>();
29732975
let our_contributed = 110_000;
29742976
let txout = TxOut { value: Amount::from_sat(10_000), script_pubkey: ScriptBuf::new() };
29752977
let outputs = vec![txout];

0 commit comments

Comments
 (0)