Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit df58b00

Browse files
author
Rahul Maganti
committedMar 12, 2024·
evm: test against inputs greater than u64MAX
1 parent 53ad6b3 commit df58b00

File tree

1 file changed

+40
-28
lines changed

1 file changed

+40
-28
lines changed
 

‎evm/test/RateLimit.t.sol

+40-28
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import "wormhole-solidity-sdk/testing/helpers/WormholeSimulator.sol";
1010
import "openzeppelin-contracts/contracts/proxy/ERC1967/ERC1967Proxy.sol";
1111
import "./libraries/TransceiverHelpers.sol";
1212
import "./libraries/NttManagerHelpers.sol";
13+
import "wormhole-solidity-sdk/libraries/BytesParsing.sol";
1314

1415
pragma solidity >=0.8.8 <0.9.0;
1516

@@ -18,6 +19,7 @@ contract TestRateLimit is Test, IRateLimiterEvents {
1819

1920
using TrimmedAmountLib for uint256;
2021
using TrimmedAmountLib for TrimmedAmount;
22+
using BytesParsing for bytes;
2123

2224
uint16 constant chainId = 7;
2325

@@ -729,54 +731,64 @@ contract TestRateLimit is Test, IRateLimiterEvents {
729731
return transceivers;
730732
}
731733

734+
function expectRevert(
735+
address contractAddress,
736+
bytes memory encodedSignature,
737+
string memory expectedRevert
738+
) internal {
739+
(bool success, bytes memory result) = contractAddress.call(
740+
encodedSignature
741+
);
742+
require(!success, "call did not revert");
743+
744+
console.log("result: %s", result.length);
745+
// // compare revert strings
746+
bytes32 expectedRevertHash = keccak256(abi.encode(expectedRevert));
747+
(bytes memory res,) = result.slice(4, result.length - 4);
748+
bytes32 actualRevertHash = keccak256(abi.encodePacked(res));
749+
require(
750+
expectedRevertHash == actualRevertHash,
751+
"call did not revert as expected"
752+
);
753+
}
754+
732755
// transfer tokens from user_A to user_B
733756
// this consumes capacity on the outbound side
734757
// send tokens from user_B to user_A
735758
// this consumes capacity on the inbound side
736759
// send tokens from user_A to user_B
737760
// this should consume capacity on the outbound side
738761
// and backfill the inbound side
739-
function testFuzz_CircularFlowBackFilling(uint64 mintAmt, uint256 transferAmt) public {
740-
mintAmt = uint64(bound(mintAmt, 2, type(uint256).max));
741-
transferAmt = uint64(bound(transferAmt, 1, mintAmt - 1));
762+
function testFuzz_CircularFlowBackFilling(uint256 mintAmt, uint256 transferAmt) public {
763+
mintAmt = bound(mintAmt, 1, type(uint256).max);
764+
// enforces transferAmt <= mintAmt
765+
transferAmt = bound(transferAmt, 0, mintAmt);
742766

743767
(address user_A, address user_B, DummyToken token, uint8 decimals) = setupToken();
744768

745-
TrimmedAmount mintAmount = packTrimmedAmount(mintAmt, 8);
769+
// allow for amounts greater than uint64 to check if [`setOutboundLimit`] reverts
770+
// on amounts greater than u64 MAX.
771+
if (mintAmt.scale(decimals, 8) > type(uint64).max) {
772+
vm.expectRevert("SafeCast: value doesn't fit in 64 bits");
773+
nttManager.setOutboundLimit(mintAmt);
774+
775+
return;
776+
}
777+
778+
nttManager.setOutboundLimit(mintAmt);
779+
TrimmedAmount mintAmount = mintAmt.trim(decimals, 8);
746780
token.mintDummy(address(user_A), mintAmount.untrim(decimals));
747781
nttManager.setOutboundLimit(mintAmount.untrim(decimals));
748782

749-
// transfer 10 tokens
750783
vm.startPrank(user_A);
751-
752-
// TrimmedAmount memory transferAmount = TrimmedAmount(transferAmt, 8);
753784
token.approve(address(nttManager), type(uint256).max);
754785

755-
// TODO: also fuzz the fromDecimals?
756-
757-
// allow for amounts greater than uint64 to check if [`transfer`] reverts
758-
// on amounts greater than u64 MAX.
759786
TrimmedAmount transferAmount = transferAmt.trim(decimals, 8);
760787

761788
// check error conditions
789+
// revert if amount to be transferred is 0
762790
if (transferAmount.getAmount() == 0) {
763-
vm.expectRevert();
764-
// transfer tokens from user_A -> user_B via the nttManager
765-
nttManager.transfer(
766-
transferAmount.untrim(decimals),
767-
chainId,
768-
toWormholeFormat(user_B),
769-
false,
770-
new bytes(1)
771-
);
772-
773-
return;
774-
}
775-
776-
if (transferAmount.getAmount() > type(uint64).max) {
777-
bytes4 selector = bytes4(keccak256("AmountTooLarge(uint256)"));
778-
vm.expectRevert(abi.encodeWithSelector(selector, transferAmt));
779-
791+
vm.expectRevert(abi.encodeWithSelector(INttManager.ZeroAmount.selector));
780792
nttManager.transfer(
781793
transferAmount.untrim(decimals),
782794
chainId,

0 commit comments

Comments
 (0)
Please sign in to comment.