Skip to content

Commit

Permalink
Add V2 ChannelPhase variants
Browse files Browse the repository at this point in the history
  • Loading branch information
dunxen committed Feb 13, 2024
1 parent 3e9c31f commit 080b027
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 1 deletion.
12 changes: 12 additions & 0 deletions lightning/src/ln/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1161,6 +1161,10 @@ impl_writeable_tlv_based!(PendingChannelMonitorUpdate, {
pub(super) enum ChannelPhase<SP: Deref> where SP::Target: SignerProvider {
UnfundedOutboundV1(OutboundV1Channel<SP>),
UnfundedInboundV1(InboundV1Channel<SP>),
#[cfg(dual_funding)]
UnfundedOutboundV2(OutboundV2Channel<SP>),
#[cfg(dual_funding)]
UnfundedInboundV2(InboundV2Channel<SP>),
Funded(Channel<SP>),
}

Expand All @@ -1173,6 +1177,10 @@ impl<'a, SP: Deref> ChannelPhase<SP> where
ChannelPhase::Funded(chan) => &chan.context,
ChannelPhase::UnfundedOutboundV1(chan) => &chan.context,
ChannelPhase::UnfundedInboundV1(chan) => &chan.context,
#[cfg(dual_funding)]
ChannelPhase::UnfundedOutboundV2(chan) => &chan.context,
#[cfg(dual_funding)]
ChannelPhase::UnfundedInboundV2(chan) => &chan.context,
}
}

Expand All @@ -1181,6 +1189,10 @@ impl<'a, SP: Deref> ChannelPhase<SP> where
ChannelPhase::Funded(ref mut chan) => &mut chan.context,
ChannelPhase::UnfundedOutboundV1(ref mut chan) => &mut chan.context,
ChannelPhase::UnfundedInboundV1(ref mut chan) => &mut chan.context,
#[cfg(dual_funding)]
ChannelPhase::UnfundedOutboundV2(ref mut chan) => &mut chan.context,
#[cfg(dual_funding)]
ChannelPhase::UnfundedInboundV2(ref mut chan) => &mut chan.context,
}
}
}
Expand Down
78 changes: 78 additions & 0 deletions lightning/src/ln/channelmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2092,6 +2092,14 @@ macro_rules! convert_chan_phase_err {
ChannelPhase::UnfundedInboundV1(channel) => {
convert_chan_phase_err!($self, $err, channel, $channel_id, UNFUNDED_CHANNEL)
},
#[cfg(dual_funding)]
ChannelPhase::UnfundedOutboundV2(channel) => {
convert_chan_phase_err!($self, $err, channel, $channel_id, UNFUNDED_CHANNEL)
},
#[cfg(dual_funding)]
ChannelPhase::UnfundedInboundV2(channel) => {
convert_chan_phase_err!($self, $err, channel, $channel_id, UNFUNDED_CHANNEL)
},
}
};
}
Expand Down Expand Up @@ -2958,6 +2966,13 @@ where
// Unfunded channel has no update
(None, chan_phase.context().get_counterparty_node_id())
},
// TODO(dual_funding): Combine this match arm with above once #[cfg(dual_funding)] is removed.
#[cfg(dual_funding)]
ChannelPhase::UnfundedOutboundV2(_) | ChannelPhase::UnfundedInboundV2(_) => {
self.finish_close_channel(chan_phase.context_mut().force_shutdown(false, closure_reason));
// Unfunded channel has no update
(None, chan_phase.context().get_counterparty_node_id())
},
}
} else if peer_state.inbound_channel_request_by_id.remove(channel_id).is_some() {
log_error!(logger, "Force-closing channel {}", &channel_id);
Expand Down Expand Up @@ -5035,6 +5050,16 @@ where
process_unfunded_channel_tick(chan_id, &mut chan.context, &mut chan.unfunded_context,
pending_msg_events, counterparty_node_id)
},
#[cfg(dual_funding)]
ChannelPhase::UnfundedInboundV2(chan) => {
process_unfunded_channel_tick(chan_id, &mut chan.context, &mut chan.unfunded_context,
pending_msg_events, counterparty_node_id)
},
#[cfg(dual_funding)]
ChannelPhase::UnfundedOutboundV2(chan) => {
process_unfunded_channel_tick(chan_id, &mut chan.context, &mut chan.unfunded_context,
pending_msg_events, counterparty_node_id)
},
}
});

