Bird
Raised Fist0
Blockchain / Solidityprogramming~10 mins

Assertion patterns in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Assertion patterns
Start Assertion
↓
Evaluate Condition
↓
Condition True?
No→Raise Assertion Error
Yes↓
Continue Execution
↓
End
The program checks a condition; if true, it continues, else it stops with an error.
Execution Sample
Blockchain / Solidity
assert balance >= amount, "Insufficient funds"
transfer(amount)
print("Transfer complete")
Checks if balance is enough before transferring money; stops if not.
Execution Table
StepCondition EvaluatedResultActionOutput
1balance >= amountTrueContinue
2transfer(amount)ExecutedTransfer done
3print("Transfer complete")ExecutedOutput messageTransfer complete
4End of program-Program ends normally
💡 Program stops if assertion condition is False, raising an error.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
balance1000100010001000
amount500500500500
Key Moments - 2 Insights
What happens if the assertion condition is false?
The program immediately stops and raises an assertion error, as shown by the exit note and the condition check in step 1.
Does the transfer happen if the assertion fails?
No, the transfer function is not called if the assertion fails, because the program stops before step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the result of the condition at step 1?
ATrue
BFalse
CError
DNot evaluated
💡 Hint
Check the 'Result' column in row for step 1 in the execution table.
At which step does the program print 'Transfer complete'?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Output' column in the execution table for the print statement.
If balance was less than amount, what would happen according to the execution flow?
AProgram continues normally
BAssertion error is raised and program stops
CTransfer happens anyway
DPrint statement runs before error
💡 Hint
Refer to the concept flow and exit note about assertion failure.
Concept Snapshot
Assertion pattern syntax:
assert condition, "error message"

If condition is false, program stops with error.
If true, program continues normally.
Used to check important assumptions in code.
Full Transcript
This visual execution shows how assertion patterns work in blockchain programming. The program starts by checking a condition with assert. If the condition is true, it continues to execute the transfer and prints a success message. If the condition is false, the program stops immediately with an assertion error. Variables like balance and amount remain unchanged during this process. This helps catch errors early by verifying important conditions before proceeding.

Practice

(1/5)
1.

What is the main purpose of an assertion in blockchain smart contract testing?

easy
A. To optimize the contract's gas usage
B. To log information without affecting execution
C. To deploy the contract to the blockchain
D. To check if a condition is true and stop execution if false

Solution

  1. Step 1: Understand assertion role

    Assertions verify conditions during execution to ensure correctness.
  2. Step 2: Identify assertion effect

    If the condition is false, assertions stop execution and revert changes to prevent errors.
  3. Final Answer:

    To check if a condition is true and stop execution if false -> Option D
  4. Quick Check:

    Assertions stop execution on false condition [OK]
Hint: Assertions stop code if condition fails, preventing errors [OK]
Common Mistakes:
  • Thinking assertions only log info without stopping
  • Confusing assertions with deployment steps
  • Assuming assertions optimize gas usage
2.

Which of the following is the correct syntax for an assertion in Solidity?

?
easy
A. assert: balance >= amount;
B. assert(balance >= amount);
C. assert(balance < amount);
D. assert(balance = amount);

Solution

  1. Step 1: Recall Solidity assertion syntax

    Assertions use the function-like syntax: assert(condition);
  2. Step 2: Check condition correctness

    Condition should be a boolean expression, here balance >= amount is valid.
  3. Final Answer:

    assert(balance >= amount); -> Option B
  4. Quick Check:

    assert(condition) uses parentheses and semicolon [OK]
Hint: Assertions use assert(condition); with parentheses and semicolon [OK]
Common Mistakes:
  • Using colon instead of parentheses
  • Using assignment (=) instead of comparison (>=)
  • Writing incorrect comparison operators
3.

Consider this Solidity code snippet:

uint balance = 100;
uint amount = 150;
assert(balance >= amount);

What will happen when this code runs?

medium
A. Execution continues normally
B. balance is updated to amount
C. Execution stops and reverts due to failed assertion
D. Compilation error due to invalid assertion

Solution

  1. Step 1: Evaluate assertion condition

    balance is 100, amount is 150, so balance >= amount is false.
  2. Step 2: Understand assertion behavior on false

    Assertion fails, so execution stops and reverts all changes.
  3. Final Answer:

    Execution stops and reverts due to failed assertion -> Option C
  4. Quick Check:

    False assertion stops execution [OK]
Hint: False assertion stops execution and reverts changes [OK]
Common Mistakes:
  • Assuming code continues after failed assertion
  • Thinking assertion causes compile error
  • Believing balance changes automatically
4.

Identify the error in this Solidity assertion statement:

assert(balance = amount);
medium
A. Using assignment operator instead of comparison
B. Missing semicolon at the end
C. Incorrect function name, should be require()
D. balance and amount are undefined

Solution

  1. Step 1: Analyze operator usage

    The single equals sign (=) assigns value, not compares values.
  2. Step 2: Correct operator for assertion

    Assertions require comparison operators like == or >= to check conditions.
  3. Final Answer:

    Using assignment operator instead of comparison -> Option A
  4. Quick Check:

    Use '==' for comparison in assertions [OK]
Hint: Use '==' for comparison, not '=' assignment in assertions [OK]
Common Mistakes:
  • Confusing '=' with '==' in conditions
  • Thinking require() must replace assert() always
  • Ignoring missing semicolon (which is present here)
5.

You want to assert that a user's balance is positive and provide a clear error message if not. Which assertion pattern is best?

?
hard
A. require(balance > 0, "Balance must be positive");
B. assert(balance > 0, "Balance must be positive");
C. assert(balance > 0);
D. if(balance <= 0) revert("Balance must be positive");

Solution

  1. Step 1: Understand assertion vs require

    In Solidity, assert is for internal errors without messages; require allows error messages.
  2. Step 2: Choose assertion with message

    require(condition, message) is best to check user input and show clear error.
  3. Final Answer:

    require(balance > 0, "Balance must be positive"); -> Option A
  4. Quick Check:

    Use require() for checks with error messages [OK]
Hint: Use require() to assert with clear error messages [OK]
Common Mistakes:
  • Using assert() with message (not supported)
  • Ignoring error messages in assertions
  • Using if + revert instead of require() unnecessarily
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