Transaction Evaluator

typedef struct cardano_tx_evaluator_t cardano_tx_evaluator_t

Opaque structure for a transaction evaluator.

The cardano_tx_evaluator_t structure serves as the public-facing handle for managing the transaction evaluation process within the Cardano framework.


cardano_error_t cardano_tx_evaluator_new(cardano_tx_evaluator_impl_t impl, cardano_tx_evaluator_t **tx_evaluator)

Creates a new transaction evaluator instance.

This function initializes a new cardano_tx_evaluator_t instance using the provided implementation. The tx_evaluator parameter will be set to point to the newly created evaluator, which can then be used for evaluating transactions’ execution units, considering additional UTXOs and redeemers.

Usage Example:

cardano_tx_evaluator_t* evaluator = NULL;
cardano_tx_evaluator_impl_t impl = ...; // Assume this is set up with a valid implementation.
cardano_error_t result = cardano_tx_evaluator_new(impl, &evaluator);

if (result == CARDANO_SUCCESS)
{
    // Evaluator successfully created, ready for use
}

Note

The caller is responsible for releasing the created tx_evaluator instance by using the appropriate unref function when done.

Parameters:
cardano_tx_evaluator_impl_t impl

[in] A cardano_tx_evaluator_impl_t implementation that defines the specific evaluation strategy and operations.

cardano_tx_evaluator_t **tx_evaluator

[out] A double pointer to the cardano_tx_evaluator_t instance, which will be allocated and set by this function.

Returns:

CARDANO_SUCCESS if the evaluator was successfully created, or an appropriate error code if the operation failed.


const char *cardano_tx_evaluator_get_name(const cardano_tx_evaluator_t *tx_evaluator)

Retrieves the name of the tx_evaluator implementation.

This function returns a constant string representing the name of the tx_evaluator implementation. The name can be used for logging, debugging, or informational purposes to identify which tx_evaluator implementation is being used.

Usage Example:

const char* tx_evaluator_name = cardano_tx_evaluator_get_name(tx_evaluator);

if (tx_evaluator_name)
{
  printf("Using tx_evaluator: %s\n", tx_evaluator_name);
}
else
{
  printf("Failed to retrieve tx_evaluator name.\n");
}

Note

The returned string remains valid as long as the cardano_tx_evaluator_t object is valid. Do not attempt to modify or free the returned string.

Parameters:
const cardano_tx_evaluator_t *tx_evaluator

[in] Pointer to the cardano_tx_evaluator_t object.

Returns:

A constant character pointer to the tx_evaluator’s name string. The returned string is owned by the tx_evaluator and must not be modified or freed by the caller. If the tx_evaluator is NULL or invalid, the function may return NULL.


cardano_error_t cardano_tx_evaluator_evaluate(cardano_tx_evaluator_t *tx_evaluator, cardano_transaction_t *tx, cardano_utxo_list_t *additional_utxos, cardano_redeemer_list_t **redeemers)

Evaluates the execution units required for a transaction.

This function calculates the execution units needed for a given transaction (tx) by using the provided tx_evaluator. Evaluation considers any additional UTXOs required for the transaction and assigns appropriate redeemers based on the evaluation.

Usage Example:

cardano_redeemer_list_t* redeemers = NULL;
cardano_error_t result = cardano_tx_evaluator_evaluate(tx_evaluator, tx, additional_utxos, &redeemers);

if (result == CARDANO_SUCCESS)
{
  // Redeemers were successfully evaluated and are now available in the redeemers list
}

// Clean up when done
cardano_redeemer_list_unref(&redeemers);

Note

The caller is responsible for managing and releasing memory for the redeemers list once it is no longer needed.

Parameters:
cardano_tx_evaluator_t *tx_evaluator

[in] A pointer to the cardano_tx_evaluator_t instance used to evaluate the transaction.

cardano_transaction_t *tx

[in] A pointer to the cardano_transaction_t object representing the transaction to evaluate.

cardano_utxo_list_t *additional_utxos

[in] An optional list of additional UTXOs that may be needed for transaction evaluation.

cardano_redeemer_list_t **redeemers

[out] A double pointer to a cardano_redeemer_list_t, which will be populated with the evaluated redeemers that specify the execution units needed for the transaction.

Returns:

CARDANO_SUCCESS if the transaction was successfully evaluated, or an appropriate error code indicating failure.


void cardano_tx_evaluator_unref(cardano_tx_evaluator_t **tx_evaluator)

Decrements the reference count of a cardano_tx_evaluator_t object.

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

Usage Example:

cardano_tx_evaluator_t* tx_evaluator = cardano_tx_evaluator_new(major, minor);

// Perform operations with the tx_evaluator...

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

Note

After calling cardano_tx_evaluator_unref, the pointer to the cardano_tx_evaluator_t object will be set to NULL to prevent its reuse.

Parameters:
cardano_tx_evaluator_t **tx_evaluator

[inout] A pointer to the pointer of the tx_evaluator 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_tx_evaluator_ref(cardano_tx_evaluator_t *tx_evaluator)

Increases the reference count of the cardano_tx_evaluator_t object.

This function is used to manually increment the reference count of an cardano_tx_evaluator_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_tx_evaluator_unref.

Usage Example:

// Assuming tx_evaluator is a previously created tx_evaluator object

cardano_tx_evaluator_ref(tx_evaluator);

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

Note

Always ensure that for every call to cardano_tx_evaluator_ref there is a corresponding call to cardano_tx_evaluator_unref to prevent memory leaks.

Parameters:
cardano_tx_evaluator_t *tx_evaluator

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


size_t cardano_tx_evaluator_refcount(const cardano_tx_evaluator_t *tx_evaluator)

Retrieves the current reference count of the cardano_tx_evaluator_t object.

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

Usage Example:

// Assuming tx_evaluator is a previously created tx_evaluator object

size_t ref_count = cardano_tx_evaluator_refcount(tx_evaluator);

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_tx_evaluator_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_tx_evaluator_t *tx_evaluator

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

Returns:

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


void cardano_tx_evaluator_set_last_error(cardano_tx_evaluator_t *tx_evaluator, const char *message)

Sets the last error message for a given cardano_tx_evaluator_t object.

Records an error message in the tx_evaluator’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_tx_evaluator_t *tx_evaluator

[in] A pointer to the cardano_tx_evaluator_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 tx_evaluator’s last_error is set to an empty string, indicating no error.


const char *cardano_tx_evaluator_get_last_error(const cardano_tx_evaluator_t *tx_evaluator)

Retrieves the last error message recorded for a specific tx_evaluator.

This function returns a pointer to the null-terminated string containing the last error message set by cardano_tx_evaluator_set_last_error for the given tx_evaluator. 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_tx_evaluator_set_last_error for the same tx_evaluator, or until the tx_evaluator is deallocated.

Parameters:
const cardano_tx_evaluator_t *tx_evaluator

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

Returns:

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