Bird
Raised Fist0
Blockchain / Solidityprogramming~20 mins

Reentrancy guard pattern in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Reentrancy Guard Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Solidity contract call?

Consider this simplified Solidity contract using a reentrancy guard. What will be the value of counter after calling increment() once?

Blockchain / Solidity
pragma solidity ^0.8.0;

contract ReentrancyGuard {
    bool private locked;
    uint public counter;

    modifier noReentrant() {
        require(!locked, "No reentrancy");
        locked = true;
        _;
        locked = false;
    }

    function increment() public noReentrant {
        counter += 1;
        if(counter < 3) {
            increment();
        }
    }
}
AThe call will revert with "No reentrancy" error
Bcounter will be 3
Ccounter will be 2
Dcounter will be 1
Attempts:
2 left
💡 Hint

Think about what happens when the function calls itself recursively with the reentrancy guard active.

🧠 Conceptual
intermediate
1:30remaining
Why is a reentrancy guard important in smart contracts?

Which of the following best explains why a reentrancy guard is used in smart contracts?

ATo increase the speed of contract execution by caching results
BTo allow the contract to call external contracts safely
CTo stop a function from being called again before the first call finishes, avoiding unexpected state changes
DTo prevent multiple users from calling the contract at the same time
Attempts:
2 left
💡 Hint

Think about what happens if a function changes state and then calls an external contract that calls back before the first call finishes.

🔧 Debug
advanced
2:00remaining
Identify the bug in this reentrancy guard implementation

What is wrong with this reentrancy guard modifier?

Blockchain / Solidity
modifier noReentrant() {
    require(!locked, "No reentrancy");
    _;
    locked = true;
}
AThe modifier does not check the <code>locked</code> variable at all
BThe <code>locked</code> variable is never set to false, so the guard locks forever
CThe <code>require</code> condition is reversed, allowing reentrancy
DThe modifier sets <code>locked</code> before the function body, which is incorrect
Attempts:
2 left
💡 Hint

Look at when locked is set to true and if it ever resets.

📝 Syntax
advanced
1:30remaining
Which option correctly implements a reentrancy guard modifier in Solidity?

Choose the correct Solidity code for a reentrancy guard modifier that prevents reentrant calls.

A
modifier noReentrant() {
    require(!locked, "No reentrancy");
    locked = true;
    _;
    locked = false;
}
B
modifier noReentrant() {
    locked = true;
    require(!locked, "No reentrancy");
    _;
    locked = false;
}
C
modifier noReentrant() {
    require(locked, "No reentrancy");
    locked = true;
    _;
    locked = false;
}
D
modifier noReentrant() {
    require(!locked, "No reentrancy");
    _;
    locked = true;
    locked = false;
}
Attempts:
2 left
💡 Hint

The guard must check locked before setting it to true and running the function body.

🚀 Application
expert
2:30remaining
How many times will the fallback function be called in this contract during a reentrant attack?

Given this contract snippet, how many times will the fallback function be called if an attacker tries to reenter during withdraw()?

Blockchain / Solidity
pragma solidity ^0.8.0;

contract Vulnerable {
    mapping(address => uint) public balances;
    bool private locked;

    modifier noReentrant() {
        require(!locked, "No reentrancy");
        locked = true;
        _;
        locked = false;
    }

    function deposit() public payable {
        balances[msg.sender] += msg.value;
    }

    function withdraw(uint amount) public noReentrant {
        require(balances[msg.sender] >= amount, "Insufficient balance");
        (bool success, ) = msg.sender.call{value: amount}("");
        require(success, "Transfer failed");
        balances[msg.sender] -= amount;
    }

    fallback() external payable {
        if (balances[msg.sender] > 0) {
            withdraw(balances[msg.sender]);
        }
    }
}
AThe fallback function will be called multiple times until the balance is zero
BThe fallback function will cause an infinite loop and run forever
CThe fallback function will never be called due to the reentrancy guard
DThe fallback function will be called exactly once
Attempts:
2 left
💡 Hint

Consider how the noReentrant modifier affects the recursive call inside fallback().

Practice

(1/5)
1.

What is the main purpose of the reentrancy guard pattern in blockchain smart contracts?

easy
A. To prevent a function from being called again before it finishes execution
B. To speed up the execution of smart contracts
C. To allow multiple users to access a function simultaneously
D. To reduce the gas cost of transactions

Solution

  1. Step 1: Understand the problem reentrancy causes

    Reentrancy happens when a function is called again before the first call finishes, causing unexpected behavior or exploits.
  2. Step 2: Identify the role of the reentrancy guard

    The reentrancy guard uses a lock to block repeated calls until the first call completes, preventing reentrancy attacks.
  3. Final Answer:

    To prevent a function from being called again before it finishes execution -> Option A
  4. Quick Check:

    Reentrancy guard = prevent repeated calls [OK]
