Skip to content

Commit

Permalink
extract duplicate method for CachedSignatures
Browse files Browse the repository at this point in the history
  • Loading branch information
byteshijinn committed Jul 12, 2024
1 parent 82e7c67 commit fa0d4de
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 45 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions crates/cast/bin/cmd/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,8 @@ impl RunArgs {
// System transactions such as on L2s don't contain any pricing info so
// we skip them otherwise this would cause
// reverts
if is_known_system_sender(tx.from)
|| tx.transaction_type == Some(SYSTEM_TRANSACTION_TYPE)
if is_known_system_sender(tx.from) ||
tx.transaction_type == Some(SYSTEM_TRANSACTION_TYPE)
{
pb.set_position((index + 1) as u64);
continue;
Expand Down
1 change: 0 additions & 1 deletion crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ indicatif = "0.17"
once_cell.workspace = true
regex = { version = "1", default-features = false }
serde.workspace = true
serde_json.workspace = true
strsim = "0.11"
strum = { workspace = true, features = ["derive"] }
tokio = { workspace = true, features = ["macros"] }
Expand Down
44 changes: 16 additions & 28 deletions crates/cli/src/utils/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use foundry_evm::{
render_trace_arena, CallTraceDecoder, CallTraceDecoderBuilder, TraceKind, Traces,
},
};
use serde_json;
use std::{
fmt::Write,
path::{Path, PathBuf},
Expand Down Expand Up @@ -162,16 +161,16 @@ pub fn init_progress(len: u64, label: &str) -> indicatif::ProgressBar {
/// True if the network calculates gas costs differently.
pub fn has_different_gas_calc(chain_id: u64) -> bool {
if let Some(chain) = Chain::from(chain_id).named() {
return matches!(
return matches! (
chain,
NamedChain::Arbitrum
| NamedChain::ArbitrumTestnet
| NamedChain::ArbitrumGoerli
| NamedChain::ArbitrumSepolia
| NamedChain::Moonbeam
| NamedChain::Moonriver
| NamedChain::Moonbase
| NamedChain::MoonbeamDev
NamedChain::Arbitrum |
NamedChain::ArbitrumTestnet |
NamedChain::ArbitrumGoerli |
NamedChain::ArbitrumSepolia |
NamedChain::Moonbeam |
NamedChain::Moonriver |
NamedChain::Moonbase |
NamedChain::MoonbeamDev
);
}
false
Expand All @@ -182,10 +181,10 @@ pub fn has_batch_support(chain_id: u64) -> bool {
if let Some(chain) = Chain::from(chain_id).named() {
return !matches!(
chain,
NamedChain::Arbitrum
| NamedChain::ArbitrumTestnet
| NamedChain::ArbitrumGoerli
| NamedChain::ArbitrumSepolia
NamedChain::Arbitrum |
NamedChain::ArbitrumTestnet |
NamedChain::ArbitrumGoerli |
NamedChain::ArbitrumSepolia
);
}
true
Expand Down Expand Up @@ -429,22 +428,11 @@ pub async fn print_traces(result: &mut TraceResult, decoder: &CallTraceDecoder)
Ok(())
}

/// Traverse the artifacts in the project to generate local signatures and merge them into the cache file.
/// Traverse the artifacts in the project to generate local signatures and merge them into the cache
/// file.
pub fn cache_local_signatures(output: &ProjectCompileOutput, cache_path: PathBuf) -> Result<()> {
let mut cached_signatures = CachedSignatures::default();
let path = cache_path.join("signatures");
if !path.is_file() {
std::fs::create_dir_all(&cache_path)?;
} else {
let cache_contents = std::fs::read_to_string(path.clone()).unwrap();
match serde_json::from_str::<CachedSignatures>(&cache_contents) {
Ok(existed_signatures) => cached_signatures = existed_signatures,
Err(e) => {
println!("parse cached local signatures file error: {}", e);
}
}
dbg!(&cached_signatures);
}
let mut cached_signatures = CachedSignatures::load(cache_path.clone());
output.artifacts().for_each(|(_, artifact)| {
if let Some(abi) = &artifact.abi {
for func in abi.functions() {
Expand Down
34 changes: 23 additions & 11 deletions crates/evm/traces/src/identifier/signatures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,25 @@ pub struct CachedSignatures {
pub functions: BTreeMap<String, String>,
}

impl CachedSignatures {
#[instrument(target = "evm::traces")]
pub fn load(
cache_path: PathBuf
) -> CachedSignatures {
let path = cache_path.join("signatures");
let cached = if path.is_file() {
fs::read_json_file(&cache_path)
.map_err(|err| warn!(target: "evm::traces", ?path, ?err, "failed to read cache file"))
.unwrap_or_default()
} else {
if let Err(err) = std::fs::create_dir_all(cache_path) {
warn!(target: "evm::traces", "could not create signatures cache dir: {:?}", err);
}
CachedSignatures::default()
};
cached
}
}
/// An identifier that tries to identify functions and events using signatures found at
/// `https://openchain.xyz` or a local cache.
#[derive(Debug)]
Expand All @@ -46,17 +65,10 @@ impl SignaturesIdentifier {
let identifier = if let Some(cache_path) = cache_path {
let path = cache_path.join("signatures");
trace!(target: "evm::traces", ?path, "reading signature cache");
let cached = if path.is_file() {
fs::read_json_file(&path)
.map_err(|err| warn!(target: "evm::traces", ?path, ?err, "failed to read cache file"))
.unwrap_or_default()
} else {
if let Err(err) = std::fs::create_dir_all(cache_path) {
warn!(target: "evm::traces", "could not create signatures cache dir: {:?}", err);
}
CachedSignatures::default()
};
Self { cached, cached_path: Some(path), unavailable: HashSet::new(), client }
let cached = CachedSignatures::load(cache_path.clone());
Self {
cached, cached_path: Some(path), unavailable: HashSet::new(), client
}
} else {
Self {
cached: Default::default(),
Expand Down
4 changes: 2 additions & 2 deletions crates/forge/bin/cmd/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ impl BuildArgs {
pub fn run(self) -> Result<ProjectCompileOutput> {
let mut config = self.try_load_config_emit_warnings()?;

if install::install_missing_dependencies(&mut config, self.args.silent)
&& config.auto_detect_remappings
if install::install_missing_dependencies(&mut config, self.args.silent) &&
config.auto_detect_remappings
{
// need to re-configure here to also catch additional remappings
config = self.load_config();
Expand Down

0 comments on commit fa0d4de

Please sign in to comment.