ReentrancyMock.sol 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.6.0;
  3. import "../utils/ReentrancyGuard.sol";
  4. import "./ReentrancyAttack.sol";
  5. contract ReentrancyMock is ReentrancyGuard {
  6. uint256 public counter;
  7. constructor () public {
  8. counter = 0;
  9. }
  10. function callback() external nonReentrant {
  11. _count();
  12. }
  13. function countLocalRecursive(uint256 n) public nonReentrant {
  14. if (n > 0) {
  15. _count();
  16. countLocalRecursive(n - 1);
  17. }
  18. }
  19. function countThisRecursive(uint256 n) public nonReentrant {
  20. if (n > 0) {
  21. _count();
  22. // solhint-disable-next-line avoid-low-level-calls
  23. (bool success,) = address(this).call(abi.encodeWithSignature("countThisRecursive(uint256)", n - 1));
  24. require(success, "ReentrancyMock: failed call");
  25. }
  26. }
  27. function countAndCall(ReentrancyAttack attacker) public nonReentrant {
  28. _count();
  29. bytes4 func = bytes4(keccak256("callback()"));
  30. attacker.callSender(func);
  31. }
  32. function _count() private {
  33. counter += 1;
  34. }
  35. }