@@ -39,7 +39,7 @@ impl_writeable_tlv_based!(PaymentDetails, {
39
39
} ) ;
40
40
41
41
/// Represents the direction of a payment.
42
- #[ derive( Clone , Debug , PartialEq , Eq ) ]
42
+ #[ derive( Copy , Clone , Debug , PartialEq , Eq ) ]
43
43
pub enum PaymentDirection {
44
44
/// The payment is inbound.
45
45
Inbound ,
@@ -53,7 +53,7 @@ impl_writeable_tlv_based_enum!(PaymentDirection,
53
53
) ;
54
54
55
55
/// Represents the current status of a payment.
56
- #[ derive( Clone , Debug , PartialEq , Eq ) ]
56
+ #[ derive( Copy , Clone , Debug , PartialEq , Eq ) ]
57
57
pub enum PaymentStatus {
58
58
/// The payment is still pending.
59
59
Pending ,
@@ -69,6 +69,29 @@ impl_writeable_tlv_based_enum!(PaymentStatus,
69
69
( 2 , Failed ) => { } ;
70
70
) ;
71
71
72
+ #[ derive( Clone , Debug , PartialEq , Eq ) ]
73
+ pub ( crate ) struct PaymentDetailsUpdate {
74
+ pub hash : PaymentHash ,
75
+ pub preimage : Option < Option < PaymentPreimage > > ,
76
+ pub secret : Option < Option < PaymentSecret > > ,
77
+ pub amount_msat : Option < Option < u64 > > ,
78
+ pub direction : Option < PaymentDirection > ,
79
+ pub status : Option < PaymentStatus > ,
80
+ }
81
+
82
+ impl PaymentDetailsUpdate {
83
+ pub fn new ( hash : PaymentHash ) -> Self {
84
+ Self {
85
+ hash,
86
+ preimage : None ,
87
+ secret : None ,
88
+ amount_msat : None ,
89
+ direction : None ,
90
+ status : None ,
91
+ }
92
+ }
93
+ }
94
+
72
95
pub ( crate ) struct PaymentStore < K : Deref + Clone , L : Deref >
73
96
where
74
97
K :: Target : KVStore ,
@@ -122,32 +145,28 @@ where
122
145
self . payments . lock ( ) . unwrap ( ) . contains_key ( hash)
123
146
}
124
147
125
- pub ( crate ) fn update (
126
- & self , hash : & PaymentHash , update_preimage : Option < Option < PaymentPreimage > > ,
127
- update_secret : Option < Option < PaymentSecret > > , update_amount_msat : Option < Option < u64 > > ,
128
- update_status : Option < PaymentStatus > ,
129
- ) -> Result < bool , Error > {
148
+ pub ( crate ) fn update ( & self , update : & PaymentDetailsUpdate ) -> Result < bool , Error > {
130
149
let mut updated = false ;
131
150
let mut locked_payments = self . payments . lock ( ) . unwrap ( ) ;
132
151
133
- if let Some ( payment) = locked_payments. get_mut ( hash) {
134
- if let Some ( preimage_opt) = update_preimage {
152
+ if let Some ( payment) = locked_payments. get_mut ( & update . hash ) {
153
+ if let Some ( preimage_opt) = update . preimage {
135
154
payment. preimage = preimage_opt;
136
155
}
137
156
138
- if let Some ( secret_opt) = update_secret {
157
+ if let Some ( secret_opt) = update . secret {
139
158
payment. secret = secret_opt;
140
159
}
141
160
142
- if let Some ( amount_opt) = update_amount_msat {
161
+ if let Some ( amount_opt) = update . amount_msat {
143
162
payment. amount_msat = amount_opt;
144
163
}
145
164
146
- if let Some ( status) = update_status {
165
+ if let Some ( status) = update . status {
147
166
payment. status = status;
148
167
}
149
168
150
- self . write_info_and_commit ( hash, payment) ?;
169
+ self . write_info_and_commit ( & update . hash , payment) ?;
151
170
updated = true ;
152
171
}
153
172
@@ -237,10 +256,9 @@ mod tests {
237
256
assert_eq ! ( Ok ( true ) , payment_store. insert( payment) ) ;
238
257
assert ! ( store. get_and_clear_did_persist( ) ) ;
239
258
240
- assert_eq ! (
241
- Ok ( true ) ,
242
- payment_store. update( & hash, None , None , None , Some ( PaymentStatus :: Succeeded ) )
243
- ) ;
259
+ let mut update = PaymentDetailsUpdate :: new ( hash) ;
260
+ update. status = Some ( PaymentStatus :: Succeeded ) ;
261
+ assert_eq ! ( Ok ( true ) , payment_store. update( & update) ) ;
244
262
assert ! ( store. get_and_clear_did_persist( ) ) ;
245
263
246
264
assert_eq ! ( PaymentStatus :: Succeeded , payment_store. get( & hash) . unwrap( ) . status) ;
0 commit comments