ReentrancyMock.sol 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.19;
  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. function guardedCheckEntered() public nonReentrant {
  35. require(_reentrancyGuardEntered());
  36. }
  37. function unguardedCheckNotEntered() public view {
  38. require(!_reentrancyGuardEntered());
  39. }
  40. }