@@ -43,6 +43,7 @@ use crate::chain;
43
43
use crate :: chain:: { BestBlock , WatchedOutput } ;
44
44
use crate :: chain:: chaininterface:: { BroadcasterInterface , FeeEstimator , LowerBoundedFeeEstimator } ;
45
45
use crate :: chain:: transaction:: { OutPoint , TransactionData } ;
46
+ use crate :: sign:: errors:: SigningError ;
46
47
use crate :: sign:: { SpendableOutputDescriptor , StaticPaymentOutputDescriptor , DelayedPaymentOutputDescriptor , WriteableEcdsaChannelSigner , SignerProvider , EntropySource } ;
47
48
use crate :: chain:: onchaintx:: { ClaimEvent , OnchainTxHandler } ;
48
49
use crate :: chain:: package:: { CounterpartyOfferedHTLCOutput , CounterpartyReceivedHTLCOutput , HolderFundingOutput , HolderHTLCOutput , PackageSolvingData , PackageTemplate , RevokedOutput , RevokedHTLCOutput } ;
@@ -1482,21 +1483,16 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitor<Signer> {
1482
1483
self . inner . lock ( ) . unwrap ( ) . counterparty_node_id
1483
1484
}
1484
1485
1485
- /// Used by [`ChannelManager`] deserialization to broadcast the latest holder state if its copy
1486
- /// of the channel state was out-of-date.
1487
- ///
1488
- /// You may also use this to broadcast the latest local commitment transaction, either because
1486
+ /// You may use this to broadcast the latest local commitment transaction, either because
1489
1487
/// a monitor update failed or because we've fallen behind (i.e. we've received proof that our
1490
1488
/// counterparty side knows a revocation secret we gave them that they shouldn't know).
1491
1489
///
1492
- /// Broadcasting these transactions in the second case is UNSAFE, as they allow counterparty
1490
+ /// Broadcasting these transactions in this manner is UNSAFE, as they allow counterparty
1493
1491
/// side to punish you. Nevertheless you may want to broadcast them if counterparty doesn't
1494
1492
/// close channel with their commitment transaction after a substantial amount of time. Best
1495
1493
/// may be to contact the other node operator out-of-band to coordinate other options available
1496
1494
/// to you.
1497
- ///
1498
- /// [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
1499
- pub fn get_latest_holder_commitment_txn < L : Deref > ( & self , logger : & L ) -> Vec < Transaction >
1495
+ pub fn get_latest_holder_commitment_txn < L : Deref > ( & self , logger : & L ) -> Result < Vec < Transaction > , SigningError >
1500
1496
where L :: Target : Logger {
1501
1497
self . inner . lock ( ) . unwrap ( ) . get_latest_holder_commitment_txn ( logger)
1502
1498
}
@@ -1505,7 +1501,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitor<Signer> {
1505
1501
/// to bypass HolderCommitmentTransaction state update lockdown after signature and generate
1506
1502
/// revoked commitment transaction.
1507
1503
#[ cfg( any( test, feature = "unsafe_revoked_tx_signing" ) ) ]
1508
- pub fn unsafe_get_latest_holder_commitment_txn < L : Deref > ( & self , logger : & L ) -> Vec < Transaction >
1504
+ pub fn unsafe_get_latest_holder_commitment_txn < L : Deref > ( & self , logger : & L ) -> Result < Vec < Transaction > , SigningError >
1509
1505
where L :: Target : Logger {
1510
1506
self . inner . lock ( ) . unwrap ( ) . unsafe_get_latest_holder_commitment_txn ( logger)
1511
1507
}
@@ -2515,18 +2511,53 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
2515
2511
}
2516
2512
}
2517
2513
2518
- pub ( crate ) fn broadcast_latest_holder_commitment_txn < B : Deref , L : Deref > ( & mut self , broadcaster : & B , logger : & L )
2514
+ fn generate_claimable_outpoints_and_watch_outputs ( & mut self ) -> ( Vec < PackageTemplate > , Vec < TransactionOutputs > ) {
2515
+ let funding_outp = HolderFundingOutput :: build (
2516
+ self . funding_redeemscript . clone ( ) ,
2517
+ self . channel_value_satoshis ,
2518
+ self . onchain_tx_handler . channel_type_features ( ) . clone ( )
2519
+ ) ;
2520
+ let commitment_package = PackageTemplate :: build_package (
2521
+ self . funding_info . 0 . txid . clone ( ) , self . funding_info . 0 . index as u32 ,
2522
+ PackageSolvingData :: HolderFundingOutput ( funding_outp) ,
2523
+ self . best_block . height ( ) , self . best_block . height ( )
2524
+ ) ;
2525
+ let mut claimable_outpoints = vec ! [ commitment_package] ;
2526
+ self . pending_monitor_events . push ( MonitorEvent :: HolderForceClosed ( self . funding_info . 0 ) ) ;
2527
+ // Although we aren't signing the transaction directly here, the transaction will be signed
2528
+ // in the claim that is queued to OnchainTxHandler. We set holder_tx_signed here to reject
2529
+ // new channel updates.
2530
+ self . holder_tx_signed = true ;
2531
+ let mut watch_outputs = Vec :: new ( ) ;
2532
+ // We can't broadcast our HTLC transactions while the commitment transaction is
2533
+ // unconfirmed. We'll delay doing so until we detect the confirmed commitment in
2534
+ // `transactions_confirmed`.
2535
+ if !self . onchain_tx_handler . channel_type_features ( ) . supports_anchors_zero_fee_htlc_tx ( ) {
2536
+ // Because we're broadcasting a commitment transaction, we should construct the package
2537
+ // assuming it gets confirmed in the next block. Sadly, we have code which considers
2538
+ // "not yet confirmed" things as discardable, so we cannot do that here.
2539
+ let ( mut new_outpoints, _) = self . get_broadcasted_holder_claims (
2540
+ & self . current_holder_commitment_tx , self . best_block . height ( )
2541
+ ) ;
2542
+ let unsigned_commitment_tx = self . onchain_tx_handler . get_unsigned_holder_commitment_tx ( ) ;
2543
+ let new_outputs = self . get_broadcasted_holder_watch_outputs (
2544
+ & self . current_holder_commitment_tx , & unsigned_commitment_tx
2545
+ ) ;
2546
+ if !new_outputs. is_empty ( ) {
2547
+ watch_outputs. push ( ( self . current_holder_commitment_tx . txid . clone ( ) , new_outputs) ) ;
2548
+ }
2549
+ claimable_outpoints. append ( & mut new_outpoints) ;
2550
+ }
2551
+ ( claimable_outpoints, watch_outputs)
2552
+ }
2553
+
2554
+ pub ( crate ) fn queue_latest_holder_commitment_txn_for_broadcast < B : Deref , F : Deref , L : Deref > ( & mut self , broadcaster : & B , fee_estimator : & LowerBoundedFeeEstimator < F > , logger : & L )
2519
2555
where B :: Target : BroadcasterInterface ,
2520
- L :: Target : Logger ,
2556
+ F :: Target : FeeEstimator ,
2557
+ L :: Target : Logger ,
2521
2558
{
2522
- let commit_txs = self . get_latest_holder_commitment_txn ( logger) ;
2523
- let mut txs = vec ! [ ] ;
2524
- for tx in commit_txs. iter ( ) {
2525
- log_info ! ( logger, "Broadcasting local {}" , log_tx!( tx) ) ;
2526
- txs. push ( tx) ;
2527
- }
2528
- broadcaster. broadcast_transactions ( & txs) ;
2529
- self . pending_monitor_events . push ( MonitorEvent :: HolderForceClosed ( self . funding_info . 0 ) ) ;
2559
+ let ( claimable_outpoints, _) = self . generate_claimable_outpoints_and_watch_outputs ( ) ;
2560
+ self . onchain_tx_handler . update_claims_view_from_requests ( claimable_outpoints, self . best_block . height ( ) , self . best_block . height ( ) , broadcaster, fee_estimator, logger) ;
2530
2561
}
2531
2562
2532
2563
pub fn update_monitor < B : Deref , F : Deref , L : Deref > ( & mut self , updates : & ChannelMonitorUpdate , broadcaster : & B , fee_estimator : F , logger : & L ) -> Result < ( ) , ( ) >
@@ -2614,26 +2645,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
2614
2645
log_trace ! ( logger, "Avoiding commitment broadcast, already detected confirmed spend onchain" ) ;
2615
2646
continue ;
2616
2647
}
2617
- self . broadcast_latest_holder_commitment_txn ( broadcaster, logger) ;
2618
- // If the channel supports anchor outputs, we'll need to emit an external
2619
- // event to be consumed such that a child transaction is broadcast with a
2620
- // high enough feerate for the parent commitment transaction to confirm.
2621
- if self . onchain_tx_handler . channel_type_features ( ) . supports_anchors_zero_fee_htlc_tx ( ) {
2622
- let funding_output = HolderFundingOutput :: build (
2623
- self . funding_redeemscript . clone ( ) , self . channel_value_satoshis ,
2624
- self . onchain_tx_handler . channel_type_features ( ) . clone ( ) ,
2625
- ) ;
2626
- let best_block_height = self . best_block . height ( ) ;
2627
- let commitment_package = PackageTemplate :: build_package (
2628
- self . funding_info . 0 . txid . clone ( ) , self . funding_info . 0 . index as u32 ,
2629
- PackageSolvingData :: HolderFundingOutput ( funding_output) ,
2630
- best_block_height, best_block_height
2631
- ) ;
2632
- self . onchain_tx_handler . update_claims_view_from_requests (
2633
- vec ! [ commitment_package] , best_block_height, best_block_height,
2634
- broadcaster, & bounded_fee_estimator, logger,
2635
- ) ;
2636
- }
2648
+ self . queue_latest_holder_commitment_txn_for_broadcast ( broadcaster, & bounded_fee_estimator, logger) ;
2637
2649
} else if !self . holder_tx_signed {
2638
2650
log_error ! ( logger, "WARNING: You have a potentially-unsafe holder commitment transaction available to broadcast" ) ;
2639
2651
log_error ! ( logger, " in channel monitor for channel {}!" , & self . funding_info. 0 . to_channel_id( ) ) ;
@@ -3211,16 +3223,16 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
3211
3223
}
3212
3224
}
3213
3225
3214
- pub fn get_latest_holder_commitment_txn < L : Deref > ( & mut self , logger : & L ) -> Vec < Transaction > where L :: Target : Logger {
3226
+ pub fn get_latest_holder_commitment_txn < L : Deref > ( & mut self , logger : & L ) -> Result < Vec < Transaction > , SigningError > where L :: Target : Logger {
3215
3227
log_debug ! ( logger, "Getting signed latest holder commitment transaction!" ) ;
3216
3228
self . holder_tx_signed = true ;
3217
- let commitment_tx = self . onchain_tx_handler . get_fully_signed_holder_tx ( & self . funding_redeemscript ) ;
3229
+ let commitment_tx = self . onchain_tx_handler . get_fully_signed_holder_tx ( & self . funding_redeemscript ) ? ;
3218
3230
let txid = commitment_tx. txid ( ) ;
3219
3231
let mut holder_transactions = vec ! [ commitment_tx] ;
3220
3232
// When anchor outputs are present, the HTLC transactions are only valid once the commitment
3221
3233
// transaction confirms.
3222
3234
if self . onchain_tx_handler . channel_type_features ( ) . supports_anchors_zero_fee_htlc_tx ( ) {
3223
- return holder_transactions;
3235
+ return Ok ( holder_transactions) ;
3224
3236
}
3225
3237
for htlc in self . current_holder_commitment_tx . htlc_outputs . iter ( ) {
3226
3238
if let Some ( vout) = htlc. 0 . transaction_output_index {
@@ -3245,20 +3257,20 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
3245
3257
}
3246
3258
// We throw away the generated waiting_first_conf data as we aren't (yet) confirmed and we don't actually know what the caller wants to do.
3247
3259
// The data will be re-generated and tracked in check_spend_holder_transaction if we get a confirmation.
3248
- holder_transactions
3260
+ Ok ( holder_transactions)
3249
3261
}
3250
3262
3251
3263
#[ cfg( any( test, feature = "unsafe_revoked_tx_signing" ) ) ]
3252
3264
/// Note that this includes possibly-locktimed-in-the-future transactions!
3253
- fn unsafe_get_latest_holder_commitment_txn < L : Deref > ( & mut self , logger : & L ) -> Vec < Transaction > where L :: Target : Logger {
3265
+ fn unsafe_get_latest_holder_commitment_txn < L : Deref > ( & mut self , logger : & L ) -> Result < Vec < Transaction > , SigningError > where L :: Target : Logger {
3254
3266
log_debug ! ( logger, "Getting signed copy of latest holder commitment transaction!" ) ;
3255
3267
let commitment_tx = self . onchain_tx_handler . get_fully_signed_copy_holder_tx ( & self . funding_redeemscript ) ;
3256
3268
let txid = commitment_tx. txid ( ) ;
3257
3269
let mut holder_transactions = vec ! [ commitment_tx] ;
3258
3270
// When anchor outputs are present, the HTLC transactions are only final once the commitment
3259
3271
// transaction confirms due to the CSV 1 encumberance.
3260
3272
if self . onchain_tx_handler . channel_type_features ( ) . supports_anchors_zero_fee_htlc_tx ( ) {
3261
- return holder_transactions;
3273
+ return Ok ( holder_transactions) ;
3262
3274
}
3263
3275
for htlc in self . current_holder_commitment_tx . htlc_outputs . iter ( ) {
3264
3276
if let Some ( vout) = htlc. 0 . transaction_output_index {
@@ -3274,7 +3286,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
3274
3286
}
3275
3287
}
3276
3288
}
3277
- holder_transactions
3289
+ Ok ( holder_transactions)
3278
3290
}
3279
3291
3280
3292
pub fn block_connected < B : Deref , F : Deref , L : Deref > ( & mut self , header : & BlockHeader , txdata : & TransactionData , height : u32 , broadcaster : B , fee_estimator : F , logger : L ) -> Vec < TransactionOutputs >
0 commit comments