ReentrancyMock.sol 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.0;
  3. import "../security/ReentrancyGuard.sol";
  4. import "./ReentrancyAttack.sol";
  5. contract ReentrancyMock is ReentrancyGuard {
  6. uint256 public counter;
  7. constructor() {
  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. (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. }