@@ -3927,6 +3927,18 @@ 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
+ match events[ 0 ] {
3936
+ MessageSendEvent :: PaymentFailureNetworkUpdate { update: msgs:: HTLCFailChannelUpdate :: NodeFailure { .. } } => {
3937
+ //TODO: check when fail_htlc_backward_internal after fecth_pending_htlcs have consistent error code
3938
+ }
3939
+ _ => { panic!( "Unexpected event" ) ; }
3940
+ }
3941
+ }
3930
3942
}
3931
3943
}
3932
3944
@@ -8422,6 +8434,112 @@ mod tests {
8422
8434
check_spends ! ( spend_txn[ 2 ] , node_txn[ 0 ] . clone( ) ) ;
8423
8435
}
8424
8436
8437
+ #[ test]
8438
+ fn test_duplicate_payment_hash_one_failure_one_success ( ) {
8439
+ // Topology : A --> B --> C
8440
+ // We route 2 payments with same hash between B and C, one will be timeout, the other successfully claim
8441
+ let mut nodes = create_network ( 3 ) ;
8442
+
8443
+ create_announced_chan_between_nodes ( & nodes, 0 , 1 ) ;
8444
+ let chan_2 = create_announced_chan_between_nodes ( & nodes, 1 , 2 ) ;
8445
+
8446
+ let ( payment_preimage, duplicate_payment_hash) = route_payment ( & nodes[ 0 ] , & vec ! ( & nodes[ 1 ] , & nodes[ 2 ] ) [ ..] , 900000 ) ;
8447
+ * nodes[ 0 ] . network_payment_count . borrow_mut ( ) -= 1 ;
8448
+ route_payment ( & nodes[ 0 ] , & vec ! ( & nodes[ 1 ] , & nodes[ 2 ] ) [ ..] , 900000 ) ;
8449
+
8450
+ let commitment_txn = nodes[ 2 ] . node . channel_state . lock ( ) . unwrap ( ) . by_id . get ( & chan_2. 2 ) . unwrap ( ) . last_local_commitment_txn . clone ( ) ;
8451
+ assert_eq ! ( commitment_txn[ 0 ] . input. len( ) , 1 ) ;
8452
+ check_spends ! ( commitment_txn[ 0 ] , chan_2. 3 . clone( ) ) ;
8453
+
8454
+ let header = BlockHeader { version : 0x20000000 , prev_blockhash : Default :: default ( ) , merkle_root : Default :: default ( ) , time : 42 , bits : 42 , nonce : 42 } ;
8455
+ nodes[ 1 ] . chain_monitor . block_connected_with_filtering ( & Block { header, txdata : vec ! [ commitment_txn[ 0 ] . clone( ) ] } , 1 ) ;
8456
+ let htlc_timeout_tx;
8457
+ {
8458
+ let node_txn = nodes[ 1 ] . tx_broadcaster . txn_broadcasted . lock ( ) . unwrap ( ) ;
8459
+ assert_eq ! ( node_txn[ 0 ] , node_txn[ 5 ] ) ;
8460
+ assert_eq ! ( node_txn[ 1 ] , node_txn[ 6 ] ) ;
8461
+ check_spends ! ( node_txn[ 0 ] , commitment_txn[ 0 ] . clone( ) ) ;
8462
+ check_spends ! ( node_txn[ 1 ] , commitment_txn[ 0 ] . clone( ) ) ;
8463
+ check_spends ! ( node_txn[ 2 ] , chan_2. 3 . clone( ) ) ;
8464
+ check_spends ! ( node_txn[ 3 ] , node_txn[ 2 ] . clone( ) ) ;
8465
+ check_spends ! ( node_txn[ 4 ] , node_txn[ 2 ] . clone( ) ) ;
8466
+ htlc_timeout_tx = node_txn[ 1 ] . clone ( ) ;
8467
+ }
8468
+
8469
+ let events = nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
8470
+ match events[ 0 ] {
8471
+ MessageSendEvent :: BroadcastChannelUpdate { .. } => { } ,
8472
+ _ => panic ! ( "Unexepected event" ) ,
8473
+ }
8474
+
8475
+ nodes[ 2 ] . node . claim_funds ( payment_preimage) ;
8476
+ nodes[ 2 ] . chain_monitor . block_connected_with_filtering ( & Block { header, txdata : vec ! [ commitment_txn[ 0 ] . clone( ) ] } , 1 ) ;
8477
+ check_added_monitors ! ( nodes[ 2 ] , 2 ) ;
8478
+ let events = nodes[ 2 ] . node . get_and_clear_pending_msg_events ( ) ;
8479
+ match events[ 0 ] {
8480
+ MessageSendEvent :: UpdateHTLCs { .. } => { } ,
8481
+ _ => panic ! ( "Unexpected event" ) ,
8482
+ }
8483
+ match events[ 1 ] {
8484
+ MessageSendEvent :: BroadcastChannelUpdate { .. } => { } ,
8485
+ _ => panic ! ( "Unexepected event" ) ,
8486
+ }
8487
+ let htlc_success_txn = nodes[ 2 ] . tx_broadcaster . txn_broadcasted . lock ( ) . unwrap ( ) ;
8488
+ check_spends ! ( htlc_success_txn[ 2 ] , chan_2. 3 . clone( ) ) ;
8489
+ assert_eq ! ( htlc_success_txn[ 0 ] , htlc_success_txn[ 3 ] ) ;
8490
+ assert_eq ! ( htlc_success_txn[ 0 ] . input. len( ) , 1 ) ;
8491
+ assert_eq ! ( htlc_success_txn[ 0 ] . input[ 0 ] . witness. last( ) . unwrap( ) . len( ) , ACCEPTED_HTLC_SCRIPT_WEIGHT ) ;
8492
+ assert_eq ! ( htlc_success_txn[ 1 ] , htlc_success_txn[ 4 ] ) ;
8493
+ assert_eq ! ( htlc_success_txn[ 1 ] . input. len( ) , 1 ) ;
8494
+ assert_eq ! ( htlc_success_txn[ 1 ] . input[ 0 ] . witness. last( ) . unwrap( ) . len( ) , ACCEPTED_HTLC_SCRIPT_WEIGHT ) ;
8495
+ check_spends ! ( htlc_success_txn[ 0 ] , commitment_txn[ 0 ] . clone( ) ) ;
8496
+ check_spends ! ( htlc_success_txn[ 1 ] , commitment_txn[ 0 ] . clone( ) ) ;
8497
+
8498
+ nodes[ 1 ] . chain_monitor . block_connected_with_filtering ( & Block { header, txdata : vec ! [ htlc_timeout_tx] } , 200 ) ;
8499
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
8500
+ let msg_events = nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
8501
+ let htlc_updates = match msg_events[ 0 ] {
8502
+ MessageSendEvent :: UpdateHTLCs { ref node_id, ref updates } => {
8503
+ assert ! ( updates. update_add_htlcs. is_empty( ) ) ;
8504
+ assert_eq ! ( updates. update_fail_htlcs. len( ) , 1 ) ;
8505
+ assert_eq ! ( updates. update_fail_htlcs[ 0 ] . htlc_id, 1 ) ;
8506
+ assert ! ( updates. update_fulfill_htlcs. is_empty( ) ) ;
8507
+ assert ! ( updates. update_fail_malformed_htlcs. is_empty( ) ) ;
8508
+ assert_eq ! ( nodes[ 0 ] . node. get_our_node_id( ) , * node_id) ;
8509
+ ( * updates) . clone ( )
8510
+ } ,
8511
+ _ => panic ! ( "Unexpected event" ) ,
8512
+ } ;
8513
+ nodes[ 0 ] . node . handle_update_fail_htlc ( & nodes[ 1 ] . node . get_our_node_id ( ) , & htlc_updates. update_fail_htlcs [ 0 ] ) . unwrap ( ) ;
8514
+ let events = nodes[ 0 ] . node . get_and_clear_pending_msg_events ( ) ;
8515
+ assert_eq ! ( events. len( ) , 0 ) ;
8516
+ commitment_signed_dance ! ( nodes[ 0 ] , nodes[ 1 ] , & htlc_updates. commitment_signed, false , false , nodes[ 2 ] . node. get_our_node_id( ) ) ;
8517
+ let events = nodes[ 0 ] . node . get_and_clear_pending_events ( ) ;
8518
+ match events[ 0 ] {
8519
+ Event :: PaymentFailed { ref payment_hash, .. } => {
8520
+ assert_eq ! ( * payment_hash, duplicate_payment_hash) ;
8521
+ }
8522
+ _ => panic ! ( "Unexpected event" ) ,
8523
+ }
8524
+
8525
+ // Solve 2nd HTLC by broadcasting on B's chain HTLC-Success Tx from C
8526
+ nodes[ 1 ] . chain_monitor . block_connected_with_filtering ( & Block { header, txdata : vec ! [ htlc_success_txn[ 0 ] . clone( ) ] } , 200 ) ;
8527
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
8528
+ let events = nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
8529
+ assert_eq ! ( events. len( ) , 1 ) ;
8530
+ match events[ 0 ] {
8531
+ 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, .. } } => {
8532
+ assert ! ( update_add_htlcs. is_empty( ) ) ;
8533
+ assert ! ( update_fail_htlcs. is_empty( ) ) ;
8534
+ assert_eq ! ( update_fulfill_htlcs. len( ) , 1 ) ;
8535
+ assert_eq ! ( update_fulfill_htlcs[ 0 ] . htlc_id, 0 ) ;
8536
+ assert ! ( update_fail_malformed_htlcs. is_empty( ) ) ;
8537
+ assert_eq ! ( nodes[ 0 ] . node. get_our_node_id( ) , * node_id) ;
8538
+ } ,
8539
+ _ => panic ! ( "Unexpected event" ) ,
8540
+ }
8541
+ }
8542
+
8425
8543
#[ test]
8426
8544
fn test_dynamic_spendable_outputs_local_htlc_success_tx ( ) {
8427
8545
let nodes = create_network ( 2 ) ;
0 commit comments