Optimize implementation to use subcommands

This commit is contained in:
howardwu 2020-04-25 18:11:49 -07:00
parent 9daeb358ba
commit a72e4ce4a3
7 changed files with 33 additions and 52 deletions

View File

@ -5,6 +5,7 @@ use clap::{App, AppSettings, Arg, ArgMatches, SubCommand};
pub trait CLI {
type Options;
type Output;
const NAME: NameType;
const ABOUT: AboutType;
@ -73,5 +74,5 @@ pub trait CLI {
fn parse(arguments: &ArgMatches) -> Result<Self::Options, CLIError>;
#[cfg_attr(tarpaulin, skip)]
fn output(options: Self::Options) -> Result<(), CLIError>;
fn output(options: Self::Options) -> Result<Self::Output, CLIError>;
}

View File

@ -16,6 +16,7 @@ pub struct BuildCommand;
impl CLI for BuildCommand {
type Options = ();
type Output = Compiler<Fr>;
const NAME: NameType = "build";
const ABOUT: AboutType = "Compile the current package";
@ -30,7 +31,7 @@ impl CLI for BuildCommand {
}
#[cfg_attr(tarpaulin, skip)]
fn output(_options: Self::Options) -> Result<(), CLIError> {
fn output(_options: Self::Options) -> Result<Self::Output, CLIError> {
let path = current_dir()?;
let _manifest = Manifest::try_from(&path)?;
@ -56,8 +57,8 @@ impl CLI for BuildCommand {
log::info!("Compiling program located in {:?}", main_file_path);
// Compile from the main file path
let _circuit = Compiler::<Fr>::init(main_file_path);
let circuit = Compiler::<Fr>::init(main_file_path);
Ok(())
Ok(circuit)
}
}

View File

@ -12,6 +12,7 @@ pub struct InitCommand;
impl CLI for InitCommand {
type Options = Option<String>;
type Output = ();
const NAME: NameType = "init";
const ABOUT: AboutType = "Creates a new Leo package in an existing directory";
@ -26,7 +27,7 @@ impl CLI for InitCommand {
}
#[cfg_attr(tarpaulin, skip)]
fn output(options: Self::Options) -> Result<(), CLIError> {
fn output(options: Self::Options) -> Result<Self::Output, CLIError> {
let name = options;
let path = current_dir()?;

View File

@ -13,6 +13,7 @@ pub struct NewCommand;
impl CLI for NewCommand {
type Options = Option<String>;
type Output = ();
const NAME: NameType = "new";
const ABOUT: AboutType = "Creates a new Leo package";
@ -33,7 +34,7 @@ impl CLI for NewCommand {
}
#[cfg_attr(tarpaulin, skip)]
fn output(options: Self::Options) -> Result<(), CLIError> {
fn output(options: Self::Options) -> Result<Self::Output, CLIError> {
let mut path = current_dir()?;
// Derive the package name

View File

@ -1,22 +1,18 @@
use crate::{cli::*, cli_types::*};
use crate::commands::BuildCommand;
use crate::compiler::Compiler;
use crate::directories::{OutputsDirectory, source::SOURCE_DIRECTORY_NAME};
use crate::errors::{CLIError, RunError};
use crate::files::{MainFile, MAIN_FILE_NAME};
use crate::manifest::Manifest;
use crate::errors::CLIError;
use snarkos_curves::bls12_377::Fr;
use clap::ArgMatches;
use std::convert::TryFrom;
use std::env::current_dir;
use std::path::PathBuf;
#[derive(Debug)]
pub struct RunCommand;
impl CLI for RunCommand {
type Options = ();
type Output = ();
const NAME: NameType = "run";
const ABOUT: AboutType = "Run a program with inputs";
@ -31,32 +27,10 @@ impl CLI for RunCommand {
}
#[cfg_attr(tarpaulin, skip)]
fn output(_options: Self::Options) -> Result<(), CLIError> {
let path = current_dir()?;
let _manifest = Manifest::try_from(&path)?;
fn output(options: Self::Options) -> Result<(), CLIError> {
let circuit = BuildCommand::output(options)?;
// Sanitize the package path to the root directory
let mut package_path = path.clone();
if package_path.is_file() {
package_path.pop();
}
// Verify the main file exists
if !MainFile::exists_at(&package_path) {
return Err(RunError::MainFileDoesNotExist(package_path.as_os_str().to_owned()).into());
}
// Create the outputs directory
OutputsDirectory::create(&package_path)?;
// Construct the path to the main file in the source directory
let mut main_file_path = package_path.clone();
main_file_path.push(SOURCE_DIRECTORY_NAME);
main_file_path.push(MAIN_FILE_NAME);
log::info!("Compiling program located in {:?}", main_file_path);
fn run(main_file_path: PathBuf) {
fn run(circuit: Compiler<Fr>) {
use snarkos_algorithms::snark::{create_random_proof, generate_random_parameters, prepare_verifying_key, verify_proof};
use snarkos_curves::bls12_377::Bls12_377;
@ -72,20 +46,14 @@ impl CLI for RunCommand {
let start = Instant::now();
let params = {
let circuit = Compiler::<Fr>::init(main_file_path.clone());
generate_random_parameters::<Bls12_377, _, _>(circuit, rng).unwrap()
};
let params = generate_random_parameters::<Bls12_377, _, _>(circuit.clone(), rng).unwrap();
let prepared_verifying_key = prepare_verifying_key::<Bls12_377>(&params.vk);
setup += start.elapsed();
let start = Instant::now();
let proof = {
let circuit = Compiler::<Fr>::init(main_file_path);
create_random_proof(circuit, &params, rng).unwrap()
};
let proof = create_random_proof(circuit, &params, rng).unwrap();
proving += start.elapsed();
@ -108,7 +76,7 @@ impl CLI for RunCommand {
println!(" ");
}
run(main_file_path);
run(circuit);
// let source_files = SourceDirectory::files(&package_path)?;
// BuildDirectory::create(&circuit_path).map_err(Error::BuildDirectory)?;

View File

@ -13,6 +13,7 @@ use std::{
path::PathBuf,
};
#[derive(Clone)]
pub struct Compiler<F: Field + PrimeField> {
main_file_path: PathBuf,
_engine: PhantomData<F>,

View File

@ -27,12 +27,20 @@ fn main() -> Result<(), CLIError> {
.set_term_width(0)
.get_matches();
match arguments.subcommand() {
("new", Some(arguments)) => NewCommand::output(NewCommand::parse(arguments)?),
("init", Some(arguments)) => InitCommand::output(InitCommand::parse(arguments)?),
("build", Some(arguments)) => BuildCommand::output(BuildCommand::parse(arguments)?),
("run", Some(arguments)) => RunCommand::output(RunCommand::parse(arguments)?),
("new", Some(arguments)) => {
NewCommand::output(NewCommand::parse(arguments)?)
},
("init", Some(arguments)) => {
InitCommand::output(InitCommand::parse(arguments)?)
},
("build", Some(arguments)) => {
BuildCommand::output(BuildCommand::parse(arguments)?)?;
Ok(())
},
("run", Some(arguments)) => {
RunCommand::output(RunCommand::parse(arguments)?)
},
_ => unreachable!(),
}
}