Skip to content

Commit

Permalink
Fix upsert-d-param ExUnit error
Browse files Browse the repository at this point in the history
Co-authored-by: Lech Głowiak <[email protected]>
  • Loading branch information
jankun4 and LGLO authored Dec 19, 2024
1 parent 7ac6614 commit de4ba1b
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 37 deletions.
17 changes: 8 additions & 9 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ jobs:
- name: Deploy and test against local environment
uses: ./.github/actions/tests/local-environment-tests
with:
tag: CI
tag: CI
image: ${{ secrets.ECR_REGISTRY_SECRET }}/substrate-node:${{ needs.build.outputs.sha }}
sha: ${{ needs.build.outputs.sha }}
tests: premerge
Expand All @@ -95,7 +95,7 @@ jobs:
ACTIONS_PAT: ${{ secrets.ACTIONS_PAT }}
ECR_REGISTRY_SECRET: ${{ secrets.ECR_REGISTRY_SECRET }}
TEST_ENVIRONMENT: local

local-environment-tests-alert:
needs: local-environment-tests
if: always() && needs.local-environment-tests.result != 'skipped'
Expand Down Expand Up @@ -226,14 +226,13 @@ jobs:
pre-merge-checks-complete:
if: ${{ always() && (github.event_name == 'workflow_dispatch' || (github.event_name == 'pull_request' && github.event.pull_request.merged == false)) }}
needs: [build, local-environment-tests, argocd-tests, devshell-tests]
needs: [build, local-environment-tests, devshell-tests]
runs-on: ubuntu-latest
steps:
- name: Check if any needed job failed
run: |
if [[ "${{ needs.build.result }}" != "success" ||
"${{ needs.local-environment-tests.result }}" != "success" ||
"${{ needs.argocd-tests.result }}" != "success" ||
if [[ "${{ needs.build.result }}" != "success" ||
"${{ needs.local-environment-tests.result }}" != "success" ||
"${{ needs.devshell-tests.result }}" != "success" ]]; then
echo "One or more needed jobs failed."
exit 1
Expand Down Expand Up @@ -291,7 +290,7 @@ jobs:
- name: Deploy and test against local environment
uses: ./.github/actions/tests/local-environment-tests
with:
tag: CI
tag: CI
image: ${{ secrets.ECR_REGISTRY_SECRET }}/substrate-node:${{ needs.build.outputs.sha }}
sha: ${{ needs.build.outputs.sha }}
tests: postmerge
Expand Down Expand Up @@ -340,8 +339,8 @@ jobs:
steps:
- name: Check if any needed job failed
run: |
if [[ "${{ needs.deploy-rustdoc.result }}" != "success" ||
"${{ needs.upload-chain-specs.result }}" != "success" ||
if [[ "${{ needs.deploy-rustdoc.result }}" != "success" ||
"${{ needs.upload-chain-specs.result }}" != "success" ||
"${{ needs.local-environment-tests-post-merge.result }}" != "success" ]]; then
echo "One or more needed jobs failed."
exit 1
Expand Down
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).expect("Ogmios response should have entry for d_param policy");
let gov_policy_ex_units = mint_ex_units.get(gov_policy_idx).expect("Ogmios response should have entry for gov policy");

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
17 changes: 13 additions & 4 deletions toolkit/offchain/src/d_param/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,22 @@ 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 ex_units_2() -> ExUnits {
ExUnits::new(&20000u32.into(), &400u32.into())
}

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_2(),
governance_utxo(),
)
.expect("Test transaction should be constructed without error")
Expand Down Expand Up @@ -64,7 +67,13 @@ 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_2());

let redeemer_2 = redeemers.get(1);
assert_eq!(redeemer_2.tag(), RedeemerTag::new_mint());
assert_eq!(redeemer_2.index(), 1u64.into());
assert_eq!(redeemer_2.data(), PlutusData::new_empty_constr_plutus_data(&0u64.into()));
assert_eq!(redeemer_2.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));
},
};
Ok(Self { bytes: script.cbor, language: language_kind })
} else {
Err(anyhow!("Expected Plutus script, got something else."))
}
}

/// 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

0 comments on commit de4ba1b

Please sign in to comment.