Expand Down Expand Up @@ -6182,9 +6207,25 @@ where
num_unfunded_channels += 1;
}
},
// TODO(dual_funding): Combine this match arm with above once #[cfg(dual_funding)] is removed.
#[cfg(dual_funding)]
ChannelPhase::UnfundedInboundV2(chan) => {
// Only inbound V2 channels that are not 0conf and that we do not contribute to will be
// included in the unfunded count.
if chan.context.minimum_depth().unwrap_or(1) != 0 &&
chan.dual_funding_context.our_funding_satoshis == 0 {
num_unfunded_channels += 1;
}
},
ChannelPhase::UnfundedOutboundV1(_) => {
// Outbound channels don't contribute to the unfunded count in the DoS context.
continue;
},
// TODO(dual_funding): Combine this match arm with above once #[cfg(dual_funding)] is removed.
#[cfg(dual_funding)]
ChannelPhase::UnfundedOutboundV2(_) => {
// Outbound channels don't contribute to the unfunded count in the DoS context.
continue;
}
}
}
Expand Down Expand Up @@ -6607,6 +6648,14 @@ where
let mut chan = remove_channel_phase!(self, chan_phase_entry);
finish_shutdown = Some(chan.context_mut().force_shutdown(false, ClosureReason::CounterpartyCoopClosedUnfundedChannel));
},
// TODO(dual_funding): Combine this match arm with above.
#[cfg(dual_funding)]
ChannelPhase::UnfundedInboundV2(_) | ChannelPhase::UnfundedOutboundV2(_) => {
let context = phase.context_mut();
log_error!(self.logger, "Immediately closing unfunded channel {} as peer asked to cooperatively shut it down (which is unnecessary)", &msg.channel_id);
let mut chan = remove_channel_phase!(self, chan_phase_entry);
finish_shutdown = Some(chan.context_mut().force_shutdown(false, ClosureReason::CounterpartyCoopClosedUnfundedChannel));
},
}
} else {
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))
Expand Down Expand Up @@ -8474,6 +8523,9 @@ where
match phase {
// Retain unfunded channels.
ChannelPhase::UnfundedOutboundV1(_) | ChannelPhase::UnfundedInboundV1(_) => true,
// TODO(dual_funding): Combine this match arm with above.
#[cfg(dual_funding)]
ChannelPhase::UnfundedOutboundV2(_) | ChannelPhase::UnfundedInboundV2(_) => true,
ChannelPhase::Funded(channel) => {
let res = f(channel);
if let Ok((channel_ready_opt, mut timed_out_pending_htlcs, announcement_sigs)) = res {
Expand Down Expand Up @@ -8943,6 +8995,14 @@ where
ChannelPhase::UnfundedInboundV1(chan) => {
&mut chan.context
},
#[cfg(dual_funding)]
ChannelPhase::UnfundedOutboundV2(chan) => {
&mut chan.context
},
#[cfg(dual_funding)]
ChannelPhase::UnfundedInboundV2(chan) => {
&mut chan.context
},
};
// Clean up for removal.
update_maps_on_chan_removal!(self, &context);
Expand Down Expand Up @@ -9095,12 +9155,30 @@ where
});
}

// TODO(dual_funding): Combine this match arm with above once #[cfg(dual_funding)] is removed.
#[cfg(dual_funding)]
ChannelPhase::UnfundedOutboundV2(chan) => {
pending_msg_events.push(events::MessageSendEvent::SendOpenChannelV2 {
node_id: chan.context.get_counterparty_node_id(),
msg: chan.get_open_channel_v2(self.chain_hash),
});
},

ChannelPhase::UnfundedInboundV1(_) => {
// Since unfunded inbound channel maps are cleared upon disconnecting a peer,
// they are not persisted and won't be recovered after a crash.
// Therefore, they shouldn't exist at this point.
debug_assert!(false);
}

// TODO(dual_funding): Combine this match arm with above once #[cfg(dual_funding)] is removed.
#[cfg(dual_funding)]
ChannelPhase::UnfundedInboundV2(channel) => {
// Since unfunded inbound channel maps are cleared upon disconnecting a peer,
// they are not persisted and won't be recovered after a crash.
// Therefore, they shouldn't exist at this point.
debug_assert!(false);
},
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion lightning/src/ln/functional_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ fn do_test_counterparty_no_reserve(send_from_initiator: bool) {
chan_context.holder_selected_channel_reserve_satoshis = 0;
chan_context.holder_max_htlc_value_in_flight_msat = 100_000_000;
},
ChannelPhase::Funded(_) => assert!(false),
_ => assert!(false),
}
}

Expand Down

0 comments on commit 080b027

Please sign in to comment.