Skip to content

Commit 0fdfee0

Browse files
author
Rahul Maganti
committed
evm: test against inputs greater than u64MAX
1 parent 9b43312 commit 0fdfee0

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
@@ -11,6 +11,7 @@ import "wormhole-solidity-sdk/testing/helpers/WormholeSimulator.sol";
1111
import "openzeppelin-contracts/contracts/proxy/ERC1967/ERC1967Proxy.sol";
1212
import "./libraries/TransceiverHelpers.sol";
1313
import "./libraries/NttManagerHelpers.sol";
14+
import "wormhole-solidity-sdk/libraries/BytesParsing.sol";
1415

1516
pragma solidity >=0.8.8 <0.9.0;
1617

@@ -19,6 +20,7 @@ contract TestRateLimit is Test, IRateLimiterEvents {
1920

2021
using TrimmedAmountLib for uint256;
2122
using TrimmedAmountLib for TrimmedAmount;
23+
using BytesParsing for bytes;
2224

2325
uint16 constant chainId = 7;
2426

@@ -731,54 +733,64 @@ contract TestRateLimit is Test, IRateLimiterEvents {
731733
return transceivers;
732734
}
733735

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

745769
(address user_A, address user_B, DummyToken token, uint8 decimals) = setupToken();
746770

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

751-
// transfer 10 tokens
752785
vm.startPrank(user_A);
753-
754-
// TrimmedAmount memory transferAmount = TrimmedAmount(transferAmt, 8);
755786
token.approve(address(nttManager), type(uint256).max);
756787

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

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

0 commit comments

Comments
 (0)