ReentrancyMock.sol 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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.encodeCall(this.countThisRecursive, (n - 1)));
  23. require(success, "ReentrancyMock: failed call");
  24. }
  25. }
  26. function countAndCall(ReentrancyAttack attacker) public nonReentrant {
  27. _count();
  28. attacker.callSender(abi.encodeCall(this.callback, ()));
  29. }
  30. function _count() private {
  31. counter += 1;
  32. }
  33. function guardedCheckEntered() public nonReentrant {
  34. require(_reentrancyGuardEntered());
  35. }
  36. function unguardedCheckNotEntered() public view {
  37. require(!_reentrancyGuardEntered());
  38. }
  39. }