Layer 2 Solutions

ScalingFree Lesson

Advertisement

Layer 2 Solutions

Rollups, state channels, sidechains, and scaling solutions.

Overview

Layer 2 solutions scale Ethereum while inheriting security.

Scaling Approaches

Architecture Diagram
ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”
│           Layer 2 Solutions          │
ā”œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¤
│ Optimistic    │ Zero-Knowledge      │
│ Rollups       │ Rollups             │
ā”œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¤
│ State Channels │ Sidechains         │
ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜

Optimistic Rollups

// Simplified fraud proof
contract OptimisticRollup {
    struct StateUpdate {
        bytes32 stateRoot;
        uint256 blockNumber;
        address submitter;
    }
    
    uint256 public constant CHALLENGE_PERIOD = 7 days;
    
    function submitState(StateUpdate calldata update) public {
        // Store state update
        // Start challenge period
    }
    
    function challenge(uint256 updateId, bytes calldata proof) public {
        // Verify fraud proof
        // Revert invalid state
    }
}

ZK-Rollups

// Simplified ZK proof verification
contract ZKRollup {
    function verifyProof(
        uint[2] calldata a,
        uint[2][2] calldata b,
        uint[2] calldata c,
        uint[4] calldata publicInputs
    ) public view returns (bool) {
        // Verify zk-SNARK proof
        return verifier.verify(a, b, c, publicInputs);
    }
}

State Channels

// Payment channel
contract PaymentChannel {
    address public sender;
    address public receiver;
    uint256 public amount;
    uint256 public expiry;
    
    function open(address _receiver, uint256 _amount) public payable {
        sender = msg.sender;
        receiver = _receiver;
        amount = msg.value;
        expiry = block.timestamp + 1 hours;
    }
    
    function close(bytes calldata signature) public {
        require(block.timestamp < expiry);
        
        // Verify signature
        // Transfer funds
    }
}

Comparison

SolutionTPSSecurityCost
Optimistic2000+EthereumLow
ZK-Rollup3000+EthereumMedium
State Channel10000+EthereumVery Low
Sidechain1000+Own chainLow

Practice

Deploy a simple payment channel contract.

Advertisement

Need Expert Blockchain Help?

Get personalized Web3 training or smart contract consulting.

Advertisement