Skip to content

Commit 5c8115e

Browse files
authored
Merge pull request #1074 from openmina/feat/heartbeats-last-hb
feat(heartbeats): Keep track of when the last submitter heartbeat was received
2 parents d203d61 + f24af1f commit 5c8115e

8 files changed

+57
-24
lines changed

tools/heartbeats-processor/.sqlx/query-14d5d1973bb6a28e4be770e15dbb0293366513edb3945b91c2ae62ca2827ecc5.json

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 8 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tools/heartbeats-processor/.sqlx/query-c69ce193a25e2b2b5c1208df04d22d54114d61c2a2fc0f026b58183e8a8232ca.json

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 8 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tools/heartbeats-processor/schema.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ CREATE TABLE IF NOT EXISTS submitter_scores (
4545
score INTEGER NOT NULL DEFAULT 0,
4646
last_updated INTEGER NOT NULL DEFAULT (strftime('%s', 'now')),
4747
blocks_produced INTEGER NOT NULL DEFAULT 0,
48+
last_heartbeat INTEGER NOT NULL DEFAULT 0,
4849
FOREIGN KEY (public_key_id) REFERENCES public_keys(id)
4950
);
5051

@@ -83,6 +84,10 @@ ON heartbeat_presence(public_key_id);
8384
CREATE INDEX IF NOT EXISTS idx_heartbeat_presence_global_slot
8485
ON heartbeat_presence(best_tip_global_slot);
8586

87+
-- Index for heartbeat time queries
88+
CREATE INDEX IF NOT EXISTS idx_heartbeat_presence_time
89+
ON heartbeat_presence(heartbeat_time);
90+
8691
-- Index for submitter counts lookup
8792
CREATE INDEX IF NOT EXISTS idx_submitter_counts_last_seen
8893
ON submitter_counts(last_seen);

tools/heartbeats-processor/src/local_db.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -516,23 +516,33 @@ pub async fn update_scores(pool: &SqlitePool, config: &Config) -> Result<()> {
516516
GROUP BY public_key_id
517517
),
518518
HeartbeatCounts AS (
519-
SELECT hp.public_key_id, COUNT(DISTINCT hp.window_id) as heartbeats
519+
SELECT
520+
hp.public_key_id,
521+
COUNT(DISTINCT hp.window_id) as heartbeats,
522+
MAX(hp.heartbeat_time) as last_heartbeat
520523
FROM heartbeat_presence hp
521524
JOIN ValidWindows vw ON vw.id = hp.window_id
522525
GROUP BY hp.public_key_id
523526
)
524-
INSERT INTO submitter_scores (public_key_id, score, blocks_produced)
527+
INSERT INTO submitter_scores (
528+
public_key_id,
529+
score,
530+
blocks_produced,
531+
last_heartbeat
532+
)
525533
SELECT
526534
pk.id,
527535
COALESCE(hc.heartbeats, 0) as score,
528-
COALESCE(bc.blocks, 0) as blocks_produced
536+
COALESCE(bc.blocks, 0) as blocks_produced,
537+
COALESCE(hc.last_heartbeat, 0) as last_heartbeat
529538
FROM public_keys pk
530539
LEFT JOIN HeartbeatCounts hc ON hc.public_key_id = pk.id
531540
LEFT JOIN BlockCounts bc ON bc.public_key_id = pk.id
532541
WHERE hc.heartbeats > 0 OR bc.blocks > 0
533542
ON CONFLICT(public_key_id) DO UPDATE SET
534543
score = excluded.score,
535544
blocks_produced = excluded.blocks_produced,
545+
last_heartbeat = excluded.last_heartbeat,
536546
last_updated = strftime('%s', 'now')
537547
"#,
538548
window_start,
@@ -580,7 +590,8 @@ pub async fn view_scores(pool: &SqlitePool, config: &Config) -> Result<()> {
580590
pk.public_key,
581591
ss.score,
582592
ss.blocks_produced,
583-
datetime(ss.last_updated, 'unixepoch') as last_updated
593+
datetime(ss.last_updated, 'unixepoch') as last_updated,
594+
datetime(ss.last_heartbeat, 'unixepoch') as last_heartbeat
584595
FROM submitter_scores ss
585596
JOIN public_keys pk ON pk.id = ss.public_key_id
586597
ORDER BY ss.score DESC, ss.blocks_produced DESC
@@ -594,19 +605,20 @@ pub async fn view_scores(pool: &SqlitePool, config: &Config) -> Result<()> {
594605
println!("\nSubmitter Scores:");
595606
println!("--------------------------------------------------------");
596607
println!(
597-
"Public Key | Score | Blocks | Current Max | Total Max | Last Updated"
608+
"Public Key | Score | Blocks | Current Max | Total Max | Last Updated | Last Heartbeat"
598609
);
599610
println!("--------------------------------------------------------");
600611

601612
for row in scores {
602613
println!(
603-
"{:<40} | {:>5} | {:>6} | {:>11} | {:>9} | {}",
614+
"{:<40} | {:>5} | {:>6} | {:>11} | {:>9} | {} | {}",
604615
row.public_key,
605616
row.score,
606617
row.blocks_produced,
607618
max_scores.current,
608619
max_scores.total,
609-
row.last_updated.unwrap_or_default()
620+
row.last_updated.unwrap_or_default(),
621+
row.last_heartbeat.unwrap_or_default()
610622
);
611623
}
612624

tools/heartbeats-processor/src/main.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ async fn post_scores_to_firestore(
7575
pk.public_key,
7676
ss.score,
7777
ss.blocks_produced,
78-
ss.last_updated
78+
ss.last_updated,
79+
ss.last_heartbeat
7980
FROM submitter_scores ss
8081
JOIN public_keys pk ON pk.id = ss.public_key_id
8182
ORDER BY ss.score DESC
@@ -91,6 +92,7 @@ async fn post_scores_to_firestore(
9192
score: row.score,
9293
blocks_produced: row.blocks_produced,
9394
last_updated: row.last_updated,
95+
last_heartbeat: row.last_heartbeat,
9496
})
9597
.collect();
9698

tools/heartbeats-processor/src/remote_db.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ pub struct ScoreDocument {
135135
pub blocks_produced: i64,
136136
#[serde(rename = "lastUpdated")]
137137
pub last_updated: i64,
138+
#[serde(rename = "lastHeartbeat")]
139+
pub last_heartbeat: i64,
138140
}
139141

140142
pub async fn get_db(config: &Config) -> Result<FirestoreDb> {

0 commit comments

Comments
 (0)