Skip to content

Commit 92e9f3b

Browse files
author
Rahul Maganti
committed
evm: use explicit error selectors in reverts
1 parent 0fdfee0 commit 92e9f3b

File tree

1 file changed

+44
-31
lines changed

1 file changed

+44
-31
lines changed

evm/test/RateLimit.t.sol

+44-31
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,11 @@ contract TestRateLimit is Test, IRateLimiterEvents {
342342
uint256 transferAmount = 3 * 10 ** decimals;
343343
token.approve(address(nttManager), transferAmount);
344344

345-
bytes4 selector = bytes4(keccak256("NotEnoughCapacity(uint256,uint256)"));
346-
vm.expectRevert(abi.encodeWithSelector(selector, outboundLimit, transferAmount));
345+
vm.expectRevert(
346+
abi.encodeWithSelector(
347+
IRateLimiter.NotEnoughCapacity.selector, outboundLimit, transferAmount
348+
)
349+
);
347350
nttManager.transfer(transferAmount, chainId, toWormholeFormat(user_B), false, new bytes(1));
348351
}
349352

@@ -377,9 +380,12 @@ contract TestRateLimit is Test, IRateLimiterEvents {
377380
uint256 badTransferAmount = 2 * 10 ** decimals;
378381
token.approve(address(nttManager), badTransferAmount);
379382

380-
bytes4 selector = bytes4(keccak256("NotEnoughCapacity(uint256,uint256)"));
381383
vm.expectRevert(
382-
abi.encodeWithSelector(selector, newCapacity.untrim(decimals), badTransferAmount)
384+
abi.encodeWithSelector(
385+
IRateLimiter.NotEnoughCapacity.selector,
386+
newCapacity.untrim(decimals),
387+
badTransferAmount
388+
)
383389
);
384390
nttManager.transfer(
385391
badTransferAmount, chainId, toWormholeFormat(user_B), false, new bytes(1)
@@ -430,9 +436,11 @@ contract TestRateLimit is Test, IRateLimiterEvents {
430436
vm.warp(durationElapsedTime - 1);
431437

432438
// assert that transfer still can't be completed
433-
bytes4 stillQueuedSelector =
434-
bytes4(keccak256("OutboundQueuedTransferStillQueued(uint64,uint256)"));
435-
vm.expectRevert(abi.encodeWithSelector(stillQueuedSelector, 0, initialBlockTimestamp));
439+
vm.expectRevert(
440+
abi.encodeWithSelector(
441+
IRateLimiter.OutboundQueuedTransferStillQueued.selector, 0, initialBlockTimestamp
442+
)
443+
);
436444
nttManager.completeOutboundQueuedTransfer(0);
437445

438446
// now complete transfer
@@ -441,8 +449,9 @@ contract TestRateLimit is Test, IRateLimiterEvents {
441449
assertEq(seq, 0);
442450

443451
// now ensure transfer was removed from queue
444-
bytes4 notFoundSelector = bytes4(keccak256("OutboundQueuedTransferNotFound(uint64)"));
445-
vm.expectRevert(abi.encodeWithSelector(notFoundSelector, 0));
452+
vm.expectRevert(
453+
abi.encodeWithSelector(IRateLimiter.OutboundQueuedTransferNotFound.selector, 0)
454+
);
446455
nttManager.completeOutboundQueuedTransfer(0);
447456
}
448457

@@ -544,10 +553,12 @@ contract TestRateLimit is Test, IRateLimiterEvents {
544553

545554
{
546555
// assert that transfer still can't be completed
547-
bytes4 stillQueuedSelector =
548-
bytes4(keccak256("InboundQueuedTransferStillQueued(bytes32,uint256)"));
549556
vm.expectRevert(
550-
abi.encodeWithSelector(stillQueuedSelector, digest, initialBlockTimestamp)
557+
abi.encodeWithSelector(
558+
IRateLimiter.InboundQueuedTransferStillQueued.selector,
559+
digest,
560+
initialBlockTimestamp
561+
)
551562
);
552563
nttManager.completeInboundQueuedTransfer(digest);
553564
}
@@ -558,8 +569,9 @@ contract TestRateLimit is Test, IRateLimiterEvents {
558569

559570
{
560571
// assert transfer no longer in queue
561-
bytes4 notQueuedSelector = bytes4(keccak256("InboundQueuedTransferNotFound(bytes32)"));
562-
vm.expectRevert(abi.encodeWithSelector(notQueuedSelector, digest));
572+
vm.expectRevert(
573+
abi.encodeWithSelector(IRateLimiter.InboundQueuedTransferNotFound.selector, digest)
574+
);
563575
nttManager.completeInboundQueuedTransfer(digest);
564576
}
565577

@@ -738,20 +750,15 @@ contract TestRateLimit is Test, IRateLimiterEvents {
738750
bytes memory encodedSignature,
739751
string memory expectedRevert
740752
) internal {
741-
(bool success, bytes memory result) = contractAddress.call(
742-
encodedSignature
743-
);
753+
(bool success, bytes memory result) = contractAddress.call(encodedSignature);
744754
require(!success, "call did not revert");
745755

746756
console.log("result: %s", result.length);
747757
// // compare revert strings
748758
bytes32 expectedRevertHash = keccak256(abi.encode(expectedRevert));
749759
(bytes memory res,) = result.slice(4, result.length - 4);
750760
bytes32 actualRevertHash = keccak256(abi.encodePacked(res));
751-
require(
752-
expectedRevertHash == actualRevertHash,
753-
"call did not revert as expected"
754-
);
761+
require(expectedRevertHash == actualRevertHash, "call did not revert as expected");
755762
}
756763

757764
// transfer tokens from user_A to user_B
@@ -948,9 +955,11 @@ contract TestRateLimit is Test, IRateLimiterEvents {
948955
vm.warp(durationElapsedTime - 1);
949956

950957
// assert that transfer still can't be completed
951-
bytes4 stillQueuedSelector =
952-
bytes4(keccak256("OutboundQueuedTransferStillQueued(uint64,uint256)"));
953-
vm.expectRevert(abi.encodeWithSelector(stillQueuedSelector, 0, initialBlockTimestamp));
958+
vm.expectRevert(
959+
abi.encodeWithSelector(
960+
IRateLimiter.OutboundQueuedTransferStillQueued.selector, 0, initialBlockTimestamp
961+
)
962+
);
954963
nttManager.completeOutboundQueuedTransfer(0);
955964

956965
// now complete transfer
@@ -959,8 +968,9 @@ contract TestRateLimit is Test, IRateLimiterEvents {
959968
assertEq(seq, 0);
960969

961970
// now ensure transfer was removed from queue
962-
bytes4 notFoundSelector = bytes4(keccak256("OutboundQueuedTransferNotFound(uint64)"));
963-
vm.expectRevert(abi.encodeWithSelector(notFoundSelector, 0));
971+
vm.expectRevert(
972+
abi.encodeWithSelector(IRateLimiter.OutboundQueuedTransferNotFound.selector, 0)
973+
);
964974
nttManager.completeOutboundQueuedTransfer(0);
965975
}
966976

@@ -1027,10 +1037,12 @@ contract TestRateLimit is Test, IRateLimiterEvents {
10271037

10281038
{
10291039
// assert that transfer still can't be completed
1030-
bytes4 stillQueuedSelector =
1031-
bytes4(keccak256("InboundQueuedTransferStillQueued(bytes32,uint256)"));
10321040
vm.expectRevert(
1033-
abi.encodeWithSelector(stillQueuedSelector, digest, initialBlockTimestamp)
1041+
abi.encodeWithSelector(
1042+
IRateLimiter.InboundQueuedTransferStillQueued.selector,
1043+
digest,
1044+
initialBlockTimestamp
1045+
)
10341046
);
10351047
nttManager.completeInboundQueuedTransfer(digest);
10361048
}
@@ -1041,8 +1053,9 @@ contract TestRateLimit is Test, IRateLimiterEvents {
10411053

10421054
{
10431055
// assert transfer no longer in queue
1044-
bytes4 notQueuedSelector = bytes4(keccak256("InboundQueuedTransferNotFound(bytes32)"));
1045-
vm.expectRevert(abi.encodeWithSelector(notQueuedSelector, digest));
1056+
vm.expectRevert(
1057+
abi.encodeWithSelector(IRateLimiter.InboundQueuedTransferNotFound.selector, digest)
1058+
);
10461059
nttManager.completeInboundQueuedTransfer(digest);
10471060
}
10481061

0 commit comments

Comments
 (0)