ReentrancyGuardTransient.sol 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.3.0) (utils/ReentrancyGuardTransient.sol)
  3. pragma solidity ^0.8.24;
  4. import {TransientSlot} from "./TransientSlot.sol";
  5. /**
  6. * @dev Variant of {ReentrancyGuard} that uses transient storage.
  7. *
  8. * NOTE: This variant only works on networks where EIP-1153 is available.
  9. *
  10. * _Available since v5.1._
  11. */
  12. abstract contract ReentrancyGuardTransient {
  13. using TransientSlot for *;
  14. // keccak256(abi.encode(uint256(keccak256("openzeppelin.storage.ReentrancyGuard")) - 1)) & ~bytes32(uint256(0xff))
  15. bytes32 private constant REENTRANCY_GUARD_STORAGE =
  16. 0x9b779b17422d0df92223018b32b4d1fa46e071723d6817e2486d003becc55f00;
  17. /**
  18. * @dev Unauthorized reentrant call.
  19. */
  20. error ReentrancyGuardReentrantCall();
  21. /**
  22. * @dev Prevents a contract from calling itself, directly or indirectly.
  23. * Calling a `nonReentrant` function from another `nonReentrant`
  24. * function is not supported. It is possible to prevent this from happening
  25. * by making the `nonReentrant` function external, and making it call a
  26. * `private` function that does the actual work.
  27. */
  28. modifier nonReentrant() {
  29. _nonReentrantBefore();
  30. _;
  31. _nonReentrantAfter();
  32. }
  33. /**
  34. * @dev A `view` only version of {nonReentrant}. Use to block view functions
  35. * from being called, preventing reading from inconsistent contract state.
  36. *
  37. * CAUTION: This is a "view" modifier and does not change the reentrancy
  38. * status. Use it only on view functions. For payable or non-payable functions,
  39. * use the standard {nonReentrant} modifier instead.
  40. */
  41. modifier nonReentrantView() {
  42. _nonReentrantBeforeView();
  43. _;
  44. }
  45. function _nonReentrantBeforeView() private view {
  46. if (_reentrancyGuardEntered()) {
  47. revert ReentrancyGuardReentrantCall();
  48. }
  49. }
  50. function _nonReentrantBefore() private {
  51. // On the first call to nonReentrant, REENTRANCY_GUARD_STORAGE.asBoolean().tload() will be false
  52. _nonReentrantBeforeView();
  53. // Any calls to nonReentrant after this point will fail
  54. REENTRANCY_GUARD_STORAGE.asBoolean().tstore(true);
  55. }
  56. function _nonReentrantAfter() private {
  57. REENTRANCY_GUARD_STORAGE.asBoolean().tstore(false);
  58. }
  59. /**
  60. * @dev Returns true if the reentrancy guard is currently set to "entered", which indicates there is a
  61. * `nonReentrant` function in the call stack.
  62. */
  63. function _reentrancyGuardEntered() internal view returns (bool) {
  64. return REENTRANCY_GUARD_STORAGE.asBoolean().tload();
  65. }
  66. }