Skip to content

Commit

Permalink
Fix MixBytes High 6 (#217)
Browse files Browse the repository at this point in the history
* Reprod issue

* Add fix

* add zero check
  • Loading branch information
cxkoda authored and Willyham committed Oct 24, 2023
1 parent f9c0d66 commit d9c36e6
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/UnstakeRequestsManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ contract UnstakeRequestsManager is

// Find the number of requests that have not been finalized.
uint256 numCancelled = 0;
uint128 amountETHCancelled = 0;
while (numCancelled < maxCancel) {
UnstakeRequest memory request = _unstakeRequests[_unstakeRequests.length - 1];

Expand All @@ -249,6 +250,7 @@ contract UnstakeRequestsManager is
_unstakeRequests.pop();
requests[numCancelled] = request;
++numCancelled;
amountETHCancelled += request.ethRequested;

emit UnstakeRequestCancelled(
request.id,
Expand All @@ -260,15 +262,18 @@ contract UnstakeRequestsManager is
);
}

// Reset the latest cumulative ETH state and check whether there are more unfinalized requests to cancel.
// Reset the latest cumulative ETH state
if (amountETHCancelled > 0) {
latestCumulativeETHRequested -= amountETHCancelled;
}

// check whether there are more unfinalized requests to cancel.
bool hasMore;
uint256 remainingRequestsLength = _unstakeRequests.length;
if (remainingRequestsLength == 0) {
latestCumulativeETHRequested = 0;
hasMore = false;
} else {
UnstakeRequest memory latestRemainingRequest = _unstakeRequests[remainingRequestsLength - 1];
latestCumulativeETHRequested = latestRemainingRequest.cumulativeETHRequested;
hasMore = !_isFinalized(latestRemainingRequest);
}

Expand Down
51 changes: 51 additions & 0 deletions test/UnstakeRequestsManager.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -782,4 +782,55 @@ contract UnstakeRequestsEmergencyCancelUnfinalizedTest is UnstakeRequestsManager
hasMore = unstakeRequestsManager.cancelUnfinalizedRequests(5);
assertFalse(hasMore);
}

function testCancelRequestsWithClaimed() public {
uint256 totalMETHLocked = 0;
uint256 startingBlockNumber = block.number;
UnstakeRequest[] memory unstakeRequestsToCancel = new UnstakeRequest[](
5
);
for (uint256 i = 0; i < 5; i++) {
uint256 id = createRequest(generalTest);
UnstakeRequest memory request = unstakeRequestsManager.requestByID(id);
unstakeRequestsToCancel[i] = request;
totalMETHLocked += request.mETHLocked;

vm.roll(block.number + 1);
}

// Allocate enough for all requesters.
uint256 allocatedETHDeficit = unstakeRequestsManager.allocatedETHDeficit();
vm.deal(address(staking), allocatedETHDeficit);
vm.prank(address(staking));
unstakeRequestsManager.allocateETH{value: allocatedETHDeficit}();
_mintMETH(address(unstakeRequestsManager), totalMETHLocked);

// Only allow the first user to claim.
OracleRecord memory record;
record.updateEndBlock = uint64(startingBlockNumber + unstakeRequestsManager.numberOfBlocksToFinalize());
oracle.pushRecord(record);

vm.prank(address(staking));
unstakeRequestsManager.claim(0, requester);

// Should only cancel the four events to the last one
for (uint256 i = unstakeRequestsToCancel.length - 1; i >= 1; --i) {
vm.expectEmit(address(unstakeRequestsManager));
emit UnstakeRequestCancelled(
unstakeRequestsToCancel[i].id,
unstakeRequestsToCancel[i].requester,
unstakeRequestsToCancel[i].mETHLocked,
unstakeRequestsToCancel[i].ethRequested,
unstakeRequestsToCancel[i].cumulativeETHRequested,
unstakeRequestsToCancel[i].blockNumber
);
}

// Cancel all unfinalized requests and check if calculations are correct.
vm.prank(requestCanceller);
bool hasMore = unstakeRequestsManager.cancelUnfinalizedRequests(5);
assertFalse(hasMore);

assertEq(unstakeRequestsManager.latestCumulativeETHRequested(), 1 * generalTest.ethRequested);
}
}

0 comments on commit d9c36e6

Please sign in to comment.