Skip to content

Commit db0bc60

Browse files
committed
Add V2 ChannelPhase variants
1 parent 29734c7 commit db0bc60

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);
@@ -5035,6 +5057,16 @@ where
50355057
process_unfunded_channel_tick(chan_id, &mut chan.context, &mut chan.unfunded_context,
50365058
pending_msg_events, counterparty_node_id)
50375059
},
5060+
#[cfg(dual_funding)]
5061+
ChannelPhase::UnfundedInboundV2(chan) => {
5062+
process_unfunded_channel_tick(chan_id, &mut chan.context, &mut chan.unfunded_context,
5063+
pending_msg_events, counterparty_node_id)
5064+
},
5065+
#[cfg(dual_funding)]
5066+
ChannelPhase::UnfundedOutboundV2(chan) => {
5067+
process_unfunded_channel_tick(chan_id, &mut chan.context, &mut chan.unfunded_context,
5068+
pending_msg_events, counterparty_node_id)
5069+
},
50385070
}
50395071
});
50405072

@@ -6182,9 +6214,25 @@ where
61826214
num_unfunded_channels += 1;
61836215
}
61846216
},
6217+
// TODO(dual_funding): Combine this match arm with above once #[cfg(dual_funding)] is removed.
6218+
#[cfg(dual_funding)]
6219+
ChannelPhase::UnfundedInboundV2(chan) => {
6220+
// Only inbound V2 channels that are not 0conf and that we do not contribute to will be
6221+
// included in the unfunded count.
6222+
if chan.context.minimum_depth().unwrap_or(1) != 0 &&
6223+
chan.dual_funding_context.our_funding_satoshis == 0 {
6224+
num_unfunded_channels += 1;
6225+
}
6226+
},
61856227
ChannelPhase::UnfundedOutboundV1(_) => {
61866228
// Outbound channels don't contribute to the unfunded count in the DoS context.
61876229
continue;
6230+
},
6231+
// TODO(dual_funding): Combine this match arm with above once #[cfg(dual_funding)] is removed.
6232+
#[cfg(dual_funding)]
6233+
ChannelPhase::UnfundedOutboundV2(_) => {
6234+
// Outbound channels don't contribute to the unfunded count in the DoS context.
6235+
continue;
61886236
}
61896237
}
61906238
}
@@ -6607,6 +6655,14 @@ where
66076655
let mut chan = remove_channel_phase!(self, chan_phase_entry);
66086656
finish_shutdown = Some(chan.context_mut().force_shutdown(false, ClosureReason::CounterpartyCoopClosedUnfundedChannel));
66096657
},
6658+
// TODO(dual_funding): Combine this match arm with above.
6659+
#[cfg(dual_funding)]
6660+
ChannelPhase::UnfundedInboundV2(_) | ChannelPhase::UnfundedOutboundV2(_) => {
6661+
let context = phase.context_mut();
6662+
log_error!(self.logger, "Immediately closing unfunded channel {} as peer asked to cooperatively shut it down (which is unnecessary)", &msg.channel_id);
6663+
let mut chan = remove_channel_phase!(self, chan_phase_entry);
6664+
finish_shutdown = Some(chan.context_mut().force_shutdown(false, ClosureReason::CounterpartyCoopClosedUnfundedChannel));
6665+
},
66106666
}
66116667
} else {
66126668
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))
@@ -8474,6 +8530,9 @@ where
84748530
match phase {
84758531
// Retain unfunded channels.
84768532
ChannelPhase::UnfundedOutboundV1(_) | ChannelPhase::UnfundedInboundV1(_) => true,
8533+
// TODO(dual_funding): Combine this match arm with above.
8534+
#[cfg(dual_funding)]
8535+
ChannelPhase::UnfundedOutboundV2(_) | ChannelPhase::UnfundedInboundV2(_) => true,
84778536
ChannelPhase::Funded(channel) => {
84788537
let res = f(channel);
84798538
if let Ok((channel_ready_opt, mut timed_out_pending_htlcs, announcement_sigs)) = res {
@@ -8943,6 +9002,14 @@ where
89439002
ChannelPhase::UnfundedInboundV1(chan) => {
89449003
&mut chan.context
89459004
},
9005+
#[cfg(dual_funding)]
9006+
ChannelPhase::UnfundedOutboundV2(chan) => {
9007+
&mut chan.context
9008+
},
9009+
#[cfg(dual_funding)]
9010+
ChannelPhase::UnfundedInboundV2(chan) => {
9011+
&mut chan.context
9012+
},
89469013
};
89479014
// Clean up for removal.
89489015
update_maps_on_chan_removal!(self, &context);
@@ -9095,12 +9162,30 @@ where
90959162
});
90969163
}
90979164

