Native Transaction Evaluator

A transaction evaluator backed by the in-process Untyped Plutus Core virtual machine. It resolves each redeemer’s script and datum, builds the version-appropriate Plutus script context, evaluates the program on the CEK machine against the supplied cost model, and writes the consumed execution units back into the redeemer. It implements the standard transaction evaluator interface, so it is used exactly like the provider-backed evaluator and requires no network access.

From protocol version 12 the evaluator also runs Plutus V4 scripts, the guarding redeemers of a transaction, and the scripts of its sub transactions. A transaction and its sub transactions are evaluated together, as the ledger does: every script of every level is deserialised before any of them runs, and a failure at any level fails the whole evaluation. The evaluator created with cardano_tx_evaluator_new_native returns the redeemers of the top level transaction, and the one created with cardano_tx_evaluator_new_native_sub_transaction returns the redeemers of one sub transaction.

cardano_error_t cardano_tx_evaluator_new_native(const cardano_slot_config_t *slot_config, cardano_costmdls_t *cost_models, uint64_t protocol_major, cardano_tx_evaluator_t **tx_evaluator)

Creates a native phase-2 transaction evaluator backed by the in-process UPLC VM.

The native evaluator is the phase-2 driver: for every redeemer in a transaction it resolves the script and (for spends) the datum, builds the version-appropriate Plutus ScriptContext, applies the arguments, evaluates the program on the CEK machine, and writes the consumed execution units back into the redeemer it returns. It plugs into the existing cardano_tx_evaluator_t interface through a cardano_tx_evaluator_impl_t, so callers use it exactly as they would the provider-backed evaluator.

A Dijkstra transaction runs scripts at two levels: the top-level transaction and each of its sub-transactions, every level with its own redeemers. Like the ledger, the evaluator runs the redeemers of every level together: a script that fails at any level fails the evaluation. The redeemers this evaluator returns are those of the top-level transaction; use cardano_tx_evaluator_new_native_sub_transaction for the redeemers of a sub-transaction. Plutus V4 scripts are evaluated with the V4 ScriptContext of the level they run at, guarding redeemers resolve to the guard at their index in the guard set of their level (key-hash guards count towards the index), and a script needed by any level can be provided by the witness set of any level or by a reference script.

The cardano_tx_evaluator_evaluate signature carries no slot config, cost models or protocol version, so this constructor binds them once: the slot config converts validity slots to POSIX time for the ScriptContext, the cost models and protocol major version select the builtin semantics variant per script. The evaluator copies slot_config and takes a reference on cost_models; both stay owned by the evaluator for its lifetime.

Usage Example:

cardano_tx_evaluator_t* evaluator = NULL;
cardano_error_t         result    = cardano_tx_evaluator_new_native(
  &CARDANO_MAINNET_SLOT_CONFIG, cost_models, 10U, &evaluator);

if (result == CARDANO_SUCCESS)
{
  cardano_redeemer_list_t* redeemers = NULL;
  result = cardano_tx_evaluator_evaluate(evaluator, tx, additional_utxos, &redeemers);
  // ... inspect redeemers' ex-units ...
  cardano_redeemer_list_unref(&redeemers);
  cardano_tx_evaluator_unref(&evaluator);
}

Remark

For each script the driver builds the structured cost model from the cost_models entry matching the script’s language version and threads it, together with the selected builtin semantics variant, into the CEK machine’s budget accounting. The returned execution units therefore reflect the supplied ledger cost model exactly: the machine charges every step and builtin against those coefficients. When cost_models carries no entry for a version, the per-version default coefficients for the protocol version are used instead.

Remark

Each script is deserialised the way the ledger does at the protocol version: its language and every builtin it names must be available there. Every script of every level is deserialised before any of them runs, so a script that cannot be deserialised makes cardano_tx_evaluator_evaluate return CARDANO_ERROR_DECODING even when another script would fail phase-2 validation. A script whose Plutus Core version is not available at the protocol version, a script with a free variable, a script that runs and fails, or a Plutus V3 or V4 script that does not return unit, returns CARDANO_ERROR_SCRIPT_EVALUATION_FAILURE. A sub-transaction redeemer that runs a Plutus V1, V2 or V3 script returns CARDANO_ERROR_INVALID_SCRIPT_LANGUAGE, since those languages only run at the top level. Below protocol version 12 the free variable check, like the ledger’s, does not look inside constr and case terms; from 12 on it covers the whole script.

