Skip to content

Commit a9bbb24

Browse files
authored
Merge pull request #4021 from TheBlueMatt/2025-08-pre-funded-state-checks
Clean up and split up `is_pre_funded_state`
2 parents b39c104 + 4cd52cb commit a9bbb24

File tree

2 files changed

+48
-25
lines changed

2 files changed

+48
-25
lines changed

lightning/src/ln/channel.rs

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -808,11 +808,11 @@ impl ChannelState {
808808
}
809809
}
810810

811-
fn is_pre_funded_state(&self) -> bool {
811+
fn can_resume_on_reconnect(&self) -> bool {
812812
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,
816816
}
817817
}
818818

@@ -4016,7 +4016,7 @@ where
40164016

40174017
// Checks whether we should emit a `ChannelPending` event.
40184018
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
40204020
}
40214021

40224022
// Returns whether we already emitted a `ChannelPending` event.
@@ -4097,11 +4097,28 @@ where
40974097
self.is_manual_broadcast = true;
40984098
}
40994099

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+
41004113
/// Returns true if funding_signed was sent/received and the
41014114
/// 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+
}
41054122
}
41064123

41074124
#[rustfmt::skip]
@@ -5592,7 +5609,7 @@ where
55925609
// be delayed in being processed! See the docs for `ChannelManagerReadArgs` for more.
55935610
assert!(!matches!(self.channel_state, ChannelState::ShutdownComplete));
55945611

5595-
let broadcast = self.is_funding_broadcast();
5612+
let broadcast = self.is_funding_broadcastable();
55965613

55975614
// We go ahead and "free" any holding cell HTLCs or HTLCs we haven't yet committed to and
55985615
// return them to fail the payment.
@@ -5656,15 +5673,14 @@ where
56565673
}
56575674

56585675
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 {
56675682
self.latest_monitor_update_id = self.get_latest_unblocked_monitor_update_id() + 1;
5683+
56685684
let update = ChannelMonitorUpdate {
56695685
update_id: self.latest_monitor_update_id,
56705686
updates: vec![ChannelMonitorUpdateStep::ChannelForceClosed {
@@ -8545,12 +8561,12 @@ where
85458561
#[rustfmt::skip]
85468562
fn remove_uncommitted_htlcs_and_mark_paused<L: Deref>(&mut self, logger: &L) -> Result<(), ()> where L::Target: Logger {
85478563
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() {
85498565
return Err(())
85508566
}
85518567

85528568
// 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.
85548570
self.context.sent_message_awaiting_response = None;
85558571

85568572
if self.context.channel_state.is_peer_disconnected() {
@@ -9544,13 +9560,20 @@ where
95449560
"Peer sent shutdown when we needed a channel_reestablish".to_owned(),
95459561
));
95469562
}
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 {
95489573
// Spec says we should fail the connection, not the channel, but that's nonsense, there
95499574
// are plenty of reasons you may want to fail a channel pre-funding, and spec says you
95509575
// 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()));
95549577
}
95559578
for htlc in self.context.pending_inbound_htlcs.iter() {
95569579
if let InboundHTLCState::RemoteAnnounced(_) = htlc.state {

lightning/src/ln/channelmanager.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15265,7 +15265,7 @@ where
1526515265
number_of_funded_channels += peer_state.channel_by_id
1526615266
.values()
1526715267
.filter_map(Channel::as_funded)
15268-
.filter(|chan| chan.context.is_funding_broadcast())
15268+
.filter(|chan| chan.context.can_resume_on_restart())
1526915269
.count();
1527015270
}
1527115271

@@ -15277,7 +15277,7 @@ where
1527715277
for channel in peer_state.channel_by_id
1527815278
.values()
1527915279
.filter_map(Channel::as_funded)
15280-
.filter(|channel| channel.context.is_funding_broadcast())
15280+
.filter(|channel| channel.context.can_resume_on_restart())
1528115281
{
1528215282
channel.write(writer)?;
1528315283
}

0 commit comments

Comments
 (0)