|
| 1 | +// SPDX-License-Identifier: MIT AND Apache-2.0 |
| 2 | +pragma solidity 0.8.23; |
| 3 | + |
| 4 | +import "forge-std/Test.sol"; |
| 5 | +import { CallIntervalEnforcer } from "../../src/enforcers/CallIntervalEnforcer.sol"; |
| 6 | +import { ModeCode } from "../../src/utils/Types.sol"; |
| 7 | + |
| 8 | +contract CallIntervalEnforcerTest is Test { |
| 9 | + CallIntervalEnforcer public enforcer; |
| 10 | + address public delegationManager = address(0x1234); |
| 11 | + bytes32 public delegationHash = keccak256("test-delegation"); |
| 12 | + uint256 public interval = 1 hours; |
| 13 | + bytes public terms; |
| 14 | + |
| 15 | + ModeCode public defaultMode = ModeCode.wrap(bytes32(0)); |
| 16 | + |
| 17 | + function setUp() public { |
| 18 | + vm.warp(10000); |
| 19 | + enforcer = new CallIntervalEnforcer(); |
| 20 | + terms = abi.encodePacked(interval); |
| 21 | + } |
| 22 | + |
| 23 | + function testRevertOnInvalidTermsLength() public { |
| 24 | + bytes memory invalidTerms = new bytes(31); |
| 25 | + vm.expectRevert("CallIntervalEnforcer:invalid-terms-length"); |
| 26 | + enforcer.getTermsInfo(invalidTerms); |
| 27 | + } |
| 28 | + |
| 29 | + function testFirstCallSucceeds() public { |
| 30 | + vm.prank(delegationManager); |
| 31 | + enforcer.beforeHook(terms, "", defaultMode, "", delegationHash, address(0), address(0)); |
| 32 | + uint256 lastCall = enforcer.lastCallExecution(delegationManager, delegationHash); |
| 33 | + assertEq(lastCall, block.timestamp); |
| 34 | + } |
| 35 | + |
| 36 | + function testRevertIfIntervalNotElapsed() public { |
| 37 | + // First call |
| 38 | + vm.prank(delegationManager); |
| 39 | + enforcer.beforeHook(terms, "", defaultMode, "", delegationHash, address(0), address(0)); |
| 40 | + // Second call immediately |
| 41 | + vm.prank(delegationManager); |
| 42 | + vm.expectRevert("CallIntervalEnforcer:early-delegation"); |
| 43 | + enforcer.beforeHook(terms, "", defaultMode, "", delegationHash, address(0), address(0)); |
| 44 | + } |
| 45 | + |
| 46 | + function testCallSucceedsAfterInterval() public { |
| 47 | + // First call |
| 48 | + vm.prank(delegationManager); |
| 49 | + enforcer.beforeHook(terms, "", defaultMode, "", delegationHash, address(0), address(0)); |
| 50 | + // Warp time forward by interval + 1 |
| 51 | + vm.warp(block.timestamp + interval + 1); |
| 52 | + // Second call |
| 53 | + vm.prank(delegationManager); |
| 54 | + enforcer.beforeHook(terms, "", defaultMode, "", delegationHash, address(0), address(0)); |
| 55 | + uint256 lastCall = enforcer.lastCallExecution(delegationManager, delegationHash); |
| 56 | + assertEq(lastCall, block.timestamp); |
| 57 | + } |
| 58 | + |
| 59 | + function testZeroIntervalAllowsImmediateCalls() public { |
| 60 | + bytes memory zeroTerms = abi.encodePacked(uint256(0)); |
| 61 | + // First call |
| 62 | + vm.prank(delegationManager); |
| 63 | + enforcer.beforeHook(zeroTerms, "", defaultMode, "", delegationHash, address(0), address(0)); |
| 64 | + // Second call immediately |
| 65 | + vm.prank(delegationManager); |
| 66 | + enforcer.beforeHook(zeroTerms, "", defaultMode, "", delegationHash, address(0), address(0)); |
| 67 | + // Should not revert |
| 68 | + } |
| 69 | + |
| 70 | + function testDifferentDelegationHashesTrackedIndependently() public { |
| 71 | + bytes32 hash1 = keccak256("hash1"); |
| 72 | + bytes32 hash2 = keccak256("hash2"); |
| 73 | + vm.prank(delegationManager); |
| 74 | + enforcer.beforeHook(terms, "", defaultMode, "", hash1, address(0), address(0)); |
| 75 | + vm.prank(delegationManager); |
| 76 | + enforcer.beforeHook(terms, "", defaultMode, "", hash2, address(0), address(0)); |
| 77 | + assertEq(enforcer.lastCallExecution(delegationManager, hash1), block.timestamp); |
| 78 | + assertEq(enforcer.lastCallExecution(delegationManager, hash2), block.timestamp); |
| 79 | + } |
| 80 | + |
| 81 | + function testDifferentManagersTrackedIndependently() public { |
| 82 | + address manager1 = address(0x1111); |
| 83 | + address manager2 = address(0x2222); |
| 84 | + vm.prank(manager1); |
| 85 | + enforcer.beforeHook(terms, "", defaultMode, "", delegationHash, address(0), address(0)); |
| 86 | + vm.prank(manager2); |
| 87 | + enforcer.beforeHook(terms, "", defaultMode, "", delegationHash, address(0), address(0)); |
| 88 | + assertEq(enforcer.lastCallExecution(manager1, delegationHash), block.timestamp); |
| 89 | + assertEq(enforcer.lastCallExecution(manager2, delegationHash), block.timestamp); |
| 90 | + } |
| 91 | +} |
0 commit comments