input-output-hk / cardano-js-sdk

JavaScript SDK for interacting with Cardano, providing various key management options, with support for popular hardware wallets
https://input-output-hk.github.io/cardano-js-sdk/
Apache License 2.0
216 stars 59 forks source link

Update CostModel Validation to Allow Dynamic Cost Model Sizes Based on CDDL Definition #1453

Closed ilap closed 1 month ago

ilap commented 2 months ago

GitHub Issue Title:

Update CostModel Validation to Allow Dynamic Cost Model Sizes Based on CDDL Definition

GitHub Issue Description:

Description: The current implementation of the CostModel class in the Plutus language enforces a strict operation count for each language version. Specifically, the code checks that the costs array length exactly matches a predefined static length, such as:

https://github.com/input-output-hk/cardano-js-sdk/blob/54e20144bc510129c71de4c414a0c44fed523398/packages/core/src/Serialization/Update/Costmdls/CostModel.ts#L34

However, according to the official CDDL definition, the format for cost models is flexible enough to allow additional integers in the costs array beyond the predefined count. These extra integers are accepted and ignored, thus making the model future-proof for potential expansions, such as adding new Plutus built-ins or language versions.

The relevant portion of the CDDL definition is as follows:

costmdls =
  { ? 0 : [ 166* int ] ; Plutus v1, only 166 integers are used, but more are accepted (and ignored)
  , ? 1 : [ 175* int ] ; Plutus v2, only 175 integers are used, but more are accepted (and ignored)
  , ? 2 : [ 223* int ] ; Plutus v3, only 223 integers are used, but more are accepted (and ignored)
  , ? 3 : [ int ] ; Any 8-bit unsigned number can be used as a key.
  }

Proposed Change:

The validation logic should be updated to check that the costs array length is at least as long as the required count but allows for longer arrays:

if (costs.length < PLUTUS_V2_COST_MODEL_OP_COUNT)
  throw new InvalidArgumentError(
    'costs',
    `Cost model for PlutusV2 language should have at least ${PLUTUS_V2_COST_MODEL_OP_COUNT} operations, but got ${costs.length}.`
  );

This change will ensure compliance with the CDDL specification and allow for more flexible and future-proof cost models.

References:

CDDL definition of cost models

AngelCastilloB commented 1 month ago

This issue should be solved in version 0.40.0