Skip to content

Commit 86bce60

Browse files
committed
Add V2 ChannelPhase variants
1 parent 4c84a41 commit 86bce60

File tree

3 files changed

+74
-4
lines changed

3 files changed

+74
-4
lines changed

lightning/src/ln/channel.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,10 @@ impl_writeable_tlv_based!(PendingChannelMonitorUpdate, {
678678
pub(super) enum ChannelPhase<SP: Deref> where SP::Target: SignerProvider {
679679
UnfundedOutboundV1(OutboundV1Channel<SP>),
680680
UnfundedInboundV1(InboundV1Channel<SP>),
681+
#[cfg(dual_funding)]
682+
UnfundedOutboundV2(OutboundV2Channel<SP>),
683+
#[cfg(dual_funding)]
684+
UnfundedInboundV2(InboundV2Channel<SP>),
681685
Funded(Channel<SP>),
682686
}
683687

@@ -690,6 +694,10 @@ impl<'a, SP: Deref> ChannelPhase<SP> where
690694
ChannelPhase::Funded(chan) => &chan.context,
691695
ChannelPhase::UnfundedOutboundV1(chan) => &chan.context,
692696
ChannelPhase::UnfundedInboundV1(chan) => &chan.context,
697+
#[cfg(dual_funding)]
698+
ChannelPhase::UnfundedOutboundV2(chan) => &chan.context,
699+
#[cfg(dual_funding)]
700+
ChannelPhase::UnfundedInboundV2(chan) => &chan.context,
693701
}
694702
}
695703

@@ -698,6 +706,10 @@ impl<'a, SP: Deref> ChannelPhase<SP> where
698706
ChannelPhase::Funded(ref mut chan) => &mut chan.context,
699707
ChannelPhase::UnfundedOutboundV1(ref mut chan) => &mut chan.context,
700708
ChannelPhase::UnfundedInboundV1(ref mut chan) => &mut chan.context,
709+
#[cfg(dual_funding)]
710+
ChannelPhase::UnfundedOutboundV2(ref mut chan) => &mut chan.context,
711+
#[cfg(dual_funding)]
712+
ChannelPhase::UnfundedInboundV2(ref mut chan) => &mut chan.context,
701713
}
702714
}
703715
}

lightning/src/ln/channelmanager.rs

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2004,6 +2004,14 @@ macro_rules! convert_chan_phase_err {
20042004
ChannelPhase::UnfundedInboundV1(channel) => {
20052005
convert_chan_phase_err!($self, $err, channel, $channel_id, UNFUNDED_CHANNEL)
20062006
},
2007+
#[cfg(dual_funding)]
2008+
ChannelPhase::UnfundedOutboundV2(channel) => {
2009+
convert_chan_phase_err!($self, $err, channel, $channel_id, UNFUNDED_CHANNEL)
2010+
},
2011+
#[cfg(dual_funding)]
2012+
ChannelPhase::UnfundedInboundV2(channel) => {
2013+
convert_chan_phase_err!($self, $err, channel, $channel_id, UNFUNDED_CHANNEL)
2014+
},
20072015
}
20082016
};
20092017
}
@@ -2880,6 +2888,13 @@ where
28802888
// Unfunded channel has no update
28812889
(None, chan_phase.context().get_counterparty_node_id())
28822890
},
2891+
// TODO(dual_funding): Combine this match arm with above.
2892+
#[cfg(dual_funding)]
2893+
ChannelPhase::UnfundedOutboundV2(_) | ChannelPhase::UnfundedInboundV2(_) => {
2894+
self.finish_close_channel(chan_phase.context_mut().force_shutdown(false));
2895+
// Unfunded channel has no update
2896+
(None, chan_phase.context().get_counterparty_node_id())
2897+
},
28832898
}
28842899
} else if peer_state.inbound_channel_request_by_id.remove(channel_id).is_some() {
28852900
log_error!(logger, "Force-closing channel {}", &channel_id);
@@ -4887,6 +4902,16 @@ where
48874902
process_unfunded_channel_tick(chan_id, &mut chan.context, &mut chan.unfunded_context,
48884903
pending_msg_events, counterparty_node_id)
48894904
},
4905+
#[cfg(dual_funding)]
4906+
ChannelPhase::UnfundedInboundV2(chan) => {
4907+
process_unfunded_channel_tick(chan_id, &mut chan.context, &mut chan.unfunded_context,
4908+
pending_msg_events, counterparty_node_id)
4909+
},
4910+
#[cfg(dual_funding)]
4911+
ChannelPhase::UnfundedOutboundV2(chan) => {
4912+
process_unfunded_channel_tick(chan_id, &mut chan.context, &mut chan.unfunded_context,
4913+
pending_msg_events, counterparty_node_id)
4914+
},
48904915
}
48914916
});
48924917

