Complete generation of blueprint multi-validators.

This commit is contained in:
KtorZ 2023-03-17 15:56:01 +01:00 committed by rvcas
parent fe92b27d50
commit 7e1403a3b2
No known key found for this signature in database
GPG Key ID: C09B64E263F7D68C
2 changed files with 48 additions and 24 deletions

View File

@ -61,13 +61,16 @@ impl Blueprint<Reference, Annotated<Schema>> {
let validators: Result<Vec<_>, Error> = modules let validators: Result<Vec<_>, Error> = modules
.validators() .validators()
.flat_map(|(validator, def)| { .flat_map(|(validator, def)| {
Validator::from_checked_module(modules, generator, validator, def).map( Validator::from_checked_module(modules, generator, validator, def)
|mut schema| { .into_iter()
definitions.merge(&mut schema.definitions); .map(|result| {
schema.definitions = Definitions::new(); result.map(|mut schema| {
schema definitions.merge(&mut schema.definitions);
}, schema.definitions = Definitions::new();
) schema
})
})
.collect::<Vec<_>>()
}) })
.collect(); .collect();

View File

@ -10,7 +10,7 @@ use aiken_lang::{
}; };
use miette::NamedSource; use miette::NamedSource;
use serde; use serde;
use uplc::ast::{DeBruijn, Name, Program, Term}; use uplc::ast::{DeBruijn, Program, Term};
#[derive(Debug, PartialEq, Clone, serde::Serialize, serde::Deserialize)] #[derive(Debug, PartialEq, Clone, serde::Serialize, serde::Deserialize)]
pub struct Validator<R, S> { pub struct Validator<R, S> {
@ -51,18 +51,28 @@ impl Validator<Reference, Annotated<Schema>> {
module: &CheckedModule, module: &CheckedModule,
def: &TypedValidator, def: &TypedValidator,
) -> Vec<Result<Validator<Reference, Annotated<Schema>>, Error>> { ) -> Vec<Result<Validator<Reference, Annotated<Schema>>, Error>> {
let program: Program<Name> = generator.generate(def).try_into().unwrap(); let program = generator.generate(def).try_into().unwrap();
let mut definitions = Definitions::new(); let is_multi_validator = def.other_fun.is_some();
let mut validators = vec![Validator::create_validator_blueprint( let mut validators = vec![Validator::create_validator_blueprint(
modules, def.params, def.fun, modules,
module,
&program,
&def.params,
&def.fun,
is_multi_validator,
)]; )];
if let Some(other_func) = def.other_fun { if let Some(ref other_func) = def.other_fun {
todo!() validators.push(Validator::create_validator_blueprint(
} else { modules,
todo!() module,
&program,
&def.params,
other_func,
is_multi_validator,
));
} }
validators validators
@ -70,22 +80,25 @@ impl Validator<Reference, Annotated<Schema>> {
fn create_validator_blueprint( fn create_validator_blueprint(
modules: &CheckedModules, modules: &CheckedModules,
params: Vec<TypedArg>, module: &CheckedModule,
func: TypedFunction, program: &Program<DeBruijn>,
params: &[TypedArg],
func: &TypedFunction,
is_multi_validator: bool,
) -> Result<Validator<Reference, Annotated<Schema>>, Error> { ) -> Result<Validator<Reference, Annotated<Schema>>, Error> {
let mut args = func.arguments.iter().rev(); let mut args = func.arguments.iter().rev();
let (_, redeemer, datum) = (args.next(), args.next().unwrap(), args.next()); let (_, redeemer, datum) = (args.next(), args.next().unwrap(), args.next());
let mut arguments = Vec::with_capacity(params.len() + func.arguments.len()); let mut arguments = Vec::with_capacity(params.len() + func.arguments.len());
arguments.extend(params.to_vec());
arguments.extend(params.clone());
arguments.extend(func.arguments.clone()); arguments.extend(func.arguments.clone());
let mut definitions = Definitions::new();
Ok(Validator { Ok(Validator {
title: format!("{}.{}", &module.name, &func.name), title: format!("{}.{}", &module.name, &func.name),
description: None, description: None,
parameters: def parameters: params
.params
.iter() .iter()
.map(|param| { .map(|param| {
Annotated::from_type(modules.into(), &param.tipo, &mut definitions) Annotated::from_type(modules.into(), &param.tipo, &mut definitions)
@ -131,13 +144,13 @@ impl Validator<Reference, Annotated<Schema>> {
), ),
}) })
.map(|schema| match datum { .map(|schema| match datum {
Some(..) if def.other_fun.is_some() => todo!(), Some(..) if is_multi_validator => todo!(),
_ => Argument { _ => Argument {
title: Some(redeemer.arg_name.get_label()), title: Some(redeemer.arg_name.get_label()),
schema, schema,
}, },
})?, })?,
program: generator.generate(def).try_into().unwrap(), program: program.clone(),
definitions, definitions,
}) })
} }
@ -283,7 +296,15 @@ mod test {
.next() .next()
.expect("source code did no yield any validator"); .expect("source code did no yield any validator");
let validator = Validator::from_checked_module(&modules, &mut generator, validator, def) let validators = Validator::from_checked_module(&modules, &mut generator, validator, def);
if validators.len() > 1 {
panic!("Multi-validator given to test bench. Don't do that.")
}
let validator = validators
.get(0)
.unwrap()
.expect("Failed to create validator blueprint"); .expect("Failed to create validator blueprint");
println!("{}", serde_json::to_string_pretty(&validator).unwrap()); println!("{}", serde_json::to_string_pretty(&validator).unwrap());