@@ -7598,8 +7598,8 @@ mod tests {
7598
7598
} else { panic ! ( "Unexpected result" ) ; }
7599
7599
}
7600
7600
7601
- macro_rules! check_dynamic_output_p2wsh {
7602
- ( $node: expr) => {
7601
+ macro_rules! check_spendable_outputs {
7602
+ ( $node: expr, $der_idx : expr ) => {
7603
7603
{
7604
7604
let events = $node. chan_monitor. simple_monitor. get_and_clear_pending_events( ) ;
7605
7605
let mut txn = Vec :: new( ) ;
@@ -7608,6 +7608,33 @@ mod tests {
7608
7608
Event :: SpendableOutputs { ref outputs } => {
7609
7609
for outp in outputs {
7610
7610
match * outp {
7611
+ SpendableOutputDescriptor :: DynamicOutputP2WPKH { ref outpoint, ref key, ref output } => {
7612
+ let input = TxIn {
7613
+ previous_output: outpoint. clone( ) ,
7614
+ script_sig: Script :: new( ) ,
7615
+ sequence: 0 ,
7616
+ witness: Vec :: new( ) ,
7617
+ } ;
7618
+ let outp = TxOut {
7619
+ script_pubkey: Builder :: new( ) . push_opcode( opcodes:: All :: OP_RETURN ) . into_script( ) ,
7620
+ value: output. value,
7621
+ } ;
7622
+ let mut spend_tx = Transaction {
7623
+ version: 2 ,
7624
+ lock_time: 0 ,
7625
+ input: vec![ input] ,
7626
+ output: vec![ outp] ,
7627
+ } ;
7628
+ let secp_ctx = Secp256k1 :: new( ) ;
7629
+ let remotepubkey = PublicKey :: from_secret_key( & secp_ctx, & key) ;
7630
+ let witness_script = Address :: p2pkh( & remotepubkey, Network :: Testnet ) . script_pubkey( ) ;
7631
+ let sighash = Message :: from_slice( & bip143:: SighashComponents :: new( & spend_tx) . sighash_all( & spend_tx. input[ 0 ] , & witness_script, output. value) [ ..] ) . unwrap( ) ;
7632
+ let remotesig = secp_ctx. sign( & sighash, key) ;
7633
+ spend_tx. input[ 0 ] . witness. push( remotesig. serialize_der( & secp_ctx) . to_vec( ) ) ;
7634
+ spend_tx. input[ 0 ] . witness[ 0 ] . push( SigHashType :: All as u8 ) ;
7635
+ spend_tx. input[ 0 ] . witness. push( remotepubkey. serialize( ) . to_vec( ) ) ;
7636
+ txn. push( spend_tx) ;
7637
+ } ,
7611
7638
SpendableOutputDescriptor :: DynamicOutputP2WSH { ref outpoint, ref key, ref witness_script, ref to_self_delay, ref output } => {
7612
7639
let input = TxIn {
7613
7640
previous_output: outpoint. clone( ) ,
@@ -7634,29 +7661,8 @@ mod tests {
7634
7661
spend_tx. input[ 0 ] . witness. push( witness_script. clone( ) . into_bytes( ) ) ;
7635
7662
txn. push( spend_tx) ;
7636
7663
} ,
7637
- _ => panic!( "Unexpected event" ) ,
7638
- }
7639
- }
7640
- } ,
7641
- _ => panic!( "Unexpected event" ) ,
7642
- } ;
7643
- }
7644
- txn
7645
- }
7646
- }
7647
- }
7648
-
7649
- macro_rules! check_dynamic_output_p2wpkh {
7650
- ( $node: expr) => {
7651
- {
7652
- let events = $node. chan_monitor. simple_monitor. get_and_clear_pending_events( ) ;
7653
- let mut txn = Vec :: new( ) ;
7654
- for event in events {
7655
- match event {
7656
- Event :: SpendableOutputs { ref outputs } => {
7657
- for outp in outputs {
7658
- match * outp {
7659
- SpendableOutputDescriptor :: DynamicOutputP2WPKH { ref outpoint, ref key, ref output } => {
7664
+ SpendableOutputDescriptor :: StaticOutput { ref outpoint, ref output } => {
7665
+ let secp_ctx = Secp256k1 :: new( ) ;
7660
7666
let input = TxIn {
7661
7667
previous_output: outpoint. clone( ) ,
7662
7668
script_sig: Script :: new( ) ,
@@ -7671,19 +7677,28 @@ mod tests {
7671
7677
version: 2 ,
7672
7678
lock_time: 0 ,
7673
7679
input: vec![ input] ,
7674
- output: vec![ outp] ,
7680
+ output: vec![ outp. clone ( ) ] ,
7675
7681
} ;
7676
- let secp_ctx = Secp256k1 :: new( ) ;
7677
- let remotepubkey = PublicKey :: from_secret_key( & secp_ctx, & key) ;
7678
- let witness_script = Address :: p2pkh( & remotepubkey, Network :: Testnet ) . script_pubkey( ) ;
7682
+ let secret = {
7683
+ match ExtendedPrivKey :: new_master( & secp_ctx, Network :: Testnet , & $node. node_seed) {
7684
+ Ok ( master_key) => {
7685
+ match master_key. ckd_priv( & secp_ctx, ChildNumber :: from_hardened_idx( $der_idx) ) {
7686
+ Ok ( key) => key,
7687
+ Err ( _) => panic!( "Your RNG is busted" ) ,
7688
+ }
7689
+ }
7690
+ Err ( _) => panic!( "Your rng is busted" ) ,
7691
+ }
7692
+ } ;
7693
+ let pubkey = ExtendedPubKey :: from_private( & secp_ctx, & secret) . public_key;
7694
+ let witness_script = Address :: p2pkh( & pubkey, Network :: Testnet ) . script_pubkey( ) ;
7679
7695
let sighash = Message :: from_slice( & bip143:: SighashComponents :: new( & spend_tx) . sighash_all( & spend_tx. input[ 0 ] , & witness_script, output. value) [ ..] ) . unwrap( ) ;
7680
- let remotesig = secp_ctx. sign( & sighash, key ) ;
7681
- spend_tx. input[ 0 ] . witness. push( remotesig . serialize_der( & secp_ctx) . to_vec( ) ) ;
7696
+ let sig = secp_ctx. sign( & sighash, & secret . secret_key ) ;
7697
+ spend_tx. input[ 0 ] . witness. push( sig . serialize_der( & secp_ctx) . to_vec( ) ) ;
7682
7698
spend_tx. input[ 0 ] . witness[ 0 ] . push( SigHashType :: All as u8 ) ;
7683
- spend_tx. input[ 0 ] . witness. push( remotepubkey . serialize( ) . to_vec( ) ) ;
7699
+ spend_tx. input[ 0 ] . witness. push( pubkey . serialize( ) . to_vec( ) ) ;
7684
7700
txn. push( spend_tx) ;
7685
7701
} ,
7686
- _ => panic!( "Unexpected event" ) ,
7687
7702
}
7688
7703
}
7689
7704
} ,
@@ -7695,57 +7710,6 @@ mod tests {
7695
7710
}
7696
7711
}
7697
7712
7698
- macro_rules! check_static_output {
7699
- ( $event: expr, $node: expr, $event_idx: expr, $output_idx: expr, $der_idx: expr, $idx_node: expr) => {
7700
- match $event[ $event_idx] {
7701
- Event :: SpendableOutputs { ref outputs } => {
7702
- match outputs[ $output_idx] {
7703
- SpendableOutputDescriptor :: StaticOutput { ref outpoint, ref output } => {
7704
- let secp_ctx = Secp256k1 :: new( ) ;
7705
- let input = TxIn {
7706
- previous_output: outpoint. clone( ) ,
7707
- script_sig: Script :: new( ) ,
7708
- sequence: 0 ,
7709
- witness: Vec :: new( ) ,
7710
- } ;
7711
- let outp = TxOut {
7712
- script_pubkey: Builder :: new( ) . push_opcode( opcodes:: All :: OP_RETURN ) . into_script( ) ,
7713
- value: output. value,
7714
- } ;
7715
- let mut spend_tx = Transaction {
7716
- version: 2 ,
7717
- lock_time: 0 ,
7718
- input: vec![ input] ,
7719
- output: vec![ outp. clone( ) ] ,
7720
- } ;
7721
- let secret = {
7722
- match ExtendedPrivKey :: new_master( & secp_ctx, Network :: Testnet , & $node[ $idx_node] . node_seed) {
7723
- Ok ( master_key) => {
7724
- match master_key. ckd_priv( & secp_ctx, ChildNumber :: from_hardened_idx( $der_idx) ) {
7725
- Ok ( key) => key,
7726
- Err ( _) => panic!( "Your RNG is busted" ) ,
7727
- }
7728
- }
7729
- Err ( _) => panic!( "Your rng is busted" ) ,
7730
- }
7731
- } ;
7732
- let pubkey = ExtendedPubKey :: from_private( & secp_ctx, & secret) . public_key;
7733
- let witness_script = Address :: p2pkh( & pubkey, Network :: Testnet ) . script_pubkey( ) ;
7734
- let sighash = Message :: from_slice( & bip143:: SighashComponents :: new( & spend_tx) . sighash_all( & spend_tx. input[ 0 ] , & witness_script, output. value) [ ..] ) . unwrap( ) ;
7735
- let sig = secp_ctx. sign( & sighash, & secret. secret_key) ;
7736
- spend_tx. input[ 0 ] . witness. push( sig. serialize_der( & secp_ctx) . to_vec( ) ) ;
7737
- spend_tx. input[ 0 ] . witness[ 0 ] . push( SigHashType :: All as u8 ) ;
7738
- spend_tx. input[ 0 ] . witness. push( pubkey. serialize( ) . to_vec( ) ) ;
7739
- spend_tx
7740
- } ,
7741
- _ => panic!( "Unexpected event !" ) ,
7742
- }
7743
- } ,
7744
- _ => panic!( "Unexpected event !" ) ,
7745
- } ;
7746
- }
7747
- }
7748
-
7749
7713
#[ test]
7750
7714
fn test_claim_sizeable_push_msat ( ) {
7751
7715
// Incidentally test SpendableOutput event generation due to detection of to_local output on commitment tx
@@ -7765,14 +7729,14 @@ mod tests {
7765
7729
7766
7730
let header = BlockHeader { version : 0x20000000 , prev_blockhash : Default :: default ( ) , merkle_root : Default :: default ( ) , time : 42 , bits : 42 , nonce : 42 } ;
7767
7731
nodes[ 1 ] . chain_monitor . block_connected_with_filtering ( & Block { header, txdata : vec ! [ node_txn[ 0 ] . clone( ) ] } , 0 ) ;
7768
- let spend_txn = check_dynamic_output_p2wsh ! ( nodes[ 1 ] ) ;
7732
+ let spend_txn = check_spendable_outputs ! ( nodes[ 1 ] , 1 ) ;
7769
7733
assert_eq ! ( spend_txn. len( ) , 1 ) ;
7770
7734
check_spends ! ( spend_txn[ 0 ] , node_txn[ 0 ] . clone( ) ) ;
7771
7735
}
7772
7736
7773
7737
#[ test]
7774
7738
fn test_claim_on_remote_sizeable_push_msat ( ) {
7775
- // Same test as precedent , just test on remote commitment tx, as per_commitment_point registration changes following you're funder/fundee and
7739
+ // Same test as previous , just test on remote commitment tx, as per_commitment_point registration changes following you're funder/fundee and
7776
7740
// to_remote output is encumbered by a P2WPKH
7777
7741
7778
7742
let nodes = create_network ( 2 ) ;
@@ -7796,12 +7760,42 @@ mod tests {
7796
7760
MessageSendEvent :: BroadcastChannelUpdate { .. } => { } ,
7797
7761
_ => panic ! ( "Unexpected event" ) ,
7798
7762
}
7799
- let spend_txn = check_dynamic_output_p2wpkh ! ( nodes[ 1 ] ) ;
7763
+ let spend_txn = check_spendable_outputs ! ( nodes[ 1 ] , 1 ) ;
7800
7764
assert_eq ! ( spend_txn. len( ) , 2 ) ;
7801
7765
assert_eq ! ( spend_txn[ 0 ] , spend_txn[ 1 ] ) ;
7802
7766
check_spends ! ( spend_txn[ 0 ] , node_txn[ 0 ] . clone( ) ) ;
7803
7767
}
7804
7768
7769
+ #[ test]
7770
+ fn test_claim_on_remote_revoked_sizeable_push_msat ( ) {
7771
+ // Same test as previous, just test on remote revoked commitment tx, as per_commitment_point registration changes following you're funder/fundee and
7772
+ // to_remote output is encumbered by a P2WPKH
7773
+
7774
+ let nodes = create_network ( 2 ) ;
7775
+
7776
+ let chan = create_announced_chan_between_nodes_with_value ( & nodes, 0 , 1 , 100000 , 59000000 ) ;
7777
+ let payment_preimage = route_payment ( & nodes[ 0 ] , & vec ! ( & nodes[ 1 ] ) [ ..] , 3000000 ) . 0 ;
7778
+ let revoked_local_txn = nodes[ 0 ] . node . channel_state . lock ( ) . unwrap ( ) . by_id . get ( & chan. 2 ) . unwrap ( ) . last_local_commitment_txn . clone ( ) ;
7779
+ assert_eq ! ( revoked_local_txn[ 0 ] . input. len( ) , 1 ) ;
7780
+ assert_eq ! ( revoked_local_txn[ 0 ] . input[ 0 ] . previous_output. txid, chan. 3 . txid( ) ) ;
7781
+
7782
+ claim_payment ( & nodes[ 0 ] , & vec ! ( & nodes[ 1 ] ) [ ..] , payment_preimage) ;
7783
+ let header = BlockHeader { version : 0x20000000 , prev_blockhash : Default :: default ( ) , merkle_root : Default :: default ( ) , time : 42 , bits : 42 , nonce : 42 } ;
7784
+ nodes[ 1 ] . chain_monitor . block_connected_with_filtering ( & Block { header, txdata : vec ! [ revoked_local_txn[ 0 ] . clone( ) ] } , 1 ) ;
7785
+ let events = nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
7786
+ match events[ 0 ] {
7787
+ MessageSendEvent :: BroadcastChannelUpdate { .. } => { } ,
7788
+ _ => panic ! ( "Unexpected event" ) ,
7789
+ }
7790
+ let node_txn = nodes[ 1 ] . tx_broadcaster . txn_broadcasted . lock ( ) . unwrap ( ) ;
7791
+ let spend_txn = check_spendable_outputs ! ( nodes[ 1 ] , 1 ) ;
7792
+ assert_eq ! ( spend_txn. len( ) , 4 ) ;
7793
+ assert_eq ! ( spend_txn[ 0 ] , spend_txn[ 2 ] ) ; // to_remote output on revoked remote commitment_tx
7794
+ check_spends ! ( spend_txn[ 0 ] , revoked_local_txn[ 0 ] . clone( ) ) ;
7795
+ assert_eq ! ( spend_txn[ 1 ] , spend_txn[ 3 ] ) ; // to_local output on local commitment tx
7796
+ check_spends ! ( spend_txn[ 1 ] , node_txn[ 0 ] . clone( ) ) ;
7797
+ }
7798
+
7805
7799
#[ test]
7806
7800
fn test_static_spendable_outputs_preimage_tx ( ) {
7807
7801
let nodes = create_network ( 2 ) ;
@@ -7837,9 +7831,10 @@ mod tests {
7837
7831
assert_eq ! ( node_txn[ 0 ] . input[ 0 ] . witness. last( ) . unwrap( ) . len( ) , 133 ) ;
7838
7832
check_spends ! ( node_txn[ 1 ] , chan_1. 3 . clone( ) ) ;
7839
7833
7840
- let events = nodes[ 1 ] . chan_monitor . simple_monitor . get_and_clear_pending_events ( ) ;
7841
- let spend_tx = check_static_output ! ( events, nodes, 0 , 0 , 1 , 1 ) ;
7842
- check_spends ! ( spend_tx, node_txn[ 0 ] . clone( ) ) ;
7834
+ let spend_txn = check_spendable_outputs ! ( nodes[ 1 ] , 1 ) ; // , 0, 0, 1, 1);
7835
+ assert_eq ! ( spend_txn. len( ) , 2 ) ;
7836
+ assert_eq ! ( spend_txn[ 0 ] , spend_txn[ 1 ] ) ;
7837
+ check_spends ! ( spend_txn[ 0 ] , node_txn[ 0 ] . clone( ) ) ;
7843
7838
}
7844
7839
7845
7840
#[ test]
@@ -7869,9 +7864,10 @@ mod tests {
7869
7864
assert_eq ! ( node_txn[ 0 ] . input. len( ) , 2 ) ;
7870
7865
check_spends ! ( node_txn[ 0 ] , revoked_local_txn[ 0 ] . clone( ) ) ;
7871
7866
7872
- let events = nodes[ 1 ] . chan_monitor . simple_monitor . get_and_clear_pending_events ( ) ;
7873
- let spend_tx = check_static_output ! ( events, nodes, 0 , 0 , 1 , 1 ) ;
7874
- check_spends ! ( spend_tx, node_txn[ 0 ] . clone( ) ) ;
7867
+ let spend_txn = check_spendable_outputs ! ( nodes[ 1 ] , 1 ) ;
7868
+ assert_eq ! ( spend_txn. len( ) , 2 ) ;
7869
+ assert_eq ! ( spend_txn[ 0 ] , spend_txn[ 1 ] ) ;
7870
+ check_spends ! ( spend_txn[ 0 ] , node_txn[ 0 ] . clone( ) ) ;
7875
7871
}
7876
7872
7877
7873
#[ test]
@@ -7915,10 +7911,12 @@ mod tests {
7915
7911
assert_eq ! ( node_txn[ 3 ] . input. len( ) , 1 ) ;
7916
7912
check_spends ! ( node_txn[ 3 ] , revoked_htlc_txn[ 0 ] . clone( ) ) ;
7917
7913
7918
- let events = nodes[ 1 ] . chan_monitor . simple_monitor . get_and_clear_pending_events ( ) ;
7919
7914
// Check B's ChannelMonitor was able to generate the right spendable output descriptor
7920
- let spend_tx = check_static_output ! ( events, nodes, 1 , 1 , 1 , 1 ) ;
7921
- check_spends ! ( spend_tx, node_txn[ 3 ] . clone( ) ) ;
7915
+ let spend_txn = check_spendable_outputs ! ( nodes[ 1 ] , 1 ) ;
7916
+ assert_eq ! ( spend_txn. len( ) , 3 ) ;
7917
+ assert_eq ! ( spend_txn[ 0 ] , spend_txn[ 1 ] ) ;
7918
+ check_spends ! ( spend_txn[ 0 ] , node_txn[ 0 ] . clone( ) ) ;
7919
+ check_spends ! ( spend_txn[ 2 ] , node_txn[ 3 ] . clone( ) ) ;
7922
7920
}
7923
7921
7924
7922
#[ test]
@@ -7963,10 +7961,14 @@ mod tests {
7963
7961
assert_eq ! ( node_txn[ 3 ] . input. len( ) , 1 ) ;
7964
7962
check_spends ! ( node_txn[ 3 ] , revoked_htlc_txn[ 0 ] . clone( ) ) ;
7965
7963
7966
- let events = nodes[ 0 ] . chan_monitor . simple_monitor . get_and_clear_pending_events ( ) ;
7967
7964
// Check A's ChannelMonitor was able to generate the right spendable output descriptor
7968
- let spend_tx = check_static_output ! ( events, nodes, 1 , 2 , 1 , 0 ) ;
7969
- check_spends ! ( spend_tx, node_txn[ 3 ] . clone( ) ) ;
7965
+ let spend_txn = check_spendable_outputs ! ( nodes[ 0 ] , 1 ) ;
7966
+ assert_eq ! ( spend_txn. len( ) , 5 ) ;
7967
+ assert_eq ! ( spend_txn[ 0 ] , spend_txn[ 2 ] ) ;
7968
+ assert_eq ! ( spend_txn[ 1 ] , spend_txn[ 3 ] ) ;
7969
+ check_spends ! ( spend_txn[ 0 ] , revoked_local_txn[ 0 ] . clone( ) ) ; // spending to_remote output from revoked local tx
7970
+ check_spends ! ( spend_txn[ 1 ] , node_txn[ 2 ] . clone( ) ) ; // spending justice tx output from revoked local tx htlc received output
7971
+ check_spends ! ( spend_txn[ 4 ] , node_txn[ 3 ] . clone( ) ) ; // spending justice tx output on htlc success tx
7970
7972
}
7971
7973
7972
7974
#[ test]
@@ -8001,7 +8003,7 @@ mod tests {
8001
8003
check_spends ! ( node_txn[ 0 ] , local_txn[ 0 ] . clone( ) ) ;
8002
8004
8003
8005
// Verify that B is able to spend its own HTLC-Success tx thanks to spendable output event given back by its ChannelMonitor
8004
- let spend_txn = check_dynamic_output_p2wsh ! ( nodes[ 1 ] ) ;
8006
+ let spend_txn = check_spendable_outputs ! ( nodes[ 1 ] , 1 ) ;
8005
8007
assert_eq ! ( spend_txn. len( ) , 1 ) ;
8006
8008
check_spends ! ( spend_txn[ 0 ] , node_txn[ 0 ] . clone( ) ) ;
8007
8009
}
@@ -8032,7 +8034,7 @@ mod tests {
8032
8034
check_spends ! ( node_txn[ 0 ] , local_txn[ 0 ] . clone( ) ) ;
8033
8035
8034
8036
// Verify that A is able to spend its own HTLC-Timeout tx thanks to spendable output event given back by its ChannelMonitor
8035
- let spend_txn = check_dynamic_output_p2wsh ! ( nodes[ 0 ] ) ;
8037
+ let spend_txn = check_spendable_outputs ! ( nodes[ 0 ] , 1 ) ;
8036
8038
assert_eq ! ( spend_txn. len( ) , 4 ) ;
8037
8039
assert_eq ! ( spend_txn[ 0 ] , spend_txn[ 2 ] ) ;
8038
8040
assert_eq ! ( spend_txn[ 1 ] , spend_txn[ 3 ] ) ;
@@ -8051,13 +8053,13 @@ mod tests {
8051
8053
8052
8054
let header = BlockHeader { version : 0x20000000 , prev_blockhash : Default :: default ( ) , merkle_root : Default :: default ( ) , time : 42 , bits : 42 , nonce : 42 } ;
8053
8055
nodes[ 0 ] . chain_monitor . block_connected_with_filtering ( & Block { header, txdata : vec ! [ closing_tx. clone( ) ] } , 1 ) ;
8054
- let events = nodes[ 0 ] . chan_monitor . simple_monitor . get_and_clear_pending_events ( ) ;
8055
- let spend_tx = check_static_output ! ( events , nodes , 0 , 0 , 2 , 0 ) ;
8056
- check_spends ! ( spend_tx , closing_tx. clone( ) ) ;
8056
+ let spend_txn = check_spendable_outputs ! ( nodes[ 0 ] , 2 ) ;
8057
+ assert_eq ! ( spend_txn . len ( ) , 1 ) ;
8058
+ check_spends ! ( spend_txn [ 0 ] , closing_tx. clone( ) ) ;
8057
8059
8058
8060
nodes[ 1 ] . chain_monitor . block_connected_with_filtering ( & Block { header, txdata : vec ! [ closing_tx. clone( ) ] } , 1 ) ;
8059
- let events = nodes[ 1 ] . chan_monitor . simple_monitor . get_and_clear_pending_events ( ) ;
8060
- let spend_tx = check_static_output ! ( events , nodes , 0 , 0 , 2 , 1 ) ;
8061
- check_spends ! ( spend_tx , closing_tx) ;
8061
+ let spend_txn = check_spendable_outputs ! ( nodes[ 1 ] , 2 ) ;
8062
+ assert_eq ! ( spend_txn . len ( ) , 1 ) ;
8063
+ check_spends ! ( spend_txn [ 0 ] , closing_tx) ;
8062
8064
}
8063
8065
}
0 commit comments