Skip to content

Commit

Permalink
feat(decompile): add support for LOG0 anonymous events
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon-Becker committed Dec 28, 2023
1 parent 61478dd commit a7fa324
Show file tree
Hide file tree
Showing 31 changed files with 164 additions and 161 deletions.
2 changes: 1 addition & 1 deletion cache/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ where
.as_secs()
{
delete_cache(key);
return None;
return None
}

c
Expand Down
6 changes: 3 additions & 3 deletions cache/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ pub fn prettify_bytes(bytes: u64) -> String {
format!("{bytes} B")
} else if bytes < 1024 * 1024 {
let kb = bytes / 1024;
return format!("{kb} KB");
return format!("{kb} KB")
} else if bytes < 1024 * 1024 * 1024 {
let mb = bytes / (1024 * 1024);
return format!("{mb} MB");
return format!("{mb} MB")
} else {
let gb = bytes / (1024 * 1024 * 1024);
return format!("{gb} GB");
return format!("{gb} GB")
}
}

Expand Down
4 changes: 2 additions & 2 deletions cli/src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ pub async fn build_output_path(

if ADDRESS_REGEX.is_match(target)? || TRANSACTION_HASH_REGEX.is_match(target)? {
let chain_id = rpc::chain_id(rpc_url).await?;
return Ok(format!("{}/output/{}/{}/{}", cwd, chain_id, target, filename));
return Ok(format!("{}/output/{}/{}/{}", cwd, chain_id, target, filename))
} else {
return Ok(format!("{}/output/local/{}", cwd, filename));
return Ok(format!("{}/output/local/{}", cwd, filename))
}
}

Expand Down
22 changes: 11 additions & 11 deletions common/src/ether/evm/core/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub fn parse_function_parameters(function_signature: &str) -> Option<Vec<ParamTy
// remove the function name from the signature, only keep the parameters
let (start, end, valid) = find_balanced_encapsulator(function_signature, ('(', ')'));
if !valid {
return None;
return None
}

