@@ -1245,6 +1245,73 @@ fn duplicate_htlc_test() {
1245
1245
claim_payment ( & nodes[ 1 ] , & vec ! ( & nodes[ 3 ] ) [ ..] , payment_preimage) ;
1246
1246
}
1247
1247
1248
+ #[ test]
1249
+ fn test_duplicate_htlc_different_direction_onchain ( ) {
1250
+ // Test that ChannelMonitor doesn't generate 2 preimage txn
1251
+ // when we have 2 HTLCs with same preimage that go across a node
1252
+ // in opposite directions.
1253
+ let nodes = create_network ( 2 , & [ None , None ] ) ;
1254
+
1255
+ let chan_1 = create_announced_chan_between_nodes ( & nodes, 0 , 1 , LocalFeatures :: new ( ) , LocalFeatures :: new ( ) ) ;
1256
+
1257
+ // balancing
1258
+ send_payment ( & nodes[ 0 ] , & vec ! ( & nodes[ 1 ] ) [ ..] , 8000000 ) ;
1259
+
1260
+ let ( payment_preimage, payment_hash) = route_payment ( & nodes[ 0 ] , & vec ! ( & nodes[ 1 ] ) [ ..] , 900_000 ) ;
1261
+
1262
+ let route = nodes[ 1 ] . router . get_route ( & nodes[ 0 ] . node . get_our_node_id ( ) , None , & Vec :: new ( ) , 800_000 , TEST_FINAL_CLTV ) . unwrap ( ) ;
1263
+ send_along_route_with_hash ( & nodes[ 1 ] , route, & vec ! ( & nodes[ 0 ] ) [ ..] , 800_000 , payment_hash) ;
1264
+
1265
+ // Provide preimage to node 0 by claiming payment
1266
+ nodes[ 0 ] . node . claim_funds ( payment_preimage) ;
1267
+ check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
1268
+
1269
+ // Broadcast node 1 commitment txn
1270
+ let remote_txn = nodes[ 1 ] . node . channel_state . lock ( ) . unwrap ( ) . by_id . get ( & chan_1. 2 ) . unwrap ( ) . last_local_commitment_txn . clone ( ) ;
1271
+
1272
+ assert_eq ! ( remote_txn[ 0 ] . output. len( ) , 4 ) ; // 1 local, 1 remote, 1 htlc inbound, 1 htlc outbound
1273
+ let mut has_both_htlcs = 0 ; // check htlcs match ones committed
1274
+ for outp in remote_txn[ 0 ] . output . iter ( ) {
1275
+ if outp. value == 800_000 / 1000 {
1276
+ has_both_htlcs += 1 ;
1277
+ } else if outp. value == 900_000 / 1000 {
1278
+ has_both_htlcs += 1 ;
1279
+ }
1280
+ }
1281
+ assert_eq ! ( has_both_htlcs, 2 ) ;
1282
+
1283
+ let header = BlockHeader { version : 0x2000_0000 , prev_blockhash : Default :: default ( ) , merkle_root : Default :: default ( ) , time : 42 , bits : 42 , nonce : 42 } ;
1284
+
1285
+ nodes[ 0 ] . chain_monitor . block_connected_with_filtering ( & Block { header, txdata : vec ! [ remote_txn[ 0 ] . clone( ) ] } , 1 ) ;
1286
+
1287
+ // Check we only broadcast 1 timeout tx
1288
+ let claim_txn = nodes[ 0 ] . tx_broadcaster . txn_broadcasted . lock ( ) . unwrap ( ) . clone ( ) ;
1289
+ let htlc_pair = if claim_txn[ 0 ] . output [ 0 ] . value == 800_000 / 1000 { ( claim_txn[ 0 ] . clone ( ) , claim_txn[ 1 ] . clone ( ) ) } else { ( claim_txn[ 1 ] . clone ( ) , claim_txn[ 0 ] . clone ( ) ) } ;
1290
+ assert_eq ! ( claim_txn. len( ) , 6 ) ;
1291
+ assert_eq ! ( htlc_pair. 0 . input. len( ) , 1 ) ;
1292
+ assert_eq ! ( htlc_pair. 0 . input[ 0 ] . witness. last( ) . unwrap( ) . len( ) , OFFERED_HTLC_SCRIPT_WEIGHT ) ; // HTLC 1 <--> 0, preimage tx
1293
+ check_spends ! ( htlc_pair. 0 , remote_txn[ 0 ] . clone( ) ) ;
1294
+ assert_eq ! ( htlc_pair. 1 . input. len( ) , 1 ) ;
1295
+ assert_eq ! ( htlc_pair. 1 . input[ 0 ] . witness. last( ) . unwrap( ) . len( ) , ACCEPTED_HTLC_SCRIPT_WEIGHT ) ; // HTLC 0 <--> 1, timeout tx
1296
+ check_spends ! ( htlc_pair. 1 , remote_txn[ 0 ] . clone( ) ) ;
1297
+
1298
+ let events = nodes[ 0 ] . node . get_and_clear_pending_msg_events ( ) ;
1299
+ assert_eq ! ( events. len( ) , 2 ) ;
1300
+ for e in events {
1301
+ match e {
1302
+ MessageSendEvent :: BroadcastChannelUpdate { .. } => { } ,
1303
+ 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, .. } } => {
1304
+ assert ! ( update_add_htlcs. is_empty( ) ) ;
1305
+ assert ! ( update_fail_htlcs. is_empty( ) ) ;
1306
+ assert_eq ! ( update_fulfill_htlcs. len( ) , 1 ) ;
1307
+ assert ! ( update_fail_malformed_htlcs. is_empty( ) ) ;
1308
+ assert_eq ! ( nodes[ 1 ] . node. get_our_node_id( ) , * node_id) ;
1309
+ } ,
1310
+ _ => panic ! ( "Unexpected event" ) ,
1311
+ }
1312
+ }
1313
+ }
1314
+
1248
1315
fn do_channel_reserve_test ( test_recv : bool ) {
1249
1316
use ln:: msgs:: HandleError ;
1250
1317
0 commit comments