Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

2 changes: 1 addition & 1 deletion crates/chisel/src/dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ impl ChiselDispatcher {
)?)
.build();

let mut identifier = TraceIdentifiers::new().with_etherscan(
let mut identifier = TraceIdentifiers::new().with_sourcify().with_etherscan(
&session_config.foundry_config,
session_config.evm_opts.get_remote_chain_id().await,
)?;
Expand Down
1 change: 1 addition & 0 deletions crates/evm/traces/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ revm-inspectors.workspace = true
eyre.workspace = true
futures.workspace = true
itertools.workspace = true
reqwest.workspace = true
serde.workspace = true
serde_json.workspace = true
tokio = { workspace = true, features = ["time", "macros"] }
Expand Down
18 changes: 16 additions & 2 deletions crates/evm/traces/src/identifier/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ pub use local::LocalTraceIdentifier;
mod etherscan;
pub use etherscan::EtherscanIdentifier;

mod sourcify;
pub use sourcify::SourceifyIdentifier;

mod signatures;
pub use signatures::{SignaturesCache, SignaturesIdentifier};

Expand Down Expand Up @@ -43,6 +46,8 @@ pub struct TraceIdentifiers<'a> {
pub local: Option<LocalTraceIdentifier<'a>>,
/// The optional Etherscan trace identifier.
pub etherscan: Option<EtherscanIdentifier>,
/// The optional Sourcify trace identifier.
pub sourcify: Option<SourceifyIdentifier>,
}

impl Default for TraceIdentifiers<'_> {
Expand All @@ -60,6 +65,9 @@ impl TraceIdentifier for TraceIdentifiers<'_> {
return identities;
}
}
if let Some(sourcify) = &mut self.sourcify {
identities.extend(sourcify.identify_addresses(nodes));
}
if let Some(etherscan) = &mut self.etherscan {
identities.extend(etherscan.identify_addresses(nodes));
}
Expand All @@ -70,7 +78,7 @@ impl TraceIdentifier for TraceIdentifiers<'_> {
impl<'a> TraceIdentifiers<'a> {
/// Creates a new, empty instance.
pub const fn new() -> Self {
Self { local: None, etherscan: None }
Self { local: None, etherscan: None, sourcify: None }
}

/// Sets the local identifier.
Expand All @@ -96,8 +104,14 @@ impl<'a> TraceIdentifiers<'a> {
Ok(self)
}

/// Sets the sourcify identifier.
pub fn with_sourcify(mut self) -> Self {
self.sourcify = Some(SourceifyIdentifier::new());
self
}

/// Returns `true` if there are no set identifiers.
pub fn is_empty(&self) -> bool {
self.local.is_none() && self.etherscan.is_none()
self.local.is_none() && self.etherscan.is_none() && self.sourcify.is_none()
}
}
67 changes: 67 additions & 0 deletions crates/evm/traces/src/identifier/sourcify.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use super::{IdentifiedAddress, TraceIdentifier};
use alloy_json_abi::JsonAbi;
use revm_inspectors::tracing::types::CallTraceNode;
use serde::Deserialize;
use std::borrow::Cow;

#[derive(Deserialize)]
struct SourceifyFile {
name: String,
content: String,
}

/// A trace identifier that uses Sourcify to identify contract ABIs.
pub struct SourceifyIdentifier;

impl SourceifyIdentifier {
pub fn new() -> Self {
Self
}
}

impl Default for SourceifyIdentifier {
fn default() -> Self {
Self::new()
}
}

impl TraceIdentifier for SourceifyIdentifier {
fn identify_addresses(&mut self, nodes: &[&CallTraceNode]) -> Vec<IdentifiedAddress<'_>> {
let mut identities = Vec::new();

for &node in nodes {
let address = node.trace.address;

// Try to get ABI from Sourcify
let abi = foundry_common::block_on(async {
let client = reqwest::Client::new();
let url = format!("https://repo.sourcify.dev/contracts/full_match/1/{address:?}/");

let files: Vec<SourceifyFile> =
client.get(&url).send().await.ok()?.json().await.ok()?;

for file in files {
if file.name == "metadata.json" {
let metadata: serde_json::Value =
serde_json::from_str(&file.content).ok()?;
let abi_value = metadata.get("output")?.get("abi")?;
return serde_json::from_value::<JsonAbi>(abi_value.clone()).ok();
}
}
None
});

if let Some(abi) = abi {
identities.push(IdentifiedAddress {
address,
label: Some("Sourcify".to_string()),
contract: Some("Sourcify".to_string()),
abi: Some(Cow::Owned(abi)),
artifact_id: None,
});
}
}

identities
}
}
6 changes: 3 additions & 3 deletions crates/forge/src/cmd/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -525,10 +525,10 @@ impl TestArgs {
// Set up trace identifiers.
let mut identifier = TraceIdentifiers::new().with_local(&known_contracts);

// Avoid using etherscan for gas report as we decode more traces and this will be
// expensive.
// Avoid using etherscan and sourcify for gas report as we decode more traces and this will
// be expensive.
if !self.gas_report {
identifier = identifier.with_etherscan(&config, remote_chain_id)?;
identifier = identifier.with_sourcify().with_etherscan(&config, remote_chain_id)?;
}

// Build the trace decoder.
Expand Down
9 changes: 5 additions & 4 deletions crates/script/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,10 +335,11 @@ impl ExecutedState {
.with_label_disabled(self.args.disable_labels)
.build();

let mut identifier = TraceIdentifiers::new().with_local(known_contracts).with_etherscan(
&self.script_config.config,
self.script_config.evm_opts.get_remote_chain_id().await,
)?;
let mut identifier =
TraceIdentifiers::new().with_local(known_contracts).with_sourcify().with_etherscan(
&self.script_config.config,
self.script_config.evm_opts.get_remote_chain_id().await,
)?;

for (_, trace) in &self.execution_result.traces {
decoder.identify(trace, &mut identifier);
Expand Down
Loading