fixed bugs in conversion to plutus data; little refactoring

This commit is contained in:
alessandrokonrad 2022-09-11 12:48:18 +02:00 committed by rvcas
parent f0d17897ab
commit ec63bd2365
No known key found for this signature in database
GPG Key ID: C09B64E263F7D68C
1 changed files with 70 additions and 156 deletions

View File

@ -84,6 +84,22 @@ pub fn get_tx_in_info_old(resolved_inputs: &[ResolvedInput]) -> anyhow::Result<V
// --------------- // ---------------
fn wrap_with_constr(tag: u64, data: PlutusData) -> PlutusData {
PlutusData::Constr(Constr {
tag,
any_constructor: None,
fields: MaybeIndefArray::Indef(vec![data]),
})
}
fn empty_constr(tag: u64) -> PlutusData {
PlutusData::Constr(Constr {
tag,
any_constructor: None,
fields: MaybeIndefArray::Indef(vec![]),
})
}
pub trait ToPlutusData { pub trait ToPlutusData {
fn to_plutus_data(&self) -> PlutusData; fn to_plutus_data(&self) -> PlutusData;
} }
@ -97,15 +113,7 @@ impl ToPlutusData for Address {
any_constructor: None, any_constructor: None,
fields: MaybeIndefArray::Indef(vec![ fields: MaybeIndefArray::Indef(vec![
//addressCredential //addressCredential
PlutusData::Constr(Constr { wrap_with_constr(0, byron_address.decode().unwrap().root.to_plutus_data()),
tag: 0,
any_constructor: None,
fields: MaybeIndefArray::Indef(vec![byron_address
.decode()
.unwrap()
.root
.to_plutus_data()]),
}),
//addressStakeCredential //addressStakeCredential
None::<StakeCredential>.to_plutus_data(), None::<StakeCredential>.to_plutus_data(),
]), ]),
@ -116,16 +124,12 @@ impl ToPlutusData for Address {
let stake_part = shelley_address.delegation(); let stake_part = shelley_address.delegation();
let payment_part_plutus_data = match payment_part { let payment_part_plutus_data = match payment_part {
ShelleyPaymentPart::Key(payment_keyhash) => PlutusData::Constr(Constr { ShelleyPaymentPart::Key(payment_keyhash) => {
tag: 0, wrap_with_constr(0, payment_keyhash.to_plutus_data())
any_constructor: None, }
fields: MaybeIndefArray::Indef(vec![payment_keyhash.to_plutus_data()]), ShelleyPaymentPart::Script(script_hash) => {
}), wrap_with_constr(1, script_hash.to_plutus_data())
ShelleyPaymentPart::Script(script_hash) => PlutusData::Constr(Constr { }
tag: 1,
any_constructor: None,
fields: MaybeIndefArray::Indef(vec![script_hash.to_plutus_data()]),
}),
}; };
let stake_part_plutus_data = match stake_part { let stake_part_plutus_data = match stake_part {
@ -168,7 +172,7 @@ impl ToPlutusData for TransactionInput {
tag: 0, tag: 0,
any_constructor: None, any_constructor: None,
fields: MaybeIndefArray::Indef(vec![ fields: MaybeIndefArray::Indef(vec![
self.transaction_id.to_plutus_data(), wrap_with_constr(0, self.transaction_id.to_plutus_data()),
PlutusData::BigInt(BigInt::Int(self.index.into())), PlutusData::BigInt(BigInt::Int(self.index.into())),
]), ]),
}) })
@ -213,29 +217,21 @@ impl<A: ToPlutusData> ToPlutusData for Option<A> {
any_constructor: None, any_constructor: None,
fields: MaybeIndefArray::Indef(vec![]), fields: MaybeIndefArray::Indef(vec![]),
}), }),
Some(data) => PlutusData::Constr(Constr { Some(data) => wrap_with_constr(0, data.to_plutus_data()),
tag: 0,
any_constructor: None,
fields: MaybeIndefArray::Indef(vec![data.to_plutus_data()]),
}),
} }
} }
} }
impl ToPlutusData for DatumOption { // Does this here surely overwrite Option from above for DatumOption?
impl ToPlutusData for Option<DatumOption> {
// NoOutputDatum = 0 | OutputDatumHash = 1 | OutputDatum = 2
fn to_plutus_data(&self) -> PlutusData { fn to_plutus_data(&self) -> PlutusData {
match self { match self {
// tag : 0 is NoOutputDatum. Determined after unwrapping Option which is wrapped around DatumOption None => empty_constr(0),
DatumOption::Hash(hash) => PlutusData::Constr(Constr { Some(option) => match option {
tag: 1, DatumOption::Hash(hash) => wrap_with_constr(1, hash.to_plutus_data()),
any_constructor: None, DatumOption::Data(data) => wrap_with_constr(2, data.0.clone()),
fields: MaybeIndefArray::Indef(vec![hash.to_plutus_data()]), },
}),
DatumOption::Data(data) => PlutusData::Constr(Constr {
tag: 2,
any_constructor: None,
fields: MaybeIndefArray::Indef(vec![data.0.clone()]), // does data need an extra wrapper constructor?
}),
} }
} }
} }
@ -347,15 +343,7 @@ impl ToPlutusData for TransactionOutput {
post_alonzo_output.value.to_plutus_data(), post_alonzo_output.value.to_plutus_data(),
// DatumOption needs to be handled differently a bit. In Haskell it's NoOutputDatum | OutputDatumHash DatumHash | OutputDatum Datum // DatumOption needs to be handled differently a bit. In Haskell it's NoOutputDatum | OutputDatumHash DatumHash | OutputDatum Datum
// So we unwrap first to check if it's someting. If it is then turn the unwrapped data to PlutusData, otherwise have None and turn that into PlutusData // So we unwrap first to check if it's someting. If it is then turn the unwrapped data to PlutusData, otherwise have None and turn that into PlutusData
if post_alonzo_output.datum_option.is_some() { post_alonzo_output.datum_option.to_plutus_data(),
post_alonzo_output
.datum_option
.clone()
.unwrap()
.to_plutus_data()
} else {
None::<DatumOption>.to_plutus_data()
},
post_alonzo_output.script_ref.to_plutus_data(), post_alonzo_output.script_ref.to_plutus_data(),
]), ]),
}), }),
@ -369,24 +357,12 @@ impl ToPlutusData for StakeCredential {
// So a StakeCredential for a Pointer address needs to be converted separately // So a StakeCredential for a Pointer address needs to be converted separately
fn to_plutus_data(&self) -> PlutusData { fn to_plutus_data(&self) -> PlutusData {
match self { match self {
StakeCredential::AddrKeyhash(addr_keyhas) => PlutusData::Constr(Constr { StakeCredential::AddrKeyhash(addr_keyhas) => {
tag: 0, wrap_with_constr(0, wrap_with_constr(0, addr_keyhas.to_plutus_data()))
any_constructor: None, }
fields: MaybeIndefArray::Indef(vec![PlutusData::Constr(Constr { StakeCredential::Scripthash(script_hash) => {
tag: 0, wrap_with_constr(0, wrap_with_constr(1, script_hash.to_plutus_data()))
any_constructor: None, }
fields: MaybeIndefArray::Indef(vec![addr_keyhas.to_plutus_data()]),
})]),
}),
StakeCredential::Scripthash(script_hash) => PlutusData::Constr(Constr {
tag: 0,
any_constructor: None,
fields: MaybeIndefArray::Indef(vec![PlutusData::Constr(Constr {
tag: 1,
any_constructor: None,
fields: MaybeIndefArray::Indef(vec![script_hash.to_plutus_data()]),
})]),
}),
} }
} }
} }
@ -483,13 +459,7 @@ impl ToPlutusData for TimeRange {
any_constructor: None, any_constructor: None,
fields: MaybeIndefArray::Indef(vec![ fields: MaybeIndefArray::Indef(vec![
// Finite // Finite
PlutusData::Constr(Constr { wrap_with_constr(1, lower_bound.to_plutus_data()),
tag: 1,
any_constructor: None,
fields: MaybeIndefArray::Indef(vec![
lower_bound.to_plutus_data()
]),
}),
// Closure // Closure
true.to_plutus_data(), true.to_plutus_data(),
]), ]),
@ -499,11 +469,7 @@ impl ToPlutusData for TimeRange {
any_constructor: None, any_constructor: None,
fields: MaybeIndefArray::Indef(vec![ fields: MaybeIndefArray::Indef(vec![
// PosInf // PosInf
PlutusData::Constr(Constr { empty_constr(2),
tag: 2,
any_constructor: None,
fields: MaybeIndefArray::Indef(vec![]),
}),
// Closure // Closure
true.to_plutus_data(), true.to_plutus_data(),
]), ]),
@ -525,11 +491,7 @@ impl ToPlutusData for TimeRange {
any_constructor: None, any_constructor: None,
fields: MaybeIndefArray::Indef(vec![ fields: MaybeIndefArray::Indef(vec![
// NegInf // NegInf
PlutusData::Constr(Constr { empty_constr(0),
tag: 0,
any_constructor: None,
fields: MaybeIndefArray::Indef(vec![]),
}),
// Closure // Closure
true.to_plutus_data(), true.to_plutus_data(),
]), ]),
@ -539,13 +501,7 @@ impl ToPlutusData for TimeRange {
any_constructor: None, any_constructor: None,
fields: MaybeIndefArray::Indef(vec![ fields: MaybeIndefArray::Indef(vec![
// Finite // Finite
PlutusData::Constr(Constr { wrap_with_constr(1, upper_bound.to_plutus_data()),
tag: 1,
any_constructor: None,
fields: MaybeIndefArray::Indef(vec![
upper_bound.to_plutus_data()
]),
}),
// Closure // Closure
true.to_plutus_data(), true.to_plutus_data(),
]), ]),
@ -567,13 +523,7 @@ impl ToPlutusData for TimeRange {
any_constructor: None, any_constructor: None,
fields: MaybeIndefArray::Indef(vec![ fields: MaybeIndefArray::Indef(vec![
// Finite // Finite
PlutusData::Constr(Constr { wrap_with_constr(1, lower_bound.to_plutus_data()),
tag: 1,
any_constructor: None,
fields: MaybeIndefArray::Indef(vec![
lower_bound.to_plutus_data()
]),
}),
// Closure // Closure
true.to_plutus_data(), true.to_plutus_data(),
]), ]),
@ -583,13 +533,7 @@ impl ToPlutusData for TimeRange {
any_constructor: None, any_constructor: None,
fields: MaybeIndefArray::Indef(vec![ fields: MaybeIndefArray::Indef(vec![
// Finite // Finite
PlutusData::Constr(Constr { wrap_with_constr(1, upper_bound.to_plutus_data()),
tag: 1,
any_constructor: None,
fields: MaybeIndefArray::Indef(vec![
upper_bound.to_plutus_data()
]),
}),
// Closure // Closure
false.to_plutus_data(), false.to_plutus_data(),
]), ]),
@ -611,11 +555,7 @@ impl ToPlutusData for TimeRange {
any_constructor: None, any_constructor: None,
fields: MaybeIndefArray::Indef(vec![ fields: MaybeIndefArray::Indef(vec![
// NegInf // NegInf
PlutusData::Constr(Constr { empty_constr(0),
tag: 0,
any_constructor: None,
fields: MaybeIndefArray::Indef(vec![]),
}),
// Closure // Closure
true.to_plutus_data(), true.to_plutus_data(),
]), ]),
@ -625,11 +565,7 @@ impl ToPlutusData for TimeRange {
any_constructor: None, any_constructor: None,
fields: MaybeIndefArray::Indef(vec![ fields: MaybeIndefArray::Indef(vec![
// PosInf // PosInf
PlutusData::Constr(Constr { empty_constr(2),
tag: 2,
any_constructor: None,
fields: MaybeIndefArray::Indef(vec![]),
}),
// Closure // Closure
true.to_plutus_data(), true.to_plutus_data(),
]), ]),
@ -657,26 +593,12 @@ impl ToPlutusData for TxInInfo {
impl ToPlutusData for ScriptPurpose { impl ToPlutusData for ScriptPurpose {
fn to_plutus_data(&self) -> PlutusData { fn to_plutus_data(&self) -> PlutusData {
match self { match self {
ScriptPurpose::Minting(policy_id) => PlutusData::Constr(Constr { ScriptPurpose::Minting(policy_id) => wrap_with_constr(0, policy_id.to_plutus_data()),
tag: 0, ScriptPurpose::Spending(out_ref) => wrap_with_constr(1, out_ref.to_plutus_data()),
any_constructor: None, ScriptPurpose::Rewarding(stake_credential) => {
fields: MaybeIndefArray::Indef(vec![policy_id.to_plutus_data()]), wrap_with_constr(2, stake_credential.to_plutus_data())
}), }
ScriptPurpose::Spending(out_ref) => PlutusData::Constr(Constr { ScriptPurpose::Certifying(dcert) => wrap_with_constr(3, dcert.to_plutus_data()),
tag: 1,
any_constructor: None,
fields: MaybeIndefArray::Indef(vec![out_ref.to_plutus_data()]),
}),
ScriptPurpose::Rewarding(stake_credential) => PlutusData::Constr(Constr {
tag: 2,
any_constructor: None,
fields: MaybeIndefArray::Indef(vec![stake_credential.to_plutus_data()]),
}),
ScriptPurpose::Certifying(dcert) => PlutusData::Constr(Constr {
tag: 3,
any_constructor: None,
fields: MaybeIndefArray::Indef(vec![dcert.to_plutus_data()]),
}),
} }
} }
} }
@ -720,16 +642,8 @@ impl ToPlutusData for ScriptContext {
impl ToPlutusData for bool { impl ToPlutusData for bool {
fn to_plutus_data(&self) -> PlutusData { fn to_plutus_data(&self) -> PlutusData {
match self { match self {
false => PlutusData::Constr(Constr { false => empty_constr(0),
tag: 0, true => empty_constr(1),
any_constructor: None,
fields: MaybeIndefArray::Indef(vec![]),
}),
true => PlutusData::Constr(Constr {
tag: 1,
any_constructor: None,
fields: MaybeIndefArray::Indef(vec![]),
}),
} }
} }
} }
@ -881,7 +795,7 @@ fn get_script_purpose(
RedeemerTag::Cert => { RedeemerTag::Cert => {
// sort by order given in the tx (just take it as it is basically) // sort by order given in the tx (just take it as it is basically)
let cert = dcert[index as usize].clone(); let cert = dcert[index as usize].clone();
Ok(ScriptPurpose::Certifying(cert.clone())) Ok(ScriptPurpose::Certifying(cert))
} }
} }
} }
@ -985,18 +899,18 @@ fn get_script_context(
// ) -> anyhow::Result<Redeemer> { // ) -> anyhow::Result<Redeemer> {
// } // }
// fn eval_tx( fn eval_tx(
// tx_bytes: &Vec<u8>, tx_bytes: &Vec<u8>,
// utxos: &Vec<(Vec<u8>, Vec<u8>)>, utxos: &Vec<(Vec<u8>, Vec<u8>)>,
// cost_model: &Vec<u8>, cost_model: &Vec<u8>,
// zero_time: u64, zero_time: u64,
// slot_length: u64, slot_length: u64,
// ) -> anyhow::Result<bool> { ) -> anyhow::Result<bool> {
// let multi_tx = MultiEraTx::decode(Era::Babbage, &tx_bytes) let multi_tx = MultiEraTx::decode(Era::Babbage, &tx_bytes)
// .or_else(|_| MultiEraTx::decode(Era::Alonzo, &tx_bytes)) .or_else(|_| MultiEraTx::decode(Era::Alonzo, &tx_bytes))
// .or_else(|_| MultiEraTx::decode(Era::Byron, &tx_bytes))?; .or_else(|_| MultiEraTx::decode(Era::Byron, &tx_bytes))?;
// let tx = multi_tx.as_babbage().unwrap(); let tx = multi_tx.as_babbage().unwrap();
// Ok(true) Ok(true)
// } }