|
| 1 | +#![cfg(feature = "kevm-backend")] |
| 2 | +use fe_yulgen::Db; |
| 3 | +use std::path::Path; |
| 4 | +use std::process::Command; |
| 5 | +use std::{env, fs}; |
| 6 | + |
| 7 | +const SPECS_DIR: &str = "tests/specs/fe/"; |
| 8 | + |
| 9 | +pub fn kevm_path() -> String { |
| 10 | + env::var("KEVM_PATH").expect("`KEVM_PATH` not set") |
| 11 | +} |
| 12 | + |
| 13 | +pub fn run_spec(name: &str, src_path: &str, src: &str, spec: &str) -> Result<(), String> { |
| 14 | + let kevm_path = kevm_path(); |
| 15 | + |
| 16 | + let spec = build_spec(name, src_path, src, spec); |
| 17 | + let spec_path = Path::new(&kevm_path) |
| 18 | + .join(SPECS_DIR) |
| 19 | + .join(name) |
| 20 | + .with_extension("k"); |
| 21 | + fs::write(spec_path.clone(), spec).expect("unable to write file"); |
| 22 | + |
| 23 | + let path = env::var("PATH").expect("PATH is not set"); |
| 24 | + |
| 25 | + let out = Command::new("kevm") |
| 26 | + .arg("prove") |
| 27 | + .arg(spec_path.to_str().unwrap()) |
| 28 | + .arg("--backend") |
| 29 | + .arg("haskell") |
| 30 | + .arg("--format-failures") |
| 31 | + // we should define out own verification module |
| 32 | + .arg("--directory") |
| 33 | + .arg("tests/specs/fe/verification/haskell") |
| 34 | + .env("PATH", format!(".build/usr/bin:{}", path)) |
| 35 | + .current_dir(&kevm_path) |
| 36 | + .output() |
| 37 | + .expect("failed to execute process"); |
| 38 | + |
| 39 | + if out.status.code() != Some(0) { |
| 40 | + Err(format!( |
| 41 | + "{}\n{}", |
| 42 | + String::from_utf8_lossy(&out.stderr), |
| 43 | + String::from_utf8_lossy(&out.stdout) |
| 44 | + )) |
| 45 | + } else { |
| 46 | + Ok(()) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +pub fn build_spec(name: &str, src_path: &str, src: &str, spec: &str) -> String { |
| 51 | + let mut db = Db::default(); |
| 52 | + let module = fe_driver::compile_single_file(&mut db, src_path, src, true, true).unwrap(); |
| 53 | + |
| 54 | + // replace placeholders |
| 55 | + let mut new_spec = spec.to_owned().replace("$TEST_NAME", &name.to_uppercase()); |
| 56 | + for (name, contract) in module.contracts.iter() { |
| 57 | + new_spec = new_spec.replace( |
| 58 | + &format!("${}::RUNTIME", name), |
| 59 | + &format!("\"0x{}\"", contract.runtime_bytecode), |
| 60 | + ) |
| 61 | + } |
| 62 | + |
| 63 | + new_spec |
| 64 | +} |
0 commit comments