diff --git a/CHANGELOG.md b/CHANGELOG.md index fcb932b1..ff5448d1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ - **aiken-project**: The `aiken.toml` file no longer supports `v1` and `v2` for the plutus version field. @rvcas - **aiken-project**: `Error::TomlLoading` now looks much better - [see](https://github.com/aiken-lang/aiken/issues/1032#issuecomment-2562122101). @rvcas +### Fixed + +- **aiken**: Fixed the panic error when using `aiken uplc decode` on cbor encoded flat bytes. @rvcas + ## v1.1.9 - 2024-12-13 ### Added diff --git a/crates/aiken/src/cmd/uplc/decode.rs b/crates/aiken/src/cmd/uplc/decode.rs index c6eb74d3..6da04226 100644 --- a/crates/aiken/src/cmd/uplc/decode.rs +++ b/crates/aiken/src/cmd/uplc/decode.rs @@ -1,4 +1,4 @@ -use miette::IntoDiagnostic; +use miette::{Context, IntoDiagnostic}; use std::{path::PathBuf, println}; use uplc::ast::{DeBruijn, Name, NamedDeBruijn, Program}; @@ -39,16 +39,14 @@ pub fn exec( std::fs::read(&input).into_diagnostic()? }; - let pretty_uplc = match from { + let program: Program = match from { Format::Name => { - let program: Program = if cbor { + if cbor { let mut flat_buffer = Vec::new(); Program::from_cbor(&bytes, &mut flat_buffer).into_diagnostic()? } else { Program::from_flat(&bytes).into_diagnostic()? - }; - - program.to_pretty() + } } Format::NamedDebruijn => { let program: Program = if cbor { @@ -58,9 +56,10 @@ pub fn exec( Program::from_flat(&bytes).into_diagnostic()? }; - let program: Program = program.try_into().unwrap(); - - program.to_pretty() + program + .try_into() + .into_diagnostic() + .context("failed to decode, maybe try `--cbor`")? } Format::Debruijn => { let program: Program = if cbor { @@ -70,13 +69,14 @@ pub fn exec( Program::from_flat(&bytes).into_diagnostic()? }; - let program: Program = program.try_into().unwrap(); - - program.to_pretty() + program + .try_into() + .into_diagnostic() + .context("failed to decode, maybe try `--cbor`")? } }; - println!("{pretty_uplc}"); + println!("{}", program.to_pretty()); Ok(()) } diff --git a/crates/uplc/src/debruijn.rs b/crates/uplc/src/debruijn.rs index 8e6d69a9..bd793d17 100644 --- a/crates/uplc/src/debruijn.rs +++ b/crates/uplc/src/debruijn.rs @@ -461,7 +461,12 @@ impl Converter { fn get_unique(&mut self, index: &DeBruijn) -> Result { for scope in self.levels.iter().rev() { - let index = Level(self.current_level.0 - index.inner()); + let index = Level( + self.current_level + .0 + .checked_sub(index.inner()) + .ok_or(Error::FreeIndex(*index))?, + ); if let Some(unique) = scope.get_right(&index) { return Ok(*unique); diff --git a/examples/gift_card/plutus.json b/examples/gift_card/plutus.json index 740ac23d..1c7f2ef7 100644 --- a/examples/gift_card/plutus.json +++ b/examples/gift_card/plutus.json @@ -6,7 +6,7 @@ "plutusVersion": "v3", "compiler": { "name": "Aiken", - "version": "v1.1.6+18054ee" + "version": "v1.1.9+2217206" }, "license": "Apache-2.0" },