Skip to content

Commit 94c8b72

Browse files
committed
Rewrite ChannelManager::unfunded_channel_count
Exposing ChannelPhase in ChannelManager has led to verbose match statements, which need to be modified each time a ChannelPhase is added. Making ChannelPhase an implementation detail of Channel would help avoid this. As a step in this direction, rewrite ChannelManager's unfunded_channel_count method to use ChannelPhase::as_funded and a new ChannelPhase::as_unfunded_v2 method.
1 parent 698632e commit 94c8b72

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

lightning/src/ln/channel.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,6 +1172,14 @@ impl<'a, SP: Deref> ChannelPhase<SP> where
11721172
None
11731173
}
11741174
}
1175+
1176+
pub fn as_unfunded_v2(&self) -> Option<&PendingV2Channel<SP>> {
1177+
if let ChannelPhase::UnfundedV2(channel) = self {
1178+
Some(channel)
1179+
} else {
1180+
None
1181+
}
1182+
}
11751183
}
11761184

11771185
/// Contains all state common to unfunded inbound/outbound channels.

lightning/src/ln/channelmanager.rs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7831,8 +7831,8 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
78317831
) -> usize {
78327832
let mut num_unfunded_channels = 0;
78337833
for (_, phase) in peer.channel_by_id.iter() {
7834-
match phase {
7835-
ChannelPhase::Funded(chan) => {
7834+
match phase.as_funded() {
7835+
Some(chan) => {
78367836
// This covers non-zero-conf inbound `Channel`s that we are currently monitoring, but those
78377837
// which have not yet had any confirmations on-chain.
78387838
if !chan.context.is_outbound() && chan.context.minimum_depth().unwrap_or(1) != 0 &&
@@ -7841,27 +7841,25 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
78417841
num_unfunded_channels += 1;
78427842
}
78437843
},
7844-
ChannelPhase::UnfundedInboundV1(chan) => {
7845-
if chan.context.minimum_depth().unwrap_or(1) != 0 {
7846-
num_unfunded_channels += 1;
7847-
}
7848-
},
7849-
ChannelPhase::UnfundedV2(chan) => {
7844+
None => {
78507845
// Outbound channels don't contribute to the unfunded count in the DoS context.
7851-
if chan.context.is_outbound() {
7846+
if phase.context().is_outbound() {
78527847
continue;
78537848
}
78547849

7855-
// Only inbound V2 channels that are not 0conf and that we do not contribute to will be
7856-
// included in the unfunded count.
7857-
if chan.context.minimum_depth().unwrap_or(1) != 0 &&
7858-
chan.dual_funding_context.our_funding_satoshis == 0 {
7859-
num_unfunded_channels += 1;
7850+
// 0conf channels are not considered unfunded.
7851+
if phase.context().minimum_depth().unwrap_or(1) == 0 {
7852+
continue;
78607853
}
7861-
},
7862-
ChannelPhase::UnfundedOutboundV1(_) => {
7863-
// Outbound channels don't contribute to the unfunded count in the DoS context.
7864-
continue;
7854+
7855+
// Inbound V2 channels with contributed inputs are not considered unfunded.
7856+
if let Some(chan) = phase.as_unfunded_v2() {
7857+
if chan.dual_funding_context.our_funding_satoshis != 0 {
7858+
continue;
7859+
}
7860+
}
7861+
7862+
num_unfunded_channels += 1;
78657863
},
78667864
}
78677865
}

0 commit comments

Comments
 (0)