Access control patterns in Blockchain / Solidity - Time & Space Complexity
When we control who can do what in a blockchain, we use access control patterns. It's important to know how the cost of checking permissions grows as more users or roles are added.
We want to find out how the time to check access changes when the number of users or roles increases.
Analyze the time complexity of the following access control check in a smart contract.
mapping(address => bool) public admins;
function isAdmin(address user) public view returns (bool) {
return admins[user];
}
function addAdmin(address user) public {
require(admins[msg.sender], "Not authorized");
admins[user] = true;
}
This code checks if a user is an admin by looking up a mapping, and allows existing admins to add new admins.
Look for repeated actions that affect time cost.
- Primary operation: Accessing the mapping to check if a user is an admin.
- How many times: Each time the function is called, the mapping is accessed once.
The time to check if a user is an admin stays about the same no matter how many admins exist.
| Input Size (number of admins) | Approx. Operations |
|---|---|
| 10 | 1 mapping lookup |
| 100 | 1 mapping lookup |
| 1000 | 1 mapping lookup |
Pattern observation: The operation count stays constant even as the number of admins grows.
Time Complexity: O(1)
This means checking access takes the same amount of time no matter how many users or roles there are.
[X] Wrong: "Checking if a user is an admin takes longer as more admins are added."
[OK] Correct: The mapping lookup is direct and does not depend on the number of admins, so the time stays constant.
Understanding how access control checks scale helps you design efficient smart contracts that stay fast as they grow. This skill shows you can think about real-world blockchain performance.
"What if we stored admins in a list instead of a mapping? How would the time complexity change?"
Practice
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 AQuick Check:
Access control = restrict actions [OK]
- Confusing access control with transaction speed
- Thinking access control stores data
- Assuming access control creates cryptocurrencies
onlyOwner for access control?Solution
Step 1: Recall modifier syntax in Solidity
A modifier uses the keyword 'modifier', parentheses for parameters, and the '_;' placeholder.Step 2: Check each option
modifier onlyOwner() { require(msg.sender == owner); _; } correctly uses 'modifier onlyOwner() { require(msg.sender == owner); _; }'. Others have syntax errors or missing parts.Final Answer:
modifier onlyOwner() { require(msg.sender == owner); _; } -> Option DQuick Check:
Modifier syntax = modifier onlyOwner() { require(msg.sender == owner); _; } [OK]
- Using 'function' instead of 'modifier'
- Omitting parentheses after modifier name
- Using arrow syntax which is invalid here
mapping(address => bool) public admins;
modifier onlyAdmin() {
require(admins[msg.sender], "Not admin");
_;
}
function addAdmin(address newAdmin) public onlyAdmin {
admins[newAdmin] = true;
}
constructor() {
admins[msg.sender] = true;
}
What happens if a non-admin calls addAdmin?Solution
Step 1: Understand the onlyAdmin modifier
The modifier requires that the caller's address is in the admins mapping as true, else it reverts with "Not admin".Step 2: Analyze calling addAdmin by non-admin
If the caller is not an admin, require fails, causing the transaction to revert with the error message.Final Answer:
The transaction reverts with error 'Not admin' -> Option BQuick Check:
Non-admin call triggers require revert [OK]
- Assuming function silently fails
- Thinking non-admin can add admins
- Believing contract crashes permanently
address public owner;
modifier onlyOwner() {
require(msg.sender = owner, "Not owner");
_;
}
constructor() {
owner = msg.sender;
}
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 CQuick Check:
Use '==' for comparison in require [OK]
- Confusing '=' and '==' in conditions
- Thinking constructor name must differ
- Ignoring parentheses in modifier
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 AQuick Check:
Multi-level roles need nested mappings + modifiers [OK]
- Using single mapping for all roles
- Skipping modifiers for access checks
- Relying only on events without restrictions
Ready to prove it?
Pass the test and this pattern moves unknown
