Skip to content

Commit

Permalink
add polygon hook and ism (#3038)
Browse files Browse the repository at this point in the history
### Description

- Add native polygon bridge hook and isms

### Related issues

- Fixes #2847 
- Documented with hyperlane-xyz/v3-docs#31

### Backward compatibility

Yes

### Testing

Unit Tests

---------

Co-authored-by: NOOMA-42 <[email protected]>
  • Loading branch information
NOOMA-42 and NOOMA-42 authored May 10, 2024
1 parent cc87319 commit c9c5d37
Show file tree
Hide file tree
Showing 9 changed files with 534 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "solidity/lib/forge-std"]
path = solidity/lib/forge-std
url = https://github.com/foundry-rs/forge-std
[submodule "solidity/lib/fx-portal"]
path = solidity/lib/fx-portal
url = https://github.com/0xPolygon/fx-portal
83 changes: 83 additions & 0 deletions solidity/contracts/hooks/PolygonPosHook.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.8.0;

/*@@@@@@@ @@@@@@@@@
@@@@@@@@@ @@@@@@@@@
@@@@@@@@@ @@@@@@@@@
@@@@@@@@@ @@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@ HYPERLANE @@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@ @@@@@@@@@
@@@@@@@@@ @@@@@@@@@
@@@@@@@@@ @@@@@@@@@
@@@@@@@@@ @@@@@@@@*/

// ============ Internal Imports ============
import {AbstractMessageIdAuthHook} from "./libs/AbstractMessageIdAuthHook.sol";
import {StandardHookMetadata} from "./libs/StandardHookMetadata.sol";
import {TypeCasts} from "../libs/TypeCasts.sol";
import {Message} from "../libs/Message.sol";
import {IPostDispatchHook} from "../interfaces/hooks/IPostDispatchHook.sol";

// ============ External Imports ============
import {FxBaseRootTunnel} from "fx-portal/contracts/tunnel/FxBaseRootTunnel.sol";
import {Address} from "@openzeppelin/contracts/utils/Address.sol";

/**
* @title PolygonPosHook
* @notice Message hook to inform the PolygonPosIsm of messages published through
* the native PoS bridge.
*/
contract PolygonPosHook is AbstractMessageIdAuthHook, FxBaseRootTunnel {
using StandardHookMetadata for bytes;

// ============ Constructor ============

constructor(
address _mailbox,
uint32 _destinationDomain,
bytes32 _ism,
address _cpManager,
address _fxRoot
)
AbstractMessageIdAuthHook(_mailbox, _destinationDomain, _ism)
FxBaseRootTunnel(_cpManager, _fxRoot)
{
require(
Address.isContract(_cpManager),
"PolygonPosHook: invalid cpManager contract"
);
require(
Address.isContract(_fxRoot),
"PolygonPosHook: invalid fxRoot contract"
);
}

// ============ Internal functions ============
function _quoteDispatch(
bytes calldata,
bytes calldata
) internal pure override returns (uint256) {
return 0;
}

/// @inheritdoc AbstractMessageIdAuthHook
function _sendMessageId(
bytes calldata metadata,
bytes memory payload
) internal override {
require(
metadata.msgValue(0) == 0,
"PolygonPosHook: does not support msgValue"
);
require(msg.value == 0, "PolygonPosHook: does not support msgValue");
_sendMessageToChild(payload);
}

bytes public latestData;

function _processMessageFromChild(bytes memory data) internal override {
latestData = data;
}
}
58 changes: 58 additions & 0 deletions solidity/contracts/isms/hook/PolygonPosIsm.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.8.0;

/*@@@@@@@ @@@@@@@@@
@@@@@@@@@ @@@@@@@@@
@@@@@@@@@ @@@@@@@@@
@@@@@@@@@ @@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@ HYPERLANE @@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@ @@@@@@@@@
@@@@@@@@@ @@@@@@@@@
@@@@@@@@@ @@@@@@@@@
@@@@@@@@@ @@@@@@@@*/

// ============ Internal Imports ============

import {IInterchainSecurityModule} from "../../interfaces/IInterchainSecurityModule.sol";
import {Message} from "../../libs/Message.sol";
import {TypeCasts} from "../../libs/TypeCasts.sol";
import {AbstractMessageIdAuthorizedIsm} from "./AbstractMessageIdAuthorizedIsm.sol";

// ============ External Imports ============
import {CrossChainEnabledPolygonChild} from "@openzeppelin/contracts/crosschain/polygon/CrossChainEnabledPolygonChild.sol";
import {Address} from "@openzeppelin/contracts/utils/Address.sol";

/**
* @title PolygonPosIsm
* @notice Uses the native Polygon Pos Fx Portal Bridge to verify interchain messages.
*/
contract PolygonPosIsm is
CrossChainEnabledPolygonChild,
AbstractMessageIdAuthorizedIsm
{
// ============ Constants ============

uint8 public constant moduleType =
uint8(IInterchainSecurityModule.Types.NULL);

// ============ Constructor ============

constructor(address _fxChild) CrossChainEnabledPolygonChild(_fxChild) {
require(
Address.isContract(_fxChild),
"PolygonPosIsm: invalid FxChild contract"
);
}

// ============ Internal function ============

/**
* @notice Check if sender is authorized to message `verifyMessageId`.
*/
function _isAuthorized() internal view override returns (bool) {
return
_crossChainSender() == TypeCasts.bytes32ToAddress(authorizedHook);
}
}
4 changes: 2 additions & 2 deletions solidity/foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ verbosity = 4
[rpc_endpoints]
mainnet = "https://eth.merkle.io"
optimism = "https://mainnet.optimism.io "

polygon = "https://rpc.ankr.com/polygon"

[fuzz]
runs = 50
dictionary_weight = 80
dictionary_weight = 80
1 change: 1 addition & 0 deletions solidity/lib/fx-portal
Submodule fx-portal added at ebd046
3 changes: 2 additions & 1 deletion solidity/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"@hyperlane-xyz/utils": "3.11.1",
"@layerzerolabs/lz-evm-oapp-v2": "2.0.2",
"@openzeppelin/contracts": "^4.9.3",
"@openzeppelin/contracts-upgradeable": "^v4.9.3"
"@openzeppelin/contracts-upgradeable": "^v4.9.3",
"fx-portal": "^1.0.3"
},
"devDependencies": {
"@layerzerolabs/solidity-examples": "^1.1.0",
Expand Down
1 change: 1 addition & 0 deletions solidity/remappings.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
@eth-optimism=../node_modules/@eth-optimism
ds-test/=lib/forge-std/lib/ds-test/src/
forge-std/=lib/forge-std/src/
fx-portal/=lib/fx-portal/
Loading

0 comments on commit c9c5d37

Please sign in to comment.