@@ -43,6 +43,7 @@ use crate::chain;
4343use  crate :: chain:: { BestBlock ,  WatchedOutput } ; 
4444use  crate :: chain:: chaininterface:: { BroadcasterInterface ,  FeeEstimator ,  LowerBoundedFeeEstimator } ; 
4545use  crate :: chain:: transaction:: { OutPoint ,  TransactionData } ; 
46+ use  crate :: sign:: errors:: SigningError ; 
4647use  crate :: sign:: { SpendableOutputDescriptor ,  StaticPaymentOutputDescriptor ,  DelayedPaymentOutputDescriptor ,  WriteableEcdsaChannelSigner ,  SignerProvider ,  EntropySource } ; 
4748use  crate :: chain:: onchaintx:: { ClaimEvent ,  OnchainTxHandler } ; 
4849use  crate :: chain:: package:: { CounterpartyOfferedHTLCOutput ,  CounterpartyReceivedHTLCOutput ,  HolderFundingOutput ,  HolderHTLCOutput ,  PackageSolvingData ,  PackageTemplate ,  RevokedOutput ,  RevokedHTLCOutput } ; 
@@ -1482,21 +1483,16 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitor<Signer> {
14821483		self . inner . lock ( ) . unwrap ( ) . counterparty_node_id 
14831484	} 
14841485
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 
14891487/// a monitor update failed or because we've fallen behind (i.e. we've received proof that our 
14901488/// counterparty side knows a revocation secret we gave them that they shouldn't know). 
14911489/// 
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 
14931491/// side to punish you. Nevertheless you may want to broadcast them if counterparty doesn't 
14941492/// close channel with their commitment transaction after a substantial amount of time. Best 
14951493/// may be to contact the other node operator out-of-band to coordinate other options available 
14961494/// 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 > 
15001496	where  L :: Target :  Logger  { 
15011497		self . inner . lock ( ) . unwrap ( ) . get_latest_holder_commitment_txn ( logger) 
15021498	} 
@@ -1505,7 +1501,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitor<Signer> {
15051501/// to bypass HolderCommitmentTransaction state update lockdown after signature and generate 
15061502/// revoked commitment transaction. 
15071503#[ 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 > 
15091505	where  L :: Target :  Logger  { 
15101506		self . inner . lock ( ) . unwrap ( ) . unsafe_get_latest_holder_commitment_txn ( logger) 
15111507	} 
@@ -2515,18 +2511,53 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
25152511		} 
25162512	} 
25172513
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 ) 
25192555		where  B :: Target :  BroadcasterInterface , 
2520- 					L :: Target :  Logger , 
2556+ 			F :: Target :  FeeEstimator , 
2557+ 			L :: Target :  Logger , 
25212558	{ 
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) ; 
25302561	} 
25312562
25322563	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> {
26142645							log_trace ! ( logger,  "Avoiding commitment broadcast, already detected confirmed spend onchain" ) ; 
26152646							continue ; 
26162647						} 
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) ; 
26372649					}  else  if  !self . holder_tx_signed  { 
26382650						log_error ! ( logger,  "WARNING: You have a potentially-unsafe holder commitment transaction available to broadcast" ) ; 
26392651						log_error ! ( logger,  "    in channel monitor for channel {}!" ,  & self . funding_info. 0 . to_channel_id( ) ) ; 
@@ -3211,16 +3223,16 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
32113223		} 
32123224	} 
32133225
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  { 
32153227		log_debug ! ( logger,  "Getting signed latest holder commitment transaction!" ) ; 
32163228		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 ) ? ; 
32183230		let  txid = commitment_tx. txid ( ) ; 
32193231		let  mut  holder_transactions = vec ! [ commitment_tx] ; 
32203232		// When anchor outputs are present, the HTLC transactions are only valid once the commitment 
32213233		// transaction confirms. 
32223234		if  self . onchain_tx_handler . channel_type_features ( ) . supports_anchors_zero_fee_htlc_tx ( )  { 
3223- 			return  holder_transactions; 
3235+ 			return  Ok ( holder_transactions) ; 
32243236		} 
32253237		for  htlc in  self . current_holder_commitment_tx . htlc_outputs . iter ( )  { 
32263238			if  let  Some ( vout)  = htlc. 0 . transaction_output_index  { 
@@ -3245,20 +3257,20 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
32453257		} 
32463258		// 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. 
32473259		// 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) 
32493261	} 
32503262
32513263	#[ cfg( any( test, feature = "unsafe_revoked_tx_signing" ) ) ]  
32523264	/// 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  { 
32543266		log_debug ! ( logger,  "Getting signed copy of latest holder commitment transaction!" ) ; 
32553267		let  commitment_tx = self . onchain_tx_handler . get_fully_signed_copy_holder_tx ( & self . funding_redeemscript ) ; 
32563268		let  txid = commitment_tx. txid ( ) ; 
32573269		let  mut  holder_transactions = vec ! [ commitment_tx] ; 
32583270		// When anchor outputs are present, the HTLC transactions are only final once the commitment 
32593271		// transaction confirms due to the CSV 1 encumberance. 
32603272		if  self . onchain_tx_handler . channel_type_features ( ) . supports_anchors_zero_fee_htlc_tx ( )  { 
3261- 			return  holder_transactions; 
3273+ 			return  Ok ( holder_transactions) ; 
32623274		} 
32633275		for  htlc in  self . current_holder_commitment_tx . htlc_outputs . iter ( )  { 
32643276			if  let  Some ( vout)  = htlc. 0 . transaction_output_index  { 
@@ -3274,7 +3286,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
32743286				} 
32753287			} 
32763288		} 
3277- 		holder_transactions
3289+ 		Ok ( holder_transactions) 
32783290	} 
32793291
32803292	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