-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(phoenix): Add delay_update_monitor.rs
- Loading branch information
Showing
6 changed files
with
99 additions
and
23 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
.sqlx/query-a9e4c026ffc88f66c7710a25d796cd024d3f687645d6605b30e58b402ab2b5b4.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use anyhow::Result; | ||
use sqlx::PgPool; | ||
use tracing::debug; | ||
|
||
use super::util::get_current_slot; | ||
use crate::phoenix::{Alarm, AlarmType}; | ||
|
||
use super::env::APP_CONFIG; | ||
|
||
async fn get_latest_header_delay_updates_slot(mev_pool: &PgPool) -> anyhow::Result<u32> { | ||
sqlx::query_scalar!( | ||
r#" | ||
SELECT MAX(latest_header_slot) | ||
FROM header_delay_updates | ||
"#, | ||
) | ||
.fetch_one(mev_pool) | ||
.await | ||
.map(|max| { | ||
max.expect("No maximum slot found from header_delay_updates") | ||
.try_into() | ||
.expect("Maximum slot is negative") | ||
}) | ||
.map_err(Into::into) | ||
} | ||
|
||
pub async fn run_header_delay_updates_monitor(mev_pool: &PgPool, alarm: &mut Alarm) -> Result<()> { | ||
let latest_slot = get_latest_header_delay_updates_slot(mev_pool).await?; | ||
let current_slot = get_current_slot()?; | ||
let slot_lag = current_slot - latest_slot; | ||
debug!( | ||
"header_delay_updates is {:} slots behind current slot", | ||
slot_lag | ||
); | ||
if slot_lag > APP_CONFIG.max_header_delay_updates_slot_lag { | ||
let message = format!( | ||
"header_delay_updates is {:} slots behind the current slot", | ||
slot_lag | ||
); | ||
alarm.fire(&message, &AlarmType::Telegram).await; | ||
} | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
use super::env::APP_CONFIG; | ||
use crate::env::{Network, ToNetwork}; | ||
use anyhow::Result; | ||
use chrono::{DateTime, Utc}; | ||
use lazy_static::lazy_static; | ||
|
||
lazy_static! { | ||
static ref GENESIS_TIMESTAMP: DateTime<Utc> = { | ||
match &APP_CONFIG.env.to_network() { | ||
Network::Mainnet => "2020-12-01T12:00:23Z".parse().unwrap(), | ||
Network::Holesky => "2023-09-28T12:00:00Z".parse().unwrap(), | ||
} | ||
}; | ||
} | ||
|
||
const SECONDS_PER_SLOT: u8 = 12; | ||
|
||
pub fn get_current_slot() -> Result<u32> { | ||
let now = Utc::now(); | ||
let seconds_since_genesis: u32 = (now - *GENESIS_TIMESTAMP).num_seconds().try_into()?; | ||
Ok(seconds_since_genesis / SECONDS_PER_SLOT as u32) | ||
} |