@@ -8,7 +8,7 @@ use lightning_types::payment::PaymentHash;
8
8
/// remaining payments forwarded.
9
9
#[ derive( Clone , Default , PartialEq , Eq , Debug ) ]
10
10
pub ( crate ) struct PaymentQueue {
11
- payments : Vec < ( PaymentHash , Vec < InterceptedHTLC > ) > ,
11
+ payments : Vec < PaymentQueueEntry > ,
12
12
}
13
13
14
14
impl PaymentQueue {
@@ -17,37 +17,48 @@ impl PaymentQueue {
17
17
}
18
18
19
19
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 {
22
23
// 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) ) ;
24
25
// 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) ;
27
31
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 ( ) )
30
34
} else {
31
35
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) ;
33
39
( expected_outbound_amount_msat, 1 )
34
40
}
35
41
}
36
42
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
42
47
} ) ;
43
48
position. map ( |position| self . payments . remove ( position) )
44
49
}
45
50
46
51
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 ( )
48
53
}
49
54
}
50
55
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
+
51
62
#[ derive( Copy , Clone , PartialEq , Eq , Debug ) ]
52
63
pub ( crate ) struct InterceptedHTLC {
53
64
pub ( crate ) intercept_id : InterceptId ,
@@ -90,24 +101,23 @@ mod tests {
90
101
} ) ,
91
102
( 500_000_000 , 2 ) ,
92
103
) ;
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) , ) ;
111
121
assert_eq ! (
112
122
payment_queue. clear( ) ,
113
123
vec![ InterceptedHTLC {
0 commit comments