Skip to content

Commit

Permalink
added gas consumption stdout to zkvm program
Browse files Browse the repository at this point in the history
  • Loading branch information
xqft committed Nov 25, 2024
1 parent eb9e1e4 commit 17c6fd0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
9 changes: 6 additions & 3 deletions cmd/ethrex_l2/src/commands/prove.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use clap::Args;
use ethrex_l2::utils::test_data_io::{generate_program_input, read_chain_file, read_genesis_file};
use ethrex_prover_lib::prover::Prover;
use log::info;

#[derive(Args)]
pub(crate) struct Command {
Expand Down Expand Up @@ -33,8 +32,12 @@ impl Command {

let mut prover = Prover::new();
prover.prove(program_input).expect("proving failed");

info!("Successfully proven block");
println!(
"Total gas consumption: {}",
prover
.get_gas()
.expect("failed to deserialize gas consumption")
);
Ok(())
}
}
6 changes: 6 additions & 0 deletions crates/l2/prover/src/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ impl<'a> Prover<'a> {
Ok(())
}

pub fn get_gas(&self) -> Result<u64, Box<dyn std::error::Error>> {
Ok(risc0_zkvm::serde::from_slice(
&self.stdout[..8], // first 8 bytes
)?)
}

pub fn get_commitment(
receipt: &risc0_zkvm::Receipt,
) -> Result<ProgramOutput, Box<dyn std::error::Error>> {
Expand Down
7 changes: 7 additions & 0 deletions crates/l2/prover/zkvm/interface/guest/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ fn main() {
let receipts = execute_block(&block, &mut state).expect("failed to execute block");
validate_gas_used(&receipts, &block.header).expect("invalid gas used");

env::write(
&receipts
.last()
.expect("no receipts found")
.cumulative_gas_used,
);

let account_updates = get_state_transitions(&mut state);

// Update tries and calculate final state root hash
Expand Down

0 comments on commit 17c6fd0

Please sign in to comment.