@@ -7588,8 +7588,8 @@ mod tests {
7588
7588
} else { panic ! ( "Unexpected result" ) ; }
7589
7589
}
7590
7590
7591
- macro_rules! check_dynamic_output_p2wsh {
7592
- ( $node: expr) => {
7591
+ macro_rules! check_spendable_outputs {
7592
+ ( $node: expr, $der_idx : expr ) => {
7593
7593
{
7594
7594
let events = $node. chan_monitor. simple_monitor. get_and_clear_pending_events( ) ;
7595
7595
let mut txn = Vec :: new( ) ;
@@ -7598,6 +7598,33 @@ mod tests {
7598
7598
Event :: SpendableOutputs { ref outputs } => {
7599
7599
for outp in outputs {
7600
7600
match * outp {
7601
+ SpendableOutputDescriptor :: DynamicOutputP2WPKH { ref outpoint, ref key, ref output } => {
7602
+ let input = TxIn {
7603
+ previous_output: outpoint. clone( ) ,
7604
+ script_sig: Script :: new( ) ,
7605
+ sequence: 0 ,
7606
+ witness: Vec :: new( ) ,
7607
+ } ;
7608
+ let outp = TxOut {
7609
+ script_pubkey: Builder :: new( ) . push_opcode( opcodes:: All :: OP_RETURN ) . into_script( ) ,
7610
+ value: output. value,
7611
+ } ;
7612
+ let mut spend_tx = Transaction {
7613
+ version: 2 ,
7614
+ lock_time: 0 ,
7615
+ input: vec![ input] ,
7616
+ output: vec![ outp] ,
7617
+ } ;
7618
+ let secp_ctx = Secp256k1 :: new( ) ;
7619
+ let remotepubkey = PublicKey :: from_secret_key( & secp_ctx, & key) ;
7620
+ let witness_script = Address :: p2pkh( & remotepubkey, Network :: Testnet ) . script_pubkey( ) ;
7621
+ let sighash = Message :: from_slice( & bip143:: SighashComponents :: new( & spend_tx) . sighash_all( & spend_tx. input[ 0 ] , & witness_script, output. value) [ ..] ) . unwrap( ) ;
7622
+ let remotesig = secp_ctx. sign( & sighash, key) ;
7623
+ spend_tx. input[ 0 ] . witness. push( remotesig. serialize_der( & secp_ctx) . to_vec( ) ) ;
7624
+ spend_tx. input[ 0 ] . witness[ 0 ] . push( SigHashType :: All as u8 ) ;
7625
+ spend_tx. input[ 0 ] . witness. push( remotepubkey. serialize( ) . to_vec( ) ) ;
7626
+ txn. push( spend_tx) ;
7627
+ } ,
7601
7628
SpendableOutputDescriptor :: DynamicOutputP2WSH { ref outpoint, ref key, ref witness_script, ref to_self_delay, ref output } => {
7602
7629
let input = TxIn {
7603
7630
previous_output: outpoint. clone( ) ,
@@ -7624,29 +7651,8 @@ mod tests {
7624
7651
spend_tx. input[ 0 ] . witness. push( witness_script. clone( ) . into_bytes( ) ) ;
7625
7652
txn. push( spend_tx) ;
7626
7653
} ,
7627
- _ => panic!( "Unexpected event" ) ,
7628
- }
7629
- }
7630
- } ,
7631
- _ => panic!( "Unexpected event" ) ,
7632
- } ;
7633
- }
7634
- txn
7635
- }
7636
- }
7637
- }
7638
-
7639
- macro_rules! check_dynamic_output_p2wpkh {
7640
- ( $node: expr) => {
7641
- {
7642
- let events = $node. chan_monitor. simple_monitor. get_and_clear_pending_events( ) ;
7643
- let mut txn = Vec :: new( ) ;
7644
- for event in events {
7645
- match event {
7646
- Event :: SpendableOutputs { ref outputs } => {
7647
- for outp in outputs {
7648
- match * outp {
7649
- SpendableOutputDescriptor :: DynamicOutputP2WPKH { ref outpoint, ref key, ref output } => {
7654
+ SpendableOutputDescriptor :: StaticOutput { ref outpoint, ref output } => {
7655
+ let secp_ctx = Secp256k1 :: new( ) ;
7650
7656
let input = TxIn {
7651
7657
previous_output: outpoint. clone( ) ,
7652
7658
script_sig: Script :: new( ) ,
@@ -7661,19 +7667,28 @@ mod tests {
7661
7667
version: 2 ,
7662
7668
lock_time: 0 ,
7663
7669
input: vec![ input] ,
7664
- output: vec![ outp] ,
7670
+ output: vec![ outp. clone ( ) ] ,
7665
7671
} ;
7666
- let secp_ctx = Secp256k1 :: new( ) ;
7667
- let remotepubkey = PublicKey :: from_secret_key( & secp_ctx, & key) ;
7668
- let witness_script = Address :: p2pkh( & remotepubkey, Network :: Testnet ) . script_pubkey( ) ;
7672
+ let secret = {
7673
+ match ExtendedPrivKey :: new_master( & secp_ctx, Network :: Testnet , & $node. node_seed) {
7674
+ Ok ( master_key) => {
7675
+ match master_key. ckd_priv( & secp_ctx, ChildNumber :: from_hardened_idx( $der_idx) ) {
7676
+ Ok ( key) => key,
7677
+ Err ( _) => panic!( "Your RNG is busted" ) ,
7678
+ }
7679
+ }
7680
+ Err ( _) => panic!( "Your rng is busted" ) ,
7681
+ }
7682
+ } ;
7683
+ let pubkey = ExtendedPubKey :: from_private( & secp_ctx, & secret) . public_key;
7684
+ let witness_script = Address :: p2pkh( & pubkey, Network :: Testnet ) . script_pubkey( ) ;
7669
7685
let sighash = Message :: from_slice( & bip143:: SighashComponents :: new( & spend_tx) . sighash_all( & spend_tx. input[ 0 ] , & witness_script, output. value) [ ..] ) . unwrap( ) ;
7670
- let remotesig = secp_ctx. sign( & sighash, key ) ;
7671
- spend_tx. input[ 0 ] . witness. push( remotesig . serialize_der( & secp_ctx) . to_vec( ) ) ;
7686
+ let sig = secp_ctx. sign( & sighash, & secret . secret_key ) ;
7687
+ spend_tx. input[ 0 ] . witness. push( sig . serialize_der( & secp_ctx) . to_vec( ) ) ;
7672
7688
spend_tx. input[ 0 ] . witness[ 0 ] . push( SigHashType :: All as u8 ) ;
7673
- spend_tx. input[ 0 ] . witness. push( remotepubkey . serialize( ) . to_vec( ) ) ;
7689
+ spend_tx. input[ 0 ] . witness. push( pubkey . serialize( ) . to_vec( ) ) ;
7674
7690
txn. push( spend_tx) ;
7675
7691
} ,
7676
- _ => panic!( "Unexpected event" ) ,
7677
7692
}
7678
7693
}
7679
7694
} ,
@@ -7685,57 +7700,6 @@ mod tests {
7685
7700
}
7686
7701
}
7687
7702
7688
- macro_rules! check_static_output {
7689
- ( $event: expr, $node: expr, $event_idx: expr, $output_idx: expr, $der_idx: expr, $idx_node: expr) => {
7690
- match $event[ $event_idx] {
7691
- Event :: SpendableOutputs { ref outputs } => {
7692
- match outputs[ $output_idx] {
7693
- SpendableOutputDescriptor :: StaticOutput { ref outpoint, ref output } => {
7694
- let secp_ctx = Secp256k1 :: new( ) ;
7695
- let input = TxIn {
7696
- previous_output: outpoint. clone( ) ,
7697
- script_sig: Script :: new( ) ,
7698
- sequence: 0 ,
7699
- witness: Vec :: new( ) ,
7700
- } ;
7701
- let outp = TxOut {
7702
- script_pubkey: Builder :: new( ) . push_opcode( opcodes:: All :: OP_RETURN ) . into_script( ) ,
7703
- value: output. value,
7704
- } ;
7705
- let mut spend_tx = Transaction {
7706
- version: 2 ,
7707
- lock_time: 0 ,
7708
- input: vec![ input] ,
7709
- output: vec![ outp. clone( ) ] ,
7710
- } ;
7711
- let secret = {
7712
- match ExtendedPrivKey :: new_master( & secp_ctx, Network :: Testnet , & $node[ $idx_node] . node_seed) {
7713
- Ok ( master_key) => {
7714
- match master_key. ckd_priv( & secp_ctx, ChildNumber :: from_hardened_idx( $der_idx) ) {
7715
- Ok ( key) => key,
7716
- Err ( _) => panic!( "Your RNG is busted" ) ,
7717
- }
7718
- }
7719
- Err ( _) => panic!( "Your rng is busted" ) ,
7720
- }
7721
- } ;
7722
- let pubkey = ExtendedPubKey :: from_private( & secp_ctx, & secret) . public_key;
7723
- let witness_script = Address :: p2pkh( & pubkey, Network :: Testnet ) . script_pubkey( ) ;
7724
- let sighash = Message :: from_slice( & bip143:: SighashComponents :: new( & spend_tx) . sighash_all( & spend_tx. input[ 0 ] , & witness_script, output. value) [ ..] ) . unwrap( ) ;
7725
- let sig = secp_ctx. sign( & sighash, & secret. secret_key) ;
7726
- spend_tx. input[ 0 ] . witness. push( sig. serialize_der( & secp_ctx) . to_vec( ) ) ;
7727
- spend_tx. input[ 0 ] . witness[ 0 ] . push( SigHashType :: All as u8 ) ;
7728
- spend_tx. input[ 0 ] . witness. push( pubkey. serialize( ) . to_vec( ) ) ;
7729
- spend_tx
7730
- } ,
7731
- _ => panic!( "Unexpected event !" ) ,
7732
- }
7733
- } ,
7734
- _ => panic!( "Unexpected event !" ) ,
7735
- } ;
7736
- }
7737
- }
7738
-
7739
7703
#[ test]
7740
7704
fn test_claim_sizeable_push_msat ( ) {
7741
7705
// Incidentally test SpendableOutput event generation due to detection of to_local output on commitment tx
@@ -7755,14 +7719,14 @@ mod tests {
7755
7719
7756
7720
let header = BlockHeader { version : 0x20000000 , prev_blockhash : Default :: default ( ) , merkle_root : Default :: default ( ) , time : 42 , bits : 42 , nonce : 42 } ;
7757
7721
nodes[ 1 ] . chain_monitor . block_connected_with_filtering ( & Block { header, txdata : vec ! [ node_txn[ 0 ] . clone( ) ] } , 0 ) ;
7758
- let spend_txn = check_dynamic_output_p2wsh ! ( nodes[ 1 ] ) ;
7722
+ let spend_txn = check_spendable_outputs ! ( nodes[ 1 ] , 1 ) ;
7759
7723
assert_eq ! ( spend_txn. len( ) , 1 ) ;
7760
7724
check_spends ! ( spend_txn[ 0 ] , node_txn[ 0 ] . clone( ) ) ;
7761
7725
}
7762
7726
7763
7727
#[ test]
7764
7728
fn test_claim_on_remote_sizeable_push_msat ( ) {
7765
- // Same test as precedent , just test on remote commitment tx, as per_commitment_point registration changes following you're funder/fundee and
7729
+ // Same test as previous , just test on remote commitment tx, as per_commitment_point registration changes following you're funder/fundee and
7766
7730
// to_remote output is encumbered by a P2WPKH
7767
7731
7768
7732
let nodes = create_network ( 2 ) ;
@@ -7786,7 +7750,7 @@ mod tests {
7786
7750
MessageSendEvent :: BroadcastChannelUpdate { .. } => { } ,
7787
7751
_ => panic ! ( "Unexpected event" ) ,
7788
7752
}
7789
- let spend_txn = check_dynamic_output_p2wpkh ! ( nodes[ 1 ] ) ;
7753
+ let spend_txn = check_spendable_outputs ! ( nodes[ 1 ] , 1 ) ;
7790
7754
assert_eq ! ( spend_txn. len( ) , 2 ) ;
7791
7755
assert_eq ! ( spend_txn[ 0 ] , spend_txn[ 1 ] ) ;
7792
7756
check_spends ! ( spend_txn[ 0 ] , node_txn[ 0 ] . clone( ) ) ;
@@ -7827,9 +7791,10 @@ mod tests {
7827
7791
assert_eq ! ( node_txn[ 0 ] . input[ 0 ] . witness. last( ) . unwrap( ) . len( ) , 133 ) ;
7828
7792
check_spends ! ( node_txn[ 1 ] , chan_1. 3 . clone( ) ) ;
7829
7793
7830
- let events = nodes[ 1 ] . chan_monitor . simple_monitor . get_and_clear_pending_events ( ) ;
7831
- let spend_tx = check_static_output ! ( events, nodes, 0 , 0 , 1 , 1 ) ;
7832
- check_spends ! ( spend_tx, node_txn[ 0 ] . clone( ) ) ;
7794
+ let spend_txn = check_spendable_outputs ! ( nodes[ 1 ] , 1 ) ; // , 0, 0, 1, 1);
7795
+ assert_eq ! ( spend_txn. len( ) , 2 ) ;
7796
+ assert_eq ! ( spend_txn[ 0 ] , spend_txn[ 1 ] ) ;
7797
+ check_spends ! ( spend_txn[ 0 ] , node_txn[ 0 ] . clone( ) ) ;
7833
7798
}
7834
7799
7835
7800
#[ test]
@@ -7859,9 +7824,10 @@ mod tests {
7859
7824
assert_eq ! ( node_txn[ 0 ] . input. len( ) , 2 ) ;
7860
7825
check_spends ! ( node_txn[ 0 ] , revoked_local_txn[ 0 ] . clone( ) ) ;
7861
7826
7862
- let events = nodes[ 1 ] . chan_monitor . simple_monitor . get_and_clear_pending_events ( ) ;
7863
- let spend_tx = check_static_output ! ( events, nodes, 0 , 0 , 1 , 1 ) ;
7864
- check_spends ! ( spend_tx, node_txn[ 0 ] . clone( ) ) ;
7827
+ let spend_txn = check_spendable_outputs ! ( nodes[ 1 ] , 1 ) ;
7828
+ assert_eq ! ( spend_txn. len( ) , 2 ) ;
7829
+ assert_eq ! ( spend_txn[ 0 ] , spend_txn[ 1 ] ) ;
7830
+ check_spends ! ( spend_txn[ 0 ] , node_txn[ 0 ] . clone( ) ) ;
7865
7831
}
7866
7832
7867
7833
#[ test]
@@ -7905,10 +7871,12 @@ mod tests {
7905
7871
assert_eq ! ( node_txn[ 3 ] . input. len( ) , 1 ) ;
7906
7872
check_spends ! ( node_txn[ 3 ] , revoked_htlc_txn[ 0 ] . clone( ) ) ;
7907
7873
7908
- let events = nodes[ 1 ] . chan_monitor . simple_monitor . get_and_clear_pending_events ( ) ;
7909
7874
// Check B's ChannelMonitor was able to generate the right spendable output descriptor
7910
- let spend_tx = check_static_output ! ( events, nodes, 1 , 1 , 1 , 1 ) ;
7911
- check_spends ! ( spend_tx, node_txn[ 3 ] . clone( ) ) ;
7875
+ let spend_txn = check_spendable_outputs ! ( nodes[ 1 ] , 1 ) ;
7876
+ assert_eq ! ( spend_txn. len( ) , 3 ) ;
7877
+ assert_eq ! ( spend_txn[ 0 ] , spend_txn[ 1 ] ) ;
7878
+ check_spends ! ( spend_txn[ 0 ] , node_txn[ 0 ] . clone( ) ) ;
7879
+ check_spends ! ( spend_txn[ 2 ] , node_txn[ 3 ] . clone( ) ) ;
7912
7880
}
7913
7881
7914
7882
#[ test]
@@ -7953,10 +7921,13 @@ mod tests {
7953
7921
assert_eq ! ( node_txn[ 3 ] . input. len( ) , 1 ) ;
7954
7922
check_spends ! ( node_txn[ 3 ] , revoked_htlc_txn[ 0 ] . clone( ) ) ;
7955
7923
7956
- let events = nodes[ 0 ] . chan_monitor . simple_monitor . get_and_clear_pending_events ( ) ;
7957
7924
// Check A's ChannelMonitor was able to generate the right spendable output descriptor
7958
- let spend_tx = check_static_output ! ( events, nodes, 1 , 2 , 1 , 0 ) ;
7959
- check_spends ! ( spend_tx, node_txn[ 3 ] . clone( ) ) ;
7925
+ let spend_txn = check_spendable_outputs ! ( nodes[ 0 ] , 1 ) ;
7926
+ assert_eq ! ( spend_txn. len( ) , 5 ) ;
7927
+ assert_eq ! ( spend_txn[ 1 ] , spend_txn[ 3 ] ) ;
7928
+ check_spends ! ( spend_txn[ 0 ] , revoked_local_txn[ 0 ] . clone( ) ) ; // spending to_remote output from revoked local tx
7929
+ check_spends ! ( spend_txn[ 1 ] , node_txn[ 2 ] . clone( ) ) ; // spending justice tx output from revoked local tx htlc received output
7930
+ check_spends ! ( spend_txn[ 4 ] , node_txn[ 3 ] . clone( ) ) ; // spending justice tx output on htlc success tx
7960
7931
}
7961
7932
7962
7933
#[ test]
@@ -7991,7 +7962,7 @@ mod tests {
7991
7962
check_spends ! ( node_txn[ 0 ] , local_txn[ 0 ] . clone( ) ) ;
7992
7963
7993
7964
// Verify that B is able to spend its own HTLC-Success tx thanks to spendable output event given back by its ChannelMonitor
7994
- let spend_txn = check_dynamic_output_p2wsh ! ( nodes[ 1 ] ) ;
7965
+ let spend_txn = check_spendable_outputs ! ( nodes[ 1 ] , 1 ) ;
7995
7966
assert_eq ! ( spend_txn. len( ) , 1 ) ;
7996
7967
check_spends ! ( spend_txn[ 0 ] , node_txn[ 0 ] . clone( ) ) ;
7997
7968
}
@@ -8022,7 +7993,7 @@ mod tests {
8022
7993
check_spends ! ( node_txn[ 0 ] , local_txn[ 0 ] . clone( ) ) ;
8023
7994
8024
7995
// Verify that A is able to spend its own HTLC-Timeout tx thanks to spendable output event given back by its ChannelMonitor
8025
- let spend_txn = check_dynamic_output_p2wsh ! ( nodes[ 0 ] ) ;
7996
+ let spend_txn = check_spendable_outputs ! ( nodes[ 0 ] , 1 ) ;
8026
7997
assert_eq ! ( spend_txn. len( ) , 4 ) ;
8027
7998
assert_eq ! ( spend_txn[ 0 ] , spend_txn[ 2 ] ) ;
8028
7999
assert_eq ! ( spend_txn[ 1 ] , spend_txn[ 3 ] ) ;
@@ -8041,13 +8012,13 @@ mod tests {
8041
8012
8042
8013
let header = BlockHeader { version : 0x20000000 , prev_blockhash : Default :: default ( ) , merkle_root : Default :: default ( ) , time : 42 , bits : 42 , nonce : 42 } ;
8043
8014
nodes[ 0 ] . chain_monitor . block_connected_with_filtering ( & Block { header, txdata : vec ! [ closing_tx. clone( ) ] } , 1 ) ;
8044
- let events = nodes[ 0 ] . chan_monitor . simple_monitor . get_and_clear_pending_events ( ) ;
8045
- let spend_tx = check_static_output ! ( events , nodes , 0 , 0 , 2 , 0 ) ;
8046
- check_spends ! ( spend_tx , closing_tx. clone( ) ) ;
8015
+ let spend_txn = check_spendable_outputs ! ( nodes[ 0 ] , 2 ) ;
8016
+ assert_eq ! ( spend_txn . len ( ) , 1 ) ;
8017
+ check_spends ! ( spend_txn [ 0 ] , closing_tx. clone( ) ) ;
8047
8018
8048
8019
nodes[ 1 ] . chain_monitor . block_connected_with_filtering ( & Block { header, txdata : vec ! [ closing_tx. clone( ) ] } , 1 ) ;
8049
- let events = nodes[ 1 ] . chan_monitor . simple_monitor . get_and_clear_pending_events ( ) ;
8050
- let spend_tx = check_static_output ! ( events , nodes , 0 , 0 , 2 , 1 ) ;
8051
- check_spends ! ( spend_tx , closing_tx) ;
8020
+ let spend_txn = check_spendable_outputs ! ( nodes[ 1 ] , 2 ) ;
8021
+ assert_eq ! ( spend_txn . len ( ) , 1 ) ;
8022
+ check_spends ! ( spend_txn [ 0 ] , closing_tx) ;
8052
8023
}
8053
8024
}
0 commit comments