
Rollup 的核心不是扩容魔法,而是一个取舍:把执行搬到链下,把数据可用性留在链上。OP-Rollup 和 ZK-Rollup 的分歧只有一个——谁来保证链下执行是对的。前者靠"假设有效 + 欺诈证明 + 挑战期",后者靠"数学证明 + 即时验证"。选型不是信仰之争,是延迟、成本、复杂度三者的工程平衡。
维度 | OP-Rollup | ZK-Rollup |
|---|---|---|
最终性 | 7 天挑战期 | 分钟级(证明生成后) |
链上成本 | 低(只提交状态根) | 高(验证 proof) |
开发复杂度 | 低(EVM 兼容直接跑) | 高(电路/证明系统) |
提款延迟 | 长(需挑战期) | 短 |
OP-Rollup 的骨架由排序器 → 状态承诺 → 欺诈证明三部分组成。
1. L2 输出承诺合约
// L2OutputOracle.sol
contract L2OutputOracle {
struct OutputProposal { bytes32 outputRoot; uint128 timestamp; }
mapping(uint256 => OutputProposal) public proposals;
uint256 public nextBlockNumber;
uint256 public constant CHALLENGE_PERIOD = 7 days;
function proposeL2Output(bytes32 _root, uint256 _l2Block)
external onlyProposer
{
require(_l2Block == nextBlockNumber, "non-sequential");
proposals[_l2Block] = OutputProposal(_root, uint128(block.timestamp));
nextBlockNumber++;
emit OutputProposed(_root, _l2Block, block.timestamp);
}
function getL2Output(uint256 _l2Block)
external view returns (OutputProposal memory)
{ return proposals[_l2Block]; }
}2. 排序器:批量执行与提交
# sequencer.py
class Sequencer:
def __init__(self, l1, l2, oracle, batch_size=100, interval=15):
self.l1, self.l2, self.oracle = l1, l2, oracle
self.pending, self.batch_size, self.interval = [], batch_size, interval
self.last = 0
def ingest(self, tx):
self._validate(tx) # 签名/nonce/余额
self.pending.append(tx)
if len(self.pending) >= self.batch_size or time.time() - self.last > self.interval:
self.submit()
def submit(self):
if not self.pending: return
new_root = self._execute_and_hash(self.pending) # 本地执行得新状态根
fn = self.oracle.functions.proposeL2Output(new_root, self.l2_block)
self.l1.eth.send_transaction(fn.build_transaction({
"gas": 200_000, "nonce": self.l1.eth.get_transaction_count(self.addr)}))
self.pending, self.last = [], time.time()3. 欺诈证明:二分法定位争议
contract FaultDisputeGame {
enum Status { IN_PROGRESS, CHALLENGER_WINS, DEFENDER_WINS }
Status public status;
bytes32 public disputedRoot;
uint256 public deadline;
function challenge(bytes32 _root) external {
require(status == Status.IN_PROGRESS, "closed");
disputedRoot = _root;
deadline = block.timestamp + 7 days;
}
// 二分定位到单个指令后由 L1 重放验证
function resolve(uint256 _step, bytes calldata _proof) external {
require(block.timestamp <= deadline, "expired");
if (!_verifyStep(_step, _proof)) {
status = Status.CHALLENGER_WINS;
_slashSequencer(); // 罚没保证金
}
}
}踩坑实录:排序器是单点,宕机即停链。生产必须做多排序器轮换 + 热备切换,且提案权用多签或质押竞价,别用单一 EOA。
ZK-Rollup 的链路更长:交易批次 → 电路约束 → 生成 proof → 链上验证。核心难点在电路,不在合约。
1. 电路:批次状态转换约束(circom)
// batch.circom
pragma circom 2.1.0;
include "poseidon.circom";
template Batch(nTx, nLevels) {
signal input oldRoot;
signal input newRoot;
signal input txHash[nTx];
signal input path[nTx][nLevels];
signal input idx[nTx];
component h[nTx];
for (var i = 0; i < nTx; i++) {
h[i] = Poseidon(2);
h[i].inputs[0] <== txHash[i];
h[i].inputs[1] <== idx[i];
// 约束:每笔交易可沿 path 追溯到 oldRoot
// 累加后得到 newRoot(略去完整 Merkle 更新逻辑)
}
// 最终约束:newRoot == 累加后的根
}
component main = Batch(100, 32);2. 生成证明(snarkjs)
# 编译 + 可信设置 + 生成 proof
circom batch.circom --r1cs --wasm
snarkjs groth16 setup batch.r1cs pot12_final.ptau batch.zkey
snarkjs zkey export verificationkey batch.zkey vk.json
snarkjs groth16 prove batch.zkey witness.wtns proof.json public.json3. 链上验证合约
contract ZKVerifier {
IVerifier public verifier;
bytes32 public stateRoot;
uint256 public batchId;
function submitBatch(
bytes32 _newRoot,
uint256[2] calldata a, uint256[2][2] calldata b, uint256[2] calldata c,
uint256[] calldata pub
) external {
require(pub[0] == uint256(stateRoot), "stale root");
require(pub[1] == uint256(_newRoot), "root mismatch");
require(verifier.verifyProof(a, b, c, pub), "invalid proof");
stateRoot = _newRoot; batchId++;
emit BatchVerified(batchId, _newRoot);
}
}踩坑实录:Groth16 每次电路变更都要重新可信设置,生产建议用 PLONK/Halo2 的可更新设置;证明生成是 CPU/GPU 密集操作,100 笔交易的 proof 在消费级机器上可能要数十秒,证明器必须独立部署并做队列削峰,不能和排序器混跑。
坑 | 后果 | 对策 |
|---|---|---|
排序器单点 | 停链 | 多排序器轮换 + 热备 |
数据未上链 | 状态不可重建 | 强制 calldata/blob 提交 |
挑战期无人监控 | 欺诈通过 | 部署 watcher 自动挑战 |
电路约束遗漏 | 伪造 proof | 形式化验证 + 多轮审计 |
证明器 OOM | 批次卡死 | 队列 + 降批次 + GPU 池 |
要 EVM 兼容、快速上线、成本敏感 → OP-Rollup,接受 7 天提款延迟,把精力放在排序器去中心化和挑战监控上。
要即时最终性、跨链桥低延迟、愿意投入电路开发 → ZK-Rollup,但预算里必须留出证明器硬件和电路审计两项大头。
两者不是替代关系:OP 栈适合通用应用链,ZK 栈适合支付、隐私、高频结算类场景。先想清楚你的用户能不能等 7 天提款,这一个问题就能筛掉一半错误选型。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。