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.
modifier onlyAuthorized {
if (!authorized[msg.sender]) {
LibRichErrors.rrevert(LibAuthorizableRichErrors.SenderNotAuthorizedError(msg.sender));
}
_assertSenderIsAuthorized();
_;
}
@ -122,4 +120,14 @@ contract Authorizable is
{
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() {
if (msg.sender != owner) {
LibRichErrors.rrevert(LibOwnableRichErrors.OnlyOwnerError(
msg.sender,
owner
));
}
_assertSenderIsOwner();
_;
}
@ -36,4 +31,16 @@ contract Ownable is
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
/// before function execution and unlocked after.
modifier nonReentrant() {
_lockMutexOrThrowIfAlreadyLocked();
_;
_unlockMutex();
}
function _lockMutexOrThrowIfAlreadyLocked()
internal
{
// Ensure mutex is unlocked.
if (locked) {
LibRichErrors.rrevert(
LibReentrancyGuardRichErrors.IllegalReentrancyError()
);
}
// Lock mutex before function call
// Lock mutex.
locked = true;
}
// Perform function call
_;
// Unlock mutex after function call
function _unlockMutex()
internal
{
// Unlock mutex.
locked = false;
}
}