Skip to content

Commit 8f85f2a

Browse files
committed
Add anchors coverage to test_balances_on_local_commitment_htlcs
1 parent 19522ea commit 8f85f2a

File tree

1 file changed

+67
-27
lines changed

1 file changed

+67
-27
lines changed

lightning/src/ln/monitor_tests.rs

Lines changed: 67 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -625,8 +625,7 @@ fn test_claim_value_force_close() {
625625
do_test_claim_value_force_close(true, false);
626626
}
627627

628-
#[test]
629-
fn test_balances_on_local_commitment_htlcs() {
628+
fn do_test_balances_on_local_commitment_htlcs(anchors: bool) {
630629
// Previously, when handling the broadcast of a local commitment transactions (with associated
631630
// CSV delays prior to spendability), we incorrectly handled the CSV delays on HTLC
632631
// transactions. This caused us to miss spendable outputs for HTLCs which were awaiting a CSV
@@ -638,9 +637,34 @@ fn test_balances_on_local_commitment_htlcs() {
638637
// claim by our counterparty).
639638
let chanmon_cfgs = create_chanmon_cfgs(2);
640639
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
641-
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
640+
let mut user_config = test_default_channel_config();
641+
if anchors {
642+
user_config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
643+
user_config.manually_accept_inbound_channels = true;
644+
}
645+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(user_config), Some(user_config)]);
642646
let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
643647

648+
let coinbase_tx = Transaction {
649+
version: 2,
650+
lock_time: PackedLockTime::ZERO,
651+
input: vec![TxIn { ..Default::default() }],
652+
output: vec![
653+
TxOut {
654+
value: Amount::ONE_BTC.to_sat(),
655+
script_pubkey: nodes[0].wallet_source.get_change_script().unwrap(),
656+
},
657+
TxOut {
658+
value: Amount::ONE_BTC.to_sat(),
659+
script_pubkey: nodes[1].wallet_source.get_change_script().unwrap(),
660+
},
661+
],
662+
};
663+
if anchors {
664+
nodes[0].wallet_source.add_utxo(bitcoin::OutPoint { txid: coinbase_tx.txid(), vout: 0 }, coinbase_tx.output[0].value);
665+
nodes[1].wallet_source.add_utxo(bitcoin::OutPoint { txid: coinbase_tx.txid(), vout: 1 }, coinbase_tx.output[1].value);
666+
}
667+
644668
// Create a single channel with two pending HTLCs from nodes[0] to nodes[1], one which nodes[1]
645669
// knows the preimage for, one which it does not.
646670
let (_, _, chan_id, funding_tx) = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
@@ -678,20 +702,26 @@ fn test_balances_on_local_commitment_htlcs() {
678702
let chan_feerate = get_feerate!(nodes[0], nodes[1], chan_id) as u64;
679703
let channel_type_features = get_channel_type_features!(nodes[0], nodes[1], chan_id);
680704

681-
// Get nodes[0]'s commitment transaction and HTLC-Timeout transactions
682-
let as_txn = get_local_commitment_txn!(nodes[0], chan_id);
683-
assert_eq!(as_txn.len(), 3);
684-
check_spends!(as_txn[1], as_txn[0]);
685-
check_spends!(as_txn[2], as_txn[0]);
686-
check_spends!(as_txn[0], funding_tx);
687-
688705
// First confirm the commitment transaction on nodes[0], which should leave us with three
689706
// claimable balances.
690707
let node_a_commitment_claimable = nodes[0].best_block_info().1 + BREAKDOWN_TIMEOUT as u32;
691-
mine_transaction(&nodes[0], &as_txn[0]);
708+
nodes[0].node.force_close_broadcasting_latest_txn(&chan_id, &nodes[1].node.get_our_node_id()).unwrap();
692709
check_added_monitors!(nodes[0], 1);
693710
check_closed_broadcast!(nodes[0], true);
694-
check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 1000000);
711+
check_closed_event!(nodes[0], 1, ClosureReason::HolderForceClosed, [nodes[1].node.get_our_node_id()], 1000000);
712+
let commitment_tx = {
713+
let mut txn = nodes[0].tx_broadcaster.unique_txn_broadcast();
714+
assert_eq!(txn.len(), 1);
715+
let commitment_tx = txn.pop().unwrap();
716+
check_spends!(commitment_tx, funding_tx);
717+
commitment_tx
718+
};
719+
mine_transaction(&nodes[0], &commitment_tx);
720+
if anchors && nodes[0].connect_style.borrow().updates_best_block_first() {
721+
let mut txn = nodes[0].tx_broadcaster.txn_broadcast();
722+
assert_eq!(txn.len(), 1);
723+
assert_eq!(txn[0].txid(), commitment_tx.txid());
724+
}
695725

696726
let htlc_balance_known_preimage = Balance::MaybeTimeoutClaimableHTLC {
697727
amount_satoshis: 10_000,
@@ -704,43 +734,49 @@ fn test_balances_on_local_commitment_htlcs() {
704734
payment_hash: payment_hash_2,
705735
};
706736

737+
let commitment_tx_fee = chan_feerate *
738+
(channel::commitment_tx_base_weight(&channel_type_features) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000;
739+
let anchor_outputs_value = if anchors { 2 * channel::ANCHOR_OUTPUT_VALUE_SATOSHI } else { 0 };
707740
assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
708-
amount_satoshis: 1_000_000 - 10_000 - 20_000 - chan_feerate *
709-
(channel::commitment_tx_base_weight(&channel_type_features) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
741+
amount_satoshis: 1_000_000 - 10_000 - 20_000 - commitment_tx_fee - anchor_outputs_value,
710742
confirmation_height: node_a_commitment_claimable,
711743
}, htlc_balance_known_preimage.clone(), htlc_balance_unknown_preimage.clone()]),
712744
sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
713745

714746
// Get nodes[1]'s HTLC claim tx for the second HTLC
715-
mine_transaction(&nodes[1], &as_txn[0]);
747+
mine_transaction(&nodes[1], &commitment_tx);
716748
check_added_monitors!(nodes[1], 1);
717749
check_closed_broadcast!(nodes[1], true);
718750
check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 1000000);
719751
let bs_htlc_claim_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
720752
assert_eq!(bs_htlc_claim_txn.len(), 1);
721-
check_spends!(bs_htlc_claim_txn[0], as_txn[0]);
753+
check_spends!(bs_htlc_claim_txn[0], commitment_tx);
722754

