Skip to content

Commit

Permalink
Add option to use native tracer in replay (#84)
Browse files Browse the repository at this point in the history
* Improve JS tracer on cargo replay

Handle EVM calls in the traces and encode binary data as hex.

* Add option to use native trace in stylus replay
  • Loading branch information
gligneul authored Aug 20, 2024
1 parent 16ace97 commit 2707d89
Show file tree
Hide file tree
Showing 5 changed files with 232 additions and 59 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions replay/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ clap.workspace = true
ethers.workspace = true
eyre.workspace = true
function_name.workspace = true
hex.workspace = true
lazy_static.workspace = true
libc.workspace = true
libloading.workspace = true
Expand All @@ -25,3 +26,6 @@ rustc-host.workspace = true
serde = { version = "1.0.203", features = ["derive"] }
sneks.workspace = true
tokio.workspace = true

[dev-dependencies]
serde_json.workspace = true
24 changes: 10 additions & 14 deletions replay/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,8 @@ enum Subcommands {

#[derive(Args, Clone, Debug)]
struct ReplayArgs {
/// RPC endpoint.
#[arg(short, long, default_value = "http://localhost:8547")]
endpoint: String,
/// Tx to replay.
#[arg(short, long)]
tx: TxHash,
/// Project path.
#[arg(short, long, default_value = ".")]
project: PathBuf,
#[command(flatten)]
trace: TraceArgs,
/// Whether to use stable Rust. Note that nightly is needed to expand macros.
#[arg(short, long)]
stable_rust: bool,
Expand All @@ -76,6 +69,9 @@ struct TraceArgs {
/// Project path.
#[arg(short, long, default_value = ".")]
project: PathBuf,
/// If set, use the native tracer instead of the JavaScript one. Notice the native tracer might not be available in the node.
#[arg(short, long, default_value_t = false)]
use_native_tracer: bool,
}

fn main() -> Result<()> {
Expand Down Expand Up @@ -106,7 +102,7 @@ async fn main_impl(args: Opts) -> Result<()> {

async fn trace(args: TraceArgs) -> Result<()> {
let provider = sys::new_provider(&args.endpoint)?;
let trace = Trace::new(provider, args.tx).await?;
let trace = Trace::new(provider, args.tx, args.use_native_tracer).await?;
println!("{}", trace.json);
Ok(())
}
Expand Down Expand Up @@ -144,11 +140,11 @@ async fn replay(args: ReplayArgs) -> Result<()> {
bail!("failed to exec gdb {:?}", err);
}

let provider = sys::new_provider(&args.endpoint)?;
let trace = Trace::new(provider, args.tx).await?;
let provider = sys::new_provider(&args.trace.endpoint)?;
let trace = Trace::new(provider, args.trace.tx, args.trace.use_native_tracer).await?;

build_so(&args.project)?;
let so = find_so(&args.project)?;
build_so(&args.trace.project)?;
let so = find_so(&args.trace.project)?;

// TODO: don't assume the contract is top-level
let args_len = trace.tx.input.len();
Expand Down
30 changes: 27 additions & 3 deletions replay/src/query.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,41 @@
{
"hostio": function(info) {
info.args = toHex(info.args);
info.outs = toHex(info.outs);
if (this.nests.includes(info.name)) {
info.info = this.open.pop();
Object.assign(info, this.open.pop());
info.name = info.name.substring(4) // remove evm_
}
this.open.push(info);
},
"enter": function(frame) {
let inner = [];
let name = "";
switch (frame.getType()) {
case "CALL":
name = "evm_call_contract";
break;
case "DELEGATECALL":
name = "evm_delegate_call_contract";
break;
case "STATICCALL":
name = "evm_static_call_contract";
break;
case "CREATE":
name = "evm_create1";
break;
case "CREATE2":
name = "evm_create2";
break;
case "SELFDESTRUCT":
name = "evm_self_destruct";
break;
}
this.open.push({
address: frame.getTo(),
address: toHex(frame.getTo()),
steps: inner,
name: name,
});

this.stack.push(this.open); // save where we were
this.open = inner;
},
Expand Down
Loading

0 comments on commit 2707d89

Please sign in to comment.