-
Notifications
You must be signed in to change notification settings - Fork 262
feat(argus): internal interfaces and shared memory model #2682
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b142f39
feat: argus skeleton and internal interfaces
tejasbadadare b08cc1d
doc: add module docs
tejasbadadare 263a2ed
feat: use dashmap instead of rwlock<hashmap>
tejasbadadare 24359c5
fix: pr comments
tejasbadadare 8215ca7
fix: make interval values configurable
tejasbadadare 9299af9
fix: remove streaming in GetChainPrices
tejasbadadare File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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,4 @@ | ||
pub mod contract; | ||
pub mod ethereum; | ||
pub mod hermes; | ||
pub mod types; |
This file contains hidden or 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,70 @@ | ||
use super::ethereum::PythPulse; | ||
use super::types::*; | ||
use crate::adapters::ethereum::SubscriptionParams; | ||
use anyhow::Result; | ||
use async_trait::async_trait; | ||
use ethers::providers::Middleware; | ||
use ethers::types::H256; | ||
use pyth_sdk::Price; | ||
use std::collections::HashMap; | ||
|
||
#[async_trait] | ||
pub trait GetChainPrices { | ||
async fn get_price_unsafe( | ||
&self, | ||
subscription_id: SubscriptionId, | ||
feed_id: &PriceId, | ||
) -> Result<Option<Price>>; | ||
} | ||
|
||
#[async_trait] | ||
impl<M: Middleware + 'static> GetChainPrices for PythPulse<M> { | ||
async fn get_price_unsafe( | ||
&self, | ||
_subscription_id: SubscriptionId, | ||
_feed_id: &PriceId, | ||
) -> Result<Option<Price>> { | ||
todo!() | ||
} | ||
} | ||
#[async_trait] | ||
pub trait UpdateChainPrices { | ||
async fn update_price_feeds( | ||
&self, | ||
subscription_id: SubscriptionId, | ||
price_ids: &[PriceId], | ||
update_data: &[Vec<u8>], | ||
) -> Result<H256>; | ||
} | ||
#[async_trait] | ||
impl<M: Middleware + 'static> UpdateChainPrices for PythPulse<M> { | ||
async fn update_price_feeds( | ||
&self, | ||
subscription_id: SubscriptionId, | ||
price_ids: &[PriceId], | ||
update_data: &[Vec<u8>], | ||
) -> Result<H256> { | ||
tracing::debug!( | ||
subscription_id = subscription_id.to_string(), | ||
price_ids_count = price_ids.len(), | ||
update_data_count = update_data.len(), | ||
"Updating price feeds on-chain via PythPulse" | ||
); | ||
todo!() | ||
} | ||
} | ||
#[async_trait] | ||
pub trait ReadChainSubscriptions { | ||
async fn get_active_subscriptions(&self) | ||
-> Result<HashMap<SubscriptionId, SubscriptionParams>>; | ||
} | ||
|
||
#[async_trait] | ||
impl<M: Middleware + 'static> ReadChainSubscriptions for PythPulse<M> { | ||
async fn get_active_subscriptions( | ||
&self, | ||
) -> Result<HashMap<SubscriptionId, SubscriptionParams>> { | ||
tracing::debug!("Getting active subscriptions via PythPulse"); | ||
Ok(HashMap::new()) | ||
} | ||
} |
This file contains hidden or 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 hidden or 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,21 @@ | ||
use super::types::*; | ||
use anyhow::Result; | ||
use async_trait::async_trait; | ||
|
||
pub struct HermesClient; | ||
|
||
#[async_trait] | ||
pub trait ReadPythPrices { | ||
async fn get_latest_prices(&self, feed_ids: &[PriceId]) -> Result<Vec<Vec<u8>>>; | ||
async fn subscribe_to_price_updates(&self, feed_ids: &[PriceId]) -> Result<()>; // TODO: return a stream | ||
} | ||
#[async_trait] | ||
impl ReadPythPrices for HermesClient { | ||
async fn get_latest_prices(&self, _feed_ids: &[PriceId]) -> Result<Vec<Vec<u8>>> { | ||
todo!() | ||
} | ||
|
||
async fn subscribe_to_price_updates(&self, _feed_ids: &[PriceId]) -> Result<()> { | ||
todo!() | ||
} | ||
} |
This file contains hidden or 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,5 @@ | ||
use ethers::types::U256; | ||
use pyth_sdk::PriceIdentifier; | ||
|
||
pub type PriceId = PriceIdentifier; | ||
pub type SubscriptionId = U256; |
This file contains hidden or 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 |
---|---|---|
@@ -1,106 +1,61 @@ | ||
//! API server for Prometheus metrics and health checks | ||
|
||
use { | ||
crate::chain::reader::BlockStatus, | ||
axum::{ | ||
body::Body, | ||
http::StatusCode, | ||
response::{IntoResponse, Response}, | ||
routing::get, | ||
Router, | ||
}, | ||
prometheus_client::{ | ||
encoding::EncodeLabelSet, | ||
metrics::{counter::Counter, family::Family}, | ||
registry::Registry, | ||
}, | ||
std::sync::Arc, | ||
tokio::sync::RwLock, | ||
anyhow::{anyhow, Result}, | ||
axum::{body::Body, routing::get, Router}, | ||
index::index, | ||
live::live, | ||
metrics::metrics, | ||
prometheus_client::registry::Registry, | ||
ready::ready, | ||
std::{net::SocketAddr, sync::Arc}, | ||
tokio::sync::{watch, RwLock}, | ||
tower_http::cors::CorsLayer, | ||
}; | ||
pub use {index::*, live::*, metrics::*, ready::*}; | ||
|
||
mod index; | ||
mod live; | ||
mod metrics; | ||
mod ready; | ||
|
||
pub type ChainId = String; | ||
|
||
#[derive(Clone, Debug, Hash, PartialEq, Eq, EncodeLabelSet)] | ||
pub struct RequestLabel { | ||
pub value: String, | ||
} | ||
|
||
pub struct ApiMetrics { | ||
pub http_requests: Family<RequestLabel, Counter>, | ||
} | ||
|
||
#[derive(Clone)] | ||
pub struct ApiState { | ||
pub metrics_registry: Arc<RwLock<Registry>>, | ||
|
||
/// Prometheus metrics | ||
pub metrics: Arc<ApiMetrics>, | ||
} | ||
|
||
impl ApiState { | ||
pub async fn new(metrics_registry: Arc<RwLock<Registry>>) -> ApiState { | ||
let metrics = ApiMetrics { | ||
http_requests: Family::default(), | ||
}; | ||
|
||
let http_requests = metrics.http_requests.clone(); | ||
metrics_registry.write().await.register( | ||
"http_requests", | ||
"Number of HTTP requests received", | ||
http_requests, | ||
); | ||
|
||
ApiState { | ||
metrics: Arc::new(metrics), | ||
metrics_registry, | ||
} | ||
} | ||
} | ||
|
||
/// The state of the service for a single blockchain. | ||
#[derive(Clone)] | ||
pub struct BlockchainState { | ||
/// The chain id for this blockchain, useful for logging | ||
pub id: ChainId, | ||
/// The BlockStatus of the block that is considered to be confirmed on the blockchain. | ||
/// For eg., Finalized, Safe | ||
pub confirmed_block_status: BlockStatus, | ||
} | ||
|
||
pub enum RestError { | ||
/// The server cannot currently communicate with the blockchain, so is not able to verify | ||
/// which random values have been requested. | ||
TemporarilyUnavailable, | ||
/// A catch-all error for all other types of errors that could occur during processing. | ||
Unknown, | ||
} | ||
|
||
impl IntoResponse for RestError { | ||
fn into_response(self) -> Response { | ||
match self { | ||
RestError::TemporarilyUnavailable => ( | ||
StatusCode::SERVICE_UNAVAILABLE, | ||
"This service is temporarily unavailable", | ||
) | ||
.into_response(), | ||
RestError::Unknown => ( | ||
StatusCode::INTERNAL_SERVER_ERROR, | ||
"An unknown error occurred processing the request", | ||
) | ||
.into_response(), | ||
} | ||
} | ||
} | ||
|
||
pub fn routes(state: ApiState) -> Router<(), Body> { | ||
pub fn routes(api_state: ApiState) -> Router<(), Body> { | ||
Router::new() | ||
.route("/", get(index)) | ||
.route("/live", get(live)) | ||
.route("/metrics", get(metrics)) | ||
.route("/ready", get(ready)) | ||
.with_state(state) | ||
.route("/metrics", get(metrics)) | ||
.with_state(api_state) | ||
} | ||
|
||
pub async fn run_api_server( | ||
socket_addr: SocketAddr, | ||
metrics_registry: Arc<RwLock<Registry>>, | ||
mut exit_rx: watch::Receiver<bool>, | ||
) -> Result<()> { | ||
let api_state = ApiState { | ||
metrics_registry: metrics_registry.clone(), | ||
}; | ||
|
||
let app = Router::new(); | ||
let app = app | ||
.merge(routes(api_state)) | ||
// Permissive CORS layer to allow all origins | ||
.layer(CorsLayer::permissive()); | ||
|
||
tracing::info!("Starting API server on: {:?}", &socket_addr); | ||
axum::Server::try_bind(&socket_addr) | ||
.map_err(|e| anyhow!("Failed to bind to address {}: {}", &socket_addr, e))? | ||
.serve(app.into_make_service()) | ||
.with_graceful_shutdown(async { | ||
// Ctrl+c signal received | ||
let _ = exit_rx.changed().await; | ||
|
||
tracing::info!("Shutting down API server..."); | ||
}) | ||
.await?; | ||
|
||
Ok(()) | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i recommend moving some of these in to the files to reduce indirection if they are only used in one place.