Skip to content

Commit 3e65d51

Browse files
committed
Use a SpliceContribution enum for passing splice-in params
ChannelManager::splice_channel takes individual parameters to support splice-in. Change these to an enum such that it can be used for splice-out as well.
1 parent 01bf037 commit 3e65d51

File tree

3 files changed

+74
-26
lines changed

3 files changed

+74
-26
lines changed

lightning/src/ln/channel.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ use crate::ln::channel_state::{
5252
ChannelShutdownState, CounterpartyForwardingInfo, InboundHTLCDetails, InboundHTLCStateDetails,
5353
OutboundHTLCDetails, OutboundHTLCStateDetails,
5454
};
55+
#[cfg(splicing)]
56+
use crate::ln::channelmanager::SpliceContribution;
5557
use crate::ln::channelmanager::{
5658
self, FundingConfirmedMessage, FundingTxInput, HTLCFailureMsg, HTLCSource, OpenChannelMessage,
5759
PaymentClaimDetails, PendingHTLCInfo, PendingHTLCStatus, RAACommitmentOrder, SentHTLCId,
@@ -10604,8 +10606,7 @@ where
1060410606
/// generated by `SignerProvider::get_destination_script`.
1060510607
#[cfg(splicing)]
1060610608
pub fn splice_channel(
10607-
&mut self, our_funding_contribution_satoshis: i64, our_funding_inputs: Vec<FundingTxInput>,
10608-
change_script: Option<ScriptBuf>, funding_feerate_per_kw: u32, locktime: u32,
10609+
&mut self, contribution: SpliceContribution, funding_feerate_per_kw: u32, locktime: u32,
1060910610
) -> Result<msgs::SpliceInit, APIError> {
1061010611
// Check if a splice has been initiated already.
1061110612
// Note: only a single outstanding splice is supported (per spec)
@@ -10629,7 +10630,7 @@ where
1062910630

1063010631
// TODO(splicing): check for quiescence
1063110632

10632-
let our_funding_contribution = SignedAmount::from_sat(our_funding_contribution_satoshis);
10633+
let our_funding_contribution = contribution.value();
1063310634
if our_funding_contribution > SignedAmount::MAX_MONEY {
1063410635
return Err(APIError::APIMisuseError {
1063510636
err: format!(
@@ -10658,7 +10659,7 @@ where
1065810659
// Check that inputs are sufficient to cover our contribution.
1065910660
let _fee = check_v2_funding_inputs_sufficient(
1066010661
our_funding_contribution.to_sat(),
10661-
&our_funding_inputs,
10662+
contribution.inputs(),
1066210663
true,
1066310664
true,
1066410665
funding_feerate_per_kw,
@@ -10671,7 +10672,7 @@ where
1067110672
),
1067210673
})?;
1067310674

10674-
for FundingTxInput { utxo, prevtx, .. } in our_funding_inputs.iter() {
10675+
for FundingTxInput { utxo, prevtx, .. } in contribution.inputs().iter() {
1067510676
const MESSAGE_TEMPLATE: msgs::TxAddInput = msgs::TxAddInput {
1067610677
channel_id: ChannelId([0; 32]),
1067710678
serial_id: 0,
@@ -10692,6 +10693,7 @@ where
1069210693
}
1069310694

1069410695
let prev_funding_input = self.funding.to_splice_funding_input();
10696+
let (our_funding_inputs, change_script) = contribution.into_tx_parts();
1069510697
let funding_negotiation_context = FundingNegotiationContext {
1069610698
is_initiator: true,
1069710699
our_funding_contribution,

lightning/src/ln/channelmanager.rs

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ use bitcoin::hashes::{Hash, HashEngine, HmacEngine};
3030

3131
use bitcoin::secp256k1::Secp256k1;
3232
use bitcoin::secp256k1::{PublicKey, SecretKey};
33-
#[cfg(splicing)]
34-
use bitcoin::ScriptBuf;
3533
use bitcoin::{secp256k1, Sequence, SignedAmount, Weight};
34+
#[cfg(splicing)]
35+
use bitcoin::{Amount, ScriptBuf};
3636

3737
use crate::blinded_path::message::MessageForwardNode;
3838
use crate::blinded_path::message::{AsyncPaymentsContext, OffersContext};
@@ -204,6 +204,47 @@ pub use crate::ln::outbound_payment::{
204204
};
205205
use crate::ln::script::ShutdownScript;
206206

207+
/// The components of a splice's funding transaction that are contributed by one party.
208+
#[cfg(splicing)]
209+
pub enum SpliceContribution {
210+
/// When funds are added to a channel.
211+
SpliceIn {
212+
/// The amount to contribute to the splice.
213+
value: Amount,
214+
215+
/// The inputs included in the splice's funding transaction to meet the contributed amount.
216+
/// Any excess amount will be sent to a change output.
217+
inputs: Vec<FundingTxInput>,
218+
219+
/// An optional change output script. This will be used if needed or, when not set,
220+
/// generated using [`SignerProvider::get_destination_script`].
221+
change_script: Option<ScriptBuf>,
222+
},
223+
}
224+
225+
#[cfg(splicing)]
226+
impl SpliceContribution {
227+
pub(super) fn value(&self) -> SignedAmount {
228+
match self {
229+
SpliceContribution::SpliceIn { value, .. } => {
230+
value.to_signed().unwrap_or(SignedAmount::MAX)
231+
},
232+
}
233+
}
234+
235+
pub(super) fn inputs(&self) -> &[FundingTxInput] {
236+
match self {
237+
SpliceContribution::SpliceIn { inputs, .. } => &inputs[..],
238+
}
239+
}
240+
241+
pub(super) fn into_tx_parts(self) -> (Vec<FundingTxInput>, Option<ScriptBuf>) {
242+
match self {
243+
SpliceContribution::SpliceIn { inputs, change_script, .. } => (inputs, change_script),
244+
}
245+
}
246+
}
247+
207248
/// An input to contribute to a channel's funding transaction either when using the v2 channel
208249
/// establishment protocol or when splicing.
209250
#[derive(Clone)]
@@ -4557,14 +4598,13 @@ where
45574598
#[cfg(splicing)]
45584599
#[rustfmt::skip]
45594600
pub fn splice_channel(
4560-
&self, channel_id: &ChannelId, counterparty_node_id: &PublicKey, our_funding_contribution_satoshis: i64,
4561-
our_funding_inputs: Vec<FundingTxInput>, change_script: Option<ScriptBuf>,
4562-
funding_feerate_per_kw: u32, locktime: Option<u32>,
4601+
&self, channel_id: &ChannelId, counterparty_node_id: &PublicKey,
4602+
contribution: SpliceContribution, funding_feerate_per_kw: u32, locktime: Option<u32>,
45634603
) -> Result<(), APIError> {
45644604
let mut res = Ok(());
45654605
PersistenceNotifierGuard::optionally_notify(self, || {
45664606
let result = self.internal_splice_channel(
4567-
channel_id, counterparty_node_id, our_funding_contribution_satoshis, our_funding_inputs, change_script, funding_feerate_per_kw, locktime
4607+
channel_id, counterparty_node_id, contribution, funding_feerate_per_kw, locktime
45684608
);
45694609
res = result;
45704610
match res {
@@ -4579,8 +4619,7 @@ where
45794619
#[cfg(splicing)]
45804620
fn internal_splice_channel(
45814621
&self, channel_id: &ChannelId, counterparty_node_id: &PublicKey,
4582-
our_funding_contribution_satoshis: i64, our_funding_inputs: Vec<FundingTxInput>,
4583-
change_script: Option<ScriptBuf>, funding_feerate_per_kw: u32, locktime: Option<u32>,
4622+
contribution: SpliceContribution, funding_feerate_per_kw: u32, locktime: Option<u32>,
45844623
) -> Result<(), APIError> {
45854624
let per_peer_state = self.per_peer_state.read().unwrap();
45864625

@@ -4601,13 +4640,8 @@ where
46014640
hash_map::Entry::Occupied(mut chan_phase_entry) => {
46024641
let locktime = locktime.unwrap_or_else(|| self.current_best_block().height);
46034642
if let Some(chan) = chan_phase_entry.get_mut().as_funded_mut() {
4604-
let msg = chan.splice_channel(
4605-
our_funding_contribution_satoshis,
4606-
our_funding_inputs,
4607-
change_script,
4608-
funding_feerate_per_kw,
4609-
locktime,
4610-
)?;
4643+
let msg =
4644+
chan.splice_channel(contribution, funding_feerate_per_kw, locktime)?;
46114645
peer_state.pending_msg_events.push(MessageSendEvent::SendSpliceInit {
46124646
node_id: *counterparty_node_id,
46134647
msg,

lightning/src/ln/splicing_tests.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77
// You may not use this file except in accordance with one or both of these
88
// licenses.
99

10+
use crate::ln::channelmanager::SpliceContribution;
1011
use crate::ln::functional_test_utils::*;
1112
use crate::ln::msgs::{BaseMessageHandler, ChannelMessageHandler, MessageSendEvent};
1213
use crate::util::errors::APIError;
1314

15+
use bitcoin::Amount;
16+
1417
/// Splicing test, simple splice-in flow. Starts with opening a V1 channel first.
1518
/// Builds on test_channel_open_simple()
1619
#[test]
@@ -66,15 +69,20 @@ fn test_v1_splice_in() {
6669
&initiator_node,
6770
&[extra_splice_funding_input_sats],
6871
);
72+
73+
let contribution = SpliceContribution::SpliceIn {
74+
value: Amount::from_sat(splice_in_sats),
75+
inputs: funding_inputs,
76+
change_script: None,
77+
};
78+
6979
// Initiate splice-in
7080
let _res = initiator_node
7181
.node
7282
.splice_channel(
7383
&channel_id,
7484
&acceptor_node.node.get_our_node_id(),
75-
splice_in_sats as i64,
76-
funding_inputs,
77-
None, // change_script
85+
contribution,
7886
funding_feerate_per_kw,
7987
None, // locktime
8088
)
@@ -295,13 +303,17 @@ fn test_v1_splice_in_negative_insufficient_inputs() {
295303
let funding_inputs =
296304
create_dual_funding_utxos_with_prev_txs(&nodes[0], &[extra_splice_funding_input_sats]);
297305

306+
let contribution = SpliceContribution::SpliceIn {
307+
value: Amount::from_sat(splice_in_sats),
308+
inputs: funding_inputs,
309+
change_script: None,
310+
};
311+
298312
// Initiate splice-in, with insufficient input contribution
299313
let res = nodes[0].node.splice_channel(
300314
&channel_id,
301315
&nodes[1].node.get_our_node_id(),
302-
splice_in_sats as i64,
303-
funding_inputs,
304-
None, // change_script
316+
contribution,
305317
1024, // funding_feerate_per_kw,
306318
None, // locktime
307319
);

0 commit comments

Comments
 (0)