0
0
Blockchain / Solidityprogramming~10 mins

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

Choose your learning style9 modes available
Concept Flow - Require, assert, and revert
Start Transaction
Check require(condition)
NoRevert with message
Yes
Execute code
Check assert(condition)
NoRevert 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.