Skip to content

Commit a3b416a

Browse files
committed
Make PackageTemplate::height_timer non-optional
Now that we leverage a package's `height_timer` even for untractable packages, there's no need to have it be an `Option` anymore. We aim to not break compatibility by keeping the deserialization of such as an `option`, and use the package's `height_original` when not present. This allows us to retry packages from older `ChannelMonitor` versions that have had a failed initial package broadcast.
1 parent 4828817 commit a3b416a

File tree

3 files changed

+38
-26
lines changed

3 files changed

+38
-26
lines changed

lightning/src/chain/onchaintx.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
489489
///
490490
/// Panics if there are signing errors, because signing operations in reaction to on-chain
491491
/// events are not expected to fail, and if they do, we may lose funds.
492-
fn generate_claim<F: Deref, L: Deref>(&mut self, cur_height: u32, cached_request: &PackageTemplate, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L) -> Option<(Option<u32>, u64, OnchainClaim)>
492+
fn generate_claim<F: Deref, L: Deref>(&mut self, cur_height: u32, cached_request: &PackageTemplate, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L) -> Option<(u32, u64, OnchainClaim)>
493493
where F::Target: FeeEstimator,
494494
L::Target: Logger,
495495
{
@@ -533,7 +533,7 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
533533

534534
// Compute new height timer to decide when we need to regenerate a new bumped version of the claim tx (if we
535535
// didn't receive confirmation of it before, or not enough reorg-safe depth on top of it).
536-
let new_timer = Some(cached_request.get_height_timer(cur_height));
536+
let new_timer = cached_request.get_height_timer(cur_height);
537537
if cached_request.is_malleable() {
538538
#[cfg(anchors)]
539539
{ // Attributes are not allowed on if expressions on our current MSRV of 1.41.
@@ -565,7 +565,7 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
565565
let transaction = cached_request.finalize_malleable_package(
566566
cur_height, self, output_value, self.destination_script.clone(), logger
567567
).unwrap();
568-
log_trace!(logger, "...with timer {} and feerate {}", new_timer.unwrap(), new_feerate);
568+
log_trace!(logger, "...with timer {} and feerate {}", new_timer, new_feerate);
569569
assert!(predicted_weight >= transaction.weight());
570570
return Some((new_timer, new_feerate, OnchainClaim::Tx(transaction)));
571571
}
@@ -616,7 +616,7 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
616616
// attempt to broadcast the transaction with its current fee rate and hope
617617
// it confirms. This is essentially the same behavior as a commitment
618618
// transaction without anchor outputs.
619-
None => Some((None, 0, OnchainClaim::Tx(tx.clone()))),
619+
None => Some((new_timer, 0, OnchainClaim::Tx(tx.clone()))),
620620
}
621621
},
622622
_ => {
@@ -885,10 +885,8 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
885885

886886
// Check if any pending claim request must be rescheduled
887887
for (package_id, request) in self.pending_claim_requests.iter() {
888-
if let Some(h) = request.timer() {
889-
if cur_height >= h {
890-
bump_candidates.insert(*package_id, request.clone());
891-
}
888+
if cur_height >= request.timer() {
889+
bump_candidates.insert(*package_id, request.clone());
892890
}
893891
}
894892

lightning/src/chain/package.rs

+13-16
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ pub struct PackageTemplate {
538538
feerate_previous: u64,
539539
// Cache of next height at which fee-bumping and rebroadcast will be attempted. In
540540
// the future, we might abstract it to an observed mempool fluctuation.
541-
height_timer: Option<u32>,
541+
height_timer: u32,
542542
// Confirmation height of the claimed outputs set transaction. In case of reorg reaching
543543
// it, we wipe out and forget the package.
544544
height_original: u32,
@@ -557,13 +557,10 @@ impl PackageTemplate {
557557
pub(crate) fn set_feerate(&mut self, new_feerate: u64) {
558558
self.feerate_previous = new_feerate;
559559
}
560-
pub(crate) fn timer(&self) -> Option<u32> {
561-
if let Some(ref timer) = self.height_timer {
562-
return Some(*timer);
563-
}
564-
None
560+
pub(crate) fn timer(&self) -> u32 {
561+
self.height_timer
565562
}
566-
pub(crate) fn set_timer(&mut self, new_timer: Option<u32>) {
563+
pub(crate) fn set_timer(&mut self, new_timer: u32) {
567564
self.height_timer = new_timer;
568565
}
569566
pub(crate) fn outpoints(&self) -> Vec<&BitcoinOutPoint> {
@@ -837,7 +834,7 @@ impl PackageTemplate {
837834
soonest_conf_deadline,
838835
aggregable,
839836
feerate_previous: 0,
840-
height_timer: None,
837+
height_timer: height_original,
841838
height_original,
842839
}
843840
}
@@ -854,7 +851,7 @@ impl Writeable for PackageTemplate {
854851
(0, self.soonest_conf_deadline, required),
855852
(2, self.feerate_previous, required),
856853
(4, self.height_original, required),
857-
(6, self.height_timer, option)
854+
(6, self.height_timer, required)
858855
});
859856
Ok(())
860857
}
@@ -893,13 +890,16 @@ impl Readable for PackageTemplate {
893890
(4, height_original, required),
894891
(6, height_timer, option),
895892
});
893+
if height_timer.is_none() {
894+
height_timer = Some(height_original);
895+
}
896896
Ok(PackageTemplate {
897897
inputs,
898898
malleability,
899899
soonest_conf_deadline,
900900
aggregable,
901901
feerate_previous,
902-
height_timer,
902+
height_timer: height_timer.unwrap(),
903903
height_original,
904904
})
905905
}
@@ -1177,12 +1177,9 @@ mod tests {
11771177
let revk_outp = dumb_revk_output!(secp_ctx);
11781178

11791179
let mut package = PackageTemplate::build_package(txid, 0, revk_outp, 1000, true, 100);
1180-
let timer_none = package.timer();
1181-
assert!(timer_none.is_none());
1182-
package.set_timer(Some(100));
1183-
if let Some(timer_some) = package.timer() {
1184-
assert_eq!(timer_some, 100);
1185-
} else { panic!() }
1180+
assert_eq!(package.timer(), 100);
1181+
package.set_timer(101);
1182+
assert_eq!(package.timer(), 101);
11861183
}
11871184

11881185
#[test]

0 commit comments

Comments
 (0)