9165+
// TODO(dual_funding): Combine this match arm with above once #[cfg(dual_funding)] is removed.
9166+
#[cfg(dual_funding)]
9167+
ChannelPhase::UnfundedOutboundV2(chan) => {
9168+
pending_msg_events.push(events::MessageSendEvent::SendOpenChannelV2 {
9169+
node_id: chan.context.get_counterparty_node_id(),
9170+
msg: chan.get_open_channel_v2(self.chain_hash),
9171+
});
9172+
},
9173+
90989174
ChannelPhase::UnfundedInboundV1(_) => {
90999175
// Since unfunded inbound channel maps are cleared upon disconnecting a peer,
91009176
// they are not persisted and won't be recovered after a crash.
91019177
// Therefore, they shouldn't exist at this point.
91029178
debug_assert!(false);
91039179
}
9180+
9181+
// TODO(dual_funding): Combine this match arm with above once #[cfg(dual_funding)] is removed.
9182+
#[cfg(dual_funding)]
9183+
ChannelPhase::UnfundedInboundV2(channel) => {
9184+
// Since unfunded inbound channel maps are cleared upon disconnecting a peer,
9185+
// they are not persisted and won't be recovered after a crash.
9186+
// Therefore, they shouldn't exist at this point.
9187+
debug_assert!(false);
9188+
},
91049189
}
91059190
}
91069191
}
@@ -9178,14 +9263,29 @@ where
91789263
if peer_state_mutex_opt.is_none() { return; }
91799264
let mut peer_state_lock = peer_state_mutex_opt.unwrap().lock().unwrap();
91809265
let peer_state = &mut *peer_state_lock;
9181-
if let Some(ChannelPhase::UnfundedOutboundV1(chan)) = peer_state.channel_by_id.get_mut(&msg.channel_id) {
9182-
if let Ok(msg) = chan.maybe_handle_error_without_close(self.chain_hash, &self.fee_estimator) {
9183-
peer_state.pending_msg_events.push(events::MessageSendEvent::SendOpenChannel {
9184-
node_id: *counterparty_node_id,
9185-
msg,
9186-
});
9187-
return;
9188-
}
9266+
match peer_state.channel_by_id.get_mut(&msg.channel_id) {
9267+
Some(ChannelPhase::UnfundedOutboundV1(ref mut chan)) => {
9268+
if let Ok(msg) = chan.maybe_handle_error_without_close(self.chain_hash, &self.fee_estimator) {
9269+
peer_state.pending_msg_events.push(events::MessageSendEvent::SendOpenChannel {
9270+
node_id: *counterparty_node_id,
9271+
msg,
9272+
});
9273+
return;
9274+
}
9275+
},
9276+
#[cfg(dual_funding)]
9277+
Some(ChannelPhase::UnfundedOutboundV2(ref mut chan)) => {
9278+
if let Ok(msg) = chan.maybe_handle_error_without_close(self.chain_hash, &self.fee_estimator) {
9279+
peer_state.pending_msg_events.push(events::MessageSendEvent::SendOpenChannelV2 {
9280+
node_id: *counterparty_node_id,
9281+
msg,
9282+
});
9283+
return;
9284+
}
9285+
},
9286+
None | Some(ChannelPhase::UnfundedInboundV1(_) | ChannelPhase::Funded(_)) => (),
9287+
#[cfg(dual_funding)]
9288+
Some(ChannelPhase::UnfundedInboundV2(_)) => (),
91899289
}
91909290
}
91919291

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)