Skip to content

Commit

Permalink
fix: use geo enum for sql queries
Browse files Browse the repository at this point in the history
  • Loading branch information
blombern committed Sep 4, 2024
1 parent 379fd80 commit d9e879d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 20 deletions.
10 changes: 5 additions & 5 deletions src/phoenix/demotion_monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
pub slot: i64,
Expand All @@ -33,9 +33,9 @@ pub async fn get_builder_demotions(
start: &DateTime<Utc>,
end: &DateTime<Utc>,
) -> Result<Vec<BuilderDemotion>> {
let query = "
let query = r#"
SELECT
bd.geo,
bd.geo as "geo!: Geo",
bd.builder_pubkey,
bb.builder_id,
bd.slot,
Expand All @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions src/phoenix/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down
10 changes: 5 additions & 5 deletions src/phoenix/inclusion_monitor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use super::{
SendAlert,
},
checkpoint::{self, CheckpointId},
env::APP_CONFIG,
env::{Geo, APP_CONFIG},
};

#[derive(Debug)]
Expand All @@ -40,27 +40,27 @@ struct DeliveredPayload {
inserted_at: DateTime<Utc>,
proposer_pubkey: String,
slot: i64,
geo: String,
geo: Geo,
}

async fn get_delivered_payloads(
relay_pool: &PgPool,
start: &DateTime<Utc>,
end: &DateTime<Utc>,
) -> anyhow::Result<Vec<DeliveredPayload>> {
let query = "
let query = r#"
SELECT
inserted_at,
slot,
geo,
geo as "geo!: Geo",
block_hash,
block_number,
proposer_pubkey
FROM payload_delivered
WHERE inserted_at > $1
AND inserted_at <= $2
ORDER BY inserted_at ASC
";
"#;

sqlx::query(query)
.bind(start)
Expand Down
22 changes: 12 additions & 10 deletions src/phoenix/promotion_monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit d9e879d

Please sign in to comment.