Skip to content

Commit efec33f

Browse files
committed
Add V2 ChannelPhase variants
1 parent e142e4a commit efec33f

File tree

3 files changed

+122
-10
lines changed

3 files changed

+122
-10
lines changed

lightning/src/ln/channel.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,6 +1161,10 @@ impl_writeable_tlv_based!(PendingChannelMonitorUpdate, {
11611161
pub(super) enum ChannelPhase<SP: Deref> where SP::Target: SignerProvider {
11621162
UnfundedOutboundV1(OutboundV1Channel<SP>),
11631163
UnfundedInboundV1(InboundV1Channel<SP>),
1164+
#[cfg(dual_funding)]
1165+
UnfundedOutboundV2(OutboundV2Channel<SP>),
1166+
#[cfg(dual_funding)]
1167+
UnfundedInboundV2(InboundV2Channel<SP>),
11641168
Funded(Channel<SP>),
11651169
}
11661170

@@ -1173,6 +1177,10 @@ impl<'a, SP: Deref> ChannelPhase<SP> where
11731177
ChannelPhase::Funded(chan) => &chan.context,
11741178
ChannelPhase::UnfundedOutboundV1(chan) => &chan.context,
11751179
ChannelPhase::UnfundedInboundV1(chan) => &chan.context,
1180+
#[cfg(dual_funding)]
1181+
ChannelPhase::UnfundedOutboundV2(chan) => &chan.context,
1182+
#[cfg(dual_funding)]
1183+
ChannelPhase::UnfundedInboundV2(chan) => &chan.context,
11761184
}
11771185
}
11781186

@@ -1181,6 +1189,10 @@ impl<'a, SP: Deref> ChannelPhase<SP> where
11811189
ChannelPhase::Funded(ref mut chan) => &mut chan.context,
11821190
ChannelPhase::UnfundedOutboundV1(ref mut chan) => &mut chan.context,
11831191
ChannelPhase::UnfundedInboundV1(ref mut chan) => &mut chan.context,
1192+
#[cfg(dual_funding)]
1193+
ChannelPhase::UnfundedOutboundV2(ref mut chan) => &mut chan.context,
1194+
#[cfg(dual_funding)]
1195+
ChannelPhase::UnfundedInboundV2(ref mut chan) => &mut chan.context,
11841196
}
11851197
}
11861198
}

lightning/src/ln/channelmanager.rs

Lines changed: 109 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -905,7 +905,14 @@ impl <SP: Deref> PeerState<SP> where SP::Target: SignerProvider {
905905
return false
906906
}
907907
!self.channel_by_id.iter().any(|(_, phase)|
908-
matches!(phase, ChannelPhase::Funded(_) | ChannelPhase::UnfundedOutboundV1(_))
908+
match phase {
909+
ChannelPhase::Funded(_) | ChannelPhase::UnfundedOutboundV1(_) => true,
910+
ChannelPhase::UnfundedInboundV1(_) => false,
911+
#[cfg(dual_funding)]
912+
ChannelPhase::UnfundedOutboundV2(_) => true,
913+
#[cfg(dual_funding)]
914+
ChannelPhase::UnfundedInboundV2(_) => false,
915+
}
909916
)
910917
&& self.monitor_update_blocked_actions.is_empty()
911918
&& self.in_flight_monitor_updates.is_empty()
@@ -2092,6 +2099,14 @@ macro_rules! convert_chan_phase_err {
20922099
ChannelPhase::UnfundedInboundV1(channel) => {
20932100
convert_chan_phase_err!($self, $err, channel, $channel_id, UNFUNDED_CHANNEL)
20942101
},
2102+
#[cfg(dual_funding)]
2103+
ChannelPhase::UnfundedOutboundV2(channel) => {
2104+
convert_chan_phase_err!($self, $err, channel, $channel_id, UNFUNDED_CHANNEL)
2105+
},
2106+
#[cfg(dual_funding)]
2107+
ChannelPhase::UnfundedInboundV2(channel) => {
2108+
convert_chan_phase_err!($self, $err, channel, $channel_id, UNFUNDED_CHANNEL)
2109+
},
20952110
}
20962111
};
20972112
}
@@ -2958,6 +2973,13 @@ where
29582973
// Unfunded channel has no update
29592974
(None, chan_phase.context().get_counterparty_node_id())
29602975
},
2976+
// TODO(dual_funding): Combine this match arm with above once #[cfg(dual_funding)] is removed.
2977+
#[cfg(dual_funding)]
2978+
ChannelPhase::UnfundedOutboundV2(_) | ChannelPhase::UnfundedInboundV2(_) => {
2979+
self.finish_close_channel(chan_phase.context_mut().force_shutdown(false, closure_reason));
2980+
// Unfunded channel has no update
2981+
(None, chan_phase.context().get_counterparty_node_id())
2982+
},
29612983
}
29622984
} else if peer_state.inbound_channel_request_by_id.remove(channel_id).is_some() {
29632985
log_error!(logger, "Force-closing channel {}", &channel_id);
@@ -5031,6 +5053,16 @@ where
50315053
process_unfunded_channel_tick(chan_id, &mut chan.context, &mut chan.unfunded_context,
50325054
pending_msg_events, counterparty_node_id)
50335055
},
5056+
#[cfg(dual_funding)]
5057+
ChannelPhase::UnfundedInboundV2(chan) => {
5058+
process_unfunded_channel_tick(chan_id, &mut chan.context, &mut chan.unfunded_context,
5059+
pending_msg_events, counterparty_node_id)
5060+
},
5061+
#[cfg(dual_funding)]
5062+
ChannelPhase::UnfundedOutboundV2(chan) => {
5063+
process_unfunded_channel_tick(chan_id, &mut chan.context, &mut chan.unfunded_context,
5064+
pending_msg_events, counterparty_node_id)
5065+
},
50345066
}
50355067
});
50365068

