ReentrancyMock.sol 905 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. pragma solidity ^0.4.18;
  2. import "../ReentrancyGuard.sol";
  3. import "./ReentrancyAttack.sol";
  4. contract ReentrancyMock is ReentrancyGuard {
  5. uint256 public counter;
  6. function ReentrancyMock() public {
  7. counter = 0;
  8. }
  9. function count() private {
  10. counter += 1;
  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. bytes4 func = bytes4(keccak256("countThisRecursive(uint256)"));
  20. if(n > 0) {
  21. count();
  22. bool result = this.call(func, 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 callback() external nonReentrant {
  32. count();
  33. }
  34. }