-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
95 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,113 @@ | ||
// SPDX-License-Identifier: CC0-1.0 | ||
|
||
pragma solidity ^0.8.15; | ||
|
||
import "forge-std/Test.sol"; | ||
import "../../src/zkbob/utils/PriorityQueue.sol"; | ||
import {PriorityQueue, PendingCommitment} from "../../src/zkbob/utils/PriorityQueue.sol"; | ||
import "forge-std/console.sol"; | ||
|
||
contract DummyQueue { | ||
using PriorityQueue for PriorityQueue.Queue; | ||
|
||
PriorityQueue.Queue _queue; | ||
address immutable prover1 = address(bytes20(new bytes(20))); | ||
contract PriorityQueueTest is Test { | ||
address immutable prover1 = makeAddr("Prover #1"); | ||
DummyQueue _queue; | ||
|
||
function list() external view returns (PendingCommitment[] memory) { | ||
return _queue.list(); | ||
function setUp() external { | ||
_queue = new DummyQueue(); | ||
} | ||
|
||
function pushBack(PendingCommitment memory _operation) external { | ||
_queue.pushBack(_operation); | ||
} | ||
function testEmptyQueue() external { | ||
assertEq(_queue.getSize(), 0); | ||
assertEq(_queue.isEmpty(), true); | ||
|
||
function head() external view returns (uint256) { | ||
return _queue.head; | ||
} | ||
vm.expectRevert("ZkBobPool: queue is empty"); | ||
_queue.popFront(); | ||
|
||
function tail() external view returns (uint256) { | ||
return _queue.tail; | ||
} | ||
vm.expectRevert("ZkBobPool: queue is empty"); | ||
_queue.front(); | ||
|
||
function popFront() external returns (PendingCommitment memory pendingCommitments) { | ||
return _queue.popFront(); | ||
PendingCommitment[] memory ops = _queue.list(); | ||
assertEq(0, ops.length); | ||
} | ||
} | ||
|
||
contract PriorityQueueTest is Test { | ||
using PriorityQueue for PriorityQueue.Queue; | ||
function testPushBackPopFront() external { | ||
for (uint256 i = 0; i < 100; i++) { | ||
_queue.pushBack(_newOp(i)); | ||
|
||
address immutable prover1 = makeAddr("Prover #1"); | ||
DummyQueue _queueContract; | ||
assertEq(i, _queue.head()); | ||
assertEq(i + 1, _queue.tail()); | ||
assertEq(1, _queue.getSize()); | ||
|
||
function setUp() external { | ||
_queueContract = new DummyQueue(); | ||
PendingCommitment memory commitment = _queue.front(); | ||
_verifyOp(i, commitment); | ||
|
||
PendingCommitment[] memory ops = _queue.list(); | ||
assertEq(1, ops.length); | ||
_verifyOp(i, ops[0]); | ||
|
||
PendingCommitment memory popped = _queue.popFront(); | ||
_verifyOp(i, popped); | ||
} | ||
} | ||
|
||
function newOp( | ||
function _newOp( | ||
uint256 id | ||
) external view returns (PendingCommitment memory) { | ||
return PendingCommitment(id, prover1, uint64(0), uint64(0)); | ||
) internal pure returns (PendingCommitment memory) { | ||
address prover = address(uint160(uint256(keccak256(abi.encodePacked("prover", id))))); | ||
uint64 fee = uint64(uint256(keccak256(abi.encodePacked("fee", id)))); | ||
uint64 timestamp = uint64(uint256(keccak256(abi.encodePacked("timestamp", id)))); | ||
return PendingCommitment(id, prover, fee, timestamp); | ||
} | ||
|
||
function testEmptyQueue() external { | ||
PendingCommitment[] memory ops = _queueContract.list(); | ||
assertEq(0, ops.length); | ||
function _verifyOp( | ||
uint256 id, | ||
PendingCommitment memory op | ||
) internal { | ||
address prover = address(uint160(uint256(keccak256(abi.encodePacked("prover", id))))); | ||
uint64 fee = uint64(uint256(keccak256(abi.encodePacked("fee", id)))); | ||
uint64 timestamp = uint64(uint256(keccak256(abi.encodePacked("timestamp", id)))); | ||
|
||
assertEq(op.commitment, id); | ||
assertEq(op.prover, prover); | ||
assertEq(op.fee, fee); | ||
assertEq(op.timestamp, timestamp); | ||
} | ||
} | ||
|
||
function testPushBack() external { | ||
_queueContract.pushBack(this.newOp(0)); | ||
|
||
assertEq(0, _queueContract.head()); | ||
assertEq(1, _queueContract.tail()); | ||
/** | ||
* @dev Helper contract to test PriorityQueue library | ||
* Without this contract forge coverage doesn't work properly | ||
*/ | ||
contract DummyQueue { | ||
PriorityQueue.Queue _queue; | ||
|
||
assertEq(1, _queueContract.list().length); | ||
function list() external view returns (PendingCommitment[] memory) { | ||
return PriorityQueue.list(_queue); | ||
} | ||
|
||
_queueContract.pushBack(this.newOp(2)); | ||
function pushBack(PendingCommitment memory _operation) external { | ||
PriorityQueue.pushBack(_queue, _operation); | ||
} | ||
|
||
assertEq(2, _queueContract.list().length); | ||
function head() external view returns (uint256) { | ||
return _queue.head; | ||
} | ||
|
||
function testPopFront() external { | ||
_queueContract.pushBack(this.newOp(0)); | ||
_queueContract.pushBack(this.newOp(1)); | ||
_queueContract.pushBack(this.newOp(2)); | ||
|
||
assertEq(0, _queueContract.head()); | ||
assertEq(3, _queueContract.tail()); | ||
function tail() external view returns (uint256) { | ||
return _queue.tail; | ||
} | ||
|
||
PendingCommitment memory first = _queueContract.popFront(); | ||
function getSize() external view returns (uint256) { | ||
return PriorityQueue.getSize(_queue); | ||
} | ||
|
||
assertEq(first.commitment, uint256(0)); | ||
function isEmpty() external view returns (bool) { | ||
return PriorityQueue.isEmpty(_queue); | ||
} | ||
|
||
assertEq(1, _queueContract.head()); | ||
assertEq(3, _queueContract.tail()); | ||
function front() external view returns (PendingCommitment memory) { | ||
return PriorityQueue.front(_queue); | ||
} | ||
|
||
assertEq(2, _queueContract.list().length); | ||
function popFront() external returns (PendingCommitment memory pendingCommitments) { | ||
return PriorityQueue.popFront(_queue); | ||
} | ||
} | ||
} |