Use internal function in onlyOwner, onlyAuthorized, and nonReentrant modifiers

This commit is contained in:
Amir Bandeali 2019-09-01 15:54:16 -07:00
parent dd499591e9
commit fe01a150f0
3 changed files with 38 additions and 15 deletions

View File

@ -30,9 +30,7 @@ contract Authorizable is
{ {
/// @dev Only authorized addresses can invoke functions with this modifier. /// @dev Only authorized addresses can invoke functions with this modifier.
modifier onlyAuthorized { modifier onlyAuthorized {
if (!authorized[msg.sender]) { _assertSenderIsAuthorized();
LibRichErrors.rrevert(LibAuthorizableRichErrors.SenderNotAuthorizedError(msg.sender));
}
_; _;
} }
@ -122,4 +120,14 @@ contract Authorizable is
{ {
return authorities; return authorities;
} }
/// @dev Reverts if msg.sender is not authorized.
function _assertSenderIsAuthorized()
internal
view
{
if (!authorized[msg.sender]) {
LibRichErrors.rrevert(LibAuthorizableRichErrors.SenderNotAuthorizedError(msg.sender));
}
}
} }

View File

@ -17,12 +17,7 @@ contract Ownable is
} }
modifier onlyOwner() { modifier onlyOwner() {
if (msg.sender != owner) { _assertSenderIsOwner();
LibRichErrors.rrevert(LibOwnableRichErrors.OnlyOwnerError(
msg.sender,
owner
));
}
_; _;
} }
@ -36,4 +31,16 @@ contract Ownable is
owner = newOwner; owner = newOwner;
} }
} }
function _assertSenderIsOwner()
internal
view
{
if (msg.sender != owner) {
LibRichErrors.rrevert(LibOwnableRichErrors.OnlyOwnerError(
msg.sender,
owner
));
}
}
} }

View File

@ -30,20 +30,28 @@ contract ReentrancyGuard {
/// @dev Functions with this modifer cannot be reentered. The mutex will be locked /// @dev Functions with this modifer cannot be reentered. The mutex will be locked
/// before function execution and unlocked after. /// before function execution and unlocked after.
modifier nonReentrant() { modifier nonReentrant() {
_lockMutexOrThrowIfAlreadyLocked();
_;
_unlockMutex();
}
function _lockMutexOrThrowIfAlreadyLocked()
internal
{
// Ensure mutex is unlocked. // Ensure mutex is unlocked.
if (locked) { if (locked) {
LibRichErrors.rrevert( LibRichErrors.rrevert(
LibReentrancyGuardRichErrors.IllegalReentrancyError() LibReentrancyGuardRichErrors.IllegalReentrancyError()
); );
} }
// Lock mutex.
// Lock mutex before function call
locked = true; locked = true;
}
// Perform function call function _unlockMutex()
_; internal
{
// Unlock mutex after function call // Unlock mutex.
locked = false; locked = false;
} }
} }