@@ -15,7 +15,7 @@ use crate::chain;
15
15
use crate::chain::{ChannelMonitorUpdateStatus, Confirm, Listen, Watch};
16
16
use crate::chain::chaininterface::LowerBoundedFeeEstimator;
17
17
use crate::chain::channelmonitor;
18
- use crate::chain::channelmonitor::{Balance, ChannelMonitorUpdateStep, CLTV_CLAIM_BUFFER, LATENCY_GRACE_PERIOD_BLOCKS, ANTI_REORG_DELAY};
18
+ use crate::chain::channelmonitor::{Balance, ChannelMonitorUpdateStep, CLTV_CLAIM_BUFFER, LATENCY_GRACE_PERIOD_BLOCKS, ANTI_REORG_DELAY, COUNTERPARTY_CLAIMABLE_WITHIN_BLOCKS_PINNABLE };
19
19
use crate::chain::transaction::OutPoint;
20
20
use crate::sign::{ecdsa::EcdsaChannelSigner, EntropySource, OutputSpender, SignerProvider};
21
21
use crate::events::bump_transaction::WalletSource;
@@ -2509,14 +2509,12 @@ fn test_justice_tx_htlc_timeout() {
2509
2509
mine_transaction(&nodes[1], &revoked_local_txn[0]);
2510
2510
{
2511
2511
let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
2512
- // The unpinnable, revoked to_self output, and the pinnable, revoked htlc output will
2513
- // be claimed in separate transactions.
2514
- assert_eq!(node_txn.len(), 2);
2515
- for tx in node_txn.iter() {
2516
- assert_eq!(tx.input.len(), 1);
2517
- check_spends!(tx, revoked_local_txn[0]);
2518
- }
2519
- assert_ne!(node_txn[0].input[0].previous_output, node_txn[1].input[0].previous_output);
2512
+ // The revoked HTLC output is not pinnable for another `TEST_FINAL_CLTV` blocks, and is
2513
+ // thus claimed in the same transaction with the revoked to_self output.
2514
+ assert_eq!(node_txn.len(), 1);
2515
+ assert_eq!(node_txn[0].input.len(), 2);
2516
+ check_spends!(node_txn[0], revoked_local_txn[0]);
2517
+ assert_ne!(node_txn[0].input[0].previous_output, node_txn[0].input[1].previous_output);
2520
2518
node_txn.clear();
2521
2519
}
2522
2520
check_added_monitors!(nodes[1], 1);
@@ -2736,28 +2734,26 @@ fn claim_htlc_outputs() {
2736
2734
assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
2737
2735
2738
2736
let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
2739
- assert_eq!(node_txn.len(), 2); // Two penalty transactions:
2740
- assert_eq!(node_txn[0].input.len(), 1); // Claims the unpinnable, revoked output.
2741
- assert_eq!(node_txn[1].input.len(), 2); // Claims both pinnable, revoked HTLC outputs separately.
2742
- check_spends!(node_txn[0], revoked_local_txn[0]);
2743
- check_spends!(node_txn[1], revoked_local_txn[0]);
2744
- assert_ne!(node_txn[0].input[0].previous_output, node_txn[1].input[0].previous_output);
2745
- assert_ne!(node_txn[0].input[0].previous_output, node_txn[1].input[1].previous_output);
2746
- assert_ne!(node_txn[1].input[0].previous_output, node_txn[1].input[1].previous_output);
2737
+ assert_eq!(node_txn.len(), 2); // ChannelMonitor: penalty txn
2738
+
2739
+ // The ChannelMonitor should claim the accepted HTLC output separately from the offered
2740
+ // HTLC and to_self outputs.
2741
+ let accepted_claim = node_txn.iter().filter(|tx| tx.input.len() == 1).next().unwrap();
2742
+ let offered_to_self_claim = node_txn.iter().filter(|tx| tx.input.len() == 2).next().unwrap();
2743
+ check_spends!(accepted_claim, revoked_local_txn[0]);
2744
+ check_spends!(offered_to_self_claim, revoked_local_txn[0]);
2745
+ assert_eq!(accepted_claim.input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
2747
2746
2748
2747
let mut witness_lens = BTreeSet::new();
2749
- witness_lens.insert(node_txn[0].input[0].witness.last().unwrap().len());
2750
- witness_lens.insert(node_txn[1].input[0].witness.last().unwrap().len());
2751
- witness_lens.insert(node_txn[1].input[1].witness.last().unwrap().len());
2752
- assert_eq!(witness_lens.len(), 3);
2748
+ witness_lens.insert(offered_to_self_claim.input[0].witness.last().unwrap().len());
2749
+ witness_lens.insert(offered_to_self_claim.input[1].witness.last().unwrap().len());
2750
+ assert_eq!(witness_lens.len(), 2);
2753
2751
assert_eq!(*witness_lens.iter().skip(0).next().unwrap(), 77); // revoked to_local
2754
- assert_eq!(*witness_lens.iter().skip(1).next().unwrap(), OFFERED_HTLC_SCRIPT_WEIGHT); // revoked offered HTLC
2755
- assert_eq!(*witness_lens.iter().skip(2).next().unwrap(), ACCEPTED_HTLC_SCRIPT_WEIGHT); // revoked received HTLC
2752
+ assert_eq!(*witness_lens.iter().skip(1).next().unwrap(), OFFERED_HTLC_SCRIPT_WEIGHT);
2756
2753
2757
- // Finally, mine the penalty transactions and check that we get an HTLC failure after
2754
+ // Finally, mine the penalty transaction and check that we get an HTLC failure after
2758
2755
// ANTI_REORG_DELAY confirmations.
2759
- mine_transaction(&nodes[1], &node_txn[0]);
2760
- mine_transaction(&nodes[1], &node_txn[1]);
2756
+ mine_transaction(&nodes[1], accepted_claim);
2761
2757
connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
2762
2758
expect_payment_failed!(nodes[1], payment_hash_2, false);
2763
2759
}
@@ -4920,8 +4916,7 @@ fn test_static_spendable_outputs_timeout_tx() {
4920
4916
check_spends!(spend_txn[2], node_txn[0], commitment_tx[0]); // All outputs
4921
4917
}
4922
4918
4923
- #[test]
4924
- fn test_static_spendable_outputs_justice_tx_revoked_commitment_tx() {
4919
+ fn do_test_static_spendable_outputs_justice_tx_revoked_commitment_tx(split_tx: bool) {
4925
4920
let chanmon_cfgs = create_chanmon_cfgs(2);
4926
4921
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
4927
4922
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
@@ -4937,20 +4932,28 @@ fn test_static_spendable_outputs_justice_tx_revoked_commitment_tx() {
4937
4932
4938
4933
claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage);
4939
4934
4935
+ if split_tx {
4936
+ connect_blocks(&nodes[1], TEST_FINAL_CLTV - COUNTERPARTY_CLAIMABLE_WITHIN_BLOCKS_PINNABLE + 1);
4937
+ }
4938
+
4940
4939
mine_transaction(&nodes[1], &revoked_local_txn[0]);
4941
4940
check_closed_broadcast!(nodes[1], true);
4942
4941
check_added_monitors!(nodes[1], 1);
4943
4942
check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 100000);
4944
4943
4945
- // The unpinnable, revoked to_self output and the pinnable, revoked HTLC output will be claimed
4946
- // in separate transactions.
4944
+ // If the HTLC expires in more than COUNTERPARTY_CLAIMABLE_WITHIN_BLOCKS_PINNABLE blocks, we'll
4945
+ // claim both the revoked and HTLC outputs in one transaction, otherwise we'll split them as we
4946
+ // consider the HTLC output as pinnable and want to claim pinnable and unpinnable outputs
4947
+ // separately.
4947
4948
let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
4948
- assert_eq!(node_txn.len(), 2 );
4949
+ assert_eq!(node_txn.len(), if split_tx { 2 } else { 1 } );
4949
4950
for tx in node_txn.iter() {
4950
- assert_eq!(tx.input.len(), 1 );
4951
+ assert_eq!(tx.input.len(), if split_tx { 1 } else { 2 } );
4951
4952
check_spends!(tx, revoked_local_txn[0]);
4952
4953
}
4953
- assert_ne!(node_txn[0].input[0].previous_output, node_txn[1].input[0].previous_output);
4954
+ if split_tx {
4955
+ assert_ne!(node_txn[0].input[0].previous_output, node_txn[1].input[0].previous_output);
4956
+ }
4954
4957
4955
4958
mine_transaction(&nodes[1], &node_txn[0]);
4956
4959
connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
@@ -4960,6 +4963,12 @@ fn test_static_spendable_outputs_justice_tx_revoked_commitment_tx() {
4960
4963
check_spends!(spend_txn[0], node_txn[0]);
4961
4964
}
4962
4965
4966
+ #[test]
4967
+ fn test_static_spendable_outputs_justice_tx_revoked_commitment_tx() {
4968
+ do_test_static_spendable_outputs_justice_tx_revoked_commitment_tx(true);
4969
+ do_test_static_spendable_outputs_justice_tx_revoked_commitment_tx(false);
4970
+ }
4971
+
4963
4972
#[test]
4964
4973
fn test_static_spendable_outputs_justice_tx_revoked_htlc_timeout_tx() {
4965
4974
let mut chanmon_cfgs = create_chanmon_cfgs(2);
@@ -4992,6 +5001,10 @@ fn test_static_spendable_outputs_justice_tx_revoked_htlc_timeout_tx() {
4992
5001
check_spends!(revoked_htlc_txn[0], revoked_local_txn[0]);
4993
5002
assert_ne!(revoked_htlc_txn[0].lock_time, LockTime::ZERO); // HTLC-Timeout
4994
5003
5004
+ // In order to connect `revoked_htlc_txn[0]` we must first advance the chain by
5005
+ // `TEST_FINAL_CLTV` blocks as otherwise the transaction is consensus-invalid due to its
5006
+ // locktime.
5007
+ connect_blocks(&nodes[1], TEST_FINAL_CLTV);
4995
5008
// B will generate justice tx from A's revoked commitment/HTLC tx
4996
5009
connect_block(&nodes[1], &create_dummy_block(nodes[1].best_block_hash(), 42, vec![revoked_local_txn[0].clone(), revoked_htlc_txn[0].clone()]));
4997
5010
check_closed_broadcast!(nodes[1], true);
0 commit comments