Skip to content

Commit

Permalink
fix(interop): map missing u5c redeemers (#490)
Browse files Browse the repository at this point in the history
  • Loading branch information
rvcas committed Jul 16, 2024
1 parent a856196 commit 4fbca7b
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 13 deletions.
1 change: 1 addition & 0 deletions pallas-traverse/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pallas-codec = { version = "=0.28.0", path = "../pallas-codec" }
hex = "0.4.3"
thiserror = "1.0.31"
paste = "1.0.14"
itertools = "0.13.0"

# TODO: remove once GenesisValue moves into new genesis crate
serde = "1.0.155"
Expand Down
22 changes: 22 additions & 0 deletions pallas-traverse/src/tx.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{borrow::Cow, collections::HashSet, ops::Deref};

use itertools::Itertools;
use pallas_codec::{minicbor, utils::KeepRaw};
use pallas_crypto::hash::Hash;
use pallas_primitives::{
Expand Down Expand Up @@ -209,6 +210,27 @@ impl<'b> MultiEraTx<'b> {
raw
}

pub fn mints_sorted_set(&self) -> Vec<MultiEraPolicyAssets> {
let mut raw = self.mints();

raw.sort_by_key(|m| *m.policy());

raw
}

pub fn withdrawals_sorted_set(&self) -> Vec<(&[u8], u64)> {
match self.withdrawals() {
MultiEraWithdrawals::NotApplicable | MultiEraWithdrawals::Empty => {
std::iter::empty().collect()
}
MultiEraWithdrawals::AlonzoCompatible(x) => x
.iter()
.map(|(k, v)| (k.as_slice(), *v))
.sorted_by_key(|(k, _)| *k)
.collect(),
}
}

/// Return the transaction reference inputs
///
/// NOTE: It is possible for this to return duplicates. See
Expand Down
20 changes: 20 additions & 0 deletions pallas-traverse/src/witnesses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,26 @@ impl<'b> MultiEraTx<'b> {
})
}

pub fn find_mint_redeemer(&self, mint_order: u32) -> Option<MultiEraRedeemer> {
self.redeemers().into_iter().find(|r| {
r.tag() == pallas_primitives::conway::RedeemerTag::Mint && r.index() == mint_order
})
}

pub fn find_withdrawal_redeemer(&self, withdrawal_order: u32) -> Option<MultiEraRedeemer> {
self.redeemers().into_iter().find(|r| {
r.tag() == pallas_primitives::conway::RedeemerTag::Reward
&& r.index() == withdrawal_order
})
}

pub fn find_certificate_redeemer(&self, certificate_order: u32) -> Option<MultiEraRedeemer> {
self.redeemers().into_iter().find(|r| {
r.tag() == pallas_primitives::conway::RedeemerTag::Cert
&& r.index() == certificate_order
})
}

pub fn plutus_v2_scripts(&self) -> &[PlutusV2Script] {
match self {
Self::Byron(_) => &[],
Expand Down
1 change: 1 addition & 0 deletions pallas-utxorpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ utxorpc-spec = { version = "0.7.0" }
[dev-dependencies]
hex = "0.4.3"
serde_json = "1.0.120"
pretty_assertions = "1.4.0"
# utxorpc-spec = { path = "../../../utxorpc/spec/gen/rust" }
53 changes: 41 additions & 12 deletions pallas-utxorpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,12 @@ impl<C: LedgerContext> Mapper<C> {
}
}

pub fn map_cert(&self, x: &trv::MultiEraCert) -> u5c::Certificate {
pub fn map_cert(
&self,
x: &trv::MultiEraCert,
tx: &trv::MultiEraTx,
order: u32,
) -> u5c::Certificate {
let inner = match x.as_alonzo().unwrap() {
babbage::Certificate::StakeRegistration(a) => {
u5c::certificate::Certificate::StakeRegistration(self.map_stake_credential(a))
Expand Down Expand Up @@ -279,15 +284,24 @@ impl<C: LedgerContext> Mapper<C> {

u5c::Certificate {
certificate: inner.into(),
redeemer: None, // TODO
redeemer: tx
.find_certificate_redeemer(order)
.map(|r| self.map_redeemer(&r)),
}
}

pub fn map_withdrawals(&self, x: &(&[u8], u64)) -> u5c::Withdrawal {
pub fn map_withdrawals(
&self,
x: &(&[u8], u64),
tx: &trv::MultiEraTx,
order: u32,
) -> u5c::Withdrawal {
u5c::Withdrawal {
reward_account: Vec::from(x.0).into(),
coin: x.1,
redeemer: None, // TODO
redeemer: tx
.find_withdrawal_redeemer(order)
.map(|x| self.map_redeemer(&x)),
}
}

Expand All @@ -303,7 +317,7 @@ impl<C: LedgerContext> Mapper<C> {
u5c::Multiasset {
policy_id: x.policy().to_vec().into(),
assets: x.assets().iter().map(|x| self.map_asset(x)).collect(),
redeemer: None, // TODO
redeemer: None,
}
}

Expand Down Expand Up @@ -534,17 +548,31 @@ impl<C: LedgerContext> Mapper<C> {
.map(|(order, i)| self.map_tx_input(i, tx, order as u32, &resolved))
.collect(),
outputs: tx.outputs().iter().map(|x| self.map_tx_output(x)).collect(),
certificates: tx.certs().iter().map(|x| self.map_cert(x)).collect(),
certificates: tx
.certs()
.iter()
.enumerate()
.map(|(order, x)| self.map_cert(x, tx, order as u32))
.collect(),
withdrawals: tx
.withdrawals()
.collect::<Vec<_>>()
.withdrawals_sorted_set()
.iter()
.map(|x| self.map_withdrawals(x))
.enumerate()
.map(|(order, x)| self.map_withdrawals(x, tx, order as u32))
.collect(),
mint: tx
.mints()
.mints_sorted_set()
.iter()
.map(|x| self.map_policy_assets(x))
.enumerate()
.map(|(order, x)| {
let mut ma = self.map_policy_assets(x);

ma.redeemer = tx
.find_mint_redeemer(order as u32)
.map(|r| self.map_redeemer(&r));

ma
})
.collect(),
reference_inputs: tx
.reference_inputs()
Expand Down Expand Up @@ -619,6 +647,7 @@ impl<C: LedgerContext> Mapper<C> {
#[cfg(test)]
mod tests {
use super::*;
use pretty_assertions::assert_eq;

#[derive(Clone)]
struct NoLedger;
Expand All @@ -642,7 +671,7 @@ mod tests {
let current = serde_json::json!(mapper.map_block(&block));
let expected: serde_json::Value = serde_json::from_str(&json_str).unwrap();

assert_eq!(current, expected)
assert_eq!(expected, current)
}
}
}
10 changes: 9 additions & 1 deletion test_data/u5c1.json
Original file line number Diff line number Diff line change
Expand Up @@ -1341,7 +1341,15 @@
"mint": [
{
"assets": [{ "mintCoin": "5000000000", "name": "VFVOQQ==" }],
"policyId": "J5+ELDPu2QVLnjxwzWo7MimCWcJLeLiVy0HZGg=="
"policyId": "J5+ELDPu2QVLnjxwzWo7MimCWcJLeLiVy0HZGg==",
"redeemer": {
"datum": {
"constr": {
"tag": 121
}
},
"purpose": "REDEEMER_PURPOSE_MINT"
}
}
],
"outputs": [
Expand Down

0 comments on commit 4fbca7b

Please sign in to comment.