Execution Units¶
-
typedef struct cardano_ex_units_t cardano_ex_units_t¶
Represent a measure of computational resources, specifically, how much memory and CPU a Plutus script will use when executed.
It’s an essential component to estimate the cost of running a Plutus script on the Cardano blockchain.
The two resources measured by ExUnits are memory and CPU. When a Plutus script is executed, it consumes both these resources. The ExUnits system quantifies this consumption, helping to ensure that scripts don’t overrun the system and that they terminate in a reasonable amount of time.
-
cardano_error_t cardano_ex_units_new(uint64_t memory, uint64_t cpu_steps, cardano_ex_units_t **ex_units)¶
Creates and initializes a new instance of cardano_ex_units_t.
This function allocates and initializes a new instance of cardano_ex_units_t. Execution units (ExUnits) are a measure of the computational resources required.
Usage Example:
cardano_ex_units_t* ex_units = NULL; uint64_t memory = 1024; // 1024 units of memory uint64_t cpu_steps = 500; // 500 CPU steps // Attempt to create a new execution units object cardano_error_t result = cardano_ex_units_new(memory, cpu_steps, &ex_units); if (result == CARDANO_SUCCESS) { // Use the ex_units // Once done, ensure to clean up and release the ex_units cardano_ex_units_unref(&ex_units); }- Parameters:¶
- uint64_t memory¶
[in] The amount of memory (in units) that the script is expected to consume.
- uint64_t cpu_steps¶
[in] The number of CPU steps that the script is expected to consume.
- cardano_ex_units_t **ex_units¶
[out] On successful initialization, this will point to a newly created cardano_ex_units_t object. This object represents a “strong reference” to the ex_units, meaning that it is fully initialized and ready for use. The caller is responsible for managing the lifecycle of this object. Specifically, once the execution units are no longer needed, the caller must release them by calling cardano_ex_units_unref.
- Returns:¶
cardano_error_t indicating the outcome of the operation. Returns CARDANO_SUCCESS if the execution units were successfully created, or an appropriate error code indicating the failure reason.
-
cardano_error_t cardano_ex_units_from_cbor(cardano_cbor_reader_t *reader, cardano_ex_units_t **ex_units)¶
Creates a cardano_ex_units_t from a CBOR reader.
This function parses CBOR data using a provided cardano_cbor_reader_t and constructs a cardano_ex_units_t object. It assumes that the CBOR reader is set up correctly and that the CBOR data corresponds to the structure expected for a ex_units.
Usage Example:
cardano_cbor_reader_t* reader = cardano_cbor_reader_new(cbor_data, data_size); cardano_ex_units_t* ex_units = NULL; cardano_error_t result = cardano_ex_units_from_cbor(reader, &ex_units); if (result == CARDANO_SUCCESS) { // Use the ex_units // Once done, ensure to clean up and release the ex_units cardano_ex_units_unref(&ex_units); } else { const char* error = cardano_cbor_reader_get_last_error(reader); printf("Failed to decode ex_units: %s\n", error); } cardano_cbor_reader_unref(&reader); // Cleanup the CBOR readerNote
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_ex_units_t object by calling cardano_ex_units_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_ex_units_t **ex_units¶
[out] A pointer to a pointer of cardano_ex_units_t that will be set to the address of the newly created ex_units object upon successful decoding.
- Returns:¶
A cardano_error_t value indicating the outcome of the operation. Returns CARDANO_SUCCESS if the execution units were successfully created, or an appropriate error code if an error occurred.
-
cardano_error_t cardano_ex_units_to_cbor(const cardano_ex_units_t *ex_units, cardano_cbor_writer_t *writer)¶
Serializes execution units into CBOR format using a CBOR writer.
This function serializes the given cardano_ex_units_t object using a cardano_cbor_writer_t.
Usage Example:
cardano_ex_units_t* ex_units = ...; cardano_cbor_writer_t* writer = cardano_cbor_writer_new(); if (writer) { cardano_error_t result = cardano_ex_units_to_cbor(ex_units, 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_ex_units_unref(&ex_units);- Parameters:¶
- const cardano_ex_units_t *ex_units¶
[in] A constant pointer to the cardano_ex_units_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
ex_unitsorwriteris NULL, returns CARDANO_ERROR_POINTER_IS_NULL.
-
cardano_error_t cardano_ex_units_to_cip116_json(const cardano_ex_units_t *ex_units, cardano_json_writer_t *writer)¶
Serializes execution units to CIP-116 JSON.
The function writes the full JSON object, including the surrounding braces. Keys are written in the order: “mem”, “steps”. Both values are encoded as strings to ensure precision for 64-bit integers in JSON.
- Parameters:¶
- const cardano_ex_units_t *ex_units¶
[in] Pointer to a valid cardano_ex_units_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
ex_unitsorwriteris NULL. Other Any error propagated from nested writers.
-
uint64_t cardano_ex_units_get_memory(const cardano_ex_units_t *ex_units)¶
Retrieves the memory component of the execution units.
Usage Example:
cardano_ex_units_t* ex_units = ...; // Assume ex_units is initialized and contains memory and cpu_steps uint64_t memory_allocated = cardano_ex_units_get_memory(ex_units); printf("Memory: %llu units\n", memory_allocated);- Parameters:¶
- const cardano_ex_units_t *ex_units¶
[in] A constant pointer to the cardano_ex_units_t object from which the memory is to be retrieved.
- Returns:¶
The amount of memory.
-
cardano_error_t cardano_ex_units_set_memory(cardano_ex_units_t *ex_units, uint64_t memory)¶
Sets the memory amount for the execution units.
This function sets the memory units in the cardano_ex_units_t object.
Usage Example:
cardano_ex_units_t* ex_units = cardano_ex_units_new(1024, 1000); // Assume ex_units is initialized with default values cardano_error_t result = cardano_ex_units_set_memory(ex_units, 2048); // Update memory to 2048 bytes if (result == CARDANO_SUCCESS) { // Memory value updated successfully } else { // Handle error }- Parameters:¶
- cardano_ex_units_t *ex_units¶
[inout] A pointer to the cardano_ex_units_t object whose memory amount is to be set. This object must be previously initialized.
- uint64_t memory¶
[in] The memory amount to be set for the execution units.
- Returns:¶
CARDANO_SUCCESS if the memory was successfully set; otherwise, an appropriate error code is returned indicating the failure reason.
-
uint64_t cardano_ex_units_get_cpu_steps(const cardano_ex_units_t *ex_units)¶
Retrieves the CPU steps allocated to an cardano_ex_units_t object.
This function retrieves the amount of CPU steps that have been allocated to a cardano_ex_units_t object, representing the computational resources allowed for executing a script.
Usage Example:
cardano_ex_units_t* ex_units = ...; // Assume this is initialized uint64_t cpu_steps = cardano_ex_units_get_cpu_steps(ex_units); printf("CPU steps allocated: %llu\n", (unsigned long long)cpu_steps);- Parameters:¶
- const cardano_ex_units_t *ex_units¶
[in] A constant pointer to the cardano_ex_units_t object from which the CPU steps are to be retrieved.
- Returns:¶
The amount of CPU steps allocated to the cardano_ex_units_t object. If the ex_units is NULL, the function returns 0.
-
cardano_error_t cardano_ex_units_set_cpu_steps(cardano_ex_units_t *ex_units, uint64_t cpu_steps)¶
Sets the CPU steps for the specified execution units object.
This function sets the number of CPU steps that the execution units object will represent.
Usage Example:
cardano_ex_units_t* ex_units = ...; // Assume ex_units is already created and initialized uint64_t cpu_steps = 5000; // Example CPU steps cardano_error_t result = cardano_ex_units_set_cpu_steps(ex_units, cpu_steps); if (result == CARDANO_SUCCESS) { // CPU steps are now set to 5000 // Proceed with using the ex_units object } else { // Handle the error fprintf(stderr, "Failed to set CPU steps: %d\n", result); }Note
It is important to ensure that the
ex_unitsobject has been properly initialized before calling this function. Ifex_unitsis NULL, the function will return CARDANO_ERROR_POINTER_IS_NULL.- Parameters:¶
- cardano_ex_units_t *ex_units¶
[inout] A pointer to the cardano_ex_units_t object whose CPU steps are to be set.
- uint64_t cpu_steps¶
[in] The number of CPU steps to set for the execution units.
- Returns:¶
cardano_error_t indicating the outcome of the operation. Returns CARDANO_SUCCESS if the CPU steps were successfully set, or an appropriate error code if an error occurred.
-
void cardano_ex_units_unref(cardano_ex_units_t **ex_units)¶
Decrements the reference count of a cardano_ex_units_t object.
This function is responsible for managing the lifecycle of a cardano_ex_units_t object by decreasing its reference count. When the reference count reaches zero, the ex_units is finalized; its associated resources are released, and its memory is deallocated.
Usage Example:
cardano_ex_units_t* ex_units = cardano_ex_units_new(mem, cpu_steps); // Perform operations with the ex_units... cardano_ex_units_unref(&ex_units); // At this point, ex_units is NULL and cannot be used.Note
After calling cardano_ex_units_unref, the pointer to the cardano_ex_units_t object will be set to NULL to prevent its reuse.
- Parameters:¶
- cardano_ex_units_t **ex_units¶
[inout] A pointer to the pointer of the ex_units 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_ex_units_ref(cardano_ex_units_t *ex_units)¶
Increases the reference count of the cardano_ex_units_t object.
This function is used to manually increment the reference count of an cardano_ex_units_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_ex_units_unref.
Usage Example:
// Assuming ex_units is a previously created ex_units object cardano_ex_units_ref(ex_units); // Now ex_units can be safely used elsewhere without worrying about premature deallocationNote
Always ensure that for every call to cardano_ex_units_ref there is a corresponding call to cardano_ex_units_unref to prevent memory leaks.
- Parameters:¶
- cardano_ex_units_t *ex_units¶
A pointer to the cardano_ex_units_t object whose reference count is to be incremented.
-
size_t cardano_ex_units_refcount(const cardano_ex_units_t *ex_units)¶
Retrieves the current reference count of the cardano_ex_units_t object.
This function returns the number of active references to an cardano_ex_units_t object. It’s useful for debugging purposes or managing the lifecycle of the object in complex scenarios.
Usage Example:
// Assuming ex_units is a previously created ex_units object size_t ref_count = cardano_ex_units_refcount(ex_units); 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_ex_units_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_ex_units_t *ex_units¶
A pointer to the cardano_ex_units_t object whose reference count is queried. The object must not be NULL.
- Returns:¶
The number of active references to the specified cardano_ex_units_t object. If the object is properly managed (i.e., every cardano_ex_units_ref call is matched with a cardano_ex_units_unref call), this count should reach zero right before the object is deallocated.
-
void cardano_ex_units_set_last_error(cardano_ex_units_t *ex_units, const char *message)¶
Sets the last error message for a given cardano_ex_units_t object.
Records an error message in the ex_units’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_ex_units_t *ex_units¶
[in] A pointer to the cardano_ex_units_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 ex_units’s last_error is set to an empty string, indicating no error.
-
const char *cardano_ex_units_get_last_error(const cardano_ex_units_t *ex_units)¶
Retrieves the last error message recorded for a specific ex_units.
This function returns a pointer to the null-terminated string containing the last error message set by cardano_ex_units_set_last_error for the given ex_units. 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_ex_units_set_last_error for the same ex_units, or until the ex_units is deallocated.
- Parameters:¶
- const cardano_ex_units_t *ex_units¶
[in] A pointer to the cardano_ex_units_t instance whose last error message is to be retrieved. If the ex_units is NULL, the function returns a generic error message indicating the null ex_units.
- Returns:¶
A pointer to a null-terminated string containing the last error message for the specified ex_units. If the ex_units is NULL, “Object is NULL.” is returned to indicate the error.