@@ -3925,6 +3925,19 @@ mod tests {
3925
3925
assert!( $node_a. node. get_and_clear_pending_msg_events( ) . is_empty( ) ) ;
3926
3926
}
3927
3927
}
3928
+ } ;
3929
+ ( $node_a: expr, $node_b: expr, $commitment_signed: expr, $fail_backward: expr, $extra_message: expr, $origin_failure: expr) => {
3930
+ {
3931
+ commitment_signed_dance!( $node_a, $node_b, $commitment_signed, $fail_backward, true ) ;
3932
+ let events = $node_a. node. get_and_clear_pending_msg_events( ) ;
3933
+ assert_eq!( events. len( ) , 1 ) ;
3934
+ match events[ 0 ] {
3935
+ MessageSendEvent :: PaymentFailureNetworkUpdate { update: msgs:: HTLCFailChannelUpdate :: ChannelClosed { .. } } => {
3936
+ //TODO: check when fail_htlc_backward_internal after fecth_pending_htlcs have consistent error code
3937
+ } ,
3938
+ _ => { panic!( "Unexpected event" ) ; }
3939
+ }
3940
+ }
3928
3941
}
3929
3942
}
3930
3943
@@ -8478,6 +8491,112 @@ mod tests {
8478
8491
}
8479
8492
}
8480
8493
8494
+ #[ test]
8495
+ fn test_duplicate_payment_hash_one_failure_one_success ( ) {
8496
+ // Topology : A --> B --> C
8497
+ // We route 2 payments with same hash between B and C, one will be timeout, the other successfully claim
8498
+ let mut nodes = create_network ( 3 ) ;
8499
+
8500
+ create_announced_chan_between_nodes ( & nodes, 0 , 1 ) ;
8501
+ let chan_2 = create_announced_chan_between_nodes ( & nodes, 1 , 2 ) ;
8502
+
8503
+ let ( payment_preimage, duplicate_payment_hash) = route_payment ( & nodes[ 0 ] , & vec ! ( & nodes[ 1 ] , & nodes[ 2 ] ) [ ..] , 900000 ) ;
8504
+ * nodes[ 0 ] . network_payment_count . borrow_mut ( ) -= 1 ;
8505
+ route_payment ( & nodes[ 0 ] , & vec ! ( & nodes[ 1 ] , & nodes[ 2 ] ) [ ..] , 900000 ) ;
8506
+
8507
+ let commitment_txn = nodes[ 2 ] . node . channel_state . lock ( ) . unwrap ( ) . by_id . get ( & chan_2. 2 ) . unwrap ( ) . last_local_commitment_txn . clone ( ) ;
8508
+ assert_eq ! ( commitment_txn[ 0 ] . input. len( ) , 1 ) ;
8509
+ check_spends ! ( commitment_txn[ 0 ] , chan_2. 3 . clone( ) ) ;
8510
+
8511
+ let header = BlockHeader { version : 0x20000000 , prev_blockhash : Default :: default ( ) , merkle_root : Default :: default ( ) , time : 42 , bits : 42 , nonce : 42 } ;
8512
+ nodes[ 1 ] . chain_monitor . block_connected_with_filtering ( & Block { header, txdata : vec ! [ commitment_txn[ 0 ] . clone( ) ] } , 1 ) ;
8513
+ let htlc_timeout_tx;
8514
+ {
8515
+ let node_txn = nodes[ 1 ] . tx_broadcaster . txn_broadcasted . lock ( ) . unwrap ( ) ;
8516
+ assert_eq ! ( node_txn[ 0 ] , node_txn[ 5 ] ) ;
8517
+ assert_eq ! ( node_txn[ 1 ] , node_txn[ 6 ] ) ;
8518
+ check_spends ! ( node_txn[ 0 ] , commitment_txn[ 0 ] . clone( ) ) ;
8519
+ check_spends ! ( node_txn[ 1 ] , commitment_txn[ 0 ] . clone( ) ) ;
8520
+ check_spends ! ( node_txn[ 2 ] , chan_2. 3 . clone( ) ) ;
8521
+ check_spends ! ( node_txn[ 3 ] , node_txn[ 2 ] . clone( ) ) ;
8522
+ check_spends ! ( node_txn[ 4 ] , node_txn[ 2 ] . clone( ) ) ;
8523
+ htlc_timeout_tx = node_txn[ 1 ] . clone ( ) ;
8524
+ }
8525
+
8526
+ let events = nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
8527
+ match events[ 0 ] {
8528
+ MessageSendEvent :: BroadcastChannelUpdate { .. } => { } ,
8529
+ _ => panic ! ( "Unexepected event" ) ,
8530
+ }
8531
+
8532
+ nodes[ 2 ] . node . claim_funds ( payment_preimage) ;
8533
+ nodes[ 2 ] . chain_monitor . block_connected_with_filtering ( & Block { header, txdata : vec ! [ commitment_txn[ 0 ] . clone( ) ] } , 1 ) ;
8534
+ check_added_monitors ! ( nodes[ 2 ] , 2 ) ;
8535
+ let events = nodes[ 2 ] . node . get_and_clear_pending_msg_events ( ) ;
8536
+ match events[ 0 ] {
8537
+ MessageSendEvent :: UpdateHTLCs { .. } => { } ,
8538
+ _ => panic ! ( "Unexpected event" ) ,
8539
+ }
8540
+ match events[ 1 ] {
8541
+ MessageSendEvent :: BroadcastChannelUpdate { .. } => { } ,
8542
+ _ => panic ! ( "Unexepected event" ) ,
8543
+ }
8544
+ let htlc_success_txn = nodes[ 2 ] . tx_broadcaster . txn_broadcasted . lock ( ) . unwrap ( ) ;
8545
+ check_spends ! ( htlc_success_txn[ 2 ] , chan_2. 3 . clone( ) ) ;
8546
+ assert_eq ! ( htlc_success_txn[ 0 ] , htlc_success_txn[ 3 ] ) ;
8547
+ assert_eq ! ( htlc_success_txn[ 0 ] . input. len( ) , 1 ) ;
8548
+ assert_eq ! ( htlc_success_txn[ 0 ] . input[ 0 ] . witness. last( ) . unwrap( ) . len( ) , ACCEPTED_HTLC_SCRIPT_WEIGHT ) ;
8549
+ assert_eq ! ( htlc_success_txn[ 1 ] , htlc_success_txn[ 4 ] ) ;
8550
+ assert_eq ! ( htlc_success_txn[ 1 ] . input. len( ) , 1 ) ;
8551
+ assert_eq ! ( htlc_success_txn[ 1 ] . input[ 0 ] . witness. last( ) . unwrap( ) . len( ) , ACCEPTED_HTLC_SCRIPT_WEIGHT ) ;
8552
+ check_spends ! ( htlc_success_txn[ 0 ] , commitment_txn[ 0 ] . clone( ) ) ;
8553
+ check_spends ! ( htlc_success_txn[ 1 ] , commitment_txn[ 0 ] . clone( ) ) ;
8554
+
8555
+ nodes[ 1 ] . chain_monitor . block_connected_with_filtering ( & Block { header, txdata : vec ! [ htlc_timeout_tx] } , 200 ) ;
8556
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
8557
+ let msg_events = nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
8558
+ let htlc_updates = match msg_events[ 0 ] {
8559
+ MessageSendEvent :: UpdateHTLCs { ref node_id, ref updates } => {
8560
+ assert ! ( updates. update_add_htlcs. is_empty( ) ) ;
8561
+ assert_eq ! ( updates. update_fail_htlcs. len( ) , 1 ) ;
8562
+ assert_eq ! ( updates. update_fail_htlcs[ 0 ] . htlc_id, 1 ) ;
8563
+ assert ! ( updates. update_fulfill_htlcs. is_empty( ) ) ;
8564
+ assert ! ( updates. update_fail_malformed_htlcs. is_empty( ) ) ;
8565
+ assert_eq ! ( nodes[ 0 ] . node. get_our_node_id( ) , * node_id) ;
8566
+ ( * updates) . clone ( )
8567
+ } ,
8568
+ _ => panic ! ( "Unexpected event" ) ,
8569
+ } ;
8570
+ nodes[ 0 ] . node . handle_update_fail_htlc ( & nodes[ 1 ] . node . get_our_node_id ( ) , & htlc_updates. update_fail_htlcs [ 0 ] ) . unwrap ( ) ;
8571
+ let events = nodes[ 0 ] . node . get_and_clear_pending_msg_events ( ) ;
8572
+ assert_eq ! ( events. len( ) , 0 ) ;
8573
+ commitment_signed_dance ! ( nodes[ 0 ] , nodes[ 1 ] , & htlc_updates. commitment_signed, false , false , nodes[ 2 ] . node. get_our_node_id( ) ) ;
8574
+ let events = nodes[ 0 ] . node . get_and_clear_pending_events ( ) ;
8575
+ match events[ 0 ] {
8576
+ Event :: PaymentFailed { ref payment_hash, .. } => {
8577
+ assert_eq ! ( * payment_hash, duplicate_payment_hash) ;
8578
+ }
8579
+ _ => panic ! ( "Unexpected event" ) ,
8580
+ }
8581
+
8582
+ // Solve 2nd HTLC by broadcasting on B's chain HTLC-Success Tx from C
8583
+ nodes[ 1 ] . chain_monitor . block_connected_with_filtering ( & Block { header, txdata : vec ! [ htlc_success_txn[ 0 ] . clone( ) ] } , 200 ) ;
8584
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
8585
+ let events = nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
8586
+ assert_eq ! ( events. len( ) , 1 ) ;
8587
+ match events[ 0 ] {
8588
+ MessageSendEvent :: UpdateHTLCs { ref node_id, updates : msgs:: CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, .. } } => {
8589
+ assert ! ( update_add_htlcs. is_empty( ) ) ;
8590
+ assert ! ( update_fail_htlcs. is_empty( ) ) ;
8591
+ assert_eq ! ( update_fulfill_htlcs. len( ) , 1 ) ;
8592
+ assert_eq ! ( update_fulfill_htlcs[ 0 ] . htlc_id, 0 ) ;
8593
+ assert ! ( update_fail_malformed_htlcs. is_empty( ) ) ;
8594
+ assert_eq ! ( nodes[ 0 ] . node. get_our_node_id( ) , * node_id) ;
8595
+ } ,
8596
+ _ => panic ! ( "Unexpected event" ) ,
8597
+ }
8598
+ }
8599
+
8481
8600
#[ test]
8482
8601
fn test_dynamic_spendable_outputs_local_htlc_success_tx ( ) {
8483
8602
let nodes = create_network ( 2 ) ;
0 commit comments