Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move Builders to use Map-format Redeemers #368

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 25 additions & 64 deletions chain/rust/src/builders/redeemer_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,52 +5,17 @@ use super::{
};
use crate::{
address::RewardAddress,
plutus::{ExUnits, LegacyRedeemer, PlutusData, RedeemerTag, Redeemers},
plutus::{ExUnits, PlutusData, RedeemerKey, RedeemerTag, RedeemerVal, Redeemers},
transaction::TransactionInput,
PolicyId,
};
use cml_core::ordered_hash_map::OrderedHashMap;
use std::{collections::BTreeMap, fmt::Debug};

#[derive(Clone, Copy, PartialOrd, Ord, Debug, PartialEq, Eq, Hash)]
pub struct RedeemerWitnessKey {
tag: RedeemerTag,
index: u64,
}

impl RedeemerWitnessKey {
pub fn new(tag: RedeemerTag, index: u64) -> Self {
Self { tag, index }
}
}

impl From<&LegacyRedeemer> for RedeemerWitnessKey {
fn from(redeemer: &LegacyRedeemer) -> Self {
Self {
tag: redeemer.tag,
index: redeemer.index,
}
}
}

/// LegacyRedeemer without the tag of index
/// This allows builder code to return partial redeemers
/// and then later have them placed in the right context
#[derive(Clone, Debug)]
pub struct UntaggedRedeemer {
pub data: PlutusData,
pub ex_units: ExUnits,
}

impl UntaggedRedeemer {
pub fn new(data: PlutusData, ex_units: ExUnits) -> Self {
Self { data, ex_units }
}
}

#[derive(Clone, Debug)]
enum UntaggedRedeemerPlaceholder {
JustData(PlutusData),
Full(UntaggedRedeemer),
Full(RedeemerVal),
}

