Skip to content

Commit b0224a0

Browse files
committed
Add V2 ChannelPhase variants
1 parent b008767 commit b0224a0

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
@@ -681,6 +681,10 @@ impl_writeable_tlv_based!(PendingChannelMonitorUpdate, {
681681
pub(super) enum ChannelPhase<SP: Deref> where SP::Target: SignerProvider {
682682
UnfundedOutboundV1(OutboundV1Channel<SP>),
683683
UnfundedInboundV1(InboundV1Channel<SP>),
684+
#[cfg(dual_funding)]
685+
UnfundedOutboundV2(OutboundV2Channel<SP>),
686+
#[cfg(dual_funding)]
687+
UnfundedInboundV2(InboundV2Channel<SP>),
684688
Funded(Channel<SP>),
685689
}
686690

@@ -693,6 +697,10 @@ impl<'a, SP: Deref> ChannelPhase<SP> where
693697
ChannelPhase::Funded(chan) => &chan.context,
694698
ChannelPhase::UnfundedOutboundV1(chan) => &chan.context,
695699
ChannelPhase::UnfundedInboundV1(chan) => &chan.context,
700+
#[cfg(dual_funding)]
701+
ChannelPhase::UnfundedOutboundV2(chan) => &chan.context,
702+
#[cfg(dual_funding)]
703+
ChannelPhase::UnfundedInboundV2(chan) => &chan.context,
696704
}
697705
}
698706

@@ -701,6 +709,10 @@ impl<'a, SP: Deref> ChannelPhase<SP> where
701709
ChannelPhase::Funded(ref mut chan) => &mut chan.context,
702710
ChannelPhase::UnfundedOutboundV1(ref mut chan) => &mut chan.context,
703711
ChannelPhase::UnfundedInboundV1(ref mut chan) => &mut chan.context,
712+
#[cfg(dual_funding)]
713+
ChannelPhase::UnfundedOutboundV2(ref mut chan) => &mut chan.context,
714+
#[cfg(dual_funding)]
715+
ChannelPhase::UnfundedInboundV2(ref mut chan) => &mut chan.context,
704716
}
705717
}
706718
}

lightning/src/ln/channelmanager.rs

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2055,6 +2055,14 @@ macro_rules! convert_chan_phase_err {
20552055
ChannelPhase::UnfundedInboundV1(channel) => {
20562056
convert_chan_phase_err!($self, $err, channel, $channel_id, UNFUNDED_CHANNEL)
20572057
},
2058+
#[cfg(dual_funding)]
2059+
ChannelPhase::UnfundedOutboundV2(channel) => {
2060+
convert_chan_phase_err!($self, $err, channel, $channel_id, UNFUNDED_CHANNEL)
2061+
},
2062+
#[cfg(dual_funding)]
2063+
ChannelPhase::UnfundedInboundV2(channel) => {
2064+
convert_chan_phase_err!($self, $err, channel, $channel_id, UNFUNDED_CHANNEL)
2065+
},
20582066
}
20592067
};
20602068
}
@@ -2934,6 +2942,13 @@ where
29342942
// Unfunded channel has no update
29352943
(None, chan_phase.context().get_counterparty_node_id())
29362944
},
2945+
// TODO(dual_funding): Combine this match arm with above.
2946+
#[cfg(dual_funding)]
2947+
ChannelPhase::UnfundedOutboundV2(_) | ChannelPhase::UnfundedInboundV2(_) => {
2948+
self.finish_close_channel(chan_phase.context_mut().force_shutdown(false));
2949+
// Unfunded channel has no update
2950+
(None, chan_phase.context().get_counterparty_node_id())
2951+
},
29372952
}
29382953
} else if peer_state.inbound_channel_request_by_id.remove(channel_id).is_some() {
29392954
log_error!(logger, "Force-closing channel {}", &channel_id);
@@ -4957,6 +4972,16 @@ where
49574972
process_unfunded_channel_tick(chan_id, &mut chan.context, &mut chan.unfunded_context,
49584973
pending_msg_events, counterparty_node_id)
49594974
},
4975+
#[cfg(dual_funding)]
4976+
ChannelPhase::UnfundedInboundV2(chan) => {
4977+
process_unfunded_channel_tick(chan_id, &mut chan.context, &mut chan.unfunded_context,
4978+
pending_msg_events, counterparty_node_id)
4979+
},
4980+
#[cfg(dual_funding)]
4981+
ChannelPhase::UnfundedOutboundV2(chan) => {
4982+
process_unfunded_channel_tick(chan_id, &mut chan.context, &mut chan.unfunded_context,
4983+
pending_msg_events, counterparty_node_id)
4984+
},
49604985
}
49614986
});
49624987

