Access control is essential in blockchain smart contracts to restrict who can perform sensitive actions like transferring tokens or changing contract settings.
💼 Career
Understanding access control patterns is key for blockchain developers to write secure smart contracts that protect assets and data.
Progress0 / 4 steps
1
DATA SETUP: Create the initial authorized users list
Create a list called authorized_users with these exact addresses: '0x123', '0x456', and '0x789'.
Blockchain / Solidity
Hint
Use a Python list with the exact strings for addresses.
2
CONFIGURATION: Add the owner address variable
Create a variable called owner and set it to the exact address '0xabc'.
Blockchain / Solidity
Hint
Just assign the string '0xabc' to the variable owner.
3
CORE LOGIC: Write a function to add authorized users by owner only
Write a function called add_authorized_user that takes parameters caller and new_user. Inside, check if caller == owner. If yes, append new_user to authorized_users. Otherwise, do nothing.
Blockchain / Solidity
Hint
Use an if statement to compare caller and owner, then append new_user to authorized_users.
4
OUTPUT: Check if a user is authorized and print the result
Write code to check if the address '0x456' is in authorized_users and print true or false accordingly.
Blockchain / Solidity
Hint
Use the in keyword to check membership and print the boolean result.
Practice
(1/5)
1. What is the main purpose of access control patterns in blockchain smart contracts?
easy
A. To restrict who can perform certain actions on the blockchain
B. To increase the speed of blockchain transactions
C. To store large amounts of data efficiently
D. To create new cryptocurrencies automatically
Solution
Step 1: Understand access control concept
Access control limits permissions, deciding who can do what on the blockchain.
Step 2: Identify the main goal
The goal is to protect blockchain data and functions by restricting unauthorized actions.
Final Answer:
To restrict who can perform certain actions on the blockchain -> Option A
Quick Check:
Access control = restrict actions [OK]
Hint: Access control means who can do what, not speed or storage [OK]
Common Mistakes:
Confusing access control with transaction speed
Thinking access control stores data
Assuming access control creates cryptocurrencies
2. Which of the following is the correct Solidity syntax to define a modifier named onlyOwner for access control?
easy
A. modifier onlyOwner() => { require(msg.sender == owner); _; }
B. function onlyOwner() { require(msg.sender == owner); }
C. modifier onlyOwner { if(msg.sender == owner) _; }
D. modifier onlyOwner() { require(msg.sender == owner); _; }
Solution
Step 1: Recall modifier syntax in Solidity
A modifier uses the keyword 'modifier', parentheses for parameters, and the '_;' placeholder.
C. Using assignment '=' instead of comparison '==' in require
D. Modifier missing parentheses
Solution
Step 1: Check require statement syntax
The require uses '=' which assigns, not '==' which compares. This causes a compile error or unexpected behavior.
Step 2: Verify other parts
Semicolon is present, constructor name is correct, and modifier has parentheses, so no issues there.
Final Answer:
Using assignment '=' instead of comparison '==' in require -> Option C
Quick Check:
Use '==' for comparison in require [OK]
Hint: Use '==' to compare, '=' is assignment [OK]
Common Mistakes:
Confusing '=' and '==' in conditions
Thinking constructor name must differ
Ignoring parentheses in modifier
5. You want to implement a multi-level access control in Solidity where:
- The contract owner can add or remove admins.
- Admins can add or remove users.
- Users can only call a restricted function.
Which pattern best fits this scenario?
hard
A. Use nested mappings with modifiers for owner, admin, and user roles
B. Use a single boolean mapping for all roles and check in functions
C. Use public variables for roles without modifiers
D. Use events only to track roles without restrictions
Solution
Step 1: Understand multi-level access control needs
Different roles require different permissions; owner controls admins, admins control users, users have limited access.
Step 2: Evaluate patterns
Nested mappings with modifiers (Use nested mappings with modifiers for owner, admin, and user roles) is complex but correct. Single boolean mapping (Use a single boolean mapping for all roles and check in functions) cannot distinguish roles well. Public variables without modifiers (Use public variables for roles without modifiers) is insecure and insufficient. Events only (Use events only to track roles without restrictions) do not enforce control.
Step 3: Choose best practical pattern
Nested mappings with modifiers provide clear, secure role checks and control flow.
Final Answer:
Use nested mappings with modifiers for owner, admin, and user roles -> Option A
Quick Check:
Multi-level roles need nested mappings + modifiers [OK]
Hint: Use nested mappings and modifiers for clear role control [OK]
Common Mistakes:
Using single mapping for all roles
Skipping modifiers for access checks
Relying only on events without restrictions
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.