@@ -15,7 +15,7 @@ use chain::Watch;
15
15
use chain:: channelmonitor;
16
16
use chain:: channelmonitor:: { ChannelMonitor , CLTV_CLAIM_BUFFER , LATENCY_GRACE_PERIOD_BLOCKS , ANTI_REORG_DELAY } ;
17
17
use chain:: transaction:: OutPoint ;
18
- use chain:: keysinterface:: { ChannelKeys , KeysInterface , SpendableOutputDescriptor } ;
18
+ use chain:: keysinterface:: { ChannelKeys , KeysInterface } ;
19
19
use ln:: channel:: { COMMITMENT_TX_BASE_WEIGHT , COMMITMENT_TX_WEIGHT_PER_HTLC } ;
20
20
use ln:: channelmanager:: { ChannelManager , ChannelManagerReadArgs , RAACommitmentOrder , PaymentPreimage , PaymentHash , PaymentSecret , PaymentSendFailure , BREAKDOWN_TIMEOUT } ;
21
21
use ln:: channel:: { Channel , ChannelError } ;
@@ -33,12 +33,8 @@ use util::config::UserConfig;
33
33
34
34
use bitcoin:: hashes:: sha256d:: Hash as Sha256dHash ;
35
35
use bitcoin:: hash_types:: { Txid , BlockHash } ;
36
- use bitcoin:: util:: bip143;
37
- use bitcoin:: util:: address:: Address ;
38
- use bitcoin:: util:: bip32:: { ChildNumber , ExtendedPubKey , ExtendedPrivKey } ;
39
36
use bitcoin:: blockdata:: block:: { Block , BlockHeader } ;
40
- use bitcoin:: blockdata:: transaction:: { Transaction , TxOut , TxIn , SigHashType } ;
41
- use bitcoin:: blockdata:: script:: { Builder , Script } ;
37
+ use bitcoin:: blockdata:: script:: Builder ;
42
38
use bitcoin:: blockdata:: opcodes;
43
39
use bitcoin:: blockdata:: constants:: genesis_block;
44
40
use bitcoin:: network:: constants:: Network ;
@@ -4655,122 +4651,27 @@ fn test_manager_serialize_deserialize_inconsistent_monitor() {
4655
4651
macro_rules! check_spendable_outputs {
4656
4652
( $node: expr, $der_idx: expr, $keysinterface: expr, $chan_value: expr) => {
4657
4653
{
4658
- let events = $node. chain_monitor. chain_monitor. get_and_clear_pending_events( ) ;
4654
+ let mut events = $node. chain_monitor. chain_monitor. get_and_clear_pending_events( ) ;
4659
4655
let mut txn = Vec :: new( ) ;
4660
- for event in events {
4656
+ let mut all_outputs = Vec :: new( ) ;
4657
+ let secp_ctx = Secp256k1 :: new( ) ;
4658
+ for event in events. drain( ..) {
4661
4659
match event {
4662
- Event :: SpendableOutputs { ref outputs } => {
4663
- for outp in outputs {
4664
- match * outp {
4665
- SpendableOutputDescriptor :: StaticOutputCounterpartyPayment { ref outpoint, ref output, ref channel_keys_id, channel_value_satoshis } => {
4666
- assert_eq!( channel_value_satoshis, $chan_value) ;
4667
- let input = TxIn {
4668
- previous_output: outpoint. into_bitcoin_outpoint( ) ,
4669
- script_sig: Script :: new( ) ,
4670
- sequence: 0 ,
4671
- witness: Vec :: new( ) ,
4672
- } ;
4673
- let outp = TxOut {
4674
- script_pubkey: Builder :: new( ) . push_opcode( opcodes:: all:: OP_RETURN ) . into_script( ) ,
4675
- value: output. value,
4676
- } ;
4677
- let mut spend_tx = Transaction {
4678
- version: 2 ,
4679
- lock_time: 0 ,
4680
- input: vec![ input] ,
4681
- output: vec![ outp] ,
4682
- } ;
4683
- spend_tx. output[ 0 ] . value -= ( spend_tx. get_weight( ) + 2 + 1 + 73 + 35 + 3 ) as u64 / 4 ; // (Max weight + 3 (to round up)) / 4
4684
- let secp_ctx = Secp256k1 :: new( ) ;
4685
- let keys = $keysinterface. derive_channel_keys( $chan_value, channel_keys_id) ;
4686
- let remotepubkey = keys. pubkeys( ) . payment_point;
4687
- let witness_script = Address :: p2pkh( & :: bitcoin:: PublicKey { compressed: true , key: remotepubkey} , Network :: Testnet ) . script_pubkey( ) ;
4688
- let sighash = Message :: from_slice( & bip143:: SigHashCache :: new( & spend_tx) . signature_hash( 0 , & witness_script, output. value, SigHashType :: All ) [ ..] ) . unwrap( ) ;
4689
- let remotesig = secp_ctx. sign( & sighash, & keys. inner. payment_key) ;
4690
- spend_tx. input[ 0 ] . witness. push( remotesig. serialize_der( ) . to_vec( ) ) ;
4691
- spend_tx. input[ 0 ] . witness[ 0 ] . push( SigHashType :: All as u8 ) ;
4692
- spend_tx. input[ 0 ] . witness. push( remotepubkey. serialize( ) . to_vec( ) ) ;
4693
- txn. push( spend_tx) ;
4694
- } ,
4695
- SpendableOutputDescriptor :: DynamicOutputP2WSH { ref outpoint, ref per_commitment_point, ref to_self_delay, ref output, ref revocation_pubkey, ref channel_keys_id, channel_value_satoshis } => {
4696
- assert_eq!( channel_value_satoshis, $chan_value) ;
4697
- let input = TxIn {
4698
- previous_output: outpoint. into_bitcoin_outpoint( ) ,
4699
- script_sig: Script :: new( ) ,
4700
- sequence: * to_self_delay as u32 ,
4701
- witness: Vec :: new( ) ,
4702
- } ;
4703
- let outp = TxOut {
4704
- script_pubkey: Builder :: new( ) . push_opcode( opcodes:: all:: OP_RETURN ) . into_script( ) ,
4705
- value: output. value,
4706
- } ;
4707
- let mut spend_tx = Transaction {
4708
- version: 2 ,
4709
- lock_time: 0 ,
4710
- input: vec![ input] ,
4711
- output: vec![ outp] ,
4712
- } ;
4713
- let secp_ctx = Secp256k1 :: new( ) ;
4714
- let keys = $keysinterface. derive_channel_keys( $chan_value, channel_keys_id) ;
4715
- if let Ok ( delayed_payment_key) = chan_utils:: derive_private_key( & secp_ctx, & per_commitment_point, & keys. inner. delayed_payment_base_key) {
4716
-
4717
- let delayed_payment_pubkey = PublicKey :: from_secret_key( & secp_ctx, & delayed_payment_key) ;
4718
- let witness_script = chan_utils:: get_revokeable_redeemscript( revocation_pubkey, * to_self_delay, & delayed_payment_pubkey) ;
4719
- spend_tx. output[ 0 ] . value -= ( spend_tx. get_weight( ) + 2 + 1 + 73 + 1 + witness_script. len( ) + 1 + 3 ) as u64 / 4 ; // (Max weight + 3 (to round up)) / 4
4720
- let sighash = Message :: from_slice( & bip143:: SigHashCache :: new( & spend_tx) . signature_hash( 0 , & witness_script, output. value, SigHashType :: All ) [ ..] ) . unwrap( ) ;
4721
- let local_delayedsig = secp_ctx. sign( & sighash, & delayed_payment_key) ;
4722
- spend_tx. input[ 0 ] . witness. push( local_delayedsig. serialize_der( ) . to_vec( ) ) ;
4723
- spend_tx. input[ 0 ] . witness[ 0 ] . push( SigHashType :: All as u8 ) ;
4724
- spend_tx. input[ 0 ] . witness. push( vec!( ) ) ; //MINIMALIF
4725
- spend_tx. input[ 0 ] . witness. push( witness_script. clone( ) . into_bytes( ) ) ;
4726
- } else { panic!( ) }
4727
- txn. push( spend_tx) ;
4728
- } ,
4729
- SpendableOutputDescriptor :: StaticOutput { ref outpoint, ref output } => {
4730
- let secp_ctx = Secp256k1 :: new( ) ;
4731
- let input = TxIn {
4732
- previous_output: outpoint. into_bitcoin_outpoint( ) ,
4733
- script_sig: Script :: new( ) ,
4734
- sequence: 0 ,
4735
- witness: Vec :: new( ) ,
4736
- } ;
4737
- let outp = TxOut {
4738
- script_pubkey: Builder :: new( ) . push_opcode( opcodes:: all:: OP_RETURN ) . into_script( ) ,
4739
- value: output. value,
4740
- } ;
4741
- let mut spend_tx = Transaction {
4742
- version: 2 ,
4743
- lock_time: 0 ,
4744
- input: vec![ input] ,
4745
- output: vec![ outp. clone( ) ] ,
4746
- } ;
4747
- spend_tx. output[ 0 ] . value -= ( spend_tx. get_weight( ) + 2 + 1 + 73 + 35 + 3 ) as u64 / 4 ; // (Max weight + 3 (to round up)) / 4
4748
- let secret = {
4749
- match ExtendedPrivKey :: new_master( Network :: Testnet , & $node. node_seed) {
4750
- Ok ( master_key) => {
4751
- match master_key. ckd_priv( & secp_ctx, ChildNumber :: from_hardened_idx( $der_idx) . expect( "key space exhausted" ) ) {
4752
- Ok ( key) => key,
4753
- Err ( _) => panic!( "Your RNG is busted" ) ,
4754
- }
4755
- }
4756
- Err ( _) => panic!( "Your rng is busted" ) ,
4757
- }
4758
- } ;
4759
- let pubkey = ExtendedPubKey :: from_private( & secp_ctx, & secret) . public_key;
4760
- let witness_script = Address :: p2pkh( & pubkey, Network :: Testnet ) . script_pubkey( ) ;
4761
- let sighash = Message :: from_slice( & bip143:: SigHashCache :: new( & spend_tx) . signature_hash( 0 , & witness_script, output. value, SigHashType :: All ) [ ..] ) . unwrap( ) ;
4762
- let sig = secp_ctx. sign( & sighash, & secret. private_key. key) ;
4763
- spend_tx. input[ 0 ] . witness. push( sig. serialize_der( ) . to_vec( ) ) ;
4764
- spend_tx. input[ 0 ] . witness[ 0 ] . push( SigHashType :: All as u8 ) ;
4765
- spend_tx. input[ 0 ] . witness. push( pubkey. key. serialize( ) . to_vec( ) ) ;
4766
- txn. push( spend_tx) ;
4767
- } ,
4768
- }
4660
+ Event :: SpendableOutputs { mut outputs } => {
4661
+ for outp in outputs. drain( ..) {
4662
+ let mut outputs = vec![ outp] ;
4663
+ txn. push( $keysinterface. backing. spend_spendable_outputs( & outputs, Vec :: new( ) , Builder :: new( ) . push_opcode( opcodes:: all:: OP_RETURN ) . into_script( ) , 253 , & secp_ctx) . unwrap( ) ) ;
4664
+ all_outputs. push( outputs. pop( ) . unwrap( ) ) ;
4769
4665
}
4770
4666
} ,
4771
4667
_ => panic!( "Unexpected event" ) ,
4772
4668
} ;
4773
4669
}
4670
+ if all_outputs. len( ) > 1 {
4671
+ if let Ok ( tx) = $keysinterface. backing. spend_spendable_outputs( & all_outputs, Vec :: new( ) , Builder :: new( ) . push_opcode( opcodes:: all:: OP_RETURN ) . into_script( ) , 253 , & secp_ctx) {
4672
+ txn. push( tx) ;
4673
+ }
4674
+ }
4774
4675
txn
4775
4676
}
4776
4677
}
@@ -4860,9 +4761,10 @@ fn test_claim_on_remote_revoked_sizeable_push_msat() {
4860
4761
connect_blocks ( & nodes[ 1 ] , ANTI_REORG_DELAY - 1 , 1 , true , header. block_hash ( ) ) ;
4861
4762
4862
4763
let spend_txn = check_spendable_outputs ! ( nodes[ 1 ] , 1 , node_cfgs[ 1 ] . keys_manager, 100000 ) ;
4863
- assert_eq ! ( spend_txn. len( ) , 2 ) ;
4764
+ assert_eq ! ( spend_txn. len( ) , 3 ) ;
4864
4765
check_spends ! ( spend_txn[ 0 ] , revoked_local_txn[ 0 ] ) ; // to_remote output on revoked remote commitment_tx
4865
4766
check_spends ! ( spend_txn[ 1 ] , node_txn[ 0 ] ) ;
4767
+ check_spends ! ( spend_txn[ 2 ] , revoked_local_txn[ 0 ] , node_txn[ 0 ] ) ; // Both outputs
4866
4768
}
4867
4769
4868
4770
#[ test]
@@ -4957,8 +4859,10 @@ fn test_static_spendable_outputs_timeout_tx() {
4957
4859
expect_payment_failed ! ( nodes[ 1 ] , our_payment_hash, true ) ;
4958
4860
4959
4861
let spend_txn = check_spendable_outputs ! ( nodes[ 1 ] , 1 , node_cfgs[ 1 ] . keys_manager, 100000 ) ;
4960
- assert_eq ! ( spend_txn. len( ) , 2 ) ; // SpendableOutput: remote_commitment_tx.to_remote, timeout_tx.output
4862
+ assert_eq ! ( spend_txn. len( ) , 3 ) ; // SpendableOutput: remote_commitment_tx.to_remote, timeout_tx.output
4863
+ check_spends ! ( spend_txn[ 0 ] , commitment_tx[ 0 ] ) ;
4961
4864
check_spends ! ( spend_txn[ 1 ] , node_txn[ 0 ] ) ;
4865
+ check_spends ! ( spend_txn[ 2 ] , node_txn[ 0 ] , commitment_tx[ 0 ] ) ; // All outputs
4962
4866
}
4963
4867
4964
4868
#[ test]
@@ -5135,11 +5039,12 @@ fn test_static_spendable_outputs_justice_tx_revoked_htlc_success_tx() {
5135
5039
5136
5040
// Check A's ChannelMonitor was able to generate the right spendable output descriptor
5137
5041
let spend_txn = check_spendable_outputs ! ( nodes[ 0 ] , 1 , node_cfgs[ 0 ] . keys_manager, 100000 ) ;
5138
- assert_eq ! ( spend_txn. len( ) , 2 ) ;
5042
+ assert_eq ! ( spend_txn. len( ) , 3 ) ;
5139
5043
assert_eq ! ( spend_txn[ 0 ] . input. len( ) , 1 ) ;
5140
5044
check_spends ! ( spend_txn[ 0 ] , revoked_local_txn[ 0 ] ) ; // spending to_remote output from revoked local tx
5141
5045
assert_ne ! ( spend_txn[ 0 ] . input[ 0 ] . previous_output, revoked_htlc_txn[ 0 ] . input[ 0 ] . previous_output) ;
5142
5046
check_spends ! ( spend_txn[ 1 ] , node_txn[ 1 ] ) ; // spending justice tx output on the htlc success tx
5047
+ check_spends ! ( spend_txn[ 2 ] , revoked_local_txn[ 0 ] , node_txn[ 1 ] ) ; // Both outputs
5143
5048
}
5144
5049
5145
5050
#[ test]
@@ -5372,6 +5277,7 @@ fn test_dynamic_spendable_outputs_local_htlc_success_tx() {
5372
5277
5373
5278
let payment_preimage = route_payment ( & nodes[ 0 ] , & vec ! ( & nodes[ 1 ] ) [ ..] , 9000000 ) . 0 ;
5374
5279
let local_txn = get_local_commitment_txn ! ( nodes[ 1 ] , chan_1. 2 ) ;
5280
+ assert_eq ! ( local_txn. len( ) , 1 ) ;
5375
5281
assert_eq ! ( local_txn[ 0 ] . input. len( ) , 1 ) ;
5376
5282
check_spends ! ( local_txn[ 0 ] , chan_1. 3 ) ;
5377
5283
@@ -5392,10 +5298,13 @@ fn test_dynamic_spendable_outputs_local_htlc_success_tx() {
5392
5298
}
5393
5299
let node_txn = {
5394
5300
let node_txn = nodes[ 1 ] . tx_broadcaster . txn_broadcasted . lock ( ) . unwrap ( ) ;
5301
+ assert_eq ! ( node_txn. len( ) , 3 ) ;
5302
+ assert_eq ! ( node_txn[ 0 ] , node_txn[ 2 ] ) ;
5303
+ assert_eq ! ( node_txn[ 1 ] , local_txn[ 0 ] ) ;
5395
5304
assert_eq ! ( node_txn[ 0 ] . input. len( ) , 1 ) ;
5396
5305
assert_eq ! ( node_txn[ 0 ] . input[ 0 ] . witness. last( ) . unwrap( ) . len( ) , ACCEPTED_HTLC_SCRIPT_WEIGHT ) ;
5397
5306
check_spends ! ( node_txn[ 0 ] , local_txn[ 0 ] ) ;
5398
- vec ! [ node_txn[ 0 ] . clone( ) , node_txn [ 2 ] . clone ( ) ]
5307
+ vec ! [ node_txn[ 0 ] . clone( ) ]
5399
5308
} ;
5400
5309
5401
5310
let header_201 = BlockHeader { version : 0x20000000 , prev_blockhash : header. block_hash ( ) , merkle_root : Default :: default ( ) , time : 42 , bits : 42 , nonce : 42 } ;
@@ -5404,9 +5313,8 @@ fn test_dynamic_spendable_outputs_local_htlc_success_tx() {
5404
5313
5405
5314
// Verify that B is able to spend its own HTLC-Success tx thanks to spendable output event given back by its ChannelMonitor
5406
5315
let spend_txn = check_spendable_outputs ! ( nodes[ 1 ] , 1 , node_cfgs[ 1 ] . keys_manager, 100000 ) ;
5407
- assert_eq ! ( spend_txn. len( ) , 2 ) ;
5316
+ assert_eq ! ( spend_txn. len( ) , 1 ) ;
5408
5317
check_spends ! ( spend_txn[ 0 ] , node_txn[ 0 ] ) ;
5409
- check_spends ! ( spend_txn[ 1 ] , node_txn[ 1 ] ) ;
5410
5318
}
5411
5319
5412
5320
fn do_test_fail_backwards_unrevoked_remote_announce ( deliver_last_raa : bool , announce_latest : bool ) {
@@ -5701,9 +5609,10 @@ fn test_dynamic_spendable_outputs_local_htlc_timeout_tx() {
5701
5609
5702
5610
// Verify that A is able to spend its own HTLC-Timeout tx thanks to spendable output event given back by its ChannelMonitor
5703
5611
let spend_txn = check_spendable_outputs ! ( nodes[ 0 ] , 1 , node_cfgs[ 0 ] . keys_manager, 100000 ) ;
5704
- assert_eq ! ( spend_txn. len( ) , 2 ) ;
5612
+ assert_eq ! ( spend_txn. len( ) , 3 ) ;
5705
5613
check_spends ! ( spend_txn[ 0 ] , local_txn[ 0 ] ) ;
5706
5614
check_spends ! ( spend_txn[ 1 ] , htlc_timeout) ;
5615
+ check_spends ! ( spend_txn[ 2 ] , local_txn[ 0 ] , htlc_timeout) ;
5707
5616
}
5708
5617
5709
5618
#[ test]
@@ -5771,9 +5680,10 @@ fn test_key_derivation_params() {
5771
5680
// Verify that A is able to spend its own HTLC-Timeout tx thanks to spendable output event given back by its ChannelMonitor
5772
5681
let new_keys_manager = test_utils:: TestKeysInterface :: new ( & seed, Network :: Testnet ) ;
5773
5682
let spend_txn = check_spendable_outputs ! ( nodes[ 0 ] , 1 , new_keys_manager, 100000 ) ;
5774
- assert_eq ! ( spend_txn. len( ) , 2 ) ;
5683
+ assert_eq ! ( spend_txn. len( ) , 3 ) ;
5775
5684
check_spends ! ( spend_txn[ 0 ] , local_txn_1[ 0 ] ) ;
5776
5685
check_spends ! ( spend_txn[ 1 ] , htlc_timeout) ;
5686
+ check_spends ! ( spend_txn[ 2 ] , local_txn_1[ 0 ] , htlc_timeout) ;
5777
5687
}
5778
5688
5779
5689
#[ test]
0 commit comments