@@ -516,13 +516,21 @@ pub async fn update_scores(pool: &SqlitePool, config: &Config) -> Result<()> {
516
516
GROUP BY public_key_id
517
517
),
518
518
HeartbeatCounts AS (
519
+ -- Count heartbeats only within valid windows
519
520
SELECT
520
521
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
523
523
FROM heartbeat_presence hp
524
524
JOIN ValidWindows vw ON vw.id = hp.window_id
525
525
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
526
534
)
527
535
INSERT INTO submitter_scores (
528
536
public_key_id,
@@ -534,10 +542,11 @@ pub async fn update_scores(pool: &SqlitePool, config: &Config) -> Result<()> {
534
542
pk.id,
535
543
COALESCE(hc.heartbeats, 0) as score,
536
544
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
538
546
FROM public_keys pk
539
547
LEFT JOIN HeartbeatCounts hc ON hc.public_key_id = pk.id
540
548
LEFT JOIN BlockCounts bc ON bc.public_key_id = pk.id
549
+ LEFT JOIN LastHeartbeats lh ON lh.public_key_id = pk.id
541
550
WHERE hc.heartbeats > 0 OR bc.blocks > 0
542
551
ON CONFLICT(public_key_id) DO UPDATE SET
543
552
score = excluded.score,
0 commit comments