The reentrancy guard pattern helps stop a contract from being called again before the first call finishes. This protects your contract from attacks that try to trick it into doing things twice.
0
0
Reentrancy guard pattern in Blockchain / Solidity
Introduction
When your contract sends money or tokens to another contract and then updates its own state.
When you want to protect functions that change balances or important data from being called multiple times at once.
When you want to avoid attackers exploiting your contract by calling it repeatedly before it finishes.
When you have functions that should only run one at a time to keep data safe.
When you want to make your smart contract more secure and trustworthy.
Syntax
Blockchain / Solidity
contract Example {
bool private locked;
modifier noReentrancy() {
require(!locked, "No reentrancy allowed");
locked = true;
_;
locked = false;
}
function safeFunction() external noReentrancy {
// function code here
}
}The locked variable tracks if the function is already running.
The noReentrancy modifier blocks repeated calls until the first finishes.
Examples
This modifier stops reentrant calls by using a lock variable.
Blockchain / Solidity
bool private locked; modifier noReentrancy() { require(!locked, "No reentrancy"); locked = true; _; locked = false; }
This withdraw function uses the guard to prevent reentrancy attacks during Ether transfer.
Blockchain / Solidity
function withdraw() external noReentrancy {
uint amount = balances[msg.sender];
require(amount > 0, "No balance");
balances[msg.sender] = 0;
(bool sent, ) = msg.sender.call{value: amount}("");
require(sent, "Failed to send Ether");
}Sample Program
This contract lets users deposit and withdraw Ether safely. The withdraw function uses the reentrancy guard to stop attacks that try to call it again before it finishes.
Blockchain / Solidity
pragma solidity ^0.8.0; contract ReentrancyGuard { bool private locked; modifier noReentrancy() { require(!locked, "No reentrancy allowed"); locked = true; _; locked = false; } mapping(address => uint) public balances; function deposit() external payable { balances[msg.sender] += msg.value; } function withdraw() external noReentrancy { uint amount = balances[msg.sender]; require(amount > 0, "No balance to withdraw"); balances[msg.sender] = 0; (bool sent, ) = msg.sender.call{value: amount}(""); require(sent, "Failed to send Ether"); } }
OutputSuccess
Important Notes
Always update your contract state before sending Ether to prevent attacks.
The reentrancy guard is a simple but powerful way to protect your contract.
Use the guard on all functions that send Ether or call external contracts.
Summary
The reentrancy guard stops a function from running again before it finishes.
Use a boolean lock and a modifier to protect your functions.
This pattern helps keep your smart contract safe from common attacks.