Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is the purpose of access control in smart contracts?
Access control restricts who can call certain functions in a smart contract to protect sensitive operations and prevent unauthorized actions.
Click to reveal answer
beginner
What is OpenZeppelin's Ownable contract used for?
Ownable provides a simple access control mechanism where there is an owner account that can be granted exclusive access to specific functions.
Click to reveal answer
beginner
How do you restrict a function to be called only by the owner using OpenZeppelin?
Use the onlyOwner modifier provided by the Ownable contract. It checks if the caller is the owner before running the function.
Click to reveal answer
intermediate
What is the difference between Ownable and AccessControl in OpenZeppelin?
Ownable manages a single owner with exclusive rights. AccessControl allows multiple roles with different permissions, offering more flexible access management.
Click to reveal answer
intermediate
How do you define a new role using OpenZeppelin's AccessControl?
Define a bytes32 constant for the role, e.g., bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");, then use _setupRole or grantRole to assign it.
Click to reveal answer
Which OpenZeppelin contract provides a simple owner-based access control?
AAccessControl
BPausable
COwnable
DERC20
✗ Incorrect
Ownable is designed for single-owner access control.
What does the onlyOwner modifier do?
AAllows only the contract owner to call the function
BAllows anyone to call the function
CPrevents the owner from calling the function
DAllows only addresses with a specific role
✗ Incorrect
onlyOwner restricts function calls to the owner.
How do you create a new role in AccessControl?
ACall <code>transferOwnership</code>
BDefine a <code>bytes32</code> constant with <code>keccak256</code>
CUse <code>onlyOwner</code> modifier
DUse <code>approve</code> function
✗ Incorrect
Roles are defined as bytes32 constants using keccak256.
Which function assigns a role to an address in AccessControl?
AonlyOwner
BtransferOwnership
Capprove
DgrantRole
✗ Incorrect
grantRole assigns roles to addresses.
What is a key benefit of using AccessControl over Ownable?
ASupports multiple roles with different permissions
Explain how to use OpenZeppelin's Ownable contract to restrict a function to the contract owner.
Think about how the contract knows who the owner is and how it checks the caller.
You got /3 concepts.
Describe the steps to create and assign a new role using OpenZeppelin's AccessControl.
Roles are like keys that let addresses do certain things.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of using onlyRole modifier in OpenZeppelin's Access Control?
easy
A. To restrict function access to accounts with a specific role
B. To automatically assign roles to all users
C. To allow anyone to call the function without restrictions
D. To log all function calls for auditing
Solution
Step 1: Understand the purpose of onlyRole
The onlyRole modifier is used to limit access to functions so only users with a certain role can execute them.
Step 2: Analyze the options
To restrict function access to accounts with a specific role correctly states that it restricts function access to accounts with a specific role. Other options describe unrelated behaviors.
Final Answer:
To restrict function access to accounts with a specific role -> Option A
Quick Check:
Access control = restrict by role [OK]
Hint: Remember: onlyRole means only users with that role can call [OK]
Common Mistakes:
Thinking onlyRole assigns roles automatically
Believing onlyRole allows open access
Confusing onlyRole with event logging
2. Which of the following is the correct way to declare a role constant in OpenZeppelin Access Control?
easy
A. address constant ADMIN_ROLE = 0x123;
B. string public ADMIN_ROLE = "ADMIN_ROLE";
C. uint256 constant ADMIN_ROLE = 1;
D. bytes32 public constant ADMIN_ROLE = keccak256("ADMIN_ROLE");
Solution
Step 1: Recall role declaration syntax
OpenZeppelin uses bytes32 constants with keccak256 hash of a string to define roles.
Step 2: Check each option
bytes32 public constant ADMIN_ROLE = keccak256("ADMIN_ROLE"); matches the correct pattern. Options B, C, and D use wrong types or formats.
Final Answer:
bytes32 public constant ADMIN_ROLE = keccak256("ADMIN_ROLE"); -> Option D
Quick Check:
Role constants use bytes32 + keccak256 [OK]
Hint: Roles are bytes32 constants hashed with keccak256 [OK]
Common Mistakes:
Using string instead of bytes32 for roles
Assigning numeric or address types to roles
Forgetting to use keccak256 hash
3. Given the following Solidity code snippet, what will happen if an account without the ADMIN_ROLE calls secureFunction()?
contract MyContract is AccessControl {
bytes32 public constant ADMIN_ROLE = keccak256("ADMIN_ROLE");
constructor() {
_grantRole(ADMIN_ROLE, msg.sender);
}
function secureFunction() public onlyRole(ADMIN_ROLE) {
// critical logic
}
}
medium
A. The function executes normally
B. The call reverts with an access control error
C. The function executes but emits a warning
D. The function executes only if the caller is the contract owner
Solution
Step 1: Understand the onlyRole modifier behavior
The onlyRole(ADMIN_ROLE) modifier restricts access to accounts with ADMIN_ROLE. If the caller lacks this role, the call reverts.
Step 2: Analyze the scenario
The constructor grants ADMIN_ROLE only to msg.sender at deployment. Any other account calling secureFunction will trigger a revert due to missing role.
Final Answer:
The call reverts with an access control error -> Option B
Quick Check:
Missing role causes revert [OK]
Hint: Only accounts with role can call; others revert [OK]
Common Mistakes:
Assuming function runs without role
Thinking warnings are emitted instead of revert
Confusing role with ownership
4. Identify the error in this OpenZeppelin Access Control code snippet:
contract MyContract is AccessControl {
bytes32 public constant ADMIN_ROLE = keccak256("ADMIN_ROLE");
constructor() {
_grantRole(ADMIN_ROLE, msg.sender);
}
function restricted() public onlyRole(ADMIN_ROLE) {
// restricted logic
}
function grantAdmin(address user) public {
_grantRole(ADMIN_ROLE, user);
}
}
medium
A. The grantAdmin function lacks access control and can be called by anyone
B. The _setupRole function is deprecated and should not be used
C. The onlyRole modifier is missing from grantAdmin
D. The ADMIN_ROLE constant is incorrectly declared
Solution
Step 1: Review access control on grantAdmin
The grantAdmin function calls _grantRole but has no modifier restricting who can call it.
Step 2: Identify security risk
Without access control, anyone can call grantAdmin and assign ADMIN_ROLE to themselves or others, breaking security.
Final Answer:
The grantAdmin function lacks access control and can be called by anyone -> Option A
Quick Check:
Grant functions need access control [OK]
Hint: Always protect grantRole functions with onlyRole [OK]
Common Mistakes:
Ignoring missing access control on grant functions
Thinking _grantRole is protected like grantRole
Confusing role declaration syntax
5. You want to create a smart contract where only users with the MINTER_ROLE can mint tokens, and only the contract owner can assign the MINTER_ROLE. Which OpenZeppelin pattern correctly enforces this?
hard
A. Use Ownable and allow only the owner to mint tokens directly without roles
B. Use AccessControl but allow anyone to assign MINTER_ROLE to themselves
C. Use AccessControl with MINTER_ROLE and add onlyRole(DEFAULT_ADMIN_ROLE) modifier to the role assignment function, granting DEFAULT_ADMIN_ROLE to the owner
D. Use AccessControl and assign MINTER_ROLE to everyone by default
Solution
Step 1: Understand role assignment control
To restrict who can assign MINTER_ROLE, use AccessControl's DEFAULT_ADMIN_ROLE for admin rights and protect assignment functions with onlyRole(DEFAULT_ADMIN_ROLE).
Step 2: Connect owner with DEFAULT_ADMIN_ROLE
Grant DEFAULT_ADMIN_ROLE to the contract owner so only they can assign MINTER_ROLE to others.
Final Answer:
Use AccessControl with MINTER_ROLE and add onlyRole(DEFAULT_ADMIN_ROLE) modifier to the role assignment function, granting DEFAULT_ADMIN_ROLE to the owner -> Option C
Quick Check:
Admin role controls role assignment [OK]
Hint: Use DEFAULT_ADMIN_ROLE for owner to control role assignments [OK]