@@ -8,7 +8,7 @@ use lightning_types::payment::PaymentHash;
88/// remaining payments forwarded.
99#[ derive( Clone , Default , PartialEq , Eq , Debug ) ]
1010pub ( crate ) struct PaymentQueue {
11- payments : Vec < ( PaymentHash , Vec < InterceptedHTLC > ) > ,
11+ payments : Vec < PaymentQueueEntry > ,
1212}
1313
1414impl PaymentQueue {
@@ -17,37 +17,48 @@ impl PaymentQueue {
1717 }
1818
1919 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 {
20+ let payment =
21+ self . payments . iter_mut ( ) . find ( |entry| entry. payment_hash == new_htlc. payment_hash ) ;
22+ if let Some ( entry) = payment {
2223 // HTLCs within a payment should have the same payment hash.
23- debug_assert ! ( htlcs. iter( ) . all( |htlc| htlc. payment_hash == * payment_hash) ) ;
24+ debug_assert ! ( entry . htlcs. iter( ) . all( |htlc| htlc. payment_hash == entry . payment_hash) ) ;
2425 // 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) ;
26+ debug_assert ! ( entry
27+ . htlcs
28+ . iter( )
29+ . all( |htlc| htlc. intercept_id != new_htlc. intercept_id) ) ;
30+ entry. htlcs . push ( new_htlc) ;
2731 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 ( ) )
32+ entry . htlcs . iter ( ) . map ( |htlc| htlc. expected_outbound_amount_msat ) . sum ( ) ;
33+ ( total_expected_outbound_amount_msat, entry . htlcs . len ( ) )
3034 } else {
3135 let expected_outbound_amount_msat = new_htlc. expected_outbound_amount_msat ;
32- self . payments . push ( ( new_htlc. payment_hash , vec ! [ new_htlc] ) ) ;
36+ let entry =
37+ PaymentQueueEntry { payment_hash : new_htlc. payment_hash , htlcs : vec ! [ new_htlc] } ;
38+ self . payments . push ( entry) ;
3339 ( expected_outbound_amount_msat, 1 )
3440 }
3541 }
3642
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
43+ pub ( crate ) fn pop_greater_than_msat ( & mut self , amount_msat : u64 ) -> Option < PaymentQueueEntry > {
44+ let position = self . payments . iter ( ) . position ( |entry| {
45+ entry. htlcs . iter ( ) . map ( |htlc| htlc. expected_outbound_amount_msat ) . sum :: < u64 > ( )
46+ >= amount_msat
4247 } ) ;
4348 position. map ( |position| self . payments . remove ( position) )
4449 }
4550
4651 pub ( crate ) fn clear ( & mut self ) -> Vec < InterceptedHTLC > {
47- self . payments . drain ( ..) . map ( |( _k , v ) | v ) . flatten ( ) . collect ( )
52+ self . payments . drain ( ..) . map ( |entry| entry . htlcs ) . flatten ( ) . collect ( )
4853 }
4954}
5055
56+ #[ derive( Clone , PartialEq , Eq , Debug ) ]
57+ pub ( crate ) struct PaymentQueueEntry {
58+ pub ( crate ) payment_hash : PaymentHash ,
59+ pub ( crate ) htlcs : Vec < InterceptedHTLC > ,
60+ }
61+
5162#[ derive( Copy , Clone , PartialEq , Eq , Debug ) ]
5263pub ( crate ) struct InterceptedHTLC {
5364 pub ( crate ) intercept_id : InterceptId ,
@@ -90,24 +101,23 @@ mod tests {
90101 } ) ,
91102 ( 500_000_000 , 2 ) ,
92103 ) ;
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- ) ;
104+
105+ let expected_entry = PaymentQueueEntry {
106+ payment_hash : PaymentHash ( [ 100 ; 32 ] ) ,
107+ htlcs : vec ! [
108+ InterceptedHTLC {
109+ intercept_id: InterceptId ( [ 0 ; 32 ] ) ,
110+ expected_outbound_amount_msat: 200_000_000 ,
111+ payment_hash: PaymentHash ( [ 100 ; 32 ] ) ,
112+ } ,
113+ InterceptedHTLC {
114+ intercept_id: InterceptId ( [ 2 ; 32 ] ) ,
115+ expected_outbound_amount_msat: 300_000_000 ,
116+ payment_hash: PaymentHash ( [ 100 ; 32 ] ) ,
117+ } ,
118+ ] ,
119+ } ;
120+ assert_eq ! ( payment_queue. pop_greater_than_msat( 500_000_000 ) , Some ( expected_entry) , ) ;
111121 assert_eq ! (
112122 payment_queue. clear( ) ,
113123 vec![ InterceptedHTLC {
0 commit comments