@@ -1193,6 +1193,14 @@ pub(crate) struct ShutdownResult {
1193
1193
pub(crate) splice_funding_failed: Option<SpliceFundingFailed>,
1194
1194
}
1195
1195
1196
+ /// The result of a peer disconnection.
1197
+ pub(crate) struct DisconnectResult {
1198
+ pub(crate) is_resumable: bool,
1199
+ /// If a splice was in progress when the channel was shut down, this contains
1200
+ /// the splice funding information for emitting a SpliceFailed event.
1201
+ pub(crate) splice_funding_failed: Option<SpliceFundingFailed>,
1202
+ }
1203
+
1196
1204
/// Tracks the transaction number, along with current and next commitment points.
1197
1205
/// This consolidates the logic to advance our commitment number and request new
1198
1206
/// commitment points from our signer.
@@ -1585,11 +1593,15 @@ where
1585
1593
/// Should be called when the peer is disconnected. Returns true if the channel can be resumed
1586
1594
/// when the peer reconnects (via [`Self::peer_connected_get_handshake`]). If not, the channel
1587
1595
/// must be immediately closed.
1588
- #[rustfmt::skip]
1589
- pub fn peer_disconnected_is_resumable<L: Deref>(&mut self, logger: &L) -> bool where L::Target: Logger {
1590
- match &mut self.phase {
1596
+ pub fn peer_disconnected_is_resumable<L: Deref>(&mut self, logger: &L) -> DisconnectResult
1597
+ where
1598
+ L::Target: Logger,
1599
+ {
1600
+ let is_resumable = match &mut self.phase {
1591
1601
ChannelPhase::Undefined => unreachable!(),
1592
- ChannelPhase::Funded(chan) => chan.remove_uncommitted_htlcs_and_mark_paused(logger).is_ok(),
1602
+ ChannelPhase::Funded(chan) => {
1603
+ chan.remove_uncommitted_htlcs_and_mark_paused(logger).is_ok()
1604
+ },
1593
1605
// If we get disconnected and haven't yet committed to a funding
1594
1606
// transaction, we can replay the `open_channel` on reconnection, so don't
1595
1607
// bother dropping the channel here. However, if we already committed to
@@ -1599,7 +1611,15 @@ where
1599
1611
ChannelPhase::UnfundedOutboundV1(chan) => chan.is_resumable(),
1600
1612
ChannelPhase::UnfundedInboundV1(_) => false,
1601
1613
ChannelPhase::UnfundedV2(_) => false,
1602
- }
1614
+ };
1615
+
1616
+ let splice_funding_failed = if let ChannelPhase::Funded(chan) = &mut self.phase {
1617
+ chan.maybe_fail_splice_negotiation()
1618
+ } else {
1619
+ None
1620
+ };
1621
+
1622
+ DisconnectResult { is_resumable, splice_funding_failed }
1603
1623
}
1604
1624
1605
1625
/// Should be called when the peer re-connects, returning an initial message which we should
@@ -6836,8 +6856,15 @@ where
6836
6856
}
6837
6857
6838
6858
pub fn force_shutdown(&mut self, closure_reason: ClosureReason) -> ShutdownResult {
6839
- let splice_funding_failed = self
6840
- .pending_splice
6859
+ let splice_funding_failed = self.maybe_fail_splice_negotiation();
6860
+
6861
+ let mut shutdown_result = self.context.force_shutdown(&self.funding, closure_reason);
6862
+ shutdown_result.splice_funding_failed = splice_funding_failed;
6863
+ shutdown_result
6864
+ }
6865
+
6866
+ fn maybe_fail_splice_negotiation(&mut self) -> Option<SpliceFundingFailed> {
6867
+ self.pending_splice
6841
6868
.as_mut()
6842
6869
.and_then(|pending_splice| pending_splice.funding_negotiation.take())
6843
6870
.filter(|funding_negotiation| funding_negotiation.is_initiator())
@@ -6867,11 +6894,7 @@ where
6867
6894
None
6868
6895
},
6869
6896
})
6870
- });
6871
-
6872
- let mut shutdown_result = self.context.force_shutdown(&self.funding, closure_reason);
6873
- shutdown_result.splice_funding_failed = splice_funding_failed;
6874
- shutdown_result
6897
+ })
6875
6898
}
6876
6899
6877
6900
fn interactive_tx_constructor_mut(&mut self) -> Option<&mut InteractiveTxConstructor> {
@@ -11708,9 +11731,9 @@ where
11708
11731
.map_err(|e| APIError::APIMisuseError { err: e.to_owned() })
11709
11732
}
11710
11733
11711
- fn send_splice_init(
11712
- &mut self, instructions: SpliceInstructions,
11713
- ) -> Result<msgs::SpliceInit, String> {
11734
+ fn send_splice_init(&mut self, instructions: SpliceInstructions) -> msgs::SpliceInit {
11735
+ debug_assert!( self.pending_splice.is_none());
11736
+
11714
11737
let SpliceInstructions {
11715
11738
adjusted_funding_contribution,
11716
11739
our_funding_inputs,
@@ -11720,15 +11743,6 @@ where
11720
11743
locktime,
11721
11744
} = instructions;
11722
11745
11723
- // Check if a splice has been initiated already.
11724
- // Note: only a single outstanding splice is supported (per spec)
11725
- if self.pending_splice.is_some() {
11726
- return Err(format!(
11727
- "Channel {} cannot be spliced, as it has already a splice pending",
11728
- self.context.channel_id(),
11729
- ));
11730
- }
11731
-
11732
11746
let prev_funding_input = self.funding.to_splice_funding_input();
11733
11747
let context = FundingNegotiationContext {
11734
11748
is_initiator: true,
@@ -11752,14 +11766,14 @@ where
11752
11766
let prev_funding_txid = self.funding.get_funding_txid();
11753
11767
let funding_pubkey = self.context.holder_pubkeys(prev_funding_txid).funding_pubkey;
11754
11768
11755
- Ok( msgs::SpliceInit {
11769
+ msgs::SpliceInit {
11756
11770
channel_id: self.context.channel_id,
11757
11771
funding_contribution_satoshis: adjusted_funding_contribution.to_sat(),
11758
11772
funding_feerate_per_kw,
11759
11773
locktime,
11760
11774
funding_pubkey,
11761
11775
require_confirmed_inputs: None,
11762
- })
11776
+ }
11763
11777
}
11764
11778
11765
11779
/// Checks during handling splice_init
@@ -12904,10 +12918,21 @@ where
12904
12918
"Internal Error: Didn't have anything to do after reaching quiescence".to_owned()
12905
12919
));
12906
12920
},
12907
- Some(QuiescentAction::Splice(_instructions)) => {
12908
- return self.send_splice_init(_instructions)
12909
- .map(|splice_init| Some(StfuResponse::SpliceInit(splice_init)))
12910
- .map_err(|e| ChannelError::WarnAndDisconnect(e.to_owned()));
12921
+ Some(QuiescentAction::Splice(instructions)) => {
12922
+ if self.pending_splice.is_some() {
12923
+ debug_assert!(false);
12924
+ self.quiescent_action = Some(QuiescentAction::Splice(instructions));
12925
+
12926
+ return Err(ChannelError::WarnAndDisconnect(
12927
+ format!(
12928
+ "Channel {} cannot be spliced as it already has a splice pending",
12929
+ self.context.channel_id(),
12930
+ ),
12931
+ ));
12932
+ }
12933
+
12934
+ let splice_init = self.send_splice_init(instructions);
12935
+ return Ok(Some(StfuResponse::SpliceInit(splice_init)));
12911
12936
},
12912
12937
#[cfg(any(test, fuzzing))]
12913
12938
Some(QuiescentAction::DoNothing) => {
0 commit comments