From cdc7ebf9a0707f4f794c8decbb30fcbb1fa00a1d Mon Sep 17 00:00:00 2001 From: KtorZ Date: Wed, 5 Apr 2023 11:51:26 +0200 Subject: [PATCH] Remove now-unnecessary blueprint generics. These were needed before as a way to _partially deserialize_ blueprints. Indeed, some commands required accessing information of the blueprint, but not necessarily the schema. So out of laziness (or cleverness?), we only deserialized validators as serde::Value and achieved that through the use of generics. Now that validators and schemas have proper deserialisers, we can simply deserialize a blueprint. TODO: Our serialisation/deserialisation is safe with regards to itself; i.e. it roundtrips. However, we only supports a subset of the specified blueprint format. For example, we would fail to deserialize blueprints that have inline data-schemas (we only use references). --- crates/aiken-project/src/blueprint/mod.rs | 26 ++++++++----------- .../aiken-project/src/blueprint/validator.rs | 26 ++++++++----------- crates/aiken-project/src/lib.rs | 19 ++++---------- crates/aiken/src/cmd/blueprint/convert.rs | 2 +- 4 files changed, 28 insertions(+), 45 deletions(-) diff --git a/crates/aiken-project/src/blueprint/mod.rs b/crates/aiken-project/src/blueprint/mod.rs index ae5334ae..f9792d4b 100644 --- a/crates/aiken-project/src/blueprint/mod.rs +++ b/crates/aiken-project/src/blueprint/mod.rs @@ -5,18 +5,18 @@ pub mod validator; use crate::{config::Config, module::CheckedModules}; use aiken_lang::gen_uplc::CodeGenerator; -use definitions::{Definitions, Reference}; +use definitions::Definitions; use error::Error; use schema::{Annotated, Schema}; use std::fmt::Debug; use validator::Validator; #[derive(Debug, PartialEq, Clone, serde::Serialize, serde::Deserialize)] -pub struct Blueprint { +pub struct Blueprint { pub preamble: Preamble, - pub validators: Vec>, + pub validators: Vec, #[serde(skip_serializing_if = "Definitions::is_empty", default)] - pub definitions: Definitions, + pub definitions: Definitions>, } #[derive(Debug, PartialEq, Clone, serde::Serialize, serde::Deserialize)] @@ -48,7 +48,7 @@ pub enum LookupResult<'a, T> { Many, } -impl Blueprint> { +impl Blueprint { pub fn new( config: &Config, modules: &CheckedModules, @@ -82,12 +82,8 @@ impl Blueprint> { } } -impl Blueprint -where - R: Clone + Default, - S: Clone + Default, -{ - pub fn lookup(&self, title: Option<&String>) -> Option>> { +impl Blueprint { + pub fn lookup(&self, title: Option<&String>) -> Option> { let mut validator = None; for v in self.validators.iter() { @@ -112,7 +108,7 @@ where action: F, ) -> Result where - F: Fn(Validator) -> Result, + F: Fn(Validator) -> Result, { match self.lookup(title) { Some(LookupResult::One(validator)) => action(validator.to_owned()), @@ -152,7 +148,7 @@ mod test { #[test] fn serialize_no_description() { - let blueprint: Blueprint> = Blueprint { + let blueprint = Blueprint { preamble: Preamble { title: "Foo".to_string(), description: None, @@ -179,7 +175,7 @@ mod test { #[test] fn serialize_with_description() { - let blueprint: Blueprint> = Blueprint { + let blueprint = Blueprint { preamble: Preamble { title: "Foo".to_string(), description: Some("Lorem ipsum".to_string()), @@ -227,7 +223,7 @@ mod test { ) .unwrap(); - let blueprint: Blueprint> = Blueprint { + let blueprint = Blueprint { preamble: Preamble { title: "Foo".to_string(), description: None, diff --git a/crates/aiken-project/src/blueprint/validator.rs b/crates/aiken-project/src/blueprint/validator.rs index b23b5c8e..d500706e 100644 --- a/crates/aiken-project/src/blueprint/validator.rs +++ b/crates/aiken-project/src/blueprint/validator.rs @@ -13,44 +13,44 @@ use serde; use uplc::ast::{DeBruijn, Program, Term}; #[derive(Debug, PartialEq, Clone, serde::Serialize, serde::Deserialize)] -pub struct Validator { +pub struct Validator { pub title: String, #[serde(skip_serializing_if = "Option::is_none")] pub description: Option, #[serde(skip_serializing_if = "Option::is_none")] - pub datum: Option>, + pub datum: Option, - pub redeemer: Argument, + pub redeemer: Argument, #[serde(skip_serializing_if = "Vec::is_empty")] #[serde(default)] - pub parameters: Vec>, + pub parameters: Vec, #[serde(flatten)] pub program: Program, #[serde(skip_serializing_if = "Definitions::is_empty")] #[serde(default)] - pub definitions: Definitions, + pub definitions: Definitions>, } #[derive(Debug, PartialEq, Eq, Clone, serde::Serialize, serde::Deserialize)] -pub struct Argument { +pub struct Argument { #[serde(skip_serializing_if = "Option::is_none")] pub title: Option, - pub schema: T, + pub schema: Reference, } -impl Validator> { +impl Validator { pub fn from_checked_module( modules: &CheckedModules, generator: &mut CodeGenerator, module: &CheckedModule, def: &TypedValidator, - ) -> Vec>, Error>> { + ) -> Vec> { let program = generator.generate(def).try_into().unwrap(); let is_multi_validator = def.other_fun.is_some(); @@ -85,7 +85,7 @@ impl Validator> { params: &[TypedArg], func: &TypedFunction, is_multi_validator: bool, - ) -> Result>, Error> { + ) -> Result { let mut args = func.arguments.iter().rev(); let (_, redeemer, datum) = (args.next(), args.next().unwrap(), args.next()); @@ -160,11 +160,7 @@ impl Validator> { } } -impl Validator -where - S: Clone, - R: Clone, -{ +impl Validator { pub fn apply(self, arg: &Term) -> Result { match self.parameters.split_first() { None => Err(Error::NoParametersToApply), diff --git a/crates/aiken-project/src/lib.rs b/crates/aiken-project/src/lib.rs index 914e3b7c..6c455270 100644 --- a/crates/aiken-project/src/lib.rs +++ b/crates/aiken-project/src/lib.rs @@ -12,11 +12,7 @@ pub mod pretty; pub mod script; pub mod telemetry; -use crate::blueprint::{ - definitions::Reference, - schema::{Annotated, Schema}, - Blueprint, -}; +use crate::blueprint::Blueprint; use aiken_lang::{ ast::{Definition, Function, ModuleKind, Tracing, TypedDataType, TypedFunction}, builtins, @@ -218,10 +214,7 @@ where self.compile(options) } - pub fn dump_uplc( - &self, - blueprint: &Blueprint>, - ) -> Result<(), Error> { + pub fn dump_uplc(&self, blueprint: &Blueprint) -> Result<(), Error> { let dir = self.root.join("artifacts"); self.event_listener @@ -362,8 +355,7 @@ where // Read blueprint let blueprint = File::open(self.blueprint_path()) .map_err(|_| blueprint::error::Error::InvalidOrMissingFile)?; - let blueprint: Blueprint = - serde_json::from_reader(BufReader::new(blueprint))?; + let blueprint: Blueprint = serde_json::from_reader(BufReader::new(blueprint))?; // Calculate the address let when_too_many = @@ -386,12 +378,11 @@ where &self, title: Option<&String>, param: &Term, - ) -> Result, Error> { + ) -> Result { // Read blueprint let blueprint = File::open(self.blueprint_path()) .map_err(|_| blueprint::error::Error::InvalidOrMissingFile)?; - let mut blueprint: Blueprint = - serde_json::from_reader(BufReader::new(blueprint))?; + let mut blueprint: Blueprint = serde_json::from_reader(BufReader::new(blueprint))?; // Apply parameters let when_too_many = diff --git a/crates/aiken/src/cmd/blueprint/convert.rs b/crates/aiken/src/cmd/blueprint/convert.rs index 2adeb3c5..d72a3183 100644 --- a/crates/aiken/src/cmd/blueprint/convert.rs +++ b/crates/aiken/src/cmd/blueprint/convert.rs @@ -65,7 +65,7 @@ pub fn exec( .map_err(|_| BlueprintError::InvalidOrMissingFile) .into_diagnostic()?; - let blueprint: Blueprint = + let blueprint: Blueprint = serde_json::from_reader(BufReader::new(blueprint)).into_diagnostic()?; // Perform the conversion