Skip to content

Commit

Permalink
chore(tests): Moved priority queue tests from hardhat to foundry (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
mm-zk authored Jan 3, 2024
1 parent 69d3df1 commit 9ddf15d
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 133 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
pragma solidity 0.8.20;

import {PriorityQueueSharedTest} from "./_PriorityQueue_Shared.t.sol";

contract OnEmptyQueueTest is PriorityQueueSharedTest {
function test_gets() public {
assertEq(0, priorityQueue.getSize());
assertEq(0, priorityQueue.getFirstUnprocessedPriorityTx());
assertEq(0, priorityQueue.getTotalPriorityTxs());
assertTrue(priorityQueue.isEmpty());
}

function test_failGetFront() public {
vm.expectRevert(bytes("D"));
priorityQueue.front();
}

function test_failPopFront() public {
vm.expectRevert(bytes("s"));
priorityQueue.popFront();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
pragma solidity 0.8.20;

import {PriorityQueueSharedTest} from "./_PriorityQueue_Shared.t.sol";
import {PriorityOperation} from "../../../../../cache/solpp-generated-contracts/dev-contracts/test/PriorityQueueTest.sol";

contract PopOperationsTest is PriorityQueueSharedTest {
uint public constant NUMBER_OPERATIONS = 10;

function setUp() public {
push_mock_entries(NUMBER_OPERATIONS);
}

function test_after_pop() public {
assertEq(NUMBER_OPERATIONS, priorityQueue.getSize());

PriorityOperation memory front = priorityQueue.popFront();
assertEq(keccak256(abi.encode(0)), front.canonicalTxHash);
assertEq(uint64(0), front.expirationTimestamp);
assertEq(uint192(0), front.layer2Tip);

assertEq(NUMBER_OPERATIONS - 1, priorityQueue.getSize());
assertEq(1, priorityQueue.getFirstUnprocessedPriorityTx());
assertEq(NUMBER_OPERATIONS, priorityQueue.getTotalPriorityTxs());
assertFalse(priorityQueue.isEmpty());

// Ok - one more pop
PriorityOperation memory front2 = priorityQueue.popFront();
assertEq(keccak256(abi.encode(1)), front2.canonicalTxHash);
assertEq(uint64(1), front2.expirationTimestamp);
assertEq(uint192(1), front2.layer2Tip);

assertEq(NUMBER_OPERATIONS - 2, priorityQueue.getSize());
assertEq(2, priorityQueue.getFirstUnprocessedPriorityTx());
assertEq(NUMBER_OPERATIONS, priorityQueue.getTotalPriorityTxs());
assertFalse(priorityQueue.isEmpty());
}

function test_pop_until_limit() public {
for (uint i = 0; i < NUMBER_OPERATIONS; ++i) {
PriorityOperation memory front = priorityQueue.popFront();
assertEq(keccak256(abi.encode(i)), front.canonicalTxHash);
}

assertEq(0, priorityQueue.getSize());
assertEq(NUMBER_OPERATIONS, priorityQueue.getFirstUnprocessedPriorityTx());
assertEq(NUMBER_OPERATIONS, priorityQueue.getTotalPriorityTxs());
assertTrue(priorityQueue.isEmpty());

// And now let's push something.

PriorityOperation memory dummyOp = PriorityOperation({
canonicalTxHash: keccak256(abi.encode(300)),
expirationTimestamp: uint64(300),
layer2Tip: uint192(300)
});
priorityQueue.pushBack(dummyOp);

assertEq(1, priorityQueue.getSize());
assertEq(NUMBER_OPERATIONS, priorityQueue.getFirstUnprocessedPriorityTx());
assertEq(NUMBER_OPERATIONS + 1, priorityQueue.getTotalPriorityTxs());
assertFalse(priorityQueue.isEmpty());

PriorityOperation memory front_end = priorityQueue.popFront();
assertEq(keccak256(abi.encode(300)), front_end.canonicalTxHash);
assertTrue(priorityQueue.isEmpty());

// And now let's go over the limit and fail.
vm.expectRevert(bytes.concat("s"));
priorityQueue.popFront();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
pragma solidity 0.8.20;

import {PriorityQueueSharedTest} from "./_PriorityQueue_Shared.t.sol";
import {PriorityOperation} from "../../../../../cache/solpp-generated-contracts/dev-contracts/test/PriorityQueueTest.sol";

contract PushOperationsTest is PriorityQueueSharedTest {
uint public constant NUMBER_OPERATIONS = 10;

function setUp() public {
push_mock_entries(NUMBER_OPERATIONS);
}

function test_front() public {
assertEq(NUMBER_OPERATIONS, priorityQueue.getSize());
PriorityOperation memory front = priorityQueue.front();
assertEq(keccak256(abi.encode(0)), front.canonicalTxHash);
assertEq(uint64(0), front.expirationTimestamp);
assertEq(uint192(0), front.layer2Tip);
// This is 'front' and not popFront, so the amount should not change.
assertEq(NUMBER_OPERATIONS, priorityQueue.getSize());
assertEq(0, priorityQueue.getFirstUnprocessedPriorityTx());
assertEq(NUMBER_OPERATIONS, priorityQueue.getTotalPriorityTxs());
assertFalse(priorityQueue.isEmpty());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;

import {Test} from "forge-std/Test.sol";
import {PriorityQueueTest, PriorityOperation} from "solpp/dev-contracts/test/PriorityQueueTest.sol";

contract PriorityQueueSharedTest is Test {
PriorityQueueTest internal priorityQueue;

constructor() {
priorityQueue = new PriorityQueueTest();
}

// Pushes 'count' entries into the priority queue.
function push_mock_entries(uint count) public {
for (uint i = 0; i < count; ++i) {
PriorityOperation memory dummyOp = PriorityOperation({
canonicalTxHash: keccak256(abi.encode(i)),
expirationTimestamp: uint64(i),
layer2Tip: uint192(i)
});
priorityQueue.pushBack(dummyOp);
}
}
}
133 changes: 0 additions & 133 deletions l1-contracts/test/unit_tests/priority_queue_test.spec.ts

This file was deleted.

0 comments on commit 9ddf15d

Please sign in to comment.