Skip to content

Commit

Permalink
chore: priority queue fail with message when empty
Browse files Browse the repository at this point in the history
To make the failure more gracefull and easier to understand.
issue: #391
  • Loading branch information
boolafish committed Oct 30, 2019
1 parent d0a062e commit 6a17399
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ contract PriorityQueue is OnlyFromAddress {
}

/**
* @notice Inserts an element into the queue by the owner
* @notice Inserts an element into the queue by the framework
* @dev Does not perform deduplication
*/
function insert(uint256 _element) external onlyFrom(framework) {
Expand All @@ -53,10 +53,12 @@ contract PriorityQueue is OnlyFromAddress {
}

/**
* @notice Deletes the smallest element from the queue
* @notice Deletes the smallest element from the queue by the framework
* @dev Fails when queue is empty
* @return The smallest element in the priority queue
*/
function delMin() external onlyFrom(framework) returns (uint256) {
require(queue.currentSize > 0, "Queue is empty");
uint256 retVal = queue.heapList[1];
queue.heapList[1] = queue.heapList[queue.currentSize];
delete queue.heapList[queue.currentSize];
Expand All @@ -72,6 +74,7 @@ contract PriorityQueue is OnlyFromAddress {
* @return The smallest element in the priority queue
*/
function getMin() external view returns (uint256) {
require(queue.currentSize > 0, "Queue is empty");
return queue.heapList[1];
}

Expand Down
14 changes: 11 additions & 3 deletions plasma_framework/test/src/framework/utils/PriorityQueue.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ contract('PriorityQueue', ([_, nonOwner]) => {
});

it('fails when empty', async () => {
const errorMsg = 'Returned error: VM Exception while processing transaction: invalid opcode';
await this.priorityQueue.getMin()
.catch(err => expect(err.message).to.equal(errorMsg));
await expectRevert(
this.priorityQueue.getMin(),
'Queue is empty',
);
});
});

Expand Down Expand Up @@ -128,5 +129,12 @@ contract('PriorityQueue', ([_, nonOwner]) => {
'Caller address is unauthorized',
);
});

it('should fail when queue is empty', async () => {
await expectRevert(
this.priorityQueue.delMin(),
'Queue is empty',
);
});
});
});

0 comments on commit 6a17399

Please sign in to comment.