@@ -808,11 +808,11 @@ impl ChannelState {
808
808
}
809
809
}
810
810
811
- fn is_pre_funded_state (&self) -> bool {
811
+ fn can_resume_on_reconnect (&self) -> bool {
812
812
match self {
813
- ChannelState::NegotiatingFunding(_) => true ,
814
- ChannelState::FundingNegotiated(flags) => ! flags.is_interactive_signing(),
815
- _ => false ,
813
+ ChannelState::NegotiatingFunding(_) => false ,
814
+ ChannelState::FundingNegotiated(flags) => flags.is_interactive_signing(),
815
+ _ => true ,
816
816
}
817
817
}
818
818
@@ -4016,7 +4016,7 @@ where
4016
4016
4017
4017
// Checks whether we should emit a `ChannelPending` event.
4018
4018
pub(crate) fn should_emit_channel_pending_event(&mut self) -> bool {
4019
- self.is_funding_broadcast () && !self.channel_pending_event_emitted
4019
+ self.is_funding_broadcastable () && !self.channel_pending_event_emitted
4020
4020
}
4021
4021
4022
4022
// Returns whether we already emitted a `ChannelPending` event.
@@ -4097,11 +4097,28 @@ where
4097
4097
self.is_manual_broadcast = true;
4098
4098
}
4099
4099
4100
+ /// Returns true if this channel can be resume after a restart, implying its past the initial
4101
+ /// funding negotiation stages (and any assocated batch channels are similarly past initial
4102
+ /// funding negotiation).
4103
+ ///
4104
+ /// This is equivalent to saying the channel can be persisted to disk.
4105
+ pub fn can_resume_on_restart(&self) -> bool {
4106
+ self.channel_state.can_resume_on_reconnect()
4107
+ && match self.channel_state {
4108
+ ChannelState::AwaitingChannelReady(flags) => !flags.is_waiting_for_batch(),
4109
+ _ => true,
4110
+ }
4111
+ }
4112
+
4100
4113
/// Returns true if funding_signed was sent/received and the
4101
4114
/// funding transaction has been broadcast if necessary.
4102
- pub fn is_funding_broadcast(&self) -> bool {
4103
- !self.channel_state.is_pre_funded_state()
4104
- && !matches!(self.channel_state, ChannelState::AwaitingChannelReady(flags) if flags.is_set(AwaitingChannelReadyFlags::WAITING_FOR_BATCH))
4115
+ fn is_funding_broadcastable(&self) -> bool {
4116
+ match self.channel_state {
4117
+ ChannelState::NegotiatingFunding(_) => false,
4118
+ ChannelState::FundingNegotiated(flags) => !flags.is_our_tx_signatures_ready(),
4119
+ ChannelState::AwaitingChannelReady(flags) => !flags.is_waiting_for_batch(),
4120
+ _ => true,
4121
+ }
4105
4122
}
4106
4123
4107
4124
#[rustfmt::skip]
@@ -5592,7 +5609,7 @@ where
5592
5609
// be delayed in being processed! See the docs for `ChannelManagerReadArgs` for more.
5593
5610
assert!(!matches!(self.channel_state, ChannelState::ShutdownComplete));
5594
5611
5595
- let broadcast = self.is_funding_broadcast ();
5612
+ let broadcast = self.is_funding_broadcastable ();
5596
5613
5597
5614
// We go ahead and "free" any holding cell HTLCs or HTLCs we haven't yet committed to and
5598
5615
// return them to fail the payment.
@@ -5656,15 +5673,14 @@ where
5656
5673
}
5657
5674
5658
5675
let monitor_update = if let Some(funding_txo) = funding.get_funding_txo() {
5659
- // If we haven't yet exchanged funding signatures (ie channel_state < AwaitingChannelReady),
5660
- // returning a channel monitor update here would imply a channel monitor update before
5661
- // we even registered the channel monitor to begin with, which is invalid.
5662
- // Thus, if we aren't actually at a point where we could conceivably broadcast the
5663
- // funding transaction, don't return a funding txo (which prevents providing the
5664
- // monitor update to the user, even if we return one).
5665
- // See test_duplicate_chan_id and test_pre_lockin_no_chan_closed_update for more.
5666
- if !self.channel_state.is_pre_funded_state() {
5676
+ // We should only generate a closing `ChannelMonitorUpdate` if we already have a
5677
+ // `ChannelMonitor` for the disk (i.e. `counterparty_next_commitment_transaction_number`
5678
+ // has been decremented once, which hapens when we generate the initial
5679
+ // `ChannelMonitor`). Otherwise, that would imply a channel monitor update before we
5680
+ // even registered the channel monitor to begin with, which is invalid.
5681
+ if self.counterparty_next_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
5667
5682
self.latest_monitor_update_id = self.get_latest_unblocked_monitor_update_id() + 1;
5683
+
5668
5684
let update = ChannelMonitorUpdate {
5669
5685
update_id: self.latest_monitor_update_id,
5670
5686
updates: vec![ChannelMonitorUpdateStep::ChannelForceClosed {
@@ -8545,12 +8561,12 @@ where
8545
8561
#[rustfmt::skip]
8546
8562
fn remove_uncommitted_htlcs_and_mark_paused<L: Deref>(&mut self, logger: &L) -> Result<(), ()> where L::Target: Logger {
8547
8563
assert!(!matches!(self.context.channel_state, ChannelState::ShutdownComplete));
8548
- if self.context.channel_state.is_pre_funded_state () {
8564
+ if ! self.context.channel_state.can_resume_on_reconnect () {
8549
8565
return Err(())
8550
8566
}
8551
8567
8552
8568
// We only clear `peer_disconnected` if we were able to reestablish the channel. We always
8553
- // reset our awaiting response in case we failed reestablishment and are disconnecting.
8569
+ // reset our awaiting response in case we failed reestablishment and are disconnecting.
8554
8570
self.context.sent_message_awaiting_response = None;
8555
8571
8556
8572
if self.context.channel_state.is_peer_disconnected() {
@@ -9544,13 +9560,20 @@ where
9544
9560
"Peer sent shutdown when we needed a channel_reestablish".to_owned(),
9545
9561
));
9546
9562
}
9547
- if self.context.channel_state.is_pre_funded_state() {
9563
+ let mut not_broadcasted =
9564
+ matches!(self.context.channel_state, ChannelState::NegotiatingFunding(_));
9565
+ if let ChannelState::FundingNegotiated(flags) = &self.context.channel_state {
9566
+ if !flags.is_our_tx_signatures_ready() {
9567
+ // If we're a V1 channel or we haven't yet sent our `tx_signatures`, the funding tx
9568
+ // couldn't be broadcasted yet, so just short-circuit the shutdown logic.
9569
+ not_broadcasted = true;
9570
+ }
9571
+ }
9572
+ if not_broadcasted {
9548
9573
// Spec says we should fail the connection, not the channel, but that's nonsense, there
9549
9574
// are plenty of reasons you may want to fail a channel pre-funding, and spec says you
9550
9575
// can do that via error message without getting a connection fail anyway...
9551
- return Err(ChannelError::close(
9552
- "Peer sent shutdown pre-funding generation".to_owned(),
9553
- ));
9576
+ return Err(ChannelError::close("Shutdown before funding was broadcasted".to_owned()));
9554
9577
}
9555
9578
for htlc in self.context.pending_inbound_htlcs.iter() {
9556
9579
if let InboundHTLCState::RemoteAnnounced(_) = htlc.state {
0 commit comments