Skip to content

Commit

Permalink
chore: deprecate etherface, switch to openchain (#205)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon-Becker authored Nov 28, 2023
1 parent 0aa5f4d commit 903905f
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 38 deletions.
100 changes: 67 additions & 33 deletions common/src/ether/signatures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,17 @@ impl ResolveSelector for ResolvedError {
0 => return None,
_ => {
logger.debug_max(&format!("found cached results for selector: {}", &selector));
return Some(cached_results)
return Some(cached_results);
}
}
}

// get function possibilities from etherface
// get function possibilities from openchain
let signatures = match get_json_from_url(
&format!("https://api.etherface.io/v1/signatures/hash/error/{}/1", &selector),
&format!(
"https://api.openchain.xyz/signature-database/v1/lookup?filter=true&function=0x{}",
&selector
),
10,
)
.await
Expand All @@ -68,13 +71,12 @@ impl ResolveSelector for ResolvedError {
};

// convert the serde value into a vec of possible functions
let results = match signatures.get("items") {
Some(items) => match items.as_array() {
Some(items) => items.to_vec(),
None => return None,
},
None => return None,
};
let results = signatures
.get("result")?
.get("function")?
.get(&format!("0x{selector}"))?
.as_array()?
.to_vec();

logger.debug_max(&format!(
"found {} possible functions for selector: {}",
Expand All @@ -86,7 +88,7 @@ impl ResolveSelector for ResolvedError {

for signature in results {
// get the function text signature and unwrap it into a string
let text_signature = match signature.get("text") {
let text_signature = match signature.get("name") {
Some(text_signature) => text_signature.to_string().replace('"', ""),
None => continue,
};
Expand Down Expand Up @@ -133,14 +135,17 @@ impl ResolveSelector for ResolvedLog {
0 => return None,
_ => {
logger.debug_max(&format!("found cached results for selector: {}", &selector));
return Some(cached_results)
return Some(cached_results);
}
}
}

// get function possibilities from etherface
// get function possibilities from openchain
let signatures = match get_json_from_url(
&format!("https://api.etherface.io/v1/signatures/hash/event/{}/1", &selector),
&format!(
"https://api.openchain.xyz/signature-database/v1/lookup?filter=true&event=0x{}",
&selector
),
10,
)
.await
Expand All @@ -151,13 +156,12 @@ impl ResolveSelector for ResolvedLog {
};

// convert the serde value into a vec of possible functions
let results = match signatures.get("items") {
Some(items) => match items.as_array() {
Some(items) => items.to_vec(),
None => return None,
},
None => return None,
};
let results = signatures
.get("result")?
.get("event")?
.get(&format!("0x{selector}"))?
.as_array()?
.to_vec();

logger.debug_max(&format!(
"found {} possible functions for selector: {}",
Expand All @@ -169,7 +173,7 @@ impl ResolveSelector for ResolvedLog {

for signature in results {
// get the function text signature and unwrap it into a string
let text_signature = match signature.get("text") {
let text_signature = match signature.get("name") {
Some(text_signature) => text_signature.to_string().replace('"', ""),
None => continue,
};
Expand Down Expand Up @@ -216,14 +220,17 @@ impl ResolveSelector for ResolvedFunction {
0 => return None,
_ => {
logger.debug_max(&format!("found cached results for selector: {}", &selector));
return Some(cached_results)
return Some(cached_results);
}
}
}

// get function possibilities from etherface
// get function possibilities from openchain
let signatures = match get_json_from_url(
&format!("https://api.etherface.io/v1/signatures/hash/function/{}/1", &selector),
&format!(
"https://api.openchain.xyz/signature-database/v1/lookup?filter=true&function=0x{}",
&selector
),
10,
)
.await
Expand All @@ -234,13 +241,12 @@ impl ResolveSelector for ResolvedFunction {
};

// convert the serde value into a vec of possible functions
let results = match signatures.get("items") {
Some(items) => match items.as_array() {
Some(items) => items.to_vec(),
None => return None,
},
None => return None,
};
let results = signatures
.get("result")?
.get("function")?
.get(&format!("0x{selector}"))?
.as_array()?
.to_vec();

logger.debug_max(&format!(
"found {} possible functions for selector: {}",
Expand All @@ -252,7 +258,7 @@ impl ResolveSelector for ResolvedFunction {

for signature in results {
// get the function text signature and unwrap it into a string
let text_signature = match signature.get("text") {
let text_signature = match signature.get("name") {
Some(text_signature) => text_signature.to_string().replace('"', ""),
None => continue,
};
Expand Down Expand Up @@ -306,6 +312,34 @@ mod tests {
score_signature, ResolveSelector, ResolvedError, ResolvedFunction, ResolvedLog,
};

#[tokio::test]
async fn resolve_function_signature_nominal() {
let signature = String::from("095ea7b3");
delete_cache(&format!("selector.{}", &signature));
let result = ResolvedFunction::resolve(&signature).await;
assert!(result.is_some());
assert!(!result.unwrap().is_empty());
}

#[tokio::test]
async fn resolve_error_signature_nominal() {
let signature = String::from("30cd7471");
delete_cache(&format!("selector.{}", &signature));
let result = ResolvedError::resolve(&signature).await;
assert!(result.is_some());
assert!(!result.unwrap().is_empty());
}

#[tokio::test]
async fn resolve_event_signature_nominal() {
let signature =
String::from("ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef");
delete_cache(&format!("selector.{}", &signature));
let result = ResolvedLog::resolve(&signature).await;
assert!(result.is_some());
assert!(!result.unwrap().is_empty());
}

#[tokio::test]
async fn resolve_function_signature_should_return_none_when_cached_results_not_found() {
let signature = String::from("test_signature_nocache");
Expand Down
6 changes: 3 additions & 3 deletions common/src/utils/io/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl TraceFactory {
"{} {} {}",
replace_last(prefix, "│ ", " ├─").bold().bright_white(),
format!("[{}]", trace.instruction).bold().bright_white(),
trace.message.get(0).expect("Failed to build trace.")
trace.message.first().expect("Failed to build trace.")
);

// print the children
Expand Down Expand Up @@ -136,7 +136,7 @@ impl TraceFactory {
println!(
"{} emit {}",
replace_last(prefix, "│ ", " ├─").bold().bright_white(),
trace.message.get(0).expect("Failed to build trace.")
trace.message.first().expect("Failed to build trace.")
);
}
TraceCategory::LogUnknown => {
Expand Down Expand Up @@ -211,7 +211,7 @@ impl TraceFactory {
"{} {} create → {}",
replace_last(prefix, "│ ", " ├─").bold().bright_white(),
format!("[{}]", trace.instruction).bold().bright_white(),
trace.message.get(0).expect("Failed to build trace.")
trace.message.first().expect("Failed to build trace.")
);

// print the children
Expand Down
2 changes: 1 addition & 1 deletion core/src/decompile/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub fn match_parameters(
&function
.arguments
.values()
.map(|(_, types)| types.get(0).unwrap().clone())
.map(|(_, types)| types.first().unwrap().clone())
.collect::<Vec<String>>()
.join(",")
));
Expand Down
2 changes: 1 addition & 1 deletion core/src/snapshot/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub fn match_parameters(
&function
.arguments
.values()
.map(|(_, types)| types.get(0).unwrap().clone())
.map(|(_, types)| types.first().unwrap().clone())
.collect::<Vec<String>>()
.join(",")
));
Expand Down

0 comments on commit 903905f

Please sign in to comment.