Blockchain Security: Simple Reentrancy Check
A minimal example of guarding against reentrancy.
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.20;
contract Vault {
mapping(address => uint256) public balance;
bool internal locked;
modifier noReentrancy(){
require(!locked, "locked");
locked = true; _; locked = false;
}
function deposit() external payable { balance[msg.sender] += msg.value; }
function withdraw(uint256 amt) external noReentrancy {
require(balance[msg.sender] >= amt, "low bal");
balance[msg.sender] -= amt;
(bool ok,) = msg.sender.call{value: amt}("");
require(ok, "xfer");
}
}