Transaction and contract execution
Transaction and contract execution turns signed user requests and contract-originated calls into deterministic state changes and execution outcomes. The runtime checks who may act, charges the cost of creating work, runs account and contract actions through receipts, and records the resulting balances, fees, logs, and follow-up work. See Runtime execution for the chunk-level runtime flow, Virtual machine execution for the contract execution boundary, and Chain data processing for how execution results enter chain data.
Transaction intake
Signed transaction
A signed transaction names a signer, a receiver, an access key, a nonce, and an ordered list of actions. The transaction hash identifies its outcome and becomes the source for the first receipt ID when the request is accepted for execution. core/…/transaction.rs defines the transaction data and the execution status shared by transaction and receipt outcomes.
The runtime first checks rules that do not need shard state. It then reads the signer account and access key to check the key, nonce, permissions, balance, and storage stake. A regular access key advances its nonce and may spend a limited allowance. A gas key uses a separate nonce stream and balance for transaction costs. Newer signed transactions can select monotonic nonce checks or strict sequential nonce checks. Gas-key nonces also select one of the key's nonce lanes. Older signed transactions retain monotonic behavior.
A self-signed universal-account bootstrap is a special transaction shape. It addresses the account derived from its state initialization and proves authority with a key named in that state. The runtime still checks account existence, initialization state, and nonce against shard state.
References: core/…/transaction.rs• L37-52 core/…/transaction.rs• L67-141 core/…/transaction.rs• L207-265 core/…/transaction.rs• L411-492 runtime/…/verifier.rs• L91-159 runtime/…/verifier.rs• L161-259
External contract call
An external contract call is an unsigned transaction envelope addressed to the contract that accepts and pays for the call. It carries a payload instead of a signer, access key, nonce, or signature. The contract's standardized external-call method supplies the authorization result at apply time.
The protocol accepts this call type only when the feature is enabled and the call is within both the general transaction size limit and the external-call size limit. A call may also carry universal-account state initialization. Chunk validation limits the total calls, their combined size share, and repeated calls to the same contract. The envelope has its own versioned encoding so signed transaction encoding remains compatible. A call's stable transaction identity excludes optional state initialization, while its full hash and size include it.
References: core/…/transaction.rs• L555-677 core/…/transaction.rs• L679-804 core/…/transaction.rs• L898-1025
Transaction admission
Admission converts a valid signed transaction into an action receipt and pays the cost of creating and sending that receipt. A signed transaction that fails validation produces a failed outcome and no action receipt. A signed transaction that passes produces a success outcome whose receipt ID points to the work that will run on the receiver account.
The runtime applies the account and access-key charge only after it has a successful verification result. A gas-key transaction can have a separate deposit failure: the gas-key cost is charged while the requested deposit is not applied. This keeps transaction admission distinct from later action execution. An external contract call follows its own validation and immediate-execution path and does not enter this signer and access-key admission flow.
References: core/…/transaction.rs• L322-409 core/…/transaction.rs• L1074-1157 runtime/…/lib.rs• L264-337 runtime/…/lib.rs• L1932-2055 runtime/…/lib.rs• L2056-2291
Receipt lifecycle
Receipt routing
A receipt is a unit of work addressed from a predecessor account to a receiver account. It carries the original signer context, the gas purchase price, ordered actions, and any data needed by those actions. The receiver account determines the shard where the receipt must execute.
Receipts created for the same account can run locally. Receipts for another shard leave the current shard as outgoing work. The runtime can buffer outgoing work when the current chunk cannot send it, then retry it in a later chunk. This asynchronous boundary lets contract calls cross shards without making the full transaction atomic.
References: core/…/receipt.rs• L42-66 core/…/receipt.rs• L342-465 runtime/…/lib.rs• L2175-2225 runtime/…/lib.rs• L1168-1184
Receipt scheduling
The runtime processes local receipts first, then delayed receipts, and then incoming receipts. A chunk-level gas or compute limit can stop receipt work, so unprocessed receipts remain in the delayed queue for a later chunk. Instant receipts handle small state tasks immediately after the receipt that creates them.
The receipt path also enforces receipt limits and outgoing capacity. These limits affect when work runs, not the meaning of a valid receipt. Congestion and bandwidth controls decide which outgoing work can move between shards; the runtime returns the resulting state and outgoing receipts to the chain layer.
The main scheduling order is:
flowchart TD
T[Signed transaction] --> V[Validate and charge]
V -->|valid| R[Create action receipt]
V -->|invalid| F[Failed transaction outcome]
R --> L[Local receipt queue]
R --> O[Outgoing receipt or buffer]
O --> I[Incoming receipt on receiver shard]
L --> A[Check data dependencies]
I --> A
A -->|missing data| P[Postpone receipt]
A -->|all data present| X[Execute actions]
P --> D[Data receipt arrives]
D --> A
X --> E[Execution outcome and new receipts]
E --> O
References: runtime/…/lib.rs• L1781-1883 runtime/…/lib.rs• L2375-2669 runtime/…/lib.rs• L2672-2737 runtime/…/receipt_manager.rs• L101-182
Data dependencies
An action receipt can wait for results from other receipts. The runtime stores a receipt with missing inputs as a postponed receipt and tracks each missing data ID. A data receipt supplies a success value or a failure marker. When the last required value arrives, the runtime resumes the action receipt with all promise results.
The receipt manager builds this promise graph by linking each new action receipt to the data produced by its parent receipts. Yield and resume use the same dependency model for a later continuation. A yielded receipt waits for an explicit resume or for a timeout. A timeout resumes it with no value, which gives the contract a deterministic failed or timed-out input.
References: core/…/receipt.rs• L551-641 runtime/…/lib.rs• L1367-1586 runtime/…/lib.rs• L1589-1657 runtime/…/lib.rs• L3009-3069 runtime/…/receipt_manager.rs• L101-182
Action execution
Account actions
An action receipt runs its ordered actions against one receiver account. Before execution, the runtime applies receipt-wide limits and action rules. It uses stricter checks for newly created receipts while allowing already in-flight receipts to continue under rules that changed later. Account checks and actor permissions then happen before each action. This protects account-owned operations while allowing transfers and function calls to use the predecessor relationship that created the receipt.
The action set covers account creation and deletion, token transfers, staking, access-key changes, contract deployment and selection, delegated actions, deterministic and universal account setup, and gas-key funding and withdrawal. Transfer-based creation follows the account type and the shape of the receipt: named accounts cannot be created by transfer; NEAR-implicit, Ethereum-style implicit, and deterministic accounts require the transfer to be the only action; and universal accounts can be created by a batched transfer because they remain uninitialized until their state is installed. Refunds never create an account. The runtime checks storage stake again before it accepts the resulting account state.
Some delegated actions validate an inner signature and the sender's access key on the receiver shard, then create a new action receipt for the requested work. Their nonce source depends on whether the delegated action uses a regular access key or a gas key. The new receipt reserves execution gas for the receiver and burns the sending cost on the shard that creates it. Receiver-side signature work also counts against the receiver's compute budget. Delegate V2 carries the same nested-work boundary while supporting gas keys.
References: core/…/mod.rs• L375-397 core/…/mod.rs• L430-467 core/…/mod.rs• L583-621 runtime/…/lib.rs• L523-769 runtime/…/actions.rs• L432-513 runtime/…/actions.rs• L553-717 runtime/…/actions.rs• L727-905 runtime/…/action_validation.rs• L73-203 runtime/…/action_validation.rs• L227-262 runtime/…/verifier.rs• L47-89
Contract actions
Contract actions change which code an account uses or call that code. Deployment stores the contract identity on the account. A function call resolves the account's contract and supplies the call context and promise results. The VM runner then returns data, logs, balance and storage changes, gas and compute use, or new receipts. Calls run as internal receipt work, external work, or view work; this mode controls whether unused gas can support child calls and whether the call can create promises. The runtime turns the result into receipt effects, while the VM runner remains the contract execution boundary. See Virtual machine execution.
The call context carries the signer, predecessor, refund target, account balances and storage, attached deposit, prepaid gas, block data, a deterministic random seed, and earlier promise results. Only the final function call in an action batch can publish output data to its declared receivers. Internal calls can create a promise graph through new action and data receipts, and can distribute unused prepaid gas to eligible child calls. External calls use the gas assigned to each child call without this distribution, while view calls cannot create promises. The runtime accepts new receipts only after receipt validation. Their receipt IDs are derived from the parent receipt and their position, so all validators derive the same follow-up work.
An external contract call is not an ordinary FunctionCall action. It can run only through the external-call or view-call mode at the target's standardized method; an ordinary function call to that method fails.
Contract deployment replaces the account's prior contract reference and updates the account's storage use. The runtime prepares deployed code under the active execution rules and can prepare it for scheduled next rules before a protocol boundary. The same contract-related storage accounting also affects whether an account can be deleted, including accounts that refer to a shared contract.
References: core/…/mod.rs• L94-205 core/…/mod.rs• L220-268 core/…/transaction.rs• L562-567 runtime/…/actions.rs• L290-328 runtime/…/actions.rs• L432-513 runtime/…/action_validation.rs• L227-262 runtime/…/action_validation.rs• L468-561 runtime/…/receipt_manager.rs• L197-421 runtime/…/receipt_manager.rs• L675-728 runtime/…/lib.rs• L581-677 runtime/…/function_call.rs• L34-249 runtime/…/function_call.rs• L252-294 runtime/…/function_call.rs• L296-428 runtime/…/lib.rs• L1092-1184
Receipt failure
Actions in one receipt execute in order and stop at the first action error. Receipt-scoped state changes and newly created receipts are kept only when the receipt succeeds. On failure, the runtime rolls back the receipt's state changes while retaining the work and gas accounting needed for the outcome.
This rollback applies to one receipt, not to the full asynchronous transaction graph. Earlier receipts can already have committed state, and later receipts can still arrive with a failure result as their input.
References: runtime/…/lib.rs• L403-493 runtime/…/lib.rs• L891-1034 runtime/…/lib.rs• L1186-1219 runtime/…/function_call.rs• L91-150 core/…/errors.rs• L848-1047
Balances and fees
Account balances
An account keeps available balance, locked stake, storage usage, and its contract identity. Transfers and contract deposits change available balance. Staking moves value into locked balance. Access-key records and contract data increase storage usage, which must remain covered by the account's available and locked value unless the account fits the zero-balance account rule.
An account can point to no contract, local contract code, or a shared global contract identified by a hash or another account. Replacing a contract removes the old contract's storage contribution before adding the new one. Account deletion applies the same contract-aware accounting before it removes the account. The protocol-selected wallet contract can serve Ethereum-style implicit accounts. An uninitialized universal account can hold funds before its state is installed. Universal state initialization can create a missing account, install state in an uninitialized account, or leave an initialized account unchanged. Only the state-init path can turn an uninitialized universal account into a usable initialized account.
References: core/…/account.rs• L70-114 core/…/account.rs• L128-225 core/…/account.rs• L236-410 runtime/…/actions.rs• L220-288 runtime/…/actions.rs• L290-451 runtime/…/actions.rs• L786-905 runtime/…/verifier.rs• L24-89
Gas accounting
Gas pays for both transaction admission and receipt execution. The transaction stage reserves the gas and deposits needed by the receipt. The receipt stage measures gas used and gas burnt for the actions that actually run. Signature verification is part of admission cost. It increases burnt gas and compute without reducing the gas budget attached to the resulting receipt. Inner delegated signatures remain economically charged to the sender; under the active charging rules their compute can be metered on the receiver shard. The receipt keeps its original gas purchase price even if the current gas price changes before execution.
Internal function calls can pass unused prepaid gas to weighted child calls. External calls do not redistribute gas, and view calls do not create child work. When a contract creates several function calls, it can assign gas weights to calls that have no fixed gas allocation. The receipt manager divides the unused gas by those weights and gives the remainder to the final eligible call. This controls how one receipt's remaining gas supports its child calls without changing their execution order.
Universal state initialization charges for the payload size and for the state content it installs. The same fee terms apply when the action comes from a transaction or from contract-created work, so the caller pays for the state it asks the runtime to create. Receipt limits also account for nested state-init entries, committed keys, and gas-key nonces added by actions, including delegated actions whose inner actions run in a child receipt.
Unused gas is returned through a system refund receipt. Failed action receipts also return eligible deposits. A refund can restore a gas-key balance or an access-key allowance when the original key is still available. The outcome reports gas burnt and tokens burnt separately because token cost can include price differences, storage-related charges, deleted gas-key balances, and permanent global-contract storage charges.
External contract calls have no signature cost. Their send and execution fees are burnt before the target method runs. A refused call burns no gas or tokens, but its validation work is reported as compute usage.
References: core/…/mod.rs• L530-621 core/…/fees.rs• L26-48 core/…/fees.rs• L484-623 core/…/fees.rs• L648-725 core/…/fees.rs• L727-771 runtime/…/config.rs• L18-40 runtime/…/config.rs• L296-360 runtime/…/lib.rs• L979-1090 runtime/…/lib.rs• L1222-1364 runtime/…/receipt_manager.rs• L373-421 runtime/…/receipt_manager.rs• L675-728 runtime/…/function_call.rs• L414-426 core/…/transaction.rs• L1223-1234
State commitment
The runtime applies account, contract, receipt, and dependency changes through a temporary state update. It commits successful stages and rolls back failed receipt work. At the end of the chunk it validates the complete update, finalizes the new state root, and returns the state changes with outcomes and outgoing receipts.
The final result is therefore more than a contract return value. It is the combination of the new shard state, receipt work for other shards, validator proposals, processed receipts, and deterministic execution outcomes.
References: runtime/…/lib.rs• L847-851 runtime/…/lib.rs• L1024-1085 runtime/…/lib.rs• L2746-2890
Execution outcomes
Outcome contents
An execution outcome belongs to a signed transaction, an external contract call, or a receipt. Its ID is the transaction hash for a signed transaction or the receipt ID for a receipt. A refused external call uses an outcome identifier supplied by the runtime. The outcome records success or failure, returned data, generated receipt IDs, logs, gas burnt, tokens burnt, the executor account, and optional execution metadata.
Signed transaction outcomes describe admission into the receipt graph. Receipt outcomes describe the actions that ran on a receiver account. External-call outcomes describe whether the contract accepted or refused the call. A success can return a value or a new receipt ID. A failure identifies the execution error and does not make the failed receipt's state changes visible. A refused external call burns no gas or tokens but reports the validation work as compute usage. The execution status also indicates whether the execution changed state: successful and action-failure outcomes count as state-changing, while an invalid transaction changes state only when its error uses the nonce.
Execution errors distinguish invalid transaction input, failed action execution, and runtime-level faults. Invalid transaction errors describe admission or chunk acceptance failures. Action errors describe a failed action or a rejected child receipt. Runtime errors describe faults such as missing state, invalid incoming receipts, validator access errors, or unsafe arithmetic. The first two are part of an execution outcome; runtime-level faults prevent a valid state transition from being committed.
Execution metadata can also carry profiling data and the contract attached to the receiver before each action. This lets consumers relate an action to the code that was active when it ran, including actions that did not call a contract and actions on accounts without a contract.
References: core/…/transaction.rs• L1203-1234 core/…/transaction.rs• L1293-1371 core/…/transaction.rs• L1400-1447 core/…/errors.rs• L15-86 core/…/errors.rs• L256-363 core/…/errors.rs• L848-1047 runtime/…/lib.rs• L1186-1219 runtime/…/lib.rs• L2188-2201
Deterministic result
Determinism comes from using the same protocol version, runtime parameters, pre-state, receipt order, action order, and receipt IDs on every validator. Function calls also derive their random seed from the action and chunk context, and each execution mode has a defined rule for child work and gas. Validation rejects malformed actions and invalid follow-up receipts before they enter the execution graph. State finalization then produces one state root for the accepted transition.
External contract calls add chunk-wide rules to this boundary. The validator and chunk producer use the same limits for call count, call size share, and one call per contract. This keeps accepted external-call sets consistent across validation and packing.
Contract code lookup is part of this deterministic boundary. A missing global contract can become a contract-call failure under the active protocol rules. Missing code for an account that commits to code is instead a state or witness problem, not a valid user result. Nondeterministic runner errors do not produce a committed state transition. View calls can report local compilation failures because they do not change chain state.
Challenge decoding also follows protocol compatibility rules. A challenge that contains an unsigned external call or a V3 action receipt is refused by the current primitives because an older release cannot decode that content. This prevents a challenge from causing different block handling across releases.
The runtime can parallelize read and preparation work, but it commits account and receipt effects through the ordered execution model. This keeps performance work separate from consensus-visible results.
References: core/…/transaction.rs• L641-765 core/…/challenge.rs• L66-140 runtime/…/action_validation.rs• L38-124 runtime/…/lib.rs• L891-951 runtime/…/lib.rs• L2819-2890 runtime/…/function_call.rs• L252-428
Change checklist
When these files or sections are changed, remember to consider these:
- Changes to transaction or action validation must stay aligned with both transaction admission and validation of receipts created by contracts.
- Changes to receipt fields or receipt ID creation must preserve transaction-to-receipt and parent-receipt-to-child provenance.
- Changes to gas, gas weights, or deposit charging must be checked at both admission and receipt execution because refunds cross the two stages.
- Changes to account storage or contract identity must preserve the storage-stake check and the account contract forms used by contract resolution and deletion.
- Changes to account-type recognition or implicit-account creation must preserve protocol gating, receipt-shape restrictions, uninitialized universal-account handling, and the rule that refunds cannot create accounts.
- Changes to delegated action validation must preserve the distinction between signer nonce updates, receiver-side signature compute, attached deposits, and sending fees.
- Changes to signature verification fees must preserve the split between signer-side economic charges and receiver-side compute metering for delegated signatures.
- Changes to postponed, yield, or resume state must keep data IDs, pending counts, timeout entries, and cleanup behavior consistent.
- Changes to receipt execution order or limit handling must preserve the distinction between local, delayed, incoming, buffered, and instant receipts.
- Changes to universal account setup must preserve the distinction between missing, uninitialized, and initialized account state.
- Changes to transfer-based implicit account creation must preserve account-type feature gates, receipt-shape restrictions, and the rule that refunds cannot create accounts.
- Changes to account deletion storage accounting must cover local code and every form of global contract identifier before applying the deletion limit.
- Changes to function-call context must preserve signer, predecessor, refund, deposit, promise-result, execution-mode, and deterministic-randomness semantics.
- Changes to function-call execution modes must preserve the different gas-distribution and promise-creation rules for internal, external, and view calls.
- Changes to function-call result handling must preserve the rule that only the final action can publish output data and that child receipt creation occurs only after successful execution.
- Changes to contract lookup or VM error handling must preserve the distinction between a user-visible call failure, a missing state witness, and a nondeterministic runtime error.
- Changes to transaction envelope encoding must preserve signed transaction compatibility, the separate external-call escape and version markers, and the stable external-call ID without state initialization.
- Changes to external-call limits must keep chunk validation and chunk packing under the same per-contract, count, and size-share rules.
- Changes to external-call outcomes must preserve the distinct outcome identifier and the rule that refused calls report compute without burning gas or tokens.
- Changes to challenge decoding must preserve rejection of challenge payloads that older protocol releases cannot decode.
- Changes to execution status classification must preserve the distinction between validation failures that consume a nonce and those that leave state unchanged.
- Changes to action variants must update nested-action detection, state-init fee counts, gas-key nonce counts, and any protocol feature checks that must classify every variant.
- Changes to execution metadata must preserve the order between receipt actions and the contract identity recorded before each action.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Expired or malformed transaction | The runtime records a failed transaction outcome and creates no action receipt. A validation failure that does not use the nonce leaves state unchanged. | Valid-period filtering and state-free transaction validation. |
| Missing signer account or access key | The transaction cannot spend balance or create work. | Signer and access-key lookup during verification. |
| Invalid nonce, nonce lane, permission, or allowance | The transaction is rejected without its normal state charge. | Nonce, nonce-index, and access-key permission checks. |
| Transfer targets a missing named account or uses a restricted implicit-account receipt shape | The transfer fails instead of creating an account. | Account-type and receipt-shape checks before transfer execution. |
| Transfer targets an account type that is not enabled by the active protocol rules | The account is treated as unavailable and the transfer fails. | Protocol-aware account-type resolution before implicit creation. |
| External call feature is disabled or its payload exceeds a configured size limit | The external call is rejected before contract execution. | Protocol-feature and per-call size validation. |
| A chunk repeats an external call to one contract or exceeds its call count or size share | The chunk's external-call set is invalid. | Shared external-call validity checks used by validation and packing. |
| External call has no target contract or reaches its chunk compute or proof share | The call is refused before the contract method runs. | Contract lookup and shared chunk external-call limits. |
| External call is refused by the target contract | The call reports failure with no gas or token burn, while its validation work counts as compute. | Contract authorization at apply time and refused-call outcome construction. |
| Receipt input data is missing | The action receipt is stored and waits instead of running with incomplete promise results. | Pending data IDs and postponed receipt state. |
| Action error or failed contract call | The receipt's state changes and new receipts are discarded, while the failure appears in the outcome. | Receipt-level rollback and action error status. |
| Contract code is absent for a global contract reference | The function call fails under the enabled missing-global-contract rule. | State lookup and protocol-aware contract resolution. |
| Contract code committed by account state is missing from the execution witness | The runtime returns a storage error instead of committing a possibly incomplete result. | Witness completeness check during contract lookup. |
| Contract runner reports a nondeterministic or unknown state-changing error | The state transition is not committed. | Abort behavior for nondeterministic execution errors. |
| Account no longer covers storage stake | The receipt fails before its account state is accepted. | Post-action storage-stake validation. |
| Account state exceeds the deletion limit after contract overhead is removed | Account deletion fails and the account remains available. | Contract-aware state-size check before removal. |
| Invalid delegated signature, nonce, sender, or permission | The delegated work is rejected and no new action receipt is created. | Inner signature, access-key, expiry, and action-permission checks. |
| Delegated function call carries a forbidden deposit | The delegated action fails instead of proceeding to later key checks or creating work. | Function-call permission validation with an early deposit error. |
| Universal state initialization is malformed or targets an initialized account through a setup-only path | The action fails and does not install the requested account state. | Canonical state-init validation and account initialization checks. |
| Chunk gas, compute, or proof limit is reached | Remaining receipts are delayed and execute in a later chunk. | Local, delayed, and incoming receipt scheduling. |
| Outgoing capacity is unavailable | New cross-shard work stays buffered until it can be sent. | Receipt sink forwarding and buffering. |
| External or view execution is treated as internal execution | Child calls may receive gas or promise behavior that does not match the call mode. | Explicit execution-mode rules at the function-call boundary. |
| A challenge contains an external call or a V3 receipt that an older release cannot decode | Challenge decoding fails instead of allowing incompatible block handling. | Compatibility check during challenge deserialization. |
| A new action variant is omitted from nested-action or fee accounting | State-init charges, gas-key charges, or delegate validation can silently undercount work. | Exhaustive action classification in the shared action helpers. |
Key files
| File | Why to read it |
|---|---|
| runtime/…/lib.rs | Main transaction, receipt, action, refund, rollback, and state-finalization flow. |
| runtime/…/verifier.rs | Signer, access-key, nonce, balance, and storage-stake checks before receipt creation. |
| runtime/…/action_validation.rs | Shared limits and validity rules for transaction actions and contract-created receipts. |
| runtime/…/actions.rs | Account, transfer, contract, staking, delegation, deletion, and permission effects of actions. |
| runtime/…/function_call.rs | Boundary between receipt actions and contract calls, including call context, execution modes, returned data, contract lookup, and new receipts. |
| runtime/…/receipt_manager.rs | High-level model for promise-created action and data receipts, plus weighted gas allocation. |
| runtime/…/config.rs | Gas, execution, sending, deposit, action, and external-call cost and limit model. |
| core/…/mod.rs | Action forms and the shared classification used for nested delegates, state-init fees, and gas-key nonce fees. |
| core/…/transaction.rs | Signed and unsigned transaction envelopes, universal-account bootstrap, validation, external-call chunk rules, and the common execution outcome model. |
| core/…/fees.rs | Shared admission, receipt, external-call, signature-verification, and state-initialization cost model. |
| core/…/errors.rs | Consensus-visible transaction, receipt, action, storage, and runtime failure classes. |
| core/…/receipt.rs | Receipt identity, routing, action/data variants, refunds, and instant-receipt rules. |
| core/…/account.rs | Account balance, locked stake, storage use, initialization, and contract identity. |
| core/…/challenge.rs | Compatibility checks for challenge payloads that include transaction and receipt forms. |