Skip to content

Commit d6637d7

Browse files
committed
Combine UnfundedInboundV2 and UnfundedOutboundV2
Now that InboundV2Channel and OutboundV2Channel have been combined into a PendingV2Channel, only one ChannelPhase variant is needed.
1 parent 270c5ab commit d6637d7

File tree

2 files changed

+54
-76
lines changed

2 files changed

+54
-76
lines changed

lightning/src/ln/channel.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -1127,9 +1127,7 @@ pub(super) enum ChannelPhase<SP: Deref> where SP::Target: SignerProvider {
11271127
UnfundedOutboundV1(OutboundV1Channel<SP>),
11281128
UnfundedInboundV1(InboundV1Channel<SP>),
11291129
#[allow(dead_code)] // TODO(dual_funding): Remove once creating V2 channels is enabled.
1130-
UnfundedOutboundV2(PendingV2Channel<SP>),
1131-
#[allow(dead_code)] // TODO(dual_funding): Remove once accepting V2 channels is enabled.
1132-
UnfundedInboundV2(PendingV2Channel<SP>),
1130+
UnfundedV2(PendingV2Channel<SP>),
11331131
Funded(Channel<SP>),
11341132
}
11351133

@@ -1142,8 +1140,7 @@ impl<'a, SP: Deref> ChannelPhase<SP> where
11421140
ChannelPhase::Funded(chan) => &chan.context,
11431141
ChannelPhase::UnfundedOutboundV1(chan) => &chan.context,
11441142
ChannelPhase::UnfundedInboundV1(chan) => &chan.context,
1145-
ChannelPhase::UnfundedOutboundV2(chan) => &chan.context,
1146-
ChannelPhase::UnfundedInboundV2(chan) => &chan.context,
1143+
ChannelPhase::UnfundedV2(chan) => &chan.context,
11471144
}
11481145
}
11491146

@@ -1152,8 +1149,7 @@ impl<'a, SP: Deref> ChannelPhase<SP> where
11521149
ChannelPhase::Funded(ref mut chan) => &mut chan.context,
11531150
ChannelPhase::UnfundedOutboundV1(ref mut chan) => &mut chan.context,
11541151
ChannelPhase::UnfundedInboundV1(ref mut chan) => &mut chan.context,
1155-
ChannelPhase::UnfundedOutboundV2(ref mut chan) => &mut chan.context,
1156-
ChannelPhase::UnfundedInboundV2(ref mut chan) => &mut chan.context,
1152+
ChannelPhase::UnfundedV2(ref mut chan) => &mut chan.context,
11571153
}
11581154
}
11591155
}

lightning/src/ln/channelmanager.rs

