Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/apollo_consensus_orchestrator/src/build_proposal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use apollo_batcher_types::batcher_types::{
use apollo_batcher_types::communication::{BatcherClient, BatcherClientError};
use apollo_consensus::types::{ProposalCommitment, Round};
use apollo_consensus_orchestrator_config::config::PricePerHeight;
use apollo_l1_gas_price_types::errors::{EthToStrkOracleClientError, L1GasPriceClientError};
use apollo_l1_gas_price_types::errors::{L1GasPriceClientError, PriceOracleClientError};
use apollo_protobuf::consensus::{
BuildParam,
CommitmentParts,
Expand Down Expand Up @@ -94,7 +94,7 @@ pub(crate) enum BuildProposalError {
#[error("Failed to send proposal part: {0}")]
SendError(String),
#[error("EthToStrkOracle error: {0}")]
EthToStrkOracle(#[from] EthToStrkOracleClientError),
EthToStrkOracle(#[from] PriceOracleClientError),
#[error("L1GasPriceProvider error: {0}")]
L1GasPriceProvider(#[from] L1GasPriceClientError),
#[error("Proposal interrupted.")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ use apollo_consensus_orchestrator_config::config::{
PricePerHeight,
};
use apollo_l1_gas_price_types::errors::{
EthToStrkOracleClientError,
L1GasPriceClientError,
L1GasPriceProviderError,
PriceOracleClientError,
};
use apollo_l1_gas_price_types::{MockL1GasPriceProviderClient, PriceInfo};
use apollo_protobuf::consensus::{
Expand Down Expand Up @@ -852,8 +852,8 @@ async fn oracle_fails_on_startup(#[case] l1_oracle_failure: bool) {
})
});
l1_prices_oracle_client.expect_get_eth_to_fri_rate().times(1).return_once(|_| {
Err(L1GasPriceClientError::EthToStrkOracleClientError(
EthToStrkOracleClientError::MissingFieldError("".to_string(), "".to_string()),
Err(L1GasPriceClientError::PriceOracleClientError(
PriceOracleClientError::MissingFieldError("".to_string(), "".to_string()),
))
});
deps.l1_gas_price_provider = l1_prices_oracle_client;
Expand Down Expand Up @@ -951,8 +951,8 @@ async fn oracle_fails_on_second_block(#[case] l1_oracle_failure: bool) {
.return_once(|_| Ok(ETH_TO_FRI_RATE));
// Set the eth_to_fri_rate to fail on second block.
l1_prices_oracle_client.expect_get_eth_to_fri_rate().times(1).return_once(|_| {
Err(L1GasPriceClientError::EthToStrkOracleClientError(
EthToStrkOracleClientError::MissingFieldError("".to_string(), "".to_string()),
Err(L1GasPriceClientError::PriceOracleClientError(
PriceOracleClientError::MissingFieldError("".to_string(), "".to_string()),
))
});
deps.l1_gas_price_provider = l1_prices_oracle_client;
Expand Down
4 changes: 2 additions & 2 deletions crates/apollo_consensus_orchestrator/src/validate_proposal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use apollo_batcher_types::batcher_types::{
use apollo_batcher_types::communication::{BatcherClient, BatcherClientError};
use apollo_batcher_types::errors::BatcherError;
use apollo_consensus::types::ProposalCommitment;
use apollo_l1_gas_price_types::errors::{EthToStrkOracleClientError, L1GasPriceClientError};
use apollo_l1_gas_price_types::errors::{L1GasPriceClientError, PriceOracleClientError};
use apollo_l1_gas_price_types::L1GasPriceProviderClient;
use apollo_protobuf::consensus::{ProposalFin, ProposalInit, ProposalPart, TransactionBatch};
use apollo_state_sync_types::communication::SharedStateSyncClient;
Expand Down Expand Up @@ -111,7 +111,7 @@ pub(crate) enum ValidateProposalError {
#[error("Failed to send commitment to consensus: {0}")]
SendError(ProposalCommitment),
#[error("EthToStrkOracle error: {0}")]
EthToStrkOracle(#[from] EthToStrkOracleClientError),
EthToStrkOracle(#[from] PriceOracleClientError),
#[error("L1GasPriceProvider error: {0}")]
L1GasPriceProvider(#[from] L1GasPriceClientError),
#[error("ProposalInit conversion error: {0}")]
Expand Down
33 changes: 14 additions & 19 deletions crates/apollo_l1_gas_price/src/eth_to_strk_oracle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::time::Duration;

use apollo_config::secrets::Sensitive;
use apollo_l1_gas_price_config::config::EthToStrkOracleConfig;
use apollo_l1_gas_price_types::errors::EthToStrkOracleClientError;
use apollo_l1_gas_price_types::errors::PriceOracleClientError;
use apollo_l1_gas_price_types::EthToStrkOracleClientTrait;
use apollo_metrics::metrics::set_unix_now_seconds;
use async_trait::async_trait;
Expand Down Expand Up @@ -52,7 +52,7 @@ pub struct UrlAndHeaderMap {
pub headers: Sensitive<HeaderMap>,
}

type PriceQuery = AbortOnDropHandle<Result<u128, EthToStrkOracleClientError>>;
type PriceQuery = AbortOnDropHandle<Result<u128, PriceOracleClientError>>;

/// Client for interacting with the eth to strk Oracle API.
#[derive(Clone, Debug)]
Expand Down Expand Up @@ -101,7 +101,7 @@ impl EthToStrkOracleClient {
fn spawn_query(
&self,
quantized_timestamp: u64,
) -> AbortOnDropHandle<Result<u128, EthToStrkOracleClientError>> {
) -> AbortOnDropHandle<Result<u128, PriceOracleClientError>> {
assert!(
self.config.lag_interval_seconds > 0,
"lag_interval_seconds should be greater than 0"
Expand All @@ -127,7 +127,7 @@ impl EthToStrkOracleClient {
.await?;
let body = response.text().await?;
let rate = resolve_query(body)?;
Ok::<_, EthToStrkOracleClientError>(rate)
Ok::<_, PriceOracleClientError>(rate)
})
.await;

Expand All @@ -148,40 +148,35 @@ impl EthToStrkOracleClient {
ETH_TO_STRK_ERROR_COUNT.increment(1);
}
warn!("All {list_len} URLs in the list failed for timestamp {adjusted_timestamp}");
Err(EthToStrkOracleClientError::AllUrlsFailedError(adjusted_timestamp, initial_index))
Err(PriceOracleClientError::AllUrlsFailedError(adjusted_timestamp, initial_index))
};
AbortOnDropHandle::new(tokio::spawn(future))
}
}

fn resolve_query(body: String) -> Result<u128, EthToStrkOracleClientError> {
fn resolve_query(body: String) -> Result<u128, PriceOracleClientError> {
let Ok(json): Result<serde_json::Value, _> = serde_json::from_str(&body) else {
return Err(EthToStrkOracleClientError::ParseError(format!(
"Failed to parse JSON: {body}"
)));
return Err(PriceOracleClientError::ParseError(format!("Failed to parse JSON: {body}")));
};
// Extract price from API response. Also returns MissingFieldError if value is not a string.
let price = match json.get("price").and_then(|v| v.as_str()) {
Some(price) => price,
None => {
return Err(EthToStrkOracleClientError::MissingFieldError("price".to_string(), body));
return Err(PriceOracleClientError::MissingFieldError("price".to_string(), body));
}
};
let rate = u128::from_str_radix(price.trim_start_matches("0x"), 16).map_err(|e| {
EthToStrkOracleClientError::ParseError(format!("Failed to parse price {price}: {e}"))
PriceOracleClientError::ParseError(format!("Failed to parse price {price}: {e}"))
})?;
// Extract decimals from API response. Also returns MissingFieldError if value is not a number.
let decimals = match json.get("decimals").and_then(|v| v.as_u64()) {
Some(decimals) => decimals,
None => {
return Err(EthToStrkOracleClientError::MissingFieldError(
"decimals".to_string(),
body,
));
return Err(PriceOracleClientError::MissingFieldError("decimals".to_string(), body));
}
};
if decimals != ETH_TO_STRK_QUANTIZATION {
return Err(EthToStrkOracleClientError::InvalidDecimalsError(
return Err(PriceOracleClientError::InvalidDecimalsError(
ETH_TO_STRK_QUANTIZATION,
decimals,
));
Expand All @@ -198,7 +193,7 @@ impl EthToStrkOracleClientTrait for EthToStrkOracleClient {
/// - `price`: a hexadecimal string representing the price.
/// - `decimals`: a `u64` value, must be equal to `ETH_TO_STRK_QUANTIZATION`.
#[instrument(skip(self))]
async fn eth_to_fri_rate(&self, timestamp: u64) -> Result<u128, EthToStrkOracleClientError> {
async fn eth_to_fri_rate(&self, timestamp: u64) -> Result<u128, PriceOracleClientError> {
const NUMBER_OF_TIMESTAMPS_BACK: u64 = 1;
let quantized_timestamp = (timestamp - self.config.lag_interval_seconds)
.checked_div(self.config.lag_interval_seconds)
Expand Down Expand Up @@ -229,7 +224,7 @@ impl EthToStrkOracleClientTrait for EthToStrkOracleClient {
return Ok(*rate);
}
// If not, return a query not ready error.
return Err(EthToStrkOracleClientError::QueryNotReadyError(timestamp));
return Err(PriceOracleClientError::QueryNotReadyError(timestamp));
}
let result = handle.now_or_never().expect("Handle must be finished if we got here");
let rate = match result {
Expand All @@ -245,7 +240,7 @@ impl EthToStrkOracleClientTrait for EthToStrkOracleClient {
ETH_TO_STRK_ERROR_COUNT.increment(1);
// Must remove failed query from the cache, to avoid re-polling it.
queries.pop(&quantized_timestamp);
return Err(EthToStrkOracleClientError::JoinError(e.to_string()));
return Err(PriceOracleClientError::JoinError(e.to_string()));
}
};

Expand Down
6 changes: 3 additions & 3 deletions crates/apollo_l1_gas_price/src/eth_to_strk_oracle_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::BTreeMap;
use std::time::Duration;

use apollo_config::converters::UrlAndHeaders;
use apollo_l1_gas_price_types::errors::EthToStrkOracleClientError;
use apollo_l1_gas_price_types::errors::PriceOracleClientError;
use apollo_l1_gas_price_types::EthToStrkOracleClientTrait;
use mockito::{Mock, ServerGuard};
use serde_json::json;
Expand Down Expand Up @@ -211,8 +211,8 @@ async fn eth_to_fri_rate_two_urls() {
loop {
match client.eth_to_fri_rate(TIMESTAMP2).await {
Ok(_) => panic!("Both servers should be returning bad JSON!"),
Err(EthToStrkOracleClientError::QueryNotReadyError(_)) => {}
Err(EthToStrkOracleClientError::AllUrlsFailedError(_, index)) => {
Err(PriceOracleClientError::QueryNotReadyError(_)) => {}
Err(PriceOracleClientError::AllUrlsFailedError(_, index)) => {
assert!(index == 1, "Last error should be index 1 (server2).");
break; // This is the expected error, since server1 and 2 returned bad JSON.
}
Expand Down
2 changes: 1 addition & 1 deletion crates/apollo_l1_gas_price/src/l1_gas_price_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ impl L1GasPriceProvider {
self.eth_to_strk_oracle_client
.eth_to_fri_rate(timestamp)
.await
.map_err(L1GasPriceProviderError::EthToStrkOracleClientError)
.map_err(L1GasPriceProviderError::PriceOracleClientError)
}
}

Expand Down
10 changes: 5 additions & 5 deletions crates/apollo_l1_gas_price_types/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub enum L1GasPriceProviderError {
)]
StaleL1GasPricesError { current_timestamp: u64, last_valid_price_timestamp: u64 },
#[error(transparent)]
EthToStrkOracleClientError(#[from] EthToStrkOracleClientError),
PriceOracleClientError(#[from] PriceOracleClientError),
}

#[derive(Clone, Debug, Error)]
Expand All @@ -28,11 +28,11 @@ pub enum L1GasPriceClientError {
#[error(transparent)]
L1GasPriceProviderError(#[from] L1GasPriceProviderError),
#[error(transparent)]
EthToStrkOracleClientError(#[from] EthToStrkOracleClientError),
PriceOracleClientError(#[from] PriceOracleClientError),
}

#[derive(Clone, Debug, Error, Serialize, Deserialize, PartialEq, Eq)]
pub enum EthToStrkOracleClientError {
pub enum PriceOracleClientError {
#[error("Join error: {0}")]
JoinError(String),
#[error("Request error: {0}")]
Expand All @@ -49,8 +49,8 @@ pub enum EthToStrkOracleClientError {
AllUrlsFailedError(u64, usize),
}

impl From<reqwest::Error> for EthToStrkOracleClientError {
impl From<reqwest::Error> for PriceOracleClientError {
fn from(value: reqwest::Error) -> Self {
EthToStrkOracleClientError::RequestError(value.to_string())
PriceOracleClientError::RequestError(value.to_string())
}
}
4 changes: 2 additions & 2 deletions crates/apollo_l1_gas_price_types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use apollo_infra::{
};
use apollo_metrics::generate_permutation_labels;
use async_trait::async_trait;
use errors::{EthToStrkOracleClientError, L1GasPriceClientError, L1GasPriceProviderError};
use errors::{L1GasPriceClientError, L1GasPriceProviderError, PriceOracleClientError};
#[cfg(any(feature = "testing", test))]
use mockall::automock;
use papyrus_base_layer::L1BlockNumber;
Expand Down Expand Up @@ -106,7 +106,7 @@ pub trait L1GasPriceProviderClient: Send + Sync {
#[async_trait]
pub trait EthToStrkOracleClientTrait: Send + Sync + Debug {
/// Fetches the eth to fri rate for a given timestamp.
async fn eth_to_fri_rate(&self, timestamp: u64) -> Result<u128, EthToStrkOracleClientError>;
async fn eth_to_fri_rate(&self, timestamp: u64) -> Result<u128, PriceOracleClientError>;
}

#[async_trait]
Expand Down
Loading