ReentrancyMockUpgradeable.sol 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.0;
  3. import "../security/ReentrancyGuardUpgradeable.sol";
  4. import "./ReentrancyAttackUpgradeable.sol";
  5. import "../proxy/utils/Initializable.sol";
  6. contract ReentrancyMockUpgradeable is Initializable, ReentrancyGuardUpgradeable {
  7. uint256 public counter;
  8. function __ReentrancyMock_init() internal onlyInitializing {
  9. __ReentrancyGuard_init_unchained();
  10. __ReentrancyMock_init_unchained();
  11. }
  12. function __ReentrancyMock_init_unchained() internal onlyInitializing {
  13. counter = 0;
  14. }
  15. function callback() external nonReentrant {
  16. _count();
  17. }
  18. function countLocalRecursive(uint256 n) public nonReentrant {
  19. if (n > 0) {
  20. _count();
  21. countLocalRecursive(n - 1);
  22. }
  23. }
  24. function countThisRecursive(uint256 n) public nonReentrant {
  25. if (n > 0) {
  26. _count();
  27. (bool success, ) = address(this).call(abi.encodeWithSignature("countThisRecursive(uint256)", n - 1));
  28. require(success, "ReentrancyMock: failed call");
  29. }
  30. }
  31. function countAndCall(ReentrancyAttackUpgradeable attacker) public nonReentrant {
  32. _count();
  33. bytes4 func = bytes4(keccak256("callback()"));
  34. attacker.callSender(func);
  35. }
  36. function _count() private {
  37. counter += 1;
  38. }
  39. /**
  40. * This empty reserved space is put in place to allow future versions to add new
  41. * variables without shifting down storage in the inheritance chain.
  42. * See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
  43. */
  44. uint256[49] private __gap;
  45. }