@@ -108,8 +108,8 @@ struct ForwardPaymentAction(ChannelId, FeePayment);
108
108
struct ForwardHTLCsAction ( ChannelId , Vec < InterceptedHTLC > ) ;
109
109
110
110
/// The different states a requested JIT channel can be in.
111
- #[ derive( Debug ) ]
112
- enum OutboundJITChannelState {
111
+ #[ derive( Clone , Debug , PartialEq , Eq ) ]
112
+ pub ( crate ) enum OutboundJITChannelState {
113
113
/// The JIT channel SCID was created after a buy request, and we are awaiting an initial payment
114
114
/// of sufficient size to open the channel.
115
115
PendingInitialPayment { payment_queue : PaymentQueue } ,
@@ -134,6 +134,30 @@ enum OutboundJITChannelState {
134
134
PaymentForwarded { channel_id : ChannelId } ,
135
135
}
136
136
137
+ impl OutboundJITChannelState {
138
+ fn ord_index ( & self ) -> u8 {
139
+ match self {
140
+ OutboundJITChannelState :: PendingInitialPayment { .. } => 0 ,
141
+ OutboundJITChannelState :: PendingChannelOpen { .. } => 1 ,
142
+ OutboundJITChannelState :: PendingPaymentForward { .. } => 2 ,
143
+ OutboundJITChannelState :: PendingPayment { .. } => 3 ,
144
+ OutboundJITChannelState :: PaymentForwarded { .. } => 4 ,
145
+ }
146
+ }
147
+ }
148
+
149
+ impl PartialOrd for OutboundJITChannelState {
150
+ fn partial_cmp ( & self , other : & Self ) -> Option < CmpOrdering > {
151
+ Some ( self . cmp ( other) )
152
+ }
153
+ }
154
+
155
+ impl Ord for OutboundJITChannelState {
156
+ fn cmp ( & self , other : & Self ) -> core:: cmp:: Ordering {
157
+ self . ord_index ( ) . cmp ( & other. ord_index ( ) )
158
+ }
159
+ }
160
+
137
161
impl OutboundJITChannelState {
138
162
fn new ( ) -> Self {
139
163
OutboundJITChannelState :: PendingInitialPayment { payment_queue : PaymentQueue :: new ( ) }
@@ -427,10 +451,6 @@ impl OutboundJITChannel {
427
451
matches ! ( self . state, OutboundJITChannelState :: PendingInitialPayment { .. } )
428
452
}
429
453
430
- fn is_pending_channel_open ( & self ) -> bool {
431
- matches ! ( self . state, OutboundJITChannelState :: PendingChannelOpen { .. } )
432
- }
433
-
434
454
fn is_prunable ( & self ) -> bool {
435
455
// We deem an OutboundJITChannel prunable if our offer expired and we haven't intercepted
436
456
// any HTLCs initiating the flow yet.
@@ -576,19 +596,15 @@ where
576
596
& self . config
577
597
}
578
598
579
- pub ( crate ) fn has_pending_channel_open_request (
599
+ pub ( crate ) fn highest_state_for_peer (
580
600
& self , counterparty_node_id : & PublicKey ,
581
- ) -> bool {
582
- let outer_state_lock = self . per_peer_state . read ( ) . unwrap ( ) ;
583
- if let Some ( inner_state_lock) = outer_state_lock. get ( counterparty_node_id) {
584
- let peer_state = inner_state_lock. lock ( ) . unwrap ( ) ;
585
- peer_state
586
- . outbound_channels_by_intercept_scid
587
- . values ( )
588
- . any ( |c| c. is_pending_channel_open ( ) )
589
- } else {
590
- false
591
- }
601
+ ) -> Option < OutboundJITChannelState > {
602
+ let outer = self . per_peer_state . read ( ) . unwrap ( ) ;
603
+ let Some ( inner) = outer. get ( counterparty_node_id) else {
604
+ return None ;
605
+ } ;
606
+ let peer_state = inner. lock ( ) . unwrap ( ) ;
607
+ peer_state. outbound_channels_by_intercept_scid . values ( ) . map ( |c| c. state . clone ( ) ) . max ( )
592
608
}
593
609
594
610
/// Used by LSP to inform a client requesting a JIT Channel the token they used is invalid.
@@ -1924,4 +1940,55 @@ mod tests {
1924
1940
) ;
1925
1941
}
1926
1942
}
1943
+
1944
+ #[ test]
1945
+ fn highest_state_for_peer_orders ( ) {
1946
+ let opening_fee_params = LSPS2OpeningFeeParams {
1947
+ min_fee_msat : 0 ,
1948
+ proportional : 0 ,
1949
+ valid_until : LSPSDateTime :: from_str ( "1970-01-01T00:00:00Z" ) . unwrap ( ) ,
1950
+ min_lifetime : 0 ,
1951
+ max_client_to_self_delay : 0 ,
1952
+ min_payment_size_msat : 0 ,
1953
+ max_payment_size_msat : 0 ,
1954
+ promise : String :: new ( ) ,
1955
+ } ;
1956
+
1957
+ let mut map = new_hash_map ( ) ;
1958
+ map. insert (
1959
+ 0 ,
1960
+ OutboundJITChannel {
1961
+ state : OutboundJITChannelState :: PendingInitialPayment {
1962
+ payment_queue : PaymentQueue :: new ( ) ,
1963
+ } ,
1964
+ user_channel_id : 0 ,
1965
+ opening_fee_params : opening_fee_params. clone ( ) ,
1966
+ payment_size_msat : None ,
1967
+ } ,
1968
+ ) ;
1969
+ map. insert (
1970
+ 1 ,
1971
+ OutboundJITChannel {
1972
+ state : OutboundJITChannelState :: PendingChannelOpen {
1973
+ payment_queue : PaymentQueue :: new ( ) ,
1974
+ opening_fee_msat : 0 ,
1975
+ } ,
1976
+ user_channel_id : 1 ,
1977
+ opening_fee_params : opening_fee_params. clone ( ) ,
1978
+ payment_size_msat : None ,
1979
+ } ,
1980
+ ) ;
1981
+ map. insert (
1982
+ 2 ,
1983
+ OutboundJITChannel {
1984
+ state : OutboundJITChannelState :: PaymentForwarded { channel_id : ChannelId ( [ 0 ; 32 ] ) } ,
1985
+ user_channel_id : 2 ,
1986
+ opening_fee_params,
1987
+ payment_size_msat : None ,
1988
+ } ,
1989
+ ) ;
1990
+
1991
+ let max_state = map. values ( ) . map ( |c| c. state . clone ( ) ) . max ( ) . unwrap ( ) ;
1992
+ assert ! ( matches!( max_state, OutboundJITChannelState :: PaymentForwarded { .. } ) ) ;
1993
+ }
1927
1994
}
0 commit comments