@@ -31,6 +31,7 @@ use crate::chain::ClaimId;
31
31
use crate :: chain:: chaininterface:: { ConfirmationTarget , FeeEstimator , BroadcasterInterface , LowerBoundedFeeEstimator } ;
32
32
use crate :: chain:: channelmonitor:: { ANTI_REORG_DELAY , CLTV_SHARED_CLAIM_BUFFER } ;
33
33
use crate :: chain:: package:: { PackageSolvingData , PackageTemplate } ;
34
+ use crate :: chain:: transaction:: MaybeSignedTransaction ;
34
35
use crate :: util:: logger:: Logger ;
35
36
use crate :: util:: ser:: { Readable , ReadableArgs , MaybeReadable , UpgradableRequired , Writer , Writeable , VecWriter } ;
36
37
@@ -204,14 +205,16 @@ pub(crate) enum ClaimEvent {
204
205
/// control) onchain.
205
206
pub ( crate ) enum OnchainClaim {
206
207
/// A finalized transaction pending confirmation spending the output to claim.
207
- Tx ( Transaction ) ,
208
+ Tx ( MaybeSignedTransaction ) ,
208
209
/// An event yielded externally to signal additional inputs must be added to a transaction
209
210
/// pending confirmation spending the output to claim.
210
211
Event ( ClaimEvent ) ,
211
212
}
212
213
213
- /// Represents the different feerates a pending request can use when generating a claim.
214
+ /// Represents the different feerate strategies a pending request can use when generating a claim.
214
215
pub ( crate ) enum FeerateStrategy {
216
+ /// We must reuse the most recently used feerate, if any.
217
+ RetryPrevious ,
215
218
/// We must pick the highest between the most recently used and the current feerate estimate.
216
219
HighestOfPreviousOrNew ,
217
220
/// We must force a bump of the most recently used feerate, either by using the current feerate
@@ -506,9 +509,13 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
506
509
}
507
510
match claim {
508
511
OnchainClaim :: Tx ( tx) => {
509
- let log_start = if bumped_feerate { "Broadcasting RBF-bumped" } else { "Rebroadcasting" } ;
510
- log_info ! ( logger, "{} onchain {}" , log_start, log_tx!( tx) ) ;
511
- broadcaster. broadcast_transactions ( & [ & tx] ) ;
512
+ if tx. is_fully_signed ( ) {
513
+ let log_start = if bumped_feerate { "Broadcasting RBF-bumped" } else { "Rebroadcasting" } ;
514
+ log_info ! ( logger, "{} onchain {}" , log_start, log_tx!( tx. 0 ) ) ;
515
+ broadcaster. broadcast_transactions ( & [ & tx. 0 ] ) ;
516
+ } else {
517
+ log_info ! ( logger, "Waiting for signature of unsigned onchain transaction {}" , tx. 0 . txid( ) ) ;
518
+ }
512
519
} ,
513
520
OnchainClaim :: Event ( event) => {
514
521
let log_start = if bumped_feerate { "Yielding fee-bumped" } else { "Replaying" } ;
@@ -610,11 +617,10 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
610
617
) {
611
618
assert ! ( new_feerate != 0 ) ;
612
619
613
- let transaction = cached_request. finalize_malleable_package (
620
+ let transaction = cached_request. maybe_finalize_malleable_package (
614
621
cur_height, self , output_value, self . destination_script . clone ( ) , logger
615
622
) . unwrap ( ) ;
616
- log_trace ! ( logger, "...with timer {} and feerate {}" , new_timer, new_feerate) ;
617
- assert ! ( predicted_weight >= transaction. weight( ) . to_wu( ) ) ;
623
+ assert ! ( predicted_weight >= transaction. 0 . weight( ) . to_wu( ) ) ;
618
624
return Some ( ( new_timer, new_feerate, OnchainClaim :: Tx ( transaction) ) ) ;
619
625
}
620
626
} else {
@@ -623,7 +629,7 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
623
629
// which require external funding.
624
630
let mut inputs = cached_request. inputs ( ) ;
625
631
debug_assert_eq ! ( inputs. len( ) , 1 ) ;
626
- let tx = match cached_request. finalize_untractable_package ( self , logger) {
632
+ let tx = match cached_request. maybe_finalize_untractable_package ( self , logger) {
627
633
Some ( tx) => tx,
628
634
None => return None ,
629
635
} ;
@@ -634,27 +640,27 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
634
640
// Commitment inputs with anchors support are the only untractable inputs supported
635
641
// thus far that require external funding.
636
642
PackageSolvingData :: HolderFundingOutput ( output) => {
637
- debug_assert_eq ! ( tx. txid( ) , self . holder_commitment. trust( ) . txid( ) ,
643
+ debug_assert_eq ! ( tx. 0 . txid( ) , self . holder_commitment. trust( ) . txid( ) ,
638
644
"Holder commitment transaction mismatch" ) ;
639
645
640
646
let conf_target = ConfirmationTarget :: OnChainSweep ;
641
647
let package_target_feerate_sat_per_1000_weight = cached_request
642
648
. compute_package_feerate ( fee_estimator, conf_target, feerate_strategy) ;
643
649
if let Some ( input_amount_sat) = output. funding_amount {
644
- let fee_sat = input_amount_sat - tx. output . iter ( ) . map ( |output| output. value ) . sum :: < u64 > ( ) ;
650
+ let fee_sat = input_amount_sat - tx. 0 . output . iter ( ) . map ( |output| output. value ) . sum :: < u64 > ( ) ;
645
651
let commitment_tx_feerate_sat_per_1000_weight =
646
- compute_feerate_sat_per_1000_weight ( fee_sat, tx. weight ( ) . to_wu ( ) ) ;
652
+ compute_feerate_sat_per_1000_weight ( fee_sat, tx. 0 . weight ( ) . to_wu ( ) ) ;
647
653
if commitment_tx_feerate_sat_per_1000_weight >= package_target_feerate_sat_per_1000_weight {
648
- log_debug ! ( logger, "Pre-signed {} already has feerate {} sat/kW above required {} sat/kW" ,
649
- log_tx! ( tx ) , commitment_tx_feerate_sat_per_1000_weight,
654
+ log_debug ! ( logger, "Pre-signed commitment {} already has feerate {} sat/kW above required {} sat/kW" ,
655
+ tx . 0 . txid ( ) , commitment_tx_feerate_sat_per_1000_weight,
650
656
package_target_feerate_sat_per_1000_weight) ;
651
657
return Some ( ( new_timer, 0 , OnchainClaim :: Tx ( tx. clone ( ) ) ) ) ;
652
658
}
653
659
}
654
660
655
661
// We'll locate an anchor output we can spend within the commitment transaction.
656
662
let funding_pubkey = & self . channel_transaction_parameters . holder_pubkeys . funding_pubkey ;
657
- match chan_utils:: get_anchor_output ( & tx, funding_pubkey) {
663
+ match chan_utils:: get_anchor_output ( & tx. 0 , funding_pubkey) {
658
664
// An anchor output was found, so we should yield a funding event externally.
659
665
Some ( ( idx, _) ) => {
660
666
// TODO: Use a lower confirmation target when both our and the
@@ -664,7 +670,7 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
664
670
package_target_feerate_sat_per_1000_weight as u64 ,
665
671
OnchainClaim :: Event ( ClaimEvent :: BumpCommitment {
666
672
package_target_feerate_sat_per_1000_weight,
667
- commitment_tx : tx. clone ( ) ,
673
+ commitment_tx : tx. 0 . clone ( ) ,
668
674
anchor_output_idx : idx,
669
675
} ) ,
670
676
) )
@@ -785,9 +791,13 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
785
791
// `OnchainClaim`.
786
792
let claim_id = match claim {
787
793
OnchainClaim :: Tx ( tx) => {
788
- log_info ! ( logger, "Broadcasting onchain {}" , log_tx!( tx) ) ;
789
- broadcaster. broadcast_transactions ( & [ & tx] ) ;
790
- ClaimId ( tx. txid ( ) . to_byte_array ( ) )
794
+ if tx. is_fully_signed ( ) {
795
+ log_info ! ( logger, "Broadcasting onchain {}" , log_tx!( tx. 0 ) ) ;
796
+ broadcaster. broadcast_transactions ( & [ & tx. 0 ] ) ;
797
+ } else {
798
+ log_info ! ( logger, "Waiting for signature of unsigned onchain transaction {}" , tx. 0 . txid( ) ) ;
799
+ }
800
+ ClaimId ( tx. 0 . txid ( ) . to_byte_array ( ) )
791
801
} ,
792
802
OnchainClaim :: Event ( claim_event) => {
793
803
log_info ! ( logger, "Yielding onchain event to spend inputs {:?}" , req. outpoints( ) ) ;
@@ -980,8 +990,13 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
980
990
) {
981
991
match bump_claim {
982
992
OnchainClaim :: Tx ( bump_tx) => {
983
- log_info ! ( logger, "Broadcasting RBF-bumped onchain {}" , log_tx!( bump_tx) ) ;
984
- broadcaster. broadcast_transactions ( & [ & bump_tx] ) ;
993
+ if bump_tx. is_fully_signed ( ) {
994
+ log_info ! ( logger, "Broadcasting RBF-bumped onchain {}" , log_tx!( bump_tx. 0 ) ) ;
995
+ broadcaster. broadcast_transactions ( & [ & bump_tx. 0 ] ) ;
996
+ } else {
997
+ log_info ! ( logger, "Waiting for signature of RBF-bumped unsigned onchain transaction {}" ,
998
+ bump_tx. 0 . txid( ) ) ;
999
+ }
985
1000
} ,
986
1001
OnchainClaim :: Event ( claim_event) => {
987
1002
log_info ! ( logger, "Yielding RBF-bumped onchain event to spend inputs {:?}" , request. outpoints( ) ) ;
@@ -1063,8 +1078,12 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
1063
1078
request. set_feerate ( new_feerate) ;
1064
1079
match bump_claim {
1065
1080
OnchainClaim :: Tx ( bump_tx) => {
1066
- log_info ! ( logger, "Broadcasting onchain {}" , log_tx!( bump_tx) ) ;
1067
- broadcaster. broadcast_transactions ( & [ & bump_tx] ) ;
1081
+ if bump_tx. is_fully_signed ( ) {
1082
+ log_info ! ( logger, "Broadcasting onchain {}" , log_tx!( bump_tx. 0 ) ) ;
1083
+ broadcaster. broadcast_transactions ( & [ & bump_tx. 0 ] ) ;
1084
+ } else {
1085
+ log_info ! ( logger, "Waiting for signature of unsigned onchain transaction {}" , bump_tx. 0 . txid( ) ) ;
1086
+ }
1068
1087
} ,
1069
1088
OnchainClaim :: Event ( claim_event) => {
1070
1089
log_info ! ( logger, "Yielding onchain event after reorg to spend inputs {:?}" , request. outpoints( ) ) ;
@@ -1117,13 +1136,11 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
1117
1136
& self . holder_commitment . trust ( ) . built_transaction ( ) . transaction
1118
1137
}
1119
1138
1120
- //TODO: getting lastest holder transactions should be infallible and result in us "force-closing the channel", but we may
1121
- // have empty holder commitment transaction if a ChannelMonitor is asked to force-close just after OutboundV1Channel::get_funding_created,
1122
- // before providing a initial commitment transaction. For outbound channel, init ChannelMonitor at Channel::funding_signed, there is nothing
1123
- // to monitor before.
1124
- pub ( crate ) fn get_fully_signed_holder_tx ( & mut self , funding_redeemscript : & Script ) -> Transaction {
1125
- let sig = self . signer . sign_holder_commitment ( & self . holder_commitment , & self . secp_ctx ) . expect ( "signing holder commitment" ) ;
1126
- self . holder_commitment . add_holder_sig ( funding_redeemscript, sig)
1139
+ pub ( crate ) fn get_maybe_signed_holder_tx ( & mut self , funding_redeemscript : & Script ) -> MaybeSignedTransaction {
1140
+ let tx = self . signer . sign_holder_commitment ( & self . holder_commitment , & self . secp_ctx )
1141
+ . map ( |sig| self . holder_commitment . add_holder_sig ( funding_redeemscript, sig) )
1142
+ . unwrap_or_else ( |_| self . get_unsigned_holder_commitment_tx ( ) . clone ( ) ) ;
1143
+ MaybeSignedTransaction ( tx)
1127
1144
}
1128
1145
1129
1146
#[ cfg( any( test, feature="unsafe_revoked_tx_signing" ) ) ]
@@ -1132,7 +1149,7 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
1132
1149
self . holder_commitment . add_holder_sig ( funding_redeemscript, sig)
1133
1150
}
1134
1151
1135
- pub ( crate ) fn get_fully_signed_htlc_tx ( & mut self , outp : & :: bitcoin:: OutPoint , preimage : & Option < PaymentPreimage > ) -> Option < Transaction > {
1152
+ pub ( crate ) fn get_maybe_signed_htlc_tx ( & mut self , outp : & :: bitcoin:: OutPoint , preimage : & Option < PaymentPreimage > ) -> Option < MaybeSignedTransaction > {
1136
1153
let get_signed_htlc_tx = |holder_commitment : & HolderCommitmentTransaction | {
1137
1154
let trusted_tx = holder_commitment. trust ( ) ;
1138
1155
if trusted_tx. txid ( ) != outp. txid {
@@ -1160,11 +1177,12 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
1160
1177
preimage : preimage. clone ( ) ,
1161
1178
counterparty_sig : counterparty_htlc_sig. clone ( ) ,
1162
1179
} ;
1163
- let htlc_sig = self . signer . sign_holder_htlc_transaction ( & htlc_tx, 0 , & htlc_descriptor, & self . secp_ctx ) . unwrap ( ) ;
1164
- htlc_tx. input [ 0 ] . witness = trusted_tx. build_htlc_input_witness (
1165
- htlc_idx, & counterparty_htlc_sig, & htlc_sig, preimage,
1166
- ) ;
1167
- Some ( htlc_tx)
1180
+ if let Ok ( htlc_sig) = self . signer . sign_holder_htlc_transaction ( & htlc_tx, 0 , & htlc_descriptor, & self . secp_ctx ) {
1181
+ htlc_tx. input [ 0 ] . witness = trusted_tx. build_htlc_input_witness (
1182
+ htlc_idx, & counterparty_htlc_sig, & htlc_sig, preimage,
1183
+ ) ;
1184
+ }
1185
+ Some ( MaybeSignedTransaction ( htlc_tx) )
1168
1186
} ;
1169
1187
1170
1188
// Check if the HTLC spends from the current holder commitment first, or the previous.
0 commit comments