-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(forge): add forge sig-collision cmd #4973
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
use crate::{ | ||
cmd::{forge::build::CoreBuildArgs, Cmd}, | ||
}; | ||
use clap::Parser; | ||
use ethers::{ | ||
prelude::{ | ||
info::ContractInfo | ||
}, | ||
solc::{ | ||
utils::canonicalize | ||
} | ||
}; | ||
use foundry_common::compile; | ||
use tracing::trace; | ||
|
||
#[derive(Debug, Clone, Parser)] | ||
pub struct SigCollisionArgs { | ||
#[clap( | ||
help = "The first of the two contracts for which to look method signature collisions in the form `(<path>:)?<contractname>`.", | ||
value_name = "FIRST_CONTRACT" | ||
)] | ||
pub first_contract: ContractInfo, | ||
|
||
#[clap( | ||
help = "The second of the two contracts for which to look method signature collisions in the form `(<path>:)?<contractname>`.", | ||
value_name = "SECOND_CONTRACT" | ||
)] | ||
pub second_contract: ContractInfo, | ||
|
||
/// Support build arguments | ||
#[clap(flatten)] | ||
build: CoreBuildArgs, | ||
} | ||
|
||
impl Cmd for SigCollisionArgs { | ||
type Output = (); | ||
|
||
fn run(self) -> eyre::Result<Self::Output> { | ||
let SigCollisionArgs {mut first_contract, mut second_contract, build} = self; | ||
|
||
trace!(target: "forge", ?first_contract, ?second_contract, "running forge sig-collision"); | ||
|
||
println!("{} {}", first_contract.path.as_ref().unwrap(), second_contract.path.as_ref().unwrap()); | ||
|
||
// Build first project | ||
let first_project = build.project()?; | ||
let first_outcome = if let Some(ref mut contract_path) = first_contract.path { | ||
let target_path = canonicalize(&*contract_path)?; | ||
*contract_path = target_path.to_string_lossy().to_string(); | ||
compile::compile_files(&first_project, vec![target_path], true) | ||
} else { | ||
compile::suppress_compile(&first_project) | ||
}?; | ||
|
||
// Build second project | ||
let second_project = build.project()?; | ||
let second_outcome = if let Some(ref mut contract_path) = second_contract.path { | ||
let target_path = canonicalize(&*contract_path)?; | ||
*contract_path = target_path.to_string_lossy().to_string(); | ||
compile::compile_files(&second_project, vec![target_path], true) | ||
} else { | ||
compile::suppress_compile(&second_project) | ||
}?; | ||
|
||
// Find the artifacts | ||
let first_found_artifact = first_outcome.find_contract(&first_contract); | ||
let second_found_artifact = second_outcome.find_contract(&second_contract); | ||
|
||
trace!(target: "forge", artifact=?first_found_artifact, input=?first_contract, "Found artifact"); | ||
trace!(target: "forge", artifact=?second_found_artifact, input=?second_contract, "Found artifact"); | ||
|
||
// Unwrapping inner artifacts | ||
let first_artifact = first_found_artifact.ok_or_else( || { | ||
eyre::eyre!("Failed to extract first artifact bytecode as a string") | ||
})?; | ||
let second_artifact = second_found_artifact.ok_or_else( || { | ||
eyre::eyre!("Failed to extract second artifact bytecode as a string") | ||
})?; | ||
|
||
let first_method_map = first_artifact.method_identifiers.as_ref().unwrap(); | ||
let second_method_map = second_artifact.method_identifiers.as_ref().unwrap(); | ||
|
||
let mut clashing_methods = Vec::new(); | ||
for (k1, v1) in first_method_map { | ||
if let Some(k2) = second_method_map.iter().find_map(| (k2, v2) | if v1 == v2 {Some(k2)} else {None} ) { | ||
clashing_methods.push((k1.clone(), k2.clone())) | ||
}; | ||
} | ||
|
||
if clashing_methods.is_empty() { | ||
println!("No clashing methods between the two contracts."); | ||
} else { | ||
println!("The two contracts have the following methods whose signatures clash: {:#?}", | ||
clashing_methods | ||
); | ||
} | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please adhere to the style of the other Clap Parser structs? This would mean the
help
andabout
are turned into doc comments, andvalue_name
is removed as it's redundant.