Skip to content

Commit

Permalink
refactor: support roundtrip encoding for script data hash components (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
scarmuega authored Oct 22, 2024
1 parent 969d561 commit 537cd45
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 119 deletions.
33 changes: 16 additions & 17 deletions pallas-applying/src/alonzo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,24 +566,22 @@ fn mk_plutus_script_redeemer_pointers(
}
}
}
match mint {
Some(minted_value) => {
let sorted_policies: Vec<PolicyId> = sort_policies(minted_value);
for (index, policy) in sorted_policies.iter().enumerate() {
for plutus_script in plutus_scripts.iter() {
let hashed_script: PolicyId =
compute_plutus_v1_script_hash(plutus_script);
if *policy == hashed_script {
res.push(RedeemerPointer {
tag: RedeemerTag::Mint,
index: index as u32,
})
}

if let Some(minted_value) = mint {
let sorted_policies: Vec<PolicyId> = sort_policies(minted_value);
for (index, policy) in sorted_policies.iter().enumerate() {
for plutus_script in plutus_scripts.iter() {
let hashed_script: PolicyId = compute_plutus_v1_script_hash(plutus_script);
if *policy == hashed_script {
res.push(RedeemerPointer {
tag: RedeemerTag::Mint,
index: index as u32,
})
}
}
}
None => (),
}

res
}
None => Vec::new(),
Expand Down Expand Up @@ -727,10 +725,11 @@ fn check_vkey_input_wits(
let tx_hash: &Vec<u8> = &Vec::from(mtx.transaction_body.original_hash().as_ref());
let mut inputs_and_collaterals: Vec<TransactionInput> = Vec::new();
inputs_and_collaterals.extend(tx_body.inputs.clone());
match &tx_body.collateral {
Some(collaterals) => inputs_and_collaterals.extend(collaterals.clone()),
None => (),

if let Some(collaterals) = &tx_body.collateral {
inputs_and_collaterals.extend(collaterals.clone());
}

for input in inputs_and_collaterals.iter() {
match utxos.get(&MultiEraInput::from_alonzo_compatible(input)) {
Some(multi_era_output) => {
Expand Down
6 changes: 4 additions & 2 deletions pallas-applying/tests/alonzo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ use std::borrow::Cow;

#[cfg(test)]
mod alonzo_tests {
use pallas_primitives::MaybeIndefArray;

use super::*;

#[test]
Expand Down Expand Up @@ -2046,7 +2048,7 @@ mod alonzo_tests {
);
let mut tx_wits: MintedWitnessSet = mtx.transaction_witness_set.unwrap().clone();
let old_datum: KeepRaw<PlutusData> = tx_wits.plutus_data.unwrap().pop().unwrap();
let new_datum: PlutusData = PlutusData::Array(Vec::new());
let new_datum: PlutusData = PlutusData::Array(MaybeIndefArray::Def(Vec::new()));
let mut new_datum_buf: Vec<u8> = Vec::new();
let _ = encode(new_datum, &mut new_datum_buf);
let keep_raw_new_datum: KeepRaw<PlutusData> =
Expand Down Expand Up @@ -2171,7 +2173,7 @@ mod alonzo_tests {
let new_redeemer: Redeemer = Redeemer {
tag: RedeemerTag::Spend,
index: 15,
data: PlutusData::Array(Vec::new()),
data: PlutusData::Array(MaybeIndefArray::Def(Vec::new())),
ex_units: ExUnits { mem: 0, steps: 0 },
};
tx_wits.redeemer = Some(vec![old_redeemer, new_redeemer]);
Expand Down
6 changes: 4 additions & 2 deletions pallas-applying/tests/babbage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ use std::borrow::Cow;

#[cfg(test)]
mod babbage_tests {
use pallas_primitives::MaybeIndefArray;

use super::*;

#[test]
Expand Down Expand Up @@ -2150,7 +2152,7 @@ mod babbage_tests {
add_collateral_babbage(&mtx.transaction_body, &mut utxos, collateral_info);
let mut tx_wits: MintedWitnessSet = mtx.transaction_witness_set.unwrap().clone();
let old_datum: KeepRaw<PlutusData> = tx_wits.plutus_data.unwrap().pop().unwrap();
let new_datum: PlutusData = PlutusData::Array(Vec::new());
let new_datum: PlutusData = PlutusData::Array(MaybeIndefArray::Def(Vec::new()));
let mut new_datum_buf: Vec<u8> = Vec::new();
let _ = encode(new_datum, &mut new_datum_buf);
let keep_raw_new_datum: KeepRaw<PlutusData> =
Expand Down Expand Up @@ -2245,7 +2247,7 @@ mod babbage_tests {
let new_redeemer: Redeemer = Redeemer {
tag: RedeemerTag::Spend,
index: 15,
data: PlutusData::Array(Vec::new()),
data: PlutusData::Array(MaybeIndefArray::Def(Vec::new())),
ex_units: ExUnits { mem: 0, steps: 0 },
};
tx_wits.redeemer = Some(vec![old_redeemer, new_redeemer]);
Expand Down
47 changes: 11 additions & 36 deletions pallas-primitives/src/conway/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
//! Handcrafted, idiomatic rust artifacts based on based on the [Conway CDDL](https://github.com/IntersectMBO/cardano-ledger/blob/master/eras/conway/impl/cddl-files/conway.cddl) file in IntersectMBO repo.

use serde::{Deserialize, Serialize};
use std::ops::Deref;

use pallas_codec::minicbor::{self, decode::Error, Decode, Encode};
use pallas_codec::minicbor::{self, Decode, Encode};
use pallas_codec::utils::CborWrap;

pub use crate::{
Expand Down Expand Up @@ -1278,53 +1277,26 @@ pub struct RedeemersValue {
pub ex_units: ExUnits,
}

// TODO: Redeemers needs to be KeepRaw because of script data hash
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
#[serde(transparent)]
pub struct Redeemers(NonEmptyKeyValuePairs<RedeemersKey, RedeemersValue>);

impl Deref for Redeemers {
type Target = NonEmptyKeyValuePairs<RedeemersKey, RedeemersValue>;

fn deref(&self) -> &Self::Target {
&self.0
}
pub enum Redeemers {
List(MaybeIndefArray<Redeemer>),
Map(NonEmptyKeyValuePairs<RedeemersKey, RedeemersValue>),
}

impl From<NonEmptyKeyValuePairs<RedeemersKey, RedeemersValue>> for Redeemers {
fn from(value: NonEmptyKeyValuePairs<RedeemersKey, RedeemersValue>) -> Self {
Redeemers(value)
Redeemers::Map(value)
}
}

impl<'b, C> minicbor::Decode<'b, C> for Redeemers {
fn decode(d: &mut minicbor::Decoder<'b>, ctx: &mut C) -> Result<Self, minicbor::decode::Error> {
match d.datatype()? {
minicbor::data::Type::Array | minicbor::data::Type::ArrayIndef => {
let redeemers: Vec<Redeemer> = d.decode_with(ctx)?;

let kvs = redeemers
.into_iter()
.map(|x| {
(
RedeemersKey {
tag: x.tag,
index: x.index,
},
RedeemersValue {
data: x.data,
ex_units: x.ex_units,
},
)
})
.collect::<Vec<_>>()
.try_into()
.map_err(|_| Error::message("decoding empty redeemers"))?;

Ok(Self(kvs))
Ok(Self::List(d.decode_with(ctx)?))
}
minicbor::data::Type::Map | minicbor::data::Type::MapIndef => {
Ok(Self(d.decode_with(ctx)?))
Ok(Self::Map(d.decode_with(ctx)?))
}
_ => Err(minicbor::decode::Error::message(
"invalid type for redeemers struct",
Expand All @@ -1339,7 +1311,10 @@ impl<C> minicbor::Encode<C> for Redeemers {
e: &mut minicbor::Encoder<W>,
ctx: &mut C,
) -> Result<(), minicbor::encode::Error<W::Error>> {
e.encode_with(&self.0, ctx)?;
match self {
Self::List(x) => e.encode_with(x, ctx)?,
Self::Map(x) => e.encode_with(x, ctx)?,
};

Ok(())
}
Expand Down
58 changes: 14 additions & 44 deletions pallas-primitives/src/plutus_data.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use crate::KeyValuePairs;
use pallas_codec::minicbor::{
self,
data::{IanaTag, Tag},
Encode,
};
use pallas_codec::utils::Int;
use pallas_codec::{
minicbor::{
self,
data::{IanaTag, Tag},
Encode,
},
utils::MaybeIndefArray,
};
use serde::{Deserialize, Serialize};
use std::{fmt, ops::Deref};

Expand All @@ -14,7 +17,7 @@ pub enum PlutusData {
Map(KeyValuePairs<PlutusData, PlutusData>),
BigInt(BigInt),
BoundedBytes(BoundedBytes),
Array(Vec<PlutusData>),
Array(MaybeIndefArray<PlutusData>),
}

impl<'b, C> minicbor::decode::Decode<'b, C> for PlutusData {
Expand Down Expand Up @@ -96,11 +99,7 @@ impl<C> minicbor::encode::Encode<C> for PlutusData {
e.encode_with(a, ctx)?;
}
Self::Array(a) => {
// we use definite array for empty array or indef array otherwise to match
// haskell implementation https://github.com/input-output-hk/plutus/blob/9538fc9829426b2ecb0628d352e2d7af96ec8204/plutus-core/plutus-core/src/PlutusCore/Data.hs#L153
// default encoder for a list:
// https://github.com/well-typed/cborg/blob/4bdc818a1f0b35f38bc118a87944630043b58384/serialise/src/Codec/Serialise/Class.hs#L181
encode_list(a, e, ctx)?;
e.encode_with(a, ctx)?;
}
};

Expand Down Expand Up @@ -182,7 +181,7 @@ impl<C> minicbor::encode::Encode<C> for BigInt {
pub struct Constr<A> {
pub tag: u64,
pub any_constructor: Option<u64>,
pub fields: Vec<A>,
pub fields: MaybeIndefArray<A>,
}

impl<'b, C, A> minicbor::decode::Decode<'b, C> for Constr<A>
Expand Down Expand Up @@ -227,22 +226,12 @@ where

match self.tag {
102 => {
// definite length array here
// https://github.com/input-output-hk/plutus/blob/9538fc9829426b2ecb0628d352e2d7af96ec8204/plutus-core/plutus-core/src/PlutusCore/Data.hs#L152
e.array(2)?;
e.encode_with(self.any_constructor.unwrap_or_default(), ctx)?;

// we use definite array for empty array or indef array otherwise to match
// haskell implementation https://github.com/input-output-hk/plutus/blob/9538fc9829426b2ecb0628d352e2d7af96ec8204/plutus-core/plutus-core/src/PlutusCore/Data.hs#L144
// default encoder for a list:
// https://github.com/well-typed/cborg/blob/4bdc818a1f0b35f38bc118a87944630043b58384/serialise/src/Codec/Serialise/Class.hs#L181
encode_list(&self.fields, e, ctx)?;
let x = (self.any_constructor.unwrap_or_default(), &self.fields);
e.encode_with(x, ctx)?;
Ok(())
}
_ => {
// we use definite array for empty array or indef array otherwise to match
// haskell implementation. See above reference.
encode_list(&self.fields, e, ctx)?;
e.encode_with(&self.fields, ctx)?;
Ok(())
}
}
Expand Down Expand Up @@ -332,22 +321,3 @@ impl<'b, C> minicbor::decode::Decode<'b, C> for BoundedBytes {
Ok(BoundedBytes::from(res))
}
}

fn encode_list<C, W: minicbor::encode::Write, A: minicbor::encode::Encode<C>>(
a: &Vec<A>,
e: &mut minicbor::Encoder<W>,
ctx: &mut C,
) -> Result<(), minicbor::encode::Error<W::Error>> {
// Mimics default haskell list encoding from cborg:
// We use indef array for non-empty arrays but definite 0-length array for empty
if a.is_empty() {
e.array(0)?;
} else {
e.begin_array()?;
for v in a {
e.encode_with(v, ctx)?;
}
e.end()?;
}
Ok(())
}
14 changes: 7 additions & 7 deletions pallas-traverse/src/hashes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ mod tests {
use crate::{Era, MultiEraTx};

use super::{ComputeHash, OriginalHash};
use pallas_codec::utils::Int;
use pallas_codec::utils::{Int, MaybeIndefArray};
use pallas_codec::{minicbor, utils::Bytes};
use pallas_crypto::hash::Hash;
use pallas_crypto::key::ed25519::PublicKey;
Expand Down Expand Up @@ -283,26 +283,26 @@ mod tests {
let pd = alonzo::PlutusData::Constr(alonzo::Constr::<alonzo::PlutusData> {
tag: 1280,
any_constructor: None,
fields: vec![
fields: MaybeIndefArray::Indef(vec![
alonzo::PlutusData::BigInt(alonzo::BigInt::Int(Int::from(4))),
alonzo::PlutusData::Constr(alonzo::Constr::<alonzo::PlutusData> {
tag: 124,
any_constructor: None,
fields: vec![
fields: MaybeIndefArray::Indef(vec![
alonzo::PlutusData::BigInt(alonzo::BigInt::Int(Int::from(-4))),
alonzo::PlutusData::Constr(alonzo::Constr::<alonzo::PlutusData> {
tag: 102,
any_constructor: Some(453),
fields: vec![
fields: MaybeIndefArray::Indef(vec![
alonzo::PlutusData::BigInt(alonzo::BigInt::Int(Int::from(2))),
alonzo::PlutusData::BigInt(alonzo::BigInt::Int(Int::from(3434))),
],
]),
}),
alonzo::PlutusData::BigInt(alonzo::BigInt::Int(Int::from(-11828293))),
],
]),
}),
alonzo::PlutusData::BigInt(alonzo::BigInt::Int(Int::from(11828293))),
],
]),
});

// if you need to try this out in the cardano-cli, uncomment this line to see
Expand Down
13 changes: 13 additions & 0 deletions pallas-traverse/src/redeemers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,17 @@ impl<'b> MultiEraRedeemer<'b> {
Box::new(Cow::Borrowed(redeemers_val)),
)
}

pub fn from_conway_deprecated(redeemer: &'b conway::Redeemer) -> Self {
Self::Conway(
Box::new(Cow::Owned(conway::RedeemersKey {
tag: redeemer.tag,
index: redeemer.index,
})),
Box::new(Cow::Owned(conway::RedeemersValue {
data: redeemer.data.clone(),
ex_units: redeemer.ex_units,
})),
)
}
}
20 changes: 12 additions & 8 deletions pallas-traverse/src/witnesses.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use pallas_codec::utils::KeepRaw;
use pallas_primitives::{
alonzo::{self, BootstrapWitness, NativeScript, VKeyWitness},
PlutusData, PlutusScript,
conway, PlutusData, PlutusScript,
};

use crate::{MultiEraRedeemer, MultiEraTx};
Expand Down Expand Up @@ -144,13 +144,17 @@ impl<'b> MultiEraTx<'b> {
.flat_map(|x| x.iter())
.map(MultiEraRedeemer::from_alonzo_compatible)
.collect(),
Self::Conway(x) => x
.transaction_witness_set
.redeemer
.iter()
.flat_map(|x| x.iter())
.map(|(k, v)| MultiEraRedeemer::from_conway(k, v))
.collect(),
Self::Conway(x) => match x.transaction_witness_set.redeemer.as_deref() {
Some(conway::Redeemers::Map(x)) => x
.iter()
.map(|(k, v)| MultiEraRedeemer::from_conway(k, v))
.collect(),
Some(conway::Redeemers::List(x)) => x
.iter()
.map(MultiEraRedeemer::from_conway_deprecated)
.collect(),
_ => vec![],
},
}
}

Expand Down
Loading

0 comments on commit 537cd45

Please sign in to comment.