Skip to content

Commit 4cd52cb

Browse files
committed
Split out remaining uses of is_pre_funded_state and rename it
`Channel::is_pre_funded_state` is used to mean several different things. In the past few commits we stopped using it for a few conflicting uses, but here we break out the remaining uses and rename the remnants for clarity. `is_funding_broadcast` was using `is_pre_funded_state` and was then later used to decide if the `Channel` could be written to disk (because it can be resumed on restart), if we should broadcast a force-close transaction, and when to emit a `ChannelPending` event. These were also somewhat divergent - we shouldn't generate a `ChannelReady` event or broadcast a force-closing transaction until we've actually broadcasted but want to write the `Channel` to disk once we enter funding signature exchange for dual-funded open. Thus, the ability to write a `Channel` to disk is provided by a new `can_resume_on_restart` method. Then, `is_funding_broadcast` is updated to only consider funding broadcasted after we provide our funding signatures (i.e. the funding *could* have been broadcasted). This is still a bit early to generate a `ChannelPending` event (as the funding may not actually have been broadcasted yet), but its better than it was. Finally, the remaining `is_pre_funded_state` is renamed `can_resume_on_reconnect`, which has slightly different semantics than on-restart channels in batch opens.
1 parent b7ba29c commit 4cd52cb

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

lightning/src/ln/channel.rs

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -806,11 +806,11 @@ impl ChannelState {
806806
}
807807
}
808808

809-
fn is_pre_funded_state(&self) -> bool {
809+
fn can_resume_on_reconnect(&self) -> bool {
810810
match self {
811-
ChannelState::NegotiatingFunding(_) => true,
812-
ChannelState::FundingNegotiated(flags) => !flags.is_interactive_signing(),
813-
_ => false,
811+
ChannelState::NegotiatingFunding(_) => false,
812+
ChannelState::FundingNegotiated(flags) => flags.is_interactive_signing(),
813+
_ => true,
814814
}
815815
}
816816

@@ -4001,7 +4001,7 @@ where
40014001

40024002
// Checks whether we should emit a `ChannelPending` event.
40034003
pub(crate) fn should_emit_channel_pending_event(&mut self) -> bool {
4004-
self.is_funding_broadcast() && !self.channel_pending_event_emitted
4004+
self.is_funding_broadcastable() && !self.channel_pending_event_emitted
40054005
}
40064006

40074007
// Returns whether we already emitted a `ChannelPending` event.
@@ -4082,11 +4082,28 @@ where
40824082
self.is_manual_broadcast = true;
40834083
}
40844084

4085+
/// Returns true if this channel can be resume after a restart, implying its past the initial
4086+
/// funding negotiation stages (and any assocated batch channels are similarly past initial
4087+
/// funding negotiation).
4088+
///
4089+
/// This is equivalent to saying the channel can be persisted to disk.
4090+
pub fn can_resume_on_restart(&self) -> bool {
4091+
self.channel_state.can_resume_on_reconnect()
4092+
&& match self.channel_state {
4093+
ChannelState::AwaitingChannelReady(flags) => !flags.is_waiting_for_batch(),
4094+
_ => true,
4095+
}
4096+
}
4097+
40854098
/// Returns true if funding_signed was sent/received and the
40864099
/// funding transaction has been broadcast if necessary.
4087-
pub fn is_funding_broadcast(&self) -> bool {
4088-
!self.channel_state.is_pre_funded_state()
4089-
&& !matches!(self.channel_state, ChannelState::AwaitingChannelReady(flags) if flags.is_set(AwaitingChannelReadyFlags::WAITING_FOR_BATCH))
4100+
fn is_funding_broadcastable(&self) -> bool {
4101+
match self.channel_state {
4102+
ChannelState::NegotiatingFunding(_) => false,
4103+
ChannelState::FundingNegotiated(flags) => !flags.is_our_tx_signatures_ready(),
4104+
ChannelState::AwaitingChannelReady(flags) => !flags.is_waiting_for_batch(),
4105+
_ => true,
4106+
}
40904107
}
40914108

40924109
#[rustfmt::skip]
@@ -5432,7 +5449,7 @@ where
54325449
// be delayed in being processed! See the docs for `ChannelManagerReadArgs` for more.
54335450
assert!(!matches!(self.channel_state, ChannelState::ShutdownComplete));
54345451

5435-
let broadcast = self.is_funding_broadcast();
5452+
let broadcast = self.is_funding_broadcastable();
54365453

54375454
// We go ahead and "free" any holding cell HTLCs or HTLCs we haven't yet committed to and
54385455
// return them to fail the payment.
@@ -8306,12 +8323,12 @@ where
83068323
#[rustfmt::skip]
83078324
fn remove_uncommitted_htlcs_and_mark_paused<L: Deref>(&mut self, logger: &L) -> Result<(), ()> where L::Target: Logger {
83088325
assert!(!matches!(self.context.channel_state, ChannelState::ShutdownComplete));
8309-
if self.context.channel_state.is_pre_funded_state() {
8326+
if !self.context.channel_state.can_resume_on_reconnect() {
83108327
return Err(())
83118328
}
83128329

83138330
// We only clear `peer_disconnected` if we were able to reestablish the channel. We always
8314-
// reset our awaiting response in case we failed reestablishment and are disconnecting.
8331+
// reset our awaiting response in case we failed reestablishment and are disconnecting.
83158332
self.context.sent_message_awaiting_response = None;
83168333

83178334
if self.context.channel_state.is_peer_disconnected() {

lightning/src/ln/channelmanager.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15242,7 +15242,7 @@ where
1524215242
number_of_funded_channels += peer_state.channel_by_id
1524315243
.values()
1524415244
.filter_map(Channel::as_funded)
15245-
.filter(|chan| chan.context.is_funding_broadcast())
15245+
.filter(|chan| chan.context.can_resume_on_restart())
1524615246
.count();
1524715247
}
1524815248

@@ -15254,7 +15254,7 @@ where
1525415254
for channel in peer_state.channel_by_id
1525515255
.values()
1525615256
.filter_map(Channel::as_funded)
15257-
.filter(|channel| channel.context.is_funding_broadcast())
15257+
.filter(|channel| channel.context.can_resume_on_restart())
1525815258
{
1525915259
channel.write(writer)?;
1526015260
}

0 commit comments

Comments
 (0)