refactor(hermes): state->benchmarks downcasting
This commit is contained in:
parent
110c6dcea3
commit
b4ed825cd6
|
@ -403,7 +403,9 @@ where
|
||||||
Ok(price_feeds_with_update_data) => Ok(price_feeds_with_update_data),
|
Ok(price_feeds_with_update_data) => Ok(price_feeds_with_update_data),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
if let RequestTime::FirstAfter(publish_time) = request_time {
|
if let RequestTime::FirstAfter(publish_time) = request_time {
|
||||||
return Benchmarks::get_verified_price_feeds(state, price_ids, publish_time).await;
|
return state
|
||||||
|
.get_verified_price_feeds(price_ids, publish_time)
|
||||||
|
.await;
|
||||||
}
|
}
|
||||||
Err(e)
|
Err(e)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
//! This module contains the global state of the application.
|
//! This module contains the global state of the application.
|
||||||
|
|
||||||
use {
|
use {
|
||||||
self::cache::CacheState,
|
self::{
|
||||||
|
benchmarks::BenchmarksState,
|
||||||
|
cache::CacheState,
|
||||||
|
},
|
||||||
crate::{
|
crate::{
|
||||||
aggregate::{
|
aggregate::{
|
||||||
AggregateState,
|
AggregateState,
|
||||||
|
@ -29,10 +32,12 @@ pub mod benchmarks;
|
||||||
pub mod cache;
|
pub mod cache;
|
||||||
|
|
||||||
pub struct State {
|
pub struct State {
|
||||||
/// Storage is a short-lived cache of the state of all the updates that have been passed to the
|
/// State for the `Cache` service for short-lived storage of updates.
|
||||||
/// store.
|
|
||||||
pub cache: CacheState,
|
pub cache: CacheState,
|
||||||
|
|
||||||
|
/// State for the `Benchmarks` service for looking up historical updates.
|
||||||
|
pub benchmarks: BenchmarksState,
|
||||||
|
|
||||||
/// Sequence numbers of lately observed Vaas. Store uses this set
|
/// Sequence numbers of lately observed Vaas. Store uses this set
|
||||||
/// to ignore the previously observed Vaas as a performance boost.
|
/// to ignore the previously observed Vaas as a performance boost.
|
||||||
pub observed_vaa_seqs: RwLock<BTreeSet<u64>>,
|
pub observed_vaa_seqs: RwLock<BTreeSet<u64>>,
|
||||||
|
@ -46,9 +51,6 @@ pub struct State {
|
||||||
/// The aggregate module state.
|
/// The aggregate module state.
|
||||||
pub aggregate_state: RwLock<AggregateState>,
|
pub aggregate_state: RwLock<AggregateState>,
|
||||||
|
|
||||||
/// Benchmarks endpoint
|
|
||||||
pub benchmarks_endpoint: Option<Url>,
|
|
||||||
|
|
||||||
/// Metrics registry
|
/// Metrics registry
|
||||||
pub metrics_registry: RwLock<Registry>,
|
pub metrics_registry: RwLock<Registry>,
|
||||||
|
|
||||||
|
@ -64,13 +66,13 @@ impl State {
|
||||||
) -> Arc<Self> {
|
) -> Arc<Self> {
|
||||||
let mut metrics_registry = Registry::default();
|
let mut metrics_registry = Registry::default();
|
||||||
Arc::new(Self {
|
Arc::new(Self {
|
||||||
cache: CacheState::new(cache_size),
|
cache: CacheState::new(cache_size),
|
||||||
observed_vaa_seqs: RwLock::new(Default::default()),
|
benchmarks: BenchmarksState::new(benchmarks_endpoint),
|
||||||
guardian_set: RwLock::new(Default::default()),
|
observed_vaa_seqs: RwLock::new(Default::default()),
|
||||||
api_update_tx: update_tx,
|
guardian_set: RwLock::new(Default::default()),
|
||||||
aggregate_state: RwLock::new(AggregateState::new(&mut metrics_registry)),
|
api_update_tx: update_tx,
|
||||||
benchmarks_endpoint,
|
aggregate_state: RwLock::new(AggregateState::new(&mut metrics_registry)),
|
||||||
metrics_registry: RwLock::new(metrics_registry),
|
metrics_registry: RwLock::new(metrics_registry),
|
||||||
price_feeds_metadata: RwLock::new(Default::default()),
|
price_feeds_metadata: RwLock::new(Default::default()),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
//! This module communicates with Pyth Benchmarks, an API for historical price feeds and their updates.
|
//! This module communicates with Pyth Benchmarks, an API for historical price feeds and their updates.
|
||||||
|
|
||||||
use {
|
use {
|
||||||
|
super::State,
|
||||||
crate::{
|
crate::{
|
||||||
aggregate::{
|
aggregate::{
|
||||||
PriceFeedsWithUpdateData,
|
PriceFeedsWithUpdateData,
|
||||||
|
@ -14,6 +15,7 @@ use {
|
||||||
Engine as _,
|
Engine as _,
|
||||||
},
|
},
|
||||||
pyth_sdk::PriceIdentifier,
|
pyth_sdk::PriceIdentifier,
|
||||||
|
reqwest::Url,
|
||||||
serde::Deserialize,
|
serde::Deserialize,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -50,7 +52,23 @@ impl TryFrom<BinaryBlob> for Vec<Vec<u8>> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait::async_trait]
|
pub struct BenchmarksState {
|
||||||
|
endpoint: Option<Url>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl BenchmarksState {
|
||||||
|
pub fn new(url: Option<Url>) -> Self {
|
||||||
|
Self { endpoint: url }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Allow downcasting State into BenchmarksState for functions that depend on the `Benchmarks` service.
|
||||||
|
impl<'a> From<&'a State> for &'a BenchmarksState {
|
||||||
|
fn from(state: &'a State) -> &'a BenchmarksState {
|
||||||
|
&state.benchmarks
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub trait Benchmarks {
|
pub trait Benchmarks {
|
||||||
async fn get_verified_price_feeds(
|
async fn get_verified_price_feeds(
|
||||||
&self,
|
&self,
|
||||||
|
@ -59,22 +77,25 @@ pub trait Benchmarks {
|
||||||
) -> Result<PriceFeedsWithUpdateData>;
|
) -> Result<PriceFeedsWithUpdateData>;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait::async_trait]
|
impl<T> Benchmarks for T
|
||||||
impl Benchmarks for crate::state::State {
|
where
|
||||||
|
for<'a> &'a T: Into<&'a BenchmarksState>,
|
||||||
|
T: Sync,
|
||||||
|
{
|
||||||
async fn get_verified_price_feeds(
|
async fn get_verified_price_feeds(
|
||||||
&self,
|
&self,
|
||||||
price_ids: &[PriceIdentifier],
|
price_ids: &[PriceIdentifier],
|
||||||
publish_time: UnixTimestamp,
|
publish_time: UnixTimestamp,
|
||||||
) -> Result<PriceFeedsWithUpdateData> {
|
) -> Result<PriceFeedsWithUpdateData> {
|
||||||
let endpoint = self
|
let endpoint = self
|
||||||
.benchmarks_endpoint
|
.into()
|
||||||
|
.endpoint
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.ok_or_else(|| anyhow::anyhow!("Benchmarks endpoint is not set"))?
|
.ok_or_else(|| anyhow::anyhow!("Benchmarks endpoint is not set"))?
|
||||||
.join(&format!("/v1/updates/price/{}", publish_time))
|
.join(&format!("/v1/updates/price/{}", publish_time))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let client = reqwest::Client::new();
|
let mut request = reqwest::Client::new()
|
||||||
let mut request = client
|
|
||||||
.get(endpoint)
|
.get(endpoint)
|
||||||
.timeout(BENCHMARKS_REQUEST_TIMEOUT)
|
.timeout(BENCHMARKS_REQUEST_TIMEOUT)
|
||||||
.query(&[("encoding", "hex")])
|
.query(&[("encoding", "hex")])
|
||||||
|
|
Loading…
Reference in New Issue