Remark

A redeemer whose index points past the items of its purpose in its transaction level (for example past the end of the guard set) returns CARDANO_ERROR_INDEX_OUT_OF_BOUNDS, and a redeemer whose item is not authorised by a script (for example a key-hash guard) returns CARDANO_ERROR_INVALID_ARGUMENT. Like the ledger, a Plutus V1, V2 or V3 script cannot run in a transaction that carries direct deposits, account balance intervals, required top-level guards or a script-hash guard, since its ScriptContext cannot represent them; such a transaction returns CARDANO_ERROR_INVALID_ARGUMENT, as does a Plutus V1 or V2 script in a transaction with votes, proposals, a current treasury value or a treasury donation.

Parameters:
const cardano_slot_config_t *slot_config

[in] The slot/time parameters used to convert the transaction validity interval to POSIX time. Must not be NULL.

cardano_costmdls_t *cost_models

[in] The ledger cost models keyed by Plutus language version. May be NULL, in which case the evaluator uses the per-version default semantics for the protocol version.

uint64_t protocol_major

[in] The protocol major version selecting the builtin semantics variant and the language, builtin and Plutus Core versions each script may use.

cardano_tx_evaluator_t **tx_evaluator

[out] On success, set to the newly created evaluator. The caller owns it and releases it with cardano_tx_evaluator_unref. Left untouched on failure.

Returns:

CARDANO_SUCCESS on success, CARDANO_ERROR_POINTER_IS_NULL if slot_config or tx_evaluator is NULL, or CARDANO_ERROR_MEMORY_ALLOCATION_FAILED if the evaluator context cannot be allocated.


cardano_error_t cardano_tx_evaluator_new_native_sub_transaction(const cardano_slot_config_t *slot_config, cardano_costmdls_t *cost_models, uint64_t protocol_major, uint64_t sub_tx_index, cardano_tx_evaluator_t **tx_evaluator)

Creates a native phase-2 evaluator that returns the redeemers of a sub-transaction.

Behaves like cardano_tx_evaluator_new_native and evaluates the whole transaction the same way, the top level and every sub-transaction together, but the redeemer list cardano_tx_evaluator_evaluate returns holds the redeemers of the sub-transaction at position sub_tx_index of the top-level transaction body, in the order of that sub-transaction’s witness set, each carrying its computed execution units. Together with the evaluator returned by cardano_tx_evaluator_new_native, this maps the execution units back to every redeemer at every level.

Evaluating a transaction that has no sub-transaction at sub_tx_index returns CARDANO_ERROR_INDEX_OUT_OF_BOUNDS.

Usage Example:

cardano_tx_evaluator_t* evaluator = NULL;
cardano_error_t         result    = cardano_tx_evaluator_new_native_sub_transaction(
  &slot_config, cost_models, 12U, 0U, &evaluator);

if (result == CARDANO_SUCCESS)
{
  cardano_redeemer_list_t* redeemers = NULL;
  result = cardano_tx_evaluator_evaluate(evaluator, tx, resolved_utxos, &redeemers);

  if (result == CARDANO_SUCCESS)
  {
    printf("The first sub transaction has %zu redeemers.\n", cardano_redeemer_list_get_length(redeemers));
  }

  cardano_redeemer_list_unref(&redeemers);
  cardano_tx_evaluator_unref(&evaluator);
}

Parameters:
const cardano_slot_config_t *slot_config

[in] The slot/time parameters used to convert the validity intervals to POSIX time. Must not be NULL.

cardano_costmdls_t *cost_models

[in] The ledger cost models keyed by Plutus language version. May be NULL, in which case the evaluator uses the per-version default semantics for the protocol version.

uint64_t protocol_major

[in] The protocol major version selecting the builtin semantics variant and the language, builtin and Plutus Core versions each script may use.

uint64_t sub_tx_index

[in] The position of the sub-transaction in the body of the evaluated transaction.

cardano_tx_evaluator_t **tx_evaluator

[out] On success, set to the newly created evaluator. The caller owns it and releases it with cardano_tx_evaluator_unref. Left untouched on failure.

Returns:

CARDANO_SUCCESS on success, CARDANO_ERROR_POINTER_IS_NULL if slot_config or tx_evaluator is NULL, or CARDANO_ERROR_MEMORY_ALLOCATION_FAILED if the evaluator context cannot be allocated.