Cost Models

typedef struct cardano_costmdls_t cardano_costmdls_t

The execution of plutus scripts consumes resources.

To make sure that these scripts don’t run indefinitely or consume excessive resources (which would be harmful to the network), Cardano introduces the concept of “cost models”.

Cost models are in place to provide predictable pricing for script execution.

It’s a way to gauge how much resource (in terms of computational steps or memory) a script would use.


cardano_error_t cardano_costmdls_new(cardano_costmdls_t **costmdls)

Initializes a new instance of the cardano_costmdls_t type.

This function allocates and initializes a new instance of the cardano_costmdls_t type, which holds a map of Plutus language versions to their respective Cost Models.

Usage Example:

cardano_costmdls_t* costmdls = NULL;

// Attempt to create a new cardano_costmdls_t object
cardano_error_t result = cardano_costmdls_new(&costmdls);

if (result == CARDANO_SUCCESS)
{
  // Use the costmdls

  // Once done, ensure to clean up and release the costmdls
  cardano_costmdls_unref(&costmdls);
}

Parameters:
cardano_costmdls_t **costmdls

[out] On successful initialization, this will point to a newly created cardano_costmdls_t object. This object represents a “strong reference”, meaning that it is fully initialized and ready for use. The caller is responsible for managing the lifecycle of this object. Specifically, once the cardano_costmdls_t is no longer needed, the caller must release it by calling cardano_costmdls_unref.

Returns:

cardano_error_t indicating the outcome of the operation. Returns CARDANO_SUCCESS if the cardano_costmdls_t was successfully created, or an appropriate error code indicating the failure reason.


cardano_error_t cardano_costmdls_from_cbor(cardano_cbor_reader_t *reader, cardano_costmdls_t **costmdls)

Creates a cardano_costmdls_t from a CBOR reader.

This function parses CBOR data using a provided cardano_cbor_reader_t and constructs a cardano_costmdls_t object. It assumes that the CBOR reader is set up correctly and that the CBOR data corresponds to the structure expected for a costmdls.

Usage Example:

cardano_cbor_reader_t* reader = cardano_cbor_reader_new(cbor_data, data_size);
cardano_costmdls_t* costmdls = NULL;

cardano_error_t result = cardano_costmdls_from_cbor(reader, &costmdls);

if (result == CARDANO_SUCCESS)
{
  // Use the costmdls

  // Once done, ensure to clean up and release the costmdls
  cardano_costmdls_unref(&costmdls);
}
else
{
  const char* error = cardano_cbor_reader_get_last_error(reader);
  printf("Failed to decode costmdls: %s\n", error);
}

cardano_cbor_reader_unref(&reader); // Cleanup the CBOR reader

Note

If the function fails, the last error can be retrieved by calling cardano_cbor_reader_get_last_error with the reader. The caller is responsible for freeing the created cardano_costmdls_t object by calling cardano_costmdls_unref when it is no longer needed.

Parameters:
cardano_cbor_reader_t *reader

[in] A pointer to an initialized cardano_cbor_reader_t that is ready to read the CBOR-encoded data.

cardano_costmdls_t **costmdls

[out] A pointer to a pointer of cardano_costmdls_t that will be set to the address of the newly created costmdls object upon successful decoding.

Returns:

A cardano_error_t value indicating the outcome of the operation. Returns CARDANO_SUCCESS if the Cost Model were successfully created, or an appropriate error code if an error occurred.


cardano_error_t cardano_costmdls_to_cbor(const cardano_costmdls_t *costmdls, cardano_cbor_writer_t *writer)

Serializes Cost Model into CBOR format using a CBOR writer.

This function serializes the given cardano_costmdls_t object using a cardano_cbor_writer_t.

Usage Example:

cardano_costmdls_t* costmdls = ...;
cardano_cbor_writer_t* writer = cardano_cbor_writer_new();

if (writer)
{
  cardano_error_t result = cardano_costmdls_to_cbor(costmdls, writer);

  if (result == CARDANO_SUCCESS)
  {
    // Use the writer's buffer containing the serialized data
  }
  else
  {
    const char* error_message = cardano_cbor_writer_get_last_error(writer);
    printf("Serialization failed: %s\n", error_message);
  }

  cardano_cbor_writer_unref(&writer);
}

cardano_costmdls_unref(&costmdls);

Parameters:
const cardano_costmdls_t *costmdls

[in] A constant pointer to the cardano_costmdls_t object that is to be serialized.

cardano_cbor_writer_t *writer

[out] A pointer to a cardano_cbor_writer_t where the CBOR serialized data will be written. The writer must already be initialized and ready to accept the data.

Returns:

Returns CARDANO_SUCCESS if the serialization is successful. If the costmdls or writer is NULL, returns CARDANO_ERROR_POINTER_IS_NULL.


cardano_error_t cardano_costmdls_to_cip116_json(const cardano_costmdls_t *costmdls, cardano_json_writer_t *writer)

Serializes a collection of cost models to CIP-116 JSON.

The function writes the full JSON object, including the surrounding braces. Keys are: “plutus_v1”, “plutus_v2”, “plutus_v3”.

