EIPs Explained

ERC-5164 · Last Call · ERC · 2022-06-14

ERC-5164: Cross-Chain Execution

A minimal bridge-agnostic interface for calling a contract on another EVM chain: a MessageDispatcher sends the call, a MessageExecutor runs it once and tells the receiver who sent it.

What problem does it solve?

Protocols deployed on several chains (L1, rollups, sidechains) need to coordinate: a DAO on Ethereum governing contracts on an L2, or a protocol syncing state between deployments. Native and third-party bridges can carry such calls, but each has its own API, trust model and quirks, so every integration is custom code.

ERC-5164 defines a tiny common interface for "execute this call on chain X". Applications write against it once, and swapping the underlying bridge becomes a matter of using a different dispatcher/executor pair.

How it works

There are two components, and an implementation must provide both:

MessageDispatcher (origin chain)

function dispatchMessage(uint256 toChainId, address to, bytes calldata data)
    external payable returns (bytes32 messageId);
event MessageDispatched(bytes32 indexed messageId, address indexed from,
    uint256 indexed toChainId, address to, bytes data);
  • Must revert for unsupported toChainId and must emit MessageDispatched.
  • Must return a messageId unique across chains and dispatchers, e.g. a hash of (chainId, dispatcherAddress, nonce).
  • May charge a fee, which is why it is payable.

MessageExecutor (destination chain)

  • Should verify the message with the bridge's transport layer.
  • Calls to with the original data and appends messageId, fromChainId and from (ABI-packed) to the calldata, so the receiver can authenticate the cross-chain sender.
  • Executes each messageId at most once (MessageIdAlreadyExecuted error otherwise) but makes no ordering guarantee.
  • On failure it must revert (MessageFailure error) so the message can be retried later.
  • Emits MessageIdExecuted(fromChainId, messageId) on success.

Sample code

A receiver that only accepts calls from a known executor and a known sender on a known chain:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

contract CrossChainReceiver {
    address public immutable executor;   // ERC-5164 MessageExecutor on this chain
    uint256 public immutable originChainId;
    address public immutable originSender;
    uint256 public value;

    constructor(address _executor, uint256 _originChainId, address _originSender) {
        executor = _executor;
        originChainId = _originChainId;
        originSender = _originSender;
    }

    function setValue(uint256 v) external {
        require(msg.sender == executor, "not executor");
        (, uint256 fromChainId, address from) = _context();
        require(fromChainId == originChainId && from == originSender, "bad origin");
        value = v;
    }

    // Last 84 bytes of calldata: messageId (32) | fromChainId (32) | from (20)
    function _context() internal pure returns (bytes32 id, uint256 chainId, address from) {
        require(msg.data.length >= 84);
        assembly {
            let end := calldatasize()
            id := calldataload(sub(end, 84))
            chainId := calldataload(sub(end, 52))
            from := shr(96, calldataload(sub(end, 20)))
        }
    }
}

On the origin chain the sender calls dispatcher.dispatchMessage{value: fee}(destChainId, receiver, abi.encodeCall(CrossChainReceiver.setValue, (42))).

Things to know

  • Status is Last Call (deadline 2023-11-15), not Final; the interface could still change.
  • Security is entirely that of the underlying bridge. The standard unifies the API, not the trust model.
  • Always check both msg.sender == executor and the appended (fromChainId, from); otherwise anyone on any chain can trigger your function.
  • Messages may arrive out of order. Design receivers so order does not matter, or add your own sequencing.
  • The text mentions "message batches" but only defines the single-message dispatchMessage.
  • ERC-7786 is a later, Final cross-chain messaging gateway interface worth comparing.

#cross-chain #bridge #messaging #interoperability

Details

Authors
Brendan Asselstine (@asselstine), Pierrick Turelier (@PierrickGT), Chris Whinfrey (@cwhinfrey)
Status
Last Call
Type
Standards Track / ERC
Created
2022-06-14

Machine-readable: Markdown · JSON