@@ -270,48 +270,62 @@ impl Wallet {
270270
271271 if payment_status == PaymentStatus :: Pending {
272272 let pending_payment =
273- self . create_pending_payment_from_tx ( payment , Vec :: new ( ) ) ;
273+ self . create_pending_payment_from_tx ( payment_id , txid , Vec :: new ( ) ) ;
274274
275275 self . runtime . block_on (
276276 self . pending_payment_store . insert_or_update ( pending_payment) ,
277277 ) ?;
278+ } else {
279+ self . runtime . block_on ( self . pending_payment_store . remove ( & payment_id) ) ?;
278280 }
279281 } ,
280282 WalletEvent :: ChainTipChanged { new_tip, .. } => {
281283 let pending_payments: Vec < PendingPaymentDetails > =
282- self . pending_payment_store . list_filter ( |p| {
283- debug_assert ! (
284- p. details. status == PaymentStatus :: Pending ,
285- "Non-pending payment {:?} found in pending store" ,
286- p. details. id,
287- ) ;
288- p. details . status == PaymentStatus :: Pending
289- && matches ! ( p. details. kind, PaymentKind :: Onchain { .. } )
290- } ) ;
284+ self . pending_payment_store . list_filter ( |_| true ) ;
291285
292286 let mut unconfirmed_outbound_txids: Vec < Txid > = Vec :: new ( ) ;
293287
294- for mut payment in pending_payments {
295- match payment. details . kind {
288+ for pending_payment in pending_payments {
289+ let Some ( mut payment) = self . payment_store . get ( & pending_payment. payment_id )
290+ else {
291+ self . runtime . block_on (
292+ self . pending_payment_store . remove ( & pending_payment. payment_id ) ,
293+ ) ?;
294+ continue ;
295+ } ;
296+
297+ debug_assert ! (
298+ payment. status == PaymentStatus :: Pending ,
299+ "Non-pending payment {:?} found in pending store" ,
300+ payment. id,
301+ ) ;
302+ if payment. status != PaymentStatus :: Pending {
303+ self . runtime . block_on (
304+ self . pending_payment_store . remove ( & pending_payment. payment_id ) ,
305+ ) ?;
306+ continue ;
307+ }
308+
309+ match & payment. kind {
296310 PaymentKind :: Onchain {
297311 status : ConfirmationStatus :: Confirmed { height, .. } ,
298312 ..
299313 } => {
300- let payment_id = payment. details . id ;
301- if new_tip. height >= height + ANTI_REORG_DELAY - 1 {
302- payment. details . status = PaymentStatus :: Succeeded ;
314+ if new_tip. height >= * height + ANTI_REORG_DELAY - 1 {
315+ payment. status = PaymentStatus :: Succeeded ;
316+ self . runtime
317+ . block_on ( self . payment_store . insert_or_update ( payment) ) ?;
303318 self . runtime . block_on (
304- self . payment_store . insert_or_update ( payment. details ) ,
319+ self . pending_payment_store
320+ . remove ( & pending_payment. payment_id ) ,
305321 ) ?;
306- self . runtime
307- . block_on ( self . pending_payment_store . remove ( & payment_id) ) ?;
308322 }
309323 } ,
310324 PaymentKind :: Onchain {
311325 txid,
312326 status : ConfirmationStatus :: Unconfirmed ,
313- } if payment. details . direction == PaymentDirection :: Outbound => {
314- unconfirmed_outbound_txids. push ( txid) ;
327+ } if payment. direction == PaymentDirection :: Outbound => {
328+ unconfirmed_outbound_txids. push ( * txid) ;
315329 } ,
316330 _ => { } ,
317331 }
@@ -359,7 +373,7 @@ impl Wallet {
359373 ConfirmationStatus :: Unconfirmed ,
360374 ) ;
361375 let pending_payment =
362- self . create_pending_payment_from_tx ( payment . clone ( ) , Vec :: new ( ) ) ;
376+ self . create_pending_payment_from_tx ( payment_id , txid , Vec :: new ( ) ) ;
363377 self . runtime . block_on ( self . payment_store . insert_or_update ( payment) ) ?;
364378 self . runtime
365379 . block_on ( self . pending_payment_store . insert_or_update ( pending_payment) ) ?;
@@ -389,8 +403,22 @@ impl Wallet {
389403 ) ;
390404 let payment =
391405 self . payment_store . get ( & payment_id) . ok_or ( Error :: InvalidPaymentId ) ?;
392- let pending_payment_details =
393- self . create_pending_payment_from_tx ( payment, conflict_txids. clone ( ) ) ;
406+ let payment_txid = match & payment. kind {
407+ PaymentKind :: Onchain { txid, .. } => * txid,
408+ _ => {
409+ log_error ! (
410+ self . logger,
411+ "Payment {:?} is not on-chain during WalletEvent::TxReplaced" ,
412+ payment_id,
413+ ) ;
414+ continue ;
415+ } ,
416+ } ;
417+ let pending_payment_details = self . create_pending_payment_from_tx (
418+ payment_id,
419+ payment_txid,
420+ conflict_txids,
421+ ) ;
394422
395423 self . runtime . block_on (
396424 self . pending_payment_store . insert_or_update ( pending_payment_details) ,
@@ -409,7 +437,7 @@ impl Wallet {
409437 ConfirmationStatus :: Unconfirmed ,
410438 ) ;
411439 let pending_payment =
412- self . create_pending_payment_from_tx ( payment . clone ( ) , Vec :: new ( ) ) ;
440+ self . create_pending_payment_from_tx ( payment_id , txid , Vec :: new ( ) ) ;
413441 self . runtime . block_on ( self . payment_store . insert_or_update ( payment) ) ?;
414442 self . runtime
415443 . block_on ( self . pending_payment_store . insert_or_update ( pending_payment) ) ?;
@@ -1207,9 +1235,9 @@ impl Wallet {
12071235 }
12081236
12091237 fn create_pending_payment_from_tx (
1210- & self , payment : PaymentDetails , conflicting_txids : Vec < Txid > ,
1238+ & self , payment_id : PaymentId , txid : Txid , conflicting_txids : Vec < Txid > ,
12111239 ) -> PendingPaymentDetails {
1212- PendingPaymentDetails :: new ( payment , conflicting_txids)
1240+ PendingPaymentDetails :: new ( payment_id , txid , conflicting_txids)
12131241 }
12141242
12151243 fn find_payment_by_txid ( & self , target_txid : Txid ) -> Option < PaymentId > {
@@ -1220,13 +1248,10 @@ impl Wallet {
12201248
12211249 if let Some ( replaced_details) = self
12221250 . pending_payment_store
1223- . list_filter ( |p| {
1224- matches ! ( p. details. kind, PaymentKind :: Onchain { txid, .. } if txid == target_txid)
1225- || p. conflicting_txids . contains ( & target_txid)
1226- } )
1251+ . list_filter ( |p| p. txid == target_txid || p. conflicting_txids . contains ( & target_txid) )
12271252 . first ( )
12281253 {
1229- return Some ( replaced_details. details . id ) ;
1254+ return Some ( replaced_details. payment_id ) ;
12301255 }
12311256
12321257 None
@@ -1428,11 +1453,11 @@ impl Wallet {
14281453 ) ;
14291454
14301455 let pending_payment_store =
1431- self . create_pending_payment_from_tx ( new_payment. clone ( ) , Vec :: new ( ) ) ;
1456+ self . create_pending_payment_from_tx ( new_payment. id , new_txid , vec ! [ txid ] ) ;
14321457
1458+ self . runtime . block_on ( self . payment_store . insert_or_update ( new_payment) ) ?;
14331459 self . runtime
14341460 . block_on ( self . pending_payment_store . insert_or_update ( pending_payment_store) ) ?;
1435- self . runtime . block_on ( self . payment_store . insert_or_update ( new_payment) ) ?;
14361461
14371462 log_info ! ( self . logger, "RBF successful: replaced {} with {}" , txid, new_txid) ;
14381463
0 commit comments