@@ -1961,45 +1961,9 @@ pub fn test_htlc_on_chain_success() {
19611961 _ => panic ! ( "Unexpected event" ) ,
19621962 }
19631963
1964- macro_rules! check_tx_local_broadcast {
1965- ( $node: expr, $htlc_offered: expr, $commitment_tx: expr) => { {
1966- let mut node_txn = $node. tx_broadcaster. txn_broadcasted. lock( ) . unwrap( ) ;
1967- // HTLC timeout claims for non-anchor channels are only aggregated when claimed from the
1968- // remote commitment transaction.
1969- if $htlc_offered {
1970- assert_eq!( node_txn. len( ) , 2 ) ;
1971- for tx in node_txn. iter( ) {
1972- check_spends!( tx, $commitment_tx) ;
1973- assert_ne!( tx. lock_time, LockTime :: ZERO ) ;
1974- assert_eq!(
1975- tx. input[ 0 ] . witness. last( ) . unwrap( ) . len( ) ,
1976- OFFERED_HTLC_SCRIPT_WEIGHT
1977- ) ;
1978- assert!( tx. output[ 0 ] . script_pubkey. is_p2wsh( ) ) ; // revokeable output
1979- }
1980- assert_ne!(
1981- node_txn[ 0 ] . input[ 0 ] . previous_output,
1982- node_txn[ 1 ] . input[ 0 ] . previous_output
1983- ) ;
1984- } else {
1985- assert_eq!( node_txn. len( ) , 1 ) ;
1986- check_spends!( node_txn[ 0 ] , $commitment_tx) ;
1987- assert_ne!( node_txn[ 0 ] . lock_time, LockTime :: ZERO ) ;
1988- assert_eq!(
1989- node_txn[ 0 ] . input[ 0 ] . witness. last( ) . unwrap( ) . len( ) ,
1990- ACCEPTED_HTLC_SCRIPT_WEIGHT
1991- ) ;
1992- assert!( node_txn[ 0 ] . output[ 0 ] . script_pubkey. is_p2wpkh( ) ) ; // direct payment
1993- assert_ne!(
1994- node_txn[ 0 ] . input[ 0 ] . previous_output,
1995- node_txn[ 0 ] . input[ 1 ] . previous_output
1996- ) ;
1997- }
1998- node_txn. clear( ) ;
1999- } } ;
2000- }
2001- // nodes[1] now broadcasts its own timeout-claim of the output that nodes[2] just claimed via success.
2002- check_tx_local_broadcast ! ( nodes[ 1 ] , false , commitment_tx[ 0 ] ) ;
1964+ // nodes[1] does not broadcast its own timeout-claim of the output as nodes[2] just claimed it
1965+ // via success.
1966+ assert ! ( nodes[ 1 ] . tx_broadcaster. txn_broadcasted. lock( ) . unwrap( ) . is_empty( ) ) ;
20031967
20041968 // Broadcast legit commitment tx from A on B's chain
20051969 // Broadcast preimage tx by B on offered output from A commitment tx on A's chain
@@ -2061,7 +2025,17 @@ pub fn test_htlc_on_chain_success() {
20612025 _ => panic ! ( "Unexpected event" ) ,
20622026 }
20632027 }
2064- check_tx_local_broadcast ! ( nodes[ 0 ] , true , node_a_commitment_tx[ 0 ] ) ;
2028+ // HTLC timeout claims for non-anchor channels are only aggregated when claimed from the
2029+ // remote commitment transaction.
2030+ let mut node_txn = nodes[ 0 ] . tx_broadcaster . txn_broadcast ( ) ;
2031+ assert_eq ! ( node_txn. len( ) , 2 ) ;
2032+ for tx in node_txn. iter ( ) {
2033+ check_spends ! ( tx, node_a_commitment_tx[ 0 ] ) ;
2034+ assert_ne ! ( tx. lock_time, LockTime :: ZERO ) ;
2035+ assert_eq ! ( tx. input[ 0 ] . witness. last( ) . unwrap( ) . len( ) , OFFERED_HTLC_SCRIPT_WEIGHT ) ;
2036+ assert ! ( tx. output[ 0 ] . script_pubkey. is_p2wsh( ) ) ; // revokeable output
2037+ }
2038+ assert_ne ! ( node_txn[ 0 ] . input[ 0 ] . previous_output, node_txn[ 1 ] . input[ 0 ] . previous_output) ;
20652039}
20662040
20672041fn do_test_htlc_on_chain_timeout ( connect_style : ConnectStyle ) {
@@ -11765,3 +11739,142 @@ pub fn test_funding_signed_event() {
1176511739 nodes[ 0 ] . node . get_and_clear_pending_msg_events ( ) ;
1176611740 nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
1176711741}
11742+
11743+ #[ xtest( feature = "_externalize_tests" ) ]
11744+ pub fn test_claimed_htlcs_not_in_pending_claim_requests ( ) {
11745+ use crate :: events:: bump_transaction:: sync:: WalletSourceSync ;
11746+
11747+ // ====== TEST SETUP ======
11748+ let chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
11749+ let node_cfgs = create_node_cfgs ( 2 , & chanmon_cfgs) ;
11750+
11751+ let mut user_cfg = test_default_channel_config ( ) ;
11752+
11753+ // Anchor channels are required so that multiple HTLC-Successes can be aggregated into a single
11754+ // transaction.
11755+ user_cfg. channel_handshake_config . negotiate_anchors_zero_fee_htlc_tx = true ;
11756+ user_cfg. manually_accept_inbound_channels = true ;
11757+
11758+ let configs = [ Some ( user_cfg. clone ( ) ) , Some ( user_cfg. clone ( ) ) ] ;
11759+ let node_chanmgrs = create_node_chanmgrs ( 2 , & node_cfgs, & configs) ;
11760+ let nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
11761+
11762+ let node_a_id = nodes[ 0 ] . node . get_our_node_id ( ) ;
11763+ let node_b_id = nodes[ 1 ] . node . get_our_node_id ( ) ;
11764+
11765+ // Since we're using anchor channels, make sure each node has a UTXO for paying fees.
11766+ let coinbase_tx = Transaction {
11767+ version : Version :: TWO ,
11768+ lock_time : LockTime :: ZERO ,
11769+ input : vec ! [ TxIn { ..Default :: default ( ) } ] ,
11770+ output : vec ! [
11771+ TxOut {
11772+ value: Amount :: ONE_BTC ,
11773+ script_pubkey: nodes[ 0 ] . wallet_source. get_change_script( ) . unwrap( ) ,
11774+ } ,
11775+ TxOut {
11776+ value: Amount :: ONE_BTC ,
11777+ script_pubkey: nodes[ 1 ] . wallet_source. get_change_script( ) . unwrap( ) ,
11778+ } ,
11779+ ] ,
11780+ } ;
11781+ nodes[ 0 ] . wallet_source . add_utxo (
11782+ bitcoin:: OutPoint { txid : coinbase_tx. compute_txid ( ) , vout : 0 } ,
11783+ coinbase_tx. output [ 0 ] . value ,
11784+ ) ;
11785+ nodes[ 1 ] . wallet_source . add_utxo (
11786+ bitcoin:: OutPoint { txid : coinbase_tx. compute_txid ( ) , vout : 1 } ,
11787+ coinbase_tx. output [ 1 ] . value ,
11788+ ) ;
11789+
11790+ const CHAN_CAPACITY : u64 = 10_000_000 ;
11791+ let ( _, _, cid, funding_tx) =
11792+ create_announced_chan_between_nodes_with_value ( & nodes, 0 , 1 , CHAN_CAPACITY , 0 ) ;
11793+
11794+ // Ensure all nodes are at the same initial height.
11795+ let node_max_height = nodes. iter ( ) . map ( |node| node. best_block_info ( ) . 1 ) . max ( ) . unwrap ( ) ;
11796+ for node in & nodes {
11797+ let blocks_to_mine = node_max_height - node. best_block_info ( ) . 1 ;
11798+ if blocks_to_mine > 0 {
11799+ connect_blocks ( node, blocks_to_mine) ;
11800+ }
11801+ }
11802+
11803+ // ====== TEST PROCESS ======
11804+
11805+ // Route HTLC 1 and 2 from A to B.
11806+ let ( preimage_1, payment_hash_1, ..) = route_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] ] , 1_000_000 ) ;
11807+ let ( _preimage_2, _payment_hash_2, ..) = route_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] ] , 1_000_000 ) ;
11808+
11809+ // Node B claims HTLC 1.
11810+ nodes[ 1 ] . node . claim_funds ( preimage_1) ;
11811+ expect_payment_claimed ! ( nodes[ 1 ] , payment_hash_1, 1_000_000 ) ;
11812+ check_added_monitors ( & nodes[ 1 ] , 1 ) ;
11813+ let _ = get_htlc_update_msgs ( & nodes[ 1 ] , & node_a_id) ;
11814+
11815+ // Force close the channel by broadcasting node B's commitment tx.
11816+ let node_b_commit_tx = get_local_commitment_txn ! ( nodes[ 1 ] , cid) ;
11817+ assert_eq ! ( node_b_commit_tx. len( ) , 1 ) ;
11818+ let node_b_commit_tx = & node_b_commit_tx[ 0 ] ;
11819+ check_spends ! ( node_b_commit_tx, funding_tx) ;
11820+
11821+ mine_transaction ( & nodes[ 0 ] , node_b_commit_tx) ;
11822+ check_closed_event (
11823+ & nodes[ 0 ] ,
11824+ 1 ,
11825+ ClosureReason :: CommitmentTxConfirmed ,
11826+ false ,
11827+ & [ node_b_id] ,
11828+ CHAN_CAPACITY ,
11829+ ) ;
11830+ check_closed_broadcast ! ( nodes[ 0 ] , true ) ;
11831+ check_added_monitors ( & nodes[ 0 ] , 1 ) ;
11832+
11833+ mine_transaction ( & nodes[ 1 ] , node_b_commit_tx) ;
11834+ check_closed_event (
11835+ & nodes[ 1 ] ,
11836+ 1 ,
11837+ ClosureReason :: CommitmentTxConfirmed ,
11838+ false ,
11839+ & [ node_a_id] ,
11840+ CHAN_CAPACITY ,
11841+ ) ;
11842+ check_closed_broadcast ! ( nodes[ 1 ] , true ) ;
11843+ check_added_monitors ( & nodes[ 1 ] , 1 ) ;
11844+
11845+ // Confirm HTLC 1 claiming tx on node A.
11846+ let process_bump_event = |node : & Node | {
11847+ let events = node. chain_monitor . chain_monitor . get_and_clear_pending_events ( ) ;
11848+ assert_eq ! ( events. len( ) , 1 ) ;
11849+ let bump_event = match & events[ 0 ] {
11850+ Event :: BumpTransaction ( bump_event) => bump_event,
11851+ e => panic ! ( "Unexepected event: {:#?}" , e) ,
11852+ } ;
11853+ node. bump_tx_handler . handle_event ( bump_event) ;
11854+
11855+ let mut tx = node. tx_broadcaster . txn_broadcast ( ) ;
11856+ assert_eq ! ( tx. len( ) , 1 ) ;
11857+ tx. pop ( ) . unwrap ( )
11858+ } ;
11859+ let bs_htlc_1_claiming_tx = process_bump_event ( & nodes[ 1 ] ) ;
11860+
11861+ mine_transaction ( & nodes[ 0 ] , & bs_htlc_1_claiming_tx) ;
11862+ // Node A reaches the HTLC timeout height.
11863+ connect_blocks ( & nodes[ 0 ] , TEST_FINAL_CLTV - 1 ) ;
11864+
11865+ // Check that node A times out only HTLC 2, and does not attempt to timeout the already
11866+ // claimed HTLC 1.
11867+ let timeout_tx = {
11868+ let mut txs = nodes[ 0 ] . tx_broadcaster . txn_broadcast ( ) ;
11869+ assert_eq ! ( txs. len( ) , 1 ) ;
11870+ txs. pop ( ) . unwrap ( )
11871+ } ;
11872+ let htlc_already_claimed = bs_htlc_1_claiming_tx. input . iter ( ) . any ( |htlc|{
11873+ htlc. previous_output == timeout_tx. input [ 0 ] . previous_output
11874+ } ) ;
11875+ assert ! ( !htlc_already_claimed, "HTLC already claimed by counterparty's HTLC-Success transaction" ) ;
11876+ assert_eq ! ( timeout_tx. input. len( ) , 1 ) ;
11877+
11878+ let events = nodes[ 0 ] . node . get_and_clear_pending_events ( ) ;
11879+ assert_eq ! ( events. len( ) , 2 ) ;
11880+ }
0 commit comments