Skip to content

Commit 5839ae4

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 4913b30 commit 5839ae4

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,
@@ -10616,8 +10618,7 @@ where
1061610618
/// generated by `SignerProvider::get_destination_script`.
1061710619
#[cfg(splicing)]
1061810620
pub fn splice_channel(
10619-
&mut self, our_funding_contribution_satoshis: i64, our_funding_inputs: Vec<FundingTxInput>,
10620-
change_script: Option<ScriptBuf>, funding_feerate_per_kw: u32, locktime: u32,
10621+
&mut self, contribution: SpliceContribution, funding_feerate_per_kw: u32, locktime: u32,
1062110622
) -> Result<msgs::SpliceInit, APIError> {
1062210623
// Check if a splice has been initiated already.
1062310624
// Note: only a single outstanding splice is supported (per spec)
@@ -10641,7 +10642,7 @@ where
1064110642

1064210643
// TODO(splicing): check for quiescence
1064310644

10644-
let our_funding_contribution = SignedAmount::from_sat(our_funding_contribution_satoshis);
10645+
let our_funding_contribution = contribution.value();
1064510646
if our_funding_contribution > SignedAmount::MAX_MONEY {
1064610647
return Err(APIError::APIMisuseError {
1064710648
err: format!(
@@ -10670,7 +10671,7 @@ where
1067010671
// Check that inputs are sufficient to cover our contribution.
1067110672
let _fee = check_v2_funding_inputs_sufficient(
1067210673
our_funding_contribution.to_sat(),
10673-
&our_funding_inputs,
10674+
contribution.inputs(),
1067410675
true,
1067510676
true,
1067610677
funding_feerate_per_kw,
@@ -10683,7 +10684,7 @@ where
1068310684
),
1068410685
})?;
1068510686

10686-
for FundingTxInput { txin, prevtx, .. } in our_funding_inputs.iter() {
10687+
for FundingTxInput { txin, prevtx, .. } in contribution.inputs().iter() {
1068710688
const MESSAGE_TEMPLATE: msgs::TxAddInput = msgs::TxAddInput {
1068810689
channel_id: ChannelId([0; 32]),
1068910690
serial_id: 0,
@@ -10704,6 +10705,7 @@ where
1070410705
}
1070510706

1070610707
let prev_funding_input = self.funding.to_splice_funding_input();
10708+
let (our_funding_inputs, change_script) = contribution.into_tx_parts();
1070710709
let funding_negotiation_context = FundingNegotiationContext {
1070810710
is_initiator: true,
1070910711
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, TxIn, 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};
@@ -200,6 +200,47 @@ pub use crate::ln::outbound_payment::{
200200
};
201201
use crate::ln::script::ShutdownScript;
202202

203+
/// The components of a splice's funding transaction that are contributed by one party.
204+
#[cfg(splicing)]
205+
pub enum SpliceContribution {
206+
/// When only inputs -- except for a possible change output -- are contributed to the splice.
207+
SpliceIn {
208+
/// The amount to contribute to the splice.
209+
value: Amount,
210+
211+
/// The inputs used to meet the contributed amount. Any excess amount will be sent to a
212+
/// change output.
213+
inputs: Vec<FundingTxInput>,
214+
215+
/// An optional change output script. This will be used if needed or, if not set, generated
216+
/// using `SignerProvider::get_destination_script`.
217+
change_script: Option<ScriptBuf>,
218+
},
219+
}
220+
221+
#[cfg(splicing)]
222+
impl SpliceContribution {
223+
pub(super) fn value(&self) -> SignedAmount {
224+
match self {
225+
SpliceContribution::SpliceIn { value, .. } => {
226+
value.to_signed().unwrap_or(SignedAmount::MAX)
227+
},
228+
}
229+
}
230+
231+
pub(super) fn inputs(&self) -> &[FundingTxInput] {
232+
match self {
233+
SpliceContribution::SpliceIn { inputs, .. } => &inputs[..],
234+
}
235+
}
236+
237+
pub(super) fn into_tx_parts(self) -> (Vec<FundingTxInput>, Option<ScriptBuf>) {
238+
match self {
239+
SpliceContribution::SpliceIn { inputs, change_script, .. } => (inputs, change_script),
240+
}
241+
}
242+
}
243+
203244
/// An input to contribute to a channel's funding transaction either when using the v2 channel
204245
/// establishment protocol or when splicing.
205246
#[derive(Clone)]
@@ -4456,14 +4497,13 @@ where
44564497
#[cfg(splicing)]
44574498
#[rustfmt::skip]
44584499
pub fn splice_channel(
4459-
&self, channel_id: &ChannelId, counterparty_node_id: &PublicKey, our_funding_contribution_satoshis: i64,
4460-
our_funding_inputs: Vec<FundingTxInput>, change_script: Option<ScriptBuf>,
4461-
funding_feerate_per_kw: u32, locktime: Option<u32>,
4500+
&self, channel_id: &ChannelId, counterparty_node_id: &PublicKey,
4501+
contribution: SpliceContribution, funding_feerate_per_kw: u32, locktime: Option<u32>,
44624502
) -> Result<(), APIError> {
44634503
let mut res = Ok(());
44644504
PersistenceNotifierGuard::optionally_notify(self, || {
44654505
let result = self.internal_splice_channel(
4466-
channel_id, counterparty_node_id, our_funding_contribution_satoshis, our_funding_inputs, change_script, funding_feerate_per_kw, locktime
4506+
channel_id, counterparty_node_id, contribution, funding_feerate_per_kw, locktime
44674507
);
44684508
res = result;
44694509
match res {
@@ -4478,8 +4518,7 @@ where
44784518
#[cfg(splicing)]
44794519
fn internal_splice_channel(
44804520
&self, channel_id: &ChannelId, counterparty_node_id: &PublicKey,
4481-
our_funding_contribution_satoshis: i64, our_funding_inputs: Vec<FundingTxInput>,
4482-
change_script: Option<ScriptBuf>, funding_feerate_per_kw: u32, locktime: Option<u32>,
4521+
contribution: SpliceContribution, funding_feerate_per_kw: u32, locktime: Option<u32>,
44834522
) -> Result<(), APIError> {
44844523
let per_peer_state = self.per_peer_state.read().unwrap();
44854524

@@ -4500,13 +4539,8 @@ where
45004539
hash_map::Entry::Occupied(mut chan_phase_entry) => {
45014540
let locktime = locktime.unwrap_or_else(|| self.current_best_block().height);
45024541
if let Some(chan) = chan_phase_entry.get_mut().as_funded_mut() {
4503-
let msg = chan.splice_channel(
4504-
our_funding_contribution_satoshis,
4505-
our_funding_inputs,
4506-
change_script,
4507-
funding_feerate_per_kw,
4508-
locktime,
4509-
)?;
4542+
let msg =
4543+
chan.splice_channel(contribution, funding_feerate_per_kw, locktime)?;
45104544
peer_state.pending_msg_events.push(MessageSendEvent::SendSpliceInit {
45114545
node_id: *counterparty_node_id,
45124546
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
)
@@ -317,13 +325,17 @@ fn test_v1_splice_in_negative_insufficient_inputs() {
317325
let funding_inputs =
318326
create_dual_funding_utxos_with_prev_txs(&nodes[0], &[extra_splice_funding_input_sats]);
319327

328+
let contribution = SpliceContribution::SpliceIn {
329+
value: Amount::from_sat(splice_in_sats),
330+
inputs: funding_inputs,
331+
change_script: None,
332+
};
333+
320334
// Initiate splice-in, with insufficient input contribution
321335
let res = nodes[0].node.splice_channel(
322336
&channel_id,
323337
&nodes[1].node.get_our_node_id(),
324-
splice_in_sats as i64,
325-
funding_inputs,
326-
None, // change_script
338+
contribution,
327339
1024, // funding_feerate_per_kw,
328340
None, // locktime
329341
);

0 commit comments

Comments
 (0)