CrossChainEnabledPolygonChild.sol 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.7.0) (crosschain/polygon/CrossChainEnabledPolygonChild.sol)
  3. pragma solidity ^0.8.4;
  4. import "../CrossChainEnabled.sol";
  5. import "../../security/ReentrancyGuard.sol";
  6. import "../../utils/Address.sol";
  7. import "../../vendor/polygon/IFxMessageProcessor.sol";
  8. address constant DEFAULT_SENDER = 0x000000000000000000000000000000000000dEaD;
  9. /**
  10. * @dev https://polygon.technology/[Polygon] specialization or the
  11. * {CrossChainEnabled} abstraction the child side (polygon/mumbai).
  12. *
  13. * This version should only be deployed on child chain to process cross-chain
  14. * messages originating from the parent chain.
  15. *
  16. * The fxChild contract is provided and maintained by the polygon team. You can
  17. * find the address of this contract polygon and mumbai in
  18. * https://docs.polygon.technology/docs/develop/l1-l2-communication/fx-portal/#contract-addresses[Polygon's Fx-Portal documentation].
  19. *
  20. * _Available since v4.6._
  21. */
  22. abstract contract CrossChainEnabledPolygonChild is IFxMessageProcessor, CrossChainEnabled, ReentrancyGuard {
  23. /// @custom:oz-upgrades-unsafe-allow state-variable-immutable
  24. address private immutable _fxChild;
  25. address private _sender = DEFAULT_SENDER;
  26. /// @custom:oz-upgrades-unsafe-allow constructor
  27. constructor(address fxChild) {
  28. _fxChild = fxChild;
  29. }
  30. /**
  31. * @dev see {CrossChainEnabled-_isCrossChain}
  32. */
  33. function _isCrossChain() internal view virtual override returns (bool) {
  34. return msg.sender == _fxChild;
  35. }
  36. /**
  37. * @dev see {CrossChainEnabled-_crossChainSender}
  38. */
  39. function _crossChainSender() internal view virtual override onlyCrossChain returns (address) {
  40. return _sender;
  41. }
  42. /**
  43. * @dev External entry point to receive and relay messages originating
  44. * from the fxChild.
  45. *
  46. * Non-reentrancy is crucial to avoid a cross-chain call being able
  47. * to impersonate anyone by just looping through this with user-defined
  48. * arguments.
  49. *
  50. * Note: if _fxChild calls any other function that does a delegate-call,
  51. * then security could be compromised.
  52. */
  53. function processMessageFromRoot(
  54. uint256, /* stateId */
  55. address rootMessageSender,
  56. bytes calldata data
  57. ) external override nonReentrant {
  58. if (!_isCrossChain()) revert NotCrossChainCall();
  59. _sender = rootMessageSender;
  60. Address.functionDelegateCall(address(this), data, "cross-chain execution failed");
  61. _sender = DEFAULT_SENDER;
  62. }
  63. }