|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.20; |
| 3 | + |
| 4 | +import {Test} from "forge-std/Test.sol"; |
| 5 | + |
| 6 | +import {ProxyAdmin} from "@openzeppelin/contracts/proxy/transparent/ProxyAdmin.sol"; |
| 7 | +import {ITransparentUpgradeableProxy, TransparentUpgradeableProxy} from "@openzeppelin/contracts/proxy/transparent/TransparentUpgradeableProxy.sol"; |
| 8 | + |
| 9 | +import {PauseController} from "../../misc/PauseController.sol"; |
| 10 | +import {IPausable} from "../../misc/IPausable.sol"; |
| 11 | +import {ScrollOwner} from "../../misc/ScrollOwner.sol"; |
| 12 | + |
| 13 | +contract MockPausable is IPausable { |
| 14 | + bool private _paused; |
| 15 | + |
| 16 | + function setPause(bool _status) external { |
| 17 | + _paused = _status; |
| 18 | + } |
| 19 | + |
| 20 | + function paused() external view returns (bool) { |
| 21 | + return _paused; |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +contract PauseControllerTest is Test { |
| 26 | + event Pause(address indexed component); |
| 27 | + event Unpause(address indexed component); |
| 28 | + event UpdatePauseCooldownPeriod(uint256 oldPauseCooldownPeriod, uint256 newPauseCooldownPeriod); |
| 29 | + |
| 30 | + uint256 public constant PAUSE_COOLDOWN_PERIOD = 1 days; |
| 31 | + |
| 32 | + ProxyAdmin public admin; |
| 33 | + PauseController public pauseController; |
| 34 | + MockPausable public mockPausable; |
| 35 | + ScrollOwner public scrollOwner; |
| 36 | + address public owner; |
| 37 | + |
| 38 | + function setUp() public { |
| 39 | + owner = makeAddr("owner"); |
| 40 | + vm.startPrank(owner); |
| 41 | + |
| 42 | + admin = new ProxyAdmin(); |
| 43 | + scrollOwner = new ScrollOwner(); |
| 44 | + PauseController impl = new PauseController(address(scrollOwner)); |
| 45 | + TransparentUpgradeableProxy proxy = new TransparentUpgradeableProxy( |
| 46 | + address(impl), |
| 47 | + address(admin), |
| 48 | + abi.encodeCall(PauseController.initialize, (PAUSE_COOLDOWN_PERIOD)) |
| 49 | + ); |
| 50 | + pauseController = PauseController(address(proxy)); |
| 51 | + mockPausable = new MockPausable(); |
| 52 | + |
| 53 | + bytes4[] memory selectors = new bytes4[](1); |
| 54 | + selectors[0] = IPausable.setPause.selector; |
| 55 | + scrollOwner.updateAccess(address(mockPausable), selectors, pauseController.PAUSE_CONTROLLER_ROLE(), true); |
| 56 | + scrollOwner.grantRole(pauseController.PAUSE_CONTROLLER_ROLE(), address(pauseController)); |
| 57 | + |
| 58 | + vm.stopPrank(); |
| 59 | + |
| 60 | + vm.warp(1e9); |
| 61 | + } |
| 62 | + |
| 63 | + function test_Pause() public { |
| 64 | + vm.startPrank(owner); |
| 65 | + |
| 66 | + vm.expectEmit(true, false, false, true); |
| 67 | + emit Pause(address(mockPausable)); |
| 68 | + pauseController.pause(mockPausable); |
| 69 | + assertTrue(mockPausable.paused()); |
| 70 | + |
| 71 | + vm.stopPrank(); |
| 72 | + } |
| 73 | + |
| 74 | + function test_Pause_AlreadyPaused() public { |
| 75 | + vm.startPrank(owner); |
| 76 | + |
| 77 | + pauseController.pause(mockPausable); |
| 78 | + |
| 79 | + vm.expectRevert(PauseController.ErrorComponentAlreadyPaused.selector); |
| 80 | + pauseController.pause(mockPausable); |
| 81 | + |
| 82 | + vm.stopPrank(); |
| 83 | + } |
| 84 | + |
| 85 | + function test_Pause_CooldownPeriodNotPassed() public { |
| 86 | + vm.startPrank(owner); |
| 87 | + |
| 88 | + pauseController.pause(mockPausable); |
| 89 | + pauseController.unpause(mockPausable); |
| 90 | + uint256 lastUnpauseTime = pauseController.getLastUnpauseTime(mockPausable); |
| 91 | + assertEq(lastUnpauseTime, block.timestamp); |
| 92 | + |
| 93 | + vm.warp(lastUnpauseTime + PAUSE_COOLDOWN_PERIOD - 1); |
| 94 | + vm.expectRevert(PauseController.ErrorCooldownPeriodNotPassed.selector); |
| 95 | + pauseController.pause(mockPausable); |
| 96 | + assertFalse(mockPausable.paused()); |
| 97 | + |
| 98 | + vm.warp(lastUnpauseTime + PAUSE_COOLDOWN_PERIOD); |
| 99 | + vm.expectRevert(PauseController.ErrorCooldownPeriodNotPassed.selector); |
| 100 | + pauseController.pause(mockPausable); |
| 101 | + assertFalse(mockPausable.paused()); |
| 102 | + |
| 103 | + vm.warp(lastUnpauseTime + PAUSE_COOLDOWN_PERIOD + 1); |
| 104 | + pauseController.pause(mockPausable); |
| 105 | + assertTrue(mockPausable.paused()); |
| 106 | + |
| 107 | + vm.stopPrank(); |
| 108 | + } |
| 109 | + |
| 110 | + function test_Unpause() public { |
| 111 | + vm.startPrank(owner); |
| 112 | + |
| 113 | + pauseController.pause(mockPausable); |
| 114 | + |
| 115 | + assertEq(pauseController.getLastUnpauseTime(mockPausable), 0); |
| 116 | + vm.expectEmit(true, false, false, true); |
| 117 | + emit Unpause(address(mockPausable)); |
| 118 | + pauseController.unpause(mockPausable); |
| 119 | + assertEq(pauseController.getLastUnpauseTime(mockPausable), block.timestamp); |
| 120 | + |
| 121 | + assertFalse(mockPausable.paused()); |
| 122 | + |
| 123 | + vm.stopPrank(); |
| 124 | + } |
| 125 | + |
| 126 | + function test_Unpause_NotPaused() public { |
| 127 | + vm.startPrank(owner); |
| 128 | + |
| 129 | + vm.expectRevert(PauseController.ErrorComponentNotPaused.selector); |
| 130 | + pauseController.unpause(mockPausable); |
| 131 | + |
| 132 | + vm.stopPrank(); |
| 133 | + } |
| 134 | + |
| 135 | + function test_UpdatePauseCooldownPeriod() public { |
| 136 | + vm.startPrank(owner); |
| 137 | + |
| 138 | + uint256 newCooldownPeriod = 2 days; |
| 139 | + |
| 140 | + vm.expectEmit(false, false, false, true); |
| 141 | + emit UpdatePauseCooldownPeriod(PAUSE_COOLDOWN_PERIOD, newCooldownPeriod); |
| 142 | + pauseController.updatePauseCooldownPeriod(newCooldownPeriod); |
| 143 | + |
| 144 | + assertEq(pauseController.pauseCooldownPeriod(), newCooldownPeriod); |
| 145 | + |
| 146 | + vm.stopPrank(); |
| 147 | + } |
| 148 | + |
| 149 | + function test_UpdatePauseCooldownPeriod_NotOwner() public { |
| 150 | + address notOwner = makeAddr("notOwner"); |
| 151 | + vm.startPrank(notOwner); |
| 152 | + |
| 153 | + vm.expectRevert("Ownable: caller is not the owner"); |
| 154 | + pauseController.updatePauseCooldownPeriod(2 days); |
| 155 | + |
| 156 | + vm.stopPrank(); |
| 157 | + } |
| 158 | + |
| 159 | + function test_Pause_NotOwner() public { |
| 160 | + address notOwner = makeAddr("notOwner"); |
| 161 | + vm.startPrank(notOwner); |
| 162 | + |
| 163 | + vm.expectRevert("Ownable: caller is not the owner"); |
| 164 | + pauseController.pause(mockPausable); |
| 165 | + |
| 166 | + vm.stopPrank(); |
| 167 | + } |
| 168 | + |
| 169 | + function test_Unpause_NotOwner() public { |
| 170 | + address notOwner = makeAddr("notOwner"); |
| 171 | + vm.startPrank(notOwner); |
| 172 | + |
| 173 | + vm.expectRevert("Ownable: caller is not the owner"); |
| 174 | + pauseController.unpause(mockPausable); |
| 175 | + |
| 176 | + vm.stopPrank(); |
| 177 | + } |
| 178 | +} |
0 commit comments