Require, assert, and revert in Blockchain / Solidity - Time & Space Complexity
When using require, assert, and revert in blockchain code, it's important to know how they affect the program's speed as input grows.
We want to see how these checks impact the number of steps the program takes.
Analyze the time complexity of the following code snippet.
function transfer(address to, uint amount) public {
require(balance[msg.sender] >= amount, "Insufficient balance");
balance[msg.sender] -= amount;
balance[to] += amount;
assert(balance[msg.sender] >= 0);
if(amount > 1000) {
revert("Amount too large");
}
}
This code checks conditions before and after transferring tokens, stopping execution if something is wrong.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: There are no loops or repeated traversals here; the checks happen once per function call.
- How many times: Each require, assert, and revert runs only once each time the function is called.
Since the checks run once per call, the number of steps stays about the same no matter the input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 5 checks and updates |
| 100 | 5 checks and updates |
| 1000 | 5 checks and updates |
Pattern observation: The work does not increase with input size; it stays constant.
Time Complexity: O(1)
This means the checks take the same amount of time no matter how big the input is.
[X] Wrong: "Require, assert, and revert slow down the program more as input grows."
[OK] Correct: These checks run once per call and do not loop over input, so their cost stays the same regardless of input size.
Understanding how these checks affect performance helps you write safe and efficient blockchain code, a skill valued in real projects.
"What if the require statement was inside a loop over an array of inputs? How would the time complexity change?"
Practice
require in Solidity smart contracts?Solution
Step 1: Understand the role of
requirerequireis used to validate inputs or conditions before continuing execution.Step 2: Compare with other statements
assertchecks internal errors, andrevertstops execution with a message, butrequireis specifically for input validation.Final Answer:
It checks user inputs or conditions and stops execution with an error message if the condition is false. -> Option AQuick Check:
require = input check with error [OK]
- Confusing require with assert for internal errors
- Thinking require does not stop execution
- Believing require logs info without stopping
assert in Solidity?Solution
Step 1: Recall assert syntax
In Solidity,asserttakes only a condition without an error message.Step 2: Check each option
assert(condition, "Error message"); wrongly includes an error message, C and D have invalid syntax.Final Answer:
assert(condition); -> Option AQuick Check:
assert syntax = assert(condition); [OK]
- Adding error messages to assert
- Using braces or missing parentheses
- Writing assert as a statement without parentheses
function test(uint x) public {
require(x > 10, "x must be greater than 10");
assert(x != 15);
if (x == 20) {
revert("x cannot be 20");
}
}Input:
x = 15Solution
Step 1: Check require condition
Input x=15 is greater than 10, so require passes.Step 2: Check assert condition
assert(x != 15) fails because x is 15, so execution stops here.Final Answer:
Execution stops at assert because x equals 15. -> Option CQuick Check:
assert fails if condition false [OK]
- Thinking require fails for x=15
- Assuming revert triggers for x=15
- Believing function runs fully without errors
function check(uint value) public {
require(value > 0, "Value must be positive");
assert(value < 100, "Value too large");
}Solution
Step 1: Review require usage
Require correctly uses a condition and an error message.Step 2: Review assert usage
Assert does not accept an error message; including one causes a syntax error.Final Answer:
The assert statement incorrectly includes an error message. -> Option DQuick Check:
Assert has no message parameter [OK]
- Adding messages to assert
- Confusing require and revert usage
- Assuming no error in assert syntax
require, assert, and revert is best to use?Solution
Step 1: Identify correct use of require
Require is ideal for checking user inputs like balance with error messages.Step 2: Identify correct use of assert
Assert is for internal errors that should never happen, stopping immediately without messages.Step 3: Consider revert usage
Revert can be used for complex checks but is not necessary here if require and assert cover cases.Final Answer:
Use require to check balance and assert for internal errors; no need for revert. -> Option BQuick Check:
Require for inputs, assert for internal errors [OK]
- Using assert for input validation
- Using revert unnecessarily for simple checks
- Mixing assert and require roles
Ready to prove it?
Pass the test and this pattern moves unknown
