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

Add a gas optimization in Pausable similar to the one used in the ReentrancyGuard #4651

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tame-rules-impress.md
@@ -0,0 +1,5 @@
---
'openzeppelin-solidity': patch
---

Add a gas optimization in `Pausable` similar to the one used in the `ReentrancyGuard`
21 changes: 13 additions & 8 deletions contracts/utils/Pausable.sol
Expand Up @@ -15,7 +15,15 @@ import {Context} from "../utils/Context.sol";
* simply including this module, only once the modifiers are put in place.
*/
abstract contract Pausable is Context {
bool private _paused;
// The values being non-zero value makes deployment a bit more expensive,
// but in exchange the refund on every call to Pausable will be lower in
// amount. Since refunds are capped to a percentage of the total
// transaction's gas, it is best to keep them low in cases like this one, to
// increase the likelihood of the full refund coming into effect.
uint256 private constant NOT_PAUSED = 1;
uint256 private constant PAUSED = 2;

uint256 private _paused;

/**
* @dev Emitted when the pause is triggered by `account`.
Expand All @@ -37,11 +45,8 @@ abstract contract Pausable is Context {
*/
error ExpectedPause();

/**
* @dev Initializes the contract in unpaused state.
*/
constructor() {
_paused = false;
_paused = NOT_PAUSED;
}

/**
Expand Down Expand Up @@ -72,7 +77,7 @@ abstract contract Pausable is Context {
* @dev Returns true if the contract is paused, and false otherwise.
*/
function paused() public view virtual returns (bool) {
return _paused;
return _paused == PAUSED;
}

/**
Expand Down Expand Up @@ -101,7 +106,7 @@ abstract contract Pausable is Context {
* - The contract must not be paused.
*/
function _pause() internal virtual whenNotPaused {
_paused = true;
_paused = PAUSED;
emit Paused(_msgSender());
}

Expand All @@ -113,7 +118,7 @@ abstract contract Pausable is Context {
* - The contract must be paused.
*/
function _unpause() internal virtual whenPaused {
_paused = false;
_paused = NOT_PAUSED;
emit Unpaused(_msgSender());
}
}