@@ -4,7 +4,7 @@ use bitcoin::blockdata::transaction::{TxIn, TxOut, Transaction, SigHashType};
4
4
use bitcoin:: blockdata:: opcodes;
5
5
use bitcoin:: util:: hash:: BitcoinHash ;
6
6
use bitcoin:: util:: bip143;
7
- use bitcoin:: consensus:: encode:: { self , Encodable , Decodable } ;
7
+ use bitcoin:: consensus:: encode;
8
8
9
9
use bitcoin_hashes:: { Hash , HashEngine } ;
10
10
use bitcoin_hashes:: sha256:: Hash as Sha256 ;
@@ -25,7 +25,7 @@ use chain::chaininterface::{FeeEstimator,ConfirmationTarget};
25
25
use chain:: transaction:: OutPoint ;
26
26
use chain:: keysinterface:: { ChannelKeys , KeysInterface } ;
27
27
use util:: transaction_utils;
28
- use util:: ser:: { Readable , ReadableArgs , Writeable , Writer , WriterWriteAdaptor } ;
28
+ use util:: ser:: { Readable , ReadableArgs , Writeable , Writer } ;
29
29
use util:: logger:: { Logger , LogHolder } ;
30
30
use util:: errors:: APIError ;
31
31
use util:: config:: { UserConfig , ChannelConfig } ;
@@ -297,12 +297,6 @@ pub(super) struct Channel<ChanSigner: ChannelKeys> {
297
297
/// Max to_local and to_remote outputs in a remote-generated commitment transaction
298
298
max_commitment_tx_output_remote : :: std:: sync:: Mutex < ( u64 , u64 ) > ,
299
299
300
- #[ cfg( test) ]
301
- // Used in ChannelManager's tests to send a revoked transaction
302
- pub last_local_commitment_txn : Vec < Transaction > ,
303
- #[ cfg( not( test) ) ]
304
- last_local_commitment_txn : Vec < Transaction > ,
305
-
306
300
last_sent_closing_fee : Option < ( u64 , u64 , Signature ) > , // (feerate, fee, our_sig)
307
301
308
302
/// The hash of the block in which the funding transaction reached our CONF_TARGET. We use this
@@ -498,8 +492,6 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
498
492
#[ cfg( debug_assertions) ]
499
493
max_commitment_tx_output_remote : :: std:: sync:: Mutex :: new ( ( channel_value_satoshis * 1000 - push_msat, push_msat) ) ,
500
494
501
- last_local_commitment_txn : Vec :: new ( ) ,
502
-
503
495
last_sent_closing_fee : None ,
504
496
505
497
funding_tx_confirmed_in : None ,
@@ -716,8 +708,6 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
716
708
#[ cfg( debug_assertions) ]
717
709
max_commitment_tx_output_remote : :: std:: sync:: Mutex :: new ( ( msg. push_msat , msg. funding_satoshis * 1000 - msg. push_msat ) ) ,
718
710
719
- last_local_commitment_txn : Vec :: new ( ) ,
720
-
721
711
last_sent_closing_fee : None ,
722
712
723
713
funding_tx_confirmed_in : None ,
@@ -1185,8 +1175,10 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
1185
1175
Ok ( ( htlc_redeemscript, self . secp_ctx . sign ( & sighash, & our_htlc_key) , is_local_tx) )
1186
1176
}
1187
1177
1178
+ #[ cfg( test) ]
1188
1179
/// Signs a transaction created by build_htlc_transaction. If the transaction is an
1189
1180
/// HTLC-Success transaction (ie htlc.offered is false), preimage must be set!
1181
+ /// TODO: Make this a chan_utils, use it in channelmonitor and tests, cause its unused now
1190
1182
fn sign_htlc_transaction ( & self , tx : & mut Transaction , their_sig : & Signature , preimage : & Option < PaymentPreimage > , htlc : & HTLCOutputInCommitment , keys : & TxCreationKeys ) -> Result < Signature , ChannelError > {
1191
1183
if tx. input . len ( ) != 1 {
1192
1184
panic ! ( "Tried to sign HTLC transaction that had input count != 1!" ) ;
@@ -1556,7 +1548,6 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
1556
1548
// Now that we're past error-generating stuff, update our local state:
1557
1549
1558
1550
self . channel_monitor . provide_latest_remote_commitment_tx_info ( & remote_initial_commitment_tx, Vec :: new ( ) , self . cur_remote_commitment_transaction_number , self . their_cur_commitment_point . unwrap ( ) ) ;
1559
- self . last_local_commitment_txn = vec ! [ local_initial_commitment_tx. clone( ) ] ;
1560
1551
self . channel_monitor . provide_latest_local_commitment_tx_info ( local_initial_commitment_tx, local_keys, self . feerate_per_kw , Vec :: new ( ) ) ;
1561
1552
self . channel_state = ChannelState :: FundingSent as u32 ;
1562
1553
self . channel_id = funding_txo. to_channel_id ( ) ;
@@ -1594,8 +1585,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
1594
1585
secp_check ! ( self . secp_ctx. verify( & local_sighash, & msg. signature, & self . their_funding_pubkey. unwrap( ) ) , "Invalid funding_signed signature from peer" ) ;
1595
1586
1596
1587
self . sign_commitment_transaction ( & mut local_initial_commitment_tx, & msg. signature ) ;
1597
- self . channel_monitor . provide_latest_local_commitment_tx_info ( local_initial_commitment_tx. clone ( ) , local_keys, self . feerate_per_kw , Vec :: new ( ) ) ;
1598
- self . last_local_commitment_txn = vec ! [ local_initial_commitment_tx] ;
1588
+ self . channel_monitor . provide_latest_local_commitment_tx_info ( local_initial_commitment_tx, local_keys, self . feerate_per_kw , Vec :: new ( ) ) ;
1599
1589
self . channel_state = ChannelState :: FundingSent as u32 | ( self . channel_state & ( ChannelState :: MonitorUpdateFailed as u32 ) ) ;
1600
1590
self . cur_local_commitment_transaction_number -= 1 ;
1601
1591
@@ -1859,25 +1849,17 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
1859
1849
return Err ( ChannelError :: Close ( "Got wrong number of HTLC signatures from remote" ) ) ;
1860
1850
}
1861
1851
1862
- let mut new_local_commitment_txn = Vec :: with_capacity ( local_commitment_tx. 1 + 1 ) ;
1863
1852
self . sign_commitment_transaction ( & mut local_commitment_tx. 0 , & msg. signature ) ;
1864
- new_local_commitment_txn. push ( local_commitment_tx. 0 . clone ( ) ) ;
1865
1853
1866
1854
let mut htlcs_and_sigs = Vec :: with_capacity ( local_commitment_tx. 2 . len ( ) ) ;
1867
1855
for ( idx, ( htlc, source) ) in local_commitment_tx. 2 . drain ( ..) . enumerate ( ) {
1868
1856
if let Some ( _) = htlc. transaction_output_index {
1869
- let mut htlc_tx = self . build_htlc_transaction ( & local_commitment_txid, & htlc, true , & local_keys, feerate_per_kw) ;
1857
+ let htlc_tx = self . build_htlc_transaction ( & local_commitment_txid, & htlc, true , & local_keys, feerate_per_kw) ;
1870
1858
let htlc_redeemscript = chan_utils:: get_htlc_redeemscript ( & htlc, & local_keys) ;
1871
1859
log_trace ! ( self , "Checking HTLC tx signature {} by key {} against tx {} with redeemscript {}" , log_bytes!( msg. htlc_signatures[ idx] . serialize_compact( ) [ ..] ) , log_bytes!( local_keys. b_htlc_key. serialize( ) ) , encode:: serialize_hex( & htlc_tx) , encode:: serialize_hex( & htlc_redeemscript) ) ;
1872
1860
let htlc_sighash = hash_to_message ! ( & bip143:: SighashComponents :: new( & htlc_tx) . sighash_all( & htlc_tx. input[ 0 ] , & htlc_redeemscript, htlc. amount_msat / 1000 ) [ ..] ) ;
1873
1861
secp_check ! ( self . secp_ctx. verify( & htlc_sighash, & msg. htlc_signatures[ idx] , & local_keys. b_htlc_key) , "Invalid HTLC tx signature from peer" ) ;
1874
- let htlc_sig = if htlc. offered {
1875
- let htlc_sig = self . sign_htlc_transaction ( & mut htlc_tx, & msg. htlc_signatures [ idx] , & None , & htlc, & local_keys) ?;
1876
- new_local_commitment_txn. push ( htlc_tx) ;
1877
- htlc_sig
1878
- } else {
1879
- self . create_htlc_tx_signature ( & htlc_tx, & htlc, & local_keys) ?. 1
1880
- } ;
1862
+ let htlc_sig = self . create_htlc_tx_signature ( & htlc_tx, & htlc, & local_keys) ?. 1 ;
1881
1863
htlcs_and_sigs. push ( ( htlc, Some ( ( msg. htlc_signatures [ idx] , htlc_sig) ) , source) ) ;
1882
1864
} else {
1883
1865
htlcs_and_sigs. push ( ( htlc, None , source) ) ;
@@ -1923,7 +1905,6 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
1923
1905
}
1924
1906
1925
1907
self . cur_local_commitment_transaction_number -= 1 ;
1926
- self . last_local_commitment_txn = new_local_commitment_txn;
1927
1908
// Note that if we need_our_commitment & !AwaitingRemoteRevoke we'll call
1928
1909
// send_commitment_no_status_check() next which will reset this to RAAFirst.
1929
1910
self . resend_order = RAACommitmentOrder :: CommitmentFirst ;
@@ -2881,11 +2862,11 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
2881
2862
}
2882
2863
2883
2864
/// May only be called after funding has been initiated (ie is_funding_initiated() is true)
2884
- pub fn channel_monitor ( & self ) -> ChannelMonitor {
2865
+ pub fn channel_monitor ( & self ) -> & ChannelMonitor {
2885
2866
if self . channel_state < ChannelState :: FundingCreated as u32 {
2886
2867
panic ! ( "Can't get a channel monitor until funding has been created" ) ;
2887
2868
}
2888
- self . channel_monitor . clone ( )
2869
+ & self . channel_monitor
2889
2870
}
2890
2871
2891
2872
/// Guaranteed to be Some after both FundingLocked messages have been exchanged (and, thus,
@@ -3681,9 +3662,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
3681
3662
3682
3663
self . channel_state = ChannelState :: ShutdownComplete as u32 ;
3683
3664
self . channel_update_count += 1 ;
3684
- let mut res = Vec :: new ( ) ;
3685
- mem:: swap ( & mut res, & mut self . last_local_commitment_txn ) ;
3686
- ( res, dropped_outbound_htlcs)
3665
+ ( self . channel_monitor . get_latest_local_commitment_txn ( ) , dropped_outbound_htlcs)
3687
3666
}
3688
3667
}
3689
3668
@@ -3873,16 +3852,6 @@ impl<ChanSigner: ChannelKeys + Writeable> Writeable for Channel<ChanSigner> {
3873
3852
self . channel_update_count . write ( writer) ?;
3874
3853
self . feerate_per_kw . write ( writer) ?;
3875
3854
3876
- ( self . last_local_commitment_txn . len ( ) as u64 ) . write ( writer) ?;
3877
- for tx in self . last_local_commitment_txn . iter ( ) {
3878
- if let Err ( e) = tx. consensus_encode ( & mut WriterWriteAdaptor ( writer) ) {
3879
- match e {
3880
- encode:: Error :: Io ( e) => return Err ( e) ,
3881
- _ => panic ! ( "last_local_commitment_txn must have been well-formed!" ) ,
3882
- }
3883
- }
3884
- }
3885
-
3886
3855
match self . last_sent_closing_fee {
3887
3856
Some ( ( feerate, fee, sig) ) => {
3888
3857
1u8 . write ( writer) ?;
@@ -4041,15 +4010,6 @@ impl<R : ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>> ReadableArgs<R,
4041
4010
let channel_update_count = Readable :: read ( reader) ?;
4042
4011
let feerate_per_kw = Readable :: read ( reader) ?;
4043
4012
4044
- let last_local_commitment_txn_count: u64 = Readable :: read ( reader) ?;
4045
- let mut last_local_commitment_txn = Vec :: with_capacity ( cmp:: min ( last_local_commitment_txn_count as usize , OUR_MAX_HTLCS as usize * 2 + 1 ) ) ;
4046
- for _ in 0 ..last_local_commitment_txn_count {
4047
- last_local_commitment_txn. push ( match Transaction :: consensus_decode ( reader. by_ref ( ) ) {
4048
- Ok ( tx) => tx,
4049
- Err ( _) => return Err ( DecodeError :: InvalidValue ) ,
4050
- } ) ;
4051
- }
4052
-
4053
4013
let last_sent_closing_fee = match <u8 as Readable < R > >:: read ( reader) ? {
4054
4014
0 => None ,
4055
4015
1 => Some ( ( Readable :: read ( reader) ?, Readable :: read ( reader) ?, Readable :: read ( reader) ?) ) ,
@@ -4132,8 +4092,6 @@ impl<R : ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>> ReadableArgs<R,
4132
4092
#[ cfg( debug_assertions) ]
4133
4093
max_commitment_tx_output_remote : :: std:: sync:: Mutex :: new ( ( 0 , 0 ) ) ,
4134
4094
4135
- last_local_commitment_txn,
4136
-
4137
4095
last_sent_closing_fee,
4138
4096
4139
4097
funding_tx_confirmed_in,
0 commit comments