From acd550c6b8a549f771cda2470d56129e155c919c Mon Sep 17 00:00:00 2001 From: zimpha Date: Tue, 5 Nov 2024 15:58:34 +0800 Subject: [PATCH] fix unit tests --- hardhat-test/ZkEvmVerifierV2.spec.ts | 6 +- src/L1/rollup/ScrollChain.sol | 17 +- src/mocks/ScrollChainMockFinalize.sol | 16 +- src/test/L1GatewayTestBase.t.sol | 22 +- src/test/ScrollChain.t.sol | 787 +++----------------------- 5 files changed, 116 insertions(+), 732 deletions(-) diff --git a/hardhat-test/ZkEvmVerifierV2.spec.ts b/hardhat-test/ZkEvmVerifierV2.spec.ts index 5eceb1a..93a6235 100644 --- a/hardhat-test/ZkEvmVerifierV2.spec.ts +++ b/hardhat-test/ZkEvmVerifierV2.spec.ts @@ -2,7 +2,7 @@ /* eslint-disable node/no-missing-import */ import { HardhatEthersSigner } from "@nomicfoundation/hardhat-ethers/signers"; import { expect } from "chai"; -import { hexlify, ZeroAddress } from "ethers"; +import { hexlify } from "ethers"; import fs from "fs"; import { ethers } from "hardhat"; @@ -99,7 +99,7 @@ describe("ZkEvmVerifierV2", async () => { layer2ChainId, deployer.address, verifier.getAddress(), - ZeroAddress + verifier.getAddress() ); await admin.upgrade(chainProxy.getAddress(), chainImpl.getAddress()); @@ -134,7 +134,7 @@ describe("ZkEvmVerifierV2", async () => { const withdrawRoot = hexlify(publicInputs.subarray(140, 172)); await chain.setOverrideBatchHashCheck(true); - await chain.setLastFinalizedBatchIndex(lastFinalizedBatchIndex); + await chain.setLastZkpVerifiedBatchIndex(lastFinalizedBatchIndex); await chain.setFinalizedStateRoots(lastFinalizedBatchIndex, prevStateRoot); await chain.setCommittedBatches(lastFinalizedBatchIndex, prevBatchHash); await chain.setCommittedBatches(batchIndex, batchHash); diff --git a/src/L1/rollup/ScrollChain.sol b/src/L1/rollup/ScrollChain.sol index 10e6a39..b9888fb 100644 --- a/src/L1/rollup/ScrollChain.sol +++ b/src/L1/rollup/ScrollChain.sol @@ -138,10 +138,10 @@ contract ScrollChain is OwnableUpgradeable, PausableUpgradeable, IScrollChain { /// @notice The address of L1MessageQueue contract. address public immutable messageQueue; - /// @notice The address of RollupVerifier. - address public immutable verifier; + /// @notice The address of `MultipleVersionRollupVerifier` for zk proof. + address public immutable zkpVerifier; - /// @notice The address of RollupVerifier. + /// @notice The address of `MultipleVersionRollupVerifier` for tee proof. address public immutable teeVerifier; /*********** @@ -219,14 +219,15 @@ contract ScrollChain is OwnableUpgradeable, PausableUpgradeable, IScrollChain { /// /// @param _chainId The chain id of L2. /// @param _messageQueue The address of `L1MessageQueue` contract. - /// @param _verifier The address of zkevm verifier contract. + /// @param _zkpVerifier The address of zkevm verifier contract. + /// @param _teeVerifier The address of tee verifier contract. constructor( uint64 _chainId, address _messageQueue, - address _verifier, + address _zkpVerifier, address _teeVerifier ) { - if (_messageQueue == address(0) || _verifier == address(0)) { + if (_messageQueue == address(0) || _zkpVerifier == address(0) || _teeVerifier == address(0)) { revert ErrorZeroAddress(); } @@ -234,7 +235,7 @@ contract ScrollChain is OwnableUpgradeable, PausableUpgradeable, IScrollChain { layer2ChainId = _chainId; messageQueue = _messageQueue; - verifier = _verifier; + zkpVerifier = _zkpVerifier; teeVerifier = _teeVerifier; } @@ -620,7 +621,7 @@ contract ScrollChain is OwnableUpgradeable, PausableUpgradeable, IScrollChain { // verify bundle, choose the correct verifier based on the last batch // our off-chain service will make sure all unfinalized batches have the same batch version. - IRollupVerifier(verifier).verifyBundleProof(batchVersion, _batchIndex, _aggrProof, _publicInput); + IRollupVerifier(zkpVerifier).verifyBundleProof(batchVersion, _batchIndex, _aggrProof, _publicInput); // store in state // @note we do not store intermediate finalized roots diff --git a/src/mocks/ScrollChainMockFinalize.sol b/src/mocks/ScrollChainMockFinalize.sol index 0596231..5cefdba 100644 --- a/src/mocks/ScrollChainMockFinalize.sol +++ b/src/mocks/ScrollChainMockFinalize.sol @@ -62,21 +62,17 @@ contract ScrollChainMockFinalize is ScrollChain { bytes calldata _batchHeader, bytes32 _postStateRoot, bytes32 _withdrawRoot - ) external OnlyProver whenNotPaused { + ) external OnlyProver whenFinalizeNotPaused { if (_postStateRoot == bytes32(0)) revert ErrorStateRootIsZero(); // compute pending batch hash and verify - ( - uint256 batchPtr, - bytes32 _batchHash, - uint256 _batchIndex, - uint256 _totalL1MessagesPoppedOverall - ) = _loadBatchHeader(_batchHeader); + (, bytes32 _batchHash, uint256 _batchIndex, ) = _loadBatchHeader(_batchHeader); // retrieve finalized state root and batch hash from storage to construct the public input uint256 _finalizedBatchIndex = lastZkpVerifiedBatchIndex; if (_batchIndex <= _finalizedBatchIndex) revert ErrorBatchIsAlreadyVerified(); + /* @note skip verifier in mock bytes memory _publicInput = abi.encodePacked( layer2ChainId, uint32(_batchIndex - _finalizedBatchIndex), // numBatches @@ -87,12 +83,12 @@ contract ScrollChainMockFinalize is ScrollChain { _withdrawRoot ); - // @note skip verifier in mock // load version from batch header, it is always the first byte. - // uint256 batchVersion = BatchHeaderV0Codec.getVersion(batchPtr); + uint256 batchVersion = BatchHeaderV0Codec.getVersion(batchPtr); // verify bundle, choose the correct verifier based on the last batch // our off-chain service will make sure all unfinalized batches have the same batch version. - // IRollupVerifier(verifier).verifyBundleProof(batchVersion, _batchIndex, _aggrProof, _publicInput); + IRollupVerifier(verifier).verifyBundleProof(batchVersion, _batchIndex, _aggrProof, _publicInput); + */ // store in state // @note we do not store intermediate finalized roots diff --git a/src/test/L1GatewayTestBase.t.sol b/src/test/L1GatewayTestBase.t.sol index ea21557..e852775 100644 --- a/src/test/L1GatewayTestBase.t.sol +++ b/src/test/L1GatewayTestBase.t.sol @@ -64,7 +64,8 @@ abstract contract L1GatewayTestBase is ScrollTestBase { EnforcedTxGateway internal enforcedTxGateway; ScrollChainMockBlob internal rollup; - MockRollupVerifier internal verifier; + MockRollupVerifier internal zkpVerifier; + MockRollupVerifier internal teeVerifier; address internal feeVault; Whitelist private whitelist; @@ -91,7 +92,8 @@ abstract contract L1GatewayTestBase is ScrollTestBase { enforcedTxGateway = EnforcedTxGateway(_deployProxy(address(new EnforcedTxGateway()))); gasOracle = L2GasPriceOracle(_deployProxy(address(new L2GasPriceOracle()))); whitelist = new Whitelist(address(this)); - verifier = new MockRollupVerifier(); + zkpVerifier = new MockRollupVerifier(); + teeVerifier = new MockRollupVerifier(); // deploy proxy and contracts in L2 l2Messenger = L2ScrollMessenger(payable(_deployProxy(address(0)))); @@ -124,7 +126,7 @@ abstract contract L1GatewayTestBase is ScrollTestBase { // Upgrade the ScrollChain implementation and initialize admin.upgrade( ITransparentUpgradeableProxy(address(rollup)), - address(new ScrollChainMockBlob(1233, address(messageQueue), address(verifier), address(0))) + address(new ScrollChainMockBlob(1233, address(messageQueue), address(zkpVerifier), address(teeVerifier))) ); rollup.initialize(address(messageQueue), address(0), 44); @@ -161,7 +163,7 @@ abstract contract L1GatewayTestBase is ScrollTestBase { chunk0[0] = bytes1(uint8(1)); // one block in this chunk chunks[0] = chunk0; hevm.startPrank(address(0)); - // rollup.commitBatch(1, batchHeader0, chunks, new bytes(0)); + rollup.commitBatch(1, batchHeader0, chunks, new bytes(0)); hevm.stopPrank(); bytes memory batchHeader1 = new bytes(121); @@ -176,16 +178,8 @@ abstract contract L1GatewayTestBase is ScrollTestBase { } hevm.startPrank(address(0)); - /* - rollup.finalizeBatchWithProof4844( - batchHeader1, - bytes32(uint256(1)), - bytes32(uint256(2)), - messageHash, - blobDataProof, - new bytes(0) - ); - */ + rollup.finalizeBundleWithProof(batchHeader1, bytes32(uint256(2)), messageHash, new bytes(0)); + rollup.finalizeBundleWithTeeProof(batchHeader1, bytes32(uint256(2)), messageHash, new bytes(0)); hevm.stopPrank(); } } diff --git a/src/test/ScrollChain.t.sol b/src/test/ScrollChain.t.sol index 5873c70..e0e0aba 100644 --- a/src/test/ScrollChain.t.sol +++ b/src/test/ScrollChain.t.sol @@ -30,9 +30,15 @@ contract ScrollChainTest is DSTestPlus { event UpdateMaxNumTxInChunk(uint256 oldMaxNumTxInChunk, uint256 newMaxNumTxInChunk); event CommitBatch(uint256 indexed batchIndex, bytes32 indexed batchHash); - event FinalizeBatch(uint256 indexed batchIndex, bytes32 indexed batchHash, bytes32 stateRoot, bytes32 withdrawRoot); event RevertBatch(uint256 indexed batchIndex, bytes32 indexed batchHash); - event FinalizeBatchWithTEEProof(uint256 indexed batchIndex); + event VerifyBatchWithZkp( + uint256 indexed batchIndex, + bytes32 indexed batchHash, + bytes32 stateRoot, + bytes32 withdrawRoot + ); + event VerifyBatchWithTee(uint256 indexed batchIndex); + event FinalizeBatch(uint256 indexed batchIndex, bytes32 indexed batchHash, bytes32 stateRoot, bytes32 withdrawRoot); event StateMismatch(uint256 indexed batchIndex, bytes32 stateRoot, bytes32 withdrawRoot); event ResolveState(uint256 indexed batchIndex, bytes32 stateRoot, bytes32 withdrawRoot); @@ -41,14 +47,16 @@ contract ScrollChainTest is DSTestPlus { ScrollChain private rollup; L1MessageQueue internal messageQueue; - MockRollupVerifier internal verifier; + MockRollupVerifier internal zkpVerifier; + MockRollupVerifier internal teeVerifier; function setUp() public { placeholder = new EmptyContract(); admin = new ProxyAdmin(); messageQueue = L1MessageQueue(_deployProxy(address(0))); rollup = ScrollChain(_deployProxy(address(0))); - verifier = new MockRollupVerifier(); + zkpVerifier = new MockRollupVerifier(); + teeVerifier = new MockRollupVerifier(); // Upgrade the L1MessageQueue implementation and initialize admin.upgrade( @@ -59,9 +67,9 @@ contract ScrollChainTest is DSTestPlus { // Upgrade the ScrollChain implementation and initialize admin.upgrade( ITransparentUpgradeableProxy(address(rollup)), - address(new ScrollChain(233, address(messageQueue), address(verifier), address(0))) + address(new ScrollChain(233, address(messageQueue), address(zkpVerifier), address(teeVerifier))) ); - rollup.initialize(address(messageQueue), address(verifier), 100); + rollup.initialize(address(messageQueue), address(zkpVerifier), 100); } function testInitialized() external { @@ -72,7 +80,6 @@ contract ScrollChainTest is DSTestPlus { rollup.initialize(address(messageQueue), address(0), 100); } - /* function testCommitBatchV1() external { bytes memory batchHeader0 = new bytes(89); @@ -193,8 +200,8 @@ contract ScrollChainTest is DSTestPlus { ScrollChainMockBlob impl = new ScrollChainMockBlob( rollup.layer2ChainId(), rollup.messageQueue(), - rollup.verifier(), - address(0) + rollup.zkpVerifier(), + rollup.teeVerifier() ); admin.upgrade(ITransparentUpgradeableProxy(address(rollup)), address(impl)); // this is keccak(""); @@ -210,431 +217,32 @@ contract ScrollChainTest is DSTestPlus { mstore(add(batchHeader1, add(0x20, 9)), 0) // l1MessagePopped mstore(add(batchHeader1, add(0x20, 17)), 0) // totalL1MessagePopped mstore(add(batchHeader1, add(0x20, 25)), 0x246394445f4fe64ed5598554d55d1682d6fb3fe04bf58eb54ef81d1189fafb51) // dataHash - mstore(add(batchHeader1, add(0x20, 57)), 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470) // blobVersionedHash - mstore(add(batchHeader1, add(0x20, 89)), batchHash0) // parentBatchHash - } - - // commit batch with one chunk, no tx, correctly - chunk0 = new bytes(1 + 60); - chunk0[0] = bytes1(uint8(1)); // one block in this chunk - chunks[0] = chunk0; - hevm.startPrank(address(0)); - assertEq(rollup.committedBatches(1), bytes32(0)); - rollup.commitBatch(1, batchHeader0, chunks, new bytes(0)); - hevm.stopPrank(); - assertEq(rollup.committedBatches(1), keccak256(batchHeader1)); - - // batch is already committed, revert - hevm.startPrank(address(0)); - hevm.expectRevert(ScrollChain.ErrorBatchIsAlreadyCommitted.selector); - rollup.commitBatch(1, batchHeader0, chunks, new bytes(0)); - hevm.stopPrank(); - - // revert when ErrorIncorrectBatchVersion - hevm.startPrank(address(0)); - hevm.expectRevert(ScrollChain.ErrorIncorrectBatchVersion.selector); - rollup.commitBatch(3, batchHeader1, chunks, new bytes(0)); - hevm.stopPrank(); - } - */ - - /* - function testFinalizeBatchWithProof4844() external { - // caller not prover, revert - hevm.expectRevert(ScrollChain.ErrorCallerIsNotProver.selector); - rollup.finalizeBatchWithProof4844(new bytes(0), bytes32(0), bytes32(0), bytes32(0), new bytes(0), new bytes(0)); - - rollup.addProver(address(0)); - rollup.addSequencer(address(0)); - - bytes memory batchHeader0 = new bytes(89); - - // import genesis batch - assembly { - mstore(add(batchHeader0, add(0x20, 25)), 1) - } - rollup.importGenesisBatch(batchHeader0, bytes32(uint256(1))); - - bytes[] memory chunks = new bytes[](1); - bytes memory chunk0; - - // upgrade to ScrollChainMockBlob - ScrollChainMockBlob impl = new ScrollChainMockBlob( - rollup.layer2ChainId(), - rollup.messageQueue(), - rollup.verifier(), - address(0) - ); - admin.upgrade(ITransparentUpgradeableProxy(address(rollup)), address(impl)); - // from https://etherscan.io/blob/0x013590dc3544d56629ba81bb14d4d31248f825001653aa575eb8e3a719046757?bid=740652 - bytes32 blobVersionedHash = 0x013590dc3544d56629ba81bb14d4d31248f825001653aa575eb8e3a719046757; - bytes - memory blobDataProof = hex"2c9d777660f14ad49803a6442935c0d24a0d83551de5995890bf70a17d24e68753ab0fe6807c7081f0885fe7da741554d658a03730b1fa006f8319f8b993bcb0a5a0c9e8a145c5ef6e415c245690effa2914ec9393f58a7251d30c0657da1453d9ad906eae8b97dd60c9a216f81b4df7af34d01e214e1ec5865f0133ecc16d7459e49dab66087340677751e82097fbdd20551d66076f425775d1758a9dfd186b"; - ScrollChainMockBlob(address(rollup)).setBlobVersionedHash(blobVersionedHash); - - bytes32 batchHash0 = rollup.committedBatches(0); - bytes memory batchHeader1 = new bytes(121); - assembly { - mstore8(add(batchHeader1, 0x20), 1) // version - mstore(add(batchHeader1, add(0x20, 1)), shl(192, 1)) // batchIndex - mstore(add(batchHeader1, add(0x20, 9)), 0) // l1MessagePopped - mstore(add(batchHeader1, add(0x20, 17)), 0) // totalL1MessagePopped - mstore(add(batchHeader1, add(0x20, 25)), 0x246394445f4fe64ed5598554d55d1682d6fb3fe04bf58eb54ef81d1189fafb51) // dataHash - mstore(add(batchHeader1, add(0x20, 57)), blobVersionedHash) // blobVersionedHash - mstore(add(batchHeader1, add(0x20, 89)), batchHash0) // parentBatchHash - } - // batch hash is 0xf7d9af8c2c8e1a84f1fa4b6af9425f85c50a61b24cdd28101a5f6d781906a5b9 - - // commit one batch - chunk0 = new bytes(1 + 60); - chunk0[0] = bytes1(uint8(1)); // one block in this chunk - chunks[0] = chunk0; - hevm.startPrank(address(0)); - rollup.commitBatch(1, batchHeader0, chunks, new bytes(0)); - hevm.stopPrank(); - assertEq(rollup.committedBatches(1), keccak256(batchHeader1)); - - // incorrect batch hash, revert - batchHeader1[1] = bytes1(uint8(1)); // change random byte - hevm.startPrank(address(0)); - hevm.expectRevert(ScrollChain.ErrorIncorrectBatchHash.selector); - rollup.finalizeBatchWithProof4844( - batchHeader1, - bytes32(uint256(1)), - bytes32(uint256(2)), - bytes32(0), - new bytes(0), - new bytes(0) - ); - hevm.stopPrank(); - batchHeader1[1] = bytes1(uint8(0)); // change back - - // batch header length too small, revert - bytes memory header = new bytes(120); - assembly { - mstore8(add(header, 0x20), 1) // version - } - hevm.startPrank(address(0)); - hevm.expectRevert(BatchHeaderV1Codec.ErrorBatchHeaderV1LengthTooSmall.selector); - rollup.finalizeBatchWithProof4844( - header, - bytes32(uint256(1)), - bytes32(uint256(2)), - bytes32(0), - new bytes(0), - new bytes(0) - ); - hevm.stopPrank(); - - // wrong bitmap length, revert - header = new bytes(122); - assembly { - mstore8(add(header, 0x20), 1) // version - } - hevm.startPrank(address(0)); - hevm.expectRevert(BatchHeaderV1Codec.ErrorIncorrectBitmapLengthV1.selector); - rollup.finalizeBatchWithProof4844( - header, - bytes32(uint256(1)), - bytes32(uint256(2)), - bytes32(0), - new bytes(0), - new bytes(0) - ); - hevm.stopPrank(); - - // verify success - assertBoolEq(rollup.isBatchFinalized(1), false); - hevm.startPrank(address(0)); - rollup.finalizeBatchWithProof4844( - batchHeader1, - bytes32(uint256(1)), - bytes32(uint256(2)), - bytes32(uint256(3)), - blobDataProof, - new bytes(0) - ); - hevm.stopPrank(); - assertBoolEq(rollup.isBatchFinalized(1), true); - assertEq(rollup.finalizedStateRoots(1), bytes32(uint256(2))); - assertEq(rollup.withdrawRoots(1), bytes32(uint256(3))); - assertEq(rollup.lastFinalizedBatchIndex(), 1); - - // batch already verified, revert - hevm.startPrank(address(0)); - hevm.expectRevert(ScrollChain.ErrorBatchIsAlreadyVerified.selector); - rollup.finalizeBatchWithProof4844( - batchHeader1, - bytes32(uint256(1)), - bytes32(uint256(2)), - bytes32(uint256(3)), - blobDataProof, - new bytes(0) - ); - hevm.stopPrank(); - } - */ - - /* - function testCommitAndFinalizeWithL1MessagesV1() external { - rollup.addSequencer(address(0)); - rollup.addProver(address(0)); - - // import 300 L1 messages - for (uint256 i = 0; i < 300; i++) { - messageQueue.appendCrossDomainMessage(address(this), 1000000, new bytes(0)); - } - - // import genesis batch first - bytes memory batchHeader0 = new bytes(89); - assembly { - mstore(add(batchHeader0, add(0x20, 25)), 1) - } - rollup.importGenesisBatch(batchHeader0, bytes32(uint256(1))); - bytes32 batchHash0 = rollup.committedBatches(0); - - // upgrade to ScrollChainMockBlob - ScrollChainMockBlob impl = new ScrollChainMockBlob( - rollup.layer2ChainId(), - rollup.messageQueue(), - rollup.verifier(), - address(0) - ); - admin.upgrade(ITransparentUpgradeableProxy(address(rollup)), address(impl)); - // from https://etherscan.io/blob/0x013590dc3544d56629ba81bb14d4d31248f825001653aa575eb8e3a719046757?bid=740652 - bytes32 blobVersionedHash = 0x013590dc3544d56629ba81bb14d4d31248f825001653aa575eb8e3a719046757; - bytes - memory blobDataProof = hex"2c9d777660f14ad49803a6442935c0d24a0d83551de5995890bf70a17d24e68753ab0fe6807c7081f0885fe7da741554d658a03730b1fa006f8319f8b993bcb0a5a0c9e8a145c5ef6e415c245690effa2914ec9393f58a7251d30c0657da1453d9ad906eae8b97dd60c9a216f81b4df7af34d01e214e1ec5865f0133ecc16d7459e49dab66087340677751e82097fbdd20551d66076f425775d1758a9dfd186b"; - ScrollChainMockBlob(address(rollup)).setBlobVersionedHash(blobVersionedHash); - - bytes memory bitmap; - bytes[] memory chunks; - bytes memory chunk0; - bytes memory chunk1; - - // commit batch1, one chunk with one block, 1 tx, 1 L1 message, no skip - // => payload for data hash of chunk0 - // 0000000000000000 - // 0000000000000000 - // 0000000000000000000000000000000000000000000000000000000000000000 - // 0000000000000000 - // 0001 - // a2277fd30bbbe74323309023b56035b376d7768ad237ae4fc46ead7dc9591ae1 - // => data hash for chunk0 - // 9ef1e5694bdb014a1eea42be756a8f63bfd8781d6332e9ef3b5126d90c62f110 - // => data hash for all chunks - // d9cb6bf9264006fcea490d5c261f7453ab95b1b26033a3805996791b8e3a62f3 - // => payload for batch header - // 01 - // 0000000000000001 - // 0000000000000001 - // 0000000000000001 - // d9cb6bf9264006fcea490d5c261f7453ab95b1b26033a3805996791b8e3a62f3 - // 013590dc3544d56629ba81bb14d4d31248f825001653aa575eb8e3a719046757 - // 119b828c2a2798d2c957228ebeaff7e10bb099ae0d4e224f3eeb779ff61cba61 - // 0000000000000000000000000000000000000000000000000000000000000000 - // => hash for batch header - // 66b68a5092940d88a8c6f203d2071303557c024275d8ceaa2e12662bc61c8d8f - bytes memory batchHeader1 = new bytes(121 + 32); - assembly { - mstore8(add(batchHeader1, 0x20), 1) // version - mstore(add(batchHeader1, add(0x20, 1)), shl(192, 1)) // batchIndex = 1 - mstore(add(batchHeader1, add(0x20, 9)), shl(192, 1)) // l1MessagePopped = 1 - mstore(add(batchHeader1, add(0x20, 17)), shl(192, 1)) // totalL1MessagePopped = 1 - mstore(add(batchHeader1, add(0x20, 25)), 0xd9cb6bf9264006fcea490d5c261f7453ab95b1b26033a3805996791b8e3a62f3) // dataHash - mstore(add(batchHeader1, add(0x20, 57)), blobVersionedHash) // blobVersionedHash - mstore(add(batchHeader1, add(0x20, 89)), batchHash0) // parentBatchHash - mstore(add(batchHeader1, add(0x20, 121)), 0) // bitmap0 - } - chunk0 = new bytes(1 + 60); - assembly { - mstore(add(chunk0, 0x20), shl(248, 1)) // numBlocks = 1 - mstore(add(chunk0, add(0x21, 56)), shl(240, 1)) // numTransactions = 1 - mstore(add(chunk0, add(0x21, 58)), shl(240, 1)) // numL1Messages = 1 - } - chunks = new bytes[](1); - chunks[0] = chunk0; - bitmap = new bytes(32); - hevm.startPrank(address(0)); - hevm.expectEmit(true, true, false, true); - emit CommitBatch(1, keccak256(batchHeader1)); - rollup.commitBatch(1, batchHeader0, chunks, bitmap); - hevm.stopPrank(); - assertBoolEq(rollup.isBatchFinalized(1), false); - bytes32 batchHash1 = rollup.committedBatches(1); - assertEq(batchHash1, keccak256(batchHeader1)); - - // finalize batch1 - hevm.startPrank(address(0)); - hevm.expectEmit(true, true, false, true); - emit FinalizeBatch(1, batchHash1, bytes32(uint256(2)), bytes32(uint256(3))); - rollup.finalizeBatchWithProof4844( - batchHeader1, - bytes32(uint256(1)), - bytes32(uint256(2)), - bytes32(uint256(3)), - blobDataProof, - new bytes(0) - ); - hevm.stopPrank(); - assertBoolEq(rollup.isBatchFinalized(1), true); - assertEq(rollup.finalizedStateRoots(1), bytes32(uint256(2))); - assertEq(rollup.withdrawRoots(1), bytes32(uint256(3))); - assertEq(rollup.lastFinalizedBatchIndex(), 1); - assertBoolEq(messageQueue.isMessageSkipped(0), false); - assertEq(messageQueue.pendingQueueIndex(), 1); - - // commit batch2 with two chunks, correctly - // 1. chunk0 has one block, 3 tx, no L1 messages - // => payload for chunk0 - // 0000000000000000 - // 0000000000000000 - // 0000000000000000000000000000000000000000000000000000000000000000 - // 0000000000000000 - // 0003 - // ... (some tx hashes) - // => data hash for chunk0 - // c4e0d99a191bfcb1ba2edd2964a0f0a56c929b1ecdf149ba3ae4f045d6e6ef8b - // 2. chunk1 has three blocks - // 2.1 block0 has 5 tx, 3 L1 messages, no skips - // 2.2 block1 has 10 tx, 5 L1 messages, even is skipped, last is not skipped - // 2.2 block1 has 300 tx, 256 L1 messages, odd position is skipped, last is not skipped - // => payload for chunk1 - // 0000000000000000 - // 0000000000000000 - // 0000000000000000000000000000000000000000000000000000000000000000 - // 0000000000000000 - // 0005 - // 0000000000000000 - // 0000000000000000 - // 0000000000000000000000000000000000000000000000000000000000000000 - // 0000000000000000 - // 000a - // 0000000000000000 - // 0000000000000000 - // 0000000000000000000000000000000000000000000000000000000000000000 - // 0000000000000000 - // 012c - // => data hash for chunk2 - // a84759a83bba5f73e3a748d138ae7b6c5a31a8a5273aeb0e578807bf1ef6ed4e - // => data hash for all chunks - // dae89323bf398ca9f6f8e83b1b0d603334be063fa3920015b6aa9df77a0ccbcd - // => payload for batch header - // 01 - // 0000000000000002 - // 0000000000000108 - // 0000000000000109 - // dae89323bf398ca9f6f8e83b1b0d603334be063fa3920015b6aa9df77a0ccbcd - // 013590dc3544d56629ba81bb14d4d31248f825001653aa575eb8e3a719046757 - // 66b68a5092940d88a8c6f203d2071303557c024275d8ceaa2e12662bc61c8d8f - // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa28000000000000000000000000000000000000000000000000000000000000002a - // => hash for batch header - // b9dff5d21381176a73b20a9294eb2703c803113f9559e358708c659fa1cf62eb - bytes memory batchHeader2 = new bytes(121 + 32 + 32); - assembly { - mstore8(add(batchHeader2, 0x20), 1) // version - mstore(add(batchHeader2, add(0x20, 1)), shl(192, 2)) // batchIndex = 2 - mstore(add(batchHeader2, add(0x20, 9)), shl(192, 264)) // l1MessagePopped = 264 - mstore(add(batchHeader2, add(0x20, 17)), shl(192, 265)) // totalL1MessagePopped = 265 - mstore(add(batchHeader2, add(0x20, 25)), 0xdae89323bf398ca9f6f8e83b1b0d603334be063fa3920015b6aa9df77a0ccbcd) // dataHash - mstore(add(batchHeader2, add(0x20, 57)), blobVersionedHash) // blobVersionedHash - mstore(add(batchHeader2, add(0x20, 89)), batchHash1) // parentBatchHash - mstore( - add(batchHeader2, add(0x20, 121)), - 77194726158210796949047323339125271902179989777093709359638389338608753093160 - ) // bitmap0 - mstore(add(batchHeader2, add(0x20, 153)), 42) // bitmap1 - } - chunk0 = new bytes(1 + 60); - assembly { - mstore(add(chunk0, 0x20), shl(248, 1)) // numBlocks = 1 - mstore(add(chunk0, add(0x21, 56)), shl(240, 3)) // numTransactions = 3 - mstore(add(chunk0, add(0x21, 58)), shl(240, 0)) // numL1Messages = 0 - } - chunk1 = new bytes(1 + 60 * 3); - assembly { - mstore(add(chunk1, 0x20), shl(248, 3)) // numBlocks = 3 - mstore(add(chunk1, add(33, 56)), shl(240, 5)) // block0.numTransactions = 5 - mstore(add(chunk1, add(33, 58)), shl(240, 3)) // block0.numL1Messages = 3 - mstore(add(chunk1, add(93, 56)), shl(240, 10)) // block1.numTransactions = 10 - mstore(add(chunk1, add(93, 58)), shl(240, 5)) // block1.numL1Messages = 5 - mstore(add(chunk1, add(153, 56)), shl(240, 300)) // block1.numTransactions = 300 - mstore(add(chunk1, add(153, 58)), shl(240, 256)) // block1.numL1Messages = 256 - } - chunks = new bytes[](2); - chunks[0] = chunk0; - chunks[1] = chunk1; - bitmap = new bytes(64); - assembly { - mstore( - add(bitmap, add(0x20, 0)), - 77194726158210796949047323339125271902179989777093709359638389338608753093160 - ) // bitmap0 - mstore(add(bitmap, add(0x20, 32)), 42) // bitmap1 - } - - // too many txs in one chunk, revert - rollup.updateMaxNumTxInChunk(2); // 3 - 1 - hevm.startPrank(address(0)); - hevm.expectRevert(ScrollChain.ErrorTooManyTxsInOneChunk.selector); - rollup.commitBatch(1, batchHeader1, chunks, bitmap); // first chunk with too many txs - hevm.stopPrank(); - rollup.updateMaxNumTxInChunk(185); // 5+10+300 - 2 - 127 - hevm.startPrank(address(0)); - hevm.expectRevert(ScrollChain.ErrorTooManyTxsInOneChunk.selector); - rollup.commitBatch(1, batchHeader1, chunks, bitmap); // second chunk with too many txs - hevm.stopPrank(); - - rollup.updateMaxNumTxInChunk(186); - hevm.startPrank(address(0)); - hevm.expectEmit(true, true, false, true); - emit CommitBatch(2, keccak256(batchHeader2)); - rollup.commitBatch(1, batchHeader1, chunks, bitmap); - hevm.stopPrank(); - assertBoolEq(rollup.isBatchFinalized(2), false); - bytes32 batchHash2 = rollup.committedBatches(2); - assertEq(batchHash2, keccak256(batchHeader2)); - - // verify committed batch correctly - hevm.startPrank(address(0)); - hevm.expectEmit(true, true, false, true); - emit FinalizeBatch(2, batchHash2, bytes32(uint256(4)), bytes32(uint256(5))); - rollup.finalizeBatchWithProof4844( - batchHeader2, - bytes32(uint256(2)), - bytes32(uint256(4)), - bytes32(uint256(5)), - blobDataProof, - new bytes(0) - ); - hevm.stopPrank(); - assertBoolEq(rollup.isBatchFinalized(2), true); - assertEq(rollup.finalizedStateRoots(2), bytes32(uint256(4))); - assertEq(rollup.withdrawRoots(2), bytes32(uint256(5))); - assertEq(rollup.lastFinalizedBatchIndex(), 2); - assertEq(messageQueue.pendingQueueIndex(), 265); - // 1 ~ 4, zero - for (uint256 i = 1; i < 4; i++) { - assertBoolEq(messageQueue.isMessageSkipped(i), false); - } - // 4 ~ 9, even is nonzero, odd is zero - for (uint256 i = 4; i < 9; i++) { - if (i % 2 == 1 || i == 8) { - assertBoolEq(messageQueue.isMessageSkipped(i), false); - } else { - assertBoolEq(messageQueue.isMessageSkipped(i), true); - } - } - // 9 ~ 265, even is nonzero, odd is zero - for (uint256 i = 9; i < 265; i++) { - if (i % 2 == 1 || i == 264) { - assertBoolEq(messageQueue.isMessageSkipped(i), false); - } else { - assertBoolEq(messageQueue.isMessageSkipped(i), true); - } + mstore(add(batchHeader1, add(0x20, 57)), 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470) // blobVersionedHash + mstore(add(batchHeader1, add(0x20, 89)), batchHash0) // parentBatchHash } + + // commit batch with one chunk, no tx, correctly + chunk0 = new bytes(1 + 60); + chunk0[0] = bytes1(uint8(1)); // one block in this chunk + chunks[0] = chunk0; + hevm.startPrank(address(0)); + assertEq(rollup.committedBatches(1), bytes32(0)); + rollup.commitBatch(1, batchHeader0, chunks, new bytes(0)); + hevm.stopPrank(); + assertEq(rollup.committedBatches(1), keccak256(batchHeader1)); + + // batch is already committed, revert + hevm.startPrank(address(0)); + hevm.expectRevert(ScrollChain.ErrorBatchIsAlreadyCommitted.selector); + rollup.commitBatch(1, batchHeader0, chunks, new bytes(0)); + hevm.stopPrank(); + + // revert when ErrorIncorrectBatchVersion + hevm.startPrank(address(0)); + hevm.expectRevert(ScrollChain.ErrorIncorrectBatchVersion.selector); + rollup.commitBatch(3, batchHeader1, chunks, new bytes(0)); + hevm.stopPrank(); } - */ function testCommitBatchV3() external { bytes memory batchHeader0 = new bytes(89); @@ -751,8 +359,8 @@ contract ScrollChainTest is DSTestPlus { ScrollChainMockBlob impl = new ScrollChainMockBlob( rollup.layer2ChainId(), rollup.messageQueue(), - rollup.verifier(), - address(0) + rollup.zkpVerifier(), + rollup.teeVerifier() ); admin.upgrade(ITransparentUpgradeableProxy(address(rollup)), address(impl)); // from https://etherscan.io/blob/0x013590dc3544d56629ba81bb14d4d31248f825001653aa575eb8e3a719046757?bid=740652 @@ -818,8 +426,8 @@ contract ScrollChainTest is DSTestPlus { ScrollChainMockBlob impl = new ScrollChainMockBlob( rollup.layer2ChainId(), rollup.messageQueue(), - rollup.verifier(), - address(0) + rollup.zkpVerifier(), + rollup.teeVerifier() ); admin.upgrade(ITransparentUpgradeableProxy(address(rollup)), address(impl)); // from https://etherscan.io/blob/0x013590dc3544d56629ba81bb14d4d31248f825001653aa575eb8e3a719046757?bid=740652 @@ -882,13 +490,15 @@ contract ScrollChainTest is DSTestPlus { // verify success assertBoolEq(rollup.isBatchFinalized(1), false); + assertEq(rollup.lastZkpVerifiedBatchIndex(), 0); hevm.startPrank(address(0)); rollup.finalizeBundleWithProof(batchHeader1, bytes32(uint256(2)), bytes32(uint256(3)), new bytes(0)); hevm.stopPrank(); - assertBoolEq(rollup.isBatchFinalized(1), true); + assertBoolEq(rollup.isBatchFinalized(1), false); assertEq(rollup.finalizedStateRoots(1), bytes32(uint256(2))); assertEq(rollup.withdrawRoots(1), bytes32(uint256(3))); - assertEq(rollup.lastFinalizedBatchIndex(), 1); + assertEq(rollup.lastFinalizedBatchIndex(), 0); + assertEq(rollup.lastZkpVerifiedBatchIndex(), 1); // revert when ErrorBatchIsAlreadyVerified hevm.startPrank(address(0)); @@ -913,12 +523,11 @@ contract ScrollChainTest is DSTestPlus { rollup.importGenesisBatch(batchHeader0, bytes32(uint256(1))); // upgrade to ScrollChainMockBlob - MockRollupVerifier sgxVerifier = new MockRollupVerifier(); ScrollChainMockBlob impl = new ScrollChainMockBlob( rollup.layer2ChainId(), rollup.messageQueue(), - rollup.verifier(), - address(sgxVerifier) + rollup.zkpVerifier(), + rollup.teeVerifier() ); admin.upgrade(ITransparentUpgradeableProxy(address(rollup)), address(impl)); // from https://etherscan.io/blob/0x013590dc3544d56629ba81bb14d4d31248f825001653aa575eb8e3a719046757?bid=740652 @@ -957,13 +566,16 @@ contract ScrollChainTest is DSTestPlus { // finalize with zk proof assertBoolEq(rollup.isBatchFinalized(1), false); + assertEq(rollup.lastFinalizedBatchIndex(), 0); + hevm.expectEmit(true, false, false, true); + emit VerifyBatchWithZkp(1, keccak256(batchHeader1), bytes32(uint256(2)), bytes32(uint256(3))); hevm.startPrank(address(0)); rollup.finalizeBundleWithProof(batchHeader1, bytes32(uint256(2)), bytes32(uint256(3)), new bytes(0)); hevm.stopPrank(); - assertBoolEq(rollup.isBatchFinalized(1), true); + assertBoolEq(rollup.isBatchFinalized(1), false); assertEq(rollup.finalizedStateRoots(1), bytes32(uint256(2))); assertEq(rollup.withdrawRoots(1), bytes32(uint256(3))); - assertEq(rollup.lastFinalizedBatchIndex(), 1); + assertEq(rollup.lastFinalizedBatchIndex(), 0); // revert, when ErrorCallerIsNotProver hevm.expectRevert(ScrollChain.ErrorCallerIsNotProver.selector); @@ -997,13 +609,18 @@ contract ScrollChainTest is DSTestPlus { hevm.stopPrank(); // succeed on batch 1 - hevm.startPrank(address(0)); hevm.expectEmit(true, false, false, true); - emit FinalizeBatchWithTEEProof(1); + emit VerifyBatchWithTee(1); + hevm.expectEmit(true, false, false, true); + emit FinalizeBatch(1, keccak256(batchHeader1), bytes32(uint256(2)), bytes32(uint256(3))); assertEq(rollup.lastTeeVerifiedBatchIndex(), 0); + assertEq(rollup.lastFinalizedBatchIndex(), 0); + hevm.startPrank(address(0)); rollup.finalizeBundleWithTeeProof(batchHeader1, bytes32(uint256(2)), bytes32(uint256(3)), new bytes(0)); - assertEq(rollup.lastTeeVerifiedBatchIndex(), 1); hevm.stopPrank(); + assertEq(rollup.lastTeeVerifiedBatchIndex(), 1); + assertEq(rollup.lastFinalizedBatchIndex(), 1); + assertBoolEq(rollup.isBatchFinalized(1), true); // revert when ErrorBatchIsAlreadyVerified hevm.startPrank(address(0)); @@ -1024,12 +641,11 @@ contract ScrollChainTest is DSTestPlus { rollup.importGenesisBatch(batchHeader0, bytes32(uint256(1))); // upgrade to ScrollChainMockBlob - MockRollupVerifier sgxVerifier = new MockRollupVerifier(); ScrollChainMockBlob impl = new ScrollChainMockBlob( rollup.layer2ChainId(), rollup.messageQueue(), - rollup.verifier(), - address(sgxVerifier) + rollup.zkpVerifier(), + rollup.teeVerifier() ); admin.upgrade(ITransparentUpgradeableProxy(address(rollup)), address(impl)); // from https://etherscan.io/blob/0x013590dc3544d56629ba81bb14d4d31248f825001653aa575eb8e3a719046757?bid=740652 @@ -1067,14 +683,9 @@ contract ScrollChainTest is DSTestPlus { assertEq(rollup.committedBatches(1), keccak256(batchHeader1)); // finalize with zk proof - assertBoolEq(rollup.isBatchFinalized(1), false); hevm.startPrank(address(0)); rollup.finalizeBundleWithProof(batchHeader1, bytes32(uint256(2)), bytes32(uint256(3)), new bytes(0)); hevm.stopPrank(); - assertBoolEq(rollup.isBatchFinalized(1), true); - assertEq(rollup.finalizedStateRoots(1), bytes32(uint256(2))); - assertEq(rollup.withdrawRoots(1), bytes32(uint256(3))); - assertEq(rollup.lastFinalizedBatchIndex(), 1); // revert, caller not owner hevm.startPrank(address(1)); @@ -1087,7 +698,6 @@ contract ScrollChainTest is DSTestPlus { rollup.resolveStateMismatch(batchHeader1, true); // mismatch on batch 1 - hevm.startPrank(address(0)); hevm.expectEmit(true, false, false, true); emit StateMismatch(1, bytes32(uint256(3)), bytes32(uint256(4))); (uint256 unresolvedBatchIndex, bytes32 unresolvedStateRoot, bytes32 unresolvedWithdrawRoot) = rollup @@ -1096,13 +706,14 @@ contract ScrollChainTest is DSTestPlus { assertEq(unresolvedBatchIndex, 0); assertEq(unresolvedStateRoot, bytes32(0)); assertEq(unresolvedWithdrawRoot, bytes32(0)); + hevm.startPrank(address(0)); rollup.finalizeBundleWithTeeProof(batchHeader1, bytes32(uint256(3)), bytes32(uint256(4)), new bytes(0)); + hevm.stopPrank(); assertEq(rollup.lastTeeVerifiedBatchIndex(), 0); (unresolvedBatchIndex, unresolvedStateRoot, unresolvedWithdrawRoot) = rollup.unresolvedState(); assertEq(unresolvedBatchIndex, 1); assertEq(unresolvedStateRoot, bytes32(uint256(3))); assertEq(unresolvedWithdrawRoot, bytes32(uint256(4))); - hevm.stopPrank(); // revert when ErrorFinalizationPaused hevm.startPrank(address(0)); @@ -1114,7 +725,11 @@ contract ScrollChainTest is DSTestPlus { assertEq(rollup.finalizedStateRoots(1), bytes32(uint256(2))); assertEq(rollup.withdrawRoots(1), bytes32(uint256(3))); hevm.expectEmit(true, false, false, true); - emit FinalizeBatchWithTEEProof(1); + emit ResolveState(1, bytes32(uint256(3)), bytes32(uint256(4))); + hevm.expectEmit(true, false, false, true); + emit VerifyBatchWithTee(1); + hevm.expectEmit(true, true, false, true); + emit FinalizeBatch(1, keccak256(batchHeader1), bytes32(uint256(3)), bytes32(uint256(4))); rollup.resolveStateMismatch(batchHeader1, true); assertEq(rollup.withdrawRoots(1), bytes32(uint256(4))); assertEq(rollup.lastTeeVerifiedBatchIndex(), 1); @@ -1144,8 +759,8 @@ contract ScrollChainTest is DSTestPlus { ScrollChainMockBlob impl = new ScrollChainMockBlob( rollup.layer2ChainId(), rollup.messageQueue(), - rollup.verifier(), - address(0) + rollup.zkpVerifier(), + rollup.teeVerifier() ); admin.upgrade(ITransparentUpgradeableProxy(address(rollup)), address(impl)); // from https://etherscan.io/blob/0x013590dc3544d56629ba81bb14d4d31248f825001653aa575eb8e3a719046757?bid=740652 @@ -1373,13 +988,22 @@ contract ScrollChainTest is DSTestPlus { hevm.startPrank(address(0)); rollup.finalizeBundleWithProof(batchHeader2, bytes32(uint256(2)), bytes32(uint256(3)), new bytes(0)); hevm.stopPrank(); - assertBoolEq(rollup.isBatchFinalized(1), true); - assertBoolEq(rollup.isBatchFinalized(2), true); + assertBoolEq(rollup.isBatchFinalized(1), false); + assertBoolEq(rollup.isBatchFinalized(2), false); assertEq(rollup.finalizedStateRoots(1), bytes32(0)); assertEq(rollup.withdrawRoots(1), bytes32(0)); assertEq(rollup.finalizedStateRoots(2), bytes32(uint256(2))); assertEq(rollup.withdrawRoots(2), bytes32(uint256(3))); + assertEq(rollup.lastFinalizedBatchIndex(), 0); + assertEq(rollup.lastZkpVerifiedBatchIndex(), 2); + assertEq(0, messageQueue.nextUnfinalizedQueueIndex()); + hevm.startPrank(address(0)); + rollup.finalizeBundleWithTeeProof(batchHeader2, bytes32(uint256(2)), bytes32(uint256(3)), new bytes(0)); + hevm.stopPrank(); + assertBoolEq(rollup.isBatchFinalized(1), true); + assertBoolEq(rollup.isBatchFinalized(2), true); assertEq(rollup.lastFinalizedBatchIndex(), 2); + assertEq(rollup.lastTeeVerifiedBatchIndex(), 2); assertEq(265, messageQueue.nextUnfinalizedQueueIndex()); } @@ -1424,244 +1048,13 @@ contract ScrollChainTest is DSTestPlus { } } - /* - function testSwitchBatchFromV1ToV3() external { - rollup.addSequencer(address(0)); - rollup.addProver(address(0)); - - // import 300 L1 messages - for (uint256 i = 0; i < 300; i++) { - messageQueue.appendCrossDomainMessage(address(this), 1000000, new bytes(0)); - } - - // import genesis batch first - bytes memory batchHeader0 = new bytes(89); - assembly { - mstore(add(batchHeader0, add(0x20, 25)), 1) - } - rollup.importGenesisBatch(batchHeader0, bytes32(uint256(1))); - bytes32 batchHash0 = rollup.committedBatches(0); - - // upgrade to ScrollChainMockBlob - ScrollChainMockBlob impl = new ScrollChainMockBlob( - rollup.layer2ChainId(), - rollup.messageQueue(), - rollup.verifier(), - address(0) - ); - admin.upgrade(ITransparentUpgradeableProxy(address(rollup)), address(impl)); - // from https://etherscan.io/blob/0x013590dc3544d56629ba81bb14d4d31248f825001653aa575eb8e3a719046757?bid=740652 - bytes32 blobVersionedHash = 0x013590dc3544d56629ba81bb14d4d31248f825001653aa575eb8e3a719046757; - bytes - memory blobDataProof = hex"2c9d777660f14ad49803a6442935c0d24a0d83551de5995890bf70a17d24e68753ab0fe6807c7081f0885fe7da741554d658a03730b1fa006f8319f8b993bcb0a5a0c9e8a145c5ef6e415c245690effa2914ec9393f58a7251d30c0657da1453d9ad906eae8b97dd60c9a216f81b4df7af34d01e214e1ec5865f0133ecc16d7459e49dab66087340677751e82097fbdd20551d66076f425775d1758a9dfd186b"; - ScrollChainMockBlob(address(rollup)).setBlobVersionedHash(blobVersionedHash); - - bytes memory bitmap; - bytes[] memory chunks; - bytes memory chunk0; - bytes memory chunk1; - - // commit batch1 with version v1, one chunk with one block, 1 tx, 1 L1 message, no skip - // => payload for data hash of chunk0 - // 0000000000000000 - // 0000000000000000 - // 0000000000000000000000000000000000000000000000000000000000000000 - // 0000000000000000 - // 0001 - // a2277fd30bbbe74323309023b56035b376d7768ad237ae4fc46ead7dc9591ae1 - // => data hash for chunk0 - // 9ef1e5694bdb014a1eea42be756a8f63bfd8781d6332e9ef3b5126d90c62f110 - // => data hash for all chunks - // d9cb6bf9264006fcea490d5c261f7453ab95b1b26033a3805996791b8e3a62f3 - // => payload for batch header - // 01 - // 0000000000000001 - // 0000000000000001 - // 0000000000000001 - // d9cb6bf9264006fcea490d5c261f7453ab95b1b26033a3805996791b8e3a62f3 - // 013590dc3544d56629ba81bb14d4d31248f825001653aa575eb8e3a719046757 - // 119b828c2a2798d2c957228ebeaff7e10bb099ae0d4e224f3eeb779ff61cba61 - // 0000000000000000000000000000000000000000000000000000000000000000 - // => hash for batch header - // 66b68a5092940d88a8c6f203d2071303557c024275d8ceaa2e12662bc61c8d8f - bytes memory batchHeader1 = new bytes(121 + 32); - assembly { - mstore8(add(batchHeader1, 0x20), 1) // version - mstore(add(batchHeader1, add(0x20, 1)), shl(192, 1)) // batchIndex = 1 - mstore(add(batchHeader1, add(0x20, 9)), shl(192, 1)) // l1MessagePopped = 1 - mstore(add(batchHeader1, add(0x20, 17)), shl(192, 1)) // totalL1MessagePopped = 1 - mstore(add(batchHeader1, add(0x20, 25)), 0xd9cb6bf9264006fcea490d5c261f7453ab95b1b26033a3805996791b8e3a62f3) // dataHash - mstore(add(batchHeader1, add(0x20, 57)), blobVersionedHash) // blobVersionedHash - mstore(add(batchHeader1, add(0x20, 89)), batchHash0) // parentBatchHash - mstore(add(batchHeader1, add(0x20, 121)), 0) // bitmap0 - } - chunk0 = new bytes(1 + 60); - assembly { - mstore(add(chunk0, 0x20), shl(248, 1)) // numBlocks = 1 - mstore(add(chunk0, add(0x21, 56)), shl(240, 1)) // numTransactions = 1 - mstore(add(chunk0, add(0x21, 58)), shl(240, 1)) // numL1Messages = 1 - } - chunks = new bytes[](1); - chunks[0] = chunk0; - bitmap = new bytes(32); - hevm.startPrank(address(0)); - hevm.expectEmit(true, true, false, true); - emit CommitBatch(1, keccak256(batchHeader1)); - rollup.commitBatch(1, batchHeader0, chunks, bitmap); - hevm.stopPrank(); - assertBoolEq(rollup.isBatchFinalized(1), false); - bytes32 batchHash1 = rollup.committedBatches(1); - assertEq(batchHash1, keccak256(batchHeader1)); - - // commit batch2 with version v2, with two chunks, correctly - // 1. chunk0 has one block, 3 tx, no L1 messages - // => payload for chunk0 - // 0000000000000000 - // 0000000000000456 - // 0000000000000000000000000000000000000000000000000000000000000000 - // 0000000000000000 - // 0003 - // ... (some tx hashes) - // => data hash for chunk0 - // 1c7649f248aed8448fa7997e44db7b7028581deb119c6d6aa1a2d126d62564cf - // 2. chunk1 has three blocks - // 2.1 block0 has 5 tx, 3 L1 messages, no skips - // 2.2 block1 has 10 tx, 5 L1 messages, even is skipped, last is not skipped - // 2.2 block1 has 300 tx, 256 L1 messages, odd position is skipped, last is not skipped - // => payload for chunk1 - // 0000000000000000 - // 0000000000000789 - // 0000000000000000000000000000000000000000000000000000000000000000 - // 0000000000000000 - // 0005 - // 0000000000000000 - // 0000000000001234 - // 0000000000000000000000000000000000000000000000000000000000000000 - // 0000000000000000 - // 000a - // 0000000000000000 - // 0000000000005678 - // 0000000000000000000000000000000000000000000000000000000000000000 - // 0000000000000000 - // 012c - // => data hash for chunk1 - // 4e82cb576135a69a0ecc2b2070c432abfdeb20076594faaa1aeed77f48d7c856 - // => data hash for all chunks - // 166e9d20206ae8cddcdf0f30093e3acc3866937172df5d7f69fb5567d9595239 - // => payload for batch header - // 03 - // 0000000000000002 - // 0000000000000108 - // 0000000000000109 - // 166e9d20206ae8cddcdf0f30093e3acc3866937172df5d7f69fb5567d9595239 - // 013590dc3544d56629ba81bb14d4d31248f825001653aa575eb8e3a719046757 - // 66b68a5092940d88a8c6f203d2071303557c024275d8ceaa2e12662bc61c8d8f - // 0000000000005678 - // 2c9d777660f14ad49803a6442935c0d24a0d83551de5995890bf70a17d24e687 - // 53ab0fe6807c7081f0885fe7da741554d658a03730b1fa006f8319f8b993bcb0 - // => hash for batch header - // f212a256744ca658dfc4eb32665aa0fe845eb757a030bd625cb2880055e3cc92 - bytes memory batchHeader2 = new bytes(193); - assembly { - mstore8(add(batchHeader2, 0x20), 3) // version - mstore(add(batchHeader2, add(0x20, 1)), shl(192, 2)) // batchIndex = 2 - mstore(add(batchHeader2, add(0x20, 9)), shl(192, 264)) // l1MessagePopped = 264 - mstore(add(batchHeader2, add(0x20, 17)), shl(192, 265)) // totalL1MessagePopped = 265 - mstore(add(batchHeader2, add(0x20, 25)), 0x166e9d20206ae8cddcdf0f30093e3acc3866937172df5d7f69fb5567d9595239) // dataHash - mstore(add(batchHeader2, add(0x20, 57)), blobVersionedHash) // blobVersionedHash - mstore(add(batchHeader2, add(0x20, 89)), batchHash1) // parentBatchHash - mstore(add(batchHeader2, add(0x20, 121)), shl(192, 0x5678)) // lastBlockTimestamp - mcopy(add(batchHeader2, add(0x20, 129)), add(blobDataProof, 0x20), 64) // blobDataProof - } - chunk0 = new bytes(1 + 60); - assembly { - mstore(add(chunk0, 0x20), shl(248, 1)) // numBlocks = 1 - mstore(add(chunk0, add(0x21, 8)), shl(192, 0x456)) // timestamp = 0x456 - mstore(add(chunk0, add(0x21, 56)), shl(240, 3)) // numTransactions = 3 - mstore(add(chunk0, add(0x21, 58)), shl(240, 0)) // numL1Messages = 0 - } - chunk1 = new bytes(1 + 60 * 3); - assembly { - mstore(add(chunk1, 0x20), shl(248, 3)) // numBlocks = 3 - mstore(add(chunk1, add(33, 8)), shl(192, 0x789)) // block0.timestamp = 0x789 - mstore(add(chunk1, add(33, 56)), shl(240, 5)) // block0.numTransactions = 5 - mstore(add(chunk1, add(33, 58)), shl(240, 3)) // block0.numL1Messages = 3 - mstore(add(chunk1, add(93, 8)), shl(192, 0x1234)) // block1.timestamp = 0x1234 - mstore(add(chunk1, add(93, 56)), shl(240, 10)) // block1.numTransactions = 10 - mstore(add(chunk1, add(93, 58)), shl(240, 5)) // block1.numL1Messages = 5 - mstore(add(chunk1, add(153, 8)), shl(192, 0x5678)) // block1.timestamp = 0x5678 - mstore(add(chunk1, add(153, 56)), shl(240, 300)) // block1.numTransactions = 300 - mstore(add(chunk1, add(153, 58)), shl(240, 256)) // block1.numL1Messages = 256 - } - chunks = new bytes[](2); - chunks[0] = chunk0; - chunks[1] = chunk1; - bitmap = new bytes(64); - assembly { - mstore( - add(bitmap, add(0x20, 0)), - 77194726158210796949047323339125271902179989777093709359638389338608753093160 - ) // bitmap0 - mstore(add(bitmap, add(0x20, 32)), 42) // bitmap1 - } - - rollup.updateMaxNumTxInChunk(186); - // should revert, when all v1 batch not finalized - hevm.startPrank(address(0)); - hevm.expectRevert("start index mismatch"); - rollup.commitBatchWithBlobProof(3, batchHeader1, chunks, bitmap, blobDataProof); - hevm.stopPrank(); - - // finalize batch1 - hevm.startPrank(address(0)); - hevm.expectEmit(true, true, false, true); - emit FinalizeBatch(1, batchHash1, bytes32(uint256(2)), bytes32(uint256(3))); - rollup.finalizeBatchWithProof4844( - batchHeader1, - bytes32(uint256(1)), - bytes32(uint256(2)), - bytes32(uint256(3)), - blobDataProof, - new bytes(0) - ); - hevm.stopPrank(); - assertBoolEq(rollup.isBatchFinalized(1), true); - assertEq(rollup.finalizedStateRoots(1), bytes32(uint256(2))); - assertEq(rollup.withdrawRoots(1), bytes32(uint256(3))); - assertEq(rollup.lastFinalizedBatchIndex(), 1); - assertBoolEq(messageQueue.isMessageSkipped(0), false); - assertEq(messageQueue.pendingQueueIndex(), 1); - assertEq(messageQueue.nextUnfinalizedQueueIndex(), 1); - - hevm.startPrank(address(0)); - hevm.expectEmit(true, true, false, true); - emit CommitBatch(2, keccak256(batchHeader2)); - rollup.commitBatchWithBlobProof(3, batchHeader1, chunks, bitmap, blobDataProof); - hevm.stopPrank(); - bytes32 batchHash2 = rollup.committedBatches(2); - assertEq(batchHash2, keccak256(batchHeader2)); - assertEq(messageQueue.pendingQueueIndex(), 265); - assertEq(messageQueue.nextUnfinalizedQueueIndex(), 1); - - // finalize batch2 - assertBoolEq(rollup.isBatchFinalized(2), false); - hevm.startPrank(address(0)); - rollup.finalizeBundleWithProof(batchHeader2, bytes32(uint256(2)), bytes32(uint256(3)), new bytes(0)); - hevm.stopPrank(); - assertBoolEq(rollup.isBatchFinalized(2), true); - assertEq(rollup.finalizedStateRoots(2), bytes32(uint256(2))); - assertEq(rollup.withdrawRoots(2), bytes32(uint256(3))); - assertEq(rollup.lastFinalizedBatchIndex(), 2); - } - */ - function testRevertBatch() external { // upgrade to ScrollChainMockBlob ScrollChainMockBlob impl = new ScrollChainMockBlob( rollup.layer2ChainId(), rollup.messageQueue(), - rollup.verifier(), - address(0) + rollup.zkpVerifier(), + rollup.teeVerifier() ); admin.upgrade(ITransparentUpgradeableProxy(address(rollup)), address(impl)); @@ -1696,7 +1089,7 @@ contract ScrollChainTest is DSTestPlus { chunk0[0] = bytes1(uint8(1)); // one block in this chunk chunks[0] = chunk0; hevm.startPrank(address(0)); - // rollup.commitBatch(1, batchHeader0, chunks, new bytes(0)); + rollup.commitBatch(1, batchHeader0, chunks, new bytes(0)); bytes32 batchHash1 = rollup.committedBatches(1); hevm.stopPrank(); @@ -1713,7 +1106,7 @@ contract ScrollChainTest is DSTestPlus { // commit another batch hevm.startPrank(address(0)); - // rollup.commitBatch(1, batchHeader1, chunks, new bytes(0)); + rollup.commitBatch(1, batchHeader1, chunks, new bytes(0)); hevm.stopPrank(); bytes memory batchHeader2 = new bytes(121); @@ -1835,7 +1228,7 @@ contract ScrollChainTest is DSTestPlus { hevm.startPrank(address(0)); hevm.expectRevert("Pausable: paused"); - // rollup.commitBatch(1, new bytes(0), new bytes[](0), new bytes(0)); + rollup.commitBatch(1, new bytes(0), new bytes[](0), new bytes(0)); hevm.expectRevert("Pausable: paused"); rollup.commitBatchWithBlobProof(3, new bytes(0), new bytes[](0), new bytes(0), new bytes(0)); hevm.stopPrank();