Hello everyone,
I was making a contract using AccessControl
and a question came up when I looked at grant
and revoke
functions.
With a pre-check for error return in my contract:
require(!hasRole(PARTNER_ROLE, _msgSender()), "Partner already stored");
// Do stuff with partner
_grantRole(PARTNER_ROLE, _msgSender());
Actual AccessControl _grantRole()
function:
function _grantRole(bytes32 role, address account) internal virtual {
if (!hasRole(role, account)) {
_roles[role].members[account] = true;
emit RoleGranted(role, account, _msgSender());
}
}
My proposal:
function _grantRole(bytes32 role, address account) internal virtual {
require(!hasRole(role, account), "AccessControl: Account already with role");
_roles[role].members[account] = true;
emit RoleGranted(role, account, _msgSender());
}
With this change there would be only one call to hasRole
in contracts that inherit and want to give a role. There are a security/optimization issues with this? What do you think?
Thanks in advance,
Iván M.M