Parameters:
const cardano_costmdls_t *costmdls

[in] Pointer to a valid cardano_costmdls_t.

cardano_json_writer_t *writer

[in] Pointer to a valid cardano_json_writer_t.

Returns:

CARDANO_SUCCESS On success. CARDANO_ERROR_POINTER_IS_NULL If costmdls or writer is NULL. Other Any error propagated from nested writers.


cardano_error_t cardano_costmdls_insert(cardano_costmdls_t *costmdls, cardano_cost_model_t *cost_model)

Inserts a new Cost Model into the Costmdls map.

This function inserts a new Cost Model into the Costmdls map. Each Cost Model is associated with a specific version of the Plutus language and contains the computational cost parameters used for script execution.

Usage Example:

cardano_costmdls_t* costmdls = NULL;
cardano_cost_model_t* cost_model = NULL;
// Assume costmdls and cost_model are initialized properly

cardano_error_t result = cardano_costmdls_insert(costmdls, cost_model);

if (result == CARDANO_SUCCESS)
{
  printf("Cost Model inserted successfully.\n");
}
else
{
  printf("Failed to insert Cost Model.\n");
}

Parameters:
cardano_costmdls_t *costmdls

[in] Pointer to the Costmdls object.

cardano_cost_model_t *cost_model

[in] Pointer to the Cost Model object to be inserted.

Returns:

cardano_error_t indicating the outcome of the operation. Returns CARDANO_SUCCESS if the Cost Model was successfully inserted, or an appropriate error code indicating the failure reason.


cardano_error_t cardano_costmdls_get(cardano_costmdls_t *costmdls, cardano_plutus_language_version_t language, cardano_cost_model_t **cost_model)

Retrieves a specific Cost Model from the Costmdls map based on the Plutus language version.

This function retrieves a Cost Model corresponding to a specific version of the Plutus language from the Costmdls map. It provides access to the computational cost parameters that are used for script execution for the specified language version.

Usage Example:

cardano_costmdls_t* costmdls = NULL;
cardano_cost_model_t* cost_model = NULL;
// Assume costmdls is initialized properly

cardano_error_t result = cardano_costmdls_get(costmdls, CARDANO_PLUTUS_LANGUAGE_VERSION_V1, &cost_model);

if (result == CARDANO_SUCCESS)
{
  // Use the cost_model for computations
  // Remember to release the cost_model after use
  cardano_cost_model_unref(&cost_model);
}
else
{
  printf("Failed to retrieve Cost Model for Plutus V1.\n");
}

Parameters:
cardano_costmdls_t *costmdls

[in] Pointer to the Costmdls object from which the Cost Model is to be retrieved.

cardano_plutus_language_version_t language

[in] The Plutus language version for which the Cost Model is required.

cardano_cost_model_t **cost_model

[out] On successful retrieval, this will point to the Cost Model object associated with the specified language version. The object represents a “strong reference” to the Cost Model, meaning that it remains valid until the caller releases it using cardano_cost_model_unref.

Returns:

cardano_error_t indicating the outcome of the operation. Returns CARDANO_SUCCESS if the Cost Model was successfully retrieved, or an appropriate error code indicating the failure reason (e.g., if no model exists for the specified language version).


bool cardano_costmdls_has(const cardano_costmdls_t *costmdls, cardano_plutus_language_version_t language)

Checks if a Cost Model for a specific Plutus language version exists in the Costmdls map.

This function determines whether there is a Cost Model associated with a specific version of the Plutus language in the provided Costmdls map. It’s useful for validating the presence of cost models before attempting to retrieve or use them in computations.

Usage Example:

cardano_costmdls_t* costmdls = NULL;
// Assume costmdls is initialized properly

bool exists = cardano_costmdls_has(costmdls, CARDANO_PLUTUS_V1);

if (exists)
{
  printf("Cost Model for Plutus V1 exists.\n");
}
else
{
  printf("Cost Model for Plutus V1 does not exist.\n");
}

Parameters:
const cardano_costmdls_t *costmdls

[in] Pointer to the Costmdls object to be checked.

cardano_plutus_language_version_t language

[in] The Plutus language version to check for.

Returns:

true if a Cost Model for the specified language version exists in the Costmdls map, false otherwise.


cardano_error_t cardano_costmdls_get_language_views_encoding(const cardano_costmdls_t *costmdls, cardano_buffer_t **language_views)

Retrieves the language views encoding from the Costmdls object.

This function encodes the costs models following the CDDL specification, necessary for computing the script data hash of a transaction. It encodes the costs associated with each Plutus language version in the Costmdls map in the appropriate CBOR format, following the rules defined for each version.

Usage Example:

cardano_costmdls_t* costmdls = NULL;
// Assume costmdls is initialized and contains cost models for various language versions
cardano_buffer_t* language_views = NULL;

cardano_error_t result = cardano_costmdls_get_language_views_encoding(costmdls, &language_views);

if (result == CARDANO_SUCCESS)
{
  // Use the language_views buffer

  // Once done, ensure to clean up and release the language_views
  cardano_buffer_unref(&language_views);
}