impl UntaggedRedeemerPlaceholder {
Expand Down Expand Up @@ -117,46 +82,46 @@ impl RedeemerSetBuilder {
}

/// note: will override existing value if called twice with the same key
pub fn update_ex_units(&mut self, key: RedeemerWitnessKey, ex_units: ExUnits) {
pub fn update_ex_units(&mut self, key: RedeemerKey, ex_units: ExUnits) {
match key.tag {
RedeemerTag::Spend => {
let entry = self.spend.iter_mut().nth(key.index as usize).unwrap().1;
*entry = Some(UntaggedRedeemerPlaceholder::Full(UntaggedRedeemer::new(
*entry = Some(UntaggedRedeemerPlaceholder::Full(RedeemerVal::new(
entry.as_ref().unwrap().data().clone(),
ex_units,
)));
}
RedeemerTag::Mint => {
let entry = self.mint.iter_mut().nth(key.index as usize).unwrap().1;
*entry = Some(UntaggedRedeemerPlaceholder::Full(UntaggedRedeemer::new(
*entry = Some(UntaggedRedeemerPlaceholder::Full(RedeemerVal::new(
entry.as_ref().unwrap().data().clone(),
ex_units,
)));
}
RedeemerTag::Cert => {
let entry = self.cert.get_mut(key.index as usize).unwrap();
*entry = Some(UntaggedRedeemerPlaceholder::Full(UntaggedRedeemer::new(
*entry = Some(UntaggedRedeemerPlaceholder::Full(RedeemerVal::new(
entry.as_ref().unwrap().data().clone(),
ex_units,
)));
}
RedeemerTag::Reward => {
let entry = self.reward.iter_mut().nth(key.index as usize).unwrap().1;
*entry = Some(UntaggedRedeemerPlaceholder::Full(UntaggedRedeemer::new(
*entry = Some(UntaggedRedeemerPlaceholder::Full(RedeemerVal::new(
entry.as_ref().unwrap().data().clone(),
ex_units,
)));
}
RedeemerTag::Proposing => {
let entry = self.proposals.get_mut(key.index as usize).unwrap();
*entry = Some(UntaggedRedeemerPlaceholder::Full(UntaggedRedeemer::new(
*entry = Some(UntaggedRedeemerPlaceholder::Full(RedeemerVal::new(
entry.as_ref().unwrap().data().clone(),
ex_units,
)));
}
RedeemerTag::Voting => {
let entry = self.votes.get_mut(key.index as usize).unwrap();
*entry = Some(UntaggedRedeemerPlaceholder::Full(UntaggedRedeemer::new(
*entry = Some(UntaggedRedeemerPlaceholder::Full(RedeemerVal::new(
entry.as_ref().unwrap().data().clone(),
ex_units,
)));
Expand Down Expand Up @@ -253,7 +218,7 @@ impl RedeemerSetBuilder {
}

pub fn build(&self, default_to_dummy_exunits: bool) -> Result<Redeemers, RedeemerBuilderError> {
let mut redeemers = Vec::new();
let mut redeemers = OrderedHashMap::new();
// Calling iter on a BTreeMap returns a list of sorted keys
self.remove_placeholders_and_tag(
&mut redeemers,
Expand Down Expand Up @@ -292,17 +257,17 @@ impl RedeemerSetBuilder {
default_to_dummy_exunits,
)?;

Ok(Redeemers::new_arr_legacy_redeemer(redeemers))
Ok(Redeemers::new_map_redeemer_key_to_redeemer_val(redeemers))
}

fn remove_placeholders_and_tag<'a, K: Debug + Clone>(
&self,
redeemers: &mut Vec<LegacyRedeemer>,
redeemers: &mut OrderedHashMap<RedeemerKey, RedeemerVal>,
tag: RedeemerTag,
entries: &mut dyn Iterator<Item = (&'a K, &'a Option<UntaggedRedeemerPlaceholder>)>,
default_to_dummy_exunits: bool,
) -> Result<(), RedeemerBuilderError> {
let mut result = vec![];
let mut untagged_redeemers = vec![];
for (i, entry) in entries.enumerate() {
let key = (tag, i, entry.0);

Expand All @@ -313,37 +278,33 @@ impl RedeemerSetBuilder {
MissingExunitError::Key(key.0, key.1, format!("{:?}", key.2)),
))
} else {
Ok(Some(UntaggedRedeemer::new(data.clone(), ExUnits::dummy())))
Ok(Some(RedeemerVal::new(data.clone(), ExUnits::dummy())))
}
}
Some(UntaggedRedeemerPlaceholder::Full(untagged_redeemer)) => {
Ok(Some(untagged_redeemer.clone()))
}
None => Ok(None),
}?;
result.push(redeemer);
untagged_redeemers.push(redeemer);
}
redeemers.append(&mut Self::tag_redeemer(tag, &result));
Self::tag_redeemer(redeemers, tag, &untagged_redeemers);
Ok(())
}

fn tag_redeemer(
redeemers: &mut OrderedHashMap<RedeemerKey, RedeemerVal>,
tag: RedeemerTag,
untagged_redeemers: &[Option<UntaggedRedeemer>],
) -> Vec<LegacyRedeemer> {
let mut result = Vec::new();

untagged_redeemers: &[Option<RedeemerVal>],
) {
for (index, untagged_redeemer) in untagged_redeemers.iter().enumerate() {
if let Some(untagged_redeemer) = untagged_redeemer {
result.push(LegacyRedeemer::new(
tag,
index as u64,
untagged_redeemer.data.clone(),
untagged_redeemer.ex_units.clone(),
));
redeemers.insert(
RedeemerKey::new(tag, index as u64),
untagged_redeemer.clone(),
);
}
}
result
}
}

Expand Down Expand Up @@ -417,7 +378,7 @@ mod tests {
builder.add_spend(&input_result);

builder.update_ex_units(
RedeemerWitnessKey::new(RedeemerTag::Spend, 0),
RedeemerKey::new(RedeemerTag::Spend, 0),
ExUnits::new(10, 10),
);

Expand Down
39 changes: 23 additions & 16 deletions chain/rust/src/builders/tx_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use super::output_builder::{OutputBuilderError, SingleOutputBuilderResult};
use super::proposal_builder::ProposalBuilderResult;
use super::redeemer_builder::RedeemerBuilderError;
use super::redeemer_builder::RedeemerSetBuilder;
use super::redeemer_builder::RedeemerWitnessKey;
use super::vote_builder::VoteBuilderResult;
use super::withdrawal_builder::WithdrawalBuilderResult;
use super::witness_builder::merge_fake_witness;
Expand All @@ -25,8 +24,7 @@ use crate::deposit::{internal_get_deposit, internal_get_implicit_input};
use crate::fees::LinearFee;
use crate::governance::{ProposalProcedure, VotingProcedures};
use crate::min_ada::min_ada_required;
use crate::plutus::{CostModels, ExUnits, Language};
use crate::plutus::{PlutusData, Redeemers};
use crate::plutus::{CostModels, ExUnits, Language, PlutusData, RedeemerKey, Redeemers};
use crate::transaction::{
DatumOption, ScriptRef, Transaction, TransactionBody, TransactionInput, TransactionOutput,
TransactionWitnessSet,
Expand Down Expand Up @@ -118,9 +116,11 @@ impl WitnessBuilders {
let redeemers = self.redeemer_set_builder.build(true)?;
let mut witness_set_clone = self.witness_set_builder.clone();
redeemers
.to_flat_format()
.into_iter()
.for_each(|r| witness_set_clone.add_redeemer(r));
.to_map_format()
.iter()
.for_each(|(key, redeemer)| {
witness_set_clone.add_redeemer(key.clone(), redeemer.clone())
});

if include_fake {
merge_fake_witness(&mut witness_set_clone, &self.fake_required_witnesses);
Expand Down Expand Up @@ -1509,7 +1509,7 @@ impl TransactionBuilder {
}

/// used to override the exunit values initially provided when adding inputs
pub fn set_exunits(&mut self, redeemer: RedeemerWitnessKey, ex_units: ExUnits) {
pub fn set_exunits(&mut self, redeemer: RedeemerKey, ex_units: ExUnits) {
self.witness_builders
.redeemer_set_builder
.update_ex_units(redeemer, ex_units);
Expand Down Expand Up @@ -1547,7 +1547,7 @@ impl TxRedeemerBuilder {
}

/// used to override the exunit values initially provided when adding inputs
pub fn set_exunits(&mut self, redeemer: RedeemerWitnessKey, ex_units: ExUnits) {
pub fn set_exunits(&mut self, redeemer: RedeemerKey, ex_units: ExUnits) {
self.witness_builders
.redeemer_set_builder
.update_ex_units(redeemer, ex_units);
Expand Down Expand Up @@ -5368,17 +5368,20 @@ mod tests {
}

let original_tx_fee = tx_builder.min_fee(false).unwrap();
assert_eq!(original_tx_fee, 470421);
// 44 more than original as the redeemer format changed to map which uses 1 byte more
assert_eq!(original_tx_fee, 470465);
tx_builder.set_fee(897753);

{
tx_builder.set_exunits(
RedeemerWitnessKey::new(RedeemerTag::Spend, 0),
RedeemerKey::new(RedeemerTag::Spend, 0),
ExUnits::new(5000000, 2000000000),
);
}
let tx = tx_builder.build(ChangeSelectionAlgo::Default, &Address::from_bech32("addr1q9tzwgthsm4hs8alk5v3rgjn7nf9pldlmnc3nrns6dvct2dqzvgjxvajrmzsvwh9fucmp65gxc6mv3fskurctfyuj5zqc7q30l").unwrap()).unwrap();
assert_eq!(hex::encode(tx.body.to_cbor_bytes()), "a700d9010281825820473899cb48414442ea107735f7fc3e020f0293122e9d05e4be6f03ffafde5a0c00018283581d71aba3c2914116298a146af57d8156b1583f183fc05c0aa48ee95bec71821a001c41caa1581c6bec713b08a2d7c64baa3596d200b41b560850919d72e634944f2d52a14f537061636542756442696433303533015820f7f2f57c58b5e4872201ab678928b0d63935e82d022d385e1bad5bfe347e89d8825839015627217786eb781fbfb51911a253f4d250fdbfdcf1198e70d35985a9a013112333b21ec5063ae54f31b0ea883635b64530b70785a49c95041a040228dd021a000db2d907582029ed935cc80249c4de9f3e96fdcea6b7da123a543bbe75fffe9e2c66119e426d0b58205d5863643ea0687f9ca3ea903e9d86d81787373da1ebf196206c31f29608ce9b0dd9010281825820a90a895d07049afc725a0d6a38c6b82218b8d1de60e7bd70ecdd58f1d9e1218b000ed9010281581c1c616f1acb460668a9b2f123c80372c2adad3583b9c6cd2b1deeed1c");
// original: a700d9010281825820473899cb48414442ea107735f7fc3e020f0293122e9d05e4be6f03ffafde5a0c00018283581d71aba3c2914116298a146af57d8156b1583f183fc05c0aa48ee95bec71821a001c41caa1581c6bec713b08a2d7c64baa3596d200b41b560850919d72e634944f2d52a14f537061636542756442696433303533015820f7f2f57c58b5e4872201ab678928b0d63935e82d022d385e1bad5bfe347e89d8825839015627217786eb781fbfb51911a253f4d250fdbfdcf1198e70d35985a9a013112333b21ec5063ae54f31b0ea883635b64530b70785a49c95041a040228dd021a000db2d907582029ed935cc80249c4de9f3e96fdcea6b7da123a543bbe75fffe9e2c66119e426d0b58205d5863643ea0687f9ca3ea903e9d86d81787373da1ebf196206c31f29608ce9b0dd9010281825820a90a895d07049afc725a0d6a38c6b82218b8d1de60e7bd70ecdd58f1d9e1218b000ed9010281581c1c616f1acb460668a9b2f123c80372c2adad3583b9c6cd2b1deeed1c
// changed due to redeemer format changing
assert_eq!(hex::encode(tx.body.to_cbor_bytes()), "a700d9010281825820473899cb48414442ea107735f7fc3e020f0293122e9d05e4be6f03ffafde5a0c00018283581d71aba3c2914116298a146af57d8156b1583f183fc05c0aa48ee95bec71821a001c41caa1581c6bec713b08a2d7c64baa3596d200b41b560850919d72e634944f2d52a14f537061636542756442696433303533015820f7f2f57c58b5e4872201ab678928b0d63935e82d022d385e1bad5bfe347e89d8825839015627217786eb781fbfb51911a253f4d250fdbfdcf1198e70d35985a9a013112333b21ec5063ae54f31b0ea883635b64530b70785a49c95041a040228dd021a000db2d907582029ed935cc80249c4de9f3e96fdcea6b7da123a543bbe75fffe9e2c66119e426d0b5820ef2bf6654c6bb34514b16edc28ae51df117f9252322735962ea571e2614525d50dd9010281825820a90a895d07049afc725a0d6a38c6b82218b8d1de60e7bd70ecdd58f1d9e1218b000ed9010281581c1c616f1acb460668a9b2f123c80372c2adad3583b9c6cd2b1deeed1c");
}

#[test]
Expand Down Expand Up @@ -5532,11 +5535,11 @@ mod tests {
);
{
tx_redeemer_builder.set_exunits(
RedeemerWitnessKey::new(RedeemerTag::Spend, 0),
RedeemerKey::new(RedeemerTag::Spend, 0),
ExUnits::new(5000000, 2000000000),
);
tx_builder.set_exunits(
RedeemerWitnessKey::new(RedeemerTag::Spend, 0),
RedeemerKey::new(RedeemerTag::Spend, 0),
ExUnits::new(5000000, 2000000000),
);
}
Expand All @@ -5548,13 +5551,17 @@ mod tests {
)
.unwrap();
let real_script_hash = signed_tx_builder.body.script_data_hash.as_ref().unwrap();
// different than original 5d5863643ea0687f9ca3ea903e9d86d81787373da1ebf196206c31f29608ce9b
// because the redeemer format changed which changes the script data hash
assert_eq!(
real_script_hash.to_hex(),
"5d5863643ea0687f9ca3ea903e9d86d81787373da1ebf196206c31f29608ce9b"
"ef2bf6654c6bb34514b16edc28ae51df117f9252322735962ea571e2614525d5"
);

let tx = &signed_tx_builder.body;
assert_eq!(hex::encode(tx.to_cbor_bytes()), "a700d9010281825820473899cb48414442ea107735f7fc3e020f0293122e9d05e4be6f03ffafde5a0c00018283581d71aba3c2914116298a146af57d8156b1583f183fc05c0aa48ee95bec71821a001c41caa1581c6bec713b08a2d7c64baa3596d200b41b560850919d72e634944f2d52a14f537061636542756442696433303533015820f7f2f57c58b5e4872201ab678928b0d63935e82d022d385e1bad5bfe347e89d8825839015627217786eb781fbfb51911a253f4d250fdbfdcf1198e70d35985a9a013112333b21ec5063ae54f31b0ea883635b64530b70785a49c95041a040228dd021a000db2d907582029ed935cc80249c4de9f3e96fdcea6b7da123a543bbe75fffe9e2c66119e426d0b58205d5863643ea0687f9ca3ea903e9d86d81787373da1ebf196206c31f29608ce9b0dd9010281825820a90a895d07049afc725a0d6a38c6b82218b8d1de60e7bd70ecdd58f1d9e1218b000ed9010281581c1c616f1acb460668a9b2f123c80372c2adad3583b9c6cd2b1deeed1c");
// original: a700d9010281825820473899cb48414442ea107735f7fc3e020f0293122e9d05e4be6f03ffafde5a0c00018283581d71aba3c2914116298a146af57d8156b1583f183fc05c0aa48ee95bec71821a001c41caa1581c6bec713b08a2d7c64baa3596d200b41b560850919d72e634944f2d52a14f537061636542756442696433303533015820f7f2f57c58b5e4872201ab678928b0d63935e82d022d385e1bad5bfe347e89d8825839015627217786eb781fbfb51911a253f4d250fdbfdcf1198e70d35985a9a013112333b21ec5063ae54f31b0ea883635b64530b70785a49c95041a040228dd021a000db2d907582029ed935cc80249c4de9f3e96fdcea6b7da123a543bbe75fffe9e2c66119e426d0b58205d5863643ea0687f9ca3ea903e9d86d81787373da1ebf196206c31f29608ce9b0dd9010281825820a90a895d07049afc725a0d6a38c6b82218b8d1de60e7bd70ecdd58f1d9e1218b000ed9010281581c1c616f1acb460668a9b2f123c80372c2adad3583b9c6cd2b1deeed1c
// changed due to redeemer format changing
assert_eq!(hex::encode(tx.to_cbor_bytes()), "a700d9010281825820473899cb48414442ea107735f7fc3e020f0293122e9d05e4be6f03ffafde5a0c00018283581d71aba3c2914116298a146af57d8156b1583f183fc05c0aa48ee95bec71821a001c41caa1581c6bec713b08a2d7c64baa3596d200b41b560850919d72e634944f2d52a14f537061636542756442696433303533015820f7f2f57c58b5e4872201ab678928b0d63935e82d022d385e1bad5bfe347e89d8825839015627217786eb781fbfb51911a253f4d250fdbfdcf1198e70d35985a9a013112333b21ec5063ae54f31b0ea883635b64530b70785a49c95041a040228dd021a000db2d907582029ed935cc80249c4de9f3e96fdcea6b7da123a543bbe75fffe9e2c66119e426d0b5820ef2bf6654c6bb34514b16edc28ae51df117f9252322735962ea571e2614525d50dd9010281825820a90a895d07049afc725a0d6a38c6b82218b8d1de60e7bd70ecdd58f1d9e1218b000ed9010281581c1c616f1acb460668a9b2f123c80372c2adad3583b9c6cd2b1deeed1c");
}

#[test]
Expand Down Expand Up @@ -5708,7 +5715,7 @@ mod tests {

{
tx_builder.set_exunits(
RedeemerWitnessKey::new(RedeemerTag::Spend, 0),
RedeemerKey::new(RedeemerTag::Spend, 0),
ExUnits::new(5000000, 2000000000),
);
}
Expand Down
Loading
Loading