|
1 | 1 | // Copyright 2025, Offchain Labs, Inc.
|
2 | 2 | // For licensing, see https://github.com/OffchainLabs/stylus-sdk-rs/blob/main/licenses/COPYRIGHT.md
|
3 | 3 |
|
4 |
| -use crate::error::CargoStylusResult; |
| 4 | +use std::iter; |
| 5 | + |
| 6 | +use eyre::bail; |
| 7 | +use stylus_tools::{ |
| 8 | + core::{ |
| 9 | + build::build_shared_library, |
| 10 | + debugger::Debugger, |
| 11 | + tracing::{hostios, Trace}, |
| 12 | + }, |
| 13 | + utils::color::Color, |
| 14 | +}; |
| 15 | + |
| 16 | +use crate::{ |
| 17 | + common_args::{ProviderArgs, TraceArgs}, |
| 18 | + error::CargoStylusResult, |
| 19 | +}; |
5 | 20 |
|
6 | 21 | #[derive(Debug, clap::Args)]
|
7 |
| -pub struct Args {} |
| 22 | +pub struct Args { |
| 23 | + /// Which specific package to build during replay, if any. |
| 24 | + #[arg(long)] |
| 25 | + package: Option<String>, |
| 26 | + /// Whether this process is the child of another. |
| 27 | + /// |
| 28 | + /// The parent process launches the debugger, with the same arguments plus this `--child` flag. |
| 29 | + /// The child process then runs the transaction. |
| 30 | + #[arg(long, hide(true))] |
| 31 | + child: bool, |
| 32 | + |
| 33 | + #[command(flatten)] |
| 34 | + trace: TraceArgs, |
| 35 | + #[command(flatten)] |
| 36 | + provider: ProviderArgs, |
| 37 | +} |
8 | 38 |
|
9 |
| -pub fn exec(_args: Args) -> CargoStylusResult { |
| 39 | +pub async fn exec(args: Args) -> CargoStylusResult { |
| 40 | + if args.child { |
| 41 | + exec_replay_transaction(args).await?; |
| 42 | + } else { |
| 43 | + launch_debugger()?; |
| 44 | + } |
10 | 45 | Ok(())
|
11 | 46 | }
|
| 47 | + |
| 48 | +async fn exec_replay_transaction(args: Args) -> eyre::Result<()> { |
| 49 | + let provider = args.provider.build_provider().await?; |
| 50 | + let trace = Trace::new(args.trace.tx, args.trace.use_native_tracer, &provider).await?; |
| 51 | + let shared_library = build_shared_library(&args.trace.project, args.package, None)?; |
| 52 | + |
| 53 | + // TODO: don't assume the contract is top-level |
| 54 | + let Some(args) = trace.tx().input.input() else { |
| 55 | + bail!("missing transaction input"); |
| 56 | + }; |
| 57 | + let args_len = args.len(); |
| 58 | + |
| 59 | + unsafe { |
| 60 | + *hostios::FRAME.lock() = Some(trace.into_frame_reader()); |
| 61 | + |
| 62 | + type Entrypoint = unsafe extern "C" fn(usize) -> usize; |
| 63 | + let lib = libloading::Library::new(shared_library)?; |
| 64 | + let main: libloading::Symbol<Entrypoint> = lib.get(b"user_entrypoint")?; |
| 65 | + |
| 66 | + match main(args_len) { |
| 67 | + 0 => println!("call completed successfully"), |
| 68 | + 1 => println!("call reverted"), |
| 69 | + x => println!("call exited with unknown status code: {}", x.red()), |
| 70 | + } |
| 71 | + } |
| 72 | + Ok(()) |
| 73 | +} |
| 74 | + |
| 75 | +// TODO: Use "never" type when feature is stabilized rust-lang/rust#35121 |
| 76 | +fn launch_debugger() -> eyre::Result<()> { |
| 77 | + let debugger = Debugger::select()?; |
| 78 | + let args = std::env::args().chain(iter::once("--child".to_string())); |
| 79 | + let err = debugger.exec(args); |
| 80 | + eyre::bail!("failed to exec {}: {}", debugger.program, err); |
| 81 | +} |
0 commit comments