Skip to content

Commit b4ed825

Browse files
committed
refactor(hermes): state->benchmarks downcasting
1 parent 110c6dc commit b4ed825

File tree

3 files changed

+45
-20
lines changed

3 files changed

+45
-20
lines changed

hermes/src/aggregate.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,9 @@ where
403403
Ok(price_feeds_with_update_data) => Ok(price_feeds_with_update_data),
404404
Err(e) => {
405405
if let RequestTime::FirstAfter(publish_time) = request_time {
406-
return Benchmarks::get_verified_price_feeds(state, price_ids, publish_time).await;
406+
return state
407+
.get_verified_price_feeds(price_ids, publish_time)
408+
.await;
407409
}
408410
Err(e)
409411
}

hermes/src/state.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
//! This module contains the global state of the application.
22
33
use {
4-
self::cache::CacheState,
4+
self::{
5+
benchmarks::BenchmarksState,
6+
cache::CacheState,
7+
},
58
crate::{
69
aggregate::{
710
AggregateState,
@@ -29,10 +32,12 @@ pub mod benchmarks;
2932
pub mod cache;
3033

3134
pub struct State {
32-
/// Storage is a short-lived cache of the state of all the updates that have been passed to the
33-
/// store.
35+
/// State for the `Cache` service for short-lived storage of updates.
3436
pub cache: CacheState,
3537

38+
/// State for the `Benchmarks` service for looking up historical updates.
39+
pub benchmarks: BenchmarksState,
40+
3641
/// Sequence numbers of lately observed Vaas. Store uses this set
3742
/// to ignore the previously observed Vaas as a performance boost.
3843
pub observed_vaa_seqs: RwLock<BTreeSet<u64>>,
@@ -46,9 +51,6 @@ pub struct State {
4651
/// The aggregate module state.
4752
pub aggregate_state: RwLock<AggregateState>,
4853

49-
/// Benchmarks endpoint
50-
pub benchmarks_endpoint: Option<Url>,
51-
5254
/// Metrics registry
5355
pub metrics_registry: RwLock<Registry>,
5456

@@ -64,13 +66,13 @@ impl State {
6466
) -> Arc<Self> {
6567
let mut metrics_registry = Registry::default();
6668
Arc::new(Self {
67-
cache: CacheState::new(cache_size),
68-
observed_vaa_seqs: RwLock::new(Default::default()),
69-
guardian_set: RwLock::new(Default::default()),
70-
api_update_tx: update_tx,
71-
aggregate_state: RwLock::new(AggregateState::new(&mut metrics_registry)),
72-
benchmarks_endpoint,
73-
metrics_registry: RwLock::new(metrics_registry),
69+
cache: CacheState::new(cache_size),
70+
benchmarks: BenchmarksState::new(benchmarks_endpoint),
71+
observed_vaa_seqs: RwLock::new(Default::default()),
72+
guardian_set: RwLock::new(Default::default()),
73+
api_update_tx: update_tx,
74+
aggregate_state: RwLock::new(AggregateState::new(&mut metrics_registry)),
75+
metrics_registry: RwLock::new(metrics_registry),
7476
price_feeds_metadata: RwLock::new(Default::default()),
7577
})
7678
}

hermes/src/state/benchmarks.rs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! This module communicates with Pyth Benchmarks, an API for historical price feeds and their updates.
22
33
use {
4+
super::State,
45
crate::{
56
aggregate::{
67
PriceFeedsWithUpdateData,
@@ -14,6 +15,7 @@ use {
1415
Engine as _,
1516
},
1617
pyth_sdk::PriceIdentifier,
18+
reqwest::Url,
1719
serde::Deserialize,
1820
};
1921

@@ -50,7 +52,23 @@ impl TryFrom<BinaryBlob> for Vec<Vec<u8>> {
5052
}
5153
}
5254

53-
#[async_trait::async_trait]
55+
pub struct BenchmarksState {
56+
endpoint: Option<Url>,
57+
}
58+
59+
impl BenchmarksState {
60+
pub fn new(url: Option<Url>) -> Self {
61+
Self { endpoint: url }
62+
}
63+
}
64+
65+
/// Allow downcasting State into BenchmarksState for functions that depend on the `Benchmarks` service.
66+
impl<'a> From<&'a State> for &'a BenchmarksState {
67+
fn from(state: &'a State) -> &'a BenchmarksState {
68+
&state.benchmarks
69+
}
70+
}
71+
5472
pub trait Benchmarks {
5573
async fn get_verified_price_feeds(
5674
&self,
@@ -59,22 +77,25 @@ pub trait Benchmarks {
5977
) -> Result<PriceFeedsWithUpdateData>;
6078
}
6179

62-
#[async_trait::async_trait]
63-
impl Benchmarks for crate::state::State {
80+
impl<T> Benchmarks for T
81+
where
82+
for<'a> &'a T: Into<&'a BenchmarksState>,
83+
T: Sync,
84+
{
6485
async fn get_verified_price_feeds(
6586
&self,
6687
price_ids: &[PriceIdentifier],
6788
publish_time: UnixTimestamp,
6889
) -> Result<PriceFeedsWithUpdateData> {
6990
let endpoint = self
70-
.benchmarks_endpoint
91+
.into()
92+
.endpoint
7193
.as_ref()
7294
.ok_or_else(|| anyhow::anyhow!("Benchmarks endpoint is not set"))?
7395
.join(&format!("/v1/updates/price/{}", publish_time))
7496
.unwrap();
7597

76-
let client = reqwest::Client::new();
77-
let mut request = client
98+
let mut request = reqwest::Client::new()
7899
.get(endpoint)
79100
.timeout(BENCHMARKS_REQUEST_TIMEOUT)
80101
.query(&[("encoding", "hex")])

0 commit comments

Comments
 (0)