@@ -6178,9 +6210,25 @@ where
61786210
num_unfunded_channels += 1;
61796211
}
61806212
},
6213+
// TODO(dual_funding): Combine this match arm with above once #[cfg(dual_funding)] is removed.
6214+
#[cfg(dual_funding)]
6215+
ChannelPhase::UnfundedInboundV2(chan) => {
6216+
// Only inbound V2 channels that are not 0conf and that we do not contribute to will be
6217+
// included in the unfunded count.
6218+
if chan.context.minimum_depth().unwrap_or(1) != 0 &&
6219+
chan.dual_funding_context.our_funding_satoshis == 0 {
6220+
num_unfunded_channels += 1;
6221+
}
6222+
},
61816223
ChannelPhase::UnfundedOutboundV1(_) => {
61826224
// Outbound channels don't contribute to the unfunded count in the DoS context.
61836225
continue;
6226+
},
6227+
// TODO(dual_funding): Combine this match arm with above once #[cfg(dual_funding)] is removed.
6228+
#[cfg(dual_funding)]
6229+
ChannelPhase::UnfundedOutboundV2(_) => {
6230+
// Outbound channels don't contribute to the unfunded count in the DoS context.
6231+
continue;
61846232
}
61856233
}
61866234
}
@@ -6603,6 +6651,14 @@ where
66036651
let mut chan = remove_channel_phase!(self, chan_phase_entry);
66046652
finish_shutdown = Some(chan.context_mut().force_shutdown(false, ClosureReason::CounterpartyCoopClosedUnfundedChannel));
66056653
},
6654+
// TODO(dual_funding): Combine this match arm with above.
6655+
#[cfg(dual_funding)]
6656+
ChannelPhase::UnfundedInboundV2(_) | ChannelPhase::UnfundedOutboundV2(_) => {
6657+
let context = phase.context_mut();
6658+
log_error!(self.logger, "Immediately closing unfunded channel {} as peer asked to cooperatively shut it down (which is unnecessary)", &msg.channel_id);
6659+
let mut chan = remove_channel_phase!(self, chan_phase_entry);
6660+
finish_shutdown = Some(chan.context_mut().force_shutdown(false, ClosureReason::CounterpartyCoopClosedUnfundedChannel));
6661+
},
66066662
}
66076663
} else {
66086664
return Err(MsgHandleErrInternal::send_err_msg_no_close(format!("Got a message for a channel from the wrong node! No such channel for the passed counterparty_node_id {}", counterparty_node_id), msg.channel_id))
@@ -8470,6 +8526,9 @@ where
84708526
match phase {
84718527
// Retain unfunded channels.
84728528
ChannelPhase::UnfundedOutboundV1(_) | ChannelPhase::UnfundedInboundV1(_) => true,
8529+
// TODO(dual_funding): Combine this match arm with above.
8530+
#[cfg(dual_funding)]
8531+
ChannelPhase::UnfundedOutboundV2(_) | ChannelPhase::UnfundedInboundV2(_) => true,
84738532
ChannelPhase::Funded(channel) => {
84748533
let res = f(channel);
84758534
if let Ok((channel_ready_opt, mut timed_out_pending_htlcs, announcement_sigs)) = res {
@@ -8939,6 +8998,14 @@ where
89398998
ChannelPhase::UnfundedInboundV1(chan) => {
89408999
&mut chan.context
89419000
},
9001+
#[cfg(dual_funding)]
9002+
ChannelPhase::UnfundedOutboundV2(chan) => {
9003+
&mut chan.context
9004+
},
9005+
#[cfg(dual_funding)]
9006+
ChannelPhase::UnfundedInboundV2(chan) => {
9007+
&mut chan.context
9008+
},
89429009
};
89439010
// Clean up for removal.
89449011
update_maps_on_chan_removal!(self, &context);
@@ -9091,12 +9158,30 @@ where
90919158
});
90929159
}
90939160

