Optimism is an optimistic rollup built on Ethereum. But what exactly is an optimistic rollup, and how does it function at the code level? This article provides a detailed breakdown.
We’ll also explore why rollups require cross-chain communication and how this communication is implemented. Below, we analyze actual code snippets that power Optimism’s core functionalities.
Outline
- What Is an Optimistic Rollup?
- Macro Overview of Optimism’s Contracts
- L1-L2 Bridge: Code Walkthrough
- Rollup Transaction Processing
- Fraud Proofs and Dispute Resolution
What Is an Optimistic Rollup?
A rollup is a Layer 2 (L2) scaling solution that improves Ethereum’s efficiency. Unlike direct L1 transactions, rollups process multiple transactions off-chain, bundle them into a batch, and submit this batch to Ethereum (L1) for finality.
Optimistic rollups operate on a “trust-but-verify” principle:
- Trust: The rollup assumes submitted state updates are valid by default.
- Verify: A challenge window (typically 7 days) allows anyone to dispute fraudulent updates via cryptographic proofs.
💡 Key Benefit: By minimizing on-chain computation, optimistic rollups reduce gas costs and increase throughput.
Macro Overview of Optimism’s Contracts
Optimism’s architecture relies on three core components:
- L1-L2 Bridge: Facilitates asset transfers between Ethereum and Optimism.
- Transaction Rollup: Aggregates L2 transactions into batches for L1 submission.
- Fraud Proofs: Enables disputes against invalid state transitions.
L1-L2 Bridge: Code Walkthrough
The bridge locks assets on L1 and mints equivalents on L2 (or vice versa). Below is the deposit function from L1StandardBridge.sol:
function depositETHTo(
address _to,
uint32 _l2Gas,
bytes calldata _data
) external payable {
_initiateETHDeposit(msg.sender, _to, _l2Gas, _data);
}
function _initiateETHDeposit(
address _from,
address _to,
uint32 _l2Gas,
bytes memory _data
) internal {
bytes memory message = abi.encodeWithSelector(
IL2ERC20Bridge.finalizeDeposit.selector,
address(0),
Lib_PredeployAddresses.OVM_ETH,
_from,
_to,
msg.value,
_data
);
sendCrossDomainMessage(l2TokenBridge, _l2Gas, message);
emit ETHDepositInitiated(_from, _to, msg.value, _data);
}How It Works:
- Accepts ETH deposits (
payable). - Encodes a message for L2 via
sendCrossDomainMessage. - L2’s
L2StandardBridgemints corresponding tokens upon receiving the message.
👉 Learn more about cross-chain messaging
Rollup Transaction Processing
The sequencer (Optimism’s off-chain node) batches L2 transactions and submits them to L1’s CanonicalTransactionChain. Key function:
function _appendBatch(
bytes32 _transactionRoot,
uint256 _batchSize,
uint256 _numQueuedTransactions,
uint40 _timestamp,
uint40 _blockNumber
) internal {
IChainStorageContainer batchesRef = batches();
Lib_OVMCodec.ChainBatchHeader memory header = Lib_OVMCodec.ChainBatchHeader({
batchIndex: batchesRef.length(),
batchRoot: _transactionRoot,
batchSize: _batchSize,
prevTotalElements: totalElements,
extraData: hex""
});
batchesRef.push(batchHeaderHash, latestBatchContext);
}Process:
- Stores batch metadata (root hash, size, timestamp) on L1.
- Uses
batchesReffor efficient data storage.
Fraud Proofs and Dispute Resolution
Invalid state updates can be challenged via OVM_FraudVerifier:
function finalizeFraudVerification(
bytes32 _preStateRoot,
Lib_OVMCodec.ChainBatchHeader memory _preStateRootBatchHeader,
bytes32 _txHash,
bytes32 _postStateRoot,
Lib_OVMCodec.ChainBatchHeader memory _postStateRootBatchHeader
) public {
require(
_postStateRoot != transitioner.getPostStateRoot(),
"State transition is valid."
);
_cancelStateTransition(_postStateRootBatchHeader, _preStateRoot);
}
function _cancelStateTransition(...) internal {
ovmStateCommitmentChain.deleteStateBatch(_postStateRootBatchHeader);
ovmBondManager.finalize(_preStateRoot, publisher, timestamp);
}Key Steps:
- Validators submit proof of invalid state transitions.
- If proven fraudulent, the batch is deleted, and the sequencer’s bond is slashed.
FAQs
1. How long is Optimism’s challenge window?
Currently 7 days. After this period, batches are finalized.
2. Can anyone become a sequencer?
Currently centralized, but Optimism plans to decentralize sequencer roles in the future.
3. What happens if a fraud proof succeeds?
The fraudulent batch is reverted, and the sequencer loses their staked bond.
👉 Explore Optimism’s developer docs
Conclusion
Optimism’s smart contracts enable scalable, low-cost transactions while maintaining Ethereum’s security. By combining optimistic execution, cross-chain bridges, and fraud proofs, it offers a robust L2 solution.
For developers, understanding these mechanisms is crucial for building on Optimism. Stay tuned for further decentralization upgrades!