@@ -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,34 @@ 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
+ // Reset any quiescence-related state as it is implicitly terminated once disconnected.
1618
+ if matches!(chan.context.channel_state, ChannelState::ChannelReady(_)) {
1619
+ if chan.quiescent_action.is_some() {
1620
+ // If we were trying to get quiescent, try again after reconnection.
1621
+ chan.context.channel_state.set_awaiting_quiescence();
1622
+ }
1623
+ chan.context.channel_state.clear_local_stfu_sent();
1624
+ chan.context.channel_state.clear_remote_stfu_sent();
1625
+ if chan.should_reset_pending_splice_funding_negotiation().unwrap_or(true) {
1626
+ // If we were in quiescence but a splice was never negotiated, or the negotiation
1627
+ // failed due to disconnecting, we shouldn't be quiescent anymore upon reconnecting.
1628
+ // If there was a pending splice negotiation that has failed due to disconnecting,
1629
+ // we also take the opportunity to clean up our state.
1630
+ chan.reset_pending_splice_state()
1631
+ } else {
1632
+ None
1633
+ }
1634
+ } else {
1635
+ None
1636
+ }
1637
+ } else {
1638
+ None
1639
+ };
1640
+
1641
+ DisconnectResult { is_resumable, splice_funding_failed }
1603
1642
}
1604
1643
1605
1644
/// Should be called when the peer re-connects, returning an initial message which we should
@@ -6808,8 +6847,15 @@ where
6808
6847
}
6809
6848
6810
6849
pub fn force_shutdown(&mut self, closure_reason: ClosureReason) -> ShutdownResult {
6811
- let splice_funding_failed = self
6812
- .reset_pending_splice_state()
6850
+ let splice_funding_failed = self.maybe_fail_splice_negotiation();
6851
+
6852
+ let mut shutdown_result = self.context.force_shutdown(&self.funding, closure_reason);
6853
+ shutdown_result.splice_funding_failed = splice_funding_failed;
6854
+ shutdown_result
6855
+ }
6856
+
6857
+ fn maybe_fail_splice_negotiation(&mut self) -> Option<SpliceFundingFailed> {
6858
+ self.reset_pending_splice_state()
6813
6859
.or_else(|| {
6814
6860
self.quiescent_action.take().and_then(|quiescent_action| match quiescent_action {
6815
6861
QuiescentAction::Splice(instructions) => {
@@ -6827,11 +6873,7 @@ where
6827
6873
None
6828
6874
},
6829
6875
})
6830
- });
6831
-
6832
- let mut shutdown_result = self.context.force_shutdown(&self.funding, closure_reason);
6833
- shutdown_result.splice_funding_failed = splice_funding_failed;
6834
- shutdown_result
6876
+ })
6835
6877
}
6836
6878
6837
6879
fn interactive_tx_constructor_mut(&mut self) -> Option<&mut InteractiveTxConstructor> {
@@ -9090,24 +9132,6 @@ where
9090
9132
}
9091
9133
}
9092
9134
9093
- // Reset any quiescence-related state as it is implicitly terminated once disconnected.
9094
- if matches!(self.context.channel_state, ChannelState::ChannelReady(_)) {
9095
- if self.quiescent_action.is_some() {
9096
- // If we were trying to get quiescent, try again after reconnection.
9097
- self.context.channel_state.set_awaiting_quiescence();
9098
- }
9099
- self.context.channel_state.clear_local_stfu_sent();
9100
- self.context.channel_state.clear_remote_stfu_sent();
9101
- if self.should_reset_pending_splice_funding_negotiation().unwrap_or(true) {
9102
- // If we were in quiescence but a splice was never negotiated, or the negotiation
9103
- // failed due to disconnecting, we shouldn't be quiescent anymore upon reconnecting.
9104
- // If there was a pending splice negotiation that has failed due to disconnecting,
9105
- // we also take the opportunity to clean up our state.
9106
- self.reset_pending_splice_state();
9107
- debug_assert!(!self.context.channel_state.is_quiescent());
9108
- }
9109
- }
9110
-
9111
9135
self.context.channel_state.set_peer_disconnected();
9112
9136
log_trace!(logger, "Peer disconnection resulted in {} remote-announced HTLC drops on channel {}", inbound_drop_count, &self.context.channel_id());
9113
9137
Ok(())
@@ -11774,9 +11798,9 @@ where
11774
11798
.map_err(|e| APIError::APIMisuseError { err: e.to_owned() })
11775
11799
}
11776
11800
11777
- fn send_splice_init(
11778
- &mut self, instructions: SpliceInstructions,
11779
- ) -> Result<msgs::SpliceInit, String> {
11801
+ fn send_splice_init(&mut self, instructions: SpliceInstructions) -> msgs::SpliceInit {
11802
+ debug_assert!( self.pending_splice.is_none());
11803
+
11780
11804
let SpliceInstructions {
11781
11805
adjusted_funding_contribution,
11782
11806
our_funding_inputs,
@@ -11786,15 +11810,6 @@ where
11786
11810
locktime,
11787
11811
} = instructions;
11788
11812
11789
- // Check if a splice has been initiated already.
11790
- // Note: only a single outstanding splice is supported (per spec)
11791
- if self.pending_splice.is_some() {
11792
- return Err(format!(
11793
- "Channel {} cannot be spliced, as it has already a splice pending",
11794
- self.context.channel_id(),
11795
- ));
11796
- }
11797
-
11798
11813
let prev_funding_input = self.funding.to_splice_funding_input();
11799
11814
let context = FundingNegotiationContext {
11800
11815
is_initiator: true,
@@ -11818,14 +11833,14 @@ where
11818
11833
let prev_funding_txid = self.funding.get_funding_txid();
11819
11834
let funding_pubkey = self.context.holder_pubkeys(prev_funding_txid).funding_pubkey;
11820
11835
11821
- Ok( msgs::SpliceInit {
11836
+ msgs::SpliceInit {
11822
11837
channel_id: self.context.channel_id,
11823
11838
funding_contribution_satoshis: adjusted_funding_contribution.to_sat(),
11824
11839
funding_feerate_per_kw,
11825
11840
locktime,
11826
11841
funding_pubkey,
11827
11842
require_confirmed_inputs: None,
11828
- })
11843
+ }
11829
11844
}
11830
11845
11831
11846
/// Checks during handling splice_init
@@ -12970,10 +12985,21 @@ where
12970
12985
"Internal Error: Didn't have anything to do after reaching quiescence".to_owned()
12971
12986
));
12972
12987
},
12973
- Some(QuiescentAction::Splice(_instructions)) => {
12974
- return self.send_splice_init(_instructions)
12975
- .map(|splice_init| Some(StfuResponse::SpliceInit(splice_init)))
12976
- .map_err(|e| ChannelError::WarnAndDisconnect(e.to_owned()));
12988
+ Some(QuiescentAction::Splice(instructions)) => {
12989
+ if self.pending_splice.is_some() {
12990
+ debug_assert!(false);
12991
+ self.quiescent_action = Some(QuiescentAction::Splice(instructions));
12992
+
12993
+ return Err(ChannelError::WarnAndDisconnect(
12994
+ format!(
12995
+ "Channel {} cannot be spliced as it already has a splice pending",
12996
+ self.context.channel_id(),
12997
+ ),
12998
+ ));
12999
+ }
13000
+
13001
+ let splice_init = self.send_splice_init(instructions);
13002
+ return Ok(Some(StfuResponse::SpliceInit(splice_init)));
12977
13003
},
12978
13004
#[cfg(any(test, fuzzing))]
12979
13005
Some(QuiescentAction::DoNothing) => {
0 commit comments