9
9
10
10
//! The logic to build claims and bump in-flight transactions until confirmations.
11
11
//!
12
- //! OnchainTxHandler objetcs are fully-part of ChannelMonitor and encapsulates all
12
+ //! OnchainTxHandler objects are fully-part of ChannelMonitor and encapsulates all
13
13
//! building, tracking, bumping and notifications functions.
14
14
15
15
use bitcoin:: blockdata:: transaction:: { Transaction , TxIn , TxOut , SigHashType } ;
@@ -24,7 +24,7 @@ use bitcoin::secp256k1;
24
24
use ln:: msgs:: DecodeError ;
25
25
use ln:: channelmanager:: PaymentPreimage ;
26
26
use ln:: chan_utils;
27
- use ln:: chan_utils:: { TxCreationKeys , HolderCommitmentTransaction } ;
27
+ use ln:: chan_utils:: { TxCreationKeys , ChannelTransactionParameters , HolderCommitmentTransaction } ;
28
28
use chain:: chaininterface:: { FeeEstimator , BroadcasterInterface , ConfirmationTarget , MIN_RELAY_FEE_SAT_PER_1000_WEIGHT } ;
29
29
use chain:: channelmonitor:: { ANTI_REORG_DELAY , CLTV_SHARED_CLAIM_BUFFER , InputMaterial , ClaimRequest } ;
30
30
use chain:: keysinterface:: ChannelKeys ;
@@ -244,14 +244,13 @@ pub struct OnchainTxHandler<ChanSigner: ChannelKeys> {
244
244
holder_commitment : Option < HolderCommitmentTransaction > ,
245
245
// holder_htlc_sigs and prev_holder_htlc_sigs are in the order as they appear in the commitment
246
246
// 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.
249
248
holder_htlc_sigs : Option < Vec < Option < ( usize , Signature ) > > > ,
250
249
prev_holder_commitment : Option < HolderCommitmentTransaction > ,
251
250
prev_holder_htlc_sigs : Option < Vec < Option < ( usize , Signature ) > > > ,
252
- on_holder_tx_csv : u16 ,
253
251
254
252
key_storage : ChanSigner ,
253
+ pub ( crate ) channel_transaction_parameters : ChannelTransactionParameters ,
255
254
256
255
// Used to track claiming requests. If claim tx doesn't confirm before height timer expiration we need to bump
257
256
// 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> {
295
294
self . prev_holder_commitment . write ( writer) ?;
296
295
self . prev_holder_htlc_sigs . write ( writer) ?;
297
296
298
- self . on_holder_tx_csv . write ( writer) ?;
299
-
300
297
self . key_storage . write ( writer) ?;
298
+ self . channel_transaction_parameters . write ( writer) ?;
301
299
302
300
writer. write_all ( & byte_utils:: be64_to_array ( self . pending_claim_requests . len ( ) as u64 ) ) ?;
303
301
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
344
342
let prev_holder_commitment = Readable :: read ( reader) ?;
345
343
let prev_holder_htlc_sigs = Readable :: read ( reader) ?;
346
344
347
- let on_holder_tx_csv = Readable :: read ( reader) ?;
348
-
349
345
let key_storage = Readable :: read ( reader) ?;
346
+ let channel_parameters = Readable :: read ( reader) ?;
350
347
351
348
let pending_claim_requests_len: u64 = Readable :: read ( reader) ?;
352
349
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
398
395
holder_htlc_sigs,
399
396
prev_holder_commitment,
400
397
prev_holder_htlc_sigs,
401
- on_holder_tx_csv,
402
398
key_storage,
399
+ channel_transaction_parameters : channel_parameters,
403
400
claimable_outpoints,
404
401
pending_claim_requests,
405
402
onchain_events_waiting_threshold_conf,
@@ -410,7 +407,7 @@ impl<ChanSigner: ChannelKeys + Readable> Readable for OnchainTxHandler<ChanSigne
410
407
}
411
408
412
409
impl < 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 {
414
411
415
412
let key_storage = keys;
416
413
@@ -420,8 +417,8 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
420
417
holder_htlc_sigs : None ,
421
418
prev_holder_commitment : None ,
422
419
prev_holder_htlc_sigs : None ,
423
- on_holder_tx_csv,
424
420
key_storage,
421
+ channel_transaction_parameters : channel_parameters,
425
422
pending_claim_requests : HashMap :: new ( ) ,
426
423
claimable_outpoints : HashMap :: new ( ) ,
427
424
onchain_events_waiting_threshold_conf : HashMap :: new ( ) ,
@@ -654,7 +651,7 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
654
651
let signed_tx = self . get_fully_signed_holder_tx ( funding_redeemscript) . unwrap ( ) ;
655
652
// Timer set to $NEVER given we can't bump tx without anchor outputs
656
653
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) ) ;
658
655
}
659
656
_ => unreachable ! ( )
660
657
}
@@ -899,44 +896,39 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
899
896
fn sign_latest_holder_htlcs ( & mut self ) {
900
897
if let Some ( ref holder_commitment) = self . holder_commitment {
901
898
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) ) ;
912
900
}
913
901
}
914
902
}
903
+
915
904
fn sign_prev_holder_htlcs ( & mut self ) {
916
905
if let Some ( ref holder_commitment) = self . prev_holder_commitment {
917
906
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) ) ;
928
908
}
929
909
}
930
910
}
931
911
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
933
923
// have empty holder commitment transaction if a ChannelMonitor is asked to force-close just after Channel::get_outbound_funding_created,
934
924
// before providing a initial commitment transaction. For outbound channel, init ChannelMonitor at Channel::funding_signed, there is nothing
935
925
// to monitor before.
936
926
pub ( crate ) fn get_fully_signed_holder_tx ( & mut self , funding_redeemscript : & Script ) -> Option < Transaction > {
937
927
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
+ } ,
940
932
Err ( _) => return None ,
941
933
}
942
934
} else {
@@ -947,9 +939,10 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
947
939
#[ cfg( any( test, feature="unsafe_revoked_tx_signing" ) ) ]
948
940
pub ( crate ) fn get_fully_signed_copy_holder_tx ( & mut self , funding_redeemscript : & Script ) -> Option < Transaction > {
949
941
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
+ } ,
953
946
Err ( _) => return None ,
954
947
}
955
948
} else {
@@ -960,24 +953,30 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
960
953
pub ( crate ) fn get_fully_signed_htlc_tx ( & mut self , outp : & :: bitcoin:: OutPoint , preimage : & Option < PaymentPreimage > ) -> Option < Transaction > {
961
954
let mut htlc_tx = None ;
962
955
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 ( ) ;
964
957
if commitment_txid == outp. txid {
965
958
self . sign_latest_holder_htlcs ( ) ;
966
959
if let & Some ( ref htlc_sigs) = & self . holder_htlc_sigs {
967
960
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) ) ;
970
966
}
971
967
}
972
968
}
973
969
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 ( ) ;
975
971
if commitment_txid == outp. txid {
976
972
self . sign_prev_holder_htlcs ( ) ;
977
973
if let & Some ( ref htlc_sigs) = & self . prev_holder_htlc_sigs {
978
974
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) ) ;
981
980
}
982
981
}
983
982
}
0 commit comments