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