@@ -1365,3 +1365,194 @@ fn first_message_on_recv_ordering() {
1365
1365
claim_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] ] , payment_preimage_1) ;
1366
1366
claim_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] ] , payment_preimage_2) ;
1367
1367
}
1368
+
1369
+ #[ test]
1370
+ fn test_monitor_update_fail_claim ( ) {
1371
+ // Basic test for monitor update failures when processing claim_funds calls.
1372
+ // We set up a simple 3-node network, sending a payment from A to B and failing B's monitor
1373
+ // update to claim the payment. We then send a payment C->B->A, making the forward of this
1374
+ // payment from B to A fail due to the paused channel. Finally, we restore the channel monitor
1375
+ // updating and claim the payment on B.
1376
+ let mut nodes = create_network ( 3 ) ;
1377
+ let chan_1 = create_announced_chan_between_nodes ( & nodes, 0 , 1 ) ;
1378
+ create_announced_chan_between_nodes ( & nodes, 1 , 2 ) ;
1379
+
1380
+ // Rebalance a bit so that we can send backwards from 3 to 2.
1381
+ send_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] , & nodes[ 2 ] ] , 5000000 ) ;
1382
+
1383
+ let ( payment_preimage_1, _) = route_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] ] , 1000000 ) ;
1384
+
1385
+ * nodes[ 1 ] . chan_monitor . update_ret . lock ( ) . unwrap ( ) = Err ( ChannelMonitorUpdateErr :: TemporaryFailure ) ;
1386
+ assert ! ( nodes[ 1 ] . node. claim_funds( payment_preimage_1) ) ;
1387
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
1388
+
1389
+ let route = nodes[ 2 ] . router . get_route ( & nodes[ 0 ] . node . get_our_node_id ( ) , None , & Vec :: new ( ) , 1000000 , TEST_FINAL_CLTV ) . unwrap ( ) ;
1390
+ let ( _, payment_hash_2) = get_payment_preimage_hash ! ( nodes[ 0 ] ) ;
1391
+ nodes[ 2 ] . node . send_payment ( route, payment_hash_2) . unwrap ( ) ;
1392
+ check_added_monitors ! ( nodes[ 2 ] , 1 ) ;
1393
+
1394
+ // Successfully update the monitor on the 1<->2 channel, but the 0<->1 channel should still be
1395
+ // paused, so forward shouldn't succeed until we call test_restore_channel_monitor().
1396
+ * nodes[ 1 ] . chan_monitor . update_ret . lock ( ) . unwrap ( ) = Ok ( ( ) ) ;
1397
+
1398
+ let mut events = nodes[ 2 ] . node . get_and_clear_pending_msg_events ( ) ;
1399
+ assert_eq ! ( events. len( ) , 1 ) ;
1400
+ let payment_event = SendEvent :: from_event ( events. pop ( ) . unwrap ( ) ) ;
1401
+ nodes[ 1 ] . node . handle_update_add_htlc ( & nodes[ 2 ] . node . get_our_node_id ( ) , & payment_event. msgs [ 0 ] ) . unwrap ( ) ;
1402
+ commitment_signed_dance ! ( nodes[ 1 ] , nodes[ 2 ] , payment_event. commitment_msg, false , true ) ;
1403
+
1404
+ let bs_fail_update = get_htlc_update_msgs ! ( nodes[ 1 ] , nodes[ 2 ] . node. get_our_node_id( ) ) ;
1405
+ nodes[ 2 ] . node . handle_update_fail_htlc ( & nodes[ 1 ] . node . get_our_node_id ( ) , & bs_fail_update. update_fail_htlcs [ 0 ] ) . unwrap ( ) ;
1406
+ commitment_signed_dance ! ( nodes[ 2 ] , nodes[ 1 ] , bs_fail_update. commitment_signed, false , true ) ;
1407
+
1408
+ let msg_events = nodes[ 2 ] . node . get_and_clear_pending_msg_events ( ) ;
1409
+ assert_eq ! ( msg_events. len( ) , 1 ) ;
1410
+ match msg_events[ 0 ] {
1411
+ MessageSendEvent :: PaymentFailureNetworkUpdate { update : msgs:: HTLCFailChannelUpdate :: ChannelUpdateMessage { ref msg } } => {
1412
+ assert_eq ! ( msg. contents. short_channel_id, chan_1. 0 . contents. short_channel_id) ;
1413
+ assert_eq ! ( msg. contents. flags & 2 , 2 ) ; // temp disabled
1414
+ } ,
1415
+ _ => panic ! ( "Unexpected event" ) ,
1416
+ }
1417
+
1418
+ let events = nodes[ 2 ] . node . get_and_clear_pending_events ( ) ;
1419
+ assert_eq ! ( events. len( ) , 1 ) ;
1420
+ if let Event :: PaymentFailed { payment_hash, rejected_by_dest, .. } = events[ 0 ] {
1421
+ assert_eq ! ( payment_hash, payment_hash_2) ;
1422
+ assert ! ( !rejected_by_dest) ;
1423
+ } else { panic ! ( "Unexpected event!" ) ; }
1424
+
1425
+ // Now restore monitor updating on the 0<->1 channel and claim the funds on B.
1426
+ nodes[ 1 ] . node . test_restore_channel_monitor ( ) ;
1427
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
1428
+
1429
+ let bs_fulfill_update = get_htlc_update_msgs ! ( nodes[ 1 ] , nodes[ 0 ] . node. get_our_node_id( ) ) ;
1430
+ nodes[ 0 ] . node . handle_update_fulfill_htlc ( & nodes[ 1 ] . node . get_our_node_id ( ) , & bs_fulfill_update. update_fulfill_htlcs [ 0 ] ) . unwrap ( ) ;
1431
+ commitment_signed_dance ! ( nodes[ 0 ] , nodes[ 1 ] , bs_fulfill_update. commitment_signed, false ) ;
1432
+
1433
+ let events = nodes[ 0 ] . node . get_and_clear_pending_events ( ) ;
1434
+ assert_eq ! ( events. len( ) , 1 ) ;
1435
+ if let Event :: PaymentSent { payment_preimage, .. } = events[ 0 ] {
1436
+ assert_eq ! ( payment_preimage, payment_preimage_1) ;
1437
+ } else { panic ! ( "Unexpected event!" ) ; }
1438
+ }
1439
+
1440
+ #[ test]
1441
+ fn test_monitor_update_on_pending_forwards ( ) {
1442
+ // Basic test for monitor update failures when processing pending HTLC fail/add forwards.
1443
+ // We do this with a simple 3-node network, sending a payment from A to C and one from C to A.
1444
+ // The payment from A to C will be failed by C and pending a back-fail to A, while the payment
1445
+ // from C to A will be pending a forward to A.
1446
+ let mut nodes = create_network ( 3 ) ;
1447
+ create_announced_chan_between_nodes ( & nodes, 0 , 1 ) ;
1448
+ create_announced_chan_between_nodes ( & nodes, 1 , 2 ) ;
1449
+
1450
+ // Rebalance a bit so that we can send backwards from 3 to 1.
1451
+ send_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] , & nodes[ 2 ] ] , 5000000 ) ;
1452
+
1453
+ let ( _, payment_hash_1) = route_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] , & nodes[ 2 ] ] , 1000000 ) ;
1454
+ assert ! ( nodes[ 2 ] . node. fail_htlc_backwards( & payment_hash_1, 1000000 ) ) ;
1455
+ expect_pending_htlcs_forwardable ! ( nodes[ 2 ] ) ;
1456
+ check_added_monitors ! ( nodes[ 2 ] , 1 ) ;
1457
+
1458
+ let cs_fail_update = get_htlc_update_msgs ! ( nodes[ 2 ] , nodes[ 1 ] . node. get_our_node_id( ) ) ;
1459
+ nodes[ 1 ] . node . handle_update_fail_htlc ( & nodes[ 2 ] . node . get_our_node_id ( ) , & cs_fail_update. update_fail_htlcs [ 0 ] ) . unwrap ( ) ;
1460
+ commitment_signed_dance ! ( nodes[ 1 ] , nodes[ 2 ] , cs_fail_update. commitment_signed, true , true ) ;
1461
+ assert ! ( nodes[ 1 ] . node. get_and_clear_pending_msg_events( ) . is_empty( ) ) ;
1462
+
1463
+ let route = nodes[ 2 ] . router . get_route ( & nodes[ 0 ] . node . get_our_node_id ( ) , None , & Vec :: new ( ) , 1000000 , TEST_FINAL_CLTV ) . unwrap ( ) ;
1464
+ let ( payment_preimage_2, payment_hash_2) = get_payment_preimage_hash ! ( nodes[ 0 ] ) ;
1465
+ nodes[ 2 ] . node . send_payment ( route, payment_hash_2) . unwrap ( ) ;
1466
+ check_added_monitors ! ( nodes[ 2 ] , 1 ) ;
1467
+
1468
+ let mut events = nodes[ 2 ] . node . get_and_clear_pending_msg_events ( ) ;
1469
+ assert_eq ! ( events. len( ) , 1 ) ;
1470
+ let payment_event = SendEvent :: from_event ( events. pop ( ) . unwrap ( ) ) ;
1471
+ nodes[ 1 ] . node . handle_update_add_htlc ( & nodes[ 2 ] . node . get_our_node_id ( ) , & payment_event. msgs [ 0 ] ) . unwrap ( ) ;
1472
+ commitment_signed_dance ! ( nodes[ 1 ] , nodes[ 2 ] , payment_event. commitment_msg, false ) ;
1473
+
1474
+ * nodes[ 1 ] . chan_monitor . update_ret . lock ( ) . unwrap ( ) = Err ( ChannelMonitorUpdateErr :: TemporaryFailure ) ;
1475
+ expect_pending_htlcs_forwardable ! ( nodes[ 1 ] ) ;
1476
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
1477
+ assert ! ( nodes[ 1 ] . node. get_and_clear_pending_msg_events( ) . is_empty( ) ) ;
1478
+
1479
+ * nodes[ 1 ] . chan_monitor . update_ret . lock ( ) . unwrap ( ) = Ok ( ( ) ) ;
1480
+ nodes[ 1 ] . node . test_restore_channel_monitor ( ) ;
1481
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
1482
+
1483
+ let bs_updates = get_htlc_update_msgs ! ( nodes[ 1 ] , nodes[ 0 ] . node. get_our_node_id( ) ) ;
1484
+ nodes[ 0 ] . node . handle_update_fail_htlc ( & nodes[ 1 ] . node . get_our_node_id ( ) , & bs_updates. update_fail_htlcs [ 0 ] ) . unwrap ( ) ;
1485
+ nodes[ 0 ] . node . handle_update_add_htlc ( & nodes[ 1 ] . node . get_our_node_id ( ) , & bs_updates. update_add_htlcs [ 0 ] ) . unwrap ( ) ;
1486
+ commitment_signed_dance ! ( nodes[ 0 ] , nodes[ 1 ] , bs_updates. commitment_signed, false , true ) ;
1487
+
1488
+ let events = nodes[ 0 ] . node . get_and_clear_pending_events ( ) ;
1489
+ assert_eq ! ( events. len( ) , 2 ) ;
1490
+ if let Event :: PaymentFailed { payment_hash, rejected_by_dest, .. } = events[ 0 ] {
1491
+ assert_eq ! ( payment_hash, payment_hash_1) ;
1492
+ assert ! ( rejected_by_dest) ;
1493
+ } else { panic ! ( "Unexpected event!" ) ; }
1494
+ match events[ 1 ] {
1495
+ Event :: PendingHTLCsForwardable { .. } => { } ,
1496
+ _ => panic ! ( "Unexpected event" ) ,
1497
+ } ;
1498
+ nodes[ 0 ] . node . channel_state . lock ( ) . unwrap ( ) . next_forward = Instant :: now ( ) ;
1499
+ nodes[ 0 ] . node . process_pending_htlc_forwards ( ) ;
1500
+ expect_payment_received ! ( nodes[ 0 ] , payment_hash_2, 1000000 ) ;
1501
+
1502
+ claim_payment ( & nodes[ 2 ] , & [ & nodes[ 1 ] , & nodes[ 0 ] ] , payment_preimage_2) ;
1503
+ }
1504
+
1505
+ #[ test]
1506
+ fn monitor_update_claim_fail_no_response ( ) {
1507
+ // Test for claim_funds resulting in both a monitor update failure and no message response (due
1508
+ // to channel being AwaitingRAA).
1509
+ // Backported from chanmon_fail_consistency fuzz tests as an unmerged version of the handling
1510
+ // code was broken.
1511
+ let mut nodes = create_network ( 2 ) ;
1512
+ create_announced_chan_between_nodes ( & nodes, 0 , 1 ) ;
1513
+
1514
+ // Forward a payment for B to claim
1515
+ let ( payment_preimage_1, _) = route_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] ] , 1000000 ) ;
1516
+
1517
+ // Now start forwarding a second payment, skipping the last RAA so B is in AwaitingRAA
1518
+ let route = nodes[ 0 ] . router . get_route ( & nodes[ 1 ] . node . get_our_node_id ( ) , None , & Vec :: new ( ) , 1000000 , TEST_FINAL_CLTV ) . unwrap ( ) ;
1519
+ let ( payment_preimage_2, payment_hash_2) = get_payment_preimage_hash ! ( nodes[ 0 ] ) ;
1520
+ nodes[ 0 ] . node . send_payment ( route, payment_hash_2) . unwrap ( ) ;
1521
+ check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
1522
+
1523
+ let mut events = nodes[ 0 ] . node . get_and_clear_pending_msg_events ( ) ;
1524
+ assert_eq ! ( events. len( ) , 1 ) ;
1525
+ let payment_event = SendEvent :: from_event ( events. pop ( ) . unwrap ( ) ) ;
1526
+ nodes[ 1 ] . node . handle_update_add_htlc ( & nodes[ 0 ] . node . get_our_node_id ( ) , & payment_event. msgs [ 0 ] ) . unwrap ( ) ;
1527
+ let as_raa = commitment_signed_dance ! ( nodes[ 1 ] , nodes[ 0 ] , payment_event. commitment_msg, false , true , false , true ) ;
1528
+
1529
+ * nodes[ 1 ] . chan_monitor . update_ret . lock ( ) . unwrap ( ) = Err ( ChannelMonitorUpdateErr :: TemporaryFailure ) ;
1530
+ assert ! ( nodes[ 1 ] . node. claim_funds( payment_preimage_1) ) ;
1531
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
1532
+ assert ! ( nodes[ 1 ] . node. get_and_clear_pending_msg_events( ) . is_empty( ) ) ;
1533
+
1534
+ * nodes[ 1 ] . chan_monitor . update_ret . lock ( ) . unwrap ( ) = Ok ( ( ) ) ;
1535
+ nodes[ 1 ] . node . test_restore_channel_monitor ( ) ;
1536
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
1537
+ assert ! ( nodes[ 1 ] . node. get_and_clear_pending_msg_events( ) . is_empty( ) ) ;
1538
+
1539
+ nodes[ 1 ] . node . handle_revoke_and_ack ( & nodes[ 0 ] . node . get_our_node_id ( ) , & as_raa) . unwrap ( ) ;
1540
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
1541
+ expect_pending_htlcs_forwardable ! ( nodes[ 1 ] ) ;
1542
+ expect_payment_received ! ( nodes[ 1 ] , payment_hash_2, 1000000 ) ;
1543
+
1544
+ let bs_updates = get_htlc_update_msgs ! ( nodes[ 1 ] , nodes[ 0 ] . node. get_our_node_id( ) ) ;
1545
+ nodes[ 0 ] . node . handle_update_fulfill_htlc ( & nodes[ 1 ] . node . get_our_node_id ( ) , & bs_updates. update_fulfill_htlcs [ 0 ] ) . unwrap ( ) ;
1546
+ commitment_signed_dance ! ( nodes[ 0 ] , nodes[ 1 ] , bs_updates. commitment_signed, false ) ;
1547
+
1548
+ let events = nodes[ 0 ] . node . get_and_clear_pending_events ( ) ;
1549
+ assert_eq ! ( events. len( ) , 1 ) ;
1550
+ match events[ 0 ] {
1551
+ Event :: PaymentSent { ref payment_preimage } => {
1552
+ assert_eq ! ( * payment_preimage, payment_preimage_1) ;
1553
+ } ,
1554
+ _ => panic ! ( "Unexpected event" ) ,
1555
+ }
1556
+
1557
+ claim_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] ] , payment_preimage_2) ;
1558
+ }
0 commit comments