let function_inputs = function_signature[start + 1..end - 1].to_string();
Expand All @@ -47,7 +47,7 @@ fn extract_types_from_string(string: &str) -> Option<Vec<ParamType>> {

// if string is empty, return None
if string.is_empty() {
return None;
return None
}

// if the string contains a tuple we cant simply split on commas
Expand All @@ -57,7 +57,7 @@ fn extract_types_from_string(string: &str) -> Option<Vec<ParamType>> {
// get balanced encapsulator
let (tuple_start, tuple_end, valid) = find_balanced_encapsulator(string, ('(', ')'));
if !valid {
return None;
return None
}

// extract the tuple
Expand All @@ -78,7 +78,7 @@ fn extract_types_from_string(string: &str) -> Option<Vec<ParamType>> {
if is_array {
let (start, end, valid) = find_balanced_encapsulator(split, ('[', ']'));
if !valid {
return None;
return None
}

let size = split[start + 1..end - 1].to_string();
Expand Down Expand Up @@ -156,7 +156,7 @@ fn extract_types_from_string(string: &str) -> Option<Vec<ParamType>> {
// iterate over the split string and convert each type to a ParamType
for string_type in split {
if string_type.is_empty() {
continue;
continue
}

let param_type = to_type(string_type);
Expand Down Expand Up @@ -191,7 +191,7 @@ pub fn to_type(string: &str) -> ParamType {
while string.ends_with(']') {
let (start, end, valid) = find_balanced_encapsulator(&string, ('[', ']'));
if !valid {
return ParamType::Bytes; // default to bytes if invalid
return ParamType::Bytes // default to bytes if invalid
}

let size = string[start + 1..end - 1].to_string();
Expand Down Expand Up @@ -239,7 +239,7 @@ pub fn to_type(string: &str) -> ParamType {
}
}

return arg_type;
return arg_type
}

arg_type
Expand Down Expand Up @@ -343,17 +343,17 @@ pub fn get_padding(bytes: &str) -> Padding {
if null_byte_indices.is_empty() ||
null_byte_indices[0] != 0 && null_byte_indices[null_byte_indices.len() - 1] != size - 1
{
return Padding::None;
return Padding::None
}

// the first byte is a null byte AND the last byte is not a null byte, it is left padded
if null_byte_indices[0] == 0 && null_byte_indices[null_byte_indices.len() - 1] != size - 1 {
return Padding::Left;
return Padding::Left
}

// the first byte is not a null byte AND the last byte is a null byte, it is right padded
if null_byte_indices[0] != 0 && null_byte_indices[null_byte_indices.len() - 1] == size - 1 {
return Padding::Right;
return Padding::Right
}

// get non-null byte indices
Expand All @@ -365,7 +365,7 @@ pub fn get_padding(bytes: &str) -> Padding {
.collect::<Vec<usize>>();

if non_null_byte_indices.is_empty() {
return Padding::None;
return Padding::None
}

// check if the there are more null-bytes before the first non-null byte than after the last
Expand Down
16 changes: 8 additions & 8 deletions common/src/ether/evm/ext/exec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl VM {

// this shouldn't be necessary, but it's safer to have it
if self.exitcode != 255 || !self.returndata.is_empty() {
break;
break
}
}

Expand Down Expand Up @@ -104,13 +104,13 @@ impl VM {

// if the stack has over 16 items of the same source, it's probably a loop
if stack_contains_too_many_of_the_same_item(&vm.stack) {
return vm_trace;
return vm_trace
}

// if any item on the stack has a depth > 16, it's probably a loop (because of stack
// too deep)
if stack_item_source_depth_too_deep(&vm.stack) {
return vm_trace;
return vm_trace
}

// break out of loops
Expand Down Expand Up @@ -211,7 +211,7 @@ impl VM {

// this key exists, but the stack is different, so the jump is new
historical_stacks.push(vm.stack.clone());
return vm_trace;
return vm_trace
} else {
debug_max!(
"adding historical stack {} to jump frame {:?}",
Expand Down Expand Up @@ -253,7 +253,7 @@ impl VM {

// push the current path onto the stack
vm_trace.children.push(vm.recursive_map(branch_count, handled_jumps));
break;
break
} else {
// push a new vm trace to the children
let mut trace_vm = vm.clone();
Expand All @@ -262,7 +262,7 @@ impl VM {

// push the current path onto the stack
vm_trace.children.push(vm.recursive_map(branch_count, handled_jumps));
break;
break
}
}

Expand Down Expand Up @@ -330,7 +330,7 @@ impl VM {

// this key exists, but the stack is different, so the jump is new
historical_stacks.push(vm.stack.clone());
return vm_trace;
return vm_trace
} else {
debug_max!(
"adding historical stack {} to jump frame {:?}",
Expand All @@ -351,7 +351,7 @@ impl VM {

// when the vm exits, this path is complete
if vm.exitcode != 255 || !vm.returndata.is_empty() {
break;
break
}
}

Expand Down
20 changes: 10 additions & 10 deletions common/src/ether/evm/ext/exec/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub fn stack_contains_too_many_of_the_same_item(stack: &Stack) -> bool {
debug_max!(
"jump matches loop-detection heuristic: 'stack_contains_too_many_of_the_same_item'",
);
return true;
return true
}

false
Expand All @@ -50,7 +50,7 @@ pub fn stack_item_source_depth_too_deep(stack: &Stack) -> bool {

logger
.debug_max("jump matches loop-detection heuristic: 'stack_item_source_depth_too_deep'");
return true;
return true
}

false
Expand All @@ -70,7 +70,7 @@ pub fn jump_condition_appears_recursive(stack_diff: &[StackFrame], jump_conditio

logger
.debug_max("jump matches loop-detection heuristic: 'jump_condition_appears_recursive'");
return true;
return true
}

false
Expand All @@ -85,15 +85,15 @@ pub fn jump_condition_contains_mutated_memory_access(
if stack_diff.iter().any(|frame| {
memory_accesses.any(|_match| {
if _match.is_err() {
return false;
return false
}
let memory_access = _match.unwrap();
let slice = &jump_condition[memory_access.start()..memory_access.end()];
frame.operation.solidify().contains(slice)
})
}) {
debug_max!("jump matches loop-detection heuristic: 'jump_condition_contains_mutated_memory_access'");
return true;
return true
}

false
Expand All @@ -108,15 +108,15 @@ pub fn jump_condition_contains_mutated_storage_access(
if stack_diff.iter().any(|frame| {
storage_accesses.any(|_match| {
if _match.is_err() {
return false;
return false
}
let storage_access = _match.unwrap();
let slice = &jump_condition[storage_access.start()..storage_access.end()];
frame.operation.solidify().contains(slice)
})
}) {
debug_max!("jump matches loop-detection heuristic: 'jump_condition_contains_mutated_storage_access'");
return true;
return true
}

false
Expand All @@ -130,7 +130,7 @@ pub fn jump_condition_historical_diffs_approximately_equal(
// break if historical_stacks.len() < 4
// this is an arbitrary number, i picked it randomly :D
if historical_stacks.len() < 4 {
return false;
return false
}

// get the stack diffs for all historical stacks
Expand All @@ -149,12 +149,12 @@ pub fn jump_condition_historical_diffs_approximately_equal(

// check if all stack diffs are similar
if !stack_diffs.iter().all(|diff| diff.len() <= threshold) {
return false;
return false
}

// check if all stack diffs are the same
if !stack_diffs.iter().all(|diff| diff[0] == stack_diffs[0][0]) {
return false;
return false
}

debug_max!("jump matches loop-detection heuristic: 'jump_condition_historical_diffs_approximately_equal'");
Expand Down
2 changes: 1 addition & 1 deletion common/src/ether/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ pub async fn get_storage_diff(
read_cache(&format!("diff.{}.{}", &chain_id, &transaction_hash))
{
debug_max!("found cached state diff for transaction '{}' .", &transaction_hash);
return Ok(state_diff);
return Ok(state_diff)
}

debug_max!(&format!(
Expand Down
8 changes: 4 additions & 4 deletions common/src/ether/selectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub fn find_function_selectors(evm: &VM, assembly: &str) -> HashMap<String, u128

// check if this function selector has already been handled
if handled_selectors.contains(&function_selector) {
continue;
continue
}

debug_max!(
Expand Down Expand Up @@ -116,21 +116,21 @@ pub fn resolve_entry_point(evm: &VM, selector: &str) -> u128 {
jump_condition.contains(" == ") &&
jump_taken == 1
{
return call.last_instruction.inputs[0].try_into().unwrap_or(0);
return call.last_instruction.inputs[0].try_into().unwrap_or(0)
} else if jump_taken == 1 {
// if handled_jumps contains the jumpi, we have already handled this jump.
// loops aren't supported in the dispatcher, so we can just return 0
if handled_jumps.contains(&call.last_instruction.inputs[0].try_into().unwrap_or(0))
{
return 0;
return 0
} else {
handled_jumps.insert(call.last_instruction.inputs[0].try_into().unwrap_or(0));
}
}
}

if vm.exitcode != 255 || !vm.returndata.is_empty() {
break;
break
}
}

Expand Down
6 changes: 3 additions & 3 deletions common/src/ether/signatures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl ResolveSelector for ResolvedError {
0 => return None,
_ => {
debug_max!("found cached results for selector: {}", &selector);
return Some(cached_results);
return Some(cached_results)
}
}
}
Expand Down Expand Up @@ -141,7 +141,7 @@ impl ResolveSelector for ResolvedLog {
0 => return None,
_ => {
debug_max!("found cached results for selector: {}", &selector);
return Some(cached_results);
return Some(cached_results)
}
}
}
Expand Down Expand Up @@ -226,7 +226,7 @@ impl ResolveSelector for ResolvedFunction {
0 => return None,
_ => {
debug_max!("found cached results for selector: {}", &selector);
return Some(cached_results);
return Some(cached_results)
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions common/src/resources/transpose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ pub async fn get_contract_creation(
}
};

return Some((block_number, transaction_hash));
return Some((block_number, transaction_hash))
};

None
Expand All @@ -273,7 +273,7 @@ pub async fn get_label(address: &str, api_key: &str) -> Option<String> {
Some(response) => response,
None => {
debug_max!(&format!("failed to get label from Transpose for address: {}", address));
return None;
return None
}
};

Expand All @@ -287,18 +287,18 @@ pub async fn get_label(address: &str, api_key: &str) -> Option<String> {
"failed to parse label from Transpose for address: {}",
address
));
return None;
return None
}
},
None => {
debug_max!(&format!(
"failed to fetch label from Transpose response for address: {}",
address
));
return None;
return None
}
};
return Some(label);
return Some(label)
};

None
Expand Down
Loading

0 comments on commit a7fa324

Please sign in to comment.