Skip to content

Commit

Permalink
Fix audittens H01 (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
StanislavBreadless authored Nov 28, 2024
1 parent 4039eab commit 519729e
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 5 deletions.
9 changes: 9 additions & 0 deletions l1-contracts/contracts/dev-contracts/test/TestExecutor.sol
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
// SPDX-License-Identifier: MIT

import {ExecutorFacet} from "../../state-transition/chain-deps/facets/Executor.sol";
import {PriorityQueue, PriorityOperation} from "../../state-transition/libraries/PriorityQueue.sol";

pragma solidity 0.8.24;

contract TestExecutor is ExecutorFacet {
using PriorityQueue for PriorityQueue.Queue;

function setPriorityTreeStartIndex(uint256 _startIndex) external {
s.priorityTree.startIndex = _startIndex;
}

function appendPriorityOp(bytes32 _hash) external {
s.priorityQueue.pushBack(
PriorityOperation({canonicalTxHash: _hash, expirationTimestamp: type(uint64).max, layer2Tip: 0})
);
}

// /// @dev Since we want to test the blob functionality we want mock the calls to the blobhash opcode.
// function _getBlobVersionedHash(uint256 _index) internal view virtual override returns (bytes32 versionedHash) {
// (bool success, bytes memory data) = s.blobVersionedHashRetriever.staticcall(abi.encode(_index));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,8 @@ contract ExecutorFacet is ZKChainBase, IExecutor {
PriorityOperation memory priorityOp = s.priorityQueue.popFront();
concatHash = keccak256(abi.encode(concatHash, priorityOp.canonicalTxHash));
}

s.priorityTree.skipUntil(s.priorityQueue.getFirstUnprocessedPriorityTx());
}

function _rollingHash(bytes32[] memory _hashes) internal pure returns (bytes32) {
Expand Down
17 changes: 17 additions & 0 deletions l1-contracts/contracts/state-transition/libraries/PriorityTree.sol
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,23 @@ library PriorityTree {
}
}

/// @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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,33 @@ contract ExecutingTest is ExecutorTest {
bytes32 l2DAValidatorOutputHash;
bytes32[] blobVersionedHashes;

bytes32[] priorityOpsHashes;
bytes32 correctRollingHash;

function appendPriorityOps() internal {
for (uint256 i = 0; i < priorityOpsHashes.length; i++) {
executor.appendPriorityOp(priorityOpsHashes[i]);
}
}

function generatePriorityOps() internal {
bytes32[] memory hashes = new bytes32[](2);
hashes[0] = keccak256("hash1");
hashes[1] = keccak256("hash2");

bytes32 rollingHash = keccak256("");

for (uint256 i = 0; i < hashes.length; i++) {
rollingHash = keccak256(bytes.concat(rollingHash, hashes[i]));
}

correctRollingHash = rollingHash;
priorityOpsHashes = hashes;
}

