Skip to content

Commit

Permalink
refactor(evm core types): change find_cast return type from String to…
Browse files Browse the repository at this point in the history
… ParamType (#415)

Co-authored-by: Jon-Becker <[email protected]>
  • Loading branch information
iankressin and Jon-Becker authored May 30, 2024
1 parent e68c8f4 commit efc4406
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 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.

9 changes: 5 additions & 4 deletions crates/common/src/ether/evm/core/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,17 +284,18 @@ pub fn byte_size_to_type(byte_size: usize) -> (usize, Vec<String>) {
}

/// Given a string (typically a line of decompiled source code), extract a type cast if one exists.
// TODO: instead of returning a String, return a ParamType
///
/// ```
/// use heimdall_common::ether::evm::core::types::find_cast;
/// use ethers::abi::ParamType;
///
/// let line = "uint256(0x000011)";
/// let (range, cast_type) = find_cast(line).expect("failed to find type cast");
/// assert_eq!(range, 8..16);
/// assert_eq!(&line[range], "0x000011");
/// assert_eq!(cast_type, "uint256");
/// assert_eq!(cast_type, ParamType::Uint(256));
/// ```
pub fn find_cast(line: &str) -> Result<(Range<usize>, String), Error> {
pub fn find_cast(line: &str) -> Result<(Range<usize>, ParamType), Error> {
// find the start of the cast
match TYPE_CAST_REGEX.find(line).expect("Failed to find type cast.") {
Some(m) => {
Expand All @@ -304,7 +305,7 @@ pub fn find_cast(line: &str) -> Result<(Range<usize>, String), Error> {

// find where the cast ends
let range = find_balanced_encapsulator(&line[end..], ('(', ')'))?;
Ok((end + range.start..end + range.end, cast_type))
Ok((end + range.start..end + range.end, to_type(&cast_type)))
}
None => Err(Error::ParseError("failed to find type cast".to_string())),
}
Expand Down
5 changes: 3 additions & 2 deletions crates/decompile/src/utils/postprocessors/bitwise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,9 @@ pub fn simplify_casts(line: &str) -> String {

let cleaned_cast_pre = cleaned[0..cast_range.start - 1].to_string();
let cleaned_cast_post = cleaned[cast_range.end + 1..].to_string();
let cleaned_cast =
cleaned[cast_range.start - 1..cast_range.end + 1].to_string().replace(&cast, "");
let cleaned_cast = cleaned[cast_range.start - 1..cast_range.end + 1]
.to_string()
.replace(&cast.to_string(), "");

cleaned = format!("{cleaned_cast_pre}{cleaned_cast}{cleaned_cast_post}");

Expand Down

0 comments on commit efc4406

Please sign in to comment.