Hint: Reentrancy guard stops repeated calls during execution [OK]
Common Mistakes:
  • Thinking it speeds up contract execution
  • Believing it allows simultaneous access
  • Confusing it with gas optimization
2.

Which of the following is the correct way to declare a reentrancy guard variable in Solidity?

contract Example {
    // Fill in the blank
    _____ private locked = false;
}
easy
A. string
B. bool
C. int
D. address

Solution

  1. Step 1: Identify the variable type for a lock

    The lock variable is used as a flag to indicate if the function is running, so it should be a boolean (true/false).
  2. Step 2: Match the correct Solidity type

    In Solidity, bool is the type for true/false values, suitable for a lock variable.
  3. Final Answer:

    bool -> Option B
  4. Quick Check:

    Lock variable type = bool [OK]
Hint: Lock variables are boolean flags in Solidity [OK]
Common Mistakes:
  • Using int instead of bool for lock
  • Using string or address types incorrectly
  • Forgetting to initialize the variable
3.

Consider this Solidity function using a reentrancy guard:

bool private locked = false;

function withdraw(uint amount) public {
    require(!locked, "Reentrant call detected");
    locked = true;
    (bool success, ) = msg.sender.call{value: amount}("");
    require(success, "Transfer failed");
    locked = false;
}

What happens if msg.sender.call triggers a fallback that calls withdraw again?

medium
A. The second call is blocked by the lock and reverts
B. The second call succeeds and causes reentrancy
C. The contract runs out of gas and fails
D. The lock variable resets before the second call

Solution

  1. Step 1: Analyze the lock status during the first call

    When withdraw is called, locked is set to true before sending funds.
  2. Step 2: Understand the effect on reentrant call

    If msg.sender.call triggers a fallback that calls withdraw again, the require(!locked) check fails because locked is true, blocking the call.
  3. Final Answer:

    The second call is blocked by the lock and reverts -> Option A
  4. Quick Check:

    Lock true blocks reentrant call [OK]
Hint: Lock true means no reentrant calls allowed [OK]
Common Mistakes:
  • Assuming reentrancy still happens
  • Thinking lock resets before second call
  • Confusing gas failure with reentrancy
4.

Identify the bug in this reentrancy guard implementation:

bool private locked = false;

function withdraw(uint amount) public {
    require(!locked, "Reentrant call detected");
    (bool success, ) = msg.sender.call{value: amount}("");
    require(success, "Transfer failed");
    locked = true;
    locked = false;
}
medium
A. The require statement is missing
B. The lock variable is never set to true
C. The lock is set after the external call, allowing reentrancy
D. The call to msg.sender is incorrect syntax

Solution

  1. Step 1: Check when the lock is set

    The lock is set to true only after the external call, which means the function is not protected during the call.
  2. Step 2: Understand reentrancy risk

    Because the lock is set too late, a reentrant call can happen during the external call, causing vulnerability.
  3. Final Answer:

    The lock is set after the external call, allowing reentrancy -> Option C
  4. Quick Check:

    Lock must be set before external calls [OK]
Hint: Set lock before external calls to prevent reentrancy [OK]
Common Mistakes:
  • Setting lock after external call
  • Forgetting to set lock at all
  • Misplacing require statements
5.

You want to protect a function that updates user balances and sends Ether. Which is the best way to apply the reentrancy guard pattern?

function safeWithdraw(uint amount) public {
    // Your code here
    balances[msg.sender] -= amount;
    (bool success, ) = msg.sender.call{value: amount}("");
    require(success, "Transfer failed");
}
hard
A. Update balances after sending Ether without any lock
B. Do not use any lock, just rely on require statements
C. Use a lock variable but set it after sending Ether
D. Set a lock variable before updating balances and sending Ether, then release it after

Solution

  1. Step 1: Understand the order of operations for safety

    To prevent reentrancy, set a lock before any state changes and external calls, then release it after.
  2. Step 2: Apply the reentrancy guard pattern correctly

    Set the lock variable to true before updating balances and sending Ether, then set it back to false after the transfer completes.
  3. Final Answer:

    Set a lock variable before updating balances and sending Ether, then release it after -> Option D
  4. Quick Check:

    Lock before state change and external call [OK]
Hint: Lock before balance update and transfer, unlock after [OK]
Common Mistakes:
  • Setting lock after sending Ether
  • Updating balances after external call
  • Not using any lock at all
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