From 7dd4d8c7deeb06438a8d2ff6c5c0205a8db5212d Mon Sep 17 00:00:00 2001 From: Niclas Blomberg Date: Wed, 4 Sep 2024 14:53:14 +0200 Subject: [PATCH] fix: use geo enum for sql queries --- src/phoenix/demotion_monitor.rs | 8 ++++---- src/phoenix/env.rs | 4 ++++ src/phoenix/inclusion_monitor/mod.rs | 8 ++++---- src/phoenix/promotion_monitor.rs | 22 ++++++++++++---------- 4 files changed, 24 insertions(+), 18 deletions(-) diff --git a/src/phoenix/demotion_monitor.rs b/src/phoenix/demotion_monitor.rs index e43cec4..d40f7b3 100644 --- a/src/phoenix/demotion_monitor.rs +++ b/src/phoenix/demotion_monitor.rs @@ -16,12 +16,12 @@ use crate::{ use super::{ checkpoint::{self, CheckpointId}, - env::APP_CONFIG, + env::{Geo, APP_CONFIG}, }; #[derive(Debug, Clone)] pub struct BuilderDemotion { - pub geo: String, + pub geo: Geo, pub builder_pubkey: String, pub builder_id: Option, pub slot: i64, @@ -33,7 +33,7 @@ pub async fn get_builder_demotions( start: &DateTime, end: &DateTime, ) -> Result> { - let query = " + let query = r#" SELECT bd.geo, bd.builder_pubkey, @@ -46,7 +46,7 @@ pub async fn get_builder_demotions( WHERE bd.inserted_at > $1 AND bd.inserted_at <= $2 ORDER BY bd.inserted_at ASC - "; + "#; sqlx::query(query) .bind(start) diff --git a/src/phoenix/env.rs b/src/phoenix/env.rs index 4880e0b..8044973 100644 --- a/src/phoenix/env.rs +++ b/src/phoenix/env.rs @@ -87,8 +87,12 @@ fn default_missed_slots_alert_threshold() -> i64 { /// Auction geography #[allow(clippy::upper_case_acronyms)] +#[derive(Debug, Clone, sqlx::Type)] +#[sqlx(type_name = "geo")] pub enum Geo { + #[sqlx(rename = "rbx")] RBX, + #[sqlx(rename = "vin")] VIN, } diff --git a/src/phoenix/inclusion_monitor/mod.rs b/src/phoenix/inclusion_monitor/mod.rs index 6e7526d..beb6a76 100644 --- a/src/phoenix/inclusion_monitor/mod.rs +++ b/src/phoenix/inclusion_monitor/mod.rs @@ -29,7 +29,7 @@ use super::{ SendAlert, }, checkpoint::{self, CheckpointId}, - env::APP_CONFIG, + env::{Geo, APP_CONFIG}, }; #[derive(Debug)] @@ -40,7 +40,7 @@ struct DeliveredPayload { inserted_at: DateTime, proposer_pubkey: String, slot: i64, - geo: String, + geo: Geo, } async fn get_delivered_payloads( @@ -48,7 +48,7 @@ async fn get_delivered_payloads( start: &DateTime, end: &DateTime, ) -> anyhow::Result> { - let query = " + let query = r#" SELECT inserted_at, slot, @@ -60,7 +60,7 @@ async fn get_delivered_payloads( WHERE inserted_at > $1 AND inserted_at <= $2 ORDER BY inserted_at ASC - "; + "#; sqlx::query(query) .bind(start) diff --git a/src/phoenix/promotion_monitor.rs b/src/phoenix/promotion_monitor.rs index d7a1906..b4da8a6 100644 --- a/src/phoenix/promotion_monitor.rs +++ b/src/phoenix/promotion_monitor.rs @@ -160,19 +160,21 @@ pub async fn run_promotion_monitor( #[cfg(test)] mod tests { + use crate::phoenix::env::Geo; + use super::*; #[test] fn test_get_eligible_builders_all_eligible() { let demotions = vec![ BuilderDemotion { - geo: "rbx".to_string(), + geo: Geo::RBX, builder_pubkey: "pubkey1".to_string(), sim_error: "json error: request timeout hit before processing".to_string(), slot: 1, builder_id: Some("builder1".to_string()), }, BuilderDemotion { - geo: "rbx".to_string(), + geo: Geo::RBX, builder_pubkey: "pubkey2".to_string(), sim_error: "simulation failed: unknown ancestor".to_string(), slot: 2, @@ -189,14 +191,14 @@ mod tests { fn test_get_eligible_builders_none_eligible() { let demotions = vec![ BuilderDemotion { - geo: "rbx".to_string(), + geo: Geo::RBX, builder_pubkey: "pubkey1".to_string(), sim_error: "invalid error".to_string(), slot: 1, builder_id: Some("builder1".to_string()), }, BuilderDemotion { - geo: "rbx".to_string(), + geo: Geo::RBX, builder_pubkey: "pubkey2".to_string(), sim_error: "simulation failed: unknown ancestor".to_string(), slot: 2, @@ -214,21 +216,21 @@ mod tests { fn test_get_eligible_builders_some_eligible() { let demotions = vec![ BuilderDemotion { - geo: "rbx".to_string(), + geo: Geo::RBX, builder_pubkey: "pubkey1".to_string(), sim_error: "json error: request timeout hit before processing".to_string(), slot: 1, builder_id: Some("builder1".to_string()), }, BuilderDemotion { - geo: "rbx".to_string(), + geo: Geo::RBX, builder_pubkey: "pubkey2".to_string(), sim_error: "invalid error".to_string(), slot: 2, builder_id: Some("builder2".to_string()), }, BuilderDemotion { - geo: "rbx".to_string(), + geo: Geo::RBX, builder_pubkey: "pubkey2".to_string(), sim_error: "simulation failed: unknown ancestor".to_string(), slot: 3, @@ -246,21 +248,21 @@ mod tests { fn test_same_slot_both_valid_and_invalid() { let demotions = vec![ BuilderDemotion { - geo: "rbx".to_string(), + geo: Geo::RBX, builder_pubkey: "pubkey2".to_string(), sim_error: "invalid error".to_string(), slot: 2, builder_id: Some("builder2".to_string()), }, BuilderDemotion { - geo: "rbx".to_string(), + geo: Geo::RBX, builder_pubkey: "pubkey1".to_string(), sim_error: "json error: request timeout hit before processing".to_string(), slot: 1, builder_id: Some("builder1".to_string()), }, BuilderDemotion { - geo: "rbx".to_string(), + geo: Geo::RBX, builder_pubkey: "pubkey2".to_string(), sim_error: "simulation failed: unknown ancestor".to_string(), slot: 2,