Skip to content

Commit 8d99fad

Browse files
committed
Removing CostModel length restrictions
1 parent 53e4097 commit 8d99fad

File tree

3 files changed

+18
-30
lines changed

3 files changed

+18
-30
lines changed

rust/pkg/cardano_serialization_lib.js.flow

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1412,11 +1412,15 @@ declare export class CostModel {
14121412
static from_bytes(bytes: Uint8Array): CostModel;
14131413

14141414
/**
1415+
* Creates a new CostModels instance of an unrestricted length
14151416
* @returns {CostModel}
14161417
*/
14171418
static new(): CostModel;
14181419

14191420
/**
1421+
* Sets the cost at the specified index to the specified value.
1422+
* In case the operation index is larger than the previous largest used index,
1423+
* it will fill any inbetween indexes with zeroes
14201424
* @param {number} operation
14211425
* @param {Int} cost
14221426
* @returns {Int}

rust/src/plutus.rs

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -209,27 +209,24 @@ to_from_bytes!(CostModel);
209209
#[wasm_bindgen]
210210
impl CostModel {
211211

212-
#[deprecated(since = "11.0.0", note="Use `.new_with_length`")]
212+
/// Creates a new CostModels instance of an unrestricted length
213213
pub fn new() -> Self {
214-
Self::new_with_length(TxBuilderConstants::PLUTUS_V1_OP_COUNT)
215-
}
216-
217-
/// Creates a new CostModel instance with the specified underlying length
218-
pub fn new_with_length(len: usize) -> Self {
219-
let mut costs = Vec::with_capacity(len);
220-
for _ in 0 .. len {
221-
costs.push(Int::new_i32(0));
222-
}
223-
Self(costs)
214+
Self(Vec::new())
224215
}
225216

217+
/// Sets the cost at the specified index to the specified value.
218+
/// In case the operation index is larger than the previous largest used index,
219+
/// it will fill any inbetween indexes with zeroes
226220
pub fn set(&mut self, operation: usize, cost: &Int) -> Result<Int, JsError> {
227-
let max = self.0.len();
228-
if operation >= max {
229-
return Err(JsError::from_str(&format!("CostModel operation {} out of bounds. Max is {}", operation, max)));
221+
let len = self.0.len();
222+
let idx = operation.clone();
223+
if idx >= len {
224+
for _ in 0 .. (idx - len + 1) {
225+
self.0.push(Int::new_i32(0));
226+
}
230227
}
231-
let old = self.0[operation].clone();
232-
self.0[operation] = cost.clone();
228+
let old = self.0[idx].clone();
229+
self.0[idx] = cost.clone();
233230
Ok(old)
234231
}
235232

@@ -801,7 +798,6 @@ impl Strings {
801798
// Serialization
802799

803800
use std::io::{SeekFrom};
804-
use crate::tx_builder_constants::TxBuilderConstants;
805801

806802

807803
impl cbor_event::se::Serialize for PlutusScript {
@@ -926,15 +922,6 @@ impl Deserialize for CostModel {
926922
}
927923
arr.push(Int::deserialize(raw)?);
928924
}
929-
let min = TxBuilderConstants::PLUTUS_V1_OP_COUNT;
930-
let max = TxBuilderConstants::PLUTUS_V2_OP_COUNT;
931-
if arr.len() != min && arr.len() != max {
932-
return Err(DeserializeFailure::OutOfRange{
933-
min,
934-
max,
935-
found: arr.len()
936-
}.into());
937-
}
938925
Ok(())
939926
})().map_err(|e| e.annotate("CostModel"))?;
940927
Ok(Self(arr.try_into().unwrap()))
@@ -1587,6 +1574,7 @@ mod tests {
15871574

15881575
#[test]
15891576
fn test_cost_model_roundtrip() {
1577+
use crate::tx_builder_constants::TxBuilderConstants;
15901578
let costmodels = TxBuilderConstants::plutus_vasil_cost_models();
15911579
assert_eq!(costmodels, Costmdls::from_bytes(costmodels.to_bytes()).unwrap());
15921580
}

rust/src/tx_builder_constants.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use super::*;
2-
use crate::plutus::{Costmdls, CostModel, Language};
32

43
// The first element is the cost model, which is an array of 166 operations costs, ordered by asc operaion names.
54
// The second value is the pre-calculated `language_views_encoding` value required for the script hash creation.
@@ -12,9 +11,6 @@ pub struct TxBuilderConstants();
1211
#[wasm_bindgen]
1312
impl TxBuilderConstants {
1413

15-
pub const PLUTUS_V1_OP_COUNT: usize = 166;
16-
pub const PLUTUS_V2_OP_COUNT: usize = 175;
17-
1814
pub fn plutus_default_cost_models() -> Costmdls {
1915
TxBuilderConstants::plutus_alonzo_cost_models()
2016
}

0 commit comments

Comments
 (0)