mirror of
https://github.com/AleoHQ/leo.git
synced 2024-11-29 11:43:28 +03:00
Optimize implementation to use subcommands
This commit is contained in:
parent
9daeb358ba
commit
a72e4ce4a3
@ -5,6 +5,7 @@ use clap::{App, AppSettings, Arg, ArgMatches, SubCommand};
|
|||||||
|
|
||||||
pub trait CLI {
|
pub trait CLI {
|
||||||
type Options;
|
type Options;
|
||||||
|
type Output;
|
||||||
|
|
||||||
const NAME: NameType;
|
const NAME: NameType;
|
||||||
const ABOUT: AboutType;
|
const ABOUT: AboutType;
|
||||||
@ -73,5 +74,5 @@ pub trait CLI {
|
|||||||
fn parse(arguments: &ArgMatches) -> Result<Self::Options, CLIError>;
|
fn parse(arguments: &ArgMatches) -> Result<Self::Options, CLIError>;
|
||||||
|
|
||||||
#[cfg_attr(tarpaulin, skip)]
|
#[cfg_attr(tarpaulin, skip)]
|
||||||
fn output(options: Self::Options) -> Result<(), CLIError>;
|
fn output(options: Self::Options) -> Result<Self::Output, CLIError>;
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,7 @@ pub struct BuildCommand;
|
|||||||
|
|
||||||
impl CLI for BuildCommand {
|
impl CLI for BuildCommand {
|
||||||
type Options = ();
|
type Options = ();
|
||||||
|
type Output = Compiler<Fr>;
|
||||||
|
|
||||||
const NAME: NameType = "build";
|
const NAME: NameType = "build";
|
||||||
const ABOUT: AboutType = "Compile the current package";
|
const ABOUT: AboutType = "Compile the current package";
|
||||||
@ -30,7 +31,7 @@ impl CLI for BuildCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg_attr(tarpaulin, skip)]
|
#[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 path = current_dir()?;
|
||||||
let _manifest = Manifest::try_from(&path)?;
|
let _manifest = Manifest::try_from(&path)?;
|
||||||
|
|
||||||
@ -56,8 +57,8 @@ impl CLI for BuildCommand {
|
|||||||
log::info!("Compiling program located in {:?}", main_file_path);
|
log::info!("Compiling program located in {:?}", main_file_path);
|
||||||
|
|
||||||
// Compile from the 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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,6 +12,7 @@ pub struct InitCommand;
|
|||||||
|
|
||||||
impl CLI for InitCommand {
|
impl CLI for InitCommand {
|
||||||
type Options = Option<String>;
|
type Options = Option<String>;
|
||||||
|
type Output = ();
|
||||||
|
|
||||||
const NAME: NameType = "init";
|
const NAME: NameType = "init";
|
||||||
const ABOUT: AboutType = "Creates a new Leo package in an existing directory";
|
const ABOUT: AboutType = "Creates a new Leo package in an existing directory";
|
||||||
@ -26,7 +27,7 @@ impl CLI for InitCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg_attr(tarpaulin, skip)]
|
#[cfg_attr(tarpaulin, skip)]
|
||||||
fn output(options: Self::Options) -> Result<(), CLIError> {
|
fn output(options: Self::Options) -> Result<Self::Output, CLIError> {
|
||||||
let name = options;
|
let name = options;
|
||||||
let path = current_dir()?;
|
let path = current_dir()?;
|
||||||
|
|
||||||
|
@ -13,6 +13,7 @@ pub struct NewCommand;
|
|||||||
|
|
||||||
impl CLI for NewCommand {
|
impl CLI for NewCommand {
|
||||||
type Options = Option<String>;
|
type Options = Option<String>;
|
||||||
|
type Output = ();
|
||||||
|
|
||||||
const NAME: NameType = "new";
|
const NAME: NameType = "new";
|
||||||
const ABOUT: AboutType = "Creates a new Leo package";
|
const ABOUT: AboutType = "Creates a new Leo package";
|
||||||
@ -33,7 +34,7 @@ impl CLI for NewCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg_attr(tarpaulin, skip)]
|
#[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()?;
|
let mut path = current_dir()?;
|
||||||
|
|
||||||
// Derive the package name
|
// Derive the package name
|
||||||
|
@ -1,22 +1,18 @@
|
|||||||
use crate::{cli::*, cli_types::*};
|
use crate::{cli::*, cli_types::*};
|
||||||
|
use crate::commands::BuildCommand;
|
||||||
use crate::compiler::Compiler;
|
use crate::compiler::Compiler;
|
||||||
use crate::directories::{OutputsDirectory, source::SOURCE_DIRECTORY_NAME};
|
use crate::errors::CLIError;
|
||||||
use crate::errors::{CLIError, RunError};
|
|
||||||
use crate::files::{MainFile, MAIN_FILE_NAME};
|
|
||||||
use crate::manifest::Manifest;
|
|
||||||
|
|
||||||
use snarkos_curves::bls12_377::Fr;
|
use snarkos_curves::bls12_377::Fr;
|
||||||
|
|
||||||
use clap::ArgMatches;
|
use clap::ArgMatches;
|
||||||
use std::convert::TryFrom;
|
|
||||||
use std::env::current_dir;
|
|
||||||
use std::path::PathBuf;
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct RunCommand;
|
pub struct RunCommand;
|
||||||
|
|
||||||
impl CLI for RunCommand {
|
impl CLI for RunCommand {
|
||||||
type Options = ();
|
type Options = ();
|
||||||
|
type Output = ();
|
||||||
|
|
||||||
const NAME: NameType = "run";
|
const NAME: NameType = "run";
|
||||||
const ABOUT: AboutType = "Run a program with inputs";
|
const ABOUT: AboutType = "Run a program with inputs";
|
||||||
@ -31,32 +27,10 @@ impl CLI for RunCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg_attr(tarpaulin, skip)]
|
#[cfg_attr(tarpaulin, skip)]
|
||||||
fn output(_options: Self::Options) -> Result<(), CLIError> {
|
fn output(options: Self::Options) -> Result<(), CLIError> {
|
||||||
let path = current_dir()?;
|
let circuit = BuildCommand::output(options)?;
|
||||||
let _manifest = Manifest::try_from(&path)?;
|
|
||||||
|
|
||||||
// Sanitize the package path to the root directory
|
fn run(circuit: Compiler<Fr>) {
|
||||||
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) {
|
|
||||||
|
|
||||||
use snarkos_algorithms::snark::{create_random_proof, generate_random_parameters, prepare_verifying_key, verify_proof};
|
use snarkos_algorithms::snark::{create_random_proof, generate_random_parameters, prepare_verifying_key, verify_proof};
|
||||||
use snarkos_curves::bls12_377::Bls12_377;
|
use snarkos_curves::bls12_377::Bls12_377;
|
||||||
@ -72,20 +46,14 @@ impl CLI for RunCommand {
|
|||||||
|
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
|
|
||||||
let params = {
|
let params = generate_random_parameters::<Bls12_377, _, _>(circuit.clone(), rng).unwrap();
|
||||||
let circuit = Compiler::<Fr>::init(main_file_path.clone());
|
|
||||||
generate_random_parameters::<Bls12_377, _, _>(circuit, rng).unwrap()
|
|
||||||
};
|
|
||||||
|
|
||||||
let prepared_verifying_key = prepare_verifying_key::<Bls12_377>(¶ms.vk);
|
let prepared_verifying_key = prepare_verifying_key::<Bls12_377>(¶ms.vk);
|
||||||
|
|
||||||
setup += start.elapsed();
|
setup += start.elapsed();
|
||||||
|
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
let proof = {
|
let proof = create_random_proof(circuit, ¶ms, rng).unwrap();
|
||||||
let circuit = Compiler::<Fr>::init(main_file_path);
|
|
||||||
create_random_proof(circuit, ¶ms, rng).unwrap()
|
|
||||||
};
|
|
||||||
|
|
||||||
proving += start.elapsed();
|
proving += start.elapsed();
|
||||||
|
|
||||||
@ -108,7 +76,7 @@ impl CLI for RunCommand {
|
|||||||
println!(" ");
|
println!(" ");
|
||||||
}
|
}
|
||||||
|
|
||||||
run(main_file_path);
|
run(circuit);
|
||||||
|
|
||||||
// let source_files = SourceDirectory::files(&package_path)?;
|
// let source_files = SourceDirectory::files(&package_path)?;
|
||||||
// BuildDirectory::create(&circuit_path).map_err(Error::BuildDirectory)?;
|
// BuildDirectory::create(&circuit_path).map_err(Error::BuildDirectory)?;
|
||||||
|
@ -13,6 +13,7 @@ use std::{
|
|||||||
path::PathBuf,
|
path::PathBuf,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
pub struct Compiler<F: Field + PrimeField> {
|
pub struct Compiler<F: Field + PrimeField> {
|
||||||
main_file_path: PathBuf,
|
main_file_path: PathBuf,
|
||||||
_engine: PhantomData<F>,
|
_engine: PhantomData<F>,
|
||||||
|
18
leo/main.rs
18
leo/main.rs
@ -27,12 +27,20 @@ fn main() -> Result<(), CLIError> {
|
|||||||
.set_term_width(0)
|
.set_term_width(0)
|
||||||
.get_matches();
|
.get_matches();
|
||||||
|
|
||||||
|
|
||||||
match arguments.subcommand() {
|
match arguments.subcommand() {
|
||||||
("new", Some(arguments)) => NewCommand::output(NewCommand::parse(arguments)?),
|
("new", Some(arguments)) => {
|
||||||
("init", Some(arguments)) => InitCommand::output(InitCommand::parse(arguments)?),
|
NewCommand::output(NewCommand::parse(arguments)?)
|
||||||
("build", Some(arguments)) => BuildCommand::output(BuildCommand::parse(arguments)?),
|
},
|
||||||
("run", Some(arguments)) => RunCommand::output(RunCommand::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!(),
|
_ => unreachable!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user