Skip to content

Commit c5a3cea

Browse files
committed
Use struct instead of enum for SpliceContribution
When adding support for mixed splice-in and splice-out, the contribution amount will need to be computed based on the splice-in and splice-out values. Rather than add a third variant to SpliceContribution, which could have an invalid contribution amount, use a more general struct that can represent splice-in, splice-out, and mixed. Constructors are provided for the typical splice-in and splice-out case whereas support for the mixed case will be added in an independent change.
1 parent de384ff commit c5a3cea

File tree

2 files changed

+127
-150
lines changed

2 files changed

+127
-150
lines changed

lightning/src/ln/funding.rs

Lines changed: 46 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -20,69 +20,65 @@ use crate::sign::{P2TR_KEY_PATH_WITNESS_WEIGHT, P2WPKH_WITNESS_WEIGHT};
2020

2121
/// The components of a splice's funding transaction that are contributed by one party.
2222
#[derive(Debug, Clone)]
23-
pub enum SpliceContribution {
24-
/// When funds are added to a channel.
25-
SpliceIn {
26-
/// The amount to contribute to the splice.
27-
value: Amount,
28-
29-
/// The inputs included in the splice's funding transaction to meet the contributed amount
30-
/// plus fees. Any excess amount will be sent to a change output.
31-
inputs: Vec<FundingTxInput>,
32-
33-
/// An optional change output script. This will be used if needed or, when not set,
34-
/// generated using [`SignerProvider::get_destination_script`].
35-
///
36-
/// [`SignerProvider::get_destination_script`]: crate::sign::SignerProvider::get_destination_script
37-
change_script: Option<ScriptBuf>,
38-
},
39-
/// When funds are removed from a channel.
40-
SpliceOut {
41-
/// The outputs to include in the splice's funding transaction. The total value of all
42-
/// outputs plus fees will be the amount that is removed.
43-
outputs: Vec<TxOut>,
44-
},
23+
pub struct SpliceContribution {
24+
/// The amount to contribute to the splice.
25+
value: SignedAmount,
26+
27+
/// The inputs included in the splice's funding transaction to meet the contributed amount
28+
/// plus fees. Any excess amount will be sent to a change output.
29+
inputs: Vec<FundingTxInput>,
30+
31+
/// The outputs to include in the splice's funding transaction. The total value of all
32+
/// outputs plus fees will be the amount that is removed.
33+
outputs: Vec<TxOut>,
34+
35+
/// An optional change output script. This will be used if needed or, when not set,
36+
/// generated using [`SignerProvider::get_destination_script`].
37+
///
38+
/// [`SignerProvider::get_destination_script`]: crate::sign::SignerProvider::get_destination_script
39+
change_script: Option<ScriptBuf>,
4540
}
4641

4742
impl SpliceContribution {
48-
pub(super) fn value(&self) -> SignedAmount {
49-
match self {
50-
SpliceContribution::SpliceIn { value, .. } => {
51-
value.to_signed().unwrap_or(SignedAmount::MAX)
52-
},
53-
SpliceContribution::SpliceOut { outputs } => {
54-
let value_removed = outputs
55-
.iter()
56-
.map(|txout| txout.value)
57-
.sum::<Amount>()
58-
.to_signed()
59-
.unwrap_or(SignedAmount::MAX);
60-
-value_removed
61-
},
43+
/// Creates a contribution for when funds are only added to a channel.
44+
pub fn splice_in(
45+
value: Amount, inputs: Vec<FundingTxInput>, change_script: Option<ScriptBuf>,
46+
) -> Self {
47+
Self {
48+
value: value.to_signed().unwrap_or(SignedAmount::MAX),
49+
inputs,
50+
outputs: vec![],
51+
change_script,
6252
}
6353
}
6454

55+
/// Creates a contribution for when funds are only removed from a channel.
56+
pub fn splice_out(outputs: Vec<TxOut>) -> Self {
57+
let value_removed = outputs
58+
.iter()
59+
.map(|txout| txout.value)
60+
.sum::<Amount>()
61+
.to_signed()
62+
.unwrap_or(SignedAmount::MAX);
63+
64+
Self { value: -value_removed, inputs: vec![], outputs, change_script: None }
65+
}
66+
67+
pub(super) fn value(&self) -> SignedAmount {
68+
self.value
69+
}
70+
6571
pub(super) fn inputs(&self) -> &[FundingTxInput] {
66-
match self {
67-
SpliceContribution::SpliceIn { inputs, .. } => &inputs[..],
68-
SpliceContribution::SpliceOut { .. } => &[],
69-
}
72+
&self.inputs[..]
7073
}
7174

7275
pub(super) fn outputs(&self) -> &[TxOut] {
73-
match self {
74-
SpliceContribution::SpliceIn { .. } => &[],
75-
SpliceContribution::SpliceOut { outputs } => &outputs[..],
76-
}
76+
&self.outputs[..]
7777
}
7878

7979
pub(super) fn into_tx_parts(self) -> (Vec<FundingTxInput>, Vec<TxOut>, Option<ScriptBuf>) {
80-
match self {
81-
SpliceContribution::SpliceIn { inputs, change_script, .. } => {
82-
(inputs, vec![], change_script)
83-
},
84-
SpliceContribution::SpliceOut { outputs } => (vec![], outputs, None),
85-
}
80+
let SpliceContribution { value: _, inputs, outputs, change_script } = self;
81+
(inputs, outputs, change_script)
8682
}
8783
}
8884

0 commit comments

Comments
 (0)