From 08d109d87725e36dce92db28c7a74bb49bde38ae Mon Sep 17 00:00:00 2001 From: James Finucane Date: Tue, 7 Jun 2022 16:21:58 +0300 Subject: [PATCH] Optimize Ownable and Pausable modifiers' size impact (#3347) --- contracts/access/Ownable.sol | 13 ++++++++++--- contracts/security/Pausable.sol | 32 +++++++++++++++++++++++--------- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/contracts/access/Ownable.sol b/contracts/access/Ownable.sol index 0b2ca8e3c89..16752d5901c 100644 --- a/contracts/access/Ownable.sol +++ b/contracts/access/Ownable.sol @@ -29,6 +29,14 @@ abstract contract Ownable is Context { _transferOwnership(_msgSender()); } + /** + * @dev Throws if called by any account other than the owner. + */ + modifier onlyOwner() { + _checkOwner(); + _; + } + /** * @dev Returns the address of the current owner. */ @@ -37,11 +45,10 @@ abstract contract Ownable is Context { } /** - * @dev Throws if called by any account other than the owner. + * @dev Throws if the sender is not the owner. */ - modifier onlyOwner() { + function _checkOwner() internal view virtual { require(owner() == _msgSender(), "Ownable: caller is not the owner"); - _; } /** diff --git a/contracts/security/Pausable.sol b/contracts/security/Pausable.sol index 0c09e6c8a6e..af04ddb84d1 100644 --- a/contracts/security/Pausable.sol +++ b/contracts/security/Pausable.sol @@ -34,13 +34,6 @@ abstract contract Pausable is Context { _paused = false; } - /** - * @dev Returns true if the contract is paused, and false otherwise. - */ - function paused() public view virtual returns (bool) { - return _paused; - } - /** * @dev Modifier to make a function callable only when the contract is not paused. * @@ -49,7 +42,7 @@ abstract contract Pausable is Context { * - The contract must not be paused. */ modifier whenNotPaused() { - require(!paused(), "Pausable: paused"); + _requireNotPaused(); _; } @@ -61,10 +54,31 @@ abstract contract Pausable is Context { * - The contract must be paused. */ modifier whenPaused() { - require(paused(), "Pausable: not paused"); + _requirePaused(); _; } + /** + * @dev Returns true if the contract is paused, and false otherwise. + */ + function paused() public view virtual returns (bool) { + return _paused; + } + + /** + * @dev Throws if the contract is paused. + */ + function _requireNotPaused() internal view virtual { + require(!paused(), "Pausable: paused"); + } + + /** + * @dev Throws if the contract is not paused. + */ + function _requirePaused() internal view virtual { + require(paused(), "Pausable: not paused"); + } + /** * @dev Triggers stopped state. *