Skip to content

Commit

Permalink
feat(inspect): --skip-resolving flag, --transpose-api-key fixes (#…
Browse files Browse the repository at this point in the history
…234)

* fix(inspect): respect empty `--transpose-api-key`, add `--skip-resolving` flag

* chore(inspect): pass `--skip-resolving` to tracing

* chore(inspect): `--transpose-api-key` should be `String`, not `Option`
  • Loading branch information
Jon-Becker authored Dec 13, 2023
1 parent 23addf5 commit e2400a1
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 32 deletions.
4 changes: 2 additions & 2 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
}

// if the user has not specified a transpose api key, use the default
if cmd.transpose_api_key.is_none() {
cmd.transpose_api_key = Some(configuration.transpose_api_key);
if cmd.transpose_api_key.is_empty() {
cmd.transpose_api_key = configuration.transpose_api_key;
}

// if the user has passed an output filename, override the default filename
Expand Down
9 changes: 9 additions & 0 deletions common/src/utils/env.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
pub fn set_env(key: &str, value: &str) {
if std::env::var(key).is_err() {
std::env::set_var(key, value);
}
}

pub fn get_env(key: &str) -> Option<String> {
std::env::var(key).ok()
}
1 change: 1 addition & 0 deletions common/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod env;
pub mod hex;
pub mod http;
pub mod integers;
Expand Down
33 changes: 26 additions & 7 deletions core/src/inspect/core/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,36 @@ use heimdall_common::{resources::transpose::get_label, utils::hex::ToLowerHex};
#[derive(Debug, Clone)]
pub struct Contracts {
pub contracts: HashMap<Address, String>,
transpose_api_key: Option<String>,
transpose_api_key: String,
skip_resolving: bool,
}

#[allow(dead_code)]
impl Contracts {
pub fn new(args: &InspectArgs) -> Self {
Self { contracts: HashMap::new(), transpose_api_key: args.transpose_api_key.clone() }
Self {
contracts: HashMap::new(),
transpose_api_key: args.transpose_api_key.clone(),
skip_resolving: args.skip_resolving,
}
}

pub async fn add(&mut self, address: Address) -> Result<(), Error> {
// if skip resolving, just add the address
if self.skip_resolving {
self.contracts.insert(address, address.to_lower_hex());
return Ok(());
}

// if alias already exists, don't overwrite
if self.contracts.contains_key(&address) {
return Ok(());
}

if let Some(transpose_api_key) = &self.transpose_api_key {
if !self.transpose_api_key.is_empty() {
self.contracts.insert(
address,
get_label(&address.to_lower_hex(), transpose_api_key)
get_label(&address.to_lower_hex(), &self.transpose_api_key)
.await
.unwrap_or(address.to_lower_hex()),
);
Expand All @@ -39,15 +50,23 @@ impl Contracts {
}

pub async fn extend(&mut self, addresses: HashSet<Address>) -> Result<(), Error> {
// if skip resolving, just add the address
if self.skip_resolving {
self.contracts
.extend(addresses.into_iter().map(|address| (address, address.to_lower_hex())));
return Ok(());
}

// for each address, get the label
if let Some(transpose_api_key) = &self.transpose_api_key {
if !self.transpose_api_key.is_empty() {
let transpose_api_key_clone = self.transpose_api_key.clone();
let handles: Vec<_> = addresses
.clone()
.into_iter()
.map(move |address| {
let transpose_api_key = transpose_api_key.clone();
let transpose_api_key_clone = transpose_api_key_clone.clone();
tokio::spawn(async move {
get_label(&address.to_lower_hex(), &transpose_api_key).await
get_label(&address.to_lower_hex(), &transpose_api_key_clone).await
})
})
.collect();
Expand Down
40 changes: 24 additions & 16 deletions core/src/inspect/core/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use ethers::types::{Address, Bytes, Log, H256, U256, U64};
use heimdall_common::{
debug_max,
ether::signatures::{ResolveSelector, ResolvedLog},
utils::hex::ToLowerHex,
utils::{env::get_env, hex::ToLowerHex},
};
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -74,21 +74,29 @@ impl TryFrom<Log> for DecodedLog {
type Error = crate::error::Error;

async fn try_from(value: Log) -> Result<Self, Self::Error> {
let signature = match value.topics.first() {
Some(topic) => {
let topic = topic.to_lower_hex();
Some(topic)
}
None => None,
};

let resolved_logs = match signature {
Some(signature) => {
debug_max!("resolving signature: {}", signature.to_string().to_lowercase());
ResolvedLog::resolve(&signature).await.unwrap_or(Vec::new())
}
None => Vec::new(),
};
let mut resolved_logs = Vec::new();
let skip_resolving = get_env("SKIP_RESOLVING")
.unwrap_or("false".to_string())
.parse::<bool>()
.unwrap_or(false);

if !skip_resolving {
let signature = match value.topics.first() {
Some(topic) => {
let topic = topic.to_lower_hex();
Some(topic)
}
None => None,
};

resolved_logs = match signature {
Some(signature) => {
debug_max!("resolving signature: {}", signature.to_string().to_lowercase());
ResolvedLog::resolve(&signature).await.unwrap_or(Vec::new())
}
None => Vec::new(),
};
}

Ok(Self {
address: value.address,
Expand Down
7 changes: 7 additions & 0 deletions core/src/inspect/core/tracing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use ethers::{
use heimdall_common::{
ether::signatures::ResolvedFunction,
utils::{
env::get_env,
hex::ToLowerHex,
io::{logging::TraceFactory, types::Parameterize},
},
Expand Down Expand Up @@ -196,6 +197,12 @@ impl TryFrom<Call> for DecodedCall {
let result = crate::decode::decode(
DecodeArgsBuilder::new()
.target(calldata)
.skip_resolving(
get_env("SKIP_RESOLVING")
.unwrap_or("false".to_string())
.parse::<bool>()
.unwrap_or(false),
)
.build()
.map_err(|_e| Error::DecodeError)?,
)
Expand Down
19 changes: 14 additions & 5 deletions core/src/inspect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use heimdall_common::{
debug_max,
ether::rpc::{get_block_logs, get_trace, get_transaction},
utils::{
env::set_env,
hex::ToLowerHex,
io::logging::{Logger, TraceFactory},
},
Expand Down Expand Up @@ -45,9 +46,9 @@ pub struct InspectArgs {
#[clap(long, short)]
pub default: bool,

/// Your OPTIONAL Transpose.io API Key, used for labeling contract addresses.
#[clap(long = "transpose-api-key", short, hide_default_value = true)]
pub transpose_api_key: Option<String>,
/// Your OPTIONAL Transpose.io API Key. Used to resolve contract labels.
#[clap(long = "transpose-api-key", short, default_value = "", hide_default_value = true)]
pub transpose_api_key: String,

/// Name for the output files.
#[clap(long, short, default_value = "", hide_default_value = true)]
Expand All @@ -56,6 +57,10 @@ pub struct InspectArgs {
/// The output directory to write the output to, or 'print' to print to the console.
#[clap(long = "output", short = 'o', default_value = "output", hide_default_value = true)]
pub output: String,

/// Whether to skip resolving function selectors and contract labels.
#[clap(long = "skip-resolving")]
pub skip_resolving: bool,
}

impl InspectArgsBuilder {
Expand All @@ -65,9 +70,10 @@ impl InspectArgsBuilder {
verbose: Some(clap_verbosity_flag::Verbosity::new(0, 1)),
rpc_url: Some(String::new()),
default: Some(true),
transpose_api_key: None,
transpose_api_key: Some(String::new()),
name: Some(String::new()),
output: Some(String::from("output")),
skip_resolving: Some(false),
}
}
}
Expand All @@ -82,7 +88,6 @@ pub struct InspectResult {
#[allow(deprecated)]
pub async fn inspect(args: InspectArgs) -> Result<InspectResult, Error> {
// set logger environment variable if not already set
// TODO: abstract this to a heimdall_common util
if std::env::var("RUST_LOG").is_err() {
std::env::set_var(
"RUST_LOG",
Expand All @@ -93,6 +98,10 @@ pub async fn inspect(args: InspectArgs) -> Result<InspectResult, Error> {
);
}

// set skip_resolving env variable
// TODO: create a trait that can be added to a struct to set env variables
set_env("SKIP_RESOLVING", &args.skip_resolving.to_string());

// get a new logger and trace
let (logger, _trace) = Logger::new(match args.verbose.log_level() {
Some(level) => level.as_str(),
Expand Down
7 changes: 5 additions & 2 deletions core/tests/test_inspect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ mod integration_tests {
verbose: Verbosity::new(0, 0),
rpc_url: String::from("https://eth.llamarpc.com"),
default: true,
transpose_api_key: None,
transpose_api_key: String::from(""),
name: String::from(""),
output: String::from("output"),
skip_resolving: true,
};

let _ = heimdall_core::inspect::inspect(args).await.unwrap();
Expand All @@ -31,9 +32,10 @@ mod integration_tests {
verbose: Verbosity::new(0, 0),
rpc_url: String::from("https://eth.llamarpc.com"),
default: true,
transpose_api_key: None,
transpose_api_key: String::from(""),
name: String::from(""),
output: String::from("output"),
skip_resolving: true,
};

let _ = heimdall_core::inspect::inspect(args).await.unwrap();
Expand Down Expand Up @@ -63,6 +65,7 @@ mod integration_tests {
.target(txid.to_string())
.verbose(Verbosity::new(-1, 0))
.rpc_url("https://eth.llamarpc.com".to_string())
.skip_resolving(true)
.build()
.unwrap();

Expand Down

0 comments on commit e2400a1

Please sign in to comment.