Skip to content

Commit

Permalink
r Add latest_stale_tip to ChannelMonitor
Browse files Browse the repository at this point in the history
  • Loading branch information
jbesraa committed Mar 28, 2024
1 parent 67545a3 commit 6059f76
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion lightning/src/chain/channelmonitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,9 @@ pub(crate) struct ChannelMonitorImpl<Signer: WriteableEcdsaChannelSigner> {
/// Ordering of tuple data: (their_per_commitment_point, feerate_per_kw, to_broadcaster_sats,
/// to_countersignatory_sats)
initial_counterparty_commitment_info: Option<(PublicKey, u32, u64, u64)>,

/// The latest block height we've seen at the time of checking for stale channels.
latest_stale_tip: Option<u32>,
}

/// Transaction outputs to watch for on-chain spends.
Expand Down Expand Up @@ -1327,6 +1330,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitor<Signer> {
best_block,
counterparty_node_id: Some(counterparty_node_id),
initial_counterparty_commitment_info: None,
latest_stale_tip: None,
})
}

Expand Down Expand Up @@ -1855,8 +1859,28 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitor<Signer> {
spendable_outputs
}

fn latest_stale_tip(&self) -> Option<u32> {
let inner = self.inner.lock().unwrap();
inner.latest_stale_tip
}

fn set_latest_stale_tip(&self, latest_stale_tip: Option<u32>) {
let mut inner = self.inner.lock().unwrap();
inner.latest_stale_tip = latest_stale_tip;
}


pub(crate) fn is_stale(&self) -> bool {
self.get_claimable_balances().is_empty()
if let Some(latest_stale_tip) = self.latest_stale_tip() {
let is_below_threshold = self.current_best_block().height > latest_stale_tip;
let best_block = self.current_best_block();
self.set_latest_stale_tip(Some(best_block.height));
is_below_threshold && self.get_claimable_balances().is_empty()
} else {
let best_block = self.current_best_block();
self.set_latest_stale_tip(Some(best_block.height));
false
}
}

#[cfg(test)]
Expand Down Expand Up @@ -4725,6 +4749,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
best_block,
counterparty_node_id,
initial_counterparty_commitment_info,
latest_stale_tip: None,
})))
}
}
Expand Down

0 comments on commit 6059f76

Please sign in to comment.