ReentrancyGuard.sol 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. pragma solidity ^0.4.24;
  2. /**
  3. * @title Helps contracts guard against reentrancy attacks.
  4. * @author Remco Bloemen <remco@2π.com>, Eenae <alexey@mixbytes.io>
  5. * @dev If you mark a function `nonReentrant`, you should also
  6. * mark it `external`.
  7. */
  8. contract ReentrancyGuard {
  9. /// @dev Constant for unlocked guard state - non-zero to prevent extra gas costs.
  10. /// See: https://github.com/OpenZeppelin/openzeppelin-solidity/issues/1056
  11. uint private constant REENTRANCY_GUARD_FREE = 1;
  12. /// @dev Constant for locked guard state
  13. uint private constant REENTRANCY_GUARD_LOCKED = 2;
  14. /**
  15. * @dev We use a single lock for the whole contract.
  16. */
  17. uint private reentrancyLock = REENTRANCY_GUARD_FREE;
  18. /**
  19. * @dev Prevents a contract from calling itself, directly or indirectly.
  20. * If you mark a function `nonReentrant`, you should also
  21. * mark it `external`. Calling one `nonReentrant` function from
  22. * another is not supported. Instead, you can implement a
  23. * `private` function doing the actual work, and an `external`
  24. * wrapper marked as `nonReentrant`.
  25. */
  26. modifier nonReentrant() {
  27. require(reentrancyLock == REENTRANCY_GUARD_FREE);
  28. reentrancyLock = REENTRANCY_GUARD_LOCKED;
  29. _;
  30. reentrancyLock = REENTRANCY_GUARD_FREE;
  31. }
  32. }