+51-69
Original file line numberDiff line numberDiff line change
@@ -1370,8 +1370,7 @@ impl <SP: Deref> PeerState<SP> where SP::Target: SignerProvider {
13701370
match phase {
13711371
ChannelPhase::Funded(_) | ChannelPhase::UnfundedOutboundV1(_) => true,
13721372
ChannelPhase::UnfundedInboundV1(_) => false,
1373-
ChannelPhase::UnfundedOutboundV2(_) => true,
1374-
ChannelPhase::UnfundedInboundV2(_) => false,
1373+
ChannelPhase::UnfundedV2(chan) => chan.context.is_outbound(),
13751374
}
13761375
)
13771376
&& self.monitor_update_blocked_actions.is_empty()
@@ -3048,10 +3047,7 @@ macro_rules! convert_chan_phase_err {
30483047
ChannelPhase::UnfundedInboundV1(channel) => {
30493048
convert_chan_phase_err!($self, $peer_state, $err, channel, $channel_id, UNFUNDED_CHANNEL)
30503049
},
3051-
ChannelPhase::UnfundedOutboundV2(channel) => {
3052-
convert_chan_phase_err!($self, $peer_state, $err, channel, $channel_id, UNFUNDED_CHANNEL)
3053-
},
3054-
ChannelPhase::UnfundedInboundV2(channel) => {
3050+
ChannelPhase::UnfundedV2(channel) => {
30553051
convert_chan_phase_err!($self, $peer_state, $err, channel, $channel_id, UNFUNDED_CHANNEL)
30563052
},
30573053
}
@@ -4114,7 +4110,7 @@ where
41144110
)
41154111
},
41164112
ChannelPhase::UnfundedOutboundV1(_) | ChannelPhase::UnfundedInboundV1(_) |
4117-
ChannelPhase::UnfundedOutboundV2(_) | ChannelPhase::UnfundedInboundV2(_) => {
4113+
ChannelPhase::UnfundedV2(_) => {
41184114
// Unfunded channel has no update
41194115
(chan_phase_entry.get_mut().context_mut().force_shutdown(false, closure_reason), None)
41204116
},
@@ -6540,10 +6536,7 @@ where
65406536
ChannelPhase::UnfundedOutboundV1(chan) => {
65416537
process_unfunded_channel_tick!(peer_state, chan, pending_msg_events)
65426538
},
6543-
ChannelPhase::UnfundedInboundV2(chan) => {
6544-
process_unfunded_channel_tick!(peer_state, chan, pending_msg_events)
6545-
},
6546-
ChannelPhase::UnfundedOutboundV2(chan) => {
6539+
ChannelPhase::UnfundedV2(chan) => {
65476540
process_unfunded_channel_tick!(peer_state, chan, pending_msg_events)
65486541
},
65496542
}
@@ -7721,7 +7714,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
77217714
node_id: channel.context.get_counterparty_node_id(),
77227715
msg: channel.accept_inbound_dual_funded_channel()
77237716
};
7724-
(channel.context.channel_id(), ChannelPhase::UnfundedInboundV2(channel), Some(message_send_event))
7717+
(channel.context.channel_id(), ChannelPhase::UnfundedV2(channel), Some(message_send_event))
77257718
})
77267719
},
77277720
}
@@ -7840,15 +7833,20 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
78407833
num_unfunded_channels += 1;
78417834
}
78427835
},
7843-
ChannelPhase::UnfundedInboundV2(chan) => {
7836+
ChannelPhase::UnfundedV2(chan) => {
7837+
// Outbound channels don't contribute to the unfunded count in the DoS context.
7838+
if chan.context.is_outbound() {
7839+
continue;
7840+
}
7841+
78447842
// Only inbound V2 channels that are not 0conf and that we do not contribute to will be
78457843
// included in the unfunded count.
78467844
if chan.context.minimum_depth().unwrap_or(1) != 0 &&
78477845
chan.dual_funding_context.our_funding_satoshis == 0 {
78487846
num_unfunded_channels += 1;
78497847
}
78507848
},
7851-
ChannelPhase::UnfundedOutboundV1(_) | ChannelPhase::UnfundedOutboundV2(_) => {
7849+
ChannelPhase::UnfundedOutboundV1(_) => {
78527850
// Outbound channels don't contribute to the unfunded count in the DoS context.
78537851
continue;
78547852
},
@@ -7996,7 +7994,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
79967994
node_id: *counterparty_node_id,
79977995
msg: channel.accept_inbound_dual_funded_channel(),
79987996
};
7999-
(ChannelPhase::UnfundedInboundV2(channel), Some(message_send_event))
7997+
(ChannelPhase::UnfundedV2(channel), Some(message_send_event))
80007998
},
80017999
};
80028000

