@@ -35,6 +35,7 @@ use util::byte_utils;
35
35
use std:: collections:: { HashMap , hash_map} ;
36
36
use std:: cmp;
37
37
use std:: ops:: Deref ;
38
+ use std:: mem:: replace;
38
39
39
40
const MAX_ALLOC_SIZE : usize = 64 * 1024 ;
40
41
@@ -241,7 +242,7 @@ impl Writeable for Option<Vec<Option<(usize, Signature)>>> {
241
242
/// do RBF bumping if possible.
242
243
pub struct OnchainTxHandler < ChanSigner : ChannelKeys > {
243
244
destination_script : Script ,
244
- holder_commitment : Option < HolderCommitmentTransaction > ,
245
+ holder_commitment : HolderCommitmentTransaction ,
245
246
// holder_htlc_sigs and prev_holder_htlc_sigs are in the order as they appear in the commitment
246
247
// transaction outputs (hence the Option<>s inside the Vec). The first usize is the index in
247
248
// the set of HTLCs in the HolderCommitmentTransaction.
@@ -423,13 +424,13 @@ impl<'a, K: KeysInterface> ReadableArgs<&'a K> for OnchainTxHandler<K::ChanKeySi
423
424
}
424
425
425
426
impl < ChanSigner : ChannelKeys > OnchainTxHandler < ChanSigner > {
426
- pub ( crate ) fn new ( destination_script : Script , keys : ChanSigner , channel_parameters : ChannelTransactionParameters ) -> Self {
427
+ pub ( crate ) fn new ( destination_script : Script , keys : ChanSigner , channel_parameters : ChannelTransactionParameters , holder_commitment : HolderCommitmentTransaction ) -> Self {
427
428
428
429
let key_storage = keys;
429
430
430
431
OnchainTxHandler {
431
432
destination_script,
432
- holder_commitment : None ,
433
+ holder_commitment,
433
434
holder_htlc_sigs : None ,
434
435
prev_holder_commitment : None ,
435
436
prev_holder_htlc_sigs : None ,
@@ -663,10 +664,10 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
663
664
return None ;
664
665
} ,
665
666
& InputMaterial :: Funding { ref funding_redeemscript } => {
666
- let signed_tx = self . get_fully_signed_holder_tx ( funding_redeemscript) . unwrap ( ) ;
667
+ let signed_tx = self . get_fully_signed_holder_tx ( funding_redeemscript) ;
667
668
// Timer set to $NEVER given we can't bump tx without anchor outputs
668
669
log_trace ! ( logger, "Going to broadcast Holder Transaction {} claiming funding output {} from {}..." , signed_tx. txid( ) , outp. vout, outp. txid) ;
669
- return Some ( ( None , self . holder_commitment . as_ref ( ) . unwrap ( ) . feerate_per_kw ( ) , signed_tx) ) ;
670
+ return Some ( ( None , self . holder_commitment . feerate_per_kw ( ) , signed_tx) ) ;
670
671
}
671
672
_ => unreachable ! ( )
672
673
}
@@ -904,25 +905,22 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
904
905
}
905
906
906
907
pub ( crate ) fn provide_latest_holder_tx ( & mut self , tx : HolderCommitmentTransaction ) {
907
- self . prev_holder_commitment = self . holder_commitment . take ( ) ;
908
+ self . prev_holder_commitment = Some ( replace ( & mut self . holder_commitment , tx ) ) ;
908
909
self . holder_htlc_sigs = None ;
909
- self . holder_commitment = Some ( tx) ;
910
910
}
911
911
912
912
// Normally holder HTLCs are signed at the same time as the holder commitment tx. However,
913
913
// in some configurations, the holder commitment tx has been signed and broadcast by a secondary
914
914
// ChannelMonitor, so we handle that case here.
915
915
fn sign_latest_holder_htlcs ( & mut self ) {
916
916
if self . holder_htlc_sigs . is_none ( ) {
917
- if let Some ( ref holder_commitment) = self . holder_commitment {
918
- let ( _sig, sigs) = self . key_storage . sign_holder_commitment ( holder_commitment, & self . secp_ctx ) . expect ( "sign holder commitment" ) ;
919
- self . holder_htlc_sigs = Some ( Self :: extract_holder_sigs ( holder_commitment, sigs) ) ;
920
- }
917
+ let ( _sig, sigs) = self . key_storage . sign_holder_commitment ( & self . holder_commitment , & self . secp_ctx ) . expect ( "sign holder commitment" ) ;
918
+ self . holder_htlc_sigs = Some ( Self :: extract_holder_sigs ( & self . holder_commitment , sigs) ) ;
921
919
}
922
920
}
923
921
924
922
// Normally only the latest commitment tx and HTLCs need to be signed. However, in some
925
- // configurations we may have updated our holder commtiment but a replica of the ChannelMonitor
923
+ // configurations we may have updated our holder commitment but a replica of the ChannelMonitor
926
924
// broadcast the previous one before we sync with it. We handle that case here.
927
925
fn sign_prev_holder_htlcs ( & mut self ) {
928
926
if self . prev_holder_htlc_sigs . is_none ( ) {
@@ -947,43 +945,34 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
947
945
// have empty holder commitment transaction if a ChannelMonitor is asked to force-close just after Channel::get_outbound_funding_created,
948
946
// before providing a initial commitment transaction. For outbound channel, init ChannelMonitor at Channel::funding_signed, there is nothing
949
947
// to monitor before.
950
- pub ( crate ) fn get_fully_signed_holder_tx ( & mut self , funding_redeemscript : & Script ) -> Option < Transaction > {
951
- if let Some ( ref mut holder_commitment) = self . holder_commitment {
952
- let ( sig, htlc_sigs) = self . key_storage . sign_holder_commitment ( holder_commitment, & self . secp_ctx ) . expect ( "signing holder commitment" ) ;
953
- self . holder_htlc_sigs = Some ( Self :: extract_holder_sigs ( holder_commitment, htlc_sigs) ) ;
954
- Some ( holder_commitment. add_holder_sig ( funding_redeemscript, sig) )
955
- } else {
956
- None
957
- }
948
+ pub ( crate ) fn get_fully_signed_holder_tx ( & mut self , funding_redeemscript : & Script ) -> Transaction {
949
+ let ( sig, htlc_sigs) = self . key_storage . sign_holder_commitment ( & self . holder_commitment , & self . secp_ctx ) . expect ( "signing holder commitment" ) ;
950
+ self . holder_htlc_sigs = Some ( Self :: extract_holder_sigs ( & self . holder_commitment , htlc_sigs) ) ;
951
+ self . holder_commitment . add_holder_sig ( funding_redeemscript, sig)
958
952
}
959
953
960
954
#[ cfg( any( test, feature="unsafe_revoked_tx_signing" ) ) ]
961
- pub ( crate ) fn get_fully_signed_copy_holder_tx ( & mut self , funding_redeemscript : & Script ) -> Option < Transaction > {
962
- if let Some ( ref mut holder_commitment) = self . holder_commitment {
963
- let ( sig, htlc_sigs) = self . key_storage . sign_holder_commitment ( holder_commitment, & self . secp_ctx ) . expect ( "sign holder commitment" ) ;
964
- self . holder_htlc_sigs = Some ( Self :: extract_holder_sigs ( holder_commitment, htlc_sigs) ) ;
965
- Some ( holder_commitment. add_holder_sig ( funding_redeemscript, sig) )
966
- } else {
967
- None
968
- }
955
+ pub ( crate ) fn get_fully_signed_copy_holder_tx ( & mut self , funding_redeemscript : & Script ) -> Transaction {
956
+ let ( sig, htlc_sigs) = self . key_storage . sign_holder_commitment ( & self . holder_commitment , & self . secp_ctx ) . expect ( "sign holder commitment" ) ;
957
+ self . holder_htlc_sigs = Some ( Self :: extract_holder_sigs ( & self . holder_commitment , htlc_sigs) ) ;
958
+ self . holder_commitment . add_holder_sig ( funding_redeemscript, sig)
969
959
}
970
960
971
961
pub ( crate ) fn get_fully_signed_htlc_tx ( & mut self , outp : & :: bitcoin:: OutPoint , preimage : & Option < PaymentPreimage > ) -> Option < Transaction > {
972
962
let mut htlc_tx = None ;
973
- if self . holder_commitment . is_some ( ) {
974
- let commitment_txid = self . holder_commitment . as_ref ( ) . unwrap ( ) . trust ( ) . txid ( ) ;
975
- if commitment_txid == outp. txid {
976
- self . sign_latest_holder_htlcs ( ) ;
977
- if let & Some ( ref htlc_sigs) = & self . holder_htlc_sigs {
978
- let & ( ref htlc_idx, ref htlc_sig) = htlc_sigs[ outp. vout as usize ] . as_ref ( ) . unwrap ( ) ;
979
- let holder_commitment = self . holder_commitment . as_ref ( ) . unwrap ( ) ;
980
- let trusted_tx = holder_commitment. trust ( ) ;
981
- let counterparty_htlc_sig = holder_commitment. counterparty_htlc_sigs [ * htlc_idx] ;
982
- htlc_tx = Some ( trusted_tx
983
- . get_signed_htlc_tx ( & self . channel_transaction_parameters . as_holder_broadcastable ( ) , * htlc_idx, & counterparty_htlc_sig, htlc_sig, preimage) ) ;
984
- }
963
+ let commitment_txid = self . holder_commitment . trust ( ) . txid ( ) ;
964
+ // Check if the HTLC spends from the current holder commitment
965
+ if commitment_txid == outp. txid {
966
+ self . sign_latest_holder_htlcs ( ) ;
967
+ if let & Some ( ref htlc_sigs) = & self . holder_htlc_sigs {
968
+ let & ( ref htlc_idx, ref htlc_sig) = htlc_sigs[ outp. vout as usize ] . as_ref ( ) . unwrap ( ) ;
969
+ let trusted_tx = self . holder_commitment . trust ( ) ;
970
+ let counterparty_htlc_sig = self . holder_commitment . counterparty_htlc_sigs [ * htlc_idx] ;
971
+ htlc_tx = Some ( trusted_tx
972
+ . get_signed_htlc_tx ( & self . channel_transaction_parameters . as_holder_broadcastable ( ) , * htlc_idx, & counterparty_htlc_sig, htlc_sig, preimage) ) ;
985
973
}
986
974
}
975
+ // If the HTLC doesn't spend the current holder commitment, check if it spends the previous one
987
976
if htlc_tx. is_none ( ) && self . prev_holder_commitment . is_some ( ) {
988
977
let commitment_txid = self . prev_holder_commitment . as_ref ( ) . unwrap ( ) . trust ( ) . txid ( ) ;
989
978
if commitment_txid == outp. txid {
0 commit comments