Parameters:
const cardano_costmdls_t *costmdls

[in] Pointer to the Costmdls object whose language views are to be encoded.

cardano_buffer_t **language_views

[out] On successful encoding, this will point to a buffer containing the encoded language views. The caller is responsible for managing the lifecycle of this buffer, which includes releasing it using cardano_buffer_unref when it is no longer needed.

Returns:

cardano_error_t indicating the outcome of the operation. Returns CARDANO_SUCCESS if the language views were successfully encoded, or an appropriate error code indicating the failure reason.


void cardano_costmdls_unref(cardano_costmdls_t **costmdls)

Decrements the reference count of a cardano_costmdls_t object.

This function is responsible for managing the lifecycle of a cardano_costmdls_t object by decreasing its reference count. When the reference count reaches zero, the costmdls is finalized; its associated resources are released, and its memory is deallocated.

Usage Example:

cardano_costmdls_t* costmdls = cardano_costmdls_new(major, minor);

// Perform operations with the costmdls...

cardano_costmdls_unref(&costmdls);
// At this point, costmdls is NULL and cannot be used.

Note

After calling cardano_costmdls_unref, the pointer to the cardano_costmdls_t object will be set to NULL to prevent its reuse.

Parameters:
cardano_costmdls_t **costmdls

[inout] A pointer to the pointer of the costmdls object. This double indirection allows the function to set the caller’s pointer to NULL, avoiding dangling pointer issues after the object has been freed.


void cardano_costmdls_ref(cardano_costmdls_t *costmdls)

Increases the reference count of the cardano_costmdls_t object.

This function is used to manually increment the reference count of an cardano_costmdls_t object, indicating that another part of the code has taken ownership of it. This ensures the object remains allocated and valid until all owners have released their reference by calling cardano_costmdls_unref.

Usage Example:

// Assuming costmdls is a previously created costmdls object

cardano_costmdls_ref(costmdls);

// Now costmdls can be safely used elsewhere without worrying about premature deallocation

Note

Always ensure that for every call to cardano_costmdls_ref there is a corresponding call to cardano_costmdls_unref to prevent memory leaks.

Parameters:
cardano_costmdls_t *costmdls

A pointer to the cardano_costmdls_t object whose reference count is to be incremented.


size_t cardano_costmdls_refcount(const cardano_costmdls_t *costmdls)

Retrieves the current reference count of the cardano_costmdls_t object.

This function returns the number of active references to an cardano_costmdls_t object. It’s useful for debugging purposes or managing the lifecycle of the object in complex scenarios.

Usage Example:

// Assuming costmdls is a previously created costmdls object

size_t ref_count = cardano_costmdls_refcount(costmdls);

printf("Reference count: %zu\n", ref_count);

Warning

This function does not account for transitive references. A transitive reference occurs when an object holds a reference to another object, rather than directly to the cardano_costmdls_t. As such, the reported count may not fully represent the total number of conceptual references in cases where such transitive relationships exist.

Parameters:
const cardano_costmdls_t *costmdls

A pointer to the cardano_costmdls_t object whose reference count is queried. The object must not be NULL.

Returns:

The number of active references to the specified cardano_costmdls_t object. If the object is properly managed (i.e., every cardano_costmdls_ref call is matched with a cardano_costmdls_unref call), this count should reach zero right before the object is deallocated.


void cardano_costmdls_set_last_error(cardano_costmdls_t *costmdls, const char *message)

Sets the last error message for a given cardano_costmdls_t object.

Records an error message in the costmdls’s last_error buffer, overwriting any existing message. This is useful for storing descriptive error information that can be later retrieved. The message is truncated if it exceeds the buffer’s capacity.

Note

The error message is limited to 1023 characters, including the null terminator, due to the fixed size of the last_error buffer.

Parameters:
cardano_costmdls_t *costmdls

[in] A pointer to the cardano_costmdls_t instance whose last error message is to be set. If NULL, the function does nothing.

const char *message

[in] A null-terminated string containing the error message. If NULL, the costmdls’s last_error is set to an empty string, indicating no error.


const char *cardano_costmdls_get_last_error(const cardano_costmdls_t *costmdls)

Retrieves the last error message recorded for a specific costmdls.

This function returns a pointer to the null-terminated string containing the last error message set by cardano_costmdls_set_last_error for the given costmdls. If no error message has been set, or if the last_error buffer was explicitly cleared, an empty string is returned, indicating no error.

Note

The returned string points to internal storage within the object and must not be modified by the caller. The string remains valid until the next call to cardano_costmdls_set_last_error for the same costmdls, or until the costmdls is deallocated.

Parameters:
const cardano_costmdls_t *costmdls

[in] A pointer to the cardano_costmdls_t instance whose last error message is to be retrieved. If the costmdls is NULL, the function returns a generic error message indicating the null costmdls.

Returns:

A pointer to a null-terminated string containing the last error message for the specified costmdls. If the costmdls is NULL, “Object is NULL.” is returned to indicate the error.