Skip to content

Commit

Permalink
Cost models update conway (#336)
Browse files Browse the repository at this point in the history
Due to changes here: IntersectMBO/cardano-ledger#4284
Clarification here: IntersectMBO/cardano-ledger#4383

Allows non-Plutus languages according to the spec and changes to an
indexable map type.
  • Loading branch information
rooooooooob committed Jul 23, 2024
1 parent 9b39648 commit 1f4d17c
Show file tree
Hide file tree
Showing 9 changed files with 304 additions and 425 deletions.
2 changes: 1 addition & 1 deletion chain/rust/src/builders/tx_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ impl TransactionBuilderConfigBuilder {
cost_models: if self.cost_models.is_some() {
self.cost_models.unwrap()
} else {
CostModels::new()
CostModels::default()
},
_collateral_percentage: self.collateral_percentage.ok_or(
TxBuilderError::UninitializedField(TxBuilderConfigField::CollateralPercentage),
Expand Down
39 changes: 9 additions & 30 deletions chain/rust/src/crypto/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,37 +88,16 @@ pub fn calc_script_data_hash(
encoding: Option<&TransactionWitnessSetEncoding>,
) -> Result<Option<ScriptDataHash>, ScriptDataHashError> {
if !redeemers.is_empty() || !datums.is_empty() {
let mut required_costmdls = CostModels::new();
let mut required_costmdls = CostModels::default();
for lang in used_langs {
match lang {
Language::PlutusV1 => {
required_costmdls.plutus_v1 = Some(
cost_models
.plutus_v1
.as_ref()
.ok_or(ScriptDataHashError::MissingCostModel(*lang))?
.clone(),
);
}
Language::PlutusV2 => {
required_costmdls.plutus_v2 = Some(
cost_models
.plutus_v2
.as_ref()
.ok_or(ScriptDataHashError::MissingCostModel(*lang))?
.clone(),
);
}
Language::PlutusV3 => {
required_costmdls.plutus_v3 = Some(
cost_models
.plutus_v3
.as_ref()
.ok_or(ScriptDataHashError::MissingCostModel(*lang))?
.clone(),
);
}
}
required_costmdls.inner.insert(
*lang as u64,
cost_models
.inner
.get(&(*lang).into())
.ok_or(ScriptDataHashError::MissingCostModel(*lang))?
.clone(),
);
}

Ok(Some(hash_script_data(
Expand Down
20 changes: 10 additions & 10 deletions chain/rust/src/genesis/network_info.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::{byron::ProtocolMagic, plutus::CostModels};
use cml_core::{
network::{
BYRON_MAINNET_NETWORK_MAGIC, BYRON_TESTNET_NETWORK_MAGIC, PREPROD_NETWORK_MAGIC,
PREVIEW_NETWORK_MAGIC, SANCHO_TESTNET_NETWORK_MAGIC,
},
Int,
use crate::{
byron::ProtocolMagic,
plutus::{CostModels, Language},
};
use cml_core::network::{
BYRON_MAINNET_NETWORK_MAGIC, BYRON_TESTNET_NETWORK_MAGIC, PREPROD_NETWORK_MAGIC,
PREVIEW_NETWORK_MAGIC, SANCHO_TESTNET_NETWORK_MAGIC,
};

#[derive(Debug, Clone, Eq, Hash, Ord, PartialEq, PartialOrd)]
Expand Down Expand Up @@ -67,7 +67,7 @@ impl NetworkInfo {

// TODO: https://github.com/dcSpark/cardano-multiplatform-lib/issues/92
pub fn plutus_alonzo_cost_models() -> CostModels {
let ops: [u64; 166] = [
let ops = vec![
197209, 0, 1, 1, 396231, 621, 0, 1, 150000, 1000, 0, 1, 150000, 32, 2477736, 29175, 4,
29773, 100, 29773, 100, 29773, 100, 29773, 100, 29773, 100, 29773, 100, 100, 100, 29773,
100, 150000, 32, 150000, 32, 150000, 32, 150000, 1000, 0, 1, 150000, 32, 150000, 1000, 0,
Expand All @@ -81,7 +81,7 @@ pub fn plutus_alonzo_cost_models() -> CostModels {
3345831, 1, 1,
];

let mut res = CostModels::new();
res.plutus_v1 = Some(ops.iter().map(|&i| Int::from(i)).collect());
let mut res = CostModels::default();
res.inner.insert(Language::PlutusV1 as u64, ops);
res
}
12 changes: 4 additions & 8 deletions chain/rust/src/plutus/cbor_encodings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@
// https://github.com/dcSpark/cddl-codegen

use cml_core::serialization::{LenEncoding, StringEncoding};
use std::collections::BTreeMap;

#[derive(Clone, Debug, Default)]
pub struct CostModelsEncoding {
pub len_encoding: LenEncoding,
pub orig_deser_order: Vec<usize>,
pub plutus_v1_encoding: LenEncoding,
pub plutus_v1_key_encoding: Option<cbor_event::Sz>,
pub plutus_v2_encoding: LenEncoding,
pub plutus_v2_key_encoding: Option<cbor_event::Sz>,
pub plutus_v3_encoding: LenEncoding,
pub plutus_v3_key_encoding: Option<cbor_event::Sz>,
pub inner_encoding: LenEncoding,
pub inner_key_encodings: BTreeMap<u64, Option<cbor_event::Sz>>,
pub inner_value_encodings: BTreeMap<u64, (LenEncoding, Vec<Option<cbor_event::Sz>>)>,
}

#[derive(Clone, Debug, Default)]
Expand Down
60 changes: 47 additions & 13 deletions chain/rust/src/plutus/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,68 @@ use cbor_encodings::{

use cml_core::ordered_hash_map::OrderedHashMap;
use cml_core::serialization::{LenEncoding, Serialize, StringEncoding};
use cml_core::Int;
use cml_crypto::{blake2b256, DatumHash};

pub use utils::{ConstrPlutusData, PlutusMap, PlutusScript};

#[derive(Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
#[derive(Clone, Debug, Default)]
pub struct CostModels {
pub plutus_v1: Option<Vec<Int>>,
pub plutus_v2: Option<Vec<Int>>,
pub plutus_v3: Option<Vec<Int>>,
#[serde(skip)]
pub inner: OrderedHashMap<u64, Vec<i64>>,
pub encodings: Option<CostModelsEncoding>,
}

impl CostModels {
pub fn new() -> Self {
pub fn new(inner: OrderedHashMap<u64, Vec<i64>>) -> Self {
Self {
plutus_v1: None,
plutus_v2: None,
plutus_v3: None,
inner,
encodings: None,
}
}
}

impl Default for CostModels {
fn default() -> Self {
Self::new()
impl From<OrderedHashMap<u64, Vec<i64>>> for CostModels {
fn from(inner: OrderedHashMap<u64, Vec<i64>>) -> Self {
CostModels::new(inner.clone())
}
}

impl From<CostModels> for OrderedHashMap<u64, Vec<i64>> {
fn from(wrapper: CostModels) -> Self {
wrapper.inner
}
}

impl serde::Serialize for CostModels {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
self.inner.serialize(serializer)
}
}

impl<'de> serde::de::Deserialize<'de> for CostModels {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::de::Deserializer<'de>,
{
let inner =
<OrderedHashMap<u64, Vec<i64>> as serde::de::Deserialize>::deserialize(deserializer)?;
Ok(Self::new(inner))
}
}

impl schemars::JsonSchema for CostModels {
fn schema_name() -> String {
String::from("CostModels")
}

fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
OrderedHashMap::<u64, Vec<i64>>::json_schema(gen)
}

fn is_referenceable() -> bool {
OrderedHashMap::<u64, Vec<i64>>::is_referenceable()
}
}

Expand Down
Loading

0 comments on commit 1f4d17c

Please sign in to comment.