use core::panic;
use hdp_cairo_runner::dry_run::DryRunResult;
use hdp_cairo_runner::{cairo_dry_run, input::dry_run::DryRunnerProgramInput};
use hdp_primitives::constant::DRY_CAIRO_RUN_OUTPUT_FILE;
use hdp_primitives::processed_types::cairo_format;
use hdp_primitives::task::ExtendedModule;
use hdp_provider::evm::from_keys::categorize_fetch_keys;
use hdp_provider::evm::provider::EvmProvider;
use std::collections::HashMap;
use std::path::PathBuf;
use tracing::info;
use super::config::CompilerConfig;
use super::{Compilable, CompilationResult, CompileError};
pub type ModuleVec = Vec<ExtendedModule>;
impl Compilable for ModuleVec {
async fn compile(
&self,
compile_config: &CompilerConfig,
) -> Result<CompilationResult, CompileError> {
info!("target task: {:#?}", self[0].task);
let dry_run_program_path = compile_config.dry_run_program_path.clone();
let tasks_commitments = self
.iter()
.map(|module| module.task.commit())
.collect::<Vec<_>>();
let input = generate_input(self.to_vec(), PathBuf::from(DRY_CAIRO_RUN_OUTPUT_FILE)).await?;
let input_string =
serde_json::to_string_pretty(&input).expect("Failed to serialize module class");
info!("2. Running dry-run... ");
let keys: DryRunResult = cairo_dry_run(
dry_run_program_path,
input_string,
compile_config.save_fetch_keys_file.clone(),
)?;
if keys[0].program_hash != self[0].task.program_hash {
return Err(CompileError::ClassHashMismatch);
}
if keys.len() != 1 {
panic!("Multiple Modules are not supported yet");
}
let dry_runned_module = keys.into_iter().next().unwrap();
let mut commit_results_maps = HashMap::new();
commit_results_maps.insert(
tasks_commitments[0],
dry_runned_module.result.to_combined_string().into(),
);
let keys_maps_chain = categorize_fetch_keys(dry_runned_module.fetch_keys);
if keys_maps_chain.len() > 1 {
panic!("Multiple chain id is not supported yet");
}
let (_, keys) = keys_maps_chain.into_iter().next().unwrap();
info!("3. Fetching proofs from provider...");
let provider = EvmProvider::new(compile_config.provider_config.clone());
let results = provider.fetch_proofs_from_keys(keys).await?;
Ok(CompilationResult::new(
true,
commit_results_maps,
results.headers.into_iter().collect(),
results.accounts.into_iter().collect(),
results.storages.into_iter().collect(),
results.transactions.into_iter().collect(),
results.transaction_receipts.into_iter().collect(),
results.mmr_metas.into_iter().collect(),
))
}
}
async fn generate_input(
extended_modules: Vec<ExtendedModule>,
identified_keys_file: PathBuf,
) -> Result<DryRunnerProgramInput, CompileError> {
let mut collected_results = Vec::new();
for module in extended_modules {
let input_module =
cairo_format::DryRunProcessedModule::new(module.task.inputs, module.module_class);
collected_results.push(input_module);
}
Ok(DryRunnerProgramInput::new(
identified_keys_file,
collected_results,
))
}