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
+
1
10
use alloc:: vec:: Vec ;
2
11
3
12
use lightning:: ln:: channelmanager:: InterceptId ;
@@ -8,7 +17,7 @@ use lightning_types::payment::PaymentHash;
8
17
/// remaining payments forwarded.
9
18
#[ derive( Clone , Default , PartialEq , Eq , Debug ) ]
10
19
pub ( crate ) struct PaymentQueue {
11
- payments : Vec < ( PaymentHash , Vec < InterceptedHTLC > ) > ,
20
+ payments : Vec < PaymentQueueEntry > ,
12
21
}
13
22
14
23
impl PaymentQueue {
@@ -17,37 +26,48 @@ impl PaymentQueue {
17
26
}
18
27
19
28
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 {
22
32
// 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) ) ;
24
34
// 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) ;
27
40
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 ( ) )
30
43
} else {
31
44
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) ;
33
48
( expected_outbound_amount_msat, 1 )
34
49
}
35
50
}
36
51
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
42
56
} ) ;
43
57
position. map ( |position| self . payments . remove ( position) )
44
58
}
45
59
46
60
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 ( )
48
62
}
49
63
}
50
64
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
+
51
71
#[ derive( Copy , Clone , PartialEq , Eq , Debug ) ]
52
72
pub ( crate ) struct InterceptedHTLC {
53
73
pub ( crate ) intercept_id : InterceptId ,
@@ -90,24 +110,23 @@ mod tests {
90
110
} ) ,
91
111
( 500_000_000 , 2 ) ,
92
112
) ;
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) , ) ;
111
130
assert_eq ! (
112
131
payment_queue. clear( ) ,
113
132
vec![ InterceptedHTLC {
0 commit comments