From 9c4f2605a0e9c7f2164444826ee27af1eab4c21d Mon Sep 17 00:00:00 2001 From: Pascal Marco Caversaccio Date: Wed, 20 Mar 2024 11:44:18 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20=20Make=20`AccessControl`?= =?UTF-8?q?=20Modules-Ready?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Pascal Marco Caversaccio --- src/snekmate/auth/AccessControl.vy | 18 ------ src/snekmate/auth/mocks/AccessControlMock.vy | 63 ++++++++++++++++++++ test/auth/AccessControl.t.sol | 4 +- 3 files changed, 65 insertions(+), 20 deletions(-) create mode 100644 src/snekmate/auth/mocks/AccessControlMock.vy diff --git a/src/snekmate/auth/AccessControl.vy b/src/snekmate/auth/AccessControl.vy index b6fdb0c3..57b984f3 100644 --- a/src/snekmate/auth/AccessControl.vy +++ b/src/snekmate/auth/AccessControl.vy @@ -69,22 +69,6 @@ implements: IAccessControl DEFAULT_ADMIN_ROLE: public(constant(bytes32)) = empty(bytes32) -# @dev An additional 32-byte access role. -# @notice Please adjust the naming of the variable -# according to your specific requirement, -# e.g. `MINTER_ROLE`. -ADDITIONAL_ROLE_1: public(constant(bytes32)) = keccak256("ADDITIONAL_ROLE_1") - - -# @dev An additional 32-byte access role. -# @notice Please adjust the naming of the variable -# according to your specific requirement, -# e.g. `PAUSER_ROLE`. Also, feel free to add more -# roles if necessary. In this case, it is important -# to extend the constructor accordingly. -ADDITIONAL_ROLE_2: public(constant(bytes32)) = keccak256("ADDITIONAL_ROLE_2") - - # @dev Stores the ERC-165 interface identifier for each # imported interface. The ERC-165 interface identifier # is defined as the XOR of all function selectors in the @@ -147,8 +131,6 @@ def __init__(): the `msg.sender`. """ self._grant_role(DEFAULT_ADMIN_ROLE, msg.sender) - self._grant_role(ADDITIONAL_ROLE_1, msg.sender) - self._grant_role(ADDITIONAL_ROLE_2, msg.sender) @external diff --git a/src/snekmate/auth/mocks/AccessControlMock.vy b/src/snekmate/auth/mocks/AccessControlMock.vy new file mode 100644 index 00000000..53a97660 --- /dev/null +++ b/src/snekmate/auth/mocks/AccessControlMock.vy @@ -0,0 +1,63 @@ +# pragma version ~=0.4.0b5 +""" +@title Wrapper Contract for Multi-Role-Based Access Control Functions +@custom:contract-name AccessControlMock +@license GNU Affero General Public License v3.0 only +@author pcaversaccio +""" + + +# @dev We import and initialise the `AccessControl` module. +from .. import AccessControl as ac +initializes: ac + + +# @dev We import and implement the `IERC165` interface, +# which is a built-in interface of the Vyper compiler. +from ethereum.ercs import IERC165 +implements: IERC165 + + +# @dev We import and implement the `IAccessControl` +# interface, which is written using standard Vyper +# syntax. +from ..interfaces import IAccessControl +implements: IAccessControl + + +# @dev An additional 32-byte access role. +ADDITIONAL_ROLE_1: public(constant(bytes32)) = keccak256("ADDITIONAL_ROLE_1") + + +# @dev An additional 32-byte access role. +ADDITIONAL_ROLE_2: public(constant(bytes32)) = keccak256("ADDITIONAL_ROLE_2") + + +# @dev We export all public functions from the `AccessControl` module. +# @notice It's important to also export public `immutable` and state +# variables. +exports: ( + ac.supportsInterface, + ac.DEFAULT_ADMIN_ROLE, + ac.hasRole, + ac.getRoleAdmin, + ac.grantRole, + ac.revokeRole, + ac.renounceRole, + ac.set_role_admin +) + + +@deploy +@payable +def __init__(): + """ + @dev To omit the opcodes for checking the `msg.value` + in the creation-time EVM bytecode, the constructor + is declared as `payable`. + @notice All predefined roles will be assigned to + the `msg.sender`. + """ + ac.__init__() # Assigns the `DEFAULT_ADMIN_ROLE` to the `msg.sender`. + ac._grant_role(ADDITIONAL_ROLE_1, msg.sender) + ac._grant_role(ADDITIONAL_ROLE_2, msg.sender) diff --git a/test/auth/AccessControl.t.sol b/test/auth/AccessControl.t.sol index 30a391af..739fc912 100644 --- a/test/auth/AccessControl.t.sol +++ b/test/auth/AccessControl.t.sol @@ -23,7 +23,7 @@ contract AccessControlTest is Test { function setUp() public { accessControl = IAccessControlExtended( - vyperDeployer.deployContract("src/snekmate/auth/", "AccessControl") + vyperDeployer.deployContract("src/snekmate/auth/mocks/", "AccessControlMock") ); } @@ -54,7 +54,7 @@ contract AccessControlTest is Test { vm.expectEmit(true, true, true, false); emit IAccessControl.RoleGranted(ADDITIONAL_ROLE_2, deployer, deployer); accessControlInitialEvent = IAccessControlExtended( - vyperDeployer.deployContract("src/snekmate/auth/", "AccessControl") + vyperDeployer.deployContract("src/snekmate/auth/mocks/", "AccessControlMock") ); assertEq( accessControlInitialEvent.DEFAULT_ADMIN_ROLE(),