@@ -59,6 +59,104 @@ impl PaymentDetails {
59
59
. as_secs ( ) ;
60
60
Self { id, kind, amount_msat, direction, status, latest_update_timestamp }
61
61
}
62
+
63
+ pub ( crate ) fn update ( & mut self , update : & PaymentDetailsUpdate ) -> bool {
64
+ debug_assert_eq ! (
65
+ self . id, update. id,
66
+ "We should only ever override payment data for the same payment id"
67
+ ) ;
68
+
69
+ let mut updated = false ;
70
+
71
+ macro_rules! update_if_necessary {
72
+ ( $val: expr, $update: expr) => {
73
+ if $val != $update {
74
+ $val = $update;
75
+ updated = true ;
76
+ }
77
+ } ;
78
+ }
79
+
80
+ if let Some ( hash_opt) = update. hash {
81
+ match self . kind {
82
+ PaymentKind :: Bolt12Offer { ref mut hash, .. } => {
83
+ debug_assert_eq ! (
84
+ self . direction,
85
+ PaymentDirection :: Outbound ,
86
+ "We should only ever override payment hash for outbound BOLT 12 payments"
87
+ ) ;
88
+ update_if_necessary ! ( * hash, hash_opt) ;
89
+ } ,
90
+ PaymentKind :: Bolt12Refund { ref mut hash, .. } => {
91
+ debug_assert_eq ! (
92
+ self . direction,
93
+ PaymentDirection :: Outbound ,
94
+ "We should only ever override payment hash for outbound BOLT 12 payments"
95
+ ) ;
96
+ update_if_necessary ! ( * hash, hash_opt) ;
97
+ } ,
98
+ _ => {
99
+ // We can omit updating the hash for BOLT11 payments as the payment hash
100
+ // will always be known from the beginning.
101
+ } ,
102
+ }
103
+ }
104
+ if let Some ( preimage_opt) = update. preimage {
105
+ match self . kind {
106
+ PaymentKind :: Bolt11 { ref mut preimage, .. } => {
107
+ update_if_necessary ! ( * preimage, preimage_opt)
108
+ } ,
109
+ PaymentKind :: Bolt11Jit { ref mut preimage, .. } => {
110
+ update_if_necessary ! ( * preimage, preimage_opt)
111
+ } ,
112
+ PaymentKind :: Bolt12Offer { ref mut preimage, .. } => {
113
+ update_if_necessary ! ( * preimage, preimage_opt)
114
+ } ,
115
+ PaymentKind :: Bolt12Refund { ref mut preimage, .. } => {
116
+ update_if_necessary ! ( * preimage, preimage_opt)
117
+ } ,
118
+ PaymentKind :: Spontaneous { ref mut preimage, .. } => {
119
+ update_if_necessary ! ( * preimage, preimage_opt)
120
+ } ,
121
+ _ => { } ,
122
+ }
123
+ }
124
+
125
+ if let Some ( secret_opt) = update. secret {
126
+ match self . kind {
127
+ PaymentKind :: Bolt11 { ref mut secret, .. } => {
128
+ update_if_necessary ! ( * secret, secret_opt)
129
+ } ,
130
+ PaymentKind :: Bolt11Jit { ref mut secret, .. } => {
131
+ update_if_necessary ! ( * secret, secret_opt)
132
+ } ,
133
+ PaymentKind :: Bolt12Offer { ref mut secret, .. } => {
134
+ update_if_necessary ! ( * secret, secret_opt)
135
+ } ,
136
+ PaymentKind :: Bolt12Refund { ref mut secret, .. } => {
137
+ update_if_necessary ! ( * secret, secret_opt)
138
+ } ,
139
+ _ => { } ,
140
+ }
141
+ }
142
+
143
+ if let Some ( amount_opt) = update. amount_msat {
144
+ update_if_necessary ! ( self . amount_msat, amount_opt) ;
145
+ }
146
+
147
+ if let Some ( status) = update. status {
148
+ update_if_necessary ! ( self . status, status) ;
149
+ }
150
+
151
+ if updated {
152
+ self . latest_update_timestamp = SystemTime :: now ( )
153
+ . duration_since ( UNIX_EPOCH )
154
+ . unwrap_or ( Duration :: from_secs ( 0 ) )
155
+ . as_secs ( ) ;
156
+ }
157
+
158
+ updated
159
+ }
62
160
}
63
161
64
162
impl Writeable for PaymentDetails {
@@ -401,60 +499,10 @@ where
401
499
let mut locked_payments = self . payments . lock ( ) . unwrap ( ) ;
402
500
403
501
if let Some ( payment) = locked_payments. get_mut ( & update. id ) {
404
- if let Some ( hash_opt) = update. hash {
405
- match payment. kind {
406
- PaymentKind :: Bolt12Offer { ref mut hash, .. } => {
407
- debug_assert_eq ! ( payment. direction, PaymentDirection :: Outbound ,
408
- "We should only ever override payment hash for outbound BOLT 12 payments" ) ;
409
- * hash = hash_opt
410
- } ,
411
- PaymentKind :: Bolt12Refund { ref mut hash, .. } => {
412
- debug_assert_eq ! ( payment. direction, PaymentDirection :: Outbound ,
413
- "We should only ever override payment hash for outbound BOLT 12 payments" ) ;
414
- * hash = hash_opt
415
- } ,
416
- _ => {
417
- // We can omit updating the hash for BOLT11 payments as the payment hash
418
- // will always be known from the beginning.
419
- } ,
420
- }
502
+ updated = payment. update ( update) ;
503
+ if updated {
504
+ self . persist_info ( & update. id , payment) ?;
421
505
}
422
- if let Some ( preimage_opt) = update. preimage {
423
- match payment. kind {
424
- PaymentKind :: Bolt11 { ref mut preimage, .. } => * preimage = preimage_opt,
425
- PaymentKind :: Bolt11Jit { ref mut preimage, .. } => * preimage = preimage_opt,
426
- PaymentKind :: Bolt12Offer { ref mut preimage, .. } => * preimage = preimage_opt,
427
- PaymentKind :: Bolt12Refund { ref mut preimage, .. } => * preimage = preimage_opt,
428
- PaymentKind :: Spontaneous { ref mut preimage, .. } => * preimage = preimage_opt,
429
- _ => { } ,
430
- }
431
- }
432
-
433
- if let Some ( secret_opt) = update. secret {
434
- match payment. kind {
435
- PaymentKind :: Bolt11 { ref mut secret, .. } => * secret = secret_opt,
436
- PaymentKind :: Bolt11Jit { ref mut secret, .. } => * secret = secret_opt,
437
- PaymentKind :: Bolt12Offer { ref mut secret, .. } => * secret = secret_opt,
438
- PaymentKind :: Bolt12Refund { ref mut secret, .. } => * secret = secret_opt,
439
- _ => { } ,
440
- }
441
- }
442
-
443
- if let Some ( amount_opt) = update. amount_msat {
444
- payment. amount_msat = amount_opt;
445
- }
446
-
447
- if let Some ( status) = update. status {
448
- payment. status = status;
449
- }
450
-
451
- payment. latest_update_timestamp = SystemTime :: now ( )
452
- . duration_since ( UNIX_EPOCH )
453
- . unwrap_or ( Duration :: from_secs ( 0 ) )
454
- . as_secs ( ) ;
455
-
456
- self . persist_info ( & update. id , payment) ?;
457
- updated = true ;
458
506
}
459
507
Ok ( updated)
460
508
}
0 commit comments