Skip to content

Commit a22bd08

Browse files
committed
Add peer state maps for V2 inbound/outbound channels
1 parent a3f0dd8 commit a22bd08

File tree

1 file changed

+78
-8
lines changed

1 file changed

+78
-8
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 78 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ use crate::events::{Event, EventHandler, EventsProvider, MessageSendEvent, Messa
4040
// Since this struct is returned in `list_channels` methods, expose it here in case users want to
4141
// construct one themselves.
4242
use crate::ln::{inbound_payment, PaymentHash, PaymentPreimage, PaymentSecret};
43-
use crate::ln::channel::{Channel, ChannelContext, ChannelError, ChannelUpdateStatus, ShutdownResult, UpdateFulfillCommitFetch, OutboundV1Channel, InboundV1Channel};
43+
use crate::ln::channel::{Channel, ChannelContext, OutboundV1Channel, InboundV1Channel, OutboundV2Channel, InboundV2Channel, ChannelError, ChannelUpdateStatus, ShutdownResult, UpdateFulfillCommitFetch};
4444
use crate::ln::features::{ChannelFeatures, ChannelTypeFeatures, InitFeatures, NodeFeatures};
4545
#[cfg(any(feature = "_test_utils", test))]
4646
use crate::ln::features::InvoiceFeatures;
@@ -628,6 +628,27 @@ pub(super) struct PeerState<Signer: ChannelSigner> {
628628
/// been assigned a `channel_id`, the entry in this map is removed and one is created in
629629
/// `channel_by_id`.
630630
pub(super) inbound_v1_channel_by_id: HashMap<[u8; 32], InboundV1Channel<Signer>>,
631+
/// `(temporary_)channel_id` -> `OutboundV2Channel`.
632+
///
633+
/// Holds all outbound V2 channels where the peer is the counterparty. V2 channels are assigned
634+
/// a `channel_id` before a funding transaction is created interactively as it's derived from
635+
/// both parties' revocation basepoints once these are known. Hence, this map's keys are either
636+
/// temporary channel IDs or channel IDs.
637+
///
638+
/// The entries in this map are only moved to `channel_by_id` once interactive transaction
639+
/// construction completes successfully.
640+
pub(super) outbound_v2_channel_by_id: HashMap<[u8; 32], OutboundV2Channel<Signer>>,
641+
/// `channel_id` -> `InboundV2Channel`.
642+
///
643+
/// Holds all inbound V2 channels where the peer is the counterparty. V2 channels are assigned
644+
/// a `channel_id` before a funding transaction is created interactively as it's derived from
645+
/// both parties' revocation basepoints once these are known. At the stage of receiving an
646+
/// `open_channel2` request, we have enough information to derive the `channel_id`. Hence, this
647+
/// map's keys are always `channel_id`s.
648+
///
649+
/// The entries in this map are only moved to `channel_by_id` once interactive transaction
650+
/// construction completes successfully.
651+
pub(super) inbound_v2_channel_by_id: HashMap<[u8; 32], InboundV2Channel<Signer>>,
631652
/// The latest `InitFeatures` we heard from the peer.
632653
latest_features: InitFeatures,
633654
/// Messages to send to the peer - pushed to in the same lock that they are generated in (except
@@ -682,14 +703,18 @@ impl <Signer: ChannelSigner> PeerState<Signer> {
682703
fn total_channel_count(&self) -> usize {
683704
self.channel_by_id.len() +
684705
self.outbound_v1_channel_by_id.len() +
685-
self.inbound_v1_channel_by_id.len()
706+
self.inbound_v1_channel_by_id.len() +
707+
self.outbound_v2_channel_by_id.len() +
708+
self.inbound_v2_channel_by_id.len()
686709
}
687710

688711
// Returns a bool indicating if the given `channel_id` matches a channel we have with this peer.
689712
fn has_channel(&self, channel_id: &[u8; 32]) -> bool {
690713
self.channel_by_id.contains_key(channel_id) ||
691714
self.outbound_v1_channel_by_id.contains_key(channel_id) ||
692-
self.inbound_v1_channel_by_id.contains_key(channel_id)
715+
self.inbound_v1_channel_by_id.contains_key(channel_id) ||
716+
self.outbound_v2_channel_by_id.contains_key(channel_id) ||
717+
self.inbound_v2_channel_by_id.contains_key(channel_id)
693718
}
694719
}
695720

@@ -1735,8 +1760,8 @@ macro_rules! convert_chan_err {
17351760
},
17361761
ChannelError::Close(msg) => {
17371762
log_error!($self.logger, "Closing channel {} due to close-required error: {}", log_bytes!($channel_id[..]), msg);
1738-
update_maps_on_chan_removal!($self, &$channel.context);
1739-
let shutdown_res = $channel.context.force_shutdown(true);
1763+
update_maps_on_chan_removal!($self, &$channel.context());
1764+
let shutdown_res = $channel.context_mut().force_shutdown(true);
17401765
(true, MsgHandleErrInternal::from_finish_shutdown(msg, *$channel_id, $channel.context.get_user_id(),
17411766
shutdown_res, $self.get_channel_update_for_broadcast(&$channel).ok()))
17421767
},
@@ -1806,7 +1831,7 @@ macro_rules! remove_channel {
18061831
($self: expr, $entry: expr) => {
18071832
{
18081833
let channel = $entry.remove_entry().1;
1809-
update_maps_on_chan_removal!($self, &channel.context);
1834+
update_maps_on_chan_removal!($self, &channel.context());
18101835
channel
18111836
}
18121837
}
@@ -1926,7 +1951,7 @@ macro_rules! handle_new_monitor_update {
19261951
ChannelMonitorUpdateStatus::PermanentFailure => {
19271952
log_error!($self.logger, "Closing channel {} due to monitor update ChannelMonitorUpdateStatus::PermanentFailure",
19281953
log_bytes!($chan.context.channel_id()[..]));
1929-
update_maps_on_chan_removal!($self, &$chan.context);
1954+
update_maps_on_chan_removal!($self, &$chan.context());
19301955
let res = Err(MsgHandleErrInternal::from_finish_shutdown(
19311956
"ChannelMonitor storage failure".to_owned(), $chan.context.channel_id(),
19321957
$chan.context.get_user_id(), $chan.context.force_shutdown(false),
@@ -2280,6 +2305,16 @@ where
22802305
peer_state.latest_features.clone(), &self.fee_estimator);
22812306
res.push(details);
22822307
}
2308+
for (_channel_id, channel) in peer_state.inbound_v2_channel_by_id.iter() {
2309+
let details = ChannelDetails::from_channel_context(&channel.context.common, best_block_height,
2310+
peer_state.latest_features.clone(), &self.fee_estimator);
2311+
res.push(details);
2312+
}
2313+
for (_channel_id, channel) in peer_state.outbound_v2_channel_by_id.iter() {
2314+
let details = ChannelDetails::from_channel_context(&channel.context.common, best_block_height,
2315+
peer_state.latest_features.clone(), &self.fee_estimator);
2316+
res.push(details);
2317+
}
22832318
}
22842319
}
22852320
res
@@ -2537,6 +2572,20 @@ where
25372572
self.finish_force_close_channel(chan.context.force_shutdown(false));
25382573
// Prefunded channel has no update
25392574
(None, chan.context.get_counterparty_node_id())
2575+
} else if let hash_map::Entry::Occupied(chan) = peer_state.outbound_v2_channel_by_id.entry(channel_id.clone()) {
2576+
log_error!(self.logger, "Force-closing channel {}", log_bytes!(channel_id[..]));
2577+
self.issue_channel_close_events(&chan.get().context.common, closure_reason);
2578+
let mut chan = remove_channel!(self, chan);
2579+
self.finish_force_close_channel(chan.context.common.force_shutdown(false));
2580+
// Prefunded channel has no update
2581+
(None, chan.context.common.get_counterparty_node_id())
2582+
} else if let hash_map::Entry::Occupied(chan) = peer_state.inbound_v2_channel_by_id.entry(channel_id.clone()) {
2583+
log_error!(self.logger, "Force-closing channel {}", log_bytes!(channel_id[..]));
2584+
self.issue_channel_close_events(&chan.get().context.common, closure_reason);
2585+
let mut chan = remove_channel!(self, chan);
2586+
self.finish_force_close_channel(chan.context.common.force_shutdown(false));
2587+
// Prefunded channel has no update
2588+
(None, chan.context.common.get_counterparty_node_id())
25402589
} else {
25412590
return Err(APIError::ChannelUnavailable{ err: format!("Channel with id {} not found for the passed counterparty node_id {}", log_bytes!(*channel_id), peer_node_id) });
25422591
}
@@ -5164,6 +5213,11 @@ where
51645213
num_unfunded_channels += 1;
51655214
}
51665215
}
5216+
for (_, chan) in peer.inbound_v2_channel_by_id.iter() {
5217+
if chan.context.common.minimum_depth().unwrap_or(1) != 0 {
5218+
num_unfunded_channels += 1;
5219+
}
5220+
}
51675221
num_unfunded_channels
51685222
}
51695223

