ReentrancyMock.sol 962 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. pragma solidity ^0.4.24;
  2. import "../utils/ReentrancyGuard.sol";
  3. import "./ReentrancyAttack.sol";
  4. contract ReentrancyMock is ReentrancyGuard {
  5. uint256 public counter;
  6. constructor() public {
  7. counter = 0;
  8. }
  9. function callback() external nonReentrant {
  10. count();
  11. }
  12. function countLocalRecursive(uint256 _n) public nonReentrant {
  13. if (_n > 0) {
  14. count();
  15. countLocalRecursive(_n - 1);
  16. }
  17. }
  18. function countThisRecursive(uint256 _n) public nonReentrant {
  19. if (_n > 0) {
  20. count();
  21. // solium-disable-next-line security/no-low-level-calls
  22. bool result = address(this).call(abi.encodeWithSignature("countThisRecursive(uint256)", _n - 1));
  23. require(result == true);
  24. }
  25. }
  26. function countAndCall(ReentrancyAttack _attacker) public nonReentrant {
  27. count();
  28. bytes4 func = bytes4(keccak256("callback()"));
  29. _attacker.callSender(func);
  30. }
  31. function count() private {
  32. counter += 1;
  33. }
  34. }