Skip to content

Commit 2bd81d9

Browse files
committed
Prefactor: Make monior archival delay a pub const
.. previously we just used the 4032 magic number, here we put it in a `pub const` that is reusable elsewhere.
1 parent 80ed2f6 commit 2bd81d9

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

lightning/src/chain/channelmonitor.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,10 @@ pub(crate) const LATENCY_GRACE_PERIOD_BLOCKS: u32 = 3;
256256
// solved by a previous claim tx. What we want to avoid is reorg evicting our claim tx and us not
257257
// keep bumping another claim tx to solve the outpoint.
258258
pub const ANTI_REORG_DELAY: u32 = 6;
259+
/// Number of blocks we wait before assuming a [`ChannelMonitor`] to be fully resolved and
260+
/// considering it be safely archived.
261+
// 4032 blocks are roughly four weeks
262+
pub const ARCHIVAL_DELAY_BLOCKS: u32 = 4032;
259263
/// Number of blocks before confirmation at which we fail back an un-relayed HTLC or at which we
260264
/// refuse to accept a new HTLC.
261265
///
@@ -2015,10 +2019,11 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
20152019
///
20162020
/// This function returns a tuple of two booleans, the first indicating whether the monitor is
20172021
/// fully resolved, and the second whether the monitor needs persistence to ensure it is
2018-
/// reliably marked as resolved within 4032 blocks.
2022+
/// reliably marked as resolved within [`ARCHIVAL_DELAY_BLOCKS`] blocks.
20192023
///
2020-
/// The first boolean is true only if [`Self::get_claimable_balances`] has been empty for at least
2021-
/// 4032 blocks as an additional protection against any bugs resulting in spuriously empty balance sets.
2024+
/// The first boolean is true only if [`Self::get_claimable_balances`] has been empty for at
2025+
/// least [`ARCHIVAL_DELAY_BLOCKS`] blocks as an additional protection against any bugs
2026+
/// resulting in spuriously empty balance sets.
20222027
pub fn check_and_update_full_resolution_status<L: Logger>(&self, logger: &L) -> (bool, bool) {
20232028
let mut is_all_funds_claimed = self.get_claimable_balances().is_empty();
20242029
let current_height = self.current_best_block().height;
@@ -2034,11 +2039,10 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
20342039
// once processed, implies the preimage exists in the corresponding inbound channel.
20352040
let preimages_not_needed_elsewhere = inner.pending_monitor_events.is_empty();
20362041

2037-
const BLOCKS_THRESHOLD: u32 = 4032; // ~four weeks
20382042
match (inner.balances_empty_height, is_all_funds_claimed, preimages_not_needed_elsewhere) {
20392043
(Some(balances_empty_height), true, true) => {
20402044
// Claimed all funds, check if reached the blocks threshold.
2041-
(current_height >= balances_empty_height + BLOCKS_THRESHOLD, false)
2045+
(current_height >= balances_empty_height + ARCHIVAL_DELAY_BLOCKS, false)
20422046
},
20432047
(Some(_), false, _)|(Some(_), _, false) => {
20442048
// previously assumed we claimed all funds, but we have new funds to claim or
@@ -2058,7 +2062,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
20582062
// None. It is set to the current block height.
20592063
log_debug!(logger,
20602064
"ChannelMonitor funded at {} is now fully resolved. It will become archivable in {} blocks",
2061-
inner.get_funding_txo().0, BLOCKS_THRESHOLD);
2065+
inner.get_funding_txo().0, ARCHIVAL_DELAY_BLOCKS);
20622066
inner.balances_empty_height = Some(current_height);
20632067
(false, true)
20642068
},

lightning/src/ln/monitor_tests.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
//! Further functional tests which test blockchain reorganizations.
1111
1212
use crate::sign::{ecdsa::EcdsaChannelSigner, OutputSpender, SpendableOutputDescriptor};
13-
use crate::chain::channelmonitor::{ANTI_REORG_DELAY, LATENCY_GRACE_PERIOD_BLOCKS, Balance, BalanceSource, ChannelMonitorUpdateStep};
13+
use crate::chain::channelmonitor::{ANTI_REORG_DELAY, ARCHIVAL_DELAY_BLOCKS,LATENCY_GRACE_PERIOD_BLOCKS, Balance, BalanceSource, ChannelMonitorUpdateStep};
1414
use crate::chain::transaction::OutPoint;
1515
use crate::chain::chaininterface::{ConfirmationTarget, LowerBoundedFeeEstimator, compute_feerate_sat_per_1000_weight};
1616
use crate::events::bump_transaction::{BumpTransactionEvent, WalletSource};
@@ -246,31 +246,31 @@ fn archive_fully_resolved_monitors() {
246246

247247
// At this point, both nodes have no more `Balance`s, but nodes[0]'s `ChannelMonitor` still
248248
// hasn't had the `MonitorEvent` that contains the preimage claimed by the `ChannelManager`.
249-
// Thus, calling `archive_fully_resolved_channel_monitors` and waiting 4032 blocks will not
250-
// result in the `ChannelMonitor` being archived.
249+
// Thus, calling `archive_fully_resolved_channel_monitors` and waiting `ARCHIVAL_DELAY_BLOCKS`
250+
// blocks will not result in the `ChannelMonitor` being archived.
251251
nodes[0].chain_monitor.chain_monitor.archive_fully_resolved_channel_monitors();
252252
assert_eq!(nodes[0].chain_monitor.chain_monitor.list_monitors().len(), 1);
253-
connect_blocks(&nodes[0], 4032);
253+
connect_blocks(&nodes[0], ARCHIVAL_DELAY_BLOCKS);
254254
nodes[0].chain_monitor.chain_monitor.archive_fully_resolved_channel_monitors();
255255
assert_eq!(nodes[0].chain_monitor.chain_monitor.list_monitors().len(), 1);
256256

257-
// ...however, nodes[1]'s `ChannelMonitor` is ready to be archived, and will be in exactly 4032
258-
// blocks.
257+
// ...however, nodes[1]'s `ChannelMonitor` is ready to be archived, and will be in exactly
258+
// `ARCHIVAL_DELAY_BLOCKS` blocks.
259259
nodes[1].chain_monitor.chain_monitor.archive_fully_resolved_channel_monitors();
260260
assert_eq!(nodes[1].chain_monitor.chain_monitor.list_monitors().len(), 1);
261-
connect_blocks(&nodes[1], 4031);
261+
connect_blocks(&nodes[1], ARCHIVAL_DELAY_BLOCKS - 1);
262262
nodes[1].chain_monitor.chain_monitor.archive_fully_resolved_channel_monitors();
263263
assert_eq!(nodes[1].chain_monitor.chain_monitor.list_monitors().len(), 1);
264264
connect_blocks(&nodes[1], 1);
265265
nodes[1].chain_monitor.chain_monitor.archive_fully_resolved_channel_monitors();
266266
assert_eq!(nodes[1].chain_monitor.chain_monitor.list_monitors().len(), 0);
267267

268268
// Finally, we process the pending `MonitorEvent` from nodes[0], allowing the `ChannelMonitor`
269-
// to be archived 4032 blocks later.
269+
// to be archived `ARCHIVAL_DELAY_BLOCKS` blocks later.
270270
expect_payment_sent(&nodes[0], payment_preimage, None, true, false);
271271
nodes[0].chain_monitor.chain_monitor.archive_fully_resolved_channel_monitors();
272272
assert_eq!(nodes[0].chain_monitor.chain_monitor.list_monitors().len(), 1);
273-
connect_blocks(&nodes[0], 4031);
273+
connect_blocks(&nodes[0], ARCHIVAL_DELAY_BLOCKS - 1);
274274
nodes[0].chain_monitor.chain_monitor.archive_fully_resolved_channel_monitors();
275275
assert_eq!(nodes[0].chain_monitor.chain_monitor.list_monitors().len(), 1);
276276
connect_blocks(&nodes[0], 1);

0 commit comments

Comments
 (0)