Skip to content

Commit

Permalink
Merge branch 'master' into ETCM-8949-use-permissioned-candidates-offc…
Browse files Browse the repository at this point in the history
…hain-in-wizard
  • Loading branch information
LGLO authored Dec 19, 2024
2 parents f76e54d + 7ac6614 commit 213da41
Show file tree
Hide file tree
Showing 8 changed files with 333 additions and 15 deletions.
11 changes: 7 additions & 4 deletions toolkit/offchain/src/csl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,15 +184,15 @@ impl ScriptExUnits {

pub(crate) fn get_validator_budgets(
mut responses: Vec<OgmiosEvaluateTransactionResponse>,
) -> Result<ScriptExUnits, JsError> {
) -> ScriptExUnits {
responses.sort_by_key(|r| r.validator.index);
let (mint_ex_units, spend_ex_units) = responses
.into_iter()
.partition::<Vec<_>, _>(|response| response.validator.purpose == "mint");
let mint_ex_units = mint_ex_units.into_iter().map(ex_units_from_response).collect();
let spend_ex_units = spend_ex_units.into_iter().map(ex_units_from_response).collect();

Ok(ScriptExUnits { mint_ex_units, spend_ex_units })
ScriptExUnits { mint_ex_units, spend_ex_units }
}

fn ex_units_from_response(resp: OgmiosEvaluateTransactionResponse) -> ExUnits {
Expand All @@ -208,6 +208,10 @@ pub(crate) fn empty_asset_name() -> AssetName {
AssetName::new(vec![]).expect("Hardcoded empty asset name is valid")
}

pub fn zero_ex_units() -> ExUnits {
ExUnits::new(&BigNum::zero(), &BigNum::zero())
}

pub(crate) trait OgmiosUtxoExt {
fn to_csl_tx_input(&self) -> TransactionInput;
fn to_csl_tx_output(&self) -> Result<TransactionOutput, JsError>;
Expand Down Expand Up @@ -706,8 +710,7 @@ mod tests {
validator: OgmiosValidatorIndex::new(2, "spend"),
budget: OgmiosBudget::new(12, 22),
},
])
.expect("Should succeed");
]);

let expected = ScriptExUnits {
mint_ex_units: vec![
Expand Down
8 changes: 2 additions & 6 deletions toolkit/offchain/src/d_param/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ where
hex::encode(tx.to_bytes())
)
})?;
let mut mint_witness_ex_units = get_validator_budgets(evaluate_response)?;
let mut mint_witness_ex_units = get_validator_budgets(evaluate_response);
mint_witness_ex_units.mint_ex_units.reverse();
let tx = mint_d_param_token_tx(
validator,
Expand Down Expand Up @@ -206,7 +206,7 @@ where
hex::encode(tx.to_bytes())
)
})?;
let spend_ex_units = get_validator_budgets(evaluate_response)?;
let spend_ex_units = get_validator_budgets(evaluate_response);

let tx = update_d_param_tx(
validator,
Expand Down Expand Up @@ -272,8 +272,6 @@ fn mint_d_param_token_tx(
.unwrap_or_else(|| panic!("Mint ex units not found")),
)?;

let tx_hash = TransactionHash::from_bytes(gov_utxo.transaction.id.into())?;
let gov_tx_input = TransactionInput::new(&tx_hash, gov_utxo.index.into());
tx_builder.add_script_reference_input(&gov_tx_input, gov_policy.bytes.len());
tx_builder.add_required_signer(&ctx.payment_key_hash());
tx_builder.balance_update_and_build(ctx)
Expand Down Expand Up @@ -327,8 +325,6 @@ fn update_d_param_tx(
.unwrap_or_else(|| panic!("Mint ex units not found")),
)?;

let tx_hash = TransactionHash::from_bytes(gov_utxo.transaction.id.into())?;
let gov_tx_input = TransactionInput::new(&tx_hash, gov_utxo.index.into());
tx_builder.add_script_reference_input(&gov_tx_input, gov_policy.bytes.len());
tx_builder.add_required_signer(&ctx.payment_key_hash());
tx_builder.balance_update_and_build(ctx)
Expand Down
19 changes: 15 additions & 4 deletions toolkit/offchain/src/init_governance/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::csl::OgmiosUtxoExt;
use crate::plutus_script;
use crate::scripts_data;
use crate::{
await_tx::{AwaitTx, FixedDelayRetries},
Expand Down Expand Up @@ -164,13 +165,20 @@ pub async fn get_governance_utxo<T: QueryLedgerState + Transactions + QueryNetwo
}

pub(crate) struct GovernanceData {
policy_script: PlutusScript,
utxo_id: UtxoId,
pub(crate) policy_script: plutus_script::PlutusScript,
pub(crate) utxo_id: UtxoId,
}

impl GovernanceData {
pub(crate) fn policy_script_hash(&self) -> ScriptHash {
self.policy_script.hash()
self.policy_script.csl_script_hash()
}

pub(crate) fn utxo_id_as_tx_input(&self) -> TransactionInput {
TransactionInput::new(
&TransactionHash::from_bytes(self.utxo_id.tx_hash.0.to_vec()).unwrap(),
self.utxo_id.index.0.into(),
)
}
}

Expand All @@ -181,7 +189,10 @@ pub(crate) async fn get_governance_data<T: QueryLedgerState + Transactions + Que
let utxo = get_governance_utxo(genesis_utxo, client).await?;
let utxo_id = utxo.to_domain();
if let Some(OgmiosScript::Plutus(ps)) = utxo.script.clone() {
Ok(GovernanceData { policy_script: PlutusScript::new_v2(ps.cbor), utxo_id })
Ok(GovernanceData {
policy_script: plutus_script::PlutusScript::from_cbor(&ps.cbor, LanguageKind::PlutusV2),
utxo_id,
})
} else {
Err(anyhow!("Programmatic Error: Governance UTXO script is not PlutusScript"))
}
Expand Down
1 change: 1 addition & 0 deletions toolkit/offchain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub mod permissioned_candidates;
mod plutus_script;
/// Supports candidate registration
pub mod register;
pub mod reserve;
/// Provides synthetized scripts data
pub mod scripts_data;
#[cfg(test)]
Expand Down
2 changes: 1 addition & 1 deletion toolkit/offchain/src/plutus_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl PlutusScript {
}

/// This function is needed to create [PlutusScript] from scripts in [raw_scripts],
/// which are encoded as a cibor byte string containing the cbor of the script
/// which are encoded as a cbor byte string containing the cbor of the script
/// itself. This function removes this layer of wrapping.
pub fn from_wrapped_cbor(cbor: &[u8], language: LanguageKind) -> anyhow::Result<Self> {
Ok(Self::from_cbor(&unwrap_one_layer_of_cbor(cbor)?, language))
Expand Down
Loading

0 comments on commit 213da41

Please sign in to comment.