diff --git a/l1-contracts/contracts/common/L1ContractErrors.sol b/l1-contracts/contracts/common/L1ContractErrors.sol index 21483fb913..c265a4804d 100644 --- a/l1-contracts/contracts/common/L1ContractErrors.sol +++ b/l1-contracts/contracts/common/L1ContractErrors.sol @@ -327,9 +327,26 @@ error ZeroAddress(); error ZeroBalance(); // 0xc84885d4 error ZeroChainId(); - +// 0x520aa59c +error PubdataIsEmpty(); +// 0x99d8fec9 +error EmptyData(); +// 0xc99a8360 +error UnsupportedCommitBatchEncoding(uint8 version); +// 0xe167e4a6 +error UnsupportedProofBatchEncoding(uint8 version); +// 0xe8e3f6f4 +error UnsupportedExecuteBatchEncoding(uint8 version); +// 0xd7d93e1f +error IncorrectBatchBounds( + uint256 processFromExpected, + uint256 processToExpected, + uint256 processFromProvided, + uint256 processToProvided +); +// 0x04a0b7e9 error AssetIdNotSupported(bytes32 assetId); - +// 0x64107968 error AssetHandlerNotRegistered(bytes32 assetId); enum SharedBridgeKey { diff --git a/l1-contracts/contracts/common/libraries/Merkle.sol b/l1-contracts/contracts/common/libraries/Merkle.sol index 560bfc7bc2..66db8ea750 100644 --- a/l1-contracts/contracts/common/libraries/Merkle.sol +++ b/l1-contracts/contracts/common/libraries/Merkle.sol @@ -70,10 +70,10 @@ library Merkle { /// @param _itemHashes Hashes of the elements in the range /// @return The Merkle root function calculateRootPaths( - bytes32[] calldata _startPath, - bytes32[] calldata _endPath, + bytes32[] memory _startPath, + bytes32[] memory _endPath, uint256 _startIndex, - bytes32[] calldata _itemHashes + bytes32[] memory _itemHashes ) internal pure returns (bytes32) { uint256 pathLength = _startPath.length; require(pathLength == _endPath.length, "Merkle: path length mismatch"); diff --git a/l1-contracts/contracts/common/libraries/UnsafeBytes.sol b/l1-contracts/contracts/common/libraries/UnsafeBytes.sol index e2680d9e05..4edf940042 100644 --- a/l1-contracts/contracts/common/libraries/UnsafeBytes.sol +++ b/l1-contracts/contracts/common/libraries/UnsafeBytes.sol @@ -30,6 +30,13 @@ library UnsafeBytes { } } + function readUint128(bytes memory _bytes, uint256 _start) internal pure returns (uint128 result, uint256 offset) { + assembly { + offset := add(_start, 16) + result := mload(add(_bytes, offset)) + } + } + function readUint256(bytes memory _bytes, uint256 _start) internal pure returns (uint256 result, uint256 offset) { assembly { offset := add(_start, 32) diff --git a/l1-contracts/contracts/dev-contracts/test/DummyExecutor.sol b/l1-contracts/contracts/dev-contracts/test/DummyExecutor.sol deleted file mode 100644 index e661cdaa1f..0000000000 --- a/l1-contracts/contracts/dev-contracts/test/DummyExecutor.sol +++ /dev/null @@ -1,143 +0,0 @@ -// SPDX-License-Identifier: MIT - -pragma solidity 0.8.24; - -import {IExecutor} from "../../state-transition/chain-interfaces/IExecutor.sol"; -import {PriorityOpsBatchInfo} from "../../state-transition/libraries/PriorityTree.sol"; - -/// @title DummyExecutor -/// @notice A test smart contract implementing the IExecutor interface to simulate Executor behavior for testing purposes. -contract DummyExecutor is IExecutor { - // add this to be excluded from coverage report - function test() internal virtual {} - - address owner; - - // Flags to control if the contract should revert during commit, prove, and execute batch operations - bool shouldRevertOnCommitBatches; - bool shouldRevertOnProveBatches; - bool shouldRevertOnExecuteBatches; - - // Counters to track the total number of committed, verified, and executed batches - uint256 public getTotalBatchesCommitted; - uint256 public getTotalBatchesVerified; - uint256 public getTotalBatchesExecuted; - string public constant override getName = "DummyExecutor"; - - /// @notice Constructor sets the contract owner to the message sender - constructor() { - owner = msg.sender; - } - - /// @notice Modifier that only allows the owner to call certain functions - modifier onlyOwner() { - require(msg.sender == owner); - _; - } - - function getAdmin() external view returns (address) { - return owner; - } - - /// @notice Removing txs from the priority queue - function removePriorityQueueFront(uint256 _index) external {} - - /// @notice Allows the owner to set whether the contract should revert during commit blocks operation - function setShouldRevertOnCommitBatches(bool _shouldRevert) external onlyOwner { - shouldRevertOnCommitBatches = _shouldRevert; - } - - /// @notice Allows the owner to set whether the contract should revert during prove batches operation - function setShouldRevertOnProveBatches(bool _shouldRevert) external onlyOwner { - shouldRevertOnProveBatches = _shouldRevert; - } - - /// @notice Allows the owner to set whether the contract should revert during execute batches operation - function setShouldRevertOnExecuteBatches(bool _shouldRevert) external onlyOwner { - shouldRevertOnExecuteBatches = _shouldRevert; - } - - function commitBatchesSharedBridge( - uint256, - StoredBatchInfo calldata _lastCommittedBatchData, - CommitBatchInfo[] calldata _newBatchesData - ) external { - require(!shouldRevertOnCommitBatches, "DummyExecutor: shouldRevertOnCommitBatches"); - require( - _lastCommittedBatchData.batchNumber == getTotalBatchesCommitted, - "DummyExecutor: Invalid last committed batch number" - ); - - uint256 batchesLength = _newBatchesData.length; - for (uint256 i = 0; i < batchesLength; ++i) { - require(getTotalBatchesCommitted + i + 1 == _newBatchesData[i].batchNumber); - } - - getTotalBatchesCommitted += batchesLength; - } - - function proveBatchesSharedBridge( - uint256, - StoredBatchInfo calldata _prevBatch, - StoredBatchInfo[] calldata _committedBatches, - ProofInput calldata _proof - ) external { - require(!shouldRevertOnProveBatches, "DummyExecutor: shouldRevertOnProveBatches"); - require(_prevBatch.batchNumber == getTotalBatchesVerified, "DummyExecutor: Invalid previous batch number"); - - require(_committedBatches.length == 1, "DummyExecutor: Can prove only one batch"); - require( - _committedBatches[0].batchNumber == _prevBatch.batchNumber + 1, - "DummyExecutor 1: Can't prove batch out of order" - ); - - getTotalBatchesVerified += 1; - require( - getTotalBatchesVerified <= getTotalBatchesCommitted, - "DummyExecutor: prove more batches than were committed" - ); - } - - function executeBatches(StoredBatchInfo[] calldata _batchesData) public { - require(!shouldRevertOnExecuteBatches, "DummyExecutor: shouldRevertOnExecuteBatches"); - uint256 nBatches = _batchesData.length; - for (uint256 i = 0; i < nBatches; ++i) { - require(_batchesData[i].batchNumber == getTotalBatchesExecuted + i + 1); - } - getTotalBatchesExecuted += nBatches; - require( - getTotalBatchesExecuted <= getTotalBatchesVerified, - "DummyExecutor 2: Can't execute batches more than committed and proven currently" - ); - } - - function executeBatchesSharedBridge(uint256, StoredBatchInfo[] calldata _batchesData) external { - executeBatches(_batchesData); - } - - function executeBatchesSharedBridge( - uint256, - StoredBatchInfo[] calldata _batchesData, - PriorityOpsBatchInfo[] calldata - ) external { - executeBatches(_batchesData); - } - - function revertBatchesSharedBridge(uint256, uint256 _newLastBatch) external { - require( - getTotalBatchesCommitted > _newLastBatch, - "DummyExecutor: The last committed batch is less than new last batch" - ); - uint256 newTotalBatchesCommitted = _maxU256(_newLastBatch, getTotalBatchesExecuted); - - if (newTotalBatchesCommitted < getTotalBatchesVerified) { - getTotalBatchesVerified = newTotalBatchesCommitted; - } - getTotalBatchesCommitted = newTotalBatchesCommitted; - } - - /// @notice Returns larger of two values - function _maxU256(uint256 a, uint256 b) internal pure returns (uint256) { - return a < b ? b : a; - } -} diff --git a/l1-contracts/contracts/state-transition/TestnetVerifier.sol b/l1-contracts/contracts/state-transition/TestnetVerifier.sol index 6e97fed05d..a347c35377 100644 --- a/l1-contracts/contracts/state-transition/TestnetVerifier.sol +++ b/l1-contracts/contracts/state-transition/TestnetVerifier.sol @@ -18,17 +18,13 @@ contract TestnetVerifier is Verifier { /// @dev Verifies a zk-SNARK proof, skipping the verification if the proof is empty. /// @inheritdoc IVerifier - function verify( - uint256[] calldata _publicInputs, - uint256[] calldata _proof, - uint256[] calldata _recursiveAggregationInput - ) public view override returns (bool) { + function verify(uint256[] calldata _publicInputs, uint256[] calldata _proof) public view override returns (bool) { // We allow skipping the zkp verification for the test(net) environment // If the proof is not empty, verify it, otherwise, skip the verification if (_proof.length == 0) { return true; } - return super.verify(_publicInputs, _proof, _recursiveAggregationInput); + return super.verify(_publicInputs, _proof); } } diff --git a/l1-contracts/contracts/state-transition/ValidatorTimelock.sol b/l1-contracts/contracts/state-transition/ValidatorTimelock.sol index c59b04a01e..64cc0bc204 100644 --- a/l1-contracts/contracts/state-transition/ValidatorTimelock.sol +++ b/l1-contracts/contracts/state-transition/ValidatorTimelock.sol @@ -6,7 +6,6 @@ import {Ownable2Step} from "@openzeppelin/contracts-v4/access/Ownable2Step.sol"; import {LibMap} from "./libraries/LibMap.sol"; import {IExecutor} from "./chain-interfaces/IExecutor.sol"; import {IChainTypeManager} from "./IChainTypeManager.sol"; -import {PriorityOpsBatchInfo} from "./libraries/PriorityTree.sol"; import {Unauthorized, TimeNotReached, ZeroAddress} from "../common/L1ContractErrors.sol"; /// @author Matter Labs @@ -119,32 +118,27 @@ contract ValidatorTimelock is IExecutor, Ownable2Step { /// a call to the zkChain diamond contract with the same calldata. function commitBatchesSharedBridge( uint256 _chainId, - StoredBatchInfo calldata, - CommitBatchInfo[] calldata _newBatchesData + uint256 _processBatchFrom, + uint256 _processBatchTo, + bytes calldata ) external onlyValidator(_chainId) { - _commitBatchesInner(_chainId, _newBatchesData); - } - - function _commitBatchesInner(uint256 _chainId, CommitBatchInfo[] calldata _newBatchesData) internal { unchecked { // This contract is only a temporary solution, that hopefully will be disabled until 2106 year, so... // It is safe to cast. uint32 timestamp = uint32(block.timestamp); // We disable this check because calldata array length is cheap. - // solhint-disable-next-line gas-length-in-loops - for (uint256 i = 0; i < _newBatchesData.length; ++i) { - committedBatchTimestamp[_chainId].set(_newBatchesData[i].batchNumber, timestamp); + for (uint256 i = _processBatchFrom; i <= _processBatchTo; ++i) { + committedBatchTimestamp[_chainId].set(i, timestamp); } } - - _propagateToZkSyncZKChain(_chainId); + _propagateToZKChain(_chainId); } /// @dev Make a call to the zkChain diamond contract with the same calldata. /// Note: If the batch is reverted, it needs to be committed first before the execution. /// So it's safe to not override the committed batches. function revertBatchesSharedBridge(uint256 _chainId, uint256) external onlyValidator(_chainId) { - _propagateToZkSyncZKChain(_chainId); + _propagateToZKChain(_chainId); } /// @dev Make a call to the zkChain diamond contract with the same calldata. @@ -152,30 +146,26 @@ contract ValidatorTimelock is IExecutor, Ownable2Step { /// the batch is known on the commit stage and the proved is not finalized (may be reverted). function proveBatchesSharedBridge( uint256 _chainId, - StoredBatchInfo calldata, - StoredBatchInfo[] calldata, - ProofInput calldata + uint256, // _processBatchFrom + uint256, // _processBatchTo + bytes calldata ) external onlyValidator(_chainId) { - _propagateToZkSyncZKChain(_chainId); + _propagateToZKChain(_chainId); } /// @dev Check that batches were committed at least X time ago and /// make a call to the zkChain diamond contract with the same calldata. function executeBatchesSharedBridge( uint256 _chainId, - StoredBatchInfo[] calldata _newBatchesData, - PriorityOpsBatchInfo[] calldata + uint256 _processBatchFrom, + uint256 _processBatchTo, + bytes calldata ) external onlyValidator(_chainId) { - _executeBatchesInner(_chainId, _newBatchesData); - } - - function _executeBatchesInner(uint256 _chainId, StoredBatchInfo[] calldata _newBatchesData) internal { uint256 delay = executionDelay; // uint32 unchecked { // We disable this check because calldata array length is cheap. - // solhint-disable-next-line gas-length-in-loops - for (uint256 i = 0; i < _newBatchesData.length; ++i) { - uint256 commitBatchTimestamp = committedBatchTimestamp[_chainId].get(_newBatchesData[i].batchNumber); + for (uint256 i = _processBatchFrom; i <= _processBatchTo; ++i) { + uint256 commitBatchTimestamp = committedBatchTimestamp[_chainId].get(i); // Note: if the `commitBatchTimestamp` is zero, that means either: // * The batch was committed, but not through this contract. @@ -187,12 +177,12 @@ contract ValidatorTimelock is IExecutor, Ownable2Step { } } } - _propagateToZkSyncZKChain(_chainId); + _propagateToZKChain(_chainId); } /// @dev Call the zkChain diamond contract with the same calldata as this contract was called. /// Note: it is called the zkChain diamond contract, not delegatecalled! - function _propagateToZkSyncZKChain(uint256 _chainId) internal { + function _propagateToZKChain(uint256 _chainId) internal { address contractAddress = chainTypeManager.getZKChain(_chainId); assembly { // Copy function signature and arguments from calldata at zero position into memory at pointer position diff --git a/l1-contracts/contracts/state-transition/Verifier.sol b/l1-contracts/contracts/state-transition/Verifier.sol index cb90bf472f..3072c2c5af 100644 --- a/l1-contracts/contracts/state-transition/Verifier.sol +++ b/l1-contracts/contracts/state-transition/Verifier.sol @@ -343,8 +343,7 @@ contract Verifier is IVerifier { /// @inheritdoc IVerifier function verify( uint256[] calldata, // _publicInputs - uint256[] calldata, // _proof - uint256[] calldata // _recursiveAggregationInput + uint256[] calldata // _proof ) public view virtual returns (bool) { // No memory was accessed yet, so keys can be loaded into the right place and not corrupt any other memory. _loadVerificationKey(); @@ -523,7 +522,17 @@ contract Verifier is IVerifier { // 2. Load the proof (except for the recursive part) offset := calldataload(0x24) let proofLengthInWords := calldataload(add(offset, 0x04)) - isValid := and(eq(proofLengthInWords, 44), isValid) + + // Check the proof length depending on whether the recursive part is present + let expectedProofLength + switch mload(VK_RECURSIVE_FLAG_SLOT) + case 0 { + expectedProofLength := 44 + } + default { + expectedProofLength := 48 + } + isValid := and(eq(proofLengthInWords, expectedProofLength), isValid) // PROOF_STATE_POLYS_0 { @@ -670,21 +679,13 @@ contract Verifier is IVerifier { } // 3. Load the recursive part of the proof - offset := calldataload(0x44) - let recursiveProofLengthInWords := calldataload(add(offset, 0x04)) - - switch mload(VK_RECURSIVE_FLAG_SLOT) - case 0 { - // recursive part should be empty - isValid := and(iszero(recursiveProofLengthInWords), isValid) - } - default { + if mload(VK_RECURSIVE_FLAG_SLOT) { // recursive part should be consist of 2 points - isValid := and(eq(recursiveProofLengthInWords, 4), isValid) + // PROOF_RECURSIVE_PART_P1 { - let x := mod(calldataload(add(offset, 0x024)), Q_MOD) - let y := mod(calldataload(add(offset, 0x044)), Q_MOD) + let x := mod(calldataload(add(offset, 0x5a4)), Q_MOD) + let y := mod(calldataload(add(offset, 0x5c4)), Q_MOD) let xx := mulmod(x, x, Q_MOD) isValid := and(eq(mulmod(y, y, Q_MOD), addmod(mulmod(x, xx, Q_MOD), 3, Q_MOD)), isValid) mstore(PROOF_RECURSIVE_PART_P1_X_SLOT, x) @@ -692,8 +693,8 @@ contract Verifier is IVerifier { } // PROOF_RECURSIVE_PART_P2 { - let x := mod(calldataload(add(offset, 0x064)), Q_MOD) - let y := mod(calldataload(add(offset, 0x084)), Q_MOD) + let x := mod(calldataload(add(offset, 0x5e4)), Q_MOD) + let y := mod(calldataload(add(offset, 0x604)), Q_MOD) let xx := mulmod(x, x, Q_MOD) isValid := and(eq(mulmod(y, y, Q_MOD), addmod(mulmod(x, xx, Q_MOD), 3, Q_MOD)), isValid) mstore(PROOF_RECURSIVE_PART_P2_X_SLOT, x) diff --git a/l1-contracts/contracts/state-transition/chain-deps/facets/Executor.sol b/l1-contracts/contracts/state-transition/chain-deps/facets/Executor.sol index 9a76108ad5..0875d2e953 100644 --- a/l1-contracts/contracts/state-transition/chain-deps/facets/Executor.sol +++ b/l1-contracts/contracts/state-transition/chain-deps/facets/Executor.sol @@ -10,6 +10,7 @@ import {IMessageRoot} from "../../../bridgehub/IMessageRoot.sol"; import {COMMIT_TIMESTAMP_NOT_OLDER, COMMIT_TIMESTAMP_APPROXIMATION_DELTA, EMPTY_STRING_KECCAK, L2_TO_L1_LOG_SERIALIZE_SIZE, MAX_L2_TO_L1_LOGS_COMMITMENT_BYTES, PACKED_L2_BLOCK_TIMESTAMP_MASK, PUBLIC_INPUT_SHIFT} from "../../../common/Config.sol"; import {IExecutor, L2_LOG_ADDRESS_OFFSET, L2_LOG_KEY_OFFSET, L2_LOG_VALUE_OFFSET, SystemLogKey, LogProcessingOutput, TOTAL_BLOBS_IN_COMMITMENT} from "../../chain-interfaces/IExecutor.sol"; import {PriorityQueue, PriorityOperation} from "../../libraries/PriorityQueue.sol"; +import {BatchDecoder} from "../../libraries/BatchDecoder.sol"; import {UncheckedMath} from "../../../common/libraries/UncheckedMath.sol"; import {UnsafeBytes} from "../../../common/libraries/UnsafeBytes.sol"; import {L2_BOOTLOADER_ADDRESS, L2_TO_L1_MESSENGER_SYSTEM_CONTRACT_ADDR, L2_SYSTEM_CONTEXT_SYSTEM_CONTRACT_ADDR} from "../../../common/L2ContractAddresses.sol"; @@ -43,7 +44,7 @@ contract ExecutorFacet is ZKChainBase, IExecutor { /// @notice Does not change storage function _commitOneBatch( StoredBatchInfo memory _previousBatch, - CommitBatchInfo calldata _newBatch, + CommitBatchInfo memory _newBatch, bytes32 _expectedSystemContractUpgradeTxHash ) internal returns (StoredBatchInfo memory) { // only commit next batch @@ -141,7 +142,7 @@ contract ExecutorFacet is ZKChainBase, IExecutor { /// SystemLogKey enum in Constants.sol is processed per new batch. /// @dev Data returned from here will be used to form the batch commitment. function _processL2Logs( - CommitBatchInfo calldata _newBatch, + CommitBatchInfo memory _newBatch, bytes32 _expectedSystemContractUpgradeTxHash ) internal view returns (LogProcessingOutput memory logOutput) { // Copy L2 to L1 logs into memory. @@ -231,16 +232,10 @@ contract ExecutorFacet is ZKChainBase, IExecutor { /// @inheritdoc IExecutor function commitBatchesSharedBridge( uint256, // _chainId - StoredBatchInfo calldata _lastCommittedBatchData, - CommitBatchInfo[] calldata _newBatchesData - ) external nonReentrant onlyValidator { - _commitBatches(_lastCommittedBatchData, _newBatchesData); - } - - function _commitBatches( - StoredBatchInfo memory _lastCommittedBatchData, - CommitBatchInfo[] calldata _newBatchesData - ) internal chainOnCurrentBridgehub { + uint256 _processFrom, + uint256 _processTo, + bytes calldata _commitData + ) external nonReentrant onlyValidator chainOnCurrentBridgehub { // check that we have the right protocol version // three comments: // 1. A chain has to keep their protocol version up to date, as processing a block requires the latest or previous protocol version @@ -251,32 +246,36 @@ contract ExecutorFacet is ZKChainBase, IExecutor { if (!IChainTypeManager(s.chainTypeManager).protocolVersionIsActive(s.protocolVersion)) { revert InvalidProtocolVersion(); } + (StoredBatchInfo memory lastCommittedBatchData, CommitBatchInfo[] memory newBatchesData) = BatchDecoder + .decodeAndCheckCommitData(_commitData, _processFrom, _processTo); // With the new changes for EIP-4844, namely the restriction on number of blobs per block, we only allow for a single batch to be committed at a time. - if (_newBatchesData.length != 1) { + // Note: Don't need to check that `_processFrom` == `_processTo` because there is only one batch, + // and so the range checked in the `decodeAndCheckCommitData` is enough. + if (newBatchesData.length != 1) { revert CanOnlyProcessOneBatch(); } // Check that we commit batches after last committed batch - if (s.storedBatchHashes[s.totalBatchesCommitted] != _hashStoredBatchInfo(_lastCommittedBatchData)) { + if (s.storedBatchHashes[s.totalBatchesCommitted] != _hashStoredBatchInfo(lastCommittedBatchData)) { // incorrect previous batch data revert BatchHashMismatch( s.storedBatchHashes[s.totalBatchesCommitted], - _hashStoredBatchInfo(_lastCommittedBatchData) + _hashStoredBatchInfo(lastCommittedBatchData) ); } bytes32 systemContractsUpgradeTxHash = s.l2SystemContractsUpgradeTxHash; // Upgrades are rarely done so we optimize a case with no active system contracts upgrade. if (systemContractsUpgradeTxHash == bytes32(0) || s.l2SystemContractsUpgradeBatchNumber != 0) { - _commitBatchesWithoutSystemContractsUpgrade(_lastCommittedBatchData, _newBatchesData); + _commitBatchesWithoutSystemContractsUpgrade(lastCommittedBatchData, newBatchesData); } else { _commitBatchesWithSystemContractsUpgrade( - _lastCommittedBatchData, - _newBatchesData, + lastCommittedBatchData, + newBatchesData, systemContractsUpgradeTxHash ); } - s.totalBatchesCommitted = s.totalBatchesCommitted + _newBatchesData.length; + s.totalBatchesCommitted = s.totalBatchesCommitted + newBatchesData.length; } /// @dev Commits new batches without any system contracts upgrade. @@ -284,7 +283,7 @@ contract ExecutorFacet is ZKChainBase, IExecutor { /// @param _newBatchesData An array of batch data that needs to be committed. function _commitBatchesWithoutSystemContractsUpgrade( StoredBatchInfo memory _lastCommittedBatchData, - CommitBatchInfo[] calldata _newBatchesData + CommitBatchInfo[] memory _newBatchesData ) internal { // We disable this check because calldata array length is cheap. // solhint-disable-next-line gas-length-in-loops @@ -306,7 +305,7 @@ contract ExecutorFacet is ZKChainBase, IExecutor { /// @param _systemContractUpgradeTxHash The transaction hash of the system contract upgrade. function _commitBatchesWithSystemContractsUpgrade( StoredBatchInfo memory _lastCommittedBatchData, - CommitBatchInfo[] calldata _newBatchesData, + CommitBatchInfo[] memory _newBatchesData, bytes32 _systemContractUpgradeTxHash ) internal { // The system contract upgrade is designed to be executed atomically with the new bootloader, a default account, @@ -352,7 +351,7 @@ contract ExecutorFacet is ZKChainBase, IExecutor { } } - function _rollingHash(bytes32[] calldata _hashes) internal pure returns (bytes32) { + function _rollingHash(bytes32[] memory _hashes) internal pure returns (bytes32) { bytes32 hash = EMPTY_STRING_KECCAK; uint256 nHashes = _hashes.length; for (uint256 i = 0; i < nHashes; i = i.uncheckedInc()) { @@ -411,7 +410,7 @@ contract ExecutorFacet is ZKChainBase, IExecutor { /// @dev _executedBatchIdx is an index in the array of the batches that we want to execute together function _executeOneBatch( StoredBatchInfo memory _storedBatch, - PriorityOpsBatchInfo calldata _priorityOpsData, + PriorityOpsBatchInfo memory _priorityOpsData, uint256 _executedBatchIdx ) internal { require(_priorityOpsData.itemHashes.length == _storedBatch.numberOfLayer1Txs, "zxc"); @@ -431,30 +430,26 @@ contract ExecutorFacet is ZKChainBase, IExecutor { /// @inheritdoc IExecutor function executeBatchesSharedBridge( - uint256, - StoredBatchInfo[] calldata _batchesData, - PriorityOpsBatchInfo[] calldata _priorityOpsData - ) external nonReentrant onlyValidator { - _executeBatches(_batchesData, _priorityOpsData); - } - - function _executeBatches( - StoredBatchInfo[] calldata _batchesData, - PriorityOpsBatchInfo[] calldata _priorityOpsData - ) internal chainOnCurrentBridgehub { - uint256 nBatches = _batchesData.length; - require(_batchesData.length == _priorityOpsData.length, "bp"); + uint256, // _chainId + uint256 _processFrom, + uint256 _processTo, + bytes calldata _executeData + ) external nonReentrant onlyValidator chainOnCurrentBridgehub { + (StoredBatchInfo[] memory batchesData, PriorityOpsBatchInfo[] memory priorityOpsData) = BatchDecoder + .decodeAndCheckExecuteData(_executeData, _processFrom, _processTo); + uint256 nBatches = batchesData.length; + require(batchesData.length == priorityOpsData.length, "bp"); for (uint256 i = 0; i < nBatches; i = i.uncheckedInc()) { if (s.priorityTree.startIndex <= s.priorityQueue.getFirstUnprocessedPriorityTx()) { - _executeOneBatch(_batchesData[i], _priorityOpsData[i], i); + _executeOneBatch(batchesData[i], priorityOpsData[i], i); } else { - require(_priorityOpsData[i].leftPath.length == 0, "le"); - require(_priorityOpsData[i].rightPath.length == 0, "re"); - require(_priorityOpsData[i].itemHashes.length == 0, "ih"); - _executeOneBatch(_batchesData[i], i); + require(priorityOpsData[i].leftPath.length == 0, "le"); + require(priorityOpsData[i].rightPath.length == 0, "re"); + require(priorityOpsData[i].itemHashes.length == 0, "ih"); + _executeOneBatch(batchesData[i], i); } - emit BlockExecution(_batchesData[i].batchNumber, _batchesData[i].batchHash, _batchesData[i].commitment); + emit BlockExecution(batchesData[i].batchNumber, batchesData[i].batchHash, batchesData[i].commitment); } uint256 newTotalBatchesExecuted = s.totalBatchesExecuted + nBatches; @@ -473,44 +468,39 @@ contract ExecutorFacet is ZKChainBase, IExecutor { /// @inheritdoc IExecutor function proveBatchesSharedBridge( uint256, // _chainId - StoredBatchInfo calldata _prevBatch, - StoredBatchInfo[] calldata _committedBatches, - ProofInput calldata _proof - ) external nonReentrant onlyValidator { - _proveBatches(_prevBatch, _committedBatches, _proof); - } + uint256 _processBatchFrom, + uint256 _processBatchTo, + bytes calldata _proofData + ) external nonReentrant onlyValidator chainOnCurrentBridgehub { + ( + StoredBatchInfo memory prevBatch, + StoredBatchInfo[] memory committedBatches, + uint256[] memory proof + ) = BatchDecoder.decodeAndCheckProofData(_proofData, _processBatchFrom, _processBatchTo); - function _proveBatches( - StoredBatchInfo calldata _prevBatch, - StoredBatchInfo[] calldata _committedBatches, - ProofInput calldata _proof - ) internal chainOnCurrentBridgehub { // Save the variables into the stack to save gas on reading them later uint256 currentTotalBatchesVerified = s.totalBatchesVerified; - uint256 committedBatchesLength = _committedBatches.length; + uint256 committedBatchesLength = committedBatches.length; // Initialize the array, that will be used as public input to the ZKP uint256[] memory proofPublicInput = new uint256[](committedBatchesLength); // Check that the batch passed by the validator is indeed the first unverified batch - if (_hashStoredBatchInfo(_prevBatch) != s.storedBatchHashes[currentTotalBatchesVerified]) { - revert BatchHashMismatch( - s.storedBatchHashes[currentTotalBatchesVerified], - _hashStoredBatchInfo(_prevBatch) - ); + if (_hashStoredBatchInfo(prevBatch) != s.storedBatchHashes[currentTotalBatchesVerified]) { + revert BatchHashMismatch(s.storedBatchHashes[currentTotalBatchesVerified], _hashStoredBatchInfo(prevBatch)); } - bytes32 prevBatchCommitment = _prevBatch.commitment; + bytes32 prevBatchCommitment = prevBatch.commitment; for (uint256 i = 0; i < committedBatchesLength; i = i.uncheckedInc()) { currentTotalBatchesVerified = currentTotalBatchesVerified.uncheckedInc(); - if (_hashStoredBatchInfo(_committedBatches[i]) != s.storedBatchHashes[currentTotalBatchesVerified]) { + if (_hashStoredBatchInfo(committedBatches[i]) != s.storedBatchHashes[currentTotalBatchesVerified]) { revert BatchHashMismatch( s.storedBatchHashes[currentTotalBatchesVerified], - _hashStoredBatchInfo(_committedBatches[i]) + _hashStoredBatchInfo(committedBatches[i]) ); } - bytes32 currentBatchCommitment = _committedBatches[i].commitment; + bytes32 currentBatchCommitment = committedBatches[i].commitment; proofPublicInput[i] = _getBatchProofPublicInput(prevBatchCommitment, currentBatchCommitment); prevBatchCommitment = currentBatchCommitment; @@ -519,23 +509,19 @@ contract ExecutorFacet is ZKChainBase, IExecutor { revert VerifiedBatchesExceedsCommittedBatches(); } - _verifyProof(proofPublicInput, _proof); + _verifyProof(proofPublicInput, proof); emit BlocksVerification(s.totalBatchesVerified, currentTotalBatchesVerified); s.totalBatchesVerified = currentTotalBatchesVerified; } - function _verifyProof(uint256[] memory proofPublicInput, ProofInput calldata _proof) internal view { + function _verifyProof(uint256[] memory proofPublicInput, uint256[] memory _proof) internal view { // We can only process 1 batch proof at a time. if (proofPublicInput.length != 1) { revert CanOnlyProcessOneBatch(); } - bool successVerifyProof = s.verifier.verify( - proofPublicInput, - _proof.serializedProof, - _proof.recursiveAggregationInput - ); + bool successVerifyProof = s.verifier.verify(proofPublicInput, _proof); if (!successVerifyProof) { revert InvalidProof(); } @@ -579,7 +565,7 @@ contract ExecutorFacet is ZKChainBase, IExecutor { /// @dev Creates batch commitment from its data function _createBatchCommitment( - CommitBatchInfo calldata _newBatchData, + CommitBatchInfo memory _newBatchData, bytes32 _stateDiffHash, bytes32[] memory _blobCommitments, bytes32[] memory _blobHashes @@ -593,7 +579,7 @@ contract ExecutorFacet is ZKChainBase, IExecutor { return keccak256(abi.encode(passThroughDataHash, metadataHash, auxiliaryOutputHash)); } - function _batchPassThroughData(CommitBatchInfo calldata _batch) internal pure returns (bytes memory) { + function _batchPassThroughData(CommitBatchInfo memory _batch) internal pure returns (bytes memory) { return abi.encodePacked( // solhint-disable-next-line func-named-parameters @@ -617,7 +603,7 @@ contract ExecutorFacet is ZKChainBase, IExecutor { } function _batchAuxiliaryOutput( - CommitBatchInfo calldata _batch, + CommitBatchInfo memory _batch, bytes32 _stateDiffHash, bytes32[] memory _blobCommitments, bytes32[] memory _blobHashes diff --git a/l1-contracts/contracts/state-transition/chain-interfaces/IExecutor.sol b/l1-contracts/contracts/state-transition/chain-interfaces/IExecutor.sol index 299b7118bd..0877dcbf9a 100644 --- a/l1-contracts/contracts/state-transition/chain-interfaces/IExecutor.sol +++ b/l1-contracts/contracts/state-transition/chain-interfaces/IExecutor.sol @@ -3,7 +3,6 @@ pragma solidity ^0.8.21; import {IZKChainBase} from "./IZKChainBase.sol"; -import {PriorityOpsBatchInfo} from "../libraries/PriorityTree.sol"; /// @dev Enum used by L2 System Contracts to differentiate logs. enum SystemLogKey { @@ -101,48 +100,46 @@ interface IExecutor is IZKChainBase { bytes operatorDAInput; } - /// @notice Recursive proof input data (individual commitments are constructed onchain) - struct ProofInput { - uint256[] recursiveAggregationInput; - uint256[] serializedProof; - } - /// @notice Function called by the operator to commit new batches. It is responsible for: /// - Verifying the correctness of their timestamps. /// - Processing their L2->L1 logs. /// - Storing batch commitments. /// @param _chainId Chain ID of the chain. - /// @param _lastCommittedBatchData Stored data of the last committed batch. - /// @param _newBatchesData Data of the new batches to be committed. + /// @param _processFrom The batch number from which the processing starts. + /// @param _processTo The batch number at which the processing ends. + /// @param _commitData The encoded data of the new batches to be committed. function commitBatchesSharedBridge( uint256 _chainId, - StoredBatchInfo calldata _lastCommittedBatchData, - CommitBatchInfo[] calldata _newBatchesData + uint256 _processFrom, + uint256 _processTo, + bytes calldata _commitData ) external; /// @notice Batches commitment verification. /// @dev Only verifies batch commitments without any other processing. /// @param _chainId Chain ID of the chain. - /// @param _prevBatch Stored data of the last committed batch. - /// @param _committedBatches Stored data of the committed batches. - /// @param _proof The zero knowledge proof. + /// @param _processBatchFrom The batch number from which the verification starts. + /// @param _processBatchTo The batch number at which the verification ends. + /// @param _proofData The encoded data of the new batches to be verified. function proveBatchesSharedBridge( uint256 _chainId, - StoredBatchInfo calldata _prevBatch, - StoredBatchInfo[] calldata _committedBatches, - ProofInput calldata _proof + uint256 _processBatchFrom, + uint256 _processBatchTo, + bytes calldata _proofData ) external; /// @notice The function called by the operator to finalize (execute) batches. It is responsible for: /// - Processing all pending operations (commpleting priority requests). /// - Finalizing this batch (i.e. allowing to withdraw funds from the system) /// @param _chainId Chain ID of the chain. - /// @param _batchesData Data of the batches to be executed. - /// @param _priorityOpsData Merkle proofs of the priority operations for each batch. + /// @param _processFrom The batch number from which the execution starts. + /// @param _processTo The batch number at which the execution ends. + /// @param _executeData The encoded data of the new batches to be executed. function executeBatchesSharedBridge( uint256 _chainId, - StoredBatchInfo[] calldata _batchesData, - PriorityOpsBatchInfo[] calldata _priorityOpsData + uint256 _processFrom, + uint256 _processTo, + bytes calldata _executeData ) external; /// @notice Reverts unexecuted batches diff --git a/l1-contracts/contracts/state-transition/chain-interfaces/IVerifier.sol b/l1-contracts/contracts/state-transition/chain-interfaces/IVerifier.sol index 97872c370f..fe5e2af2c9 100644 --- a/l1-contracts/contracts/state-transition/chain-interfaces/IVerifier.sol +++ b/l1-contracts/contracts/state-transition/chain-interfaces/IVerifier.sol @@ -16,11 +16,7 @@ interface IVerifier { /// @dev Verifies a zk-SNARK proof. /// @return A boolean value indicating whether the zk-SNARK proof is valid. /// Note: The function may revert execution instead of returning false in some cases. - function verify( - uint256[] calldata _publicInputs, - uint256[] calldata _proof, - uint256[] calldata _recursiveAggregationInput - ) external view returns (bool); + function verify(uint256[] calldata _publicInputs, uint256[] calldata _proof) external view returns (bool); /// @notice Calculates a keccak256 hash of the runtime loaded verification keys. /// @return vkHash The keccak256 hash of the loaded verification keys. diff --git a/l1-contracts/contracts/state-transition/libraries/BatchDecoder.sol b/l1-contracts/contracts/state-transition/libraries/BatchDecoder.sol new file mode 100644 index 0000000000..05b16537bd --- /dev/null +++ b/l1-contracts/contracts/state-transition/libraries/BatchDecoder.sol @@ -0,0 +1,219 @@ +// SPDX-License-Identifier: MIT +// We use a floating point pragma here so it can be used within other projects that interact with the ZKsync ecosystem without using our exact pragma version. +pragma solidity ^0.8.21; + +import {IExecutor} from "../chain-interfaces/IExecutor.sol"; +import {PriorityOpsBatchInfo} from "./PriorityTree.sol"; +import {IncorrectBatchBounds, EmptyData, UnsupportedCommitBatchEncoding, UnsupportedProofBatchEncoding, UnsupportedExecuteBatchEncoding} from "../../common/L1ContractErrors.sol"; + +/// @author Matter Labs +/// @custom:security-contact security@matterlabs.dev +/// @notice Utility library for decoding and validating batch data. +/// @dev This library decodes commit, proof, and execution batch data and verifies batch number bounds. +/// It reverts with custom errors when the data is invalid or unsupported encoding is used. +library BatchDecoder { + /// @notice The currently supported encoding version. + uint8 internal constant SUPPORTED_ENCODING_VERSION = 0; + + /// @notice Decodes commit data from a calldata bytes into the last committed batch data and an array of new batch data. + /// @param _commitData The calldata byte array containing the data for committing batches. + /// @return lastCommittedBatchData The data for the batch before newly committed batches. + /// @return newBatchesData An array containing the newly committed batches. + function _decodeCommitData( + bytes calldata _commitData + ) + private + pure + returns ( + IExecutor.StoredBatchInfo memory lastCommittedBatchData, + IExecutor.CommitBatchInfo[] memory newBatchesData + ) + { + if (_commitData.length == 0) { + revert EmptyData(); + } + + uint8 encodingVersion = uint8(_commitData[0]); + if (encodingVersion == SUPPORTED_ENCODING_VERSION) { + (lastCommittedBatchData, newBatchesData) = abi.decode( + _commitData[1:], + (IExecutor.StoredBatchInfo, IExecutor.CommitBatchInfo[]) + ); + } else { + revert UnsupportedCommitBatchEncoding(encodingVersion); + } + } + + /// @notice Decodes the commit data and checks that the provided batch bounds are correct. + /// @dev Note that it only checks that the last and the first batches in the array correspond to the provided bounds. + /// The fact that the batches inside the array are provided in the correct order should be checked by the caller. + /// @param _commitData The calldata byte array containing the data for committing batches. + /// @param _processBatchFrom The expected batch number of the first commit batch in the array. + /// @param _processBatchTo The expected batch number of the last commit batch in the array. + /// @return lastCommittedBatchData The data for the batch before newly committed batches. + /// @return newBatchesData An array containing the newly committed batches. + function decodeAndCheckCommitData( + bytes calldata _commitData, + uint256 _processBatchFrom, + uint256 _processBatchTo + ) + internal + pure + returns ( + IExecutor.StoredBatchInfo memory lastCommittedBatchData, + IExecutor.CommitBatchInfo[] memory newBatchesData + ) + { + (lastCommittedBatchData, newBatchesData) = _decodeCommitData(_commitData); + + if (newBatchesData.length == 0) { + revert EmptyData(); + } + + if ( + newBatchesData[0].batchNumber != _processBatchFrom || + newBatchesData[newBatchesData.length - 1].batchNumber != _processBatchTo + ) { + revert IncorrectBatchBounds( + _processBatchFrom, + _processBatchTo, + newBatchesData[0].batchNumber, + newBatchesData[newBatchesData.length - 1].batchNumber + ); + } + } + + /// @notice Decodes proof data from a calldata byte array into the previous batch, an array of proved batches, and a proof array. + /// @param _proofData The calldata byte array containing the data for proving batches. + /// @return prevBatch The batch information before the batches to be verified. + /// @return provedBatches An array containing the the batches to be verified. + /// @return proof An array containing the proof for the verifier. + function _decodeProofData( + bytes calldata _proofData + ) + private + pure + returns ( + IExecutor.StoredBatchInfo memory prevBatch, + IExecutor.StoredBatchInfo[] memory provedBatches, + uint256[] memory proof + ) + { + if (_proofData.length == 0) { + revert EmptyData(); + } + + uint8 encodingVersion = uint8(_proofData[0]); + if (encodingVersion == SUPPORTED_ENCODING_VERSION) { + (prevBatch, provedBatches, proof) = abi.decode( + _proofData[1:], + (IExecutor.StoredBatchInfo, IExecutor.StoredBatchInfo[], uint256[]) + ); + } else { + revert UnsupportedProofBatchEncoding(encodingVersion); + } + } + + /// @notice Decodes the commit data and checks that the provided batch bounds are correct. + /// @dev Note that it only checks that the last and the first batches in the array correspond to the provided bounds. + /// The fact that the batches inside the array are provided in the correct order should be checked by the caller. + /// @param _proofData The commit data to decode. + /// @param _processBatchFrom The expected batch number of the first batch in the array. + /// @param _processBatchTo The expected batch number of the last batch in the array. + /// @return prevBatch The batch information before the batches to be verified. + /// @return provedBatches An array containing the the batches to be verified. + /// @return proof An array containing the proof for the verifier. + function decodeAndCheckProofData( + bytes calldata _proofData, + uint256 _processBatchFrom, + uint256 _processBatchTo + ) + internal + pure + returns ( + IExecutor.StoredBatchInfo memory prevBatch, + IExecutor.StoredBatchInfo[] memory provedBatches, + uint256[] memory proof + ) + { + (prevBatch, provedBatches, proof) = _decodeProofData(_proofData); + + if (provedBatches.length == 0) { + revert EmptyData(); + } + + if ( + provedBatches[0].batchNumber != _processBatchFrom || + provedBatches[provedBatches.length - 1].batchNumber != _processBatchTo + ) { + revert IncorrectBatchBounds( + _processBatchFrom, + _processBatchTo, + provedBatches[0].batchNumber, + provedBatches[provedBatches.length - 1].batchNumber + ); + } + } + + /// @notice Decodes execution data from a calldata byte array into an array of stored batch information. + /// @param _executeData The calldata byte array containing the execution data to decode. + /// @return executeData An array containing the stored batch information for execution. + /// @return priorityOpsData Merkle proofs of the priority operations for each batch. + function _decodeExecuteData( + bytes calldata _executeData + ) + private + pure + returns (IExecutor.StoredBatchInfo[] memory executeData, PriorityOpsBatchInfo[] memory priorityOpsData) + { + if (_executeData.length == 0) { + revert EmptyData(); + } + + uint8 encodingVersion = uint8(_executeData[0]); + if (encodingVersion == 0) { + (executeData, priorityOpsData) = abi.decode( + _executeData[1:], + (IExecutor.StoredBatchInfo[], PriorityOpsBatchInfo[]) + ); + } else { + revert UnsupportedExecuteBatchEncoding(encodingVersion); + } + } + + /// @notice Decodes the execute data and checks that the provided batch bounds are correct. + /// @dev Note that it only checks that the last and the first batches in the array correspond to the provided bounds. + /// The fact that the batches inside the array are provided in the correct order should be checked by the caller. + /// @param _executeData The calldata byte array containing the execution data to decode. + /// @param _processBatchFrom The expected batch number of the first batch in the array. + /// @param _processBatchTo The expected batch number of the last batch in the array. + /// @return executeData An array containing the stored batch information for execution. + /// @return priorityOpsData Merkle proofs of the priority operations for each batch. + function decodeAndCheckExecuteData( + bytes calldata _executeData, + uint256 _processBatchFrom, + uint256 _processBatchTo + ) + internal + pure + returns (IExecutor.StoredBatchInfo[] memory executeData, PriorityOpsBatchInfo[] memory priorityOpsData) + { + (executeData, priorityOpsData) = _decodeExecuteData(_executeData); + + if (executeData.length == 0) { + revert EmptyData(); + } + + if ( + executeData[0].batchNumber != _processBatchFrom || + executeData[executeData.length - 1].batchNumber != _processBatchTo + ) { + revert IncorrectBatchBounds( + _processBatchFrom, + _processBatchTo, + executeData[0].batchNumber, + executeData[executeData.length - 1].batchNumber + ); + } + } +} diff --git a/l1-contracts/contracts/state-transition/libraries/PriorityTree.sol b/l1-contracts/contracts/state-transition/libraries/PriorityTree.sol index 7fdf713f2f..8098508046 100644 --- a/l1-contracts/contracts/state-transition/libraries/PriorityTree.sol +++ b/l1-contracts/contracts/state-transition/libraries/PriorityTree.sol @@ -67,7 +67,7 @@ library PriorityTree { } /// @notice Process the priority operations of a batch. - function processBatch(Tree storage _tree, PriorityOpsBatchInfo calldata _priorityOpsData) internal { + function processBatch(Tree storage _tree, PriorityOpsBatchInfo memory _priorityOpsData) internal { if (_priorityOpsData.itemHashes.length > 0) { bytes32 expectedRoot = Merkle.calculateRootPaths( _priorityOpsData.leftPath, diff --git a/l1-contracts/src.ts/utils.ts b/l1-contracts/src.ts/utils.ts index 3f048dc5d7..92f38244f8 100644 --- a/l1-contracts/src.ts/utils.ts +++ b/l1-contracts/src.ts/utils.ts @@ -34,6 +34,12 @@ const CREATE2_PREFIX = ethers.utils.solidityKeccak256(["string"], ["zksyncCreate export const priorityTxMaxGasLimit = getNumberFromEnv("CONTRACTS_PRIORITY_TX_MAX_GAS_LIMIT"); const ADDRESS_MODULO = ethers.BigNumber.from(2).pow(160); +export const STORED_BATCH_INFO_ABI_STRING = + "tuple(uint64 batchNumber, bytes32 batchHash, uint64 indexRepeatedStorageChanges, uint256 numberOfLayer1Txs, bytes32 priorityOperationsHash, bytes32 l2LogsTreeRoot, uint256 timestamp, bytes32 commitment)"; +export const COMMIT_BATCH_INFO_ABI_STRING = + "tuple(uint64 batchNumber, uint64 timestamp, uint64 indexRepeatedStorageChanges, bytes32 newStateRoot, uint256 numberOfLayer1Txs, bytes32 priorityOperationsHash, bytes32 bootloaderHeapInitialContentsHash, bytes32 eventsQueueStateHash, bytes systemLogs, bytes operatorDAInput)"; +export const PRIORITY_OPS_BATCH_INFO_ABI_STRING = + "tuple(bytes32[] leftPath, bytes32[] rightPath, bytes32[] itemHashes)"; export const DIAMOND_CUT_DATA_ABI_STRING = "tuple(tuple(address facet, uint8 action, bool isFreezable, bytes4[] selectors)[] facetCuts, address initAddress, bytes initCalldata)"; export const FORCE_DEPLOYMENT_ABI_STRING = diff --git a/l1-contracts/test/foundry/unit/concrete/Executor/Authorization.t.sol b/l1-contracts/test/foundry/unit/concrete/Executor/Authorization.t.sol index 4d818e6bfe..59869620b4 100644 --- a/l1-contracts/test/foundry/unit/concrete/Executor/Authorization.t.sol +++ b/l1-contracts/test/foundry/unit/concrete/Executor/Authorization.t.sol @@ -45,7 +45,11 @@ contract AuthorizationTest is ExecutorTest { vm.prank(randomSigner); vm.expectRevert(abi.encodeWithSelector(Unauthorized.selector, randomSigner)); - executor.commitBatchesSharedBridge(uint256(0), storedBatchInfo, commitBatchInfoArray); + (uint256 commitBatchFrom, uint256 commitBatchTo, bytes memory commitData) = Utils.encodeCommitBatchesData( + storedBatchInfo, + commitBatchInfoArray + ); + executor.commitBatchesSharedBridge(uint256(0), commitBatchFrom, commitBatchTo, commitData); } function test_RevertWhen_ProvingByUnauthorisedAddress() public { @@ -55,7 +59,12 @@ contract AuthorizationTest is ExecutorTest { vm.prank(owner); vm.expectRevert(abi.encodeWithSelector(Unauthorized.selector, owner)); - executor.proveBatchesSharedBridge(uint256(0), storedBatchInfo, storedBatchInfoArray, proofInput); + (uint256 proveBatchFrom, uint256 proveBatchTo, bytes memory proveData) = Utils.encodeProveBatchesData( + storedBatchInfo, + storedBatchInfoArray, + proofInput + ); + executor.proveBatchesSharedBridge(uint256(0), proveBatchFrom, proveBatchTo, proveData); } function test_RevertWhen_ExecutingByUnauthorizedAddress() public { @@ -65,6 +74,10 @@ contract AuthorizationTest is ExecutorTest { vm.prank(randomSigner); vm.expectRevert(abi.encodeWithSelector(Unauthorized.selector, randomSigner)); - executor.executeBatchesSharedBridge(uint256(0), storedBatchInfoArray, Utils.emptyData()); + (uint256 executeBatchFrom, uint256 executeBatchTo, bytes memory executeData) = Utils.encodeExecuteBatchesData( + storedBatchInfoArray, + Utils.emptyData() + ); + executor.executeBatchesSharedBridge(uint256(0), executeBatchFrom, executeBatchTo, executeData); } } diff --git a/l1-contracts/test/foundry/unit/concrete/Executor/Committing.t.sol b/l1-contracts/test/foundry/unit/concrete/Executor/Committing.t.sol index e7bfa5fc0b..8ba961ed21 100644 --- a/l1-contracts/test/foundry/unit/concrete/Executor/Committing.t.sol +++ b/l1-contracts/test/foundry/unit/concrete/Executor/Committing.t.sol @@ -72,7 +72,11 @@ contract CommittingTest is ExecutorTest { keccak256(abi.encode(wrongGenesisStoredBatchInfo)) ) ); - executor.commitBatchesSharedBridge(uint256(0), wrongGenesisStoredBatchInfo, newCommitBatchInfoArray); + (uint256 commitBatchFrom, uint256 commitBatchTo, bytes memory commitData) = Utils.encodeCommitBatchesData( + wrongGenesisStoredBatchInfo, + newCommitBatchInfoArray + ); + executor.commitBatchesSharedBridge(uint256(0), commitBatchFrom, commitBatchTo, commitData); } function test_RevertWhen_CommittingWithWrongOrderOfBatches() public { @@ -85,7 +89,11 @@ contract CommittingTest is ExecutorTest { vm.prank(validator); vm.expectRevert(abi.encodeWithSelector(BatchNumberMismatch.selector, uint256(1), uint256(2))); - executor.commitBatchesSharedBridge(uint256(0), genesisStoredBatchInfo, wrongNewCommitBatchInfoArray); + (uint256 commitBatchFrom, uint256 commitBatchTo, bytes memory commitData) = Utils.encodeCommitBatchesData( + genesisStoredBatchInfo, + wrongNewCommitBatchInfoArray + ); + executor.commitBatchesSharedBridge(uint256(0), commitBatchFrom, commitBatchTo, commitData); } function test_RevertWhen_CommittingWithWrongNewBatchTimestamp() public { @@ -110,7 +118,11 @@ contract CommittingTest is ExecutorTest { vm.blobhashes(defaultBlobVersionedHashes); vm.expectRevert(TimestampError.selector); - executor.commitBatchesSharedBridge(uint256(0), genesisStoredBatchInfo, wrongNewCommitBatchInfoArray); + (uint256 commitBatchFrom, uint256 commitBatchTo, bytes memory commitData) = Utils.encodeCommitBatchesData( + genesisStoredBatchInfo, + wrongNewCommitBatchInfoArray + ); + executor.commitBatchesSharedBridge(uint256(0), commitBatchFrom, commitBatchTo, commitData); } function test_RevertWhen_CommittingWithTooSmallNewBatchTimestamp() public { @@ -135,7 +147,11 @@ contract CommittingTest is ExecutorTest { vm.blobhashes(defaultBlobVersionedHashes); vm.expectRevert(abi.encodeWithSelector(TimeNotReached.selector, 1, 2)); - executor.commitBatchesSharedBridge(uint256(0), genesisStoredBatchInfo, wrongNewCommitBatchInfoArray); + (uint256 commitBatchFrom, uint256 commitBatchTo, bytes memory commitData) = Utils.encodeCommitBatchesData( + genesisStoredBatchInfo, + wrongNewCommitBatchInfoArray + ); + executor.commitBatchesSharedBridge(uint256(0), commitBatchFrom, commitBatchTo, commitData); } function test_RevertWhen_CommittingTooBigLastL2BatchTimestamp() public { @@ -160,7 +176,11 @@ contract CommittingTest is ExecutorTest { vm.blobhashes(defaultBlobVersionedHashes); vm.expectRevert(abi.encodeWithSelector(L2TimestampTooBig.selector)); - executor.commitBatchesSharedBridge(uint256(0), genesisStoredBatchInfo, wrongNewCommitBatchInfoArray); + (uint256 commitBatchFrom, uint256 commitBatchTo, bytes memory commitData) = Utils.encodeCommitBatchesData( + genesisStoredBatchInfo, + wrongNewCommitBatchInfoArray + ); + executor.commitBatchesSharedBridge(uint256(0), commitBatchFrom, commitBatchTo, commitData); } function test_RevertWhen_CommittingWithWrongPreviousBatchHash() public { @@ -184,7 +204,11 @@ contract CommittingTest is ExecutorTest { vm.blobhashes(defaultBlobVersionedHashes); vm.expectRevert(abi.encodeWithSelector(HashMismatch.selector, wrongPreviousBatchHash, bytes32(0))); - executor.commitBatchesSharedBridge(uint256(0), genesisStoredBatchInfo, wrongNewCommitBatchInfoArray); + (uint256 commitBatchFrom, uint256 commitBatchTo, bytes memory commitData) = Utils.encodeCommitBatchesData( + genesisStoredBatchInfo, + wrongNewCommitBatchInfoArray + ); + executor.commitBatchesSharedBridge(uint256(0), commitBatchFrom, commitBatchTo, commitData); } // function test_RevertWhen_CommittingWithoutProcessingSystemContextLog() public { @@ -202,7 +226,9 @@ contract CommittingTest is ExecutorTest { // vm.blobhashes(defaultBlobVersionedHashes); // vm.expectRevert(abi.encodeWithSelector(MissingSystemLogs.selector, 8191, 8183)); - // executor.commitBatchesSharedBridge(uint256(0), genesisStoredBatchInfo, wrongNewCommitBatchInfoArray); + // (uint256 commitBatchFrom, uint256 commitBatchTo, bytes memory commitData) = + // Utils.encodeCommitBatchesData(genesisStoredBatchInfo, wrongNewCommitBatchInfoArray); + // executor.commitBatchesSharedBridge(uint256(0), commitBatchFrom, commitBatchTo, commitData); // } function test_RevertWhen_CommittingWithProcessingSystemContextLogTwice() public { @@ -230,7 +256,11 @@ contract CommittingTest is ExecutorTest { vm.blobhashes(defaultBlobVersionedHashes); vm.expectRevert(abi.encodeWithSelector(LogAlreadyProcessed.selector, 1)); - executor.commitBatchesSharedBridge(uint256(0), genesisStoredBatchInfo, wrongNewCommitBatchInfoArray); + (uint256 commitBatchFrom, uint256 commitBatchTo, bytes memory commitData) = Utils.encodeCommitBatchesData( + genesisStoredBatchInfo, + wrongNewCommitBatchInfoArray + ); + executor.commitBatchesSharedBridge(uint256(0), commitBatchFrom, commitBatchTo, commitData); } function test_RevertWhen_UnexpectedL2ToL1Log() public { @@ -258,7 +288,11 @@ contract CommittingTest is ExecutorTest { uint256(SystemLogKey.PACKED_BATCH_AND_L2_BLOCK_TIMESTAMP_KEY) ) ); - executor.commitBatchesSharedBridge(uint256(0), genesisStoredBatchInfo, wrongNewCommitBatchInfoArray); + (uint256 commitBatchFrom, uint256 commitBatchTo, bytes memory commitData) = Utils.encodeCommitBatchesData( + genesisStoredBatchInfo, + wrongNewCommitBatchInfoArray + ); + executor.commitBatchesSharedBridge(uint256(0), commitBatchFrom, commitBatchTo, commitData); } function test_RevertWhen_CommittingWithWrongCanonicalTxHash() public { @@ -282,7 +316,11 @@ contract CommittingTest is ExecutorTest { vm.prank(validator); vm.expectRevert(abi.encodeWithSelector(HashMismatch.selector, wrongChainedPriorityHash, keccak256(""))); - executor.commitBatchesSharedBridge(uint256(0), genesisStoredBatchInfo, wrongNewCommitBatchInfoArray); + (uint256 commitBatchFrom, uint256 commitBatchTo, bytes memory commitData) = Utils.encodeCommitBatchesData( + genesisStoredBatchInfo, + wrongNewCommitBatchInfoArray + ); + executor.commitBatchesSharedBridge(uint256(0), commitBatchFrom, commitBatchTo, commitData); } function test_RevertWhen_CommittingWithWrongNumberOfLayer1txs() public { @@ -306,7 +344,11 @@ contract CommittingTest is ExecutorTest { vm.prank(validator); vm.expectRevert(abi.encodeWithSelector(ValueMismatch.selector, uint256(bytes32(bytes1(0x01))), uint256(2))); - executor.commitBatchesSharedBridge(uint256(0), genesisStoredBatchInfo, wrongNewCommitBatchInfoArray); + (uint256 commitBatchFrom, uint256 commitBatchTo, bytes memory commitData) = Utils.encodeCommitBatchesData( + genesisStoredBatchInfo, + wrongNewCommitBatchInfoArray + ); + executor.commitBatchesSharedBridge(uint256(0), commitBatchFrom, commitBatchTo, commitData); } function test_RevertWhen_CommittingWithUnknownSystemLogKey() public { @@ -326,7 +368,11 @@ contract CommittingTest is ExecutorTest { vm.prank(validator); vm.expectRevert(abi.encodeWithSelector(UnexpectedSystemLog.selector, uint256(119))); - executor.commitBatchesSharedBridge(uint256(0), genesisStoredBatchInfo, wrongNewCommitBatchInfoArray); + (uint256 commitBatchFrom, uint256 commitBatchTo, bytes memory commitData) = Utils.encodeCommitBatchesData( + genesisStoredBatchInfo, + wrongNewCommitBatchInfoArray + ); + executor.commitBatchesSharedBridge(uint256(0), commitBatchFrom, commitBatchTo, commitData); } function test_RevertWhen_SystemLogIsFromIncorrectAddress() public { @@ -354,7 +400,11 @@ contract CommittingTest is ExecutorTest { vm.prank(validator); vm.expectRevert(abi.encodeWithSelector(InvalidLogSender.selector, wrongAddress, i)); - executor.commitBatchesSharedBridge(uint256(0), genesisStoredBatchInfo, wrongNewCommitBatchInfoArray); + (uint256 commitBatchFrom, uint256 commitBatchTo, bytes memory commitData) = Utils.encodeCommitBatchesData( + genesisStoredBatchInfo, + wrongNewCommitBatchInfoArray + ); + executor.commitBatchesSharedBridge(uint256(0), commitBatchFrom, commitBatchTo, commitData); } } @@ -374,7 +424,9 @@ contract CommittingTest is ExecutorTest { // uint256 allLogsProcessed = uint256(8191); // vm.expectRevert(abi.encodeWithSelector(MissingSystemLogs.selector, 8191, allLogsProcessed ^ (1 << i))); - // executor.commitBatchesSharedBridge(uint256(0), genesisStoredBatchInfo, wrongNewCommitBatchInfoArray); + // (uint256 commitBatchFrom, uint256 commitBatchTo, bytes memory commitData) = + // Utils.encodeCommitBatchesData(genesisStoredBatchInfo, wrongNewCommitBatchInfoArray); + // executor.commitBatchesSharedBridge(uint256(0), commitBatchFrom, commitBatchTo, commitData); // } // } @@ -439,8 +491,11 @@ contract CommittingTest is ExecutorTest { vm.prank(validator); vm.blobhashes(defaultBlobVersionedHashes); vm.recordLogs(); - - executor.commitBatchesSharedBridge(uint256(0), genesisStoredBatchInfo, correctCommitBatchInfoArray); + (uint256 commitBatchFrom, uint256 commitBatchTo, bytes memory commitData) = Utils.encodeCommitBatchesData( + genesisStoredBatchInfo, + correctCommitBatchInfoArray + ); + executor.commitBatchesSharedBridge(uint256(0), commitBatchFrom, commitBatchTo, commitData); Vm.Log[] memory entries = vm.getRecordedLogs(); @@ -476,7 +531,11 @@ contract CommittingTest is ExecutorTest { vm.recordLogs(); - executor.commitBatchesSharedBridge(uint256(0), genesisStoredBatchInfo, correctCommitBatchInfoArray); + (uint256 commitBatchFrom, uint256 commitBatchTo, bytes memory commitData) = Utils.encodeCommitBatchesData( + genesisStoredBatchInfo, + correctCommitBatchInfoArray + ); + executor.commitBatchesSharedBridge(uint256(0), commitBatchFrom, commitBatchTo, commitData); Vm.Log[] memory entries = vm.getRecordedLogs(); @@ -541,7 +600,11 @@ contract CommittingTest is ExecutorTest { vm.recordLogs(); - executor.commitBatchesSharedBridge(uint256(0), genesisStoredBatchInfo, correctCommitBatchInfoArray); + (uint256 commitBatchFrom, uint256 commitBatchTo, bytes memory commitData) = Utils.encodeCommitBatchesData( + genesisStoredBatchInfo, + correctCommitBatchInfoArray + ); + executor.commitBatchesSharedBridge(uint256(0), commitBatchFrom, commitBatchTo, commitData); Vm.Log[] memory entries = vm.getRecordedLogs(); @@ -565,7 +628,11 @@ contract CommittingTest is ExecutorTest { vm.prank(validator); vm.expectRevert(abi.encodeWithSelector(CanOnlyProcessOneBatch.selector)); - executor.commitBatchesSharedBridge(uint256(0), genesisStoredBatchInfo, correctCommitBatchInfoArray); + (uint256 commitBatchFrom, uint256 commitBatchTo, bytes memory commitData) = Utils.encodeCommitBatchesData( + genesisStoredBatchInfo, + correctCommitBatchInfoArray + ); + executor.commitBatchesSharedBridge(uint256(0), commitBatchFrom, commitBatchTo, commitData); } // function test_RevertWhen_EmptyPubdataCommitments() public { @@ -589,7 +656,9 @@ contract CommittingTest is ExecutorTest { // vm.prank(validator); // vm.expectRevert(PubdataCommitmentsEmpty.selector); - // executor.commitBatchesSharedBridge(uint256(0), genesisStoredBatchInfo, correctCommitBatchInfoArray); + // (uint256 commitBatchFrom, uint256 commitBatchTo, bytes memory commitData) = + // Utils.encodeCommitBatchesData(genesisStoredBatchInfo, correctCommitBatchInfoArray); + // executor.commitBatchesSharedBridge(uint256(0), commitBatchFrom, commitBatchTo, commitData); // } // function test_RevertWhen_PartialPubdataCommitment() public { @@ -625,7 +694,9 @@ contract CommittingTest is ExecutorTest { // vm.blobhashes(defaultBlobVersionedHashes); // vm.expectRevert(InvalidPubdataCommitmentsSize.selector); - // executor.commitBatchesSharedBridge(uint256(0), genesisStoredBatchInfo, correctCommitBatchInfoArray); + // (uint256 commitBatchFrom, uint256 commitBatchTo, bytes memory commitData) = + // Utils.encodeCommitBatchesData(genesisStoredBatchInfo, correctCommitBatchInfoArray); + // executor.commitBatchesSharedBridge(uint256(0), commitBatchFrom, commitBatchTo, commitData); // } function test_RevertWhen_TooManyPubdataCommitments() public { @@ -662,7 +733,11 @@ contract CommittingTest is ExecutorTest { vm.prank(validator); vm.expectRevert(InvalidPubdataCommitmentsSize.selector); - executor.commitBatchesSharedBridge(uint256(0), genesisStoredBatchInfo, correctCommitBatchInfoArray); + (uint256 commitBatchFrom, uint256 commitBatchTo, bytes memory commitData) = Utils.encodeCommitBatchesData( + genesisStoredBatchInfo, + correctCommitBatchInfoArray + ); + executor.commitBatchesSharedBridge(uint256(0), commitBatchFrom, commitBatchTo, commitData); } function test_RevertWhen_NotEnoughPubdataCommitments() public { @@ -689,7 +764,11 @@ contract CommittingTest is ExecutorTest { vm.blobhashes(versionedHashes); vm.expectRevert(abi.encodeWithSelector(NonEmptyBlobVersionHash.selector, uint256(1))); - executor.commitBatchesSharedBridge(uint256(0), genesisStoredBatchInfo, correctCommitBatchInfoArray); + (uint256 commitBatchFrom, uint256 commitBatchTo, bytes memory commitData) = Utils.encodeCommitBatchesData( + genesisStoredBatchInfo, + correctCommitBatchInfoArray + ); + executor.commitBatchesSharedBridge(uint256(0), commitBatchFrom, commitBatchTo, commitData); vm.clearMockedCalls(); } @@ -715,7 +794,11 @@ contract CommittingTest is ExecutorTest { vm.prank(validator); vm.expectRevert(abi.encodeWithSelector(EmptyBlobVersionHash.selector, 0)); - executor.commitBatchesSharedBridge(uint256(0), genesisStoredBatchInfo, correctCommitBatchInfoArray); + (uint256 commitBatchFrom, uint256 commitBatchTo, bytes memory commitData) = Utils.encodeCommitBatchesData( + genesisStoredBatchInfo, + correctCommitBatchInfoArray + ); + executor.commitBatchesSharedBridge(uint256(0), commitBatchFrom, commitBatchTo, commitData); vm.clearMockedCalls(); } @@ -744,7 +827,11 @@ contract CommittingTest is ExecutorTest { vm.blobhashes(blobVersionedHashes); vm.expectRevert(abi.encodeWithSelector(NonEmptyBlobVersionHash.selector, uint256(1))); - executor.commitBatchesSharedBridge(uint256(0), genesisStoredBatchInfo, correctCommitBatchInfoArray); + (uint256 commitBatchFrom, uint256 commitBatchTo, bytes memory commitData) = Utils.encodeCommitBatchesData( + genesisStoredBatchInfo, + correctCommitBatchInfoArray + ); + executor.commitBatchesSharedBridge(uint256(0), commitBatchFrom, commitBatchTo, commitData); vm.clearMockedCalls(); } @@ -798,7 +885,9 @@ contract CommittingTest is ExecutorTest { // vm.prank(validator); // vm.expectRevert(abi.encodeWithSelector(BlobHashCommitmentError.selector, uint256(1), true, false)); - // executor.commitBatchesSharedBridge(uint256(0), genesisStoredBatchInfo, correctCommitBatchInfoArray); + // (uint256 commitBatchFrom, uint256 commitBatchTo, bytes memory commitData) = + // Utils.encodeCommitBatchesData(genesisStoredBatchInfo, correctCommitBatchInfoArray); + // executor.commitBatchesSharedBridge(uint256(0), commitBatchFrom, commitBatchTo, commitData); // } // function test_RevertWhen_SecondBlobLinearHashNotZeroWithEmptyCommitment() public { diff --git a/l1-contracts/test/foundry/unit/concrete/Executor/Executing.t.sol b/l1-contracts/test/foundry/unit/concrete/Executor/Executing.t.sol index 85d828ea0f..fbfc92fd44 100644 --- a/l1-contracts/test/foundry/unit/concrete/Executor/Executing.t.sol +++ b/l1-contracts/test/foundry/unit/concrete/Executor/Executing.t.sol @@ -74,7 +74,11 @@ contract ExecutingTest is ExecutorTest { vm.prank(validator); vm.blobhashes(blobVersionedHashes); vm.recordLogs(); - executor.commitBatchesSharedBridge(uint256(0), genesisStoredBatchInfo, commitBatchInfoArray); + (uint256 commitBatchFrom, uint256 commitBatchTo, bytes memory commitData) = Utils.encodeCommitBatchesData( + genesisStoredBatchInfo, + commitBatchInfoArray + ); + executor.commitBatchesSharedBridge(uint256(0), commitBatchFrom, commitBatchTo, commitData); Vm.Log[] memory entries = vm.getRecordedLogs(); newStoredBatchInfo = IExecutor.StoredBatchInfo({ @@ -92,7 +96,12 @@ contract ExecutingTest is ExecutorTest { storedBatchInfoArray[0] = newStoredBatchInfo; vm.prank(validator); - executor.proveBatchesSharedBridge(uint256(0), genesisStoredBatchInfo, storedBatchInfoArray, proofInput); + (uint256 proveBatchFrom, uint256 proveBatchTo, bytes memory proveData) = Utils.encodeProveBatchesData( + genesisStoredBatchInfo, + storedBatchInfoArray, + proofInput + ); + executor.proveBatchesSharedBridge(uint256(0), proveBatchFrom, proveBatchTo, proveData); } function test_RevertWhen_ExecutingBlockWithWrongBatchNumber() public { @@ -104,11 +113,11 @@ contract ExecutingTest is ExecutorTest { vm.prank(validator); vm.expectRevert(NonSequentialBatch.selector); - executor.executeBatchesSharedBridge( - uint256(0), + (uint256 executeBatchFrom, uint256 executeBatchTo, bytes memory executeData) = Utils.encodeExecuteBatchesData( storedBatchInfoArray, Utils.generatePriorityOps(storedBatchInfoArray.length) ); + executor.executeBatchesSharedBridge(uint256(0), executeBatchFrom, executeBatchTo, executeData); } function test_RevertWhen_ExecutingBlockWithWrongData() public { @@ -126,11 +135,11 @@ contract ExecutingTest is ExecutorTest { keccak256(abi.encode(wrongNewStoredBatchInfo)) ) ); - executor.executeBatchesSharedBridge( - uint256(0), + (uint256 executeBatchFrom, uint256 executeBatchTo, bytes memory executeData) = Utils.encodeExecuteBatchesData( storedBatchInfoArray, Utils.generatePriorityOps(storedBatchInfoArray.length) ); + executor.executeBatchesSharedBridge(uint256(0), executeBatchFrom, executeBatchTo, executeData); } function test_RevertWhen_ExecutingRevertedBlockWithoutCommittingAndProvingAgain() public { @@ -142,11 +151,11 @@ contract ExecutingTest is ExecutorTest { vm.prank(validator); vm.expectRevert(CantExecuteUnprovenBatches.selector); - executor.executeBatchesSharedBridge( - uint256(0), + (uint256 executeBatchFrom, uint256 executeBatchTo, bytes memory executeData) = Utils.encodeExecuteBatchesData( storedBatchInfoArray, Utils.generatePriorityOps(storedBatchInfoArray.length) ); + executor.executeBatchesSharedBridge(uint256(0), executeBatchFrom, executeBatchTo, executeData); } function test_RevertWhen_ExecutingUnavailablePriorityOperationHash() public { @@ -187,7 +196,11 @@ contract ExecutingTest is ExecutorTest { vm.prank(validator); vm.blobhashes(blobVersionedHashes); vm.recordLogs(); - executor.commitBatchesSharedBridge(uint256(0), genesisStoredBatchInfo, correctNewCommitBatchInfoArray); + (uint256 commitBatchFrom, uint256 commitBatchTo, bytes memory commitData) = Utils.encodeCommitBatchesData( + genesisStoredBatchInfo, + correctNewCommitBatchInfoArray + ); + executor.commitBatchesSharedBridge(uint256(0), commitBatchFrom, commitBatchTo, commitData); Vm.Log[] memory entries = vm.getRecordedLogs(); IExecutor.StoredBatchInfo memory correctNewStoredBatchInfo = newStoredBatchInfo; @@ -200,20 +213,27 @@ contract ExecutingTest is ExecutorTest { correctNewStoredBatchInfoArray[0] = correctNewStoredBatchInfo; vm.prank(validator); - executor.proveBatchesSharedBridge( - uint256(0), - genesisStoredBatchInfo, - correctNewStoredBatchInfoArray, - proofInput - ); + uint256 processBatchFrom; + uint256 processBatchTo; + bytes memory processData; + { + (processBatchFrom, processBatchTo, processData) = Utils.encodeProveBatchesData( + genesisStoredBatchInfo, + correctNewStoredBatchInfoArray, + proofInput + ); + executor.proveBatchesSharedBridge(uint256(0), processBatchFrom, processBatchTo, processData); + } vm.prank(validator); vm.expectRevert(QueueIsEmpty.selector); - executor.executeBatchesSharedBridge( - uint256(0), - correctNewStoredBatchInfoArray, - Utils.generatePriorityOps(correctNewStoredBatchInfoArray.length) - ); + { + (processBatchFrom, processBatchTo, processData) = Utils.encodeExecuteBatchesData( + correctNewStoredBatchInfoArray, + Utils.generatePriorityOps(correctNewStoredBatchInfoArray.length) + ); + executor.executeBatchesSharedBridge(uint256(0), processBatchFrom, processBatchTo, processData); + } } function test_RevertWhen_ExecutingWithUnmatchedPriorityOperationHash() public { @@ -253,7 +273,11 @@ contract ExecutingTest is ExecutorTest { vm.prank(validator); vm.blobhashes(blobVersionedHashes); vm.recordLogs(); - executor.commitBatchesSharedBridge(uint256(0), genesisStoredBatchInfo, correctNewCommitBatchInfoArray); + (uint256 commitBatchFrom, uint256 commitBatchTo, bytes memory commitData) = Utils.encodeCommitBatchesData( + genesisStoredBatchInfo, + correctNewCommitBatchInfoArray + ); + executor.commitBatchesSharedBridge(uint256(0), commitBatchFrom, commitBatchTo, commitData); Vm.Log[] memory entries = vm.getRecordedLogs(); IExecutor.StoredBatchInfo memory correctNewStoredBatchInfo = newStoredBatchInfo; @@ -266,12 +290,17 @@ contract ExecutingTest is ExecutorTest { correctNewStoredBatchInfoArray[0] = correctNewStoredBatchInfo; vm.prank(validator); - executor.proveBatchesSharedBridge( - uint256(0), - genesisStoredBatchInfo, - correctNewStoredBatchInfoArray, - proofInput - ); + uint256 processBatchFrom; + uint256 processBatchTo; + bytes memory processData; + { + (processBatchFrom, processBatchTo, processData) = Utils.encodeProveBatchesData( + genesisStoredBatchInfo, + correctNewStoredBatchInfoArray, + proofInput + ); + executor.proveBatchesSharedBridge(uint256(0), processBatchFrom, processBatchTo, processData); + } bytes32 randomFactoryDeps0 = Utils.randomBytes32("randomFactoryDeps0"); @@ -296,11 +325,14 @@ contract ExecutingTest is ExecutorTest { vm.prank(validator); vm.expectRevert(PriorityOperationsRollingHashMismatch.selector); - executor.executeBatchesSharedBridge( - uint256(0), - correctNewStoredBatchInfoArray, - Utils.generatePriorityOps(correctNewStoredBatchInfoArray.length) - ); + + { + (processBatchFrom, processBatchTo, processData) = Utils.encodeExecuteBatchesData( + correctNewStoredBatchInfoArray, + Utils.generatePriorityOps(correctNewStoredBatchInfoArray.length) + ); + executor.executeBatchesSharedBridge(uint256(0), processBatchFrom, processBatchTo, processData); + } } function test_RevertWhen_CommittingBlockWithWrongPreviousBatchHash() public { @@ -330,7 +362,11 @@ contract ExecutingTest is ExecutorTest { vm.expectRevert( abi.encodeWithSelector(BatchHashMismatch.selector, storedBatchHash, keccak256(abi.encode(genesisBlock))) ); - executor.commitBatchesSharedBridge(uint256(0), genesisBlock, correctNewCommitBatchInfoArray); + (uint256 commitBatchFrom, uint256 commitBatchTo, bytes memory commitData) = Utils.encodeCommitBatchesData( + genesisBlock, + correctNewCommitBatchInfoArray + ); + executor.commitBatchesSharedBridge(uint256(0), commitBatchFrom, commitBatchTo, commitData); } function test_ShouldExecuteBatchesuccessfully() public { @@ -338,11 +374,11 @@ contract ExecutingTest is ExecutorTest { storedBatchInfoArray[0] = newStoredBatchInfo; vm.prank(validator); - executor.executeBatchesSharedBridge( - uint256(0), + (uint256 executeBatchFrom, uint256 executeBatchTo, bytes memory executeData) = Utils.encodeExecuteBatchesData( storedBatchInfoArray, Utils.generatePriorityOps(storedBatchInfoArray.length) ); + executor.executeBatchesSharedBridge(uint256(0), executeBatchFrom, executeBatchTo, executeData); uint256 totalBlocksExecuted = getters.getTotalBlocksExecuted(); assertEq(totalBlocksExecuted, 1); diff --git a/l1-contracts/test/foundry/unit/concrete/Executor/Proving.t.sol b/l1-contracts/test/foundry/unit/concrete/Executor/Proving.t.sol index c5b97b54db..73b104186f 100644 --- a/l1-contracts/test/foundry/unit/concrete/Executor/Proving.t.sol +++ b/l1-contracts/test/foundry/unit/concrete/Executor/Proving.t.sol @@ -41,7 +41,11 @@ contract ProvingTest is ExecutorTest { vm.prank(validator); vm.blobhashes(blobVersionedHashes); vm.recordLogs(); - executor.commitBatchesSharedBridge(uint256(0), genesisStoredBatchInfo, commitBatchInfoArray); + (uint256 commitBatchFrom, uint256 commitBatchTo, bytes memory commitData) = Utils.encodeCommitBatchesData( + genesisStoredBatchInfo, + commitBatchInfoArray + ); + executor.commitBatchesSharedBridge(uint256(0), commitBatchFrom, commitBatchTo, commitData); Vm.Log[] memory entries = vm.getRecordedLogs(); newStoredBatchInfo = IExecutor.StoredBatchInfo({ @@ -106,7 +110,12 @@ contract ProvingTest is ExecutorTest { keccak256(abi.encode(wrongPreviousStoredBatchInfo)) ) ); - executor.proveBatchesSharedBridge(uint256(0), wrongPreviousStoredBatchInfo, storedBatchInfoArray, proofInput); + (uint256 proveBatchFrom, uint256 proveBatchTo, bytes memory proveData) = Utils.encodeProveBatchesData( + wrongPreviousStoredBatchInfo, + storedBatchInfoArray, + proofInput + ); + executor.proveBatchesSharedBridge(uint256(0), proveBatchFrom, proveBatchTo, proveData); } function test_RevertWhen_ProvingWithWrongCommittedBlock() public { @@ -125,7 +134,12 @@ contract ProvingTest is ExecutorTest { keccak256(abi.encode(wrongNewStoredBatchInfo)) ) ); - executor.proveBatchesSharedBridge(uint256(0), genesisStoredBatchInfo, storedBatchInfoArray, proofInput); + (uint256 proveBatchFrom, uint256 proveBatchTo, bytes memory proveData) = Utils.encodeProveBatchesData( + genesisStoredBatchInfo, + storedBatchInfoArray, + proofInput + ); + executor.proveBatchesSharedBridge(uint256(0), proveBatchFrom, proveBatchTo, proveData); } function test_RevertWhen_ProvingRevertedBlockWithoutCommittingAgain() public { @@ -138,7 +152,12 @@ contract ProvingTest is ExecutorTest { vm.prank(validator); vm.expectRevert(VerifiedBatchesExceedsCommittedBatches.selector); - executor.proveBatchesSharedBridge(uint256(0), genesisStoredBatchInfo, storedBatchInfoArray, proofInput); + (uint256 proveBatchFrom, uint256 proveBatchTo, bytes memory proveData) = Utils.encodeProveBatchesData( + genesisStoredBatchInfo, + storedBatchInfoArray, + proofInput + ); + executor.proveBatchesSharedBridge(uint256(0), proveBatchFrom, proveBatchTo, proveData); } function test_SuccessfulProve() public { @@ -146,8 +165,12 @@ contract ProvingTest is ExecutorTest { storedBatchInfoArray[0] = newStoredBatchInfo; vm.prank(validator); - - executor.proveBatchesSharedBridge(uint256(0), genesisStoredBatchInfo, storedBatchInfoArray, proofInput); + (uint256 proveBatchFrom, uint256 proveBatchTo, bytes memory proveData) = Utils.encodeProveBatchesData( + genesisStoredBatchInfo, + storedBatchInfoArray, + proofInput + ); + executor.proveBatchesSharedBridge(uint256(0), proveBatchFrom, proveBatchTo, proveData); uint256 totalBlocksVerified = getters.getTotalBlocksVerified(); assertEq(totalBlocksVerified, 1); diff --git a/l1-contracts/test/foundry/unit/concrete/Executor/Reverting.t.sol b/l1-contracts/test/foundry/unit/concrete/Executor/Reverting.t.sol index 6ce83e15e1..ba2fc4b604 100644 --- a/l1-contracts/test/foundry/unit/concrete/Executor/Reverting.t.sol +++ b/l1-contracts/test/foundry/unit/concrete/Executor/Reverting.t.sol @@ -40,7 +40,11 @@ contract RevertingTest is ExecutorTest { vm.prank(validator); vm.blobhashes(blobVersionedHashes); vm.recordLogs(); - executor.commitBatchesSharedBridge(uint256(0), genesisStoredBatchInfo, commitBatchInfoArray); + (uint256 commitBatchFrom, uint256 commitBatchTo, bytes memory commitData) = Utils.encodeCommitBatchesData( + genesisStoredBatchInfo, + commitBatchInfoArray + ); + executor.commitBatchesSharedBridge(uint256(0), commitBatchFrom, commitBatchTo, commitData); Vm.Log[] memory entries = vm.getRecordedLogs(); newStoredBatchInfo = IExecutor.StoredBatchInfo({ @@ -58,8 +62,12 @@ contract RevertingTest is ExecutorTest { storedBatchInfoArray[0] = newStoredBatchInfo; vm.prank(validator); - - executor.proveBatchesSharedBridge(uint256(0), genesisStoredBatchInfo, storedBatchInfoArray, proofInput); + (uint256 proveBatchFrom, uint256 proveBatchTo, bytes memory proveData) = Utils.encodeProveBatchesData( + genesisStoredBatchInfo, + storedBatchInfoArray, + proofInput + ); + executor.proveBatchesSharedBridge(uint256(0), proveBatchFrom, proveBatchTo, proveData); } function setUpCommitBatch() public { diff --git a/l1-contracts/test/foundry/unit/concrete/Executor/_Executor_Shared.t.sol b/l1-contracts/test/foundry/unit/concrete/Executor/_Executor_Shared.t.sol index 821cbf8fe6..07aab22c78 100644 --- a/l1-contracts/test/foundry/unit/concrete/Executor/_Executor_Shared.t.sol +++ b/l1-contracts/test/foundry/unit/concrete/Executor/_Executor_Shared.t.sol @@ -55,7 +55,7 @@ contract ExecutorTest is Test { uint256 eraChainId; IExecutor.StoredBatchInfo internal genesisStoredBatchInfo; - IExecutor.ProofInput internal proofInput; + uint256[] internal proofInput; function getAdminSelectors() private view returns (bytes4[] memory) { bytes4[] memory selectors = new bytes4[](12); @@ -259,10 +259,6 @@ contract ExecutorTest is Test { vm.prank(address(owner)); admin.setDAValidatorPair(address(rollupL1DAValidator), L2_DA_VALIDATOR_ADDRESS); - uint256[] memory recursiveAggregationInput; - uint256[] memory serializedProof; - proofInput = IExecutor.ProofInput(recursiveAggregationInput, serializedProof); - // foundry's default value is 1 for the block's timestamp, it is expected // that block.timestamp > COMMIT_TIMESTAMP_NOT_OLDER + 1 vm.warp(COMMIT_TIMESTAMP_NOT_OLDER + 1 + 1); diff --git a/l1-contracts/test/foundry/unit/concrete/Utils/Utils.sol b/l1-contracts/test/foundry/unit/concrete/Utils/Utils.sol index 2cb954e9c7..8ab52c9769 100644 --- a/l1-contracts/test/foundry/unit/concrete/Utils/Utils.sol +++ b/l1-contracts/test/foundry/unit/concrete/Utils/Utils.sol @@ -155,17 +155,6 @@ library Utils { }); } - function createProofInput() public pure returns (IExecutor.ProofInput memory) { - uint256[] memory recursiveAggregationInput; - uint256[] memory serializedProof; - - return - IExecutor.ProofInput({ - recursiveAggregationInput: recursiveAggregationInput, - serializedProof: serializedProof - }); - } - function encodePacked(bytes[] memory data) public pure returns (bytes memory) { bytes memory result; for (uint256 i = 0; i < data.length; i++) { @@ -174,6 +163,40 @@ library Utils { return result; } + function encodeCommitBatchesData( + IExecutor.StoredBatchInfo memory _lastCommittedBatchData, + IExecutor.CommitBatchInfo[] memory _newBatchesData + ) internal pure returns (uint256, uint256, bytes memory) { + return ( + _newBatchesData[0].batchNumber, + _newBatchesData[_newBatchesData.length - 1].batchNumber, + bytes.concat(bytes1(0x00), abi.encode(_lastCommittedBatchData, _newBatchesData)) + ); + } + + function encodeProveBatchesData( + IExecutor.StoredBatchInfo memory _prevBatch, + IExecutor.StoredBatchInfo[] memory _committedBatches, + uint256[] memory _proof + ) internal pure returns (uint256, uint256, bytes memory) { + return ( + _committedBatches[0].batchNumber, + _committedBatches[_committedBatches.length - 1].batchNumber, + bytes.concat(bytes1(0x00), abi.encode(_prevBatch, _committedBatches, _proof)) + ); + } + + function encodeExecuteBatchesData( + IExecutor.StoredBatchInfo[] memory _batchesData, + PriorityOpsBatchInfo[] memory _priorityOpsData + ) internal pure returns (uint256, uint256, bytes memory) { + return ( + _batchesData[0].batchNumber, + _batchesData[_batchesData.length - 1].batchNumber, + bytes.concat(bytes1(0x00), abi.encode(_batchesData, _priorityOpsData)) + ); + } + function getAdminSelectors() public pure returns (bytes4[] memory) { bytes4[] memory selectors = new bytes4[](12); selectors[0] = AdminFacet.setPendingAdmin.selector; diff --git a/l1-contracts/test/foundry/unit/concrete/ValidatorTimelock/ValidatorTimelock.t.sol b/l1-contracts/test/foundry/unit/concrete/ValidatorTimelock/ValidatorTimelock.t.sol index 9a7b7bcf4f..3725f54e2c 100644 --- a/l1-contracts/test/foundry/unit/concrete/ValidatorTimelock/ValidatorTimelock.t.sol +++ b/l1-contracts/test/foundry/unit/concrete/ValidatorTimelock/ValidatorTimelock.t.sol @@ -92,7 +92,11 @@ contract ValidatorTimelockTest is Test { batchesToCommit[0] = batchToCommit; vm.prank(alice); - validator.commitBatchesSharedBridge(chainId, storedBatch, batchesToCommit); + (uint256 commitBatchFrom, uint256 commitBatchTo, bytes memory commitData) = Utils.encodeCommitBatchesData( + storedBatch, + batchesToCommit + ); + validator.commitBatchesSharedBridge(chainId, commitBatchFrom, commitBatchTo, commitData); } function test_setChainTypeManager() public { @@ -140,7 +144,11 @@ contract ValidatorTimelockTest is Test { batchesToCommit[0] = batchToCommit; vm.prank(alice); - validator.commitBatchesSharedBridge(chainId, storedBatch, batchesToCommit); + (uint256 commitBatchFrom, uint256 commitBatchTo, bytes memory commitData) = Utils.encodeCommitBatchesData( + storedBatch, + batchesToCommit + ); + validator.commitBatchesSharedBridge(chainId, commitBatchFrom, commitBatchTo, commitData); assert(validator.getCommittedBatchTimestamp(chainId, batchNumber) == timestamp); } @@ -155,7 +163,11 @@ contract ValidatorTimelockTest is Test { batchesToCommit[0] = batchToCommit; vm.prank(alice); - validator.commitBatchesSharedBridge(chainId, storedBatch, batchesToCommit); + (uint256 commitBatchFrom, uint256 commitBatchTo, bytes memory commitData) = Utils.encodeCommitBatchesData( + storedBatch, + batchesToCommit + ); + validator.commitBatchesSharedBridge(chainId, commitBatchFrom, commitBatchTo, commitData); } function test_revertBatchesSharedBridge() public { @@ -168,7 +180,7 @@ contract ValidatorTimelockTest is Test { function test_proveBatchesSharedBridge() public { IExecutor.StoredBatchInfo memory prevBatch = Utils.createStoredBatchInfo(); IExecutor.StoredBatchInfo memory batchToProve = Utils.createStoredBatchInfo(); - IExecutor.ProofInput memory proof = Utils.createProofInput(); + uint256[] memory proof = new uint256[](0); IExecutor.StoredBatchInfo[] memory batchesToProve = new IExecutor.StoredBatchInfo[](1); batchesToProve[0] = batchToProve; @@ -179,7 +191,12 @@ contract ValidatorTimelockTest is Test { abi.encode(chainId, prevBatch, batchesToProve, proof) ); vm.prank(alice); - validator.proveBatchesSharedBridge(chainId, prevBatch, batchesToProve, proof); + (uint256 proveBatchFrom, uint256 proveBatchTo, bytes memory proveData) = Utils.encodeProveBatchesData( + prevBatch, + batchesToProve, + proof + ); + validator.proveBatchesSharedBridge(chainId, proveBatchFrom, proveBatchTo, proveData); } function test_executeBatchesSharedBridge() public { @@ -197,7 +214,11 @@ contract ValidatorTimelockTest is Test { vm.prank(alice); vm.warp(timestamp); - validator.commitBatchesSharedBridge(chainId, storedBatch1, batchesToCommit); + (uint256 commitBatchFrom, uint256 commitBatchTo, bytes memory commitData) = Utils.encodeCommitBatchesData( + storedBatch1, + batchesToCommit + ); + validator.commitBatchesSharedBridge(chainId, commitBatchFrom, commitBatchTo, commitData); // Execute batches IExecutor.StoredBatchInfo memory storedBatch2 = Utils.createStoredBatchInfo(); @@ -213,7 +234,11 @@ contract ValidatorTimelockTest is Test { vm.prank(alice); vm.warp(timestamp + executionDelay + 1); - validator.executeBatchesSharedBridge(chainId, storedBatches, Utils.emptyData()); + (uint256 executeBatchFrom, uint256 executeBatchTo, bytes memory executeData) = Utils.encodeExecuteBatchesData( + storedBatches, + Utils.emptyData() + ); + validator.executeBatchesSharedBridge(chainId, executeBatchFrom, executeBatchTo, executeData); } function test_RevertWhen_setExecutionDelayNotOwner() public { @@ -265,7 +290,11 @@ contract ValidatorTimelockTest is Test { vm.prank(bob); vm.expectRevert(abi.encodeWithSelector(Unauthorized.selector, bob)); - validator.commitBatchesSharedBridge(chainId, storedBatch, batchesToCommit); + (uint256 commitBatchFrom, uint256 commitBatchTo, bytes memory commitData) = Utils.encodeCommitBatchesData( + storedBatch, + batchesToCommit + ); + validator.commitBatchesSharedBridge(chainId, commitBatchFrom, commitBatchTo, commitData); } function test_RevertWhen_setChainTypeManagerNotOwner() public { @@ -286,14 +315,19 @@ contract ValidatorTimelockTest is Test { function test_RevertWhen_proveBatchesSharedBridgeNotValidator() public { IExecutor.StoredBatchInfo memory prevBatch = Utils.createStoredBatchInfo(); IExecutor.StoredBatchInfo memory batchToProve = Utils.createStoredBatchInfo(); - IExecutor.ProofInput memory proof = Utils.createProofInput(); + uint256[] memory proof = new uint256[](0); IExecutor.StoredBatchInfo[] memory batchesToProve = new IExecutor.StoredBatchInfo[](1); batchesToProve[0] = batchToProve; vm.prank(bob); vm.expectRevert(abi.encodeWithSelector(Unauthorized.selector, bob)); - validator.proveBatchesSharedBridge(chainId, prevBatch, batchesToProve, proof); + (uint256 proveBatchFrom, uint256 proveBatchTo, bytes memory proveData) = Utils.encodeProveBatchesData( + prevBatch, + batchesToProve, + proof + ); + validator.proveBatchesSharedBridge(chainId, proveBatchFrom, proveBatchTo, proveData); } function test_RevertWhen_executeBatchesSharedBridgeNotValidator() public { @@ -304,7 +338,11 @@ contract ValidatorTimelockTest is Test { vm.prank(bob); vm.expectRevert(abi.encodeWithSelector(Unauthorized.selector, bob)); - validator.executeBatchesSharedBridge(chainId, storedBatches, Utils.emptyData()); + (uint256 executeBatchFrom, uint256 executeBatchTo, bytes memory executeData) = Utils.encodeExecuteBatchesData( + storedBatches, + Utils.emptyData() + ); + validator.executeBatchesSharedBridge(chainId, executeBatchFrom, executeBatchTo, executeData); } function test_RevertWhen_executeBatchesSharedBridgeTooEarly() public { @@ -322,7 +360,11 @@ contract ValidatorTimelockTest is Test { vm.prank(alice); vm.warp(timestamp); - validator.commitBatchesSharedBridge(chainId, storedBatch1, batchesToCommit); + (uint256 commitBatchFrom, uint256 commitBatchTo, bytes memory commitData) = Utils.encodeCommitBatchesData( + storedBatch1, + batchesToCommit + ); + validator.commitBatchesSharedBridge(chainId, commitBatchFrom, commitBatchTo, commitData); // Execute batches IExecutor.StoredBatchInfo memory storedBatch2 = Utils.createStoredBatchInfo(); @@ -335,6 +377,10 @@ contract ValidatorTimelockTest is Test { vm.expectRevert( abi.encodeWithSelector(TimeNotReached.selector, timestamp + executionDelay, timestamp + executionDelay - 1) ); - validator.executeBatchesSharedBridge(chainId, storedBatches, Utils.emptyData()); + (uint256 executeBatchFrom, uint256 executeBatchTo, bytes memory executeData) = Utils.encodeExecuteBatchesData( + storedBatches, + Utils.emptyData() + ); + validator.executeBatchesSharedBridge(chainId, executeBatchFrom, executeBatchTo, executeData); } } diff --git a/l1-contracts/test/foundry/unit/concrete/Verifier/Verifier.t.sol b/l1-contracts/test/foundry/unit/concrete/Verifier/Verifier.t.sol index 54ab499749..bd67cfa2b7 100644 --- a/l1-contracts/test/foundry/unit/concrete/Verifier/Verifier.t.sol +++ b/l1-contracts/test/foundry/unit/concrete/Verifier/Verifier.t.sol @@ -12,7 +12,6 @@ contract VerifierTestTest is Test { uint256[] public publicInputs; uint256[] public serializedProof; - uint256[] public recursiveAggregationInput; Verifier public verifier; @@ -68,7 +67,7 @@ contract VerifierTestTest is Test { } function testShouldVerify() public view { - bool success = verifier.verify(publicInputs, serializedProof, recursiveAggregationInput); + bool success = verifier.verify(publicInputs, serializedProof); assert(success); } @@ -76,7 +75,7 @@ contract VerifierTestTest is Test { uint256[] memory newPublicInputs = publicInputs; newPublicInputs[0] += uint256(bytes32(0xe000000000000000000000000000000000000000000000000000000000000000)); - bool success = verifier.verify(newPublicInputs, serializedProof, recursiveAggregationInput); + bool success = verifier.verify(newPublicInputs, serializedProof); assert(success); } @@ -86,7 +85,7 @@ contract VerifierTestTest is Test { newSerializedProof[1] += Q_MOD; newSerializedProof[1] += Q_MOD; - bool success = verifier.verify(publicInputs, newSerializedProof, recursiveAggregationInput); + bool success = verifier.verify(publicInputs, newSerializedProof); assert(success); } @@ -94,7 +93,7 @@ contract VerifierTestTest is Test { uint256[] memory newSerializedProof = serializedProof; newSerializedProof[22] += R_MOD; - bool success = verifier.verify(publicInputs, newSerializedProof, recursiveAggregationInput); + bool success = verifier.verify(publicInputs, newSerializedProof); assert(success); } @@ -104,14 +103,14 @@ contract VerifierTestTest is Test { newPublicInputs[1] = publicInputs[0]; vm.expectRevert(bytes("loadProof: Proof is invalid")); - verifier.verify(newPublicInputs, serializedProof, recursiveAggregationInput); + verifier.verify(newPublicInputs, serializedProof); } function testEmptyPublicInput_shouldRevert() public { uint256[] memory newPublicInputs; vm.expectRevert(bytes("loadProof: Proof is invalid")); - verifier.verify(newPublicInputs, serializedProof, recursiveAggregationInput); + verifier.verify(newPublicInputs, serializedProof); } function testMoreThan44WordsProof_shouldRevert() public { @@ -123,21 +122,25 @@ contract VerifierTestTest is Test { newSerializedProof[newSerializedProof.length - 1] = serializedProof[serializedProof.length - 1]; vm.expectRevert(bytes("loadProof: Proof is invalid")); - verifier.verify(publicInputs, newSerializedProof, recursiveAggregationInput); + verifier.verify(publicInputs, newSerializedProof); } function testEmptyProof_shouldRevert() public { uint256[] memory newSerializedProof; vm.expectRevert(bytes("loadProof: Proof is invalid")); - verifier.verify(publicInputs, newSerializedProof, recursiveAggregationInput); + verifier.verify(publicInputs, newSerializedProof); } - function testNotEmptyRecursiveAggregationInput_shouldRevert() public { - uint256[] memory newRecursiveAggregationInput = publicInputs; + function testLongerProofInput_shouldRevert() public { + uint256[] memory newSerializedProof = new uint256[](serializedProof.length + 1); + for (uint256 i = 0; i < serializedProof.length; i++) { + newSerializedProof[i] = serializedProof[i]; + } + newSerializedProof[newSerializedProof.length - 1] = publicInputs[0]; vm.expectRevert(bytes("loadProof: Proof is invalid")); - verifier.verify(publicInputs, serializedProof, newRecursiveAggregationInput); + verifier.verify(publicInputs, newSerializedProof); } function testEllipticCurvePointAtInfinity_shouldRevert() public { @@ -146,7 +149,7 @@ contract VerifierTestTest is Test { newSerializedProof[1] = 0; vm.expectRevert(bytes("loadProof: Proof is invalid")); - verifier.verify(publicInputs, newSerializedProof, recursiveAggregationInput); + verifier.verify(publicInputs, newSerializedProof); } function testInvalidPublicInput_shouldRevert() public { @@ -154,7 +157,7 @@ contract VerifierTestTest is Test { newPublicInputs[0] = 0; vm.expectRevert(bytes("invalid quotient evaluation")); - verifier.verify(newPublicInputs, serializedProof, recursiveAggregationInput); + verifier.verify(newPublicInputs, serializedProof); } function testVerificationKeyHash() public virtual { diff --git a/l1-contracts/test/foundry/unit/concrete/Verifier/VerifierRecursive.t.sol b/l1-contracts/test/foundry/unit/concrete/Verifier/VerifierRecursive.t.sol index 69bad2303c..c23759f355 100644 --- a/l1-contracts/test/foundry/unit/concrete/Verifier/VerifierRecursive.t.sol +++ b/l1-contracts/test/foundry/unit/concrete/Verifier/VerifierRecursive.t.sol @@ -8,44 +8,45 @@ contract VerifierRecursiveTestTest is VerifierTestTest { function setUp() public override { super.setUp(); - recursiveAggregationInput.push(2257920826825449939414463854743099397427742128922725774525544832270890253504); - recursiveAggregationInput.push(9091218701914748532331969127001446391756173432977615061129552313204917562530); - recursiveAggregationInput.push(16188304989094043810949359833767911976672882599560690320245309499206765021563); - recursiveAggregationInput.push(3201093556796962656759050531176732990872300033146738631772984017549903765305); + serializedProof.push(2257920826825449939414463854743099397427742128922725774525544832270890253504); + serializedProof.push(9091218701914748532331969127001446391756173432977615061129552313204917562530); + serializedProof.push(16188304989094043810949359833767911976672882599560690320245309499206765021563); + serializedProof.push(3201093556796962656759050531176732990872300033146738631772984017549903765305); verifier = new VerifierRecursiveTest(); } function testMoreThan4WordsRecursiveInput_shouldRevert() public { - uint256[] memory newRecursiveAggregationInput = new uint256[](recursiveAggregationInput.length + 1); + uint256[] memory newSerializedProof = new uint256[](serializedProof.length + 1); - for (uint256 i = 0; i < recursiveAggregationInput.length; i++) { - newRecursiveAggregationInput[i] = recursiveAggregationInput[i]; + for (uint256 i = 0; i < serializedProof.length; i++) { + newSerializedProof[i] = serializedProof[i]; } - newRecursiveAggregationInput[newRecursiveAggregationInput.length - 1] = recursiveAggregationInput[ - recursiveAggregationInput.length - 1 - ]; + newSerializedProof[newSerializedProof.length - 1] = serializedProof[serializedProof.length - 1]; vm.expectRevert(bytes("loadProof: Proof is invalid")); - verifier.verify(publicInputs, serializedProof, newRecursiveAggregationInput); + verifier.verify(publicInputs, newSerializedProof); } function testEmptyRecursiveInput_shouldRevert() public { - uint256[] memory newRecursiveAggregationInput; + uint256[] memory newSerializedProof = new uint256[](serializedProof.length - 4); + for (uint256 i = 0; i < newSerializedProof.length; i++) { + newSerializedProof[i] = serializedProof[i]; + } vm.expectRevert(bytes("loadProof: Proof is invalid")); - verifier.verify(publicInputs, serializedProof, newRecursiveAggregationInput); + verifier.verify(publicInputs, newSerializedProof); } function testInvalidRecursiveInput_shouldRevert() public { - uint256[] memory newRecursiveAggregationInput = new uint256[](4); - newRecursiveAggregationInput[0] = 1; - newRecursiveAggregationInput[1] = 2; - newRecursiveAggregationInput[2] = 1; - newRecursiveAggregationInput[3] = 2; + uint256[] memory newSerializedProof = serializedProof; + newSerializedProof[newSerializedProof.length - 4] = 1; + newSerializedProof[newSerializedProof.length - 3] = 2; + newSerializedProof[newSerializedProof.length - 2] = 1; + newSerializedProof[newSerializedProof.length - 1] = 2; vm.expectRevert(bytes("finalPairing: pairing failure")); - verifier.verify(publicInputs, serializedProof, newRecursiveAggregationInput); + verifier.verify(publicInputs, newSerializedProof); } function testVerificationKeyHash() public override { diff --git a/l1-contracts/test/foundry/unit/concrete/state-transition/ChainTypeManager/RevertBatches.t.sol b/l1-contracts/test/foundry/unit/concrete/state-transition/ChainTypeManager/RevertBatches.t.sol index 71eab393fd..cdac3e7768 100644 --- a/l1-contracts/test/foundry/unit/concrete/state-transition/ChainTypeManager/RevertBatches.t.sol +++ b/l1-contracts/test/foundry/unit/concrete/state-transition/ChainTypeManager/RevertBatches.t.sol @@ -19,7 +19,7 @@ contract revertBatchesTest is ChainTypeManagerTest { IExecutor.CommitBatchInfo internal newCommitBatchInfo; IExecutor.StoredBatchInfo internal newStoredBatchInfo; IExecutor.StoredBatchInfo internal genesisStoredBatchInfo; - IExecutor.ProofInput internal proofInput; + uint256[] internal proofInput; // Facets exposing the diamond AdminFacet internal adminFacet; diff --git a/l1-contracts/test/unit_tests/l2-upgrade.test.spec.ts b/l1-contracts/test/unit_tests/l2-upgrade.test.spec.ts index c85afab1f4..4d04f66955 100644 --- a/l1-contracts/test/unit_tests/l2-upgrade.test.spec.ts +++ b/l1-contracts/test/unit_tests/l2-upgrade.test.spec.ts @@ -27,6 +27,7 @@ import { diamondCut, Action, facetCut } from "../../src.ts/diamondCut"; import type { CommitBatchInfo, StoredBatchInfo, CommitBatchInfoWithTimestamp } from "./utils"; import { + encodeCommitBatchesData, L2_BOOTLOADER_ADDRESS, L2_SYSTEM_CONTEXT_ADDRESS, SYSTEM_LOG_KEYS, @@ -135,9 +136,11 @@ describe("L2 upgrade test", function () { ); const commitReceipt = await ( - await proxyExecutor.commitBatchesSharedBridge(chainId, genesisStoredBatchInfo(), [batch1InfoChainIdUpgrade]) + await proxyExecutor.commitBatchesSharedBridge( + chainId, + ...encodeCommitBatchesData(genesisStoredBatchInfo(), [batch1InfoChainIdUpgrade]) + ) ).wait(); - const commitment = commitReceipt.events[0].args.commitment; storedBatch1InfoChainIdUpgrade = getBatchStoredInfo(batch1InfoChainIdUpgrade, commitment); await makeExecutedEqualCommitted(proxyExecutor, genesisStoredBatchInfo(), [storedBatch1InfoChainIdUpgrade], []); @@ -151,7 +154,10 @@ describe("L2 upgrade test", function () { }); const commitReceipt = await ( - await proxyExecutor.commitBatchesSharedBridge(chainId, storedBatch1InfoChainIdUpgrade, [batch2Info]) + await proxyExecutor.commitBatchesSharedBridge( + chainId, + ...encodeCommitBatchesData(storedBatch1InfoChainIdUpgrade, [batch2Info]) + ) ).wait(); const commitment = commitReceipt.events[0].args.commitment; @@ -628,7 +634,7 @@ describe("L2 upgrade test", function () { // batchNumber: 3, // }); // const revertReason = await getCallRevertReason( - // proxyExecutor.commitBatchesSharedBridge(storedBatch2Info, [batch3InfoNoUpgradeTx]) + // proxyExecutor.commitBatchesSharedBridge(chainId, ...encodeCommitBatchesData(storedBatch2Info, [batch3InfoNoUpgradeTx])) // ); // expect(revertReason).to.contains("MissingSystemLogs"); // }); @@ -670,8 +676,12 @@ describe("L2 upgrade test", function () { }, systemLogs ); + const revertReason = await getCallRevertReason( - proxyExecutor.commitBatchesSharedBridge(chainId, storedBatch2Info, [batch3InfoNoUpgradeTx]) + proxyExecutor.commitBatchesSharedBridge( + chainId, + ...encodeCommitBatchesData(storedBatch2Info, [batch3InfoNoUpgradeTx]) + ) ); expect(revertReason).to.contains("LogAlreadyProcessed"); }); @@ -704,7 +714,10 @@ describe("L2 upgrade test", function () { ); const revertReason = await getCallRevertReason( - proxyExecutor.commitBatchesSharedBridge(chainId, storedBatch2Info, [batch3InfoTwoUpgradeTx]) + proxyExecutor.commitBatchesSharedBridge( + chainId, + ...encodeCommitBatchesData(storedBatch2Info, [batch3InfoTwoUpgradeTx]) + ) ); expect(revertReason).to.contains("TxHashMismatch"); }); @@ -736,7 +749,12 @@ describe("L2 upgrade test", function () { systemLogs ); - await (await proxyExecutor.commitBatchesSharedBridge(chainId, storedBatch2Info, [batch3InfoTwoUpgradeTx])).wait(); + await ( + await proxyExecutor.commitBatchesSharedBridge( + chainId, + ...encodeCommitBatchesData(storedBatch2Info, [batch3InfoTwoUpgradeTx]) + ) + ).wait(); expect(await proxyGetters.getL2SystemContractsUpgradeBatchNumber()).to.equal(3); }); @@ -770,7 +788,10 @@ describe("L2 upgrade test", function () { ); const commitReceipt = await ( - await proxyExecutor.commitBatchesSharedBridge(chainId, storedBatch2Info, [batch3InfoTwoUpgradeTx]) + await proxyExecutor.commitBatchesSharedBridge( + chainId, + ...encodeCommitBatchesData(storedBatch2Info, [batch3InfoTwoUpgradeTx]) + ) ).wait(); expect(await proxyGetters.getL2SystemContractsUpgradeBatchNumber()).to.equal(3); @@ -809,7 +830,10 @@ describe("L2 upgrade test", function () { ); const commitReceipt = await ( - await proxyExecutor.commitBatchesSharedBridge(chainId, storedBatch2Info, [batch4InfoTwoUpgradeTx]) + await proxyExecutor.commitBatchesSharedBridge( + chainId, + ...encodeCommitBatchesData(storedBatch2Info, [batch4InfoTwoUpgradeTx]) + ) ).wait(); const commitment = commitReceipt.events[0].args.commitment; const newBatchStoredInfo = getBatchStoredInfo(batch4InfoTwoUpgradeTx, commitment); @@ -865,7 +889,10 @@ describe("L2 upgrade test", function () { ); const commitReceipt = await ( - await proxyExecutor.commitBatchesSharedBridge(chainId, storedBatch2Info, [batch5InfoTwoUpgradeTx]) + await proxyExecutor.commitBatchesSharedBridge( + chainId, + ...encodeCommitBatchesData(storedBatch2Info, [batch5InfoTwoUpgradeTx]) + ) ).wait(); const commitment = commitReceipt.events[0].args.commitment; const newBatchStoredInfo = getBatchStoredInfo(batch5InfoTwoUpgradeTx, commitment); diff --git a/l1-contracts/test/unit_tests/utils.ts b/l1-contracts/test/unit_tests/utils.ts index 3d9fd89b7a..af74fa28f0 100644 --- a/l1-contracts/test/unit_tests/utils.ts +++ b/l1-contracts/test/unit_tests/utils.ts @@ -10,9 +10,16 @@ import type { IMailbox } from "../../typechain/IMailbox"; import type { ExecutorFacet } from "../../typechain"; import type { FeeParams, L2CanonicalTransaction } from "../../src.ts/utils"; -import { ADDRESS_ONE, PubdataPricingMode, EMPTY_STRING_KECCAK } from "../../src.ts/utils"; +import { + ADDRESS_ONE, + PubdataPricingMode, + EMPTY_STRING_KECCAK, + STORED_BATCH_INFO_ABI_STRING, + COMMIT_BATCH_INFO_ABI_STRING, + PRIORITY_OPS_BATCH_INFO_ABI_STRING, +} from "../../src.ts/utils"; import { packSemver } from "../../scripts/utils"; -import { keccak256 } from "ethers/lib/utils"; +import { keccak256, hexConcat, defaultAbiCoder } from "ethers/lib/utils"; export const CONTRACTS_GENESIS_PROTOCOL_VERSION = packSemver(0, 21, 0).toString(); // eslint-disable-next-line @typescript-eslint/no-var-requires @@ -362,6 +369,12 @@ export interface CommitBatchInfo { operatorDAInput: BytesLike; } +export interface PriorityOpsBatchInfo { + leftPath: Array; + rightPath: Array; + itemHashes: Array; +} + export async function depositERC20( bridge: IL1ERC20Bridge, bridgehubContract: IBridgehub, @@ -503,14 +516,13 @@ export async function makeExecutedEqualCommitted( batchesToExecute = [...batchesToProve, ...batchesToExecute]; await ( - await proxyExecutor.proveBatchesSharedBridge(0, prevBatchInfo, batchesToProve, { - recursiveAggregationInput: [], - serializedProof: [], - }) + await proxyExecutor.proveBatchesSharedBridge(0, ...encodeProveBatchesData(prevBatchInfo, batchesToProve, [])) ).wait(); const dummyMerkleProofs = batchesToExecute.map(() => ({ leftPath: [], rightPath: [], itemHashes: [] })); - await (await proxyExecutor.executeBatchesSharedBridge(0, batchesToExecute, dummyMerkleProofs)).wait(); + await ( + await proxyExecutor.executeBatchesSharedBridge(0, ...encodeExecuteBatchesData(batchesToExecute, dummyMerkleProofs)) + ).wait(); } export function getBatchStoredInfo(commitInfo: CommitBatchInfo, commitment: string): StoredBatchInfo { @@ -525,3 +537,40 @@ export function getBatchStoredInfo(commitInfo: CommitBatchInfo, commitment: stri commitment: commitment, }; } + +export function encodeCommitBatchesData( + storedBatchInfo: StoredBatchInfo, + commitBatchInfos: Array +): [BigNumberish, BigNumberish, string] { + const encodedCommitDataWithoutVersion = defaultAbiCoder.encode( + [STORED_BATCH_INFO_ABI_STRING, `${COMMIT_BATCH_INFO_ABI_STRING}[]`], + [storedBatchInfo, commitBatchInfos] + ); + const commitData = hexConcat(["0x00", encodedCommitDataWithoutVersion]); + return [commitBatchInfos[0].batchNumber, commitBatchInfos[commitBatchInfos.length - 1].batchNumber, commitData]; +} + +export function encodeProveBatchesData( + prevBatch: StoredBatchInfo, + committedBatches: Array, + proof: Array +): [BigNumberish, BigNumberish, string] { + const encodedProveDataWithoutVersion = defaultAbiCoder.encode( + [STORED_BATCH_INFO_ABI_STRING, `${STORED_BATCH_INFO_ABI_STRING}[]`, "uint256[]"], + [prevBatch, committedBatches, proof] + ); + const proveData = hexConcat(["0x00", encodedProveDataWithoutVersion]); + return [committedBatches[0].batchNumber, committedBatches[committedBatches.length - 1].batchNumber, proveData]; +} + +export function encodeExecuteBatchesData( + batchesData: Array, + priorityOpsBatchInfo: Array +): [BigNumberish, BigNumberish, string] { + const encodedExecuteDataWithoutVersion = defaultAbiCoder.encode( + [`${STORED_BATCH_INFO_ABI_STRING}[]`, `${PRIORITY_OPS_BATCH_INFO_ABI_STRING}[]`], + [batchesData, priorityOpsBatchInfo] + ); + const executeData = hexConcat(["0x00", encodedExecuteDataWithoutVersion]); + return [batchesData[0].batchNumber, batchesData[batchesData.length - 1].batchNumber, executeData]; +} diff --git a/l1-contracts/test/unit_tests/validator_timelock_test.spec.ts b/l1-contracts/test/unit_tests/validator_timelock_test.spec.ts deleted file mode 100644 index 5093c25730..0000000000 --- a/l1-contracts/test/unit_tests/validator_timelock_test.spec.ts +++ /dev/null @@ -1,290 +0,0 @@ -import { expect } from "chai"; -import { ethers } from "ethers"; -import * as hardhat from "hardhat"; -import type { DummyExecutor, ValidatorTimelock, DummyChainTypeManager } from "../../typechain"; -import { DummyExecutorFactory, ValidatorTimelockFactory, DummyChainTypeManagerFactory } from "../../typechain"; -import { getCallRevertReason } from "./utils"; - -describe("ValidatorTimelock tests", function () { - let owner: ethers.Signer; - let validator: ethers.Signer; - let randomSigner: ethers.Signer; - let validatorTimelock: ValidatorTimelock; - let dummyExecutor: DummyExecutor; - let dummyChainTypeManager: DummyChainTypeManager; - const chainId: number = 270; - - const MOCK_PROOF_INPUT = { - recursiveAggregationInput: [], - serializedProof: [], - }; - - function getMockCommitBatchInfo(batchNumber: number, timestamp: number = 0) { - return { - batchNumber, - timestamp, - indexRepeatedStorageChanges: 0, - newStateRoot: ethers.constants.HashZero, - numberOfLayer1Txs: 0, - priorityOperationsHash: ethers.constants.HashZero, - bootloaderHeapInitialContentsHash: ethers.utils.randomBytes(32), - eventsQueueStateHash: ethers.utils.randomBytes(32), - systemLogs: [], - operatorDAInput: - "0x00290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e56300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", - }; - } - - function getMockStoredBatchInfo(batchNumber: number, timestamp: number = 0) { - return { - batchNumber, - batchHash: ethers.constants.HashZero, - indexRepeatedStorageChanges: 0, - numberOfLayer1Txs: 0, - priorityOperationsHash: ethers.constants.HashZero, - l2LogsTreeRoot: ethers.constants.HashZero, - timestamp, - commitment: ethers.constants.HashZero, - }; - } - - before(async () => { - [owner, validator, randomSigner] = await hardhat.ethers.getSigners(); - - const dummyExecutorFactory = await hardhat.ethers.getContractFactory("DummyExecutor"); - const dummyExecutorContract = await dummyExecutorFactory.deploy(); - dummyExecutor = DummyExecutorFactory.connect(dummyExecutorContract.address, dummyExecutorContract.signer); - - const dummyChainTypeManagerFactory = await hardhat.ethers.getContractFactory( - "DummyChainTypeManagerForValidatorTimelock" - ); - const dummyChainTypeManagerContract = await dummyChainTypeManagerFactory.deploy( - await owner.getAddress(), - dummyExecutor.address - ); - dummyChainTypeManager = DummyChainTypeManagerFactory.connect( - dummyChainTypeManagerContract.address, - dummyChainTypeManagerContract.signer - ); - - const validatorTimelockFactory = await hardhat.ethers.getContractFactory("ValidatorTimelock"); - const validatorTimelockContract = await validatorTimelockFactory.deploy(await owner.getAddress(), 0, chainId); - validatorTimelock = ValidatorTimelockFactory.connect( - validatorTimelockContract.address, - validatorTimelockContract.signer - ); - const setCTMtx = await validatorTimelock.setChainTypeManager(dummyChainTypeManager.address); - await setCTMtx.wait(); - }); - - it("Should check deployment", async () => { - expect(await validatorTimelock.owner()).equal(await owner.getAddress()); - expect(await validatorTimelock.executionDelay()).equal(0); - expect(await validatorTimelock.validators(chainId, ethers.constants.AddressZero)).equal(false); - expect(await validatorTimelock.chainTypeManager()).equal(dummyChainTypeManager.address); - expect(await dummyChainTypeManager.getZKChain(chainId)).equal(dummyExecutor.address); - expect(await dummyChainTypeManager.getChainAdmin(chainId)).equal(await owner.getAddress()); - expect(await dummyExecutor.getAdmin()).equal(await owner.getAddress()); - }); - - it("Should revert if non-validator commits batches", async () => { - const revertReason = await getCallRevertReason( - validatorTimelock - .connect(randomSigner) - .commitBatchesSharedBridge(chainId, getMockStoredBatchInfo(0), [getMockCommitBatchInfo(1)]) - ); - - expect(revertReason).contains("Unauthorized"); - }); - - it("Should revert if non-validator proves batches", async () => { - const revertReason = await getCallRevertReason( - validatorTimelock - .connect(randomSigner) - .proveBatchesSharedBridge(chainId, getMockStoredBatchInfo(0), [getMockStoredBatchInfo(1)], MOCK_PROOF_INPUT) - ); - - expect(revertReason).contains("Unauthorized"); - }); - - it("Should revert if non-validator revert batches", async () => { - const revertReason = await getCallRevertReason( - validatorTimelock.connect(randomSigner).revertBatchesSharedBridge(chainId, 1) - ); - - expect(revertReason).contains("Unauthorized"); - }); - - it("Should revert if non-validator executes batches", async () => { - const revertReason = await getCallRevertReason( - validatorTimelock.connect(randomSigner).executeBatchesSharedBridge(chainId, [getMockStoredBatchInfo(1)], []) - ); - - expect(revertReason).contains("Unauthorized"); - }); - - it("Should revert if not chain governor sets validator", async () => { - const revertReason = await getCallRevertReason( - validatorTimelock.connect(randomSigner).addValidator(chainId, await randomSigner.getAddress()) - ); - - expect(revertReason).contains("Unauthorized"); - }); - - it("Should revert if non-owner sets execution delay", async () => { - const revertReason = await getCallRevertReason(validatorTimelock.connect(randomSigner).setExecutionDelay(1000)); - - expect(revertReason).equal("Ownable: caller is not the owner"); - }); - - it("Should successfully set the validator", async () => { - const validatorAddress = await validator.getAddress(); - await validatorTimelock.connect(owner).addValidator(chainId, validatorAddress); - - expect(await validatorTimelock.validators(chainId, validatorAddress)).equal(true); - }); - - it("Should successfully set the execution delay", async () => { - await validatorTimelock.connect(owner).setExecutionDelay(10); // set to 10 seconds - - expect(await validatorTimelock.executionDelay()).equal(10); - }); - - it("Should successfully commit batches", async () => { - await validatorTimelock - .connect(validator) - .commitBatchesSharedBridge(chainId, getMockStoredBatchInfo(0), [getMockCommitBatchInfo(1)]); - - expect(await dummyExecutor.getTotalBatchesCommitted()).equal(1); - }); - - it("Should successfully prove batches", async () => { - await validatorTimelock - .connect(validator) - .proveBatchesSharedBridge(chainId, getMockStoredBatchInfo(0), [getMockStoredBatchInfo(1, 1)], MOCK_PROOF_INPUT); - - expect(await dummyExecutor.getTotalBatchesVerified()).equal(1); - }); - - it("Should revert on executing earlier than the delay", async () => { - const revertReason = await getCallRevertReason( - validatorTimelock.connect(validator).executeBatchesSharedBridge(chainId, [getMockStoredBatchInfo(1)], []) - ); - - expect(revertReason).contains("TimeNotReached"); - }); - - it("Should successfully revert batches", async () => { - await validatorTimelock.connect(validator).revertBatchesSharedBridge(chainId, 0); - - expect(await dummyExecutor.getTotalBatchesVerified()).equal(0); - expect(await dummyExecutor.getTotalBatchesCommitted()).equal(0); - }); - - it("Should successfully overwrite the committing timestamp on the reverted batches timestamp", async () => { - const revertedBatchesTimestamp = Number(await validatorTimelock.getCommittedBatchTimestamp(chainId, 1)); - - await validatorTimelock - .connect(validator) - .commitBatchesSharedBridge(chainId, getMockStoredBatchInfo(0), [getMockCommitBatchInfo(1)]); - - await validatorTimelock - .connect(validator) - .proveBatchesSharedBridge(chainId, getMockStoredBatchInfo(0), [getMockStoredBatchInfo(1)], MOCK_PROOF_INPUT); - - const newBatchesTimestamp = Number(await validatorTimelock.getCommittedBatchTimestamp(chainId, 1)); - - expect(newBatchesTimestamp).greaterThanOrEqual(revertedBatchesTimestamp); - }); - - it("Should successfully execute batches after the delay", async () => { - await hardhat.network.provider.send("hardhat_mine", ["0x2", "0xc"]); //mine 2 batches with intervals of 12 seconds - await validatorTimelock.connect(validator).executeBatchesSharedBridge(chainId, [getMockStoredBatchInfo(1)], []); - expect(await dummyExecutor.getTotalBatchesExecuted()).equal(1); - }); - - it("Should revert if validator tries to commit batches with invalid last committed batchNumber", async () => { - const revertReason = await getCallRevertReason( - validatorTimelock - .connect(validator) - .commitBatchesSharedBridge(chainId, getMockStoredBatchInfo(0), [getMockCommitBatchInfo(2)]) - ); - - // Error should be forwarded from the DummyExecutor - expect(revertReason).equal("DummyExecutor: Invalid last committed batch number"); - }); - - // Test case to check if proving batches with invalid batchNumber fails - it("Should revert if validator tries to prove batches with invalid batchNumber", async () => { - const revertReason = await getCallRevertReason( - validatorTimelock - .connect(validator) - .proveBatchesSharedBridge(chainId, getMockStoredBatchInfo(0), [getMockStoredBatchInfo(2, 1)], MOCK_PROOF_INPUT) - ); - - expect(revertReason).equal("DummyExecutor: Invalid previous batch number"); - }); - - it("Should revert if validator tries to execute more batches than were proven", async () => { - await hardhat.network.provider.send("hardhat_mine", ["0x2", "0xc"]); //mine 2 batches with intervals of 12 seconds - const revertReason = await getCallRevertReason( - validatorTimelock.connect(validator).executeBatchesSharedBridge(chainId, [getMockStoredBatchInfo(2)], []) - ); - - expect(revertReason).equal("DummyExecutor 2: Can"); - }); - - // These tests primarily needed to make gas statistics be more accurate. - - it("Should commit multiple batches in one transaction", async () => { - await validatorTimelock - .connect(validator) - .commitBatchesSharedBridge(chainId, getMockStoredBatchInfo(1), [ - getMockCommitBatchInfo(2), - getMockCommitBatchInfo(3), - getMockCommitBatchInfo(4), - getMockCommitBatchInfo(5), - getMockCommitBatchInfo(6), - getMockCommitBatchInfo(7), - getMockCommitBatchInfo(8), - ]); - - expect(await dummyExecutor.getTotalBatchesCommitted()).equal(8); - }); - - it("Should prove multiple batches in one transactions", async () => { - for (let i = 1; i < 8; i++) { - await validatorTimelock - .connect(validator) - .proveBatchesSharedBridge( - chainId, - getMockStoredBatchInfo(i), - [getMockStoredBatchInfo(i + 1)], - MOCK_PROOF_INPUT - ); - - expect(await dummyExecutor.getTotalBatchesVerified()).equal(i + 1); - } - }); - - it("Should execute multiple batches in multiple transactions", async () => { - await hardhat.network.provider.send("hardhat_mine", ["0x2", "0xc"]); //mine 2 batches with intervals of 12 seconds - await validatorTimelock - .connect(validator) - .executeBatchesSharedBridge( - chainId, - [ - getMockStoredBatchInfo(2), - getMockStoredBatchInfo(3), - getMockStoredBatchInfo(4), - getMockStoredBatchInfo(5), - getMockStoredBatchInfo(6), - getMockStoredBatchInfo(7), - getMockStoredBatchInfo(8), - ], - [] - ); - - expect(await dummyExecutor.getTotalBatchesExecuted()).equal(8); - }); -}); diff --git a/l2-contracts/contracts/verifier/Verifier.sol b/l2-contracts/contracts/verifier/Verifier.sol index 54d9b00be7..dd4eaff559 100644 --- a/l2-contracts/contracts/verifier/Verifier.sol +++ b/l2-contracts/contracts/verifier/Verifier.sol @@ -343,8 +343,7 @@ contract Verifier is IVerifier { /// @inheritdoc IVerifier function verify( uint256[] calldata, // _publicInputs - uint256[] calldata, // _proof - uint256[] calldata // _recursiveAggregationInput + uint256[] calldata // _proof ) public view virtual returns (bool) { // No memory was accessed yet, so keys can be loaded into the right place and not corrupt any other memory. _loadVerificationKey(); @@ -525,7 +524,17 @@ contract Verifier is IVerifier { // 2. Load the proof (except for the recursive part) offset := calldataload(0x24) let proofLengthInWords := calldataload(add(offset, 0x04)) - isValid := and(eq(proofLengthInWords, 44), isValid) + + // Check the proof length depending on whether the recursive part is present + let expectedProofLength + switch mload(VK_RECURSIVE_FLAG_SLOT) + case 0 { + expectedProofLength := 44 + } + default { + expectedProofLength := 48 + } + isValid := and(eq(proofLengthInWords, expectedProofLength), isValid) // PROOF_STATE_POLYS_0 { @@ -672,21 +681,13 @@ contract Verifier is IVerifier { } // 3. Load the recursive part of the proof - offset := calldataload(0x44) - let recursiveProofLengthInWords := calldataload(add(offset, 0x04)) - - switch mload(VK_RECURSIVE_FLAG_SLOT) - case 0 { - // recursive part should be empty - isValid := and(iszero(recursiveProofLengthInWords), isValid) - } - default { + if mload(VK_RECURSIVE_FLAG_SLOT) { // recursive part should be consist of 2 points - isValid := and(eq(recursiveProofLengthInWords, 4), isValid) + // PROOF_RECURSIVE_PART_P1 { - let x := mod(calldataload(add(offset, 0x024)), Q_MOD) - let y := mod(calldataload(add(offset, 0x044)), Q_MOD) + let x := mod(calldataload(add(offset, 0x5a4)), Q_MOD) + let y := mod(calldataload(add(offset, 0x5c4)), Q_MOD) let xx := mulmod(x, x, Q_MOD) isValid := and(eq(mulmod(y, y, Q_MOD), addmod(mulmod(x, xx, Q_MOD), 3, Q_MOD)), isValid) mstore(PROOF_RECURSIVE_PART_P1_X_SLOT, x) @@ -694,8 +695,8 @@ contract Verifier is IVerifier { } // PROOF_RECURSIVE_PART_P2 { - let x := mod(calldataload(add(offset, 0x064)), Q_MOD) - let y := mod(calldataload(add(offset, 0x084)), Q_MOD) + let x := mod(calldataload(add(offset, 0x5e4)), Q_MOD) + let y := mod(calldataload(add(offset, 0x604)), Q_MOD) let xx := mulmod(x, x, Q_MOD) isValid := and(eq(mulmod(y, y, Q_MOD), addmod(mulmod(x, xx, Q_MOD), 3, Q_MOD)), isValid) mstore(PROOF_RECURSIVE_PART_P2_X_SLOT, x) diff --git a/l2-contracts/contracts/verifier/chain-interfaces/IVerifier.sol b/l2-contracts/contracts/verifier/chain-interfaces/IVerifier.sol index dbca3bf0c9..40a6903f41 100644 --- a/l2-contracts/contracts/verifier/chain-interfaces/IVerifier.sol +++ b/l2-contracts/contracts/verifier/chain-interfaces/IVerifier.sol @@ -16,11 +16,7 @@ interface IVerifier { /// @dev Verifies a zk-SNARK proof. /// @return A boolean value indicating whether the zk-SNARK proof is valid. /// Note: The function may revert execution instead of returning false in some cases. - function verify( - uint256[] calldata _publicInputs, - uint256[] calldata _proof, - uint256[] calldata _recursiveAggregationInput - ) external view returns (bool); + function verify(uint256[] calldata _publicInputs, uint256[] calldata _proof) external view returns (bool); /// @notice Calculates a keccak256 hash of the runtime loaded verification keys. /// @return vkHash The keccak256 hash of the loaded verification keys. diff --git a/l2-contracts/test/foundry/unit/verifier/Verifier.t.sol b/l2-contracts/test/foundry/unit/verifier/Verifier.t.sol index 5d2a429e4c..39b7ad9441 100644 --- a/l2-contracts/test/foundry/unit/verifier/Verifier.t.sol +++ b/l2-contracts/test/foundry/unit/verifier/Verifier.t.sol @@ -17,11 +17,10 @@ contract VerifierCaller { function verify( uint256[] memory publicInputs, - uint256[] memory serializedProof, - uint256[] memory recursiveAggregationInput + uint256[] memory serializedProof ) public view returns (bool result, uint256 gasUsed) { uint256 gasBefore = gasleft(); - result = verifier.verify(publicInputs, serializedProof, recursiveAggregationInput); + result = verifier.verify(publicInputs, serializedProof); gasUsed = gasBefore - gasleft(); } } @@ -32,7 +31,6 @@ contract VerifierTestTest is Test { uint256[] public publicInputs; uint256[] public serializedProof; - uint256[] public recursiveAggregationInput; Verifier public verifier; @@ -88,7 +86,7 @@ contract VerifierTestTest is Test { } function testShouldVerify() public view { - bool success = verifier.verify(publicInputs, serializedProof, recursiveAggregationInput); + bool success = verifier.verify(publicInputs, serializedProof); assert(success); } @@ -99,7 +97,7 @@ contract VerifierTestTest is Test { // - Call the verify function from the VerifierCaller contract and return the gas used VerifierCaller caller = new VerifierCaller(verifier); - (bool success, uint256 gasUsed) = caller.verify(publicInputs, serializedProof, recursiveAggregationInput); + (bool success, uint256 gasUsed) = caller.verify(publicInputs, serializedProof); assert(success); console.log("Gas used: %d", gasUsed); @@ -109,7 +107,7 @@ contract VerifierTestTest is Test { uint256[] memory newPublicInputs = publicInputs; newPublicInputs[0] += uint256(bytes32(0xe000000000000000000000000000000000000000000000000000000000000000)); - bool success = verifier.verify(newPublicInputs, serializedProof, recursiveAggregationInput); + bool success = verifier.verify(newPublicInputs, serializedProof); assert(success); } @@ -119,7 +117,7 @@ contract VerifierTestTest is Test { newSerializedProof[1] += Q_MOD; newSerializedProof[1] += Q_MOD; - bool success = verifier.verify(publicInputs, newSerializedProof, recursiveAggregationInput); + bool success = verifier.verify(publicInputs, newSerializedProof); assert(success); } @@ -127,7 +125,7 @@ contract VerifierTestTest is Test { uint256[] memory newSerializedProof = serializedProof; newSerializedProof[22] += R_MOD; - bool success = verifier.verify(publicInputs, newSerializedProof, recursiveAggregationInput); + bool success = verifier.verify(publicInputs, newSerializedProof); assert(success); } @@ -137,14 +135,14 @@ contract VerifierTestTest is Test { newPublicInputs[1] = publicInputs[0]; vm.expectRevert(bytes("loadProof: Proof is invalid")); - verifier.verify(newPublicInputs, serializedProof, recursiveAggregationInput); + verifier.verify(newPublicInputs, serializedProof); } function testEmptyPublicInput_shouldRevert() public { uint256[] memory newPublicInputs; vm.expectRevert(bytes("loadProof: Proof is invalid")); - verifier.verify(newPublicInputs, serializedProof, recursiveAggregationInput); + verifier.verify(newPublicInputs, serializedProof); } function testMoreThan44WordsProof_shouldRevert() public { @@ -156,21 +154,25 @@ contract VerifierTestTest is Test { newSerializedProof[newSerializedProof.length - 1] = serializedProof[serializedProof.length - 1]; vm.expectRevert(bytes("loadProof: Proof is invalid")); - verifier.verify(publicInputs, newSerializedProof, recursiveAggregationInput); + verifier.verify(publicInputs, newSerializedProof); } function testEmptyProof_shouldRevert() public { uint256[] memory newSerializedProof; vm.expectRevert(bytes("loadProof: Proof is invalid")); - verifier.verify(publicInputs, newSerializedProof, recursiveAggregationInput); + verifier.verify(publicInputs, newSerializedProof); } - function testNotEmptyRecursiveAggregationInput_shouldRevert() public { - uint256[] memory newRecursiveAggregationInput = publicInputs; + function testLongerProofInput_shouldRevert() public { + uint256[] memory newSerializedProof = new uint256[](serializedProof.length + 1); + for (uint256 i = 0; i < serializedProof.length; i++) { + newSerializedProof[i] = serializedProof[i]; + } + newSerializedProof[newSerializedProof.length - 1] = publicInputs[0]; vm.expectRevert(bytes("loadProof: Proof is invalid")); - verifier.verify(publicInputs, serializedProof, newRecursiveAggregationInput); + verifier.verify(publicInputs, newSerializedProof); } function testEllipticCurvePointAtInfinity_shouldRevert() public { @@ -179,7 +181,7 @@ contract VerifierTestTest is Test { newSerializedProof[1] = 0; vm.expectRevert(bytes("loadProof: Proof is invalid")); - verifier.verify(publicInputs, newSerializedProof, recursiveAggregationInput); + verifier.verify(publicInputs, newSerializedProof); } function testInvalidPublicInput_shouldRevert() public { @@ -187,7 +189,7 @@ contract VerifierTestTest is Test { newPublicInputs[0] = 0; vm.expectRevert(bytes("invalid quotient evaluation")); - verifier.verify(newPublicInputs, serializedProof, recursiveAggregationInput); + verifier.verify(newPublicInputs, serializedProof); } function testVerificationKeyHash() public virtual { diff --git a/l2-contracts/test/foundry/unit/verifier/VerifierRecursive.t.sol b/l2-contracts/test/foundry/unit/verifier/VerifierRecursive.t.sol index cf9d1ef69d..df43a07ed3 100644 --- a/l2-contracts/test/foundry/unit/verifier/VerifierRecursive.t.sol +++ b/l2-contracts/test/foundry/unit/verifier/VerifierRecursive.t.sol @@ -8,44 +8,45 @@ contract VerifierRecursiveTestTest is VerifierTestTest { function setUp() public override { super.setUp(); - recursiveAggregationInput.push(2257920826825449939414463854743099397427742128922725774525544832270890253504); - recursiveAggregationInput.push(9091218701914748532331969127001446391756173432977615061129552313204917562530); - recursiveAggregationInput.push(16188304989094043810949359833767911976672882599560690320245309499206765021563); - recursiveAggregationInput.push(3201093556796962656759050531176732990872300033146738631772984017549903765305); + serializedProof.push(2257920826825449939414463854743099397427742128922725774525544832270890253504); + serializedProof.push(9091218701914748532331969127001446391756173432977615061129552313204917562530); + serializedProof.push(16188304989094043810949359833767911976672882599560690320245309499206765021563); + serializedProof.push(3201093556796962656759050531176732990872300033146738631772984017549903765305); verifier = new VerifierRecursiveTest(); } function testMoreThan4WordsRecursiveInput_shouldRevert() public { - uint256[] memory newRecursiveAggregationInput = new uint256[](recursiveAggregationInput.length + 1); + uint256[] memory newSerializedProof = new uint256[](serializedProof.length + 1); - for (uint256 i = 0; i < recursiveAggregationInput.length; i++) { - newRecursiveAggregationInput[i] = recursiveAggregationInput[i]; + for (uint256 i = 0; i < serializedProof.length; i++) { + newSerializedProof[i] = serializedProof[i]; } - newRecursiveAggregationInput[newRecursiveAggregationInput.length - 1] = recursiveAggregationInput[ - recursiveAggregationInput.length - 1 - ]; + newSerializedProof[newSerializedProof.length - 1] = serializedProof[serializedProof.length - 1]; vm.expectRevert(bytes("loadProof: Proof is invalid")); - verifier.verify(publicInputs, serializedProof, newRecursiveAggregationInput); + verifier.verify(publicInputs, newSerializedProof); } function testEmptyRecursiveInput_shouldRevert() public { - uint256[] memory newRecursiveAggregationInput; + uint256[] memory newSerializedProof = new uint256[](serializedProof.length - 4); + for (uint256 i = 0; i < newSerializedProof.length; i++) { + newSerializedProof[i] = serializedProof[i]; + } vm.expectRevert(bytes("loadProof: Proof is invalid")); - verifier.verify(publicInputs, serializedProof, newRecursiveAggregationInput); + verifier.verify(publicInputs, newSerializedProof); } function testInvalidRecursiveInput_shouldRevert() public { - uint256[] memory newRecursiveAggregationInput = new uint256[](4); - newRecursiveAggregationInput[0] = 1; - newRecursiveAggregationInput[1] = 2; - newRecursiveAggregationInput[2] = 1; - newRecursiveAggregationInput[3] = 2; + uint256[] memory newSerializedProof = serializedProof; + newSerializedProof[newSerializedProof.length - 4] = 1; + newSerializedProof[newSerializedProof.length - 3] = 2; + newSerializedProof[newSerializedProof.length - 2] = 1; + newSerializedProof[newSerializedProof.length - 1] = 2; vm.expectRevert(bytes("finalPairing: pairing failure")); - verifier.verify(publicInputs, serializedProof, newRecursiveAggregationInput); + verifier.verify(publicInputs, newSerializedProof); } function testVerificationKeyHash() public override { diff --git a/tools/data/verifier_contract_template.txt b/tools/data/verifier_contract_template.txt index 314e92dc39..23249c9ab4 100644 --- a/tools/data/verifier_contract_template.txt +++ b/tools/data/verifier_contract_template.txt @@ -278,8 +278,7 @@ contract Verifier is IVerifier { /// @inheritdoc IVerifier function verify( uint256[] calldata, // _publicInputs - uint256[] calldata, // _proof - uint256[] calldata // _recursiveAggregationInput + uint256[] calldata // _proof ) public view virtual returns (bool) { // No memory was accessed yet, so keys can be loaded into the right place and not corrupt any other memory. _loadVerificationKey(); @@ -447,7 +446,17 @@ contract Verifier is IVerifier { // 2. Load the proof (except for the recursive part) offset := calldataload(0x24) let proofLengthInWords := calldataload(add(offset, 0x04)) - isValid := and(eq(proofLengthInWords, 44), isValid) + + // Check the proof length depending on whether the recursive part is present + let expectedProofLength + switch mload(VK_RECURSIVE_FLAG_SLOT) + case 0 { + expectedProofLength := 44 + } + default { + expectedProofLength := 48 + } + isValid := and(eq(proofLengthInWords, expectedProofLength), isValid) // PROOF_STATE_POLYS_0 { @@ -594,21 +603,13 @@ contract Verifier is IVerifier { } // 3. Load the recursive part of the proof - offset := calldataload(0x44) - let recursiveProofLengthInWords := calldataload(add(offset, 0x04)) - - switch mload(VK_RECURSIVE_FLAG_SLOT) - case 0 { - // recursive part should be empty - isValid := and(iszero(recursiveProofLengthInWords), isValid) - } - default { + if mload(VK_RECURSIVE_FLAG_SLOT) { // recursive part should be consist of 2 points - isValid := and(eq(recursiveProofLengthInWords, 4), isValid) + // PROOF_RECURSIVE_PART_P1 { - let x := mod(calldataload(add(offset, 0x024)), Q_MOD) - let y := mod(calldataload(add(offset, 0x044)), Q_MOD) + let x := mod(calldataload(add(offset, 0x5a4)), Q_MOD) + let y := mod(calldataload(add(offset, 0x5c4)), Q_MOD) let xx := mulmod(x, x, Q_MOD) isValid := and(eq(mulmod(y, y, Q_MOD), addmod(mulmod(x, xx, Q_MOD), 3, Q_MOD)), isValid) mstore(PROOF_RECURSIVE_PART_P1_X_SLOT, x) @@ -616,8 +617,8 @@ contract Verifier is IVerifier { } // PROOF_RECURSIVE_PART_P2 { - let x := mod(calldataload(add(offset, 0x064)), Q_MOD) - let y := mod(calldataload(add(offset, 0x084)), Q_MOD) + let x := mod(calldataload(add(offset, 0x5e4)), Q_MOD) + let y := mod(calldataload(add(offset, 0x604)), Q_MOD) let xx := mulmod(x, x, Q_MOD) isValid := and(eq(mulmod(y, y, Q_MOD), addmod(mulmod(x, xx, Q_MOD), 3, Q_MOD)), isValid) mstore(PROOF_RECURSIVE_PART_P2_X_SLOT, x)