@@ -209,27 +209,24 @@ to_from_bytes!(CostModel);
209209#[ wasm_bindgen]
210210impl 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
803800use std:: io:: { SeekFrom } ;
804- use crate :: tx_builder_constants:: TxBuilderConstants ;
805801
806802
807803impl 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 }
0 commit comments