Skip to content

Commit

Permalink
replace err matching with unwrap && use only std mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
juan518munoz committed May 17, 2024
1 parent b5d9231 commit ac3c3e3
Showing 1 changed file with 13 additions and 39 deletions.
52 changes: 13 additions & 39 deletions core/node/base_token_fetcher/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use std::{
cmp::min,
str::FromStr,
sync::{Arc, Mutex as StdMutex},
sync::{Arc, Mutex},
time::Duration,
};

use anyhow::{anyhow, Context};
use anyhow::Context;
use async_trait::async_trait;
use bigdecimal::BigDecimal;
use hex::ToHex;
use tokio::{sync::Mutex, time::sleep};
use tokio::time::sleep;
use zksync_config::configs::BaseTokenFetcherConfig;

const MAX_CONVERSION_RATE_FETCH_RETRIES: u8 = 10;
Expand Down Expand Up @@ -51,7 +51,7 @@ impl ConversionRateFetcher for NoOpConversionRateFetcher {
#[derive(Debug)]
pub struct BaseTokenFetcher {
pub config: BaseTokenFetcherConfig,
pub latest_to_eth_conversion_rate: Arc<StdMutex<BigDecimal>>,
pub latest_to_eth_conversion_rate: Arc<Mutex<BigDecimal>>,
http_client: reqwest::Client,
error_reporter: Arc<Mutex<ErrorReporter>>,
}
Expand All @@ -78,7 +78,7 @@ impl BaseTokenFetcher {

Ok(Self {
config,
latest_to_eth_conversion_rate: Arc::new(StdMutex::new(conversion_rate)),
latest_to_eth_conversion_rate: Arc::new(Mutex::new(conversion_rate)),
http_client,
error_reporter,
})
Expand Down Expand Up @@ -119,7 +119,7 @@ impl BaseTokenFetcher {
let error_reporter = Arc::new(Mutex::new(ErrorReporter::new()));
Ok(Self {
config,
latest_to_eth_conversion_rate: Arc::new(StdMutex::new(conversion_rate)),
latest_to_eth_conversion_rate: Arc::new(Mutex::new(conversion_rate)),
http_client,
error_reporter,
})
Expand All @@ -129,20 +129,7 @@ impl BaseTokenFetcher {
#[async_trait]
impl ConversionRateFetcher for BaseTokenFetcher {
fn conversion_rate(&self) -> anyhow::Result<BigDecimal> {
let lock = match self.latest_to_eth_conversion_rate.lock() {
Ok(lock) => lock,
Err(err) => {
tracing::error!(
"Error while getting lock of latest conversion rate: {:?}",
err,
);
return Err(anyhow!(
"Error while getting lock of latest conversion rate: {:?}",
err,
));
}
};
anyhow::Ok(lock.clone())
Ok(self.latest_to_eth_conversion_rate.lock().unwrap().clone())
}

async fn update(&self) -> anyhow::Result<()> {
Expand All @@ -160,30 +147,17 @@ impl ConversionRateFetcher for BaseTokenFetcher {
let conversion_rate_str = response.json::<String>().await.context(
"Unable to parse the response of the native token conversion rate server",
)?;
match self.latest_to_eth_conversion_rate.lock() {
Ok(mut lock) => {
*lock = BigDecimal::from_str(&conversion_rate_str).context(
"Unable to parse the response of the native token conversion rate server",
)?;
}
Err(err) => {
tracing::error!(
"Error while getting lock of latest conversion rate: {:?}",
err,
);
return Err(anyhow!(
"Error while getting lock of latest conversion rate: {:?}",
err,
));
}
}
*self.latest_to_eth_conversion_rate.lock().unwrap() =
BigDecimal::from_str(&conversion_rate_str).context(
"Unable to parse the response of the native token conversion rate server",
)?;

self.error_reporter.lock().await.reset();
self.error_reporter.lock().unwrap().reset();
}
Err(err) => self
.error_reporter
.lock()
.await
.unwrap()
.process(anyhow::anyhow!(err)),
}

Expand Down

0 comments on commit ac3c3e3

Please sign in to comment.