From 6f8443fdaf39d8e07bcdb099b0812fe0d40122da Mon Sep 17 00:00:00 2001 From: damirka Date: Wed, 18 Aug 2021 14:41:27 +0300 Subject: [PATCH 1/3] removes ast snapshots on leo clean --- leo/commands/clean.rs | 9 ++- package/src/outputs/ast_snapshot.rs | 117 ++++++++++++++++++++++++++++ package/src/outputs/mod.rs | 3 + 3 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 package/src/outputs/ast_snapshot.rs diff --git a/leo/commands/clean.rs b/leo/commands/clean.rs index 83c2503555..d8f1f3d8a4 100644 --- a/leo/commands/clean.rs +++ b/leo/commands/clean.rs @@ -16,7 +16,9 @@ use crate::{commands::Command, context::Context}; use leo_compiler::OutputFile; -use leo_package::outputs::{ChecksumFile, CircuitFile, ProofFile, ProvingKeyFile, VerificationKeyFile}; +use leo_package::outputs::{ + ChecksumFile, CircuitFile, ProofFile, ProvingKeyFile, Snapshot, SnapshotFile, VerificationKeyFile, +}; use anyhow::Result; use structopt::StructOpt; @@ -61,6 +63,11 @@ impl Command for Clean { // Remove the proof from the output directory ProofFile::new(&package_name).remove(&path)?; + // Remove AST snapshots from the output directory + SnapshotFile::new(&package_name, Snapshot::Initial).remove(&path)?; + SnapshotFile::new(&package_name, Snapshot::TypeInference).remove(&path)?; + SnapshotFile::new(&package_name, Snapshot::Canonicalization).remove(&path)?; + Ok(()) } } diff --git a/package/src/outputs/ast_snapshot.rs b/package/src/outputs/ast_snapshot.rs new file mode 100644 index 0000000000..75d2621b1f --- /dev/null +++ b/package/src/outputs/ast_snapshot.rs @@ -0,0 +1,117 @@ +// Copyright (C) 2019-2021 Aleo Systems Inc. +// This file is part of the Leo library. + +// The Leo library is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// The Leo library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with the Leo library. If not, see . + +//! The serialized circuit output file. + +use crate::{errors::CircuitFileError, outputs::OUTPUTS_DIRECTORY_NAME}; + +use serde::Deserialize; +use std::{ + borrow::Cow, + fmt, + fs::{ + File, {self}, + }, + io::Write, + path::Path, +}; + +/// Enum to handle all 3 types of snapshots. +#[derive(Deserialize)] +pub enum Snapshot { + Initial, + TypeInference, + Canonicalization, +} + +impl fmt::Display for Snapshot { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!( + f, + "{}", + match self { + Self::Initial => "initial_ast", + Self::TypeInference => "type_inferenced_ast", + Self::Canonicalization => "canonicalization_ast", + } + ) + } +} + +pub static AST_SNAPSHOT_FILE_EXTENSION: &str = ".json"; + +/// Generic Snapshot file wrapper. Each package can have up to 3 +/// different snapshots: initial_ast, canonicalization_ast and type_inferenced_ast; +#[derive(Deserialize)] +pub struct SnapshotFile { + pub package_name: String, + pub snapshot: Snapshot, +} + +impl SnapshotFile { + pub fn new(package_name: &str, snapshot: Snapshot) -> Self { + Self { + package_name: package_name.to_string(), + snapshot, + } + } + + pub fn exists_at(&self, path: &Path) -> bool { + let path = self.snapshot_file_path(path); + path.exists() + } + + /// Reads the serialized circuit from the given file path if it exists. + pub fn read_from(&self, path: &Path) -> Result { + let path = self.snapshot_file_path(path); + + fs::read_to_string(&path).map_err(|_| CircuitFileError::FileReadError(path.into_owned())) + } + + /// Writes the given serialized circuit to a file. + pub fn write_to(&self, path: &Path, circuit: String) -> Result<(), CircuitFileError> { + let path = self.snapshot_file_path(path); + + let mut file = File::create(&path)?; + file.write_all(circuit.as_bytes())?; + + Ok(()) + } + + /// Removes the serialized circuit at the given path if it exists. Returns `true` on success, + /// `false` if the file doesn't exist, and `Error` if the file system fails during operation. + pub fn remove(&self, path: &Path) -> Result { + let path = self.snapshot_file_path(path); + if !path.exists() { + return Ok(false); + } + + fs::remove_file(&path).map_err(|_| CircuitFileError::FileRemovalError(path.into_owned()))?; + Ok(true) + } + + fn snapshot_file_path<'a>(&self, path: &'a Path) -> Cow<'a, Path> { + let mut path = Cow::from(path); + if path.is_dir() { + if !path.ends_with(OUTPUTS_DIRECTORY_NAME) { + path.to_mut().push(OUTPUTS_DIRECTORY_NAME); + } + path.to_mut() + .push(format!("{}{}", self.snapshot, AST_SNAPSHOT_FILE_EXTENSION)); + } + path + } +} diff --git a/package/src/outputs/mod.rs b/package/src/outputs/mod.rs index f4c02b8f18..e65058198e 100644 --- a/package/src/outputs/mod.rs +++ b/package/src/outputs/mod.rs @@ -29,5 +29,8 @@ pub use self::proof::*; pub mod proving_key; pub use self::proving_key::*; +pub mod ast_snapshot; +pub use self::ast_snapshot::*; + pub mod verification_key; pub use self::verification_key::*; From 9caca65409ff4816dbee5d74cf3fb13154eb02f1 Mon Sep 17 00:00:00 2001 From: damirka Date: Wed, 18 Aug 2021 18:07:05 +0300 Subject: [PATCH 2/3] alphabetical in package/outputs/mod --- package/src/outputs/mod.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/package/src/outputs/mod.rs b/package/src/outputs/mod.rs index e65058198e..d448130673 100644 --- a/package/src/outputs/mod.rs +++ b/package/src/outputs/mod.rs @@ -14,6 +14,9 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . +pub mod ast_snapshot; +pub use self::ast_snapshot::*; + pub mod circuit; pub use self::circuit::*; @@ -29,8 +32,5 @@ pub use self::proof::*; pub mod proving_key; pub use self::proving_key::*; -pub mod ast_snapshot; -pub use self::ast_snapshot::*; - pub mod verification_key; pub use self::verification_key::*; From 79fb41eac43018e8f814ece2b996ed4d4836fed4 Mon Sep 17 00:00:00 2001 From: damirka Date: Thu, 19 Aug 2021 17:03:12 +0300 Subject: [PATCH 3/3] fmt --- leo/commands/clean.rs | 4 +++- package/src/outputs/ast_snapshot.rs | 10 +++------- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/leo/commands/clean.rs b/leo/commands/clean.rs index 6e09bcfa43..4b1f85b67b 100644 --- a/leo/commands/clean.rs +++ b/leo/commands/clean.rs @@ -17,7 +17,9 @@ use crate::{commands::Command, context::Context}; use leo_compiler::OutputFile; use leo_errors::Result; -use leo_package::outputs::{ChecksumFile, CircuitFile, ProofFile, ProvingKeyFile, Snapshot, SnapshotFile, VerificationKeyFile}; +use leo_package::outputs::{ + ChecksumFile, CircuitFile, ProofFile, ProvingKeyFile, Snapshot, SnapshotFile, VerificationKeyFile, +}; use structopt::StructOpt; use tracing::span::Span; diff --git a/package/src/outputs/ast_snapshot.rs b/package/src/outputs/ast_snapshot.rs index 7b910e1a0d..e48a9977ec 100644 --- a/package/src/outputs/ast_snapshot.rs +++ b/package/src/outputs/ast_snapshot.rs @@ -20,12 +20,7 @@ use crate::outputs::OUTPUTS_DIRECTORY_NAME; use leo_errors::{PackageError, Result}; use serde::Deserialize; -use std::{ - borrow::Cow, - fmt, - fs, - path::Path, -}; +use std::{borrow::Cow, fmt, fs, path::Path}; /// Enum to handle all 3 types of snapshots. #[derive(Deserialize)] @@ -76,7 +71,8 @@ impl SnapshotFile { pub fn read_from(&self, path: &Path) -> Result { let path = self.snapshot_file_path(path); - let result = fs::read_to_string(&path).map_err(|_| PackageError::failed_to_read_snapshot_file(path.into_owned()))?; + let result = + fs::read_to_string(&path).map_err(|_| PackageError::failed_to_read_snapshot_file(path.into_owned()))?; Ok(result) }