@@ -242,55 +242,58 @@ async fn batch_insert_produced_blocks(pool: &SqlitePool, blocks: &[ProducedBlock
242
242
Ok ( ( ) )
243
243
}
244
244
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.
246
246
///
247
247
/// 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).
252
252
///
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.
256
256
///
257
257
/// Note: The first window in the sequence will not have any entries marked as disabled,
258
258
/// as there is no previous window to compare against.
259
259
///
260
260
/// Returns the number of presence entries marked as disabled.
261
261
async fn mark_outdated_presence ( pool : & SqlitePool ) -> Result < usize > {
262
+ const HEIGHT_TOLERANCE : i64 = 5 ;
263
+
262
264
let affected = sqlx:: query!(
263
265
r#"
264
- WITH MaxSlots AS (
266
+ WITH MaxHeights AS (
265
267
SELECT
266
268
window_id,
267
- MAX(best_tip_global_slot ) as max_slot
269
+ MAX(best_tip_height ) as max_height
268
270
FROM heartbeat_presence
269
271
WHERE disabled = FALSE
270
272
GROUP BY window_id
271
273
),
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
274
276
SELECT
275
277
tw.id as window_id,
276
- prev.max_slot as prev_max_slot
278
+ prev.max_height as prev_max_height
277
279
FROM time_windows tw
278
280
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
280
282
)
281
283
UPDATE heartbeat_presence
282
284
SET disabled = TRUE
283
- WHERE (window_id, best_tip_global_slot ) IN (
285
+ WHERE (window_id, best_tip_height ) IN (
284
286
SELECT
285
287
hp.window_id,
286
- hp.best_tip_global_slot
288
+ hp.best_tip_height
287
289
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
289
291
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 - ?)
292
294
)
293
- "#
295
+ "# ,
296
+ HEIGHT_TOLERANCE
294
297
)
295
298
. execute ( pool)
296
299
. await ?;
0 commit comments