· 3 min read
Cross-chain without a trusted bridge: building on LayerZero V2
How LayerZero's messaging model works, how I structure an OApp across EVM and Solana, and the failure modes worth designing for before mainnet.
Jose Rai
Senior software engineer
Most cross-chain incidents come from the same place: a bridge that holds funds and trusts a small set of signers. When I needed a protocol to work across several chains, I wanted the opposite. Each chain keeps its own funds and state, and chains only exchange messages that are verified independently. LayerZero V2 is built around that idea. These are my notes from using it in production.
The model in one paragraph
Each chain has an immutable Endpoint contract. Your application, an OApp, sends a message to its Endpoint with a destination endpoint ID. On the other side, a set of Decentralized Verifier Networks (DVNs) that you choose must each confirm the message before it can be delivered, and an Executor calls your OApp's receive function, using the destination gas you paid for up front. The Endpoint handles nonces and replay protection. You decide who verifies your messages, which is the part most teams skip.
A minimal OApp
On EVM, an OApp inherits from OApp and implements sending, receiving and a fee quote. This one broadcasts Merkle roots to other chains:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.22;
import { OApp, Origin, MessagingFee } from "@layerzerolabs/oapp-evm/contracts/oapp/OApp.sol";
import { Ownable } from "@openzeppelin/contracts/access/Ownable.sol";
contract RootSync is OApp {
// source endpoint ID => root => seen
mapping(uint32 => mapping(bytes32 => bool)) public knownRoots;
constructor(address endpoint, address owner) OApp(endpoint, owner) Ownable(owner) {}
function quote(uint32 dstEid, bytes32 root, bytes calldata options)
external view returns (MessagingFee memory)
{
return _quote(dstEid, abi.encode(root), options, false);
}
function broadcastRoot(uint32 dstEid, bytes32 root, bytes calldata options)
external payable onlyOwner
{
_lzSend(dstEid, abi.encode(root), options, MessagingFee(msg.value, 0), payable(msg.sender));
}
function _lzReceive(
Origin calldata origin,
bytes32, /* guid */
bytes calldata payload,
address, /* executor */
bytes calldata /* extraData */
) internal override {
bytes32 root = abi.decode(payload, (bytes32));
knownRoots[origin.srcEid][root] = true; // idempotent: safe to deliver twice
}
}_lzReceive is only reachable through the Endpoint, and only for messages from a peer you registered with setPeer. That peer check is what stops a random contract on another chain from writing into your state, so set peers on both sides. Peers are stored as bytes32, which is how EVM addresses and Solana program IDs share one format.
Quoting and paying for delivery
Delivery on the destination is paid for on the source chain. You build an options blob that says how much gas the Executor should forward, quote the fee, and send it as msg.value. If the gas is too low, the message is verified but the receive call reverts, and it waits in the Endpoint until someone retries it with more gas. Nothing is lost, but users see a stuck transfer, so I measure receive gas on every chain and add headroom.
import { Options } from "@layerzerolabs/lz-v2-utilities";
const options = Options.newOptions()
.addExecutorLzReceiveOption(120_000, 0) // destination gas, destination msg.value
.toHex();
const fee = await rootSync.quote(dstEid, root, options);
await rootSync.broadcastRoot(dstEid, root, options, { value: fee.nativeFee });Solana is a different shape
On Solana the same concepts exist, but an OApp is a program, and the Executor has to know every account your receive instruction touches before it can build the transaction. LayerZero handles this with a separate instruction that returns the accounts needed for a given message, so the receive path needs a fixed, predictable account list. In practice that means keeping cross-chain state in PDAs derived from data in the message, like the source chain and a root or sequence number, rather than in accounts the sender gets to pick.
Designing a cross-chain privacy flow
For a privacy protocol, the tempting design is to move funds between chains. I prefer moving facts. Each chain keeps its own pool and liquidity, and LayerZero carries small, verifiable messages such as new Merkle roots or settlement instructions. A user can deposit on one chain and later prove membership on another, because the destination knows the source root really came from there.
Two things need care. Message contents are public, so never send anything that links a deposit to a withdrawal: roots and fixed denominations are fine, addresses usually aren't. And settlement still needs liquidity on the destination chain, so plan rebalancing as a separate, slower process.
Failure modes to design for
- Stuck messages. Under-quoted gas or a reverting receive leaves a verified message waiting. Make receive idempotent so a retry is always safe, and give operators a view of pending messages.
- Ordering. By default, delivery isn't strictly ordered across retries. If order matters, enforce it with ordered execution or a sequence number in the payload, and handle gaps.
- Security config. Defaults are a starting point. Choose your DVN set and block confirmations per chain on purpose, and treat changes to them like a contract upgrade.
- Ownership. The OApp owner can change peers and configuration. Put it behind a multisig before anything holds value.
LayerZero handles the hardest part, getting a verified message from one chain to another. What it can't do is make your application safe to receive it. That part is ordinary engineering: idempotent handlers, explicit trust settings and a clear plan for when a message arrives late.