0
0
Blockchain / Solidityprogramming~5 mins

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

Choose your learning style9 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.