Skip to content

Commit

Permalink
rename generate_local_signatures to cache_local_signatures
Browse files Browse the repository at this point in the history
merge project signatures with exists cached local signatures instead of
just override them
  • Loading branch information
byteshijinn committed Jul 6, 2024
1 parent 7a13ae4 commit 273a62e
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 17 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

11 changes: 5 additions & 6 deletions crates/cast/bin/cmd/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use clap::Parser;
use eyre::{Result, WrapErr};
use foundry_cli::{
opts::RpcOpts,
utils::{generate_local_signatures, handle_traces, init_progress, TraceResult},
utils::{cache_local_signatures, handle_traces, init_progress, TraceResult},
};
use foundry_common::{compile::ProjectCompiler, is_known_system_sender, SYSTEM_TRANSACTION_TYPE};
use foundry_compilers::artifacts::EvmVersion;
Expand Down Expand Up @@ -76,8 +76,8 @@ pub struct RunArgs {
/// The file will be saved in the foundry cache directory.
///
/// default value: false
#[arg(long, short = 'G', visible_alias = "gs")]
pub generate_local_signatures: bool,
#[arg(long, short = 'c', visible_alias = "cls")]
pub cache_local_signatures: bool,
}

impl RunArgs {
Expand Down Expand Up @@ -227,12 +227,11 @@ impl RunArgs {
}
};

if self.generate_local_signatures {
if self.cache_local_signatures {
let project = config.project()?;
let compiler = ProjectCompiler::new().quiet(true);
let output = compiler.compile(&project)?;
if let Err(err) =
generate_local_signatures(&output, Config::foundry_cache_dir().unwrap())
if let Err(err) = cache_local_signatures(&output, Config::foundry_cache_dir().unwrap())
{
warn!(target: "cast::run", ?err, "failed to flush signature cache");
} else {
Expand Down
1 change: 1 addition & 0 deletions crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ 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
4 changes: 2 additions & 2 deletions crates/cli/src/opts/build/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ pub struct CoreBuildArgs {
/// The file will be saved in the foundry cache directory.
///
/// default value: false
#[arg(long, short = 'G', visible_alias = "gs")]
#[arg(long, visible_alias = "cls")]
#[serde(skip)]
pub generate_local_signatures: bool,
pub cache_local_signatures: bool,
}

impl CoreBuildArgs {
Expand Down
22 changes: 17 additions & 5 deletions crates/cli/src/utils/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use foundry_evm::{
render_trace_arena, CallTraceDecoder, CallTraceDecoderBuilder, TraceKind, Traces,
},
};
use serde_json;
use std::{
fmt::Write,
path::{Path, PathBuf},
Expand Down Expand Up @@ -417,8 +418,22 @@ pub async fn print_traces(result: &mut TraceResult, decoder: &CallTraceDecoder)
Ok(())
}

pub fn generate_local_signatures(output: &ProjectCompileOutput, cache_path: PathBuf) -> Result<()> {
/// 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);
}
output.artifacts().for_each(|(_, artifact)| {
if let Some(abi) = &artifact.abi {
for func in abi.functions() {
Expand All @@ -431,10 +446,7 @@ pub fn generate_local_signatures(output: &ProjectCompileOutput, cache_path: Path
}
}
});
let path = cache_path.join("signatures");
if !path.is_file() {
std::fs::create_dir_all(&cache_path)?;
}

fs::write_json_file(&path, &cached_signatures)?;
Ok(())
}
7 changes: 3 additions & 4 deletions crates/forge/bin/cmd/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use clap::Parser;
use eyre::Result;
use foundry_cli::{
opts::CoreBuildArgs,
utils::{generate_local_signatures, LoadConfig},
utils::{cache_local_signatures, LoadConfig},
};
use foundry_common::compile::ProjectCompiler;
use foundry_compilers::{
Expand Down Expand Up @@ -115,9 +115,8 @@ impl BuildArgs {
println!("{}", serde_json::to_string_pretty(&output.output())?);
}

if self.args.generate_local_signatures {
if let Err(err) =
generate_local_signatures(&output, Config::foundry_cache_dir().unwrap())
if self.args.cache_local_signatures {
if let Err(err) = cache_local_signatures(&output, Config::foundry_cache_dir().unwrap())
{
warn!(target: "forge::build", ?err, "failed to flush signature cache");
} else {
Expand Down

0 comments on commit 273a62e

Please sign in to comment.