@@ -8243,10 +8241,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
82438241
fn internal_tx_add_input(&self, counterparty_node_id: PublicKey, msg: &msgs::TxAddInput) -> Result<(), MsgHandleErrInternal> {
82448242
self.internal_tx_msg(&counterparty_node_id, msg.channel_id, |channel_phase: &mut ChannelPhase<SP>| {
82458243
match channel_phase {
8246-
ChannelPhase::UnfundedInboundV2(ref mut channel) => {
8247-
Ok(channel.tx_add_input(msg).into_msg_send_event(counterparty_node_id))
8248-
},
8249-
ChannelPhase::UnfundedOutboundV2(ref mut channel) => {
8244+
ChannelPhase::UnfundedV2(ref mut channel) => {
82508245
Ok(channel.tx_add_input(msg).into_msg_send_event(counterparty_node_id))
82518246
},
82528247
_ => Err("tx_add_input"),
@@ -8257,10 +8252,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
82578252
fn internal_tx_add_output(&self, counterparty_node_id: PublicKey, msg: &msgs::TxAddOutput) -> Result<(), MsgHandleErrInternal> {
82588253
self.internal_tx_msg(&counterparty_node_id, msg.channel_id, |channel_phase: &mut ChannelPhase<SP>| {
82598254
match channel_phase {
8260-
ChannelPhase::UnfundedInboundV2(ref mut channel) => {
8261-
Ok(channel.tx_add_output(msg).into_msg_send_event(counterparty_node_id))
8262-
},
8263-
ChannelPhase::UnfundedOutboundV2(ref mut channel) => {
8255+
ChannelPhase::UnfundedV2(ref mut channel) => {
82648256
Ok(channel.tx_add_output(msg).into_msg_send_event(counterparty_node_id))
82658257
},
82668258
_ => Err("tx_add_output"),
@@ -8271,10 +8263,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
82718263
fn internal_tx_remove_input(&self, counterparty_node_id: PublicKey, msg: &msgs::TxRemoveInput) -> Result<(), MsgHandleErrInternal> {
82728264
self.internal_tx_msg(&counterparty_node_id, msg.channel_id, |channel_phase: &mut ChannelPhase<SP>| {
82738265
match channel_phase {
8274-
ChannelPhase::UnfundedInboundV2(ref mut channel) => {
8275-
Ok(channel.tx_remove_input(msg).into_msg_send_event(counterparty_node_id))
8276-
},
8277-
ChannelPhase::UnfundedOutboundV2(ref mut channel) => {
8266+
ChannelPhase::UnfundedV2(ref mut channel) => {
82788267
Ok(channel.tx_remove_input(msg).into_msg_send_event(counterparty_node_id))
82798268
},
82808269
_ => Err("tx_remove_input"),
@@ -8285,10 +8274,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
82858274
fn internal_tx_remove_output(&self, counterparty_node_id: PublicKey, msg: &msgs::TxRemoveOutput) -> Result<(), MsgHandleErrInternal> {
82868275
self.internal_tx_msg(&counterparty_node_id, msg.channel_id, |channel_phase: &mut ChannelPhase<SP>| {
82878276
match channel_phase {
8288-
ChannelPhase::UnfundedInboundV2(ref mut channel) => {
8289-
Ok(channel.tx_remove_output(msg).into_msg_send_event(counterparty_node_id))
8290-
},
8291-
ChannelPhase::UnfundedOutboundV2(ref mut channel) => {
8277+
ChannelPhase::UnfundedV2(ref mut channel) => {
82928278
Ok(channel.tx_remove_output(msg).into_msg_send_event(counterparty_node_id))
82938279
},
82948280
_ => Err("tx_remove_output"),
@@ -8311,9 +8297,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
83118297
hash_map::Entry::Occupied(mut chan_phase_entry) => {
83128298
let channel_phase = chan_phase_entry.get_mut();
83138299
let (msg_send_event_opt, signing_session_opt) = match channel_phase {
8314-
ChannelPhase::UnfundedInboundV2(channel) => channel.tx_complete(msg)
8315-
.into_msg_send_event_or_signing_session(counterparty_node_id),
8316-
ChannelPhase::UnfundedOutboundV2(channel) => channel.tx_complete(msg)
8300+
ChannelPhase::UnfundedV2(channel) => channel.tx_complete(msg)
83178301
.into_msg_send_event_or_signing_session(counterparty_node_id),
83188302
_ => try_chan_phase_entry!(self, peer_state, Err(ChannelError::Close(
83198303
(
@@ -8326,10 +8310,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
83268310
};
83278311
if let Some(mut signing_session) = signing_session_opt {
83288312
let (commitment_signed, funding_ready_for_sig_event_opt) = match chan_phase_entry.get_mut() {
8329-
ChannelPhase::UnfundedOutboundV2(chan) => {
8330-
chan.funding_tx_constructed(&mut signing_session, &self.logger)
8331-
},
8332-
ChannelPhase::UnfundedInboundV2(chan) => {
8313+
ChannelPhase::UnfundedV2(chan) => {
83338314
chan.funding_tx_constructed(&mut signing_session, &self.logger)
83348315
},
83358316
_ => Err(ChannelError::Warn(
@@ -8338,8 +8319,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
83388319
}.map_err(|err| MsgHandleErrInternal::send_err_msg_no_close(format!("{}", err), msg.channel_id))?;
83398320
let (channel_id, channel_phase) = chan_phase_entry.remove_entry();
83408321
let channel = match channel_phase {
8341-
ChannelPhase::UnfundedOutboundV2(chan) => chan.into_channel(signing_session),
8342-
ChannelPhase::UnfundedInboundV2(chan) => chan.into_channel(signing_session),
8322+
ChannelPhase::UnfundedV2(chan) => chan.into_channel(signing_session),
83438323
_ => {
83448324
debug_assert!(false); // It cannot be another variant as we are in the `Ok` branch of the above match.
83458325
Err(ChannelError::Warn(
@@ -8435,8 +8415,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
84358415
hash_map::Entry::Occupied(mut chan_phase_entry) => {
84368416
let channel_phase = chan_phase_entry.get_mut();
84378417
let tx_constructor = match channel_phase {
8438-
ChannelPhase::UnfundedInboundV2(chan) => &mut chan.interactive_tx_constructor,
8439-
ChannelPhase::UnfundedOutboundV2(chan) => &mut chan.interactive_tx_constructor,
8418+
ChannelPhase::UnfundedV2(chan) => &mut chan.interactive_tx_constructor,
84408419
ChannelPhase::Funded(_) => {
84418420
// TODO(splicing)/TODO(RBF): We'll also be doing interactive tx construction
84428421
// for a "ChannelPhase::Funded" when we want to bump the fee on an interactively
@@ -8583,7 +8562,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
85838562
}
85848563
},
85858564
ChannelPhase::UnfundedInboundV1(_) | ChannelPhase::UnfundedOutboundV1(_) |
8586-
ChannelPhase::UnfundedInboundV2(_) | ChannelPhase::UnfundedOutboundV2(_) => {
8565+
ChannelPhase::UnfundedV2(_) => {
85878566
let context = phase.context_mut();
85888567
let logger = WithChannelContext::from(&self.logger, context, None);
85898568
log_error!(logger, "Immediately closing unfunded channel {} as peer asked to cooperatively shut it down (which is unnecessary)", &msg.channel_id);
@@ -9560,7 +9539,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
95609539
}
95619540
None
95629541
},
9563-
ChannelPhase::UnfundedInboundV2(_) | ChannelPhase::UnfundedOutboundV2(_) => None,
9542+
ChannelPhase::UnfundedV2(_) => None,
95649543
}
95659544
};
95669545

@@ -10997,7 +10976,7 @@ where
1099710976
match phase {
1099810977
// Retain unfunded channels.
1099910978
ChannelPhase::UnfundedOutboundV1(_) | ChannelPhase::UnfundedInboundV1(_) |
11000-
ChannelPhase::UnfundedOutboundV2(_) | ChannelPhase::UnfundedInboundV2(_) => true,
10979+
ChannelPhase::UnfundedV2(_) => true,
1100110980
ChannelPhase::Funded(channel) => {
1100210981
let res = f(channel);
1100310982
if let Ok((channel_ready_opt, mut timed_out_pending_htlcs, announcement_sigs)) = res {
@@ -11525,10 +11504,7 @@ where
1152511504
ChannelPhase::UnfundedInboundV1(chan) => {
1152611505
&mut chan.context
1152711506
},
11528-
ChannelPhase::UnfundedOutboundV2(chan) => {
11529-
&mut chan.context
11530-
},
11531-
ChannelPhase::UnfundedInboundV2(chan) => {
11507+
ChannelPhase::UnfundedV2(chan) => {
1153211508
&mut chan.context
1153311509
},
1153411510
};
@@ -11681,8 +11657,7 @@ where
1168111657
node_id: chan.context.get_counterparty_node_id(),
1168211658
msg: chan.get_channel_reestablish(&&logger),
1168311659
});
11684-
}
11685-
11660+
},
1168611661
ChannelPhase::UnfundedOutboundV1(chan) => {
1168711662
let logger = WithChannelContext::from(&self.logger, &chan.context, None);
1168811663
if let Some(msg) = chan.get_open_channel(self.chain_hash, &&logger) {
@@ -11691,21 +11666,26 @@ where
1169111666
msg,
1169211667
});
1169311668
}
11694-
}
11695-
11696-
ChannelPhase::UnfundedOutboundV2(chan) => {
11697-
pending_msg_events.push(events::MessageSendEvent::SendOpenChannelV2 {
11698-
node_id: chan.context.get_counterparty_node_id(),
11699-
msg: chan.get_open_channel_v2(self.chain_hash),
11700-
});
1170111669
},
11702-
11703-
ChannelPhase::UnfundedInboundV1(_) | ChannelPhase::UnfundedInboundV2(_) => {
11670+
ChannelPhase::UnfundedV2(chan) => {
11671+
if chan.context.is_outbound() {
11672+
pending_msg_events.push(events::MessageSendEvent::SendOpenChannelV2 {
11673+
node_id: chan.context.get_counterparty_node_id(),
11674+
msg: chan.get_open_channel_v2(self.chain_hash),
11675+
});
11676+
} else {
11677+
// Since unfunded inbound channel maps are cleared upon disconnecting a peer,
11678+
// they are not persisted and won't be recovered after a crash.
11679+
// Therefore, they shouldn't exist at this point.
11680+
debug_assert!(false);
11681+
}
11682+
},
11683+
ChannelPhase::UnfundedInboundV1(_) => {
1170411684
// Since unfunded inbound channel maps are cleared upon disconnecting a peer,
1170511685
// they are not persisted and won't be recovered after a crash.
1170611686
// Therefore, they shouldn't exist at this point.
1170711687
debug_assert!(false);
11708-
}
11688+
},
1170911689
}
1171011690
}
1171111691
}
@@ -11803,16 +11783,18 @@ where
1180311783
return;
1180411784
}
1180511785
},
11806-
Some(ChannelPhase::UnfundedOutboundV2(ref mut chan)) => {
11807-
if let Ok(msg) = chan.maybe_handle_error_without_close(self.chain_hash, &self.fee_estimator) {
11808-
peer_state.pending_msg_events.push(events::MessageSendEvent::SendOpenChannelV2 {
11809-
node_id: counterparty_node_id,
11810-
msg,
11811-
});
11812-
return;
11786+
Some(ChannelPhase::UnfundedV2(ref mut chan)) => {
11787+
if chan.context.is_outbound() {
11788+
if let Ok(msg) = chan.maybe_handle_error_without_close(self.chain_hash, &self.fee_estimator) {
11789+
peer_state.pending_msg_events.push(events::MessageSendEvent::SendOpenChannelV2 {
11790+
node_id: counterparty_node_id,
11791+
msg,
11792+
});
11793+
return;
11794+
}
1181311795
}
1181411796
},
11815-
None | Some(ChannelPhase::UnfundedInboundV1(_) | ChannelPhase::UnfundedInboundV2(_) | ChannelPhase::Funded(_)) => (),
11797+
None | Some(ChannelPhase::UnfundedInboundV1(_) | ChannelPhase::Funded(_)) => (),
1181611798
}
1181711799
}
1181811800

0 commit comments

Comments
 (0)