Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ratankaliani committed Aug 22, 2024
1 parent 6807a71 commit abdc72a
Showing 1 changed file with 35 additions and 34 deletions.
69 changes: 35 additions & 34 deletions bin/host/src/execute.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
use csv::Writer;
use rsp_client_executor::{io::ClientExecutorInput, ChainVariant};
use serde::{Deserialize, Serialize};
use sp1_sdk::ExecutionReport;
use std::{fs::OpenOptions, path::PathBuf};

#[derive(Serialize, Deserialize)]
struct ExecutionReportData {
chain_id: u64,
block_number: u64,
gas_used: u64,
tx_count: usize,
number_cycles: u64,
number_syscalls: u64,
bn_add_cycles: u64,
bn_mul_cycles: u64,
bn_pair_cycles: u64,
kzg_point_eval_cycles: u64,
}

/// Given an execution report, print it out and write it to a CSV specified by report_path.
pub fn process_execution_report(
variant: ChainVariant,
Expand All @@ -20,53 +35,39 @@ pub fn process_execution_report(
let number_cycles = execution_report.total_instruction_count();
let number_syscalls = execution_report.total_syscall_count();

let bn_add_cycles = execution_report.cycle_tracker.get("precompile-bn-add").unwrap_or(&0);
let bn_mul_cycles = execution_report.cycle_tracker.get("precompile-bn-mul").unwrap_or(&0);
let bn_pair_cycles = execution_report.cycle_tracker.get("precompile-bn-pair").unwrap_or(&0);
let kzg_cyles =
execution_report.cycle_tracker.get("precompile-kzg-point-evaluation").unwrap_or(&0);
let bn_add_cycles = *execution_report.cycle_tracker.get("precompile-bn-add").unwrap_or(&0);
let bn_mul_cycles = *execution_report.cycle_tracker.get("precompile-bn-mul").unwrap_or(&0);
let bn_pair_cycles = *execution_report.cycle_tracker.get("precompile-bn-pair").unwrap_or(&0);
let kzg_point_eval_cycles =
*execution_report.cycle_tracker.get("precompile-kzg-point-evaluation").unwrap_or(&0);

// TODO: we can track individual syscalls in our CSV once we have sp1-core as a dependency
// let keccak_count = execution_report.syscall_counts.get(SyscallCode::KECCAK_PERMUTE);
// let secp256k1_decompress_count =
// execution_report.syscall_counts.get(SyscallCode::SECP256K1_DECOMPRESS);

let report_data = ExecutionReportData {
chain_id,
block_number,
gas_used,
tx_count,
number_cycles,
number_syscalls,
bn_add_cycles,
bn_mul_cycles,
bn_pair_cycles,
kzg_point_eval_cycles,
};

let report_file_exists = report_path.exists();

// Open the file for appending or create it if it doesn't exist
let file = OpenOptions::new().append(true).create(true).open(report_path)?;

let mut writer = Writer::from_writer(file);

// Write the header if the file doesn't exist
if !report_file_exists {
writer.write_record([
"chain_id",
"block_number",
"gas_used",
"tx_count",
"number_cycles",
"number_syscalls",
"bn_add_cycles",
"bn_mul_cycles",
"bn_pair_cycles",
"kzg_point_eval_cycles",
])?;
}

// Write the data
writer.write_record(&[
chain_id.to_string(),
block_number.to_string(),
gas_used.to_string(),
tx_count.to_string(),
number_cycles.to_string(),
number_syscalls.to_string(),
bn_add_cycles.to_string(),
bn_mul_cycles.to_string(),
bn_pair_cycles.to_string(),
kzg_cyles.to_string(),
])?;
// Serialize and write the data. Note: Writes the header if the file is empty.
writer.serialize(report_data)?;

writer.flush()?;

Expand Down

0 comments on commit abdc72a

Please sign in to comment.