@@ -6053,14 +6078,27 @@ where
60536078
num_unfunded_channels += 1;
60546079
}
60556080
},
6056-
ChannelPhase::UnfundedInboundV1(chan) => {
6057-
if chan.context.minimum_depth().unwrap_or(1) != 0 {
6081+
ChannelPhase::UnfundedInboundV1(_) => {
6082+
if phase.context().minimum_depth().unwrap_or(1) != 0 {
6083+
num_unfunded_channels += 1;
6084+
}
6085+
},
6086+
// TODO(dual_funding): Combine this match arm with above.
6087+
#[cfg(dual_funding)]
6088+
ChannelPhase::UnfundedInboundV2(_) => {
6089+
if phase.context().minimum_depth().unwrap_or(1) != 0 {
60586090
num_unfunded_channels += 1;
60596091
}
60606092
},
60616093
ChannelPhase::UnfundedOutboundV1(_) => {
60626094
// Outbound channels don't contribute to the unfunded count in the DoS context.
60636095
continue;
6096+
},
6097+
// TODO(dual_funding): Combine this match arm with above.
6098+
#[cfg(dual_funding)]
6099+
ChannelPhase::UnfundedOutboundV2(_) => {
6100+
// Outbound channels don't contribute to the unfunded count in the DoS context.
6101+
continue;
60646102
}
60656103
}
60666104
}
@@ -6236,7 +6274,7 @@ where
62366274
},
62376275
}
62386276
},
6239-
Some(ChannelPhase::Funded(_)) | Some(ChannelPhase::UnfundedOutboundV1(_)) => {
6277+
Some(_) => {
62406278
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));
62416279
},
62426280
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))
@@ -6438,6 +6476,15 @@ where
64386476
let mut chan = remove_channel_phase!(self, chan_phase_entry);
64396477
finish_shutdown = Some(chan.context_mut().force_shutdown(false));
64406478
},
6479+
// TODO(dual_funding): Combine this match arm with above.
6480+
#[cfg(dual_funding)]
6481+
ChannelPhase::UnfundedInboundV2(_) | ChannelPhase::UnfundedOutboundV2(_) => {
6482+
let context = phase.context_mut();
6483+
log_error!(self.logger, "Immediately closing unfunded channel {} as peer asked to cooperatively shut it down (which is unnecessary)", &msg.channel_id);
6484+
self.issue_channel_close_events(&context, ClosureReason::CounterpartyCoopClosedUnfundedChannel);
6485+
let mut chan = remove_channel_phase!(self, chan_phase_entry);
6486+
finish_shutdown = Some(chan.context_mut().force_shutdown(false));
6487+
},
64416488
}
64426489
} else {
64436490
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))
@@ -8248,6 +8295,9 @@ where
82488295
match phase {
82498296
// Retain unfunded channels.
82508297
ChannelPhase::UnfundedOutboundV1(_) | ChannelPhase::UnfundedInboundV1(_) => true,
8298+
// TODO(dual_funding): Combine this match arm with above.
8299+
#[cfg(dual_funding)]
8300+
ChannelPhase::UnfundedOutboundV2(_) | ChannelPhase::UnfundedInboundV2(_) => true,
82518301
ChannelPhase::Funded(channel) => {
82528302
let res = f(channel);
82538303
if let Ok((channel_ready_opt, mut timed_out_pending_htlcs, announcement_sigs)) = res {
@@ -8715,6 +8765,14 @@ where
87158765
ChannelPhase::UnfundedInboundV1(chan) => {
87168766
&mut chan.context
87178767
},
8768+
#[cfg(dual_funding)]
8769+
ChannelPhase::UnfundedOutboundV2(chan) => {
8770+
&mut chan.context
8771+
},
8772+
#[cfg(dual_funding)]
8773+
ChannelPhase::UnfundedInboundV2(chan) => {
8774+
&mut chan.context
8775+
},
87188776
};
87198777
// Clean up for removal.
87208778
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)