Bird
Raised Fist0
Blockchain / Solidityprogramming~5 mins

Reentrancy guard pattern in Blockchain / Solidity - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is the Reentrancy Guard Pattern in blockchain smart contracts?
It is a technique used to prevent a function from being called again before the first call finishes, stopping attacks where a contract repeatedly calls itself to drain funds.
Click to reveal answer
beginner
Why is reentrancy a problem in smart contracts?
Because it allows attackers to repeatedly enter a function before the previous execution finishes, potentially causing unexpected behavior like multiple withdrawals.
Click to reveal answer
intermediate
How does a bool locked variable help in implementing a reentrancy guard?
It acts like a lock: when a function starts, it sets locked = true. If the function is called again before finishing, it sees the lock and stops execution.
Click to reveal answer
intermediate
What Solidity keyword is commonly used to create a reentrancy guard modifier?
The modifier keyword is used to create reusable code that can check and set the lock before running the function body.
Click to reveal answer
advanced
Give a simple example of a reentrancy guard modifier in Solidity.
```solidity bool private locked = false; modifier noReentrant() { require(!locked, "No reentrancy"); locked = true; _; locked = false; } ```
Click to reveal answer
What does the reentrancy guard pattern primarily prevent?
AMultiple simultaneous calls to a function before it finishes
BUnauthorized access to private variables
COverflow errors in arithmetic operations
DSlow transaction processing
In Solidity, which of these is commonly used to implement a reentrancy guard?
AA public state variable
BA boolean lock variable and a modifier
CA fallback function
DAn event logger
What happens if a reentrant call is detected in a function protected by a reentrancy guard?
AThe contract self-destructs
BThe call proceeds normally
CThe call is rejected with an error
DThe call is queued for later
Why is it important to set the lock variable back to false after the function finishes?
ATo allow future calls to the function
BTo save gas fees
CTo increase contract balance
DTo trigger an event
Which of these attacks does the reentrancy guard help prevent?
ASpam transactions
BPhishing attacks
CSybil attacks
DRepeated withdrawal attacks
Explain how the reentrancy guard pattern works to protect a smart contract function.
Think about how a door lock stops someone from entering twice at the same time.
You got /4 concepts.
    Describe a simple Solidity code example that uses a reentrancy guard modifier.
    Focus on the lock variable and how it changes before and after the function runs.
    You got /5 concepts.

      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