Skip to content

Commit

Permalink
♻ Async subcmd dispatch
Browse files Browse the repository at this point in the history
  • Loading branch information
clabby committed Sep 29, 2023
1 parent 06b3c49 commit a6f2be2
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 15 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions bin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ serde_json = "1.0.107"
alloy-primitives = "0.3.3"
tokio = { version = "1.32.0", features = ["macros"] }
flate2 = "1.0.27"
async-trait = "0.1.73"

# Local
cannon-mipsevm = { path = "../crates/mipsevm" }
Expand Down
5 changes: 3 additions & 2 deletions bin/src/cannon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@ struct Args {
subcommand: subcommands::CannonSubcommand,
}

fn main() -> Result<()> {
#[tokio::main]
async fn main() -> Result<()> {
// Parse the command arguments
let Args { v, subcommand } = Args::parse();

// Initialize the tracing subscriber
init_tracing_subscriber(v)?;

tracing::debug!(target: "cannon-cli", "Dispatching subcommand");
subcommand.dispatch()?;
subcommand.dispatch().await?;

Ok(())
}
Expand Down
9 changes: 4 additions & 5 deletions bin/src/compressor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use anyhow::Result;
use flate2::{bufread::GzDecoder, write::GzEncoder, Compression};
use std::io::{Cursor, Read, Write};
use std::io::{Read, Write};

/// Compresses a byte slice using gzip.
pub(crate) fn compress_bytes(bytes: &[u8]) -> Result<Vec<u8>> {
Expand All @@ -13,11 +13,10 @@ pub(crate) fn compress_bytes(bytes: &[u8]) -> Result<Vec<u8>> {

/// Decompresses a byte slice using gzip.
pub(crate) fn decompress_bytes(compressed_bytes: &[u8]) -> Result<Vec<u8>> {
let cursor = Cursor::new(compressed_bytes);
let mut decoder = GzDecoder::new(cursor);
let mut decoder = GzDecoder::new(compressed_bytes);

// Give our decompressed buffer the same capacity as the compressed buffer. It'll still
// reallocate, but less.
// Give our decompressed buffer the same capacity as the compressed buffer to reduce
// reallocations up to the compressed buffer's size.
let mut decompressed_bytes = Vec::with_capacity(compressed_bytes.len());
decoder.read_to_end(&mut decompressed_bytes)?;

Expand Down
4 changes: 3 additions & 1 deletion bin/src/subcommands/load_elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::compressor::compress_bytes;

use super::CannonSubcommandDispatcher;
use anyhow::Result;
use async_trait::async_trait;
use cannon_mipsevm::{load_elf, patch_go, patch_stack};
use clap::{builder::PossibleValue, Args, ValueEnum};
use std::{fs, path::PathBuf};
Expand Down Expand Up @@ -45,8 +46,9 @@ impl ValueEnum for PatchKind {
}
}

#[async_trait]
impl CannonSubcommandDispatcher for LoadElfArgs {
fn dispatch(&self) -> Result<()> {
async fn dispatch(&self) -> Result<()> {
tracing::info!(target: "cannon-cli::load-elf", "Loading ELF file @ {}", self.path.display());
let elf_raw = fs::read(&self.path)?;
let mut state = load_elf(&elf_raw)?;
Expand Down
13 changes: 8 additions & 5 deletions bin/src/subcommands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
//! Subcommands for the `cannon` binary

use anyhow::Result;
use async_trait::async_trait;
use clap::Subcommand;

mod load_elf;
mod run;
mod witness;

#[async_trait]
pub(crate) trait CannonSubcommandDispatcher {
/// Dispatches the subcommand
fn dispatch(&self) -> Result<()>;
async fn dispatch(&self) -> Result<()>;
}

/// The subcommands for the `cannon` binary
Expand All @@ -20,13 +22,14 @@ pub(crate) enum CannonSubcommand {
LoadElf(load_elf::LoadElfArgs),
}

#[async_trait]
impl CannonSubcommandDispatcher for CannonSubcommand {
/// TODO(clabby): Dynamic dispatch on Box<dyn CannonSubcommandDispatcher>
fn dispatch(&self) -> Result<()> {
async fn dispatch(&self) -> Result<()> {
match self {
CannonSubcommand::Run(args) => args.dispatch(),
CannonSubcommand::Witness(args) => args.dispatch(),
CannonSubcommand::LoadElf(args) => args.dispatch(),
CannonSubcommand::Run(args) => args.dispatch().await,
CannonSubcommand::Witness(args) => args.dispatch().await,
CannonSubcommand::LoadElf(args) => args.dispatch().await,
}
}
}
4 changes: 3 additions & 1 deletion bin/src/subcommands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use super::CannonSubcommandDispatcher;
use anyhow::Result;
use async_trait::async_trait;
use clap::Args;

/// Command line arguments for `cannon run`
Expand Down Expand Up @@ -50,8 +51,9 @@ pub(crate) struct RunArgs {
l2_endpoint: String,
}

#[async_trait]
impl CannonSubcommandDispatcher for RunArgs {
fn dispatch(&self) -> Result<()> {
async fn dispatch(&self) -> Result<()> {
todo!()
}
}
4 changes: 3 additions & 1 deletion bin/src/subcommands/witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use super::CannonSubcommandDispatcher;
use crate::compressor::decompress_bytes;
use alloy_primitives::B256;
use anyhow::Result;
use async_trait::async_trait;
use cannon_mipsevm::{State, StateWitnessHasher};
use clap::Args;
use std::{fs, path::PathBuf};
Expand All @@ -21,8 +22,9 @@ pub(crate) struct WitnessArgs {
output: Option<PathBuf>,
}

#[async_trait]
impl CannonSubcommandDispatcher for WitnessArgs {
fn dispatch(&self) -> Result<()> {
async fn dispatch(&self) -> Result<()> {
tracing::info!(target: "cannon-cli::witness", "Loading state JSON dump from {}", self.input.display());

let state_raw = fs::read(&self.input)?;
Expand Down

0 comments on commit a6f2be2

Please sign in to comment.