Re-entrancy attack
One of the earliest bugs to be discovered in the Ethereum ecosystem is called the re-entrancy bug. This involves functions that can be called repeatedly before the first call to the function is finished. This allows the caller to withdraw all of the contract funds.
In this recipe, you will learn to avoid such issues in your smart contract.
Getting ready
It's expected that you have a basic understanding of the Ethereum blockchain and solidity before stepping through this recipe.
The Remix IDE (https://remix.ethereum.org) can help you quickly test and deploy the contract. Also, you can use any Ethereum client (geth
, parity
, and so on) and the solc
compiler to run this contract.
How to do it...
- Consider the following contract. This is a simple contract that allows the user to deposit and withdraw the amount:
pragma solidity^0.4.24; contract Victim { // Mapping to keep tract of user deposits mapping (address => uint) private balances; // Function to withdraw the...