@@ -516,13 +516,21 @@ pub async fn update_scores(pool: &SqlitePool, config: &Config) -> Result<()> {
516516 GROUP BY public_key_id
517517 ),
518518 HeartbeatCounts AS (
519+ -- Count heartbeats only within valid windows
519520 SELECT
520521 hp.public_key_id,
521- COUNT(DISTINCT hp.window_id) as heartbeats,
522- MAX(hp.heartbeat_time) as last_heartbeat
522+ COUNT(DISTINCT hp.window_id) as heartbeats
523523 FROM heartbeat_presence hp
524524 JOIN ValidWindows vw ON vw.id = hp.window_id
525525 GROUP BY hp.public_key_id
526+ ),
527+ LastHeartbeats AS (
528+ -- Get last heartbeat time across all windows
529+ SELECT
530+ public_key_id,
531+ MAX(heartbeat_time) as last_heartbeat
532+ FROM heartbeat_presence
533+ GROUP BY public_key_id
526534 )
527535 INSERT INTO submitter_scores (
528536 public_key_id,
@@ -534,10 +542,11 @@ pub async fn update_scores(pool: &SqlitePool, config: &Config) -> Result<()> {
534542 pk.id,
535543 COALESCE(hc.heartbeats, 0) as score,
536544 COALESCE(bc.blocks, 0) as blocks_produced,
537- COALESCE(hc .last_heartbeat, 0) as last_heartbeat
545+ COALESCE(lh .last_heartbeat, 0) as last_heartbeat
538546 FROM public_keys pk
539547 LEFT JOIN HeartbeatCounts hc ON hc.public_key_id = pk.id
540548 LEFT JOIN BlockCounts bc ON bc.public_key_id = pk.id
549+ LEFT JOIN LastHeartbeats lh ON lh.public_key_id = pk.id
541550 WHERE hc.heartbeats > 0 OR bc.blocks > 0
542551 ON CONFLICT(public_key_id) DO UPDATE SET
543552 score = excluded.score,
0 commit comments