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

Fix upsert-d-param ExUnit error #341

Merged
merged 3 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
47 changes: 24 additions & 23 deletions toolkit/offchain/src/d_param/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::csl::{
use crate::plutus_script::PlutusScript;
use anyhow::anyhow;
use cardano_serialization_lib::{
ExUnits, JsError, PlutusData, Transaction, TransactionBuilder, TxInputsBuilder,
ExUnits, JsError, PlutusData, ScriptHash, Transaction, TransactionBuilder, TxInputsBuilder,
};
use cardano_serialization_lib::{LanguageKind, TransactionHash, TransactionInput};
use ogmios_client::{
Expand All @@ -20,6 +20,7 @@ use ogmios_client::{
};
use partner_chains_plutus_data::d_param::{d_parameter_to_plutus_data, DParamDatum};
use sidechain_domain::{DParameter, McTxHash, UtxoId};
use std::collections::HashMap;

#[cfg(test)]
mod tests;
Expand Down Expand Up @@ -119,13 +120,7 @@ async fn insert_d_param<C: QueryLedgerState + Transactions + QueryNetwork>(
where
C: Transactions,
{
let zero_ex_units = ScriptExUnits {
mint_ex_units: vec![
ExUnits::new(&0u64.into(), &0u64.into()),
ExUnits::new(&0u64.into(), &0u64.into()),
],
spend_ex_units: vec![],
};
let zero_ex_units = ExUnits::new(&0u64.into(), &0u64.into());

let gov_utxo = crate::init_governance::get_governance_utxo(genesis_utxo, client)
.await
Expand All @@ -136,24 +131,38 @@ where
policy,
d_parameter,
&ctx,
zero_ex_units.clone(),
zero_ex_units,
gov_utxo.clone(),
)?;

let evaluate_response = client.evaluate_transaction(&tx.to_bytes()).await.map_err(|e| {
anyhow!(
"Evaluate insert D-parameter transaction request failed: {}, bytes: {}",
e,
hex::encode(tx.to_bytes())
)
})?;
let mut mint_witness_ex_units = get_validator_budgets(evaluate_response);
mint_witness_ex_units.mint_ex_units.reverse();

let ogmios_gov_script =
gov_utxo.clone().script.expect("Gov UTXO should have a reference script");
let gov_policy = PlutusScript::from_ogmios(ogmios_gov_script)?;
let mint_keys = tx.body().mint().expect("insert D parameter transaction has two mints").keys();
let script_to_index: HashMap<ScriptHash, usize> =
vec![(mint_keys.get(0), 0), (mint_keys.get(1), 1)].into_iter().collect();
let mint_ex_units = get_validator_budgets(evaluate_response).mint_ex_units;
let policy_idx = script_to_index.get(&policy.csl_script_hash()).unwrap().clone();
let gov_policy_idx = script_to_index.get(&gov_policy.csl_script_hash()).unwrap().clone();
let policy_ex_units = mint_ex_units.get(policy_idx).unwrap();
jankun4 marked this conversation as resolved.
Show resolved Hide resolved
let gov_policy_ex_units = mint_ex_units.get(gov_policy_idx).unwrap();

let tx = mint_d_param_token_tx(
validator,
policy,
d_parameter,
&ctx,
mint_witness_ex_units,
policy_ex_units.clone(),
gov_policy_ex_units.clone(),
gov_utxo,
)?;
let signed_tx = ctx.sign(&tx).to_bytes();
Expand Down Expand Up @@ -235,18 +244,13 @@ fn mint_d_param_token_tx(
policy: &PlutusScript,
d_parameter: &DParameter,
ctx: &TransactionContext,
mut ex_units: ScriptExUnits,
d_param_policy_ex_units: ExUnits,
gov_policy_ex_units: ExUnits,
gov_utxo: OgmiosUtxo,
) -> Result<Transaction, JsError> {
let mut tx_builder = TransactionBuilder::new(&get_builder_config(ctx)?);
// The essence of transaction: mint a governance token, then mint tokena D-Param and set output with it
tx_builder.add_mint_one_script_token(
policy,
ex_units
.mint_ex_units
.pop()
.unwrap_or_else(|| panic!("Mint ex units not found")),
)?;
tx_builder.add_mint_one_script_token(policy, d_param_policy_ex_units)?;
tx_builder.add_output_with_one_script_token(
validator,
policy,
Expand All @@ -266,10 +270,7 @@ fn mint_d_param_token_tx(
tx_builder.add_mint_one_script_token_using_reference_script(
&gov_policy,
&gov_tx_input,
ex_units
.mint_ex_units
.pop()
.unwrap_or_else(|| panic!("Mint ex units not found")),
gov_policy_ex_units,
)?;

tx_builder.add_script_reference_input(&gov_tx_input, gov_policy.bytes.len());
Expand Down
8 changes: 3 additions & 5 deletions toolkit/offchain/src/d_param/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,17 @@ mod mint_tx {
use super::*;
use cardano_serialization_lib::Transaction;

fn mint_ex_units() -> ExUnits {
fn ex_units() -> ExUnits {
ExUnits::new(&10000u32.into(), &200u32.into())
}
fn ex_units() -> ScriptExUnits {
ScriptExUnits::new().with_mint_ex_units(vec![mint_ex_units(), mint_ex_units()])
}
fn mint_d_param_tx() -> Transaction {
mint_d_param_token_tx(
&test_validator(),
&test_policy(),
&input_d_param(),
&test_tx_context(),
ex_units(),
ex_units(),
jankun4 marked this conversation as resolved.
Show resolved Hide resolved
governance_utxo(),
)
.expect("Test transaction should be constructed without error")
Expand Down Expand Up @@ -64,7 +62,7 @@ mod mint_tx {
assert_eq!(redeemer.tag(), RedeemerTag::new_mint());
assert_eq!(redeemer.index(), 0u64.into());
assert_eq!(redeemer.data(), PlutusData::new_empty_constr_plutus_data(&0u64.into()));
assert_eq!(redeemer.ex_units(), mint_ex_units());
assert_eq!(redeemer.ex_units(), ex_units());
}

#[test]
Expand Down
17 changes: 17 additions & 0 deletions toolkit/offchain/src/plutus_script.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use anyhow::{anyhow, Context};
use cardano_serialization_lib::{Address, LanguageKind, NetworkIdKind, PlutusData, ScriptHash};
use ogmios_client::types::{OgmiosScript, OgmiosScript::Plutus};
use plutus::ToDatum;
use sidechain_domain::PolicyId;
use uplc::ast::{DeBruijn, Program};
Expand All @@ -18,6 +19,22 @@ impl PlutusScript {
Self { bytes: cbor.into(), language }
}

pub fn from_ogmios(ogmios_script: OgmiosScript) -> anyhow::Result<Self> {
if let Plutus(script) = ogmios_script {
let language_kind = match script.language.as_str() {
"plutus:v1" => LanguageKind::PlutusV1,
"plutus:v2" => LanguageKind::PlutusV2,
"plutus:v3" => LanguageKind::PlutusV3,
_ => {
return Err(anyhow!("Unsupported Plutus language version: {}", script.language))
},
};
return Ok(Self { bytes: script.cbor, language: language_kind });
} else {
return Err(anyhow!("Expected Plutus script, got something else."));
jankun4 marked this conversation as resolved.
Show resolved Hide resolved
}
}

/// This function is needed to create [PlutusScript] from scripts in [raw_scripts],
/// which are encoded as a cbor byte string containing the cbor of the script
/// itself. This function removes this layer of wrapping.
Expand Down
1 change: 0 additions & 1 deletion toolkit/offchain/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ async fn init_goveranance() {
()
}

#[ignore = "awaiting fix for matching evaluation costs of redeemers"]
#[tokio::test]
async fn upsert_d_param() {
let image = GenericImage::new(TEST_IMAGE, TEST_IMAGE_TAG);
Expand Down
Loading