Răsfoiți Sursa

refactor(hermes): introduce Benchmarks trait

Reisen 2 ani în urmă
părinte
comite
cbeada6c6d

+ 1 - 1
hermes/src/api/rest/get_price_feed.rs

@@ -66,7 +66,7 @@ pub async fn get_price_feed(
     let price_id: PriceIdentifier = params.id.into();
 
     let price_feeds_with_update_data = crate::store::get_price_feeds_with_update_data(
-        &state.store,
+        &*state.store,
         vec![price_id],
         RequestTime::FirstAfter(params.publish_time),
     )

+ 1 - 1
hermes/src/api/rest/get_vaa.rs

@@ -76,7 +76,7 @@ pub async fn get_vaa(
     let price_id: PriceIdentifier = params.id.into();
 
     let price_feeds_with_update_data = store::get_price_feeds_with_update_data(
-        &state.store,
+        &*state.store,
         vec![price_id],
         RequestTime::FirstAfter(params.publish_time),
     )

+ 1 - 1
hermes/src/api/rest/get_vaa_ccip.rs

@@ -69,7 +69,7 @@ pub async fn get_vaa_ccip(
     );
 
     let price_feeds_with_update_data = crate::store::get_price_feeds_with_update_data(
-        &state.store,
+        &*state.store,
         vec![price_id],
         RequestTime::FirstAfter(publish_time),
     )

+ 1 - 1
hermes/src/api/rest/latest_price_feeds.rs

@@ -64,7 +64,7 @@ pub async fn latest_price_feeds(
 ) -> Result<Json<Vec<RpcPriceFeed>>, RestError> {
     let price_ids: Vec<PriceIdentifier> = params.ids.into_iter().map(|id| id.into()).collect();
     let price_feeds_with_update_data = crate::store::get_price_feeds_with_update_data(
-        &state.store,
+        &*state.store,
         price_ids,
         RequestTime::Latest,
     )

+ 1 - 1
hermes/src/api/rest/latest_vaas.rs

@@ -59,7 +59,7 @@ pub async fn latest_vaas(
 ) -> Result<Json<Vec<String>>, RestError> {
     let price_ids: Vec<PriceIdentifier> = params.ids.into_iter().map(|id| id.into()).collect();
     let price_feeds_with_update_data = crate::store::get_price_feeds_with_update_data(
-        &state.store,
+        &*state.store,
         price_ids,
         RequestTime::Latest,
     )

+ 1 - 1
hermes/src/api/ws.rs

@@ -150,7 +150,7 @@ impl Subscriber {
     async fn handle_price_feeds_update(&mut self) -> Result<()> {
         let price_feed_ids = self.price_feeds_with_config.keys().cloned().collect();
         for update in crate::store::get_price_feeds_with_update_data(
-            &self.store,
+            &*self.store,
             price_feed_ids,
             RequestTime::Latest,
         )

+ 9 - 11
hermes/src/store.rs

@@ -12,6 +12,7 @@ use std::time::{
 };
 use {
     self::{
+        benchmarks::Benchmarks,
         cache::{
             Cache,
             CacheStore,
@@ -329,23 +330,20 @@ where
     })
 }
 
-pub async fn get_price_feeds_with_update_data(
-    store: &Store,
+pub async fn get_price_feeds_with_update_data<S>(
+    store: &S,
     price_ids: Vec<PriceIdentifier>,
     request_time: RequestTime,
-) -> Result<PriceFeedsWithUpdateData> {
+) -> Result<PriceFeedsWithUpdateData>
+where
+    S: CacheStore,
+    S: Benchmarks,
+{
     match get_verified_price_feeds(store, price_ids.clone(), request_time.clone()).await {
         Ok(price_feeds_with_update_data) => Ok(price_feeds_with_update_data),
         Err(e) => {
             if let RequestTime::FirstAfter(publish_time) = request_time {
-                if let Some(endpoint) = &store.benchmarks_endpoint {
-                    return benchmarks::get_price_feeds_with_update_data_from_benchmarks(
-                        endpoint.clone(),
-                        price_ids,
-                        publish_time,
-                    )
-                    .await;
-                }
+                return Benchmarks::get_verified_price_feeds(store, price_ids, publish_time).await;
             }
             Err(e)
         }

+ 29 - 23
hermes/src/store/benchmarks.rs

@@ -15,7 +15,6 @@ use {
         PriceFeed,
         PriceIdentifier,
     },
-    reqwest::Url,
 };
 
 const BENCHMARKS_REQUEST_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(30);
@@ -76,36 +75,43 @@ impl TryFrom<BenchmarkUpdates> for PriceFeedsWithUpdateData {
     }
 }
 
-trait Benchmarks {
-    fn get_verified_price_feeds(
+#[async_trait::async_trait]
+pub trait Benchmarks {
+    async fn get_verified_price_feeds(
         &self,
         price_ids: Vec<PriceIdentifier>,
         publish_time: UnixTimestamp,
     ) -> Result<PriceFeedsWithUpdateData>;
 }
 
-pub async fn get_price_feeds_with_update_data_from_benchmarks(
-    endpoint: Url,
-    price_ids: Vec<PriceIdentifier>,
-    publish_time: UnixTimestamp,
-) -> Result<PriceFeedsWithUpdateData> {
-    let endpoint = endpoint
-        .join(&format!("/v1/updates/price/{}", publish_time))
-        .unwrap();
+#[async_trait::async_trait]
+impl Benchmarks for crate::store::Store {
+    async fn get_verified_price_feeds(
+        &self,
+        price_ids: Vec<PriceIdentifier>,
+        publish_time: UnixTimestamp,
+    ) -> Result<PriceFeedsWithUpdateData> {
+        let endpoint = self
+            .benchmarks_endpoint
+            .as_ref()
+            .ok_or_else(|| anyhow::anyhow!("Benchmarks endpoint is not set"))?
+            .join(&format!("/v1/updates/price/{}", publish_time))
+            .unwrap();
 
-    let client = reqwest::Client::new();
-    let mut request = client
-        .get(endpoint)
-        .timeout(BENCHMARKS_REQUEST_TIMEOUT)
-        .query(&[("encoding", "hex")])
-        .query(&[("parsed", "true")]);
+        let client = reqwest::Client::new();
+        let mut request = client
+            .get(endpoint)
+            .timeout(BENCHMARKS_REQUEST_TIMEOUT)
+            .query(&[("encoding", "hex")])
+            .query(&[("parsed", "true")]);
 
-    for price_id in price_ids {
-        request = request.query(&[("ids", price_id)])
-    }
+        for price_id in price_ids {
+            request = request.query(&[("ids", price_id)])
+        }
 
-    let response = request.send().await?;
+        let response = request.send().await?;
 
-    let benchmark_updates: BenchmarkUpdates = response.json().await?;
-    benchmark_updates.try_into()
+        let benchmark_updates: BenchmarkUpdates = response.json().await?;
+        benchmark_updates.try_into()
+    }
 }