@@ -625,8 +625,7 @@ fn test_claim_value_force_close() {
625
625
do_test_claim_value_force_close ( true , false ) ;
626
626
}
627
627
628
- #[ test]
629
- fn test_balances_on_local_commitment_htlcs ( ) {
628
+ fn do_test_balances_on_local_commitment_htlcs ( anchors : bool ) {
630
629
// Previously, when handling the broadcast of a local commitment transactions (with associated
631
630
// CSV delays prior to spendability), we incorrectly handled the CSV delays on HTLC
632
631
// 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() {
638
637
// claim by our counterparty).
639
638
let chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
640
639
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) ] ) ;
642
646
let mut nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
643
647
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
+
644
668
// Create a single channel with two pending HTLCs from nodes[0] to nodes[1], one which nodes[1]
645
669
// knows the preimage for, one which it does not.
646
670
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() {
678
702
let chan_feerate = get_feerate ! ( nodes[ 0 ] , nodes[ 1 ] , chan_id) as u64 ;
679
703
let channel_type_features = get_channel_type_features ! ( nodes[ 0 ] , nodes[ 1 ] , chan_id) ;
680
704
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
-
688
705
// First confirm the commitment transaction on nodes[0], which should leave us with three
689
706
// claimable balances.
690
707
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 ( ) ;
692
709
check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
693
710
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
+ }
695
725
696
726
let htlc_balance_known_preimage = Balance :: MaybeTimeoutClaimableHTLC {
697
727
amount_satoshis : 10_000 ,
@@ -704,43 +734,49 @@ fn test_balances_on_local_commitment_htlcs() {
704
734
payment_hash : payment_hash_2,
705
735
} ;
706
736
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 } ;
707
740
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,
710
742
confirmation_height: node_a_commitment_claimable,
711
743
} , htlc_balance_known_preimage. clone( ) , htlc_balance_unknown_preimage. clone( ) ] ) ,
712
744
sorted_vec( nodes[ 0 ] . chain_monitor. chain_monitor. get_monitor( funding_outpoint) . unwrap( ) . get_claimable_balances( ) ) ) ;
713
745
714
746
// 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 ) ;
716
748
check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
717
749
check_closed_broadcast ! ( nodes[ 1 ] , true ) ;
718
750
check_closed_event ! ( nodes[ 1 ] , 1 , ClosureReason :: CommitmentTxConfirmed , [ nodes[ 0 ] . node. get_our_node_id( ) ] , 1000000 ) ;
719
751
let bs_htlc_claim_txn = nodes[ 1 ] . tx_broadcaster . txn_broadcasted . lock ( ) . unwrap ( ) . split_off ( 0 ) ;
720
752
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 ) ;
722
754
723
755
// Connect blocks until the HTLCs expire, allowing us to (validly) broadcast the HTLC-Timeout
724
756
// transaction.
725
- connect_blocks ( & nodes[ 0 ] , TEST_FINAL_CLTV - 1 ) ;
757
+ connect_blocks ( & nodes[ 0 ] , TEST_FINAL_CLTV ) ;
726
758
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,
729
760
confirmation_height: node_a_commitment_claimable,
730
761
} , htlc_balance_known_preimage. clone( ) , htlc_balance_unknown_preimage. clone( ) ] ) ,
731
762
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) ;
733
770
734
771
// Now confirm nodes[0]'s HTLC-Timeout transaction, which changes the claimable balance to an
735
772
// "awaiting confirmations" one.
736
773
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 ] ) ;
738
775
// Note that prior to the fix in the commit which introduced this test, this (and the next
739
776
// balance) check failed. With this check removed, the code panicked in the `connect_blocks`
740
777
// call, as described, two hunks down.
741
778
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,
744
780
confirmation_height: node_a_commitment_claimable,
745
781
} , Balance :: ClaimableAwaitingConfirmations {
746
782
amount_satoshis: 10_000 ,
@@ -753,8 +789,7 @@ fn test_balances_on_local_commitment_htlcs() {
753
789
mine_transaction ( & nodes[ 0 ] , & bs_htlc_claim_txn[ 0 ] ) ;
754
790
expect_payment_sent ( & nodes[ 0 ] , payment_preimage_2, None , true , false ) ;
755
791
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,
758
793
confirmation_height: node_a_commitment_claimable,
759
794
} , Balance :: ClaimableAwaitingConfirmations {
760
795
amount_satoshis: 10_000 ,
@@ -769,8 +804,7 @@ fn test_balances_on_local_commitment_htlcs() {
769
804
expect_payment_failed ! ( nodes[ 0 ] , payment_hash, false ) ;
770
805
771
806
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,
774
808
confirmation_height: node_a_commitment_claimable,
775
809
} , Balance :: ClaimableAwaitingConfirmations {
776
810
amount_satoshis: 10_000 ,
@@ -803,6 +837,12 @@ fn test_balances_on_local_commitment_htlcs() {
803
837
assert ! ( nodes[ 0 ] . chain_monitor. chain_monitor. get_monitor( funding_outpoint) . unwrap( ) . get_claimable_balances( ) . is_empty( ) ) ;
804
838
}
805
839
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
+
806
846
#[ test]
807
847
fn test_no_preimage_inbound_htlc_balances ( ) {
808
848
// Tests that MaybePreimageClaimableHTLC are generated for inbound HTLCs for which we do not
0 commit comments