Introduction

Hi, I'm cardene, a software engineer and technical writer focused on blockchain and AI.

I've also authored Japanese technical books on specification-driven development with LLMs and decentralized application (DApp) development.

I also build and publish a variety of tools, libraries, and open-source projects.

ERC-5164 defines interfaces for supporting execution across EVM networks.

This article translates, summarizes, and expands on the proposal linked below.

As of October 3, 2023, ERC-5164 was in Review.

Overview

This specification defines how smart contracts on different EVM (Ethereum Virtual Machine) blockchains can interact with one another.

It provides a way for a smart contract on one blockchain to call a smart contract on another.

The specification has two main components. The first is the Message Dispatcher, which resides on the originating chain where the calling smart contract is located. The second is the Message Executor, which resides on the destination chain where the target smart contract is called. When a message is sent, the dispatcher forwards it through a transport layer to the executor, where it is processed. Both components must be implemented to support this specification.

The specification enables smart contracts on different blockchains to interact through two components: the Message Dispatcher and the Message Executor. The dispatcher is on the source chain and sends the message. The executor receives the message and performs the requested operation. This arrangement allows smart contracts on different blockchains to communicate.

Motivation

Many Ethereum protocols need to share information across different EVM-based blockchains. These networks use mechanisms called bridges to connect blockchains and enable Ethereum smart contracts to execute code across them. However, different bridges communicate in different ways and expose different application programming interfaces (APIs). Each protocol therefore requires custom integration work for its bridges. Bridges also have different security, performance, and control characteristics.

The proposal addresses this situation by defining a common, concise specification. Following the specification makes it easier to share code and transmit information across EVM-based blockchains, improves code reuse, and enables common bridge implementations.

Ethereum protocols need to communicate and share information across blockchains, but they do so through bridges with different characteristics. A shared specification simplifies bridge integration, increases code reuse, and makes common bridge implementations easier to build.

Specification

The specification enables smart contracts on one blockchain to send messages to smart contracts on another. It requires two main roles: the MessageDispatcher and the MessageExecutor.

MessageDispatcher

The dispatcher resides on the blockchain where messages originate. It receives messages from smart contracts and sends them to the target blockchain.

MessageExecutor

The executor resides on the blockchain where messages are executed. It receives messages and performs smart contract operations according to their contents.

MessageDispatcher methods

dispatchMessage

The MessageDispatcher resides on the originating blockchain. It broadcasts messages through a transport layer to one or more MessageExecutor contracts.

A unique message ID (messageId) must be generated for each message or batch of messages. The ID must be unique across different blockchains and dispatchers. This is achieved by combining three pieces of information into a tuple and hashing it: chainId (the blockchain identifier), dispatcherAddress (the dispatcher's address), and messageNonce (an integer that increments with each message).

The MessageDispatcher sends messages from the source blockchain through the transport layer and generates a unique ID for each message. A hash function generates this ID to ensure uniqueness across blockchains and dispatchers.

javascript
interface MessageDispatcher {
  function dispatchMessage(uint256 toChainId, address to, bytes calldata data) external payable returns (bytes32 messageId);
}
yaml
- name: dispatchMessage
  type: function
  stateMutability: payable
  inputs:
    - name: toChainId
      type: uint256
    - name: to
      type: address
    - name: data
      type: bytes
  outputs:
    - name: messageId
      type: bytes32

MessageDispatcher events

MessageDispatched

The MessageDispatcher emits the MessageDispatched event whenever an individual message is dispatched.

javascript
interface MessageDispatcher {
  event MessageDispatched(
    bytes32 indexed messageId,
    address indexed from,
    uint256 indexed toChainId,
    address to,
    bytes data,
  );
}
yaml
- name: MessageDispatched
  type: event
  inputs:
    - name: messageId
      indexed: true
      type: bytes32
    - name: from
      indexed: true
      type: address
    - name: toChainId
      indexed: true
      type: uint256
    - name: to
      type: address
    - name: data
      type: bytes

MessageExecutor

The executor executes the dispatched messages or message batches. Developers must implement a MessageExecutor to execute messages on the destination chain.

The MessageExecutor executes a given messageId (message identifier) only once, but provides no guarantee about the execution order of messageIds. Messages or message batches may travel through the transport layer out of order.

Execution

MessageExecutors are advised to verify message data using the bridge's transport layer. This allows them to confirm that a received message is authentic.

MessageExecutors must not execute the same message more than once. In other words, a messageId that has already been executed must not be executed again.

If message execution fails, MessageExecutors roll back the transaction (revert). This allows the message to be retried later: if a problem occurs, the transaction is reverted to its previous state and message execution can be attempted again.

Calldata

MessageExecutors must append the ABI-encoded information (messageId, fromChainId, and from) to the calldata of each message being executed. This allows the recipient to verify the source chain and sender information.

The MessageExecutor executes messages, prevents the same messageId from being executed multiple times, and ensures message authenticity. If execution fails, it reverts the transaction and supplies information that lets recipients verify the origin of messages.

javascript
to.call(abi.encodePacked(data, messageId, fromChainId, from));
yaml
- name: calldata
  type: bytes
  inputs:
    - name: data
      type: bytes
    - name: messageId
      type: bytes32
    - name: fromChainId
      type: uint256
    - name: from
      type: address

MessageExecutor events

MessageIdExecuted

Emitted when a message or message batch has been executed.

javascript
interface MessageExecutor {
  event MessageIdExecuted(
    uint256 indexed fromChainId,
    bytes32 indexed messageId
  );
}
yaml
- name: MessageIdExecuted
  type: event
  inputs:
    - name: fromChainId
      indexed: true
      type: uint256
    - name: messageId
      indexed: true
      type: bytes32

MessageExecutor errors

MessageAlreadyExecuted

Reverts if the messageId has already been executed. The custom error MessageIdAlreadyExecuted is returned.

javascript
interface MessageExecutor {
  error MessageIdAlreadyExecuted(
    bytes32 messageId
  );
}

MessageFailure

Reverts if an individual message fails. The custom error MessageFailure is returned.

javascript
interface MessageExecutor {
  error MessageFailure(
    bytes32 messageId,
    bytes errorData
  );
}

Additional notes

A MessageDispatcher can work with one or more MessageExecutors. How they interact depends on the bridge design. Users can easily bridge a message by calling the dispatchMessage function without needing to know the MessageExecutor address. Clients can also use data from the MessageIdExecuted event to track message execution.

Some bridges require payment in the native currency to bridge messages. The dispatchMessage function must therefore be payable.

The design allows users to pay in the native currency when bridging messages if a payment is required.

Backward compatibility

The specification provides simple cross-chain execution and is compatible with existing governance systems.

Security considerations

Different bridges offer different levels of security. When choosing a bridge, users need to understand how it is implemented. A bridge's security depends on its implementation, and reliability can therefore vary from one bridge to another.

Bridge security deserves careful consideration. Different bridges provide different levels of reliability, so choosing a trustworthy bridge matters. Because security depends on implementation, users should understand how a bridge works and obtain accurate information about its security.

Citation

Brendan Asselstine (@asselstine), Pierrick Turelier (@PierrickGT), Chris Whinfrey (@cwhinfrey), "ERC-5164: Cross-Chain Execution [DRAFT]," Ethereum Improvement Proposals, no. 5164, June 2022. [Online serial]. Available: https://eips.ethereum.org/EIPS/eip-5164.

Conclusion

This article covered ERC-5164, which defines interfaces for supporting execution across EVM networks. I hope you found it useful.

If you have questions, feel free to send me a DM on Twitter.

Twitter @cardene777

I also publish on the following platforms.

https://chaldene.net/