723755
// Connect blocks until the HTLCs expire, allowing us to (validly) broadcast the HTLC-Timeout
724756
// transaction.
725-
connect_blocks(&nodes[0], TEST_FINAL_CLTV - 1);
757+
connect_blocks(&nodes[0], TEST_FINAL_CLTV);
726758
assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
727-
amount_satoshis: 1_000_000 - 10_000 - 20_000 - chan_feerate *
728-
(channel::commitment_tx_base_weight(&channel_type_features) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
759+
amount_satoshis: 1_000_000 - 10_000 - 20_000 - commitment_tx_fee - anchor_outputs_value,
729760
confirmation_height: node_a_commitment_claimable,
730761
}, htlc_balance_known_preimage.clone(), htlc_balance_unknown_preimage.clone()]),
731762
sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
732-
assert_eq!(as_txn[1].lock_time.0, nodes[0].best_block_info().1 + 1); // as_txn[1] can be included in the next block
763+
if anchors {
764+
handle_bump_htlc_event(&nodes[0], 2);
765+
}
766+
let timeout_htlc_txn = nodes[0].tx_broadcaster.unique_txn_broadcast();
767+
assert_eq!(timeout_htlc_txn.len(), 2);
768+
check_spends!(timeout_htlc_txn[0], commitment_tx, coinbase_tx);
769+
check_spends!(timeout_htlc_txn[1], commitment_tx, coinbase_tx);
733770

734771
// Now confirm nodes[0]'s HTLC-Timeout transaction, which changes the claimable balance to an
735772
// "awaiting confirmations" one.
736773
let node_a_htlc_claimable = nodes[0].best_block_info().1 + BREAKDOWN_TIMEOUT as u32;
737-
mine_transaction(&nodes[0], &as_txn[1]);
774+
mine_transaction(&nodes[0], &timeout_htlc_txn[0]);
738775
// Note that prior to the fix in the commit which introduced this test, this (and the next
739776
// balance) check failed. With this check removed, the code panicked in the `connect_blocks`
740777
// call, as described, two hunks down.
741778
assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
742-
amount_satoshis: 1_000_000 - 10_000 - 20_000 - chan_feerate *
743-
(channel::commitment_tx_base_weight(&channel_type_features) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
779+
amount_satoshis: 1_000_000 - 10_000 - 20_000 - commitment_tx_fee - anchor_outputs_value,
744780
confirmation_height: node_a_commitment_claimable,
745781
}, Balance::ClaimableAwaitingConfirmations {
746782
amount_satoshis: 10_000,
@@ -753,8 +789,7 @@ fn test_balances_on_local_commitment_htlcs() {
753789
mine_transaction(&nodes[0], &bs_htlc_claim_txn[0]);
754790
expect_payment_sent(&nodes[0], payment_preimage_2, None, true, false);
755791
assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
756-
amount_satoshis: 1_000_000 - 10_000 - 20_000 - chan_feerate *
757-
(channel::commitment_tx_base_weight(&channel_type_features) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
792+
amount_satoshis: 1_000_000 - 10_000 - 20_000 - commitment_tx_fee - anchor_outputs_value,
758793
confirmation_height: node_a_commitment_claimable,
759794
}, Balance::ClaimableAwaitingConfirmations {
760795
amount_satoshis: 10_000,
@@ -769,8 +804,7 @@ fn test_balances_on_local_commitment_htlcs() {
769804
expect_payment_failed!(nodes[0], payment_hash, false);
770805

771806
assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
772-
amount_satoshis: 1_000_000 - 10_000 - 20_000 - chan_feerate *
773-
(channel::commitment_tx_base_weight(&channel_type_features) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
807+
amount_satoshis: 1_000_000 - 10_000 - 20_000 - commitment_tx_fee - anchor_outputs_value,
774808
confirmation_height: node_a_commitment_claimable,
775809
}, Balance::ClaimableAwaitingConfirmations {
776810
amount_satoshis: 10_000,
@@ -803,6 +837,12 @@ fn test_balances_on_local_commitment_htlcs() {
803837
assert!(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
804838
}
805839

840+
#[test]
841+
fn test_balances_on_local_commitment_htlcs() {
842+
do_test_balances_on_local_commitment_htlcs(false);
843+
do_test_balances_on_local_commitment_htlcs(true);
844+
}
845+
806846
#[test]
807847
fn test_no_preimage_inbound_htlc_balances() {
808848
// Tests that MaybePreimageClaimableHTLC are generated for inbound HTLCs for which we do not

0 commit comments

Comments
 (0)