(Mastery Level β Security β’ zk β’ Cross-chain β’ Deployment)
βCode without security is like a vault without a lock.β
Occurs when a contract calls another contract before updating its state β allowing the attacker to re-enter the function multiple times.
// β Vulnerable
function withdraw(uint _amount) public {
require(balances[msg.sender] >= _amount, "Not enough balance");
(bool sent, ) = msg.sender.call{value: _amount}("");
require(sent, "Failed");
balances[msg.sender] -= _amount;
}
// β
Safe
function withdraw(uint _amount) public nonReentrant {
require(balances[msg.sender] >= _amount, "Not enough balance");
balances[msg.sender] -= _amount;
(bool sent, ) = msg.sender.call{value: _amount}("");
require(sent, "Failed");
}
Use ReentrancyGuard from OpenZeppelin:
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
contract SafeVault is ReentrancyGuard { ... }
Before Solidity 0.8, arithmetic overflow/underflow caused bugs.
In 0.8+, checks are automatic β but still use unchecked carefully for optimization.