// 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.0; import { PriorityTreeCommitment } from "../../common/Config.sol"; import { DynamicIncrementalMerkle } from "../../common/libraries/DynamicIncrementalMerkle.sol"; import { Merkle } from "../../common/libraries/Merkle.sol"; import { InvalidCommitment, InvalidNextLeafIndex, InvalidStartIndex, InvalidUnprocessedIndex, NotHistoricalRoot } from "../L1StateTransitionErrors.sol"; struct PriorityOpsBatchInfo { bytes32[] leftPath; bytes32[] rightPath; bytes32[] itemHashes; } bytes32 constant ZERO_LEAF_HASH = keccak256(""); library PriorityTree { using PriorityTree for Tree; using DynamicIncrementalMerkle for DynamicIncrementalMerkle.Bytes32PushTree; struct Tree { uint256 startIndex; // priority tree started accepting priority ops from this index uint256 unprocessedIndex; // relative to `startIndex` mapping(bytes32 => bool) historicalRoots; DynamicIncrementalMerkle.Bytes32PushTree tree; } /// @notice Returns zero if and only if no operations were processed from the tree /// @return Index of the oldest priority operation that wasn't processed yet function getFirstUnprocessedPriorityTx(Tree storage _tree) internal view returns (uint256) { return _tree.startIndex + _tree.unprocessedIndex; } /// @return The total number of priority operations that were added to the priority queue, including all processed ones function getTotalPriorityTxs(Tree storage _tree) internal view returns (uint256) { return _tree.startIndex + _tree.tree._nextLeafIndex; } /// @return The total number of unprocessed priority operations in a priority queue function getSize(Tree storage _tree) internal view returns (uint256) { return _tree.tree._nextLeafIndex - _tree.unprocessedIndex; } /// @notice Add the priority operation to the end of the priority queue function push(Tree storage _tree, bytes32 _hash) internal { (, bytes32 newRoot) = _tree.tree.push(_hash); _tree.historicalRoots[newRoot] = true; } /// @notice Set up the tree function setup(Tree storage _tree, uint256 _startIndex) internal { bytes32 initialRoot = _tree.tree.setup(ZERO_LEAF_HASH); _tree.historicalRoots[initialRoot] = true; _tree.startIndex = _startIndex; } /// @return Returns the tree root. function getRoot(Tree storage _tree) internal view returns (bytes32) { return _tree.tree.root(); } /// @param _root The root to check. /// @return Returns true if the root is a historical root. function isHistoricalRoot(Tree storage _tree, bytes32 _root) internal view returns (bool) { return _tree.historicalRoots[_root]; } /// @notice Process the priority operations of a batch. /// @dev Note, that the function below only checks that a certain segment of items is present in the tree. /// It does not check that e.g. there are no zero items inside the provided `itemHashes`, so in theory proofs /// that include non-existing priority operations could be created. This function relies on the fact /// that the `itemHashes` of `_priorityOpsData` are hashes of valid priority transactions. /// This fact is ensured by the fact the rolling hash of those is sent to the Executor by the bootloader /// and so assuming that zero knowledge proofs are correct, so is the structure of the `itemHashes`. function processBatch( Tree storage _tree, PriorityOpsBatchInfo memory _priorityOpsData ) internal { if (_priorityOpsData.itemHashes.length > 0) { bytes32 expectedRoot = Merkle.calculateRootPaths( _priorityOpsData.leftPath, _priorityOpsData.rightPath, _tree.unprocessedIndex, _priorityOpsData.itemHashes ); if (!_tree.historicalRoots[expectedRoot]) { revert NotHistoricalRoot(expectedRoot); } _tree.unprocessedIndex += _priorityOpsData.itemHashes.length; } } /// @notice Allows to skip a certain number of operations. /// @param _lastUnprocessed The new expected id of the unprocessed transaction. /// @dev It is used when the corresponding transactions have been processed by priority queue. function skipUntil(Tree storage _tree, uint256 _lastUnprocessed) internal { if (_tree.startIndex > _lastUnprocessed) { // Nothing to do, return return; } uint256 newUnprocessedIndex = _lastUnprocessed - _tree.startIndex; if (newUnprocessedIndex <= _tree.unprocessedIndex) { // These transactions were already processed, skip. return; } _tree.unprocessedIndex = newUnprocessedIndex; } /// @notice Initialize a chain from a commitment. function initFromCommitment( Tree storage _tree, PriorityTreeCommitment memory _commitment ) internal { uint256 height = _commitment.sides.length; // Height, including the root node. if (height == 0) { revert InvalidCommitment(); } _tree.startIndex = _commitment.startIndex; _tree.unprocessedIndex = _commitment.unprocessedIndex; _tree.tree._nextLeafIndex = _commitment.nextLeafIndex; _tree.tree._sides = _commitment.sides; bytes32 zero = ZERO_LEAF_HASH; _tree.tree._zeros = new bytes32[](height); for (uint256 i; i < height; ++i) { _tree.tree._zeros[i] = zero; zero = Merkle.efficientHash(zero, zero); } _tree.historicalRoots[_tree.tree.root()] = true; } /// @notice Reinitialize the tree from a commitment on L1. function l1Reinit( Tree storage _tree, PriorityTreeCommitment memory _commitment ) internal { if (_tree.startIndex != _commitment.startIndex) { revert InvalidStartIndex(_tree.startIndex, _commitment.startIndex); } if (_tree.unprocessedIndex > _commitment.unprocessedIndex) { revert InvalidUnprocessedIndex( _tree.unprocessedIndex, _commitment.unprocessedIndex ); } if (_tree.tree._nextLeafIndex < _commitment.nextLeafIndex) { revert InvalidNextLeafIndex( _tree.tree._nextLeafIndex, _commitment.nextLeafIndex ); } _tree.unprocessedIndex = _commitment.unprocessedIndex; } /// @notice Reinitialize the tree from a commitment on GW. function checkGWReinit( Tree storage _tree, PriorityTreeCommitment memory _commitment ) internal view { if (_tree.startIndex != _commitment.startIndex) { revert InvalidStartIndex(_tree.startIndex, _commitment.startIndex); } if (_tree.unprocessedIndex > _commitment.unprocessedIndex) { revert InvalidUnprocessedIndex( _tree.unprocessedIndex, _commitment.unprocessedIndex ); } if (_tree.tree._nextLeafIndex > _commitment.nextLeafIndex) { revert InvalidNextLeafIndex( _tree.tree._nextLeafIndex, _commitment.nextLeafIndex ); } } /// @notice Returns the commitment to the priority tree. function getCommitment(Tree storage _tree) internal view returns (PriorityTreeCommitment memory commitment) { commitment.nextLeafIndex = _tree.tree._nextLeafIndex; commitment.startIndex = _tree.startIndex; commitment.unprocessedIndex = _tree.unprocessedIndex; commitment.sides = _tree.tree._sides; } }