Generated wrapped schemas for multi-validators' redeemers

This commit is contained in:
KtorZ 2023-03-17 16:30:30 +01:00 committed by rvcas
parent bb6fc76971
commit bc690c5410
No known key found for this signature in database
GPG Key ID: C09B64E263F7D68C
4 changed files with 40 additions and 19 deletions

View File

@ -521,11 +521,7 @@ impl<'comments> Formatter<'comments> {
)) ))
.nest(INDENT) .nest(INDENT)
.group() .group()
.append(if other_fun.is_some() { .append(if other_fun.is_some() { line() } else { nil() })
line()
} else {
nil()
})
.append( .append(
other_fun other_fun
.as_ref() .as_ref()

View File

@ -306,8 +306,8 @@ pub fn validator_parser() -> impl Parser<Token, ast::UntypedDefinition, Error =
ast::UntypedDefinition::Validator(ast::Validator { ast::UntypedDefinition::Validator(ast::Validator {
doc: None, doc: None,
fun: functions fun: functions
.next() .next()
.expect("unwrapping safe because there's 'at_least(1)' function"), .expect("unwrapping safe because there's 'at_least(1)' function"),
other_fun: functions.next(), other_fun: functions.next(),
location: Span { location: Span {
start: span.start, start: span.start,

View File

@ -2,6 +2,7 @@ use crate::blueprint::definitions::{Definitions, Reference};
use crate::CheckedModule; use crate::CheckedModule;
use aiken_lang::{ use aiken_lang::{
ast::{DataType, Definition, TypedDefinition}, ast::{DataType, Definition, TypedDefinition},
builtins::wrapped_redeemer,
tipo::{pretty, Type, TypeVar}, tipo::{pretty, Type, TypeVar},
}; };
use owo_colors::{OwoColorize, Stream::Stdout}; use owo_colors::{OwoColorize, Stream::Stdout};
@ -12,6 +13,9 @@ use serde::{
use std::ops::Deref; use std::ops::Deref;
use std::{collections::HashMap, sync::Arc}; use std::{collections::HashMap, sync::Arc};
// NOTE: Can be anything BUT 0
pub const REDEEMER_DISCRIMINANT: usize = 1;
#[derive(Debug, PartialEq, Eq, Clone, serde::Serialize, serde::Deserialize)] #[derive(Debug, PartialEq, Eq, Clone, serde::Serialize, serde::Deserialize)]
pub struct Annotated<T> { pub struct Annotated<T> {
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
@ -71,6 +75,30 @@ impl<T> From<T> for Annotated<T> {
} }
impl Annotated<Schema> { impl Annotated<Schema> {
pub fn as_wrapped_redeemer(
definitions: &mut Definitions<Annotated<Schema>>,
schema: Reference,
type_info: Arc<Type>,
) -> Reference {
definitions
.register(
&wrapped_redeemer(type_info),
&HashMap::new(),
|_| {
Ok::<_, Error>(Annotated {
title: Some("Wrapped Redeemer".to_string()),
description: Some("A redeemer wrapped in an extra constructor to make multi-validator detection possible on-chain.".to_string()),
annotated: Schema::Data(Data::AnyOf(vec![Constructor {
index: REDEEMER_DISCRIMINANT,
fields: vec![schema.into()],
}
.into()])),
})
},
)
.expect("cannot fail because Ok")
}
pub fn from_type( pub fn from_type(
modules: &HashMap<String, CheckedModule>, modules: &HashMap<String, CheckedModule>,
type_info: &Type, type_info: &Type,

View File

@ -1,5 +1,3 @@
use std::collections::HashMap;
use super::{ use super::{
definitions::{Definitions, Reference}, definitions::{Definitions, Reference},
error::Error, error::Error,
@ -8,7 +6,6 @@ use super::{
use crate::module::{CheckedModule, CheckedModules}; use crate::module::{CheckedModule, CheckedModules};
use aiken_lang::{ use aiken_lang::{
ast::{TypedArg, TypedFunction, TypedValidator}, ast::{TypedArg, TypedFunction, TypedValidator},
builtins::wrapped_redeemer,
uplc::CodeGenerator, uplc::CodeGenerator,
}; };
use miette::NamedSource; use miette::NamedSource;
@ -146,15 +143,15 @@ impl Validator<Reference, Annotated<Schema>> {
module.code.clone(), module.code.clone(),
), ),
}) })
.map(|schema| match datum { .map(|schema| Argument {
Some(..) if is_multi_validator => { title: Some(redeemer.arg_name.get_label()),
let wrap_redeemer_type = wrapped_redeemer(redeemer.tipo); schema: match datum {
Some(..) if is_multi_validator => Annotated::as_wrapped_redeemer(
definitions.register(&wrap_redeemer_type, &HashMap::new(), todo!()); &mut definitions,
} schema,
_ => Argument { redeemer.tipo.clone(),
title: Some(redeemer.arg_name.get_label()), ),
schema, _ => schema,
}, },
})?, })?,
program: program.clone(), program: program.clone(),