@@ -53,6 +53,10 @@ pub struct TrackedSpendableOutput {
53
53
///
54
54
/// Will be `None` if it hasn't been broadcast yet.
55
55
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 no transaction has been generated yet.
59
+ pub latest_spend_generation_height : Option < u32 > ,
56
60
/// The transaction spending this output we last broadcast.
57
61
///
58
62
/// After confirmation, this will be set to the confirmed transaction.
@@ -111,6 +115,7 @@ impl TrackedSpendableOutput {
111
115
112
116
impl_writeable_tlv_based ! ( TrackedSpendableOutput , {
113
117
( 0 , id, required) ,
118
+ ( 1 , latest_spend_generation_height, option) ,
114
119
( 2 , descriptor, required) ,
115
120
( 4 , channel_id, option) ,
116
121
( 6 , first_broadcast_hash, option) ,
@@ -339,6 +344,7 @@ where
339
344
channel_id,
340
345
first_broadcast_hash : None ,
341
346
latest_broadcast_height : None ,
347
+ latest_spend_generation_height : None ,
342
348
latest_spending_tx : None ,
343
349
confirmation_height : None ,
344
350
confirmation_hash : None ,
@@ -377,13 +383,21 @@ where
377
383
continue ;
378
384
}
379
385
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
+
381
392
// Re-generate spending tx after regenerate_spend_threshold, rebroadcast
382
393
// 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
+
384
398
respend_descriptors. push ( output_info. descriptor . clone ( ) ) ;
385
399
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) {
387
401
if let Some ( latest_spending_tx) = output_info. latest_spending_tx . as_ref ( ) {
388
402
log_debug ! ( self . logger, "Rebroadcasting output sweeping transaction {}" ,
389
403
latest_spending_tx. txid( ) ) ;
@@ -399,7 +413,7 @@ where
399
413
}
400
414
}
401
415
} else {
402
- // Our first broadcast.
416
+ // Our first spend generation + broadcast.
403
417
respend_descriptors. push ( output_info. descriptor . clone ( ) ) ;
404
418
respend_ids. push ( output_info. id ) ;
405
419
output_info. first_broadcast_hash = Some ( cur_hash) ;
@@ -428,6 +442,7 @@ where
428
442
429
443
output_info. latest_spending_tx = Some ( spending_tx. clone ( ) ) ;
430
444
output_info. latest_broadcast_height = Some ( cur_height) ;
445
+ output_info. latest_spend_generation_height = Some ( cur_height) ;
431
446
self . persist_info ( & output_info) . unwrap_or_else ( |e| {
432
447
log_error ! (
433
448
self . logger,
0 commit comments