-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
added hyperlane adapter #17
Open
degencodebeast
wants to merge
12
commits into
pooltogether:main
Choose a base branch
from
degencodebeast:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
2224532
added hyperlane adapter
41c447a
refactored code + fixed bugs
787c755
formatted code with prettier
8ac5460
fixed bugs
73a1fb9
adding overrides to dispatchMessage + handle fns
9bdd08b
fix: updated the code according to the review from nambrot
7879ac5
added hyperlane dependency
d778c87
updated to hyperlane V3
17d4ebd
fix: fixed bug in executeMessageBatch
57b4df8
fix: fixed bug in executeMessageBatch
fad1b72
reverted to use openzeppelin's old ownable
9bf2e93
updated ownable
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
pragma solidity 0.8.16; | ||
|
||
import { Ownable } from "@openzeppelin/contracts/access/Ownable.sol"; | ||
import { IMailbox } from "./interfaces/IMailbox.sol"; | ||
import { IMessageRecipient } from "./interfaces/IMessageRecipient.sol"; | ||
import { IInterchainSecurityModule, ISpecifiesInterchainSecurityModule } from "./interfaces/IInterchainSecurityModule.sol"; | ||
import { TypeCasts } from "./libraries/TypeCasts.sol"; | ||
import { EncodeDecodeUtil } from "./libraries/EncodeDecodeUtil.sol"; | ||
import { Errors } from "./libraries/Errors.sol"; | ||
import { IMessageDispatcher } from "../interfaces/IMessageDispatcher.sol"; | ||
import { IMessageExecutor } from "../interfaces/IMessageExecutor.sol"; | ||
import "../libraries/MessageLib.sol"; | ||
|
||
/** | ||
* @title HyperlaneReceiverAdapter implementation. | ||
* @notice `IBridgeReceiverAdapter` implementation that uses Hyperlane as the bridge. | ||
*/ | ||
contract HyperlaneReceiverAdapterV2 is | ||
IMessageRecipient, | ||
ISpecifiesInterchainSecurityModule, | ||
Ownable | ||
{ | ||
/// @notice `Mailbox` contract reference. | ||
IMailbox public immutable mailbox; | ||
|
||
/// @notice `ISM` contract reference. | ||
IInterchainSecurityModule public ism; | ||
|
||
/** | ||
* @notice Sender adapter address for each source chain. | ||
* @dev srcChainId => senderAdapter address. | ||
*/ | ||
mapping(uint256 => IMessageDispatcher) public senderAdapters; | ||
|
||
/** | ||
* @notice Ensure that messages cannot be replayed once they have been executed. | ||
* @dev msgId => isExecuted. | ||
*/ | ||
mapping(bytes32 => bool) public executedMessages; | ||
|
||
/** | ||
* @notice Emitted when the ISM is set. | ||
* @param module The new ISM for this adapter/recipient. | ||
*/ | ||
event IsmSet(address indexed module); | ||
|
||
/** | ||
* @notice Emitted when a message has successfully been executed. | ||
* @param fromChainId ID of the chain that dispatched the message | ||
* @param messageId ID uniquely identifying the message that was executed | ||
*/ | ||
event MessageIdExecuted(uint256 indexed fromChainId, bytes32 indexed messageId); | ||
|
||
/** | ||
* @notice Emitted when a messageId has already been executed. | ||
* @param messageId ID uniquely identifying the message or message batch that were re-executed | ||
*/ | ||
error MessageIdAlreadyExecuted(bytes32 messageId); | ||
|
||
/** | ||
* @notice Emitted when a sender adapter for a source chain is updated. | ||
* @param srcChainId Source chain identifier. | ||
* @param senderAdapter Address of the sender adapter. | ||
*/ | ||
event SenderAdapterUpdated(uint256 srcChainId, IMessageDispatcher senderAdapter); | ||
|
||
/* Constructor */ | ||
/** | ||
* @notice HyperlaneReceiverAdapter constructor. | ||
* @param _mailbox Address of the Hyperlane `Mailbox` contract. | ||
*/ | ||
constructor(address _mailbox) { | ||
if (_mailbox == address(0)) { | ||
revert Errors.InvalidMailboxZeroAddress(); | ||
} | ||
mailbox = IMailbox(_mailbox); | ||
} | ||
|
||
/// @notice Restrict access to trusted `Mailbox` contract. | ||
modifier onlyMailbox() { | ||
if (msg.sender != address(mailbox)) { | ||
revert Errors.UnauthorizedMailbox(msg.sender); | ||
} | ||
_; | ||
} | ||
|
||
/// @inheritdoc ISpecifiesInterchainSecurityModule | ||
function interchainSecurityModule() external view returns (IInterchainSecurityModule) { | ||
return ism; | ||
} | ||
|
||
/** | ||
* @notice Sets the ISM for this adapter/recipient. | ||
* @param _ism The ISM contract address. | ||
*/ | ||
function setIsm(address _ism) external onlyOwner { | ||
ism = IInterchainSecurityModule(_ism); | ||
emit IsmSet(_ism); | ||
} | ||
|
||
function executeMessage( | ||
address _to, | ||
bytes memory _message, | ||
bytes32 _messageId, | ||
uint256 _fromChainId, | ||
address _from, | ||
bool _executedMessageId | ||
) internal { | ||
MessageLib.executeMessage(_to, _message, _messageId, _fromChainId, _from, _executedMessageId); | ||
|
||
emit MessageIdExecuted(_fromChainId, _messageId); | ||
} | ||
|
||
function executeMessageBatch( | ||
MessageLib.Message[] memory _messages, | ||
bytes32 _messageId, | ||
uint256 _fromChainId, | ||
address _from, | ||
bool _executedMessageId | ||
) internal { | ||
MessageLib.executeMessageBatch(_messages, _messageId, _fromChainId, _from, _executedMessageId); | ||
|
||
emit MessageIdExecuted(_fromChainId, _messageId); | ||
} | ||
|
||
/** | ||
* @notice Called by Hyperlane `Mailbox` contract on destination chain to receive cross-chain messages. | ||
* @dev _origin Source chain domain identifier (not currently used). | ||
* @param _sender Address of the sender on the source chain. | ||
* @param _body Body of the message. | ||
*/ | ||
function handle( | ||
uint32 _origin, | ||
bytes32 _sender, | ||
bytes memory _body | ||
) external onlyMailbox { | ||
address adapter = TypeCasts.bytes32ToAddress(_sender); | ||
bool _executedMessageId; | ||
( | ||
MessageLib.Message[] memory _messages, | ||
bytes32 msgId, | ||
uint256 srcChainId, | ||
address srcSender | ||
) = EncodeDecodeUtil.decode(_body); | ||
|
||
if (_origin != srcChainId) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. comparing the origin and sender chain |
||
revert Errors.UnauthorizedOrigin(_origin); | ||
} | ||
|
||
if (IMessageDispatcher(adapter) != senderAdapters[srcChainId]) { | ||
revert Errors.UnauthorizedAdapter(srcChainId, adapter); | ||
} | ||
|
||
if (_messages.length < 1) { | ||
revert Errors.NoMessagesSent(srcChainId); | ||
} | ||
|
||
if (executedMessages[msgId]) { | ||
revert MessageIdAlreadyExecuted(msgId); | ||
} else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. did all my revert conditions up front and did state changes after |
||
_executedMessageId = executedMessages[msgId]; | ||
executedMessages[msgId] = true; | ||
} | ||
|
||
if (_messages.length == 1) { | ||
MessageLib.Message memory _message = _messages[0]; | ||
executeMessage(_message.to, _message.data, msgId, srcChainId, srcSender, _executedMessageId); | ||
} else { | ||
executeMessageBatch(_messages, msgId, srcChainId, srcSender, _executedMessageId); | ||
} | ||
} | ||
|
||
function updateSenderAdapter( | ||
uint256[] calldata _srcChainIds, | ||
IMessageDispatcher[] calldata _senderAdapters | ||
) external onlyOwner { | ||
if (_srcChainIds.length != _senderAdapters.length) { | ||
revert Errors.MismatchChainsAdaptersLength(_srcChainIds.length, _senderAdapters.length); | ||
} | ||
for (uint256 i; i < _srcChainIds.length; ++i) { | ||
senderAdapters[_srcChainIds[i]] = _senderAdapters[i]; | ||
emit SenderAdapterUpdated(_srcChainIds[i], _senderAdapters[i]); | ||
} | ||
} | ||
|
||
function getSenderAdapter(uint256 _srcChainId) | ||
public | ||
view | ||
returns (IMessageDispatcher _senderAdapter) | ||
{ | ||
_senderAdapter = senderAdapters[_srcChainId]; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated to V2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be v3?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have updated the adapter to use hyperlane V3 and added some unit tests as well