99
1010//! The logic to build claims and bump in-flight transactions until confirmations.
1111//!
12- //! OnchainTxHandler objetcs are fully-part of ChannelMonitor and encapsulates all
12+ //! OnchainTxHandler objects are fully-part of ChannelMonitor and encapsulates all
1313//! building, tracking, bumping and notifications functions.
1414
1515use bitcoin:: blockdata:: transaction:: { Transaction , TxIn , TxOut , SigHashType } ;
@@ -24,7 +24,7 @@ use bitcoin::secp256k1;
2424use ln:: msgs:: DecodeError ;
2525use ln:: channelmanager:: PaymentPreimage ;
2626use ln:: chan_utils;
27- use ln:: chan_utils:: { TxCreationKeys , HolderCommitmentTransaction } ;
27+ use ln:: chan_utils:: { TxCreationKeys , ChannelTransactionParameters , HolderCommitmentTransaction } ;
2828use chain:: chaininterface:: { FeeEstimator , BroadcasterInterface , ConfirmationTarget , MIN_RELAY_FEE_SAT_PER_1000_WEIGHT } ;
2929use chain:: channelmonitor:: { ANTI_REORG_DELAY , CLTV_SHARED_CLAIM_BUFFER , InputMaterial , ClaimRequest } ;
3030use chain:: keysinterface:: ChannelKeys ;
@@ -244,14 +244,13 @@ pub struct OnchainTxHandler<ChanSigner: ChannelKeys> {
244244 holder_commitment : Option < HolderCommitmentTransaction > ,
245245 // holder_htlc_sigs and prev_holder_htlc_sigs are in the order as they appear in the commitment
246246 // transaction outputs (hence the Option<>s inside the Vec). The first usize is the index in
247- // the set of HTLCs in the HolderCommitmentTransaction (including those which do not appear in
248- // the commitment transaction).
247+ // the set of HTLCs in the HolderCommitmentTransaction.
249248 holder_htlc_sigs : Option < Vec < Option < ( usize , Signature ) > > > ,
250249 prev_holder_commitment : Option < HolderCommitmentTransaction > ,
251250 prev_holder_htlc_sigs : Option < Vec < Option < ( usize , Signature ) > > > ,
252- on_holder_tx_csv : u16 ,
253251
254252 key_storage : ChanSigner ,
253+ pub ( crate ) channel_transaction_parameters : ChannelTransactionParameters ,
255254
256255 // Used to track claiming requests. If claim tx doesn't confirm before height timer expiration we need to bump
257256 // it (RBF or CPFP). If an input has been part of an aggregate tx at first claim try, we need to keep it within
@@ -295,9 +294,8 @@ impl<ChanSigner: ChannelKeys + Writeable> OnchainTxHandler<ChanSigner> {
295294 self . prev_holder_commitment . write ( writer) ?;
296295 self . prev_holder_htlc_sigs . write ( writer) ?;
297296
298- self . on_holder_tx_csv . write ( writer) ?;
299-
300297 self . key_storage . write ( writer) ?;
298+ self . channel_transaction_parameters . write ( writer) ?;
301299
302300 writer. write_all ( & byte_utils:: be64_to_array ( self . pending_claim_requests . len ( ) as u64 ) ) ?;
303301 for ( ref ancestor_claim_txid, claim_tx_data) in self . pending_claim_requests . iter ( ) {
@@ -344,9 +342,8 @@ impl<ChanSigner: ChannelKeys + Readable> Readable for OnchainTxHandler<ChanSigne
344342 let prev_holder_commitment = Readable :: read ( reader) ?;
345343 let prev_holder_htlc_sigs = Readable :: read ( reader) ?;
346344
347- let on_holder_tx_csv = Readable :: read ( reader) ?;
348-
349345 let key_storage = Readable :: read ( reader) ?;
346+ let channel_parameters = Readable :: read ( reader) ?;
350347
351348 let pending_claim_requests_len: u64 = Readable :: read ( reader) ?;
352349 let mut pending_claim_requests = HashMap :: with_capacity ( cmp:: min ( pending_claim_requests_len as usize , MAX_ALLOC_SIZE / 128 ) ) ;
@@ -398,8 +395,8 @@ impl<ChanSigner: ChannelKeys + Readable> Readable for OnchainTxHandler<ChanSigne
398395 holder_htlc_sigs,
399396 prev_holder_commitment,
400397 prev_holder_htlc_sigs,
401- on_holder_tx_csv,
402398 key_storage,
399+ channel_transaction_parameters : channel_parameters,
403400 claimable_outpoints,
404401 pending_claim_requests,
405402 onchain_events_waiting_threshold_conf,
@@ -410,7 +407,7 @@ impl<ChanSigner: ChannelKeys + Readable> Readable for OnchainTxHandler<ChanSigne
410407}
411408
412409impl < ChanSigner : ChannelKeys > OnchainTxHandler < ChanSigner > {
413- pub ( crate ) fn new ( destination_script : Script , keys : ChanSigner , on_holder_tx_csv : u16 ) -> Self {
410+ pub ( crate ) fn new ( destination_script : Script , keys : ChanSigner , channel_parameters : ChannelTransactionParameters ) -> Self {
414411
415412 let key_storage = keys;
416413
@@ -420,8 +417,8 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
420417 holder_htlc_sigs : None ,
421418 prev_holder_commitment : None ,
422419 prev_holder_htlc_sigs : None ,
423- on_holder_tx_csv,
424420 key_storage,
421+ channel_transaction_parameters : channel_parameters,
425422 pending_claim_requests : HashMap :: new ( ) ,
426423 claimable_outpoints : HashMap :: new ( ) ,
427424 onchain_events_waiting_threshold_conf : HashMap :: new ( ) ,
@@ -654,7 +651,7 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
654651 let signed_tx = self . get_fully_signed_holder_tx ( funding_redeemscript) . unwrap ( ) ;
655652 // Timer set to $NEVER given we can't bump tx without anchor outputs
656653 log_trace ! ( logger, "Going to broadcast Holder Transaction {} claiming funding output {} from {}..." , signed_tx. txid( ) , outp. vout, outp. txid) ;
657- return Some ( ( None , self . holder_commitment . as_ref ( ) . unwrap ( ) . feerate_per_kw , signed_tx) ) ;
654+ return Some ( ( None , self . holder_commitment . as_ref ( ) . unwrap ( ) . feerate_per_kw ( ) , signed_tx) ) ;
658655 }
659656 _ => unreachable ! ( )
660657 }
@@ -899,44 +896,39 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
899896 fn sign_latest_holder_htlcs ( & mut self ) {
900897 if let Some ( ref holder_commitment) = self . holder_commitment {
901898 if let Ok ( sigs) = self . key_storage . sign_holder_commitment_htlc_transactions ( holder_commitment, & self . secp_ctx ) {
902- self . holder_htlc_sigs = Some ( Vec :: new ( ) ) ;
903- let ret = self . holder_htlc_sigs . as_mut ( ) . unwrap ( ) ;
904- for ( htlc_idx, ( holder_sig, & ( ref htlc, _) ) ) in sigs. iter ( ) . zip ( holder_commitment. per_htlc . iter ( ) ) . enumerate ( ) {
905- if let Some ( tx_idx) = htlc. transaction_output_index {
906- if ret. len ( ) <= tx_idx as usize { ret. resize ( tx_idx as usize + 1 , None ) ; }
907- ret[ tx_idx as usize ] = Some ( ( htlc_idx, holder_sig. expect ( "Did not receive a signature for a non-dust HTLC" ) ) ) ;
908- } else {
909- assert ! ( holder_sig. is_none( ) , "Received a signature for a dust HTLC" ) ;
910- }
911- }
899+ self . holder_htlc_sigs = Some ( Self :: extract_holder_sigs ( holder_commitment, sigs) ) ;
912900 }
913901 }
914902 }
903+
915904 fn sign_prev_holder_htlcs ( & mut self ) {
916905 if let Some ( ref holder_commitment) = self . prev_holder_commitment {
917906 if let Ok ( sigs) = self . key_storage . sign_holder_commitment_htlc_transactions ( holder_commitment, & self . secp_ctx ) {
918- self . prev_holder_htlc_sigs = Some ( Vec :: new ( ) ) ;
919- let ret = self . prev_holder_htlc_sigs . as_mut ( ) . unwrap ( ) ;
920- for ( htlc_idx, ( holder_sig, & ( ref htlc, _) ) ) in sigs. iter ( ) . zip ( holder_commitment. per_htlc . iter ( ) ) . enumerate ( ) {
921- if let Some ( tx_idx) = htlc. transaction_output_index {
922- if ret. len ( ) <= tx_idx as usize { ret. resize ( tx_idx as usize + 1 , None ) ; }
923- ret[ tx_idx as usize ] = Some ( ( htlc_idx, holder_sig. expect ( "Did not receive a signature for a non-dust HTLC" ) ) ) ;
924- } else {
925- assert ! ( holder_sig. is_none( ) , "Received a signature for a dust HTLC" ) ;
926- }
927- }
907+ self . prev_holder_htlc_sigs = Some ( Self :: extract_holder_sigs ( holder_commitment, sigs) ) ;
928908 }
929909 }
930910 }
931911
932- //TODO: getting lastest holder transactions should be infaillible and result in us "force-closing the channel", but we may
912+ fn extract_holder_sigs ( holder_commitment : & HolderCommitmentTransaction , sigs : Vec < Signature > ) -> Vec < Option < ( usize , Signature ) > > {
913+ let mut ret = Vec :: new ( ) ;
914+ for ( htlc_idx, ( holder_sig, htlc) ) in sigs. iter ( ) . zip ( holder_commitment. htlcs ( ) . iter ( ) ) . enumerate ( ) {
915+ let tx_idx = htlc. transaction_output_index . unwrap ( ) ;
916+ if ret. len ( ) <= tx_idx as usize { ret. resize ( tx_idx as usize + 1 , None ) ; }
917+ ret[ tx_idx as usize ] = Some ( ( htlc_idx, holder_sig. clone ( ) ) ) ;
918+ }
919+ ret
920+ }
921+
922+ //TODO: getting lastest holder transactions should be infallible and result in us "force-closing the channel", but we may
933923 // have empty holder commitment transaction if a ChannelMonitor is asked to force-close just after Channel::get_outbound_funding_created,
934924 // before providing a initial commitment transaction. For outbound channel, init ChannelMonitor at Channel::funding_signed, there is nothing
935925 // to monitor before.
936926 pub ( crate ) fn get_fully_signed_holder_tx ( & mut self , funding_redeemscript : & Script ) -> Option < Transaction > {
937927 if let Some ( ref mut holder_commitment) = self . holder_commitment {
938- match self . key_storage . sign_holder_commitment ( holder_commitment, & self . secp_ctx ) {
939- Ok ( sig) => Some ( holder_commitment. add_holder_sig ( funding_redeemscript, sig) ) ,
928+ match self . key_storage . sign_holder_commitment ( & holder_commitment, & self . secp_ctx ) {
929+ Ok ( sig) => {
930+ Some ( holder_commitment. add_holder_sig ( funding_redeemscript, sig) )
931+ } ,
940932 Err ( _) => return None ,
941933 }
942934 } else {
@@ -947,9 +939,10 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
947939 #[ cfg( any( test, feature="unsafe_revoked_tx_signing" ) ) ]
948940 pub ( crate ) fn get_fully_signed_copy_holder_tx ( & mut self , funding_redeemscript : & Script ) -> Option < Transaction > {
949941 if let Some ( ref mut holder_commitment) = self . holder_commitment {
950- let holder_commitment = holder_commitment. clone ( ) ;
951- match self . key_storage . sign_holder_commitment ( & holder_commitment, & self . secp_ctx ) {
952- Ok ( sig) => Some ( holder_commitment. add_holder_sig ( funding_redeemscript, sig) ) ,
942+ match self . key_storage . sign_holder_commitment ( holder_commitment, & self . secp_ctx ) {
943+ Ok ( sig) => {
944+ Some ( holder_commitment. add_holder_sig ( funding_redeemscript, sig) )
945+ } ,
953946 Err ( _) => return None ,
954947 }
955948 } else {
@@ -960,24 +953,30 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
960953 pub ( crate ) fn get_fully_signed_htlc_tx ( & mut self , outp : & :: bitcoin:: OutPoint , preimage : & Option < PaymentPreimage > ) -> Option < Transaction > {
961954 let mut htlc_tx = None ;
962955 if self . holder_commitment . is_some ( ) {
963- let commitment_txid = self . holder_commitment . as_ref ( ) . unwrap ( ) . txid ( ) ;
956+ let commitment_txid = self . holder_commitment . as_ref ( ) . unwrap ( ) . trust ( ) . txid ( ) ;
964957 if commitment_txid == outp. txid {
965958 self . sign_latest_holder_htlcs ( ) ;
966959 if let & Some ( ref htlc_sigs) = & self . holder_htlc_sigs {
967960 let & ( ref htlc_idx, ref htlc_sig) = htlc_sigs[ outp. vout as usize ] . as_ref ( ) . unwrap ( ) ;
968- htlc_tx = Some ( self . holder_commitment . as_ref ( ) . unwrap ( )
969- . get_signed_htlc_tx ( * htlc_idx, htlc_sig, preimage, self . on_holder_tx_csv ) ) ;
961+ let holder_commitment = self . holder_commitment . as_ref ( ) . unwrap ( ) ;
962+ let trusted_tx = holder_commitment. trust ( ) ;
963+ let counterparty_htlc_sig = holder_commitment. counterparty_htlc_sigs [ * htlc_idx] ;
964+ htlc_tx = Some ( trusted_tx
965+ . get_signed_htlc_tx ( & self . channel_transaction_parameters . as_holder_broadcastable ( ) , * htlc_idx, & counterparty_htlc_sig, htlc_sig, preimage) ) ;
970966 }
971967 }
972968 }
973969 if self . prev_holder_commitment . is_some ( ) {
974- let commitment_txid = self . prev_holder_commitment . as_ref ( ) . unwrap ( ) . txid ( ) ;
970+ let commitment_txid = self . prev_holder_commitment . as_ref ( ) . unwrap ( ) . trust ( ) . txid ( ) ;
975971 if commitment_txid == outp. txid {
976972 self . sign_prev_holder_htlcs ( ) ;
977973 if let & Some ( ref htlc_sigs) = & self . prev_holder_htlc_sigs {
978974 let & ( ref htlc_idx, ref htlc_sig) = htlc_sigs[ outp. vout as usize ] . as_ref ( ) . unwrap ( ) ;
979- htlc_tx = Some ( self . prev_holder_commitment . as_ref ( ) . unwrap ( )
980- . get_signed_htlc_tx ( * htlc_idx, htlc_sig, preimage, self . on_holder_tx_csv ) ) ;
975+ let holder_commitment = self . prev_holder_commitment . as_ref ( ) . unwrap ( ) ;
976+ let trusted_tx = holder_commitment. trust ( ) ;
977+ let counterparty_htlc_sig = holder_commitment. counterparty_htlc_sigs [ * htlc_idx] ;
978+ htlc_tx = Some ( trusted_tx
979+ . get_signed_htlc_tx ( & self . channel_transaction_parameters . as_holder_broadcastable ( ) , * htlc_idx, & counterparty_htlc_sig, htlc_sig, preimage) ) ;
981980 }
982981 }
983982 }
0 commit comments