Replace error strings with custom errors (#4261)
This commit is contained in:
@ -67,6 +67,16 @@ abstract contract Initializable {
|
||||
*/
|
||||
bool private _initializing;
|
||||
|
||||
/**
|
||||
* @dev The contract is already initialized.
|
||||
*/
|
||||
error AlreadyInitialized();
|
||||
|
||||
/**
|
||||
* @dev The contract is not initializing.
|
||||
*/
|
||||
error NotInitializing();
|
||||
|
||||
/**
|
||||
* @dev Triggered when the contract has been initialized or reinitialized.
|
||||
*/
|
||||
@ -83,10 +93,9 @@ abstract contract Initializable {
|
||||
*/
|
||||
modifier initializer() {
|
||||
bool isTopLevelCall = !_initializing;
|
||||
require(
|
||||
(isTopLevelCall && _initialized < 1) || (address(this).code.length == 0 && _initialized == 1),
|
||||
"Initializable: contract is already initialized"
|
||||
);
|
||||
if (!(isTopLevelCall && _initialized < 1) && !(address(this).code.length == 0 && _initialized == 1)) {
|
||||
revert AlreadyInitialized();
|
||||
}
|
||||
_initialized = 1;
|
||||
if (isTopLevelCall) {
|
||||
_initializing = true;
|
||||
@ -117,7 +126,9 @@ abstract contract Initializable {
|
||||
* Emits an {Initialized} event.
|
||||
*/
|
||||
modifier reinitializer(uint8 version) {
|
||||
require(!_initializing && _initialized < version, "Initializable: contract is already initialized");
|
||||
if (_initializing || _initialized >= version) {
|
||||
revert AlreadyInitialized();
|
||||
}
|
||||
_initialized = version;
|
||||
_initializing = true;
|
||||
_;
|
||||
@ -130,7 +141,9 @@ abstract contract Initializable {
|
||||
* {initializer} and {reinitializer} modifiers, directly or indirectly.
|
||||
*/
|
||||
modifier onlyInitializing() {
|
||||
require(_initializing, "Initializable: contract is not initializing");
|
||||
if (!_initializing) {
|
||||
revert NotInitializing();
|
||||
}
|
||||
_;
|
||||
}
|
||||
|
||||
@ -143,7 +156,9 @@ abstract contract Initializable {
|
||||
* Emits an {Initialized} event the first time it is successfully executed.
|
||||
*/
|
||||
function _disableInitializers() internal virtual {
|
||||
require(!_initializing, "Initializable: contract is initializing");
|
||||
if (_initializing) {
|
||||
revert AlreadyInitialized();
|
||||
}
|
||||
if (_initialized != type(uint8).max) {
|
||||
_initialized = type(uint8).max;
|
||||
emit Initialized(type(uint8).max);
|
||||
|
||||
@ -22,6 +22,11 @@ abstract contract UUPSUpgradeable is IERC1822Proxiable, ERC1967Upgrade {
|
||||
/// @custom:oz-upgrades-unsafe-allow state-variable-immutable state-variable-assignment
|
||||
address private immutable __self = address(this);
|
||||
|
||||
/**
|
||||
* @dev The call is from an unauthorized context.
|
||||
*/
|
||||
error UUPSUnauthorizedCallContext(address context);
|
||||
|
||||
/**
|
||||
* @dev Check that the execution is being performed through a delegatecall call and that the execution context is
|
||||
* a proxy contract with an implementation (as defined in ERC1967) pointing to self. This should only be the case
|
||||
@ -30,8 +35,15 @@ abstract contract UUPSUpgradeable is IERC1822Proxiable, ERC1967Upgrade {
|
||||
* fail.
|
||||
*/
|
||||
modifier onlyProxy() {
|
||||
require(address(this) != __self, "Function must be called through delegatecall");
|
||||
require(_getImplementation() == __self, "Function must be called through active proxy");
|
||||
if (address(this) == __self) {
|
||||
// Must be called through delegatecall
|
||||
revert UUPSUnauthorizedCallContext(address(this));
|
||||
}
|
||||
address implementation = _getImplementation();
|
||||
if (implementation != __self) {
|
||||
// Must be called through an active proxy
|
||||
revert UUPSUnauthorizedCallContext(implementation);
|
||||
}
|
||||
_;
|
||||
}
|
||||
|
||||
@ -40,7 +52,10 @@ abstract contract UUPSUpgradeable is IERC1822Proxiable, ERC1967Upgrade {
|
||||
* callable on the implementing contract but not through proxies.
|
||||
*/
|
||||
modifier notDelegated() {
|
||||
require(address(this) == __self, "UUPSUpgradeable: must not be called through delegatecall");
|
||||
if (address(this) != __self) {
|
||||
// Must not be called through delegatecall
|
||||
revert UUPSUnauthorizedCallContext(address(this));
|
||||
}
|
||||
_;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user