fix: Provide original hash for inline datum (#221)

This commit is contained in:
Santiago Carmuega 2023-02-03 18:58:39 +01:00 committed by GitHub
parent 9d8ca617ef
commit e600190107
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 216 additions and 110 deletions

View file

@ -55,11 +55,11 @@ impl<'b> MultiEraOutput<'b> {
match self {
MultiEraOutput::Byron(x) => x.amount,
MultiEraOutput::Babbage(x) => match x.deref().deref() {
babbage::TransactionOutput::Legacy(x) => match x.amount {
babbage::MintedTransactionOutput::Legacy(x) => match x.amount {
babbage::Value::Coin(c) => c,
babbage::Value::Multiasset(c, _) => c,
},
babbage::TransactionOutput::PostAlonzo(x) => match x.value {
babbage::MintedTransactionOutput::PostAlonzo(x) => match x.value {
babbage::Value::Coin(c) => c,
babbage::Value::Multiasset(c, _) => c,
},
@ -80,11 +80,11 @@ impl<'b> MultiEraOutput<'b> {
match self {
MultiEraOutput::Byron(_) => vec![],
MultiEraOutput::Babbage(x) => match x.deref().deref() {
babbage::TransactionOutput::Legacy(x) => match &x.amount {
babbage::MintedTransactionOutput::Legacy(x) => match &x.amount {
babbage::Value::Coin(_) => vec![],
babbage::Value::Multiasset(_, x) => collect_multiassets(x),
},
babbage::TransactionOutput::PostAlonzo(x) => match &x.value {
babbage::MintedTransactionOutput::PostAlonzo(x) => match &x.value {
babbage::Value::Coin(_) => vec![],
babbage::Value::Multiasset(_, x) => collect_multiassets(x),
},

View file

@ -127,6 +127,12 @@ impl OriginalHash<32> for KeepRaw<'_, babbage::TransactionBody> {
}
}
impl OriginalHash<32> for KeepRaw<'_, babbage::MintedTransactionBody<'_>> {
fn original_hash(&self) -> pallas_crypto::hash::Hash<32> {
Hasher::<256>::hash(self.raw_cbor())
}
}
impl ComputeHash<32> for babbage::DatumOption {
fn compute_hash(&self) -> Hash<32> {
match self {
@ -138,12 +144,13 @@ impl ComputeHash<32> for babbage::DatumOption {
#[cfg(test)]
mod tests {
use crate::{Era, MultiEraTx};
use crate::{Era, MultiEraBlock, MultiEraTx};
use super::{ComputeHash, OriginalHash};
use pallas_codec::utils::Int;
use pallas_codec::{minicbor, utils::Bytes};
use pallas_crypto::hash::Hash;
use pallas_primitives::babbage::MintedDatumOption;
use pallas_primitives::{alonzo, babbage, byron};
use std::str::FromStr;
@ -328,7 +335,7 @@ mod tests {
}
#[test]
fn test_datum_hash_respects_original_cbor() {
fn test_witness_datum_hash_respects_original_cbor() {
let expected = [
"54ad3c112d58e8946480e21d6a35b2a215d1a9a8f540c13714ded86e4b0b6aea",
"831a557bc2948e1b8c9f5e8e594d62299abff4eb1a11dc19da38bfaf9f2da407",
@ -347,4 +354,19 @@ mod tests {
assert_eq!(datum.original_hash().to_string(), expected_hash);
}
}
#[test]
fn test_inline_datum_hash_respects_original_cbor() {
let expected = "7607117edd3189347a2898defbb9042e9ea3bf094466718cdaf65f7f9bfeefdb";
let tx_hex = include_str!("../../test_data/babbage2.tx");
let tx_bytes = hex::decode(tx_hex).unwrap();
let tx = MultiEraTx::decode(Era::Babbage, &tx_bytes).unwrap();
for output in tx.outputs() {
if let Some(MintedDatumOption::Data(datum)) = output.datum() {
assert_eq!(datum.original_hash().to_string(), expected);
}
}
}
}

View file

@ -80,7 +80,7 @@ pub enum MultiEraTx<'b> {
#[non_exhaustive]
pub enum MultiEraOutput<'b> {
AlonzoCompatible(Box<Cow<'b, alonzo::TransactionOutput>>),
Babbage(Box<Cow<'b, babbage::TransactionOutput>>),
Babbage(Box<Cow<'b, babbage::MintedTransactionOutput<'b>>>),
Byron(Box<Cow<'b, byron::TxOut>>),
}

View file

@ -15,18 +15,20 @@ impl<'b> MultiEraOutput<'b> {
Self::AlonzoCompatible(Box::new(Cow::Borrowed(output)))
}
pub fn from_babbage(output: &'b babbage::TransactionOutput) -> Self {
pub fn from_babbage(output: &'b babbage::MintedTransactionOutput<'b>) -> Self {
Self::Babbage(Box::new(Cow::Borrowed(output)))
}
pub fn datum(&self) -> Option<babbage::DatumOption> {
pub fn datum(&self) -> Option<babbage::MintedDatumOption> {
match self {
MultiEraOutput::AlonzoCompatible(x) => x.datum_hash.map(babbage::DatumOption::Hash),
MultiEraOutput::AlonzoCompatible(x) => {
x.datum_hash.map(babbage::MintedDatumOption::Hash)
}
MultiEraOutput::Babbage(x) => match x.deref().deref() {
babbage::TransactionOutput::Legacy(x) => {
x.datum_hash.map(babbage::DatumOption::Hash)
babbage::MintedTransactionOutput::Legacy(x) => {
x.datum_hash.map(babbage::MintedDatumOption::Hash)
}
babbage::TransactionOutput::PostAlonzo(x) => x.datum_option.clone(),
babbage::MintedTransactionOutput::PostAlonzo(x) => x.datum_option.clone(),
},
_ => None,
}
@ -35,8 +37,8 @@ impl<'b> MultiEraOutput<'b> {
pub fn script_ref(&self) -> Option<&babbage::ScriptRef> {
match &self {
MultiEraOutput::Babbage(x) => match x.deref().deref() {
babbage::TransactionOutput::Legacy(_) => None,
babbage::TransactionOutput::PostAlonzo(x) => x.script_ref.as_ref(),
babbage::MintedTransactionOutput::Legacy(_) => None,
babbage::MintedTransactionOutput::PostAlonzo(x) => x.script_ref.as_ref(),
},
_ => None,
}
@ -46,8 +48,8 @@ impl<'b> MultiEraOutput<'b> {
match self {
MultiEraOutput::AlonzoCompatible(x) => Address::from_bytes(&x.address),
MultiEraOutput::Babbage(x) => match x.deref().deref() {
babbage::TransactionOutput::Legacy(x) => Address::from_bytes(&x.address),
babbage::TransactionOutput::PostAlonzo(x) => Address::from_bytes(&x.address),
babbage::MintedTransactionOutput::Legacy(x) => Address::from_bytes(&x.address),
babbage::MintedTransactionOutput::PostAlonzo(x) => Address::from_bytes(&x.address),
},
MultiEraOutput::Byron(x) => {
Ok(ByronAddress::new(&x.address.payload.0, x.address.crc).into())
@ -55,7 +57,7 @@ impl<'b> MultiEraOutput<'b> {
}
}
pub fn as_babbage(&self) -> Option<&babbage::TransactionOutput> {
pub fn as_babbage(&self) -> Option<&babbage::MintedTransactionOutput> {
match self {
MultiEraOutput::AlonzoCompatible(_) => None,
MultiEraOutput::Babbage(x) => Some(x),