function setUp() public {
generatePriorityOps();

bytes1 source = bytes1(0x01);
bytes memory defaultBlobCommitment = Utils.getDefaultBlobCommitment();

Expand Down Expand Up @@ -50,7 +76,7 @@ contract ExecutingTest is ExecutorTest {
vm.mockCall(POINT_EVALUATION_PRECOMPILE_ADDR, precompileInput, POINT_EVALUATION_PRECOMPILE_RESULT);

// This currently only uses the legacy priority queue, not the priority tree.
executor.setPriorityTreeStartIndex(100);
executor.setPriorityTreeStartIndex(1);
vm.warp(COMMIT_TIMESTAMP_NOT_OLDER + 1);
currentTimestamp = block.timestamp;

Expand All @@ -61,12 +87,26 @@ contract ExecutingTest is ExecutorTest {
uint256(SystemLogKey.PACKED_BATCH_AND_L2_BLOCK_TIMESTAMP_KEY),
Utils.packBatchTimestampAndBlockTimestamp(currentTimestamp, currentTimestamp)
);
correctL2Logs[uint256(uint256(SystemLogKey.CHAINED_PRIORITY_TXN_HASH_KEY))] = Utils.constructL2Log(
true,
L2_BOOTLOADER_ADDRESS,
uint256(SystemLogKey.CHAINED_PRIORITY_TXN_HASH_KEY),
correctRollingHash
);
correctL2Logs[uint256(uint256(SystemLogKey.NUMBER_OF_LAYER_1_TXS_KEY))] = Utils.constructL2Log(
true,
L2_BOOTLOADER_ADDRESS,
uint256(SystemLogKey.NUMBER_OF_LAYER_1_TXS_KEY),
bytes32(priorityOpsHashes.length)
);

bytes memory l2Logs = Utils.encodePacked(correctL2Logs);

newCommitBatchInfo.systemLogs = l2Logs;
newCommitBatchInfo.timestamp = uint64(currentTimestamp);
newCommitBatchInfo.operatorDAInput = operatorDAInput;
newCommitBatchInfo.priorityOperationsHash = correctRollingHash;
newCommitBatchInfo.numberOfLayer1Txs = priorityOpsHashes.length;

IExecutor.CommitBatchInfo[] memory commitBatchInfoArray = new IExecutor.CommitBatchInfo[](1);
commitBatchInfoArray[0] = newCommitBatchInfo;
Expand All @@ -85,8 +125,8 @@ contract ExecutingTest is ExecutorTest {
batchNumber: 1,
batchHash: entries[0].topics[2],
indexRepeatedStorageChanges: 0,
numberOfLayer1Txs: 0,
priorityOperationsHash: keccak256(""),
numberOfLayer1Txs: priorityOpsHashes.length,
priorityOperationsHash: correctRollingHash,
l2LogsTreeRoot: 0,
timestamp: currentTimestamp,
commitment: entries[0].topics[3]
Expand All @@ -105,6 +145,8 @@ contract ExecutingTest is ExecutorTest {
}

function test_RevertWhen_ExecutingBlockWithWrongBatchNumber() public {
appendPriorityOps();

IExecutor.StoredBatchInfo memory wrongNewStoredBatchInfo = newStoredBatchInfo;
wrongNewStoredBatchInfo.batchNumber = 10; // Correct is 1

Expand All @@ -121,6 +163,8 @@ contract ExecutingTest is ExecutorTest {
}

function test_RevertWhen_ExecutingBlockWithWrongData() public {
appendPriorityOps();

IExecutor.StoredBatchInfo memory wrongNewStoredBatchInfo = newStoredBatchInfo;
wrongNewStoredBatchInfo.timestamp = 0; // incorrect timestamp

Expand All @@ -143,6 +187,8 @@ contract ExecutingTest is ExecutorTest {
}

function test_RevertWhen_ExecutingRevertedBlockWithoutCommittingAndProvingAgain() public {
appendPriorityOps();

vm.prank(validator);
executor.revertBatchesSharedBridge(0, 0);

Expand Down Expand Up @@ -237,6 +283,8 @@ contract ExecutingTest is ExecutorTest {
}

function test_RevertWhen_ExecutingWithUnmatchedPriorityOperationHash() public {
appendPriorityOps();

vm.prank(validator);
executor.revertBatchesSharedBridge(0, 0);

Expand Down Expand Up @@ -336,6 +384,8 @@ contract ExecutingTest is ExecutorTest {
}

function test_RevertWhen_CommittingBlockWithWrongPreviousBatchHash() public {
appendPriorityOps();

// solhint-disable-next-line func-named-parameters
bytes memory correctL2Logs = abi.encodePacked(
bytes4(0x00000001),
Expand Down Expand Up @@ -370,6 +420,8 @@ contract ExecutingTest is ExecutorTest {
}

function test_ShouldExecuteBatchesuccessfully() public {
appendPriorityOps();

IExecutor.StoredBatchInfo[] memory storedBatchInfoArray = new IExecutor.StoredBatchInfo[](1);
storedBatchInfoArray[0] = newStoredBatchInfo;

Expand All @@ -382,5 +434,11 @@ contract ExecutingTest is ExecutorTest {

uint256 totalBlocksExecuted = getters.getTotalBlocksExecuted();
assertEq(totalBlocksExecuted, 1);

bool isPriorityQueueActive = getters.isPriorityQueueActive();
assertFalse(isPriorityQueueActive);

uint256 processed = getters.getFirstUnprocessedPriorityTx();
assertEq(processed, 2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,18 @@ contract ExecutorTest is Test {
}

function getExecutorSelectors() private view returns (bytes4[] memory) {
bytes4[] memory selectors = new bytes4[](5);
bytes4[] memory selectors = new bytes4[](6);
selectors[0] = executor.commitBatchesSharedBridge.selector;
selectors[1] = executor.proveBatchesSharedBridge.selector;
selectors[2] = executor.executeBatchesSharedBridge.selector;
selectors[3] = executor.revertBatchesSharedBridge.selector;
selectors[4] = executor.setPriorityTreeStartIndex.selector;
selectors[5] = executor.appendPriorityOp.selector;
return selectors;
}

function getGettersSelectors() public view returns (bytes4[] memory) {
bytes4[] memory selectors = new bytes4[](28);
bytes4[] memory selectors = new bytes4[](29);
selectors[0] = getters.getVerifier.selector;
selectors[1] = getters.getAdmin.selector;
selectors[2] = getters.getPendingAdmin.selector;
Expand Down Expand Up @@ -115,6 +116,7 @@ contract ExecutorTest is Test {
selectors[25] = getters.getTotalBatchesCommitted.selector;
selectors[26] = getters.getTotalBatchesVerified.selector;
selectors[27] = getters.storedBlockHash.selector;
selectors[28] = getters.isPriorityQueueActive.selector;
return selectors;
}

Expand Down

0 comments on commit 519729e

Please sign in to comment.