@@ -5981,14 +6006,27 @@ where
59816006
num_unfunded_channels += 1;
59826007
}
59836008
},
5984-
ChannelPhase::UnfundedInboundV1(chan) => {
5985-
if chan.context.minimum_depth().unwrap_or(1) != 0 {
6009+
ChannelPhase::UnfundedInboundV1(_) => {
6010+
if phase.context().minimum_depth().unwrap_or(1) != 0 {
6011+
num_unfunded_channels += 1;
6012+
}
6013+
},
6014+
// TODO(dual_funding): Combine this match arm with above.
6015+
#[cfg(dual_funding)]
6016+
ChannelPhase::UnfundedInboundV2(_) => {
6017+
if phase.context().minimum_depth().unwrap_or(1) != 0 {
59866018
num_unfunded_channels += 1;
59876019
}
59886020
},
59896021
ChannelPhase::UnfundedOutboundV1(_) => {
59906022
// Outbound channels don't contribute to the unfunded count in the DoS context.
59916023
continue;
6024+
},
6025+
// TODO(dual_funding): Combine this match arm with above.
6026+
#[cfg(dual_funding)]
6027+
ChannelPhase::UnfundedOutboundV2(_) => {
6028+
// Outbound channels don't contribute to the unfunded count in the DoS context.
6029+
continue;
59926030
}
59936031
}
59946032
}
@@ -6164,7 +6202,7 @@ where
61646202
},
61656203
}
61666204
},
6167-
Some(ChannelPhase::Funded(_)) | Some(ChannelPhase::UnfundedOutboundV1(_)) => {
6205+
Some(_) => {
61686206
return Err(MsgHandleErrInternal::send_err_msg_no_close(format!("Got an unexpected funding_created message from peer with counterparty_node_id {}", counterparty_node_id), msg.temporary_channel_id));
61696207
},
61706208
None => 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.temporary_channel_id))
@@ -6366,6 +6404,15 @@ where
63666404
let mut chan = remove_channel_phase!(self, chan_phase_entry);
63676405
finish_shutdown = Some(chan.context_mut().force_shutdown(false));
63686406
},
6407+
// TODO(dual_funding): Combine this match arm with above.
6408+
#[cfg(dual_funding)]
6409+
ChannelPhase::UnfundedInboundV2(_) | ChannelPhase::UnfundedOutboundV2(_) => {
6410+
let context = phase.context_mut();
6411+
log_error!(self.logger, "Immediately closing unfunded channel {} as peer asked to cooperatively shut it down (which is unnecessary)", &msg.channel_id);
6412+
self.issue_channel_close_events(&context, ClosureReason::CounterpartyCoopClosedUnfundedChannel);
6413+
let mut chan = remove_channel_phase!(self, chan_phase_entry);
6414+
finish_shutdown = Some(chan.context_mut().force_shutdown(false));
6415+
},
63696416
}
63706417
} else {
63716418
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))
@@ -8174,6 +8221,9 @@ where
81748221
match phase {
81758222
// Retain unfunded channels.
81768223
ChannelPhase::UnfundedOutboundV1(_) | ChannelPhase::UnfundedInboundV1(_) => true,
8224+
// TODO(dual_funding): Combine this match arm with above.
8225+
#[cfg(dual_funding)]
8226+
ChannelPhase::UnfundedOutboundV2(_) | ChannelPhase::UnfundedInboundV2(_) => true,
81778227
ChannelPhase::Funded(channel) => {
81788228
let res = f(channel);
81798229
if let Ok((channel_ready_opt, mut timed_out_pending_htlcs, announcement_sigs)) = res {
@@ -8641,6 +8691,14 @@ where
86418691
ChannelPhase::UnfundedInboundV1(chan) => {
86428692
&mut chan.context
86438693
},
8694+
#[cfg(dual_funding)]
8695+
ChannelPhase::UnfundedOutboundV2(chan) => {
8696+
&mut chan.context
8697+
},
8698+
#[cfg(dual_funding)]
8699+
ChannelPhase::UnfundedInboundV2(chan) => {
8700+
&mut chan.context
8701+
},
86448702
};
86458703
// Clean up for removal.
86468704
update_maps_on_chan_removal!(self, &context);

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)