Skip to content

chore(apollo_l1_endpoint_monitor): implement the core of l1 endpoint monitor #6328

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions crates/apollo_l1_endpoint_monitor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ repository.workspace = true
license.workspace = true

[dependencies]
alloy.workspace = true
serde.workspace = true
thiserror.workspace = true
tracing.workspace = true
url = { workspace = true, features = ["serde"] }

[dev-dependencies]

Expand Down
2 changes: 1 addition & 1 deletion crates/apollo_l1_endpoint_monitor/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@

pub mod monitor;
69 changes: 69 additions & 0 deletions crates/apollo_l1_endpoint_monitor/src/monitor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use alloy::providers::{Provider, ProviderBuilder};
use serde::{Deserialize, Serialize};
use thiserror::Error;
use tracing::{error, warn};
use url::Url;

type L1EndpointMonitorResult<T> = Result<T, L1EndpointMonitorError>;

#[derive(Debug, Clone)]
pub struct L1EndpointMonitor {
pub current_l1_endpoint_index: usize,
pub config: L1EndpointMonitorConfig,
}

impl L1EndpointMonitor {
/// Returns a functional L1 endpoint, or fails if all configured endpoints are non-operational.
/// The method cycles through the configured endpoints, starting from the currently selected one
/// and returns the first one that is operational.
pub async fn get_active_l1_endpoint(&mut self) -> L1EndpointMonitorResult<Url> {
let current_l1_endpoint_index = self.current_l1_endpoint_index;
// This check can be done async, instead of blocking the user, but this requires an
// additional "active" component or async task in our infra.
if self.is_operational(current_l1_endpoint_index).await {
return Ok(self.get_node_url(current_l1_endpoint_index).clone());
}

let n_urls = self.config.ordered_l1_endpoint_urls.len();
for offset in 1..n_urls {
let idx = (current_l1_endpoint_index + offset) % n_urls;
if self.is_operational(idx).await {
warn!(
"L1 endpoint {} down; switched to {}",
self.get_node_url(current_l1_endpoint_index),
self.get_node_url(idx)
);

self.current_l1_endpoint_index = idx;
return Ok(self.get_node_url(idx).clone());
}
}

error!("No operational L1 endpoints found in {:?}", self.config.ordered_l1_endpoint_urls);
Err(L1EndpointMonitorError::NoActiveL1Endpoint)
}

fn get_node_url(&self, index: usize) -> &Url {
&self.config.ordered_l1_endpoint_urls[index]
}

async fn is_operational(&self, l1_endpoint_index: usize) -> bool {
let l1_endpoint_url = self.get_node_url(l1_endpoint_index);
let l1_client = ProviderBuilder::new().on_http(l1_endpoint_url.clone());
// Is this fast enough? we can use something to just check connectivity, but a recent infura
// bug failed on this API even though connectivity was fine. Besides, this API is called for
// most of our operations anyway.
l1_client.get_block_number().await.is_ok()
}
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct L1EndpointMonitorConfig {
pub ordered_l1_endpoint_urls: Vec<Url>,
}

#[derive(Debug, Clone, Error, Serialize, Deserialize, PartialEq, Eq)]
pub enum L1EndpointMonitorError {
#[error("All L1 endpoints are non-operational")]
NoActiveL1Endpoint,
}
Loading