0
0
Blockchain / Solidityprogramming~5 mins

Reentrancy attacks in Blockchain / Solidity - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a reentrancy attack in blockchain smart contracts?
A reentrancy attack happens when a smart contract calls another contract that calls back into the original contract before the first call finishes, allowing repeated withdrawals or actions that can drain funds or cause unexpected behavior.
Click to reveal answer
beginner
Why are reentrancy attacks dangerous?
They let attackers repeatedly withdraw funds or manipulate contract state before the contract updates its balance or state, leading to loss of money or corrupted data.
Click to reveal answer
intermediate
What is the common pattern to prevent reentrancy attacks?
Use the 'checks-effects-interactions' pattern: first check conditions, then update state, and only after that interact with other contracts or send funds.
Click to reveal answer
intermediate
How does the 'ReentrancyGuard' contract help prevent attacks?
It uses a lock mechanism to prevent a contract function from being called again before the first call finishes, blocking reentrant calls.
Click to reveal answer
advanced
Give a simple example of a vulnerable Solidity function to reentrancy.
A function that sends Ether to a user before updating their balance, like: <pre>function withdraw(uint amount) public { require(balances[msg.sender] >= amount); (bool success, ) = msg.sender.call{value: amount}(""); require(success); balances[msg.sender] -= amount; }</pre> This allows reentrancy because balance is updated after sending Ether.
Click to reveal answer
What is the main cause of a reentrancy attack?
ASending Ether without a fallback function
BUsing too much gas in a transaction
CNot verifying user input
DCalling an external contract before updating state
Which pattern helps prevent reentrancy attacks?
AEvent logging
BChecks-Effects-Interactions
CLazy loading
DGas optimization
What does the ReentrancyGuard contract do?
APrevents multiple calls to a function at the same time
BEncrypts contract data
CLimits gas usage
DAutomatically updates balances
In Solidity, what is a risky way to send Ether that can cause reentrancy?
AUsing call.value() without updating state first
BUsing transfer() after updating state
CUsing send() with a gas limit
DUsing payable fallback functions
Which of these is NOT a way to prevent reentrancy?
AUpdate state before external calls
BUse mutex locks like ReentrancyGuard
CAllow multiple reentrant calls
DAvoid external calls entirely
Explain what a reentrancy attack is and why it can cause loss of funds in smart contracts.
Think about a contract calling another contract that calls back before state updates.
You got /3 concepts.
    Describe the 'checks-effects-interactions' pattern and how it helps prevent reentrancy attacks.
    Focus on the order of operations in a function.
    You got /4 concepts.