@@ -3426,6 +3426,71 @@ mod tests {
34263426 assert_eq ! ( nodes[ 1 ] . node. list_channels( ) . len( ) , 0 ) ;
34273427 }
34283428
3429+ #[ test]
3430+ fn claim_htlc_outputs_shared_tx ( ) {
3431+ // Node revoked old state, htlcs haven't time out yet, claim them in shared justice tx
3432+
3433+ let nodes = create_network ( 2 ) ;
3434+
3435+ // Create some new channel:
3436+ let chan_1 = create_announced_chan_between_nodes ( & nodes, 0 , 1 ) ;
3437+
3438+ // Rebalance the network to generate htlc in the two directions
3439+ send_payment ( & nodes[ 0 ] , & vec ! ( & nodes[ 1 ] ) [ ..] , 8000000 ) ;
3440+ // node[0] is gonna to revoke an old state thus node[1] should be able to claim both offered/received HTLC outputs on top of commitment tx
3441+ let payment_preimage_1 = route_payment ( & nodes[ 0 ] , & vec ! ( & nodes[ 1 ] ) [ ..] , 3000000 ) . 0 ;
3442+ let _payment_preimage_2 = route_payment ( & nodes[ 1 ] , & vec ! ( & nodes[ 0 ] ) [ ..] , 3000000 ) . 0 ;
3443+
3444+ // Get the will-be-revoked local txn from node[0]
3445+ let revoked_local_txn = nodes[ 0 ] . node . channel_state . lock ( ) . unwrap ( ) . by_id . get ( & chan_1. 2 ) . unwrap ( ) . last_local_commitment_txn . clone ( ) ;
3446+
3447+ //Revoke the old state
3448+ claim_payment ( & nodes[ 0 ] , & vec ! ( & nodes[ 1 ] ) [ ..] , payment_preimage_1) ;
3449+
3450+ {
3451+ let header = BlockHeader { version : 0x20000000 , prev_blockhash : Default :: default ( ) , merkle_root : Default :: default ( ) , time : 42 , bits : 42 , nonce : 42 } ;
3452+
3453+ nodes[ 0 ] . chain_monitor . block_connected_with_filtering ( & Block { header, txdata : vec ! [ revoked_local_txn[ 0 ] . clone( ) ] } , 1 ) ;
3454+
3455+ nodes[ 1 ] . chain_monitor . block_connected_with_filtering ( & Block { header, txdata : vec ! [ revoked_local_txn[ 0 ] . clone( ) ] } , 1 ) ;
3456+ let node_txn = nodes[ 1 ] . tx_broadcaster . txn_broadcasted . lock ( ) . unwrap ( ) ;
3457+ assert_eq ! ( node_txn. len( ) , 4 ) ;
3458+
3459+ let mut revoked_tx_map = HashMap :: new ( ) ;
3460+ revoked_tx_map. insert ( revoked_local_txn[ 0 ] . txid ( ) , revoked_local_txn[ 0 ] . clone ( ) ) ;
3461+
3462+ assert_eq ! ( node_txn[ 0 ] . input. len( ) , 2 ) ;
3463+ node_txn[ 0 ] . verify ( & revoked_tx_map) . unwrap ( ) ;
3464+
3465+ assert_eq ! ( node_txn[ 3 ] . input. len( ) , 2 ) ;
3466+ node_txn[ 3 ] . verify ( & revoked_tx_map) . unwrap ( ) ;
3467+
3468+ assert_eq ! ( node_txn[ 0 ] . txid( ) , node_txn[ 3 ] . txid( ) ) ; // justice tx is duplicated due to block re-scanning
3469+
3470+ let witness_script_1 = node_txn[ 0 ] . clone ( ) . input [ 0 ] . witness . pop ( ) . unwrap ( ) ;
3471+ let witness_script_2 = node_txn[ 0 ] . clone ( ) . input [ 1 ] . witness . pop ( ) . unwrap ( ) ;
3472+ if witness_script_1. len ( ) > 133 {
3473+ assert_eq ! ( witness_script_1. len( ) , 138 ) ;
3474+ assert_eq ! ( witness_script_2. len( ) , 133 ) ;
3475+ } else {
3476+ assert_eq ! ( witness_script_1. len( ) , 133 ) ;
3477+ assert_eq ! ( witness_script_2. len( ) , 138 ) ;
3478+ }
3479+
3480+ assert_eq ! ( node_txn[ 1 ] . input. len( ) , 1 ) ;
3481+ let witness_script = node_txn[ 1 ] . clone ( ) . input [ 0 ] . witness . pop ( ) . unwrap ( ) ;
3482+ assert_eq ! ( witness_script. len( ) , 71 ) ; // Spending funding tx unique txoutput, tx broadcasted by ChannelManager
3483+
3484+ assert_eq ! ( node_txn[ 2 ] . input. len( ) , 1 ) ;
3485+ let witness_script = node_txn[ 2 ] . clone ( ) . input [ 0 ] . witness . pop ( ) . unwrap ( ) ;
3486+ assert_eq ! ( witness_script. len( ) , 133 ) ; //Spending a offered htlc output
3487+
3488+ }
3489+ get_announce_close_broadcast_events ( & nodes, 0 , 1 ) ;
3490+ assert_eq ! ( nodes[ 0 ] . node. list_channels( ) . len( ) , 0 ) ;
3491+ assert_eq ! ( nodes[ 1 ] . node. list_channels( ) . len( ) , 0 ) ;
3492+ }
3493+
34293494 #[ test]
34303495 fn test_htlc_ignore_latest_remote_commitment ( ) {
34313496 // Test that HTLC transactions spending the latest remote commitment transaction are simply
0 commit comments