@@ -53,6 +53,10 @@ pub struct TrackedSpendableOutput {
5353 ///
5454 /// Will be `None` if it hasn't been broadcast yet.
5555 pub latest_broadcast_height : Option < u32 > ,
56+ /// The best height when we last (re-)generated a transaction spending this output.
57+ ///
58+ /// Will be `None` if it hasn't been broadcast yet.
59+ pub latest_spend_generation_height : Option < u32 > ,
5660 /// The transaction spending this output we last broadcast.
5761 ///
5862 /// After confirmation, this will be set to the confirmed transaction.
@@ -111,6 +115,7 @@ impl TrackedSpendableOutput {
111115
112116impl_writeable_tlv_based ! ( TrackedSpendableOutput , {
113117 ( 0 , id, required) ,
118+ ( 1 , latest_spend_generation_height, option) ,
114119 ( 2 , descriptor, required) ,
115120 ( 4 , channel_id, option) ,
116121 ( 6 , first_broadcast_hash, option) ,
@@ -339,6 +344,7 @@ where
339344 channel_id,
340345 first_broadcast_hash : None ,
341346 latest_broadcast_height : None ,
347+ latest_spend_generation_height : None ,
342348 latest_spending_tx : None ,
343349 confirmation_height : None ,
344350 confirmation_hash : None ,
@@ -377,13 +383,21 @@ where
377383 continue ;
378384 }
379385
380- if let Some ( latest_broadcast_height) = output_info. latest_broadcast_height {
386+ if let Some ( latest_spend_generation_height) = output_info. latest_spend_generation_height {
387+ debug_assert ! ( output_info. latest_broadcast_height. is_some( ) ,
388+ "If we had spent before, we should have broadcast, too." ) ;
389+ debug_assert ! ( output_info. latest_spending_tx. is_some( ) ,
390+ "If we had spent before, we should have a spending_tx set." ) ;
391+
381392 // Re-generate spending tx after regenerate_spend_threshold, rebroadcast
382393 // after every block
383- if latest_broadcast_height + self . regenerate_spend_threshold >= cur_height {
394+ if latest_spend_generation_height + self . regenerate_spend_threshold <= cur_height {
395+ log_debug ! ( self . logger,
396+ "Regeneration threshold was reached, will regenerate sweeping transaction." ) ;
397+
384398 respend_descriptors. push ( output_info. descriptor . clone ( ) ) ;
385399 respend_ids. push ( output_info. id ) ;
386- } else if latest_broadcast_height < cur_height {
400+ } else if output_info . latest_broadcast_height < Some ( cur_height) {
387401 if let Some ( latest_spending_tx) = output_info. latest_spending_tx . as_ref ( ) {
388402 log_debug ! ( self . logger, "Rebroadcasting output sweeping transaction {}" ,
389403 latest_spending_tx. txid( ) ) ;
@@ -399,7 +413,7 @@ where
399413 }
400414 }
401415 } else {
402- // Our first broadcast.
416+ // Our first spend generation + broadcast.
403417 respend_descriptors. push ( output_info. descriptor . clone ( ) ) ;
404418 respend_ids. push ( output_info. id ) ;
405419 output_info. first_broadcast_hash = Some ( cur_hash) ;
@@ -428,6 +442,7 @@ where
428442
429443 output_info. latest_spending_tx = Some ( spending_tx. clone ( ) ) ;
430444 output_info. latest_broadcast_height = Some ( cur_height) ;
445+ output_info. latest_spend_generation_height = Some ( cur_height) ;
431446 self . persist_info ( & output_info) . unwrap_or_else ( |e| {
432447 log_error ! (
433448 self . logger,
0 commit comments