1+ // This file is Copyright its original authors, visible in version control
2+ // history.
3+ //
4+ // This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5+ // or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6+ // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7+ // You may not use this file except in accordance with one or both of these
8+ // licenses.
9+
110use alloc:: vec:: Vec ;
211
312use lightning:: ln:: channelmanager:: InterceptId ;
@@ -8,7 +17,7 @@ use lightning_types::payment::PaymentHash;
817/// remaining payments forwarded.
918#[ derive( Clone , Default , PartialEq , Eq , Debug ) ]
1019pub ( crate ) struct PaymentQueue {
11- payments : Vec < ( PaymentHash , Vec < InterceptedHTLC > ) > ,
20+ payments : Vec < PaymentQueueEntry > ,
1221}
1322
1423impl PaymentQueue {
@@ -17,37 +26,48 @@ impl PaymentQueue {
1726 }
1827
1928 pub ( crate ) fn add_htlc ( & mut self , new_htlc : InterceptedHTLC ) -> ( u64 , usize ) {
20- let payment = self . payments . iter_mut ( ) . find ( |( p, _) | p == & new_htlc. payment_hash ) ;
21- if let Some ( ( payment_hash, htlcs) ) = payment {
29+ let payment =
30+ self . payments . iter_mut ( ) . find ( |entry| entry. payment_hash == new_htlc. payment_hash ) ;
31+ if let Some ( entry) = payment {
2232 // HTLCs within a payment should have the same payment hash.
23- debug_assert ! ( htlcs. iter( ) . all( |htlc| htlc. payment_hash == * payment_hash) ) ;
33+ debug_assert ! ( entry . htlcs. iter( ) . all( |htlc| htlc. payment_hash == entry . payment_hash) ) ;
2434 // The given HTLC should not already be present.
25- debug_assert ! ( htlcs. iter( ) . all( |htlc| htlc. intercept_id != new_htlc. intercept_id) ) ;
26- htlcs. push ( new_htlc) ;
35+ debug_assert ! ( entry
36+ . htlcs
37+ . iter( )
38+ . all( |htlc| htlc. intercept_id != new_htlc. intercept_id) ) ;
39+ entry. htlcs . push ( new_htlc) ;
2740 let total_expected_outbound_amount_msat =
28- htlcs. iter ( ) . map ( |htlc| htlc. expected_outbound_amount_msat ) . sum ( ) ;
29- ( total_expected_outbound_amount_msat, htlcs. len ( ) )
41+ entry . htlcs . iter ( ) . map ( |htlc| htlc. expected_outbound_amount_msat ) . sum ( ) ;
42+ ( total_expected_outbound_amount_msat, entry . htlcs . len ( ) )
3043 } else {
3144 let expected_outbound_amount_msat = new_htlc. expected_outbound_amount_msat ;
32- self . payments . push ( ( new_htlc. payment_hash , vec ! [ new_htlc] ) ) ;
45+ let entry =
46+ PaymentQueueEntry { payment_hash : new_htlc. payment_hash , htlcs : vec ! [ new_htlc] } ;
47+ self . payments . push ( entry) ;
3348 ( expected_outbound_amount_msat, 1 )
3449 }
3550 }
3651
37- pub ( crate ) fn pop_greater_than_msat (
38- & mut self , amount_msat : u64 ,
39- ) -> Option < ( PaymentHash , Vec < InterceptedHTLC > ) > {
40- let position = self . payments . iter ( ) . position ( |( _payment_hash, htlcs) | {
41- htlcs. iter ( ) . map ( |htlc| htlc. expected_outbound_amount_msat ) . sum :: < u64 > ( ) >= amount_msat
52+ pub ( crate ) fn pop_greater_than_msat ( & mut self , amount_msat : u64 ) -> Option < PaymentQueueEntry > {
53+ let position = self . payments . iter ( ) . position ( |entry| {
54+ entry. htlcs . iter ( ) . map ( |htlc| htlc. expected_outbound_amount_msat ) . sum :: < u64 > ( )
55+ >= amount_msat
4256 } ) ;
4357 position. map ( |position| self . payments . remove ( position) )
4458 }
4559
4660 pub ( crate ) fn clear ( & mut self ) -> Vec < InterceptedHTLC > {
47- self . payments . drain ( ..) . map ( |( _k , v ) | v ) . flatten ( ) . collect ( )
61+ self . payments . drain ( ..) . map ( |entry| entry . htlcs ) . flatten ( ) . collect ( )
4862 }
4963}
5064
65+ #[ derive( Clone , PartialEq , Eq , Debug ) ]
66+ pub ( crate ) struct PaymentQueueEntry {
67+ pub ( crate ) payment_hash : PaymentHash ,
68+ pub ( crate ) htlcs : Vec < InterceptedHTLC > ,
69+ }
70+
5171#[ derive( Copy , Clone , PartialEq , Eq , Debug ) ]
5272pub ( crate ) struct InterceptedHTLC {
5373 pub ( crate ) intercept_id : InterceptId ,
@@ -90,24 +110,23 @@ mod tests {
90110 } ) ,
91111 ( 500_000_000 , 2 ) ,
92112 ) ;
93- assert_eq ! (
94- payment_queue. pop_greater_than_msat( 500_000_000 ) ,
95- Some ( (
96- PaymentHash ( [ 100 ; 32 ] ) ,
97- vec![
98- InterceptedHTLC {
99- intercept_id: InterceptId ( [ 0 ; 32 ] ) ,
100- expected_outbound_amount_msat: 200_000_000 ,
101- payment_hash: PaymentHash ( [ 100 ; 32 ] ) ,
102- } ,
103- InterceptedHTLC {
104- intercept_id: InterceptId ( [ 2 ; 32 ] ) ,
105- expected_outbound_amount_msat: 300_000_000 ,
106- payment_hash: PaymentHash ( [ 100 ; 32 ] ) ,
107- } ,
108- ]
109- ) )
110- ) ;
113+
114+ let expected_entry = PaymentQueueEntry {
115+ payment_hash : PaymentHash ( [ 100 ; 32 ] ) ,
116+ htlcs : vec ! [
117+ InterceptedHTLC {
118+ intercept_id: InterceptId ( [ 0 ; 32 ] ) ,
119+ expected_outbound_amount_msat: 200_000_000 ,
120+ payment_hash: PaymentHash ( [ 100 ; 32 ] ) ,
121+ } ,
122+ InterceptedHTLC {
123+ intercept_id: InterceptId ( [ 2 ; 32 ] ) ,
124+ expected_outbound_amount_msat: 300_000_000 ,
125+ payment_hash: PaymentHash ( [ 100 ; 32 ] ) ,
126+ } ,
127+ ] ,
128+ } ;
129+ assert_eq ! ( payment_queue. pop_greater_than_msat( 500_000_000 ) , Some ( expected_entry) , ) ;
111130 assert_eq ! (
112131 payment_queue. clear( ) ,
113132 vec![ InterceptedHTLC {
0 commit comments