@@ -40,7 +40,7 @@ use crate::events::{Event, EventHandler, EventsProvider, MessageSendEvent, Messa
40
40
// Since this struct is returned in `list_channels` methods, expose it here in case users want to
41
41
// construct one themselves.
42
42
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 } ;
44
44
use crate :: ln:: features:: { ChannelFeatures , ChannelTypeFeatures , InitFeatures , NodeFeatures } ;
45
45
#[ cfg( any( feature = "_test_utils" , test) ) ]
46
46
use crate :: ln:: features:: InvoiceFeatures ;
@@ -628,6 +628,27 @@ pub(super) struct PeerState<Signer: ChannelSigner> {
628
628
/// been assigned a `channel_id`, the entry in this map is removed and one is created in
629
629
/// `channel_by_id`.
630
630
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 > > ,
631
652
/// The latest `InitFeatures` we heard from the peer.
632
653
latest_features : InitFeatures ,
633
654
/// 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> {
682
703
fn total_channel_count ( & self ) -> usize {
683
704
self . channel_by_id . len ( ) +
684
705
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 ( )
686
709
}
687
710
688
711
// Returns a bool indicating if the given `channel_id` matches a channel we have with this peer.
689
712
fn has_channel ( & self , channel_id : & [ u8 ; 32 ] ) -> bool {
690
713
self . channel_by_id . contains_key ( channel_id) ||
691
714
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)
693
718
}
694
719
}
695
720
@@ -1735,8 +1760,8 @@ macro_rules! convert_chan_err {
1735
1760
} ,
1736
1761
ChannelError :: Close ( msg) => {
1737
1762
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 ) ;
1740
1765
( true , MsgHandleErrInternal :: from_finish_shutdown( msg, * $channel_id, $channel. context. get_user_id( ) ,
1741
1766
shutdown_res, $self. get_channel_update_for_broadcast( & $channel) . ok( ) ) )
1742
1767
} ,
@@ -1806,7 +1831,7 @@ macro_rules! remove_channel {
1806
1831
( $self: expr, $entry: expr) => {
1807
1832
{
1808
1833
let channel = $entry. remove_entry( ) . 1 ;
1809
- update_maps_on_chan_removal!( $self, & channel. context) ;
1834
+ update_maps_on_chan_removal!( $self, & channel. context( ) ) ;
1810
1835
channel
1811
1836
}
1812
1837
}
@@ -1926,7 +1951,7 @@ macro_rules! handle_new_monitor_update {
1926
1951
ChannelMonitorUpdateStatus :: PermanentFailure => {
1927
1952
log_error!( $self. logger, "Closing channel {} due to monitor update ChannelMonitorUpdateStatus::PermanentFailure" ,
1928
1953
log_bytes!( $chan. context. channel_id( ) [ ..] ) ) ;
1929
- update_maps_on_chan_removal!( $self, & $chan. context) ;
1954
+ update_maps_on_chan_removal!( $self, & $chan. context( ) ) ;
1930
1955
let res = Err ( MsgHandleErrInternal :: from_finish_shutdown(
1931
1956
"ChannelMonitor storage failure" . to_owned( ) , $chan. context. channel_id( ) ,
1932
1957
$chan. context. get_user_id( ) , $chan. context. force_shutdown( false ) ,
@@ -2280,6 +2305,16 @@ where
2280
2305
peer_state. latest_features . clone ( ) , & self . fee_estimator ) ;
2281
2306
res. push ( details) ;
2282
2307
}
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
+ }
2283
2318
}
2284
2319
}
2285
2320
res
@@ -2537,6 +2572,20 @@ where
2537
2572
self . finish_force_close_channel ( chan. context . force_shutdown ( false ) ) ;
2538
2573
// Prefunded channel has no update
2539
2574
( 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 ( ) )
2540
2589
} else {
2541
2590
return Err ( APIError :: ChannelUnavailable { err : format ! ( "Channel with id {} not found for the passed counterparty node_id {}" , log_bytes!( * channel_id) , peer_node_id) } ) ;
2542
2591
}
@@ -5164,6 +5213,11 @@ where
5164
5213
num_unfunded_channels += 1 ;
5165
5214
}
5166
5215
}
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
+ }
5167
5221
num_unfunded_channels
5168
5222
}
5169
5223
@@ -7074,6 +7128,16 @@ where
7074
7128
self . issue_channel_close_events ( & chan. context , ClosureReason :: DisconnectedPeer ) ;
7075
7129
false
7076
7130
} ) ;
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
+ } ) ;
7077
7141
pending_msg_events. retain ( |msg| {
7078
7142
match msg {
7079
7143
// V1 Channel Establishment
@@ -7157,6 +7221,8 @@ where
7157
7221
channel_by_id : HashMap :: new ( ) ,
7158
7222
outbound_v1_channel_by_id : HashMap :: new ( ) ,
7159
7223
inbound_v1_channel_by_id : HashMap :: new ( ) ,
7224
+ outbound_v2_channel_by_id : HashMap :: new ( ) ,
7225
+ inbound_v2_channel_by_id : HashMap :: new ( ) ,
7160
7226
latest_features : init_msg. features . clone ( ) ,
7161
7227
pending_msg_events : Vec :: new ( ) ,
7162
7228
in_flight_monitor_updates : BTreeMap :: new ( ) ,
@@ -7235,7 +7301,9 @@ where
7235
7301
let peer_state = & mut * peer_state_lock;
7236
7302
peer_state. channel_by_id . keys ( ) . cloned ( )
7237
7303
. 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 ( )
7239
7307
} ;
7240
7308
for channel_id in channel_ids {
7241
7309
// Untrusted messages from peer, we throw away the error if id points to a non-existent channel
@@ -8368,6 +8436,8 @@ where
8368
8436
channel_by_id,
8369
8437
outbound_v1_channel_by_id : HashMap :: new ( ) ,
8370
8438
inbound_v1_channel_by_id : HashMap :: new ( ) ,
8439
+ outbound_v2_channel_by_id : HashMap :: new ( ) ,
8440
+ inbound_v2_channel_by_id : HashMap :: new ( ) ,
8371
8441
latest_features : InitFeatures :: empty ( ) ,
8372
8442
pending_msg_events : Vec :: new ( ) ,
8373
8443
in_flight_monitor_updates : BTreeMap :: new ( ) ,
0 commit comments