Rework generate_raw to avoid need to intern in prop tests

Also, this commit makes `apply_term` automatically re-intern the
  program since it isn't safe to apply any term onto a UPLC program. In
  particular, terms that introduce new let-bindings (via lambdas) will
  mess with the already generated DeBruijn indices.

  The problem doesn't occur for pure constant terms like Data. So we
  still have a safe and fast version 'apply_data' when needed.
This commit is contained in:
KtorZ
2024-03-03 19:24:35 +01:00
parent 1134b8d7d0
commit 30841fe000
9 changed files with 127 additions and 120 deletions

View File

@@ -13,9 +13,8 @@ use num_bigint::BigInt;
use ordinal::Ordinal;
use owo_colors::{OwoColorize, Stream::Stderr};
use pallas::ledger::primitives::alonzo::PlutusData;
use std::str::FromStr;
use std::{fs, path::PathBuf, process, rc::Rc};
use uplc::ast::{Constant, Data as UplcData, DeBruijn, Term};
use std::{fs, path::PathBuf, process, str::FromStr};
use uplc::ast::Data as UplcData;
/// Apply a parameter to a parameterized validator.
#[derive(clap::Args)]
@@ -68,7 +67,7 @@ pub fn exec(
.if_supports_color(Stderr, |s| s.bold()),
);
let term: Term<DeBruijn> = match &parameter {
let data: PlutusData = match &parameter {
Some(param) => {
eprintln!(
"{} inputs",
@@ -90,7 +89,7 @@ pub fn exec(
process::exit(1)
});
let data = uplc::plutus_data(&bytes)
uplc::plutus_data(&bytes)
.map_err::<Error, _>(|e| {
blueprint::error::Error::MalformedParameter {
hint: format!("Invalid Plutus data; malformed CBOR encoding: {e}"),
@@ -101,9 +100,7 @@ pub fn exec(
println!();
e.report();
process::exit(1)
});
Term::Constant(Rc::new(Constant::Data(data)))
})
}
None => p.construct_parameter_incrementally(title, ask_schema)?,
@@ -114,16 +111,13 @@ pub fn exec(
" Applying"
.if_supports_color(Stderr, |s| s.purple())
.if_supports_color(Stderr, |s| s.bold()),
match TryInto::<PlutusData>::try_into(term.clone()) {
Ok(data) => {
let padding = "\n ";
multiline(48, UplcData::to_hex(data)).join(padding)
}
Err(_) => term.to_pretty(),
{
let padding = "\n ";
multiline(48, UplcData::to_hex(data.clone())).join(padding)
}
);
let blueprint = p.apply_parameter(title, &term)?;
let blueprint = p.apply_parameter(title, &data)?;
let json = serde_json::to_string_pretty(&blueprint).unwrap();
@@ -179,16 +173,21 @@ fn ask_schema(
}
Schema::Data(Data::List(Items::Many(ref decls))) => {
eprintln!(" {}", asking(schema, "Found", &format!("a {}-tuple", decls.len())));
eprintln!(
" {}",
asking(schema, "Found", &format!("a {}-tuple", decls.len()))
);
let mut elems = vec![];
for (ix, decl) in decls.iter().enumerate() {
eprintln!(
" {} Tuple's {}{} element",
"Asking".if_supports_color(Stderr, |s| s.purple()).if_supports_color(Stderr, |s| s.bold()),
ix+1,
Ordinal::<usize>(ix+1).suffix()
"Asking"
.if_supports_color(Stderr, |s| s.purple())
.if_supports_color(Stderr, |s| s.bold()),
ix + 1,
Ordinal::<usize>(ix + 1).suffix()
);
let inner_schema = lookup_declaration(&decl.clone().into(), definitions);
elems.push(ask_schema(&inner_schema, definitions)?);
@@ -252,7 +251,9 @@ fn ask_schema(
Ok(UplcData::constr(ix.try_into().unwrap(), fields))
}
_ => unimplemented!("Hey! You've found a case that we haven't implemented yet. Yes, we've been a bit lazy on that one... If that use-case is important to you, please let us know on Discord or on Github."),
_ => unimplemented!(
"Hey! You've found a case that we haven't implemented yet. Yes, we've been a bit lazy on that one... If that use-case is important to you, please let us know on Discord or on Github."
),
}
}

View File

@@ -30,39 +30,41 @@ pub fn exec(
cbor,
}: Args,
) -> miette::Result<()> {
let mut program = if cbor {
let mut program: Program<Name> = if cbor {
let cbor_hex = std::fs::read_to_string(&script).into_diagnostic()?;
let raw_cbor = hex::decode(cbor_hex.trim()).into_diagnostic()?;
let prog = Program::<FakeNamedDeBruijn>::from_cbor(&raw_cbor, &mut Vec::new())
let program = Program::<FakeNamedDeBruijn>::from_cbor(&raw_cbor, &mut Vec::new())
.into_diagnostic()?;
prog.into()
let program: Program<NamedDeBruijn> = program.into();
Program::<Name>::try_from(program).into_diagnostic()?
} else if flat {
let bytes = std::fs::read(&script).into_diagnostic()?;
let prog = Program::<FakeNamedDeBruijn>::from_flat(&bytes).into_diagnostic()?;
let program = Program::<FakeNamedDeBruijn>::from_flat(&bytes).into_diagnostic()?;
prog.into()
let program: Program<NamedDeBruijn> = program.into();
Program::<Name>::try_from(program).into_diagnostic()?
} else {
let code = std::fs::read_to_string(&script).into_diagnostic()?;
let prog = parser::program(&code).into_diagnostic()?;
Program::<NamedDeBruijn>::try_from(prog).into_diagnostic()?
parser::program(&code).into_diagnostic()?
};
for arg in args {
let term = parser::term(&arg).into_diagnostic()?;
let term = Term::<NamedDeBruijn>::try_from(term).into_diagnostic()?;
program = program.apply_term(&term);
program = program.apply_term(&term)
}
let budget = ExBudget::default();
let program = Program::<NamedDeBruijn>::try_from(program).into_diagnostic()?;
let mut eval_result = program.eval(budget);
let cost = eval_result.cost();