Skip to content

Execution Framework

Adam Boudj edited this page Jun 5, 2024 · 1 revision

Execution Framework

The Nexus protocol provides a robust framework for executing transactions within smart accounts. This document details the execution process, including security considerations, call types, execution types, execution modes, and the functions involved in handling these executions.

Table of Contents

Execution Overview

The Nexus execution framework allows smart accounts to perform transactions in various modes, providing flexibility and security. This capability is essential for adapting to different use cases and ensuring optimal performance.

Key Concepts

  1. Call Types (CallType):

    • Single: Executes a single transaction.
    • Batch: Executes multiple transactions in a batch.
    • Delegatecall: Executes a delegatecall (optional and should be implemented with caution).
    • Static: Executes a static call.
  2. Execution Types (ExecType):

    • Default (0x00): Reverts the entire operation if any transaction fails.
    • Try (0x01): Attempts to execute each transaction and logs any failures without reverting the entire batch.
  3. Execution Modes (ExecutionMode):

    • Combines call types and execution types with additional context to determine how transactions are handled.

Note

Ensuring the correct execution mode is crucial for the desired transaction behavior. Incorrect configurations can lead to unexpected results.

Call Types (CallType)

The call type determines how the transactions are handled within the execution process.

CallType Description
Single (0x00) Executes a single transaction.
Batch (0x01) Executes multiple transactions in a batch.
Static (0xFE) Executes a static call, which does not alter the state.
Delegatecall (0xFF) Executes a delegatecall, which allows a contract to execute code in another context.

Execution Types (ExecType)

The execution type specifies how errors are handled during transaction execution.

ExecType Description
Default (0x00) Reverts the entire operation if any transaction fails.
Try (0x01) Attempts to execute each transaction and logs any failures without reverting the entire batch.

Execution Modes (ExecutionMode)

Execution modes combine call types and execution types, providing a flexible and context-specific approach to transaction execution.

Structure of ExecutionMode

Field Length Description
CALLTYPE 1 byte Specifies the type of call (single, batch, delegatecall, static).
EXECTYPE 1 byte Specifies how to handle execution failures (default, try).
UNUSED 4 bytes Reserved for future use.
ModeSelector 4 bytes Optional mode selector for custom behavior defined by the account vendor.
ModePayload 22 bytes Additional data passed to the smart account execution, interpreted depending on the ModeSelector.

Tip

Batch execution is useful for performing multiple operations atomically, reducing the need for multiple transaction submissions.

Functions for Execution

Nexus provides several functions to handle execution, each designed for specific use cases:

execute

Function Signature:

function execute(ExecutionMode mode, bytes calldata executionCalldata) external payable onlyEntryPointOrSelf withHook

This function executes transactions based on the specified execution mode and transaction data.

Key Parameters:

  • mode: The execution mode detailing how transactions should be handled (single, batch, default, try/catch).
  • executionCalldata: The encoded transaction data to execute.

Security Considerations:

  • Only callable by the entry point or self, ensuring controlled execution.
  • Includes hook checks for additional security and flexibility.

executeFromExecutor

Function Signature:

function executeFromExecutor(ExecutionMode mode, bytes calldata executionCalldata) external payable onlyExecutorModule withHook returns (bytes[] memory returnData)

This function allows an executor module to perform transactions on behalf of the account.

Key Parameters:

  • mode: The execution mode (single or batch, default or try).
  • executionCalldata: The transaction data to execute.

Return Data:

  • Returns the results of the transaction executions, which may include errors in try mode.

Security Considerations:

  • Callable only by an executor module to maintain integrity.
  • Utilizes hook checks for added security.

executeUserOp

Function Signature:

function executeUserOp(PackedUserOperation calldata userOp, bytes32) external payable virtual onlyEntryPoint

This function executes a user operation via a call using the contract's context.

Key Parameters:

  • userOp: The user operation to execute, containing transaction details.

Security Considerations:

  • Only callable by the entry point, ensuring controlled execution.
  • Decodes the user operation calldata, skipping the first four bytes, and executes the inner call.

Important

Always ensure that the initialization data includes necessary modules and validators to maintain the integrity and security of the Nexus smart account.

Security Considerations

  • Access Control: Ensure that only authorized entities can call execution functions to prevent unauthorized transactions.
  • Error Handling: Proper error handling and logging are essential for diagnosing issues and maintaining system integrity.
  • Gas Optimization: Efficient gas usage is crucial for cost-effective operations, especially in batch executions.

Warning

Always test execution functions in a controlled environment before deploying to production to avoid any potential disruptions.