Function modifiers in Blockchain / Solidity - Time & Space Complexity
When using function modifiers in blockchain code, it's important to know how they affect the speed of your program.
We want to find out how the time needed changes as the input or calls grow.
Analyze the time complexity of the following code snippet.
modifier onlyOwner() {
require(msg.sender == owner, "Not owner");
_;
}
function updateValue(uint newValue) public onlyOwner {
value = newValue;
}
This code uses a modifier to check if the caller is the owner before running the function.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The modifier runs a simple check once per function call.
- How many times: Exactly once each time the function is called.
The modifier check happens once per call, no matter the input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 check |
| 100 | 1 check |
| 1000 | 1 check |
Pattern observation: The number of operations per call is constant (1 check), independent of the input size.
Time Complexity: O(1)
This means the time complexity per function call is constant, since the modifier performs a fixed check once.
[X] Wrong: "Modifiers add extra loops or slow down the function a lot as input grows."
[OK] Correct: Modifiers usually run simple checks once per call, so they add a small fixed cost, not extra loops or big slowdowns.
Understanding how modifiers affect time helps you write clear and efficient blockchain code, a skill that shows you know how to balance security and performance.
"What if the modifier included a loop over an array? How would the time complexity change?"
Practice
Solution
Step 1: Understand function modifiers
Function modifiers are used to add extra rules or steps before or after a function runs.Step 2: Compare options
Only To add extra rules or checks around a function correctly describes adding extra rules or checks around a function.Final Answer:
To add extra rules or checks around a function -> Option BQuick Check:
Function modifiers add rules [OK]
- Thinking modifiers rename or delete functions
- Confusing modifiers with return type changes
- Believing modifiers execute functions themselves
Solution
Step 1: Recall Solidity modifier syntax
Modifiers use the keyword 'modifier', name, parentheses, and curly braces with '_;' inside.Step 2: Identify correct syntax
modifier onlyOwner() { require(msg.sender == owner); _; } matches the correct Solidity syntax with braces and '_;'. Others use invalid symbols like '=>', '->', or ':'.Final Answer:
modifier onlyOwner() { require(msg.sender == owner); _; } -> Option DQuick Check:
Modifier syntax uses braces and '_;' [OK]
- Using arrows (=>, ->) instead of braces
- Omitting the '_;' statement
- Using colon ':' instead of braces
contract Test {
address owner;
modifier onlyOwner() {
require(msg.sender == owner, "Not owner");
_;
}
function setOwner(address _owner) public {
owner = _owner;
}
function secureAction() public onlyOwner {
// action code
}
}
If msg.sender is not the owner and calls secureAction(), what happens?Solution
Step 1: Understand the modifier behavior
The modifier checks if msg.sender equals owner; if not, it calls require which reverts with "Not owner".Step 2: Analyze the call with wrong sender
If msg.sender is not owner, require fails and the transaction reverts with the error message.Final Answer:
The transaction reverts with error 'Not owner' -> Option CQuick Check:
require fails if sender not owner [OK]
- Assuming function runs even if require fails
- Thinking owner changes on secureAction call
- Ignoring the require statement effect
contract Sample {
address owner;
modifier onlyOwner() {
require(msg.sender == owner);
_
}
function changeOwner(address newOwner) public onlyOwner {
owner = newOwner;
}
}Solution
Step 1: Check modifier syntax
The modifier has '_', but it misses the semicolon '_;' which is required to mark function execution point.Step 2: Verify other parts
Function visibility 'public' is correct, owner can be uninitialized but not a syntax error, and modifier names are case-sensitive but not required to be capitalized.Final Answer:
Missing semicolon after '_' in modifier -> Option AQuick Check:
Modifier must have '_;' not just '_' [OK]
- Forgetting semicolon after '_' in modifier
- Thinking function visibility causes error here
- Assuming variable initialization causes syntax error
paused. Which modifier code correctly implements this logic?Solution
Step 1: Understand the paused logic
The function should run only if paused is false, so require(!paused) ensures this.Step 2: Check modifier completeness
modifier whenNotPaused() { require(!paused, "Paused"); _; } uses require with negation and includes '_;' to run the function. modifier whenNotPaused() { if(paused) { revert("Paused"); } } lacks '_;', so function never runs. modifier whenNotPaused() { require(paused, "Paused"); _; } requires paused to be true, which is opposite. modifier whenNotPaused() { if(!paused) { _; } } runs function only if not paused but lacks else handling and is less clear.Final Answer:
modifier whenNotPaused() { require(!paused, "Paused"); _; } -> Option AQuick Check:
Require !paused with '_;' runs function [OK]
- Reversing paused condition in require
- Omitting '_;' causing function not to run
- Using if without else or revert
Ready to prove it?
Pass the test and this pattern moves unknown
