ReentrancyMock.sol 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. pragma solidity ^0.6.0;
  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. // solhint-disable-next-line avoid-low-level-calls
  22. (bool success,) = address(this).call(abi.encodeWithSignature("countThisRecursive(uint256)", n - 1));
  23. require(success, "ReentrancyMock: failed call");
  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. }