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

Cost models update conway #336

Merged
merged 2 commits into from
Jun 18, 2024
Merged
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
7 changes: 3 additions & 4 deletions 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 Expand Up @@ -720,13 +720,12 @@ impl TransactionBuilder {

match &script_witness.script {
PlutusScriptWitness::Ref(ref_script) => {
if self
if !self
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These diffs should go away if you rebase

.witness_builders
.witness_set_builder
.required_wits
.script_refs
.get(ref_script)
.is_none()
.contains(ref_script)
{
Err(TxBuilderError::RefScriptNotFound(
*ref_script,
Expand Down
6 changes: 3 additions & 3 deletions chain/rust/src/builders/witness_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ impl TransactionWitnessSetBuilder {
pub fn get_native_script(&self) -> Vec<NativeScript> {
self.scripts
.iter()
.filter(|entry| self.required_wits.script_refs.get(entry.0).is_none())
.filter(|entry| !self.required_wits.script_refs.contains(entry.0))
.fold(
Vec::<NativeScript>::new(),
|mut acc, script| match &script.1 {
Expand All @@ -291,7 +291,7 @@ impl TransactionWitnessSetBuilder {
pub fn get_plutus_v1_script(&self) -> Vec<PlutusV1Script> {
self.scripts
.iter()
.filter(|entry| self.required_wits.script_refs.get(entry.0).is_none())
.filter(|entry| !self.required_wits.script_refs.contains(entry.0))
.fold(
Vec::<PlutusV1Script>::new(),
|mut acc, script| match &script.1 {
Expand All @@ -307,7 +307,7 @@ impl TransactionWitnessSetBuilder {
pub fn get_plutus_v2_script(&self) -> Vec<PlutusV2Script> {
self.scripts
.iter()
.filter(|entry| self.required_wits.script_refs.get(entry.0).is_none())
.filter(|entry| !self.required_wits.script_refs.contains(entry.0))
.fold(
Vec::<PlutusV2Script>::new(),
|mut acc, script| match &script.1 {
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
Loading