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