Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pauseUntil tested, coded, documented #57

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 10 additions & 35 deletions contracts/mocks/PausableMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,19 @@

pragma solidity ^0.8.20;

import {DefaultPausable} from "../utils/pausability/DefaultPausable.sol";
import {SafeCast} from "@openzeppelin/contracts/utils/math/SafeCast.sol";
import {PausableUntil} from "../utils/PausableUntil.sol";

contract PausableMock is DefaultPausable {
// solhint-disable-next-line openzeppelin/private-variables
bool public drasticMeasureTaken;
// solhint-disable-next-line openzeppelin/private-variables
uint256 public count;

constructor() {
drasticMeasureTaken = false;
count = 0;
}

function normalProcess() external whenNotPaused {
count++;
}

function drasticMeasure() external whenPaused {
drasticMeasureTaken = true;
}

function pause() external {
_pause();
}

function unpause() external {
_unpause();
abstract contract PausableUntilMock is PausableUntil {
function clock() public view virtual override returns (uint48) {
return SafeCast.toUint48(block.timestamp);
}

function pauseUntil(uint256 duration) external {
_pauseUntil(uint48(duration));
// solhint-disable-next-line func-name-mixedcase
function CLOCK_MODE() public view virtual override returns (string memory) {
return "mode=timestamp";
}

function getPausedUntilDeadline() external view returns (uint256) {
return _unpauseDeadline();
}

function getPausedUntilDeadlineAndTimestamp() external view returns (uint256, uint256) {
return (_unpauseDeadline(), clock());
}
function canCallWhenNotPaused() external whenNotPaused {}
function canCallWhenPaused() external whenPaused {}
}
107 changes: 107 additions & 0 deletions contracts/utils/PausableUntil.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.20;

import {Pausable} from "@openzeppelin/contracts/utils/Pausable.sol";
import {IERC6372} from "@openzeppelin/contracts/interfaces/IERC6372.sol";

/**
* @title Pausable
* @author @CarlosAlegreUr
*
* @dev Contract module which allows children to implement an emergency stop
* mechanism that can be triggered by an authorized account.
*
* Stops can be of undefined duration or for a certain amount of time.
*
* This module is used through inheritance. It will make available the
* modifiers `whenNotPaused` and `whenPaused`, which can be applied to
* the functions of your contract. Note that they will not be `Pausable` by
* simply including this module, only once the modifiers are put in place
* and access to call the internal `_pause()`, `_unpause()`, `_pauseUntil()`
* functions is coded.
*
* [ ⚠️ WARNING ⚠️ ]
* This version should be backwards compatible with previous OpenZeppelin `Pausable`
* versions as it uses the same 1 storage slot in a backwards compatible way.
*
* However this has not been tested yet. Please test locally before updating any
* contract to use this version.
*/
abstract contract PausableUntil is Pausable, IERC6372 {
/**
* @dev Storage slot is structured like so:
*
* - Least significant 8 bits: signal pause state.
* 1 for paused, 0 for unpaused.
*
* - After, the following 48 bits: signal timestamp at which the contract
* will be automatically unpaused if the pause had a duration set.
*/
uint48 private _pausedUntil;

/**
* @dev Emitted when the pause is triggered by `account`. `unpauseDeadline` is 0 if the pause is indefinite.
*/
Comment on lines +43 to +45
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is now incorrect. If unpauseDeadline is 0 (or less than block.timestamp) the contract is not paused at all.

event Paused(address account, uint48 unpauseDeadline);

/**
* @inheritdoc IERC6372
*/
function clock() public view virtual returns (uint48);

/**
* @dev Returns the time date at which the contract will be automatically unpaused.
*
* If returned 0, the contract might or might not be paused.
* This function must not be used for checking paused state.
*/
function _unpauseDeadline() internal view virtual returns (uint48) {
return _pausedUntil;
}

/**
* @dev Triggers stopped state while `unpauseDeadline` date is still in the future.
*
* This function should be used to prevent eternally pausing contracts in complex
* permissioned systems.
*
* Requirements:
*
* - The contract must not be paused.
* - `unpauseDeadline` must be in the future.
* - `clock()` return value and `unpauseDeadline` must be in the same time units.
* - If pausing with an `unpauseDeadline` in the past this function will not pause neither revert.
*/
function _pauseUntil(uint48 unpauseDeadline) internal virtual whenNotPaused {
_pausedUntil = unpauseDeadline;
emit Paused(_msgSender(), unpauseDeadline);
}

/**
* @inheritdoc Pausable
*/
function paused() public view virtual override returns (bool) {
// exit early without an sload if normal paused is enabled
if (super.paused()) return true;

uint48 unpauseDeadline = _unpauseDeadline();
return unpauseDeadline != 0 && this.clock() < unpauseDeadline;
}

/**
* @inheritdoc Pausable
*/
function _pause() internal virtual override {
super._pause();
delete _pausedUntil;
}

/**
* @inheritdoc Pausable
*/
function _unpause() internal virtual override {
super._unpause();
delete _pausedUntil;
}
}
34 changes: 0 additions & 34 deletions contracts/utils/pausability/DefaultPausable.sol

This file was deleted.

201 changes: 0 additions & 201 deletions contracts/utils/pausability/Pausable.sol

This file was deleted.

Loading