From 55ecc199d185aad87615a8bff83706edd23aed39 Mon Sep 17 00:00:00 2001 From: KtorZ Date: Sat, 4 Feb 2023 09:31:39 +0100 Subject: [PATCH] Add 'parameters' to blueprints for parameterized validators. Without that, we have no way to distinguish between fully applied validators and those that still require some hard-coded parameters. Next steps is to make it easier to apply parameters to those, as well as forbid the creation of addresses of validators that aren't fully qualified. --- .../aiken-project/src/blueprint/validator.rs | 57 ++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/crates/aiken-project/src/blueprint/validator.rs b/crates/aiken-project/src/blueprint/validator.rs index 6de76d15..faf7b229 100644 --- a/crates/aiken-project/src/blueprint/validator.rs +++ b/crates/aiken-project/src/blueprint/validator.rs @@ -21,6 +21,8 @@ pub struct Validator { #[serde(skip_serializing_if = "Option::is_none")] pub datum: Option>, pub redeemer: Annotated, + #[serde(skip_serializing_if = "Vec::is_empty")] + pub parameters: Vec>, #[serde(flatten)] pub program: Program, } @@ -58,12 +60,39 @@ impl Validator { assert_min_arity(validator, def, purpose.min_arity())?; let mut args = def.arguments.iter().rev(); - let (_, redeemer, datum) = (args.next(), args.next().unwrap(), args.next()); + let (_, redeemer) = (args.next(), args.next().unwrap()); + let datum = if purpose.min_arity() > 2 { + args.next() + } else { + None + }; Ok(Validator { title: validator.name.clone(), description: None, purpose, + parameters: args + .rev() + .map(|param| { + let annotation = + Annotated::from_type(modules.into(), ¶m.tipo, &HashMap::new()).map_err( + |error| Error::Schema { + error, + location: param.location, + source_code: NamedSource::new( + validator.input_path.display().to_string(), + validator.code.clone(), + ), + }, + ); + annotation.map(|mut annotation| { + annotation.title = annotation + .title + .or_else(|| Some(param.arg_name.get_label())); + annotation + }) + }) + .collect::>()?, datum: datum .map(|datum| { Annotated::from_type(modules.into(), &datum.tipo, &HashMap::new()).map_err( @@ -272,6 +301,32 @@ mod test { ); } + #[test] + fn validator_mint_parameterized() { + assert_validator( + r#" + fn mint(utxo_ref: Int, redeemer: Data, ctx: Data) { + True + } + "#, + json!({ + "title": "test_module", + "purpose": "mint", + "hash": "455f24922a520c59499fdafad95e1272fab81a99452f6b9545f95337", + "parameters": [{ + "title": "utxo_ref", + "dataType": "integer" + + }], + "redeemer": { + "title": "Data", + "description": "Any Plutus data." + }, + "compiledCode": "4d01000022253335734944526161" + }), + ); + } + #[test] fn validator_spend() { assert_validator(