@@ -7970,4 +7970,76 @@ mod tests {
7970
7970
} ;
7971
7971
check_spends ! ( spend_tx, node_txn[ 3 ] . clone( ) ) ;
7972
7972
}
7973
+
7974
+ #[ test]
7975
+ fn test_dynamic_spendable_outputs_local_htlc_success_tx ( ) {
7976
+ let nodes = create_network ( 2 ) ;
7977
+
7978
+ // Create some initial channels
7979
+ let chan_1 = create_announced_chan_between_nodes ( & nodes, 0 , 1 ) ;
7980
+
7981
+ let payment_preimage = route_payment ( & nodes[ 0 ] , & vec ! ( & nodes[ 1 ] ) [ ..] , 9000000 ) . 0 ;
7982
+ let local_txn = nodes[ 1 ] . node . channel_state . lock ( ) . unwrap ( ) . by_id . get ( & chan_1. 2 ) . unwrap ( ) . last_local_commitment_txn . clone ( ) ;
7983
+ assert_eq ! ( local_txn[ 0 ] . input. len( ) , 1 ) ;
7984
+ check_spends ! ( local_txn[ 0 ] , chan_1. 3 . clone( ) ) ;
7985
+
7986
+ nodes[ 1 ] . node . claim_funds ( payment_preimage) ;
7987
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
7988
+ let header = BlockHeader { version : 0x20000000 , prev_blockhash : Default :: default ( ) , merkle_root : Default :: default ( ) , time : 42 , bits : 42 , nonce : 42 } ;
7989
+ nodes[ 1 ] . chain_monitor . block_connected_with_filtering ( & Block { header, txdata : vec ! [ local_txn[ 0 ] . clone( ) ] } , 1 ) ;
7990
+ let events = nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
7991
+ match events[ 0 ] {
7992
+ MessageSendEvent :: UpdateHTLCs { .. } => { } ,
7993
+ _ => panic ! ( "Unexpected event" ) ,
7994
+ }
7995
+ match events[ 1 ] {
7996
+ MessageSendEvent :: BroadcastChannelUpdate { .. } => { } ,
7997
+ _ => panic ! ( "Unexepected event" ) ,
7998
+ }
7999
+ let node_txn = nodes[ 1 ] . tx_broadcaster . txn_broadcasted . lock ( ) . unwrap ( ) ;
8000
+ assert_eq ! ( node_txn[ 0 ] . input. len( ) , 1 ) ;
8001
+ assert_eq ! ( node_txn[ 0 ] . input[ 0 ] . witness. last( ) . unwrap( ) . len( ) , 138 ) ;
8002
+ check_spends ! ( node_txn[ 0 ] , local_txn[ 0 ] . clone( ) ) ;
8003
+
8004
+ let events = nodes[ 1 ] . chan_monitor . simple_monitor . get_and_clear_pending_events ( ) ;
8005
+ let spend_tx = match events[ 0 ] {
8006
+ Event :: SpendableOutputs { ref outputs } => {
8007
+ match outputs[ 0 ] {
8008
+ SpendableOutputDescriptor :: DynamicOutput { ref outpoint, ref key, ref witness_script, ref to_self_delay } => {
8009
+ let input = TxIn {
8010
+ previous_output : outpoint. clone ( ) ,
8011
+ script_sig : Script :: new ( ) ,
8012
+ sequence : * to_self_delay as u32 ,
8013
+ witness : Vec :: new ( ) ,
8014
+ } ;
8015
+ let output = TxOut {
8016
+ script_pubkey : Builder :: new ( ) . push_opcode ( opcodes:: All :: OP_RETURN ) . into_script ( ) ,
8017
+ value : node_txn[ 0 ] . output [ 0 ] . value ,
8018
+ } ;
8019
+ let mut spend_tx = Transaction {
8020
+ version : 2 ,
8021
+ lock_time : 0 ,
8022
+ input : vec ! [ input] ,
8023
+ output : vec ! [ output] ,
8024
+ } ;
8025
+ if let Some ( witness_script) = witness_script. clone ( ) {
8026
+ let sighash = Message :: from_slice ( & bip143:: SighashComponents :: new ( & spend_tx) . sighash_all ( & spend_tx. input [ 0 ] , & witness_script, node_txn[ 0 ] . output [ 0 ] . value ) [ ..] ) . unwrap ( ) ;
8027
+ let secp_ctx = Secp256k1 :: new ( ) ;
8028
+ let local_delaysig = secp_ctx. sign ( & sighash, key) ;
8029
+ spend_tx. input [ 0 ] . witness . push ( local_delaysig. serialize_der ( & secp_ctx) . to_vec ( ) ) ;
8030
+ spend_tx. input [ 0 ] . witness [ 0 ] . push ( SigHashType :: All as u8 ) ;
8031
+ spend_tx. input [ 0 ] . witness . push ( vec ! ( 0 ) ) ;
8032
+ spend_tx. input [ 0 ] . witness . push ( witness_script. clone ( ) . into_bytes ( ) ) ;
8033
+ spend_tx
8034
+ } else {
8035
+ panic ! ( "Unexpected event" ) ;
8036
+ }
8037
+ } ,
8038
+ _ => panic ! ( "Unexpected event" ) ,
8039
+ }
8040
+ } ,
8041
+ _ => panic ! ( "Unexpected event" ) ,
8042
+ } ;
8043
+ check_spends ! ( spend_tx, node_txn[ 0 ] . clone( ) ) ;
8044
+ }
7973
8045
}
0 commit comments