Skip to content

Commit

Permalink
chore: don't build OpenChain client if offline (#8390)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes authored Jul 8, 2024
1 parent 8b694bb commit 0116be1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 22 deletions.
6 changes: 4 additions & 2 deletions crates/common/src/selectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use crate::abi::abi_decode_calldata;
use alloy_json_abi::JsonAbi;
use eyre::Context;
use reqwest::header::{HeaderMap, HeaderName, HeaderValue};
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use std::{
Expand Down Expand Up @@ -39,14 +40,15 @@ pub struct OpenChainClient {

impl OpenChainClient {
/// Creates a new client with default settings
pub fn new() -> reqwest::Result<Self> {
pub fn new() -> eyre::Result<Self> {
let inner = reqwest::Client::builder()
.default_headers(HeaderMap::from_iter([(
HeaderName::from_static("user-agent"),
HeaderValue::from_static("forge"),
)]))
.timeout(REQ_TIMEOUT)
.build()?;
.build()
.wrap_err("failed to build OpenChain client")?;
Ok(Self {
inner,
spurious_connection: Arc::new(Default::default()),
Expand Down
30 changes: 10 additions & 20 deletions crates/evm/traces/src/identifier/signatures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,17 @@ struct CachedSignatures {
}

/// An identifier that tries to identify functions and events using signatures found at
/// `https://openchain.xyz`.
/// `https://openchain.xyz` or a local cache.
#[derive(Debug)]
pub struct SignaturesIdentifier {
/// Cached selectors for functions and events
/// Cached selectors for functions and events.
cached: CachedSignatures,
/// Location where to save `CachedSignatures`
/// Location where to save `CachedSignatures`.
cached_path: Option<PathBuf>,
/// Selectors that were unavailable during the session.
unavailable: HashSet<String>,
/// The API client to fetch signatures from
sign_eth_api: OpenChainClient,
/// whether traces should be decoded via `sign_eth_api`
offline: bool,
/// The OpenChain client to fetch signatures from.
client: Option<OpenChainClient>,
}

impl SignaturesIdentifier {
Expand All @@ -43,7 +41,7 @@ impl SignaturesIdentifier {
cache_path: Option<PathBuf>,
offline: bool,
) -> eyre::Result<SingleSignaturesIdentifier> {
let sign_eth_api = OpenChainClient::new()?;
let client = if !offline { Some(OpenChainClient::new()?) } else { None };

let identifier = if let Some(cache_path) = cache_path {
let path = cache_path.join("signatures");
Expand All @@ -58,20 +56,13 @@ impl SignaturesIdentifier {
}
CachedSignatures::default()
};
Self {
cached,
cached_path: Some(path),
unavailable: HashSet::new(),
sign_eth_api,
offline,
}
Self { cached, cached_path: Some(path), unavailable: HashSet::new(), client }
} else {
Self {
cached: Default::default(),
cached_path: None,
unavailable: HashSet::new(),
sign_eth_api,
offline,
client,
}
};

Expand Down Expand Up @@ -110,15 +101,14 @@ impl SignaturesIdentifier {
let hex_identifiers: Vec<String> =
identifiers.into_iter().map(hex::encode_prefixed).collect();

if !self.offline {
if let Some(client) = &self.client {
let query: Vec<_> = hex_identifiers
.iter()
.filter(|v| !cache.contains_key(v.as_str()))
.filter(|v| !self.unavailable.contains(v.as_str()))
.collect();

if let Ok(res) = self.sign_eth_api.decode_selectors(selector_type, query.clone()).await
{
if let Ok(res) = client.decode_selectors(selector_type, query.clone()).await {
for (hex_id, selector_result) in query.into_iter().zip(res.into_iter()) {
let mut found = false;
if let Some(decoded_results) = selector_result {
Expand Down

0 comments on commit 0116be1

Please sign in to comment.