Skip to content

Commit c67d8a6

Browse files
committed
feat(heartbeats): Increase outdated presence tolerance
1 parent b48e29b commit c67d8a6

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed

tools/heartbeats-processor/src/local_db.rs

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -242,55 +242,58 @@ async fn batch_insert_produced_blocks(pool: &SqlitePool, blocks: &[ProducedBlock
242242
Ok(())
243243
}
244244

245-
/// Marks heartbeat presence entries as outdated (disabled) based on global slot comparisons.
245+
/// Marks heartbeat presence entries as outdated (disabled) based on block height comparisons.
246246
///
247247
/// This function performs the following steps:
248-
/// 1. Finds the maximum global slot for each window (considering only non-disabled entries).
249-
/// 2. Identifies the previous window's maximum global slot for each window.
250-
/// 3. Marks a presence entry as disabled if its global slot is less than:
251-
/// - The maximum global slot of the previous window (if it exists).
248+
/// 1. Finds the maximum block height for each window (considering only non-disabled entries).
249+
/// 2. Identifies the previous window's maximum block height for each window.
250+
/// 3. Marks a presence entry as disabled if its block height is less than:
251+
/// - The maximum block height of the previous window minus a tolerance of $HEIGHT_TOLERANCE blocks (if it exists).
252252
///
253-
/// This approach allows for a full window of tolerance in synchronization:
254-
/// - Entries matching or exceeding the previous window's max slot are considered up-to-date.
255-
/// - This allows for slight delays in propagation between windows.
253+
/// This approach allows for a reasonable tolerance in synchronization:
254+
/// - Entries matching or exceeding the previous window's max height - $HEIGHT_TOLERANCE are considered up-to-date.
255+
/// - This allows for slight delays in block propagation between windows.
256256
///
257257
/// Note: The first window in the sequence will not have any entries marked as disabled,
258258
/// as there is no previous window to compare against.
259259
///
260260
/// Returns the number of presence entries marked as disabled.
261261
async fn mark_outdated_presence(pool: &SqlitePool) -> Result<usize> {
262+
const HEIGHT_TOLERANCE: i64 = 5;
263+
262264
let affected = sqlx::query!(
263265
r#"
264-
WITH MaxSlots AS (
266+
WITH MaxHeights AS (
265267
SELECT
266268
window_id,
267-
MAX(best_tip_global_slot) as max_slot
269+
MAX(best_tip_height) as max_height
268270
FROM heartbeat_presence
269271
WHERE disabled = FALSE
270272
GROUP BY window_id
271273
),
272-
PrevMaxSlots AS (
273-
-- Get the max slot from the immediate previous window
274+
PrevMaxHeights AS (
275+
-- Get the max height from the immediate previous window
274276
SELECT
275277
tw.id as window_id,
276-
prev.max_slot as prev_max_slot
278+
prev.max_height as prev_max_height
277279
FROM time_windows tw
278280
LEFT JOIN time_windows prev_tw ON prev_tw.id = tw.id - 1
279-
LEFT JOIN MaxSlots prev ON prev.window_id = prev_tw.id
281+
LEFT JOIN MaxHeights prev ON prev.window_id = prev_tw.id
280282
)
281283
UPDATE heartbeat_presence
282284
SET disabled = TRUE
283-
WHERE (window_id, best_tip_global_slot) IN (
285+
WHERE (window_id, best_tip_height) IN (
284286
SELECT
285287
hp.window_id,
286-
hp.best_tip_global_slot
288+
hp.best_tip_height
287289
FROM heartbeat_presence hp
288-
JOIN PrevMaxSlots pms ON pms.window_id = hp.window_id
290+
JOIN PrevMaxHeights pmh ON pmh.window_id = hp.window_id
289291
WHERE hp.disabled = FALSE
290-
AND pms.prev_max_slot IS NOT NULL -- Ensure there is a previous window
291-
AND hp.best_tip_global_slot < pms.prev_max_slot -- Less than previous window max
292+
AND pmh.prev_max_height IS NOT NULL -- Ensure there is a previous window
293+
AND hp.best_tip_height < (pmh.prev_max_height - ?)
292294
)
293-
"#
295+
"#,
296+
HEIGHT_TOLERANCE
294297
)
295298
.execute(pool)
296299
.await?;

0 commit comments

Comments
 (0)