Bird
Raised Fist0
Blockchain / Solidityprogramming~10 mins

Require, assert, and revert in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Require, assert, and revert
Start Transaction
↓
Check require(condition)
No→Revert with message
Yes↓
Execute code
↓
Check assert(condition)
No→Revert without message
Yes↓
Continue execution
↓
End Transaction
The transaction starts, checks require conditions first, reverts with a message if false, then executes code, checks assert conditions, reverts without message if false, else continues and ends.
Execution Sample
Blockchain / Solidity
function test(uint x) public {
  require(x > 0, "x must be positive");
  assert(x != 42);
  // code continues
}
This function checks if x is positive using require, then asserts x is not 42, reverting if any check fails.
Execution Table
StepCheckConditionResultAction
1requirex > 0TrueContinue execution
2assertx != 42TrueContinue execution
3End--Function completes successfully
💡 Execution stops if require or assert condition is false, reverting the transaction.
Variable Tracker
VariableStartAfter requireAfter assertFinal
xinput valueunchangedunchangedunchanged
Key Moments - 3 Insights
Why does require include an error message but assert does not?
Require is used for input validation and user errors, so it provides a message (see step 1 in execution_table). Assert is for internal errors and reverts without a message (see step 2).
What happens if require condition is false?
The transaction immediately reverts with the provided message, stopping all execution (see step 1 in execution_table where condition is false).
Can assert fail due to user input?
No, assert is meant to check for internal errors and invariants, not user input. It should never fail if the code is correct (see step 2).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what happens if x = 0 at step 1?
AAssert condition is false, transaction reverts without message
BRequire condition is false, transaction reverts with message
CFunction completes successfully
DRequire condition is true, continue execution
💡 Hint
Check step 1 in execution_table where require checks x > 0
At which step does the transaction revert without an error message?
AStep 1 require failure
BStep 3 end
CStep 2 assert failure
DNo revert happens
💡 Hint
See step 2 in execution_table where assert condition is checked
If x = 42, what does the variable_tracker show after assert?
Ax unchanged, but transaction reverts at assert
Bx changes to 0
Cx changes to 42
Dx is removed
💡 Hint
Variable x does not change during checks, see variable_tracker
Concept Snapshot
Require checks user inputs and reverts with message if false.
Assert checks internal errors and reverts without message.
Revert stops execution and undoes changes.
Use require for validation, assert for invariants.
Failed require shows error; failed assert does not.
Execution stops immediately on failure.
Full Transcript
This concept explains how require, assert, and revert work in blockchain smart contracts. The transaction starts and first checks the require condition. If require fails, it reverts with a message and stops. If require passes, the code runs and then assert checks internal conditions. If assert fails, it reverts without a message. If both pass, the function completes successfully. Variables like input x remain unchanged during these checks. Require is for user input validation with error messages, assert is for internal errors without messages. Revert undoes all changes and stops execution immediately.

Practice

(1/5)
1. Which statement best describes the purpose of require in Solidity smart contracts?
easy
A. It checks user inputs or conditions and stops execution with an error message if the condition is false.
B. It is used to check for internal errors that should never happen in the code.
C. It immediately stops execution without any error message.
D. It logs information but does not stop the contract execution.

Solution

  1. Step 1: Understand the role of require

    require is used to validate inputs or conditions before continuing execution.
  2. Step 2: Compare with other statements

    assert checks internal errors, and revert stops execution with a message, but require is specifically for input validation.
  3. Final Answer:

    It checks user inputs or conditions and stops execution with an error message if the condition is false. -> Option A
  4. Quick Check:

    require = input check with error [OK]
Hint: Require is for input checks and stops if false [OK]
Common Mistakes:
  • Confusing require with assert for internal errors
  • Thinking require does not stop execution
  • Believing require logs info without stopping
2. Which of the following is the correct syntax to use assert in Solidity?
easy
A. assert(condition);
B. assert(condition, "Error message");
C. assert {condition};
D. assert condition;

Solution

  1. Step 1: Recall assert syntax

    In Solidity, assert takes only a condition without an error message.
  2. Step 2: Check each option

    assert(condition, "Error message"); wrongly includes an error message, C and D have invalid syntax.
  3. Final Answer:

    assert(condition); -> Option A
  4. Quick Check:

    assert syntax = assert(condition); [OK]
Hint: Assert takes only a condition, no message [OK]
Common Mistakes:
  • Adding error messages to assert
  • Using braces or missing parentheses
  • Writing assert as a statement without parentheses
3. What will happen when the following Solidity code runs?
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 = 15
medium
A. Execution stops at require with error 'x must be greater than 10'.
B. Execution stops at revert with error 'x cannot be 20'.
C. Execution stops at assert because x equals 15.
D. Function completes successfully without errors.

Solution

  1. Step 1: Check require condition

    Input x=15 is greater than 10, so require passes.
  2. Step 2: Check assert condition

    assert(x != 15) fails because x is 15, so execution stops here.
  3. Final Answer:

    Execution stops at assert because x equals 15. -> Option C
  4. Quick Check:

    assert fails if condition false [OK]
Hint: Assert fails if condition is false, stopping execution [OK]
Common Mistakes:
  • Thinking require fails for x=15
  • Assuming revert triggers for x=15
  • Believing function runs fully without errors
4. Identify the error in this Solidity function:
function check(uint value) public {
    require(value > 0, "Value must be positive");
    assert(value < 100, "Value too large");
}
medium
A. The require statement is missing a message string.
B. There is no error; the code is correct.
C. The function should use revert instead of require.
D. The assert statement incorrectly includes an error message.

Solution

  1. Step 1: Review require usage

    Require correctly uses a condition and an error message.
  2. Step 2: Review assert usage

    Assert does not accept an error message; including one causes a syntax error.
  3. Final Answer:

    The assert statement incorrectly includes an error message. -> Option D
  4. Quick Check:

    Assert has no message parameter [OK]
Hint: Assert never takes error messages, only conditions [OK]
Common Mistakes:
  • Adding messages to assert
  • Confusing require and revert usage
  • Assuming no error in assert syntax
5. You want to write a Solidity function that checks if a user has enough balance to withdraw an amount. If the balance is insufficient, it should stop with a clear error message. If an unexpected internal error occurs, it should stop immediately without a message. Which combination of require, assert, and revert is best to use?
hard
A. Use assert for balance check and require for internal errors.
B. Use require to check balance and assert for internal errors; no need for revert.
C. Use revert to check balance with message and assert for internal errors.
D. Use only revert for both balance check and internal errors.

Solution

  1. Step 1: Identify correct use of require

    Require is ideal for checking user inputs like balance with error messages.
  2. Step 2: Identify correct use of assert

    Assert is for internal errors that should never happen, stopping immediately without messages.
  3. Step 3: Consider revert usage

    Revert can be used for complex checks but is not necessary here if require and assert cover cases.
  4. Final Answer:

    Use require to check balance and assert for internal errors; no need for revert. -> Option B
  5. Quick Check:

    Require for inputs, assert for internal errors [OK]
Hint: Require for inputs, assert for internal errors, revert optional [OK]
Common Mistakes:
  • Using assert for input validation
  • Using revert unnecessarily for simple checks
  • Mixing assert and require roles
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard