Skip to content

Commit

Permalink
Generated wrapped schemas for multi-validators' redeemers
Browse files Browse the repository at this point in the history
  • Loading branch information
KtorZ committed Mar 17, 2023
1 parent ae03d0b commit 4f4b1cf
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 19 deletions.
6 changes: 1 addition & 5 deletions crates/aiken-lang/src/format.rs
Expand Up @@ -521,11 +521,7 @@ impl<'comments> Formatter<'comments> {
))
.nest(INDENT)
.group()
.append(if other_fun.is_some() {
line()
} else {
nil()
})
.append(if other_fun.is_some() { line() } else { nil() })
.append(
other_fun
.as_ref()
Expand Down
4 changes: 2 additions & 2 deletions crates/aiken-lang/src/parser.rs
Expand Up @@ -308,8 +308,8 @@ pub fn validator_parser() -> impl Parser<Token, ast::UntypedDefinition, Error =
ast::UntypedDefinition::Validator(ast::Validator {
doc: None,
fun: functions
.next()
.expect("unwrapping safe because there's 'at_least(1)' function"),
.next()
.expect("unwrapping safe because there's 'at_least(1)' function"),
other_fun: functions.next(),
location: Span {
start: span.start,
Expand Down
28 changes: 28 additions & 0 deletions crates/aiken-project/src/blueprint/schema.rs
Expand Up @@ -2,6 +2,7 @@ use crate::blueprint::definitions::{Definitions, Reference};
use crate::CheckedModule;
use aiken_lang::{
ast::{DataType, Definition, TypedDefinition},
builtins::wrapped_redeemer,
tipo::{pretty, Type, TypeVar},
};
use owo_colors::{OwoColorize, Stream::Stdout};
Expand All @@ -12,6 +13,9 @@ use serde::{
use std::ops::Deref;
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)]
pub struct Annotated<T> {
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down Expand Up @@ -71,6 +75,30 @@ impl<T> From<T> for Annotated<T> {
}

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(
modules: &HashMap<String, CheckedModule>,
type_info: &Type,
Expand Down
21 changes: 9 additions & 12 deletions crates/aiken-project/src/blueprint/validator.rs
@@ -1,5 +1,3 @@
use std::collections::HashMap;

use super::{
definitions::{Definitions, Reference},
error::Error,
Expand All @@ -8,7 +6,6 @@ use super::{
use crate::module::{CheckedModule, CheckedModules};
use aiken_lang::{
ast::{TypedArg, TypedFunction, TypedValidator},
builtins::wrapped_redeemer,
uplc::CodeGenerator,
};
use miette::NamedSource;
Expand Down Expand Up @@ -146,15 +143,15 @@ impl Validator<Reference, Annotated<Schema>> {
module.code.clone(),
),
})
.map(|schema| match datum {
Some(..) if is_multi_validator => {
let wrap_redeemer_type = wrapped_redeemer(redeemer.tipo);

definitions.register(&wrap_redeemer_type, &HashMap::new(), todo!());
}
_ => Argument {
title: Some(redeemer.arg_name.get_label()),
schema,
.map(|schema| Argument {
title: Some(redeemer.arg_name.get_label()),
schema: match datum {
Some(..) if is_multi_validator => Annotated::as_wrapped_redeemer(
&mut definitions,
schema,
redeemer.tipo.clone(),
),
_ => schema,
},
})?,
program: program.clone(),
Expand Down

0 comments on commit 4f4b1cf

Please sign in to comment.