CrossChainEnabledPolygonChild.sol 2.4 KB

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