Skip to content

Commit f1db67e

Browse files
committed
Track funding negotiation state with an enum
PendingSplice holds a FundingScope being negotiated. However, when implementing funding negotiation, other states are possible depending on which party initiated the splice. Using an enum prevents needing various Option fields which may result in invalid states. When the user initiates the splice, the FundingNegotiationContext must be held until the counterparty responds with splice_ack. At that point enough information becomes available to create a new FundingScope and an InteractiveTxConstructor. When the counterparty initiates the splice, both a new FundingScope and an InteractiveTxConstructor can be created immediately when responding with splice_ack. After the transaction is constructed, those are no longer needed. At that point an InteractiveTxSigningSession is tracked until signatures are exchanged.
1 parent 823e251 commit f1db67e

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

lightning/src/ln/channel.rs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1821,7 +1821,11 @@ where
18211821
ChannelPhase::Funded(mut funded_channel) => {
18221822
#[cfg(splicing)]
18231823
let has_negotiated_pending_splice = funded_channel.pending_splice.as_ref()
1824-
.map(|pending_splice| pending_splice.funding.is_some())
1824+
.and_then(|pending_splice| pending_splice.funding_negotiation.as_ref())
1825+
.filter(|funding_negotiation| {
1826+
matches!(funding_negotiation, FundingNegotiation::AwaitingSignatures(_))
1827+
})
1828+
.map(|funding_negotiation| funding_negotiation.as_funding().is_some())
18251829
.unwrap_or(false);
18261830
#[cfg(splicing)]
18271831
let session_received_commitment_signed = funded_channel
@@ -2171,7 +2175,7 @@ impl FundingScope {
21712175
#[cfg(splicing)]
21722176
struct PendingSplice {
21732177
pub our_funding_contribution: i64,
2174-
funding: Option<FundingScope>,
2178+
funding_negotiation: Option<FundingNegotiation>,
21752179

21762180
/// The funding txid used in the `splice_locked` sent to the counterparty.
21772181
sent_funding_txid: Option<Txid>,
@@ -2180,6 +2184,24 @@ struct PendingSplice {
21802184
received_funding_txid: Option<Txid>,
21812185
}
21822186

2187+
#[cfg(splicing)]
2188+
enum FundingNegotiation {
2189+
AwaitingAck(FundingNegotiationContext),
2190+
ConstructingTransaction(FundingScope, InteractiveTxConstructor),
2191+
AwaitingSignatures(FundingScope),
2192+
}
2193+
2194+
#[cfg(splicing)]
2195+
impl FundingNegotiation {
2196+
fn as_funding(&self) -> Option<&FundingScope> {
2197+
match self {
2198+
FundingNegotiation::AwaitingAck(_) => None,
2199+
FundingNegotiation::ConstructingTransaction(funding, _) => Some(funding),
2200+
FundingNegotiation::AwaitingSignatures(funding) => Some(funding),
2201+
}
2202+
}
2203+
}
2204+
21832205
#[cfg(splicing)]
21842206
impl PendingSplice {
21852207
fn check_get_splice_locked<SP: Deref>(
@@ -6808,7 +6830,11 @@ where
68086830
let pending_splice_funding = self
68096831
.pending_splice
68106832
.as_ref()
6811-
.and_then(|pending_splice| pending_splice.funding.as_ref())
6833+
.and_then(|pending_splice| pending_splice.funding_negotiation.as_ref())
6834+
.filter(|funding_negotiation| {
6835+
matches!(funding_negotiation, FundingNegotiation::AwaitingSignatures(_))
6836+
})
6837+
.and_then(|funding_negotiation| funding_negotiation.as_funding())
68126838
.expect("Funding must exist for negotiated pending splice");
68136839
let (holder_commitment_tx, _) = self.context.validate_commitment_signed(
68146840
pending_splice_funding,
@@ -10436,7 +10462,7 @@ where
1043610462

1043710463
self.pending_splice = Some(PendingSplice {
1043810464
our_funding_contribution: our_funding_contribution_satoshis,
10439-
funding: None,
10465+
funding_negotiation: None,
1044010466
sent_funding_txid: None,
1044110467
received_funding_txid: None,
1044210468
});

0 commit comments

Comments
 (0)