Directly use Process in compile tests

This commit is contained in:
Pranav Gaddamadugu 2024-03-26 13:10:50 -07:00
parent f4af8a77d2
commit 2213feb027
3 changed files with 30 additions and 22 deletions

View File

@ -23,8 +23,8 @@ use utilities::{
hash_content,
hash_symbol_tables,
parse_program,
setup_build_directory,
BufferEmitter,
CurrentNetwork,
};
use disassembler::disassemble_from_str;
@ -43,6 +43,7 @@ use indexmap::IndexMap;
use leo_span::Symbol;
use serde::{Deserialize, Serialize};
use serde_yaml::Value;
use snarkvm::{prelude::Process, synthesizer::program::ProgramCore};
use std::{fs, path::Path, rc::Rc};
struct CompileNamespace;
@ -111,6 +112,9 @@ fn run_test(test: Test, handler: &Handler, buf: &BufferEmitter) -> Result<Value,
// Initialize storage for the stubs.
let mut import_stubs = IndexMap::new();
// Initialize a `Process`. This should always succeed.
let mut process = Process::<CurrentNetwork>::load().unwrap();
// Compile each program string separately.
for program_string in program_strings {
// Parse the program.
@ -124,20 +128,20 @@ fn run_test(test: Test, handler: &Handler, buf: &BufferEmitter) -> Result<Value,
// Compile the program to bytecode.
let program_name = parsed.program_name.to_string();
let full_program_name = format!("{program_name}.{}", parsed.network);
let bytecode = handler.extend_if_error(compile_and_process(&mut parsed))?;
// Parse the bytecode as an Aleo program.
// Note that this function checks that the bytecode is well-formed.
let aleo_program = handler.extend_if_error(ProgramCore::from_str(&bytecode).map_err(LeoError::Anyhow))?;
// Add the program to the process.
// Note that this function performs an additional validity check on the bytecode.
handler.extend_if_error(process.add_program(&aleo_program).map_err(LeoError::Anyhow))?;
// Add the bytecode to the import stubs.
let stub = handler.extend_if_error(disassemble_from_str(&bytecode).map_err(|err| err.into()))?;
import_stubs.insert(Symbol::intern(&program_name), stub);
// Set up the build directory.
// Note that this function checks that the bytecode is well-formed.
let package = setup_build_directory(&full_program_name, &bytecode, handler)?;
// Get the program process and check all instructions.
handler.extend_if_error(package.get_process().map_err(LeoError::Anyhow))?;
// Hash the ast files.
let (initial_ast, unrolled_ast, ssa_ast, flattened_ast, destructured_ast, inlined_ast, dce_ast) =
hash_asts();

View File

@ -26,9 +26,9 @@ use utilities::{
hash_symbol_tables,
parse_program,
setup_build_directory,
Aleo,
BufferEmitter,
Network,
CurrentAleo,
CurrentNetwork,
};
use leo_compiler::{CompilerOptions, OutputOptions};
@ -160,7 +160,7 @@ fn run_test(test: Test, handler: &Handler, err_buf: &BufferEmitter) -> Result<Va
.as_sequence()
.unwrap()
.iter()
.map(|input| console::program::Value::<Network>::from_str(input.as_str().unwrap()).unwrap())
.map(|input| console::program::Value::<CurrentNetwork>::from_str(input.as_str().unwrap()).unwrap())
.collect();
let input_string = format!("[{}]", inputs.iter().map(|input| input.to_string()).join(", "));
let private_key = match case.get(&Value::from("private_key")) {
@ -173,7 +173,7 @@ fn run_test(test: Test, handler: &Handler, err_buf: &BufferEmitter) -> Result<Va
// TODO: Add support for custom config like custom private keys.
// Execute the program and get the outputs.
let output_string = match package.run::<Aleo, _>(&private_key, function_name, &inputs, rng) {
let output_string = match package.run::<CurrentAleo, _>(&private_key, function_name, &inputs, rng) {
Ok((response, _)) => format!(
"[{}]",
response

View File

@ -42,9 +42,9 @@ use std::{
rc::Rc,
};
pub type Network = Testnet3;
pub type CurrentNetwork = Testnet3;
#[allow(unused)]
pub type Aleo = snarkvm::circuit::AleoV0;
pub type CurrentAleo = snarkvm::circuit::AleoV0;
pub fn hash_asts() -> (String, String, String, String, String, String, String) {
let initial_ast = hash_file("/tmp/output/test.initial_ast.json");
@ -107,12 +107,16 @@ pub fn get_build_options(test_config: &TestConfig) -> Vec<BuildOptions> {
}
#[allow(unused)]
pub fn setup_build_directory(program_name: &str, bytecode: &String, handler: &Handler) -> Result<Package<Network>, ()> {
pub fn setup_build_directory(
program_name: &str,
bytecode: &String,
handler: &Handler,
) -> Result<Package<CurrentNetwork>, ()> {
// Initialize a temporary directory.
let directory = temp_dir();
// Create the program id.
let program_id = ProgramID::<Network>::from_str(program_name).unwrap();
let program_id = ProgramID::<CurrentNetwork>::from_str(program_name).unwrap();
// Write the program string to a file in the temporary directory.
let path = directory.join("main.aleo");
@ -123,8 +127,8 @@ pub fn setup_build_directory(program_name: &str, bytecode: &String, handler: &Ha
let _manifest_file = Manifest::create(&directory, &program_id).unwrap();
// Create the environment file.
Env::<Network>::new().unwrap().write_to(&directory).unwrap();
if Env::<Network>::exists_at(&directory) {
Env::<CurrentNetwork>::new().unwrap().write_to(&directory).unwrap();
if Env::<CurrentNetwork>::exists_at(&directory) {
println!(".env file created at {:?}", &directory);
}
@ -133,7 +137,7 @@ pub fn setup_build_directory(program_name: &str, bytecode: &String, handler: &Ha
fs::create_dir_all(build_directory).unwrap();
// Open the package at the temporary directory.
handler.extend_if_error(Package::<Network>::open(&directory).map_err(LeoError::Anyhow))
handler.extend_if_error(Package::<CurrentNetwork>::open(&directory).map_err(LeoError::Anyhow))
}
pub fn new_compiler(
@ -263,11 +267,11 @@ pub fn compile_and_process<'a>(parsed: &'a mut Compiler<'a>) -> Result<String, L
/// Returns the private key from the .env file specified in the directory.
#[allow(unused)]
pub fn dotenv_private_key(directory: &Path) -> Result<PrivateKey<Network>> {
pub fn dotenv_private_key(directory: &Path) -> Result<PrivateKey<CurrentNetwork>> {
use std::str::FromStr;
dotenvy::from_path(directory.join(".env")).map_err(|_| anyhow!("Missing a '.env' file in the test directory."))?;
// Load the private key from the environment.
let private_key = dotenvy::var("PRIVATE_KEY").map_err(|e| anyhow!("Missing PRIVATE_KEY - {e}"))?;
// Parse the private key.
PrivateKey::<Network>::from_str(&private_key)
PrivateKey::<CurrentNetwork>::from_str(&private_key)
}