SolidityIn

How rollups work

Lesson 33 of 40 · Layer 2

A Layer 2, or L2, is a separate blockchain that runs transactions cheaply and relies on Ethereum, the Layer 1, for security. A rollup executes many transactions off-chain, then posts a compressed record of them to Ethereum, so anyone can rebuild the L2's state and check it.

There are two main kinds. Optimistic rollups, such as Arbitrum, OP Mainnet and Base, assume results are correct and allow a challenge period, commonly about seven days, in which anyone can submit a fraud proof. Zero-knowledge rollups, such as zkSync, Starknet and Scroll, prove each batch is correct with a validity proof, so withdrawals can be quicker.

Fees on rollups are low mostly because the cost of posting data is shared among many users. Since the Dencun upgrade in March 2024, rollups post their data in cheaper blobs, which cut fees further.

For a developer, most L2s are EVM-compatible, so your Solidity code works unchanged. Differences appear at the edges: gas pricing, block numbers and times, and chain IDs. Include block.chainid in signed messages, so that a signature made on one chain cannot be replayed on another, as in the example.

contract ChainAware {
    uint256 public immutable deployedChain = block.chainid;

    function info() external view returns (uint256 chainId, uint256 time, uint256 gasPrice) {
        return (block.chainid, block.timestamp, tx.gasprice);
    }
    function onDeployedChain() public view returns (bool) {
        return block.chainid == deployedChain;     // false after a chain fork
    }
    // Replay protection: a message hash that is only valid on this chain and contract
    function messageHash(bytes32 data) external view returns (bytes32) {
        return keccak256(abi.encode(block.chainid, address(this), data));
    }
}

Try it yourself

In Remix, deploy ChainAware on a local VM and then on a testnet, and compare the values from info and messageHash. Why does messageHash change between the two?

Press Compile to check your code in the browser. Short fragments are wrapped in a contract automatically.

Quick check: Why include block.chainid in a signed message?