9161+
// TODO(dual_funding): Combine this match arm with above once #[cfg(dual_funding)] is removed.
9162+
#[cfg(dual_funding)]
9163+
ChannelPhase::UnfundedOutboundV2(chan) => {
9164+
pending_msg_events.push(events::MessageSendEvent::SendOpenChannelV2 {
9165+
node_id: chan.context.get_counterparty_node_id(),
9166+
msg: chan.get_open_channel_v2(self.chain_hash),
9167+
});
9168+
},
9169+
90949170
ChannelPhase::UnfundedInboundV1(_) => {
90959171
// Since unfunded inbound channel maps are cleared upon disconnecting a peer,
90969172
// they are not persisted and won't be recovered after a crash.
90979173
// Therefore, they shouldn't exist at this point.
90989174
debug_assert!(false);
90999175
}
9176+
9177+
// TODO(dual_funding): Combine this match arm with above once #[cfg(dual_funding)] is removed.
9178+
#[cfg(dual_funding)]
9179+
ChannelPhase::UnfundedInboundV2(channel) => {
9180+
// Since unfunded inbound channel maps are cleared upon disconnecting a peer,
9181+
// they are not persisted and won't be recovered after a crash.
9182+
// Therefore, they shouldn't exist at this point.
9183+
debug_assert!(false);
9184+
},
91009185
}
91019186
}
91029187
}
@@ -9174,14 +9259,29 @@ where
91749259
if peer_state_mutex_opt.is_none() { return; }
91759260
let mut peer_state_lock = peer_state_mutex_opt.unwrap().lock().unwrap();
91769261
let peer_state = &mut *peer_state_lock;
9177-
if let Some(ChannelPhase::UnfundedOutboundV1(chan)) = peer_state.channel_by_id.get_mut(&msg.channel_id) {
9178-
if let Ok(msg) = chan.maybe_handle_error_without_close(self.chain_hash, &self.fee_estimator) {
9179-
peer_state.pending_msg_events.push(events::MessageSendEvent::SendOpenChannel {
9180-
node_id: *counterparty_node_id,
9181-
msg,
9182-
});
9183-
return;
9184-
}
9262+
match peer_state.channel_by_id.get_mut(&msg.channel_id) {
9263+
Some(ChannelPhase::UnfundedOutboundV1(ref mut chan)) => {
9264+
if let Ok(msg) = chan.maybe_handle_error_without_close(self.chain_hash, &self.fee_estimator) {
9265+
peer_state.pending_msg_events.push(events::MessageSendEvent::SendOpenChannel {
9266+
node_id: *counterparty_node_id,
9267+
msg,
9268+
});
9269+
return;
9270+
}
9271+
},
9272+
#[cfg(dual_funding)]
9273+
Some(ChannelPhase::UnfundedOutboundV2(ref mut chan)) => {
9274+
if let Ok(msg) = chan.maybe_handle_error_without_close(self.chain_hash, &self.fee_estimator) {
9275+
peer_state.pending_msg_events.push(events::MessageSendEvent::SendOpenChannelV2 {
9276+
node_id: *counterparty_node_id,
9277+
msg,
9278+
});
9279+
return;
9280+
}
9281+
},
9282+
None | Some(ChannelPhase::UnfundedInboundV1(_) | ChannelPhase::Funded(_)) => (),
9283+
#[cfg(dual_funding)]
9284+
Some(ChannelPhase::UnfundedInboundV2(_)) => (),
91859285
}
91869286
}
91879287

lightning/src/ln/functional_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ fn do_test_counterparty_no_reserve(send_from_initiator: bool) {
190190
chan_context.holder_selected_channel_reserve_satoshis = 0;
191191
chan_context.holder_max_htlc_value_in_flight_msat = 100_000_000;
192192
},
193-
ChannelPhase::Funded(_) => assert!(false),
193+
_ => assert!(false),
194194
}
195195
}
196196

0 commit comments

Comments
 (0)