@@ -7074,6 +7128,16 @@ where
70747128
self.issue_channel_close_events(&chan.context, ClosureReason::DisconnectedPeer);
70757129
false
70767130
});
7131+
peer_state.inbound_v2_channel_by_id.retain(|_, chan| {
7132+
update_maps_on_chan_removal!(self, &chan.context.common);
7133+
self.issue_channel_close_events(&chan.context.common, ClosureReason::DisconnectedPeer);
7134+
false
7135+
});
7136+
peer_state.outbound_v2_channel_by_id.retain(|_, chan| {
7137+
update_maps_on_chan_removal!(self, &chan.context.common);
7138+
self.issue_channel_close_events(&chan.context.common, ClosureReason::DisconnectedPeer);
7139+
false
7140+
});
70777141
pending_msg_events.retain(|msg| {
70787142
match msg {
70797143
// V1 Channel Establishment
@@ -7157,6 +7221,8 @@ where
71577221
channel_by_id: HashMap::new(),
71587222
outbound_v1_channel_by_id: HashMap::new(),
71597223
inbound_v1_channel_by_id: HashMap::new(),
7224+
outbound_v2_channel_by_id: HashMap::new(),
7225+
inbound_v2_channel_by_id: HashMap::new(),
71607226
latest_features: init_msg.features.clone(),
71617227
pending_msg_events: Vec::new(),
71627228
in_flight_monitor_updates: BTreeMap::new(),
@@ -7235,7 +7301,9 @@ where
72357301
let peer_state = &mut *peer_state_lock;
72367302
peer_state.channel_by_id.keys().cloned()
72377303
.chain(peer_state.outbound_v1_channel_by_id.keys().cloned())
7238-
.chain(peer_state.inbound_v1_channel_by_id.keys().cloned()).collect()
7304+
.chain(peer_state.inbound_v1_channel_by_id.keys().cloned())
7305+
.chain(peer_state.outbound_v2_channel_by_id.keys().cloned())
7306+
.chain(peer_state.inbound_v2_channel_by_id.keys().cloned()).collect()
72397307
};
72407308
for channel_id in channel_ids {
72417309
// Untrusted messages from peer, we throw away the error if id points to a non-existent channel
@@ -8368,6 +8436,8 @@ where
83688436
channel_by_id,
83698437
outbound_v1_channel_by_id: HashMap::new(),
83708438
inbound_v1_channel_by_id: HashMap::new(),
8439+
outbound_v2_channel_by_id: HashMap::new(),
8440+
inbound_v2_channel_by_id: HashMap::new(),
83718441
latest_features: InitFeatures::empty(),
83728442
pending_msg_events: Vec::new(),
83738443
in_flight_monitor_updates: BTreeMap::new(),

0 commit comments

Comments
 (0)