@@ -2390,8 +2390,8 @@ fn channel_monitor_network_test() {
2390
2390
}
2391
2391
2392
2392
#[test]
2393
- fn test_justice_tx () {
2394
- // Test justice txn built on revoked HTLC-Success tx, against both sides
2393
+ fn test_justice_tx_htlc_timeout () {
2394
+ // Test justice txn built on revoked HTLC-Timeout tx, against both sides
2395
2395
let mut alice_config = UserConfig::default();
2396
2396
alice_config.channel_handshake_config.announced_channel = true;
2397
2397
alice_config.channel_handshake_limits.force_announced_channel_preference = false;
@@ -2407,7 +2407,6 @@ fn test_justice_tx() {
2407
2407
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
2408
2408
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &user_cfgs);
2409
2409
let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
2410
- *nodes[0].connect_style.borrow_mut() = ConnectStyle::FullBlockViaListen;
2411
2410
// Create some new channels:
2412
2411
let chan_5 = create_announced_chan_between_nodes(&nodes, 0, 1);
2413
2412
@@ -2431,7 +2430,6 @@ fn test_justice_tx() {
2431
2430
let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
2432
2431
assert_eq!(node_txn.len(), 1); // ChannelMonitor: penalty tx
2433
2432
assert_eq!(node_txn[0].input.len(), 2); // We should claim the revoked output and the HTLC output
2434
-
2435
2433
check_spends!(node_txn[0], revoked_local_txn[0]);
2436
2434
node_txn.swap_remove(0);
2437
2435
}
@@ -2450,17 +2448,30 @@ fn test_justice_tx() {
2450
2448
test_revoked_htlc_claim_txn_broadcast(&nodes[1], node_txn[1].clone(), revoked_local_txn[0].clone());
2451
2449
}
2452
2450
get_announce_close_broadcast_events(&nodes, 0, 1);
2453
-
2454
2451
assert_eq!(nodes[0].node.list_channels().len(), 0);
2455
2452
assert_eq!(nodes[1].node.list_channels().len(), 0);
2453
+ }
2456
2454
2457
- // We test justice_tx build by A on B's revoked HTLC-Success tx
2455
+ #[test]
2456
+ fn test_justice_tx_htlc_success() {
2457
+ // Test justice txn built on revoked HTLC-Success tx, against both sides
2458
+ let mut alice_config = UserConfig::default();
2459
+ alice_config.channel_handshake_config.announced_channel = true;
2460
+ alice_config.channel_handshake_limits.force_announced_channel_preference = false;
2461
+ alice_config.channel_handshake_config.our_to_self_delay = 6 * 24 * 5;
2462
+ let mut bob_config = UserConfig::default();
2463
+ bob_config.channel_handshake_config.announced_channel = true;
2464
+ bob_config.channel_handshake_limits.force_announced_channel_preference = false;
2465
+ bob_config.channel_handshake_config.our_to_self_delay = 6 * 24 * 3;
2466
+ let user_cfgs = [Some(alice_config), Some(bob_config)];
2467
+ let mut chanmon_cfgs = create_chanmon_cfgs(2);
2468
+ chanmon_cfgs[0].keys_manager.disable_revocation_policy_check = true;
2469
+ chanmon_cfgs[1].keys_manager.disable_revocation_policy_check = true;
2470
+ let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
2471
+ let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &user_cfgs);
2472
+ let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
2458
2473
// Create some new channels:
2459
2474
let chan_6 = create_announced_chan_between_nodes(&nodes, 0, 1);
2460
- {
2461
- let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
2462
- node_txn.clear();
2463
- }
2464
2475
2465
2476
// A pending HTLC which will be revoked:
2466
2477
let payment_preimage_4 = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
@@ -2638,8 +2649,7 @@ fn claim_htlc_outputs_single_tx() {
2638
2649
connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
2639
2650
assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
2640
2651
2641
- let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
2642
- assert_eq!(node_txn.len(), 7);
2652
+ let mut node_txn = nodes[1].tx_broadcaster.txn_broadcast();
2643
2653
2644
2654
// Check the pair local commitment and HTLC-timeout broadcast due to HTLC expiration
2645
2655
assert_eq!(node_txn[0].input.len(), 1);
@@ -2649,29 +2659,32 @@ fn claim_htlc_outputs_single_tx() {
2649
2659
assert_eq!(witness_script.len(), OFFERED_HTLC_SCRIPT_WEIGHT); //Spending an offered htlc output
2650
2660
check_spends!(node_txn[1], node_txn[0]);
2651
2661
2652
- // Justice transactions are indices 2-3-4
2662
+ // Filter out any non justice transactions.
2663
+ node_txn.retain(|tx| tx.input[0].previous_output.txid == revoked_local_txn[0].txid());
2664
+ assert!(node_txn.len() > 3);
2665
+
2666
+ assert_eq!(node_txn[0].input.len(), 1);
2667
+ assert_eq!(node_txn[1].input.len(), 1);
2653
2668
assert_eq!(node_txn[2].input.len(), 1);
2654
- assert_eq!(node_txn[3].input.len(), 1);
2655
- assert_eq!(node_txn[4].input.len(), 1);
2656
2669
2670
+ check_spends!(node_txn[0], revoked_local_txn[0]);
2671
+ check_spends!(node_txn[1], revoked_local_txn[0]);
2657
2672
check_spends!(node_txn[2], revoked_local_txn[0]);
2658
- check_spends!(node_txn[3], revoked_local_txn[0]);
2659
- check_spends!(node_txn[4], revoked_local_txn[0]);
2660
2673
2661
2674
let mut witness_lens = BTreeSet::new();
2675
+ witness_lens.insert(node_txn[0].input[0].witness.last().unwrap().len());
2676
+ witness_lens.insert(node_txn[1].input[0].witness.last().unwrap().len());
2662
2677
witness_lens.insert(node_txn[2].input[0].witness.last().unwrap().len());
2663
- witness_lens.insert(node_txn[3].input[0].witness.last().unwrap().len());
2664
- witness_lens.insert(node_txn[4].input[0].witness.last().unwrap().len());
2665
2678
assert_eq!(witness_lens.len(), 3);
2666
2679
assert_eq!(*witness_lens.iter().skip(0).next().unwrap(), 77); // revoked to_local
2667
2680
assert_eq!(*witness_lens.iter().skip(1).next().unwrap(), OFFERED_HTLC_SCRIPT_WEIGHT); // revoked offered HTLC
2668
2681
assert_eq!(*witness_lens.iter().skip(2).next().unwrap(), ACCEPTED_HTLC_SCRIPT_WEIGHT); // revoked received HTLC
2669
2682
2670
2683
// Finally, mine the penalty transactions and check that we get an HTLC failure after
2671
2684
// ANTI_REORG_DELAY confirmations.
2685
+ mine_transaction(&nodes[1], &node_txn[0]);
2686
+ mine_transaction(&nodes[1], &node_txn[1]);
2672
2687
mine_transaction(&nodes[1], &node_txn[2]);
2673
- mine_transaction(&nodes[1], &node_txn[3]);
2674
- mine_transaction(&nodes[1], &node_txn[4]);
2675
2688
connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
2676
2689
expect_payment_failed!(nodes[1], payment_hash_2, false);
2677
2690
}
@@ -2970,25 +2983,20 @@ fn do_test_htlc_on_chain_timeout(connect_style: ConnectStyle) {
2970
2983
2971
2984
// Broadcast timeout transaction by B on received output from C's commitment tx on B's chain
2972
2985
// Verify that B's ChannelManager is able to detect that HTLC is timeout by its own tx and react backward in consequence
2973
- connect_blocks(&nodes[1], 200 - nodes[2].best_block_info().1);
2974
2986
mine_transaction(&nodes[1], &commitment_tx[0]);
2975
- check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
2976
- let timeout_tx;
2977
- {
2978
- let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
2979
- assert_eq!(node_txn.len(), 3); // 2 (local commitment tx + HTLC-timeout), 1 timeout tx
2980
-
2981
- check_spends!(node_txn[2], commitment_tx[0]);
2982
- assert_eq!(node_txn[2].clone().input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
2983
-
2984
- check_spends!(node_txn[0], chan_2.3);
2985
- check_spends!(node_txn[1], node_txn[0]);
2986
- assert_eq!(node_txn[0].clone().input[0].witness.last().unwrap().len(), 71);
2987
- assert_eq!(node_txn[1].clone().input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
2988
-
2989
- timeout_tx = node_txn[2].clone();
2990
- node_txn.clear();
2991
- }
2987
+ check_closed_event(&nodes[1], 1, ClosureReason::CommitmentTxConfirmed, false);
2988
+ connect_blocks(&nodes[1], 200 - nodes[2].best_block_info().1);
2989
+ let timeout_tx = {
2990
+ let mut txn = nodes[1].tx_broadcaster.txn_broadcast();
2991
+ if nodes[1].connect_style.borrow().skips_blocks() {
2992
+ assert_eq!(txn.len(), 1);
2993
+ } else {
2994
+ assert_eq!(txn.len(), 2); // Extra rebroadcast of timeout transaction
2995
+ }
2996
+ check_spends!(txn[0], commitment_tx[0]);
2997
+ assert_eq!(txn[0].clone().input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
2998
+ txn.remove(0)
2999
+ };
2992
3000
2993
3001
mine_transaction(&nodes[1], &timeout_tx);
2994
3002
check_added_monitors!(nodes[1], 1);
@@ -7312,17 +7320,21 @@ fn test_bump_penalty_txn_on_revoked_htlcs() {
7312
7320
check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
7313
7321
connect_blocks(&nodes[1], 49); // Confirm blocks until the HTLC expires (note CLTV was explicitly 50 above)
7314
7322
7315
- let revoked_htlc_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
7316
- assert_eq!(revoked_htlc_txn.len(), 2);
7323
+ let revoked_htlc_txn = {
7324
+ let txn = nodes[1].tx_broadcaster.unique_txn_broadcast();
7325
+ assert_eq!(txn.len(), 2);
7317
7326
7318
- assert_eq!(revoked_htlc_txn[0].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
7319
- assert_eq!(revoked_htlc_txn[0].input.len(), 1);
7320
- check_spends!(revoked_htlc_txn[0], revoked_local_txn[0]);
7327
+ assert_eq!(txn[0].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
7328
+ assert_eq!(txn[0].input.len(), 1);
7329
+ check_spends!(txn[0], revoked_local_txn[0]);
7330
+
7331
+ assert_eq!(txn[1].input.len(), 1);
7332
+ assert_eq!(txn[1].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
7333
+ assert_eq!(txn[1].output.len(), 1);
7334
+ check_spends!(txn[1], revoked_local_txn[0]);
7321
7335
7322
- assert_eq!(revoked_htlc_txn[1].input.len(), 1);
7323
- assert_eq!(revoked_htlc_txn[1].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
7324
- assert_eq!(revoked_htlc_txn[1].output.len(), 1);
7325
- check_spends!(revoked_htlc_txn[1], revoked_local_txn[0]);
7336
+ txn
7337
+ };
7326
7338
7327
7339
// Broadcast set of revoked txn on A
7328
7340
let hash_128 = connect_blocks(&nodes[0], 40);
@@ -8494,11 +8506,11 @@ fn test_concurrent_monitor_claim() {
8494
8506
watchtower_alice.chain_monitor.block_connected(&block, CHAN_CONFIRM_DEPTH + 1 + TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS);
8495
8507
8496
8508
// Watchtower Alice should have broadcast a commitment/HTLC-timeout
8497
- {
8498
- let mut txn = chanmon_cfgs[0].tx_broadcaster.txn_broadcasted.lock().unwrap ();
8509
+ let alice_state = {
8510
+ let mut txn = chanmon_cfgs[0].tx_broadcaster.txn_broadcast ();
8499
8511
assert_eq!(txn.len(), 2);
8500
- txn.clear();
8501
- }
8512
+ txn.remove(0)
8513
+ };
8502
8514
8503
8515
// Copy ChainMonitor to simulate watchtower Bob and make it receive a commitment update first.
8504
8516
let chain_source = test_utils::TestChainSource::new(Network::Testnet);
@@ -8549,19 +8561,21 @@ fn test_concurrent_monitor_claim() {
8549
8561
// Watchtower Bob should have broadcast a commitment/HTLC-timeout
8550
8562
let bob_state_y;
8551
8563
{
8552
- let mut txn = chanmon_cfgs[0].tx_broadcaster.txn_broadcasted.lock().unwrap ();
8564
+ let mut txn = chanmon_cfgs[0].tx_broadcaster.txn_broadcast ();
8553
8565
assert_eq!(txn.len(), 2);
8554
- bob_state_y = txn[0].clone();
8555
- txn.clear();
8566
+ bob_state_y = txn.remove(0);
8556
8567
};
8557
8568
8558
8569
// We confirm Bob's state Y on Alice, she should broadcast a HTLC-timeout
8559
8570
let header = BlockHeader { version: 0x20000000, prev_blockhash: BlockHash::all_zeros(), merkle_root: TxMerkleNode::all_zeros(), time: 42, bits: 42, nonce: 42 };
8560
8571
watchtower_alice.chain_monitor.block_connected(&Block { header, txdata: vec![bob_state_y.clone()] }, CHAN_CONFIRM_DEPTH + 2 + TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS);
8561
8572
{
8562
- let htlc_txn = chanmon_cfgs[0].tx_broadcaster.txn_broadcasted.lock().unwrap ();
8563
- assert_eq!(htlc_txn.len(), 1 );
8573
+ let htlc_txn = chanmon_cfgs[0].tx_broadcaster.txn_broadcast ();
8574
+ assert_eq!(htlc_txn.len(), 2 );
8564
8575
check_spends!(htlc_txn[0], bob_state_y);
8576
+ // Alice doesn't clean up the old HTLC claim since it hasn't seen a conflicting spend for
8577
+ // it. However, she should, because it now has an invalid parent.
8578
+ check_spends!(htlc_txn[1], alice_state);
8565
8579
}
8566
8580
}
8567
8581
0 commit comments