You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
112 lines
3.6 KiB
112 lines
3.6 KiB
4 years ago
|
// SPDX-License-Identifier: MIT OR Apache-2.0
|
||
|
pragma solidity >=0.6.11;
|
||
|
|
||
3 years ago
|
// ============ Internal Imports ============
|
||
|
import {XAppConnectionClient} from "./XAppConnectionClient.sol";
|
||
3 years ago
|
import {IMessageRecipient} from "../../interfaces/IMessageRecipient.sol";
|
||
4 years ago
|
|
||
3 years ago
|
abstract contract Router is XAppConnectionClient, IMessageRecipient {
|
||
4 years ago
|
// ============ Mutable Storage ============
|
||
|
|
||
3 years ago
|
mapping(uint32 => bytes32) public routers;
|
||
3 years ago
|
uint256[49] private __GAP; // gap for upgrade safety
|
||
4 years ago
|
|
||
3 years ago
|
// ============ Events ============
|
||
|
|
||
|
/**
|
||
|
* @notice Emitted when a router is set.
|
||
|
* @param domain The domain of the new router
|
||
|
* @param router The address of the new router
|
||
|
*/
|
||
|
event EnrollRemoteRouter(uint32 indexed domain, bytes32 indexed router);
|
||
|
|
||
4 years ago
|
// ============ Modifiers ============
|
||
|
|
||
|
/**
|
||
|
* @notice Only accept messages from a remote Router contract
|
||
|
* @param _origin The domain the message is coming from
|
||
|
* @param _router The address the message is coming from
|
||
|
*/
|
||
|
modifier onlyRemoteRouter(uint32 _origin, bytes32 _router) {
|
||
3 years ago
|
require(_isRemoteRouter(_origin, _router), "!router");
|
||
4 years ago
|
_;
|
||
|
}
|
||
|
|
||
|
// ============ External functions ============
|
||
|
|
||
|
/**
|
||
|
* @notice Register the address of a Router contract for the same xApp on a remote chain
|
||
|
* @param _domain The domain of the remote xApp Router
|
||
|
* @param _router The address of the remote xApp Router
|
||
|
*/
|
||
|
function enrollRemoteRouter(uint32 _domain, bytes32 _router)
|
||
|
external
|
||
3 years ago
|
virtual
|
||
4 years ago
|
onlyOwner
|
||
|
{
|
||
3 years ago
|
_enrollRemoteRouter(_domain, _router);
|
||
4 years ago
|
}
|
||
|
|
||
|
// ============ Virtual functions ============
|
||
|
|
||
|
function handle(
|
||
|
uint32 _origin,
|
||
|
bytes32 _sender,
|
||
|
bytes memory _message
|
||
3 years ago
|
) external virtual override;
|
||
4 years ago
|
|
||
|
// ============ Internal functions ============
|
||
3 years ago
|
|
||
|
/**
|
||
|
* @notice Set the router for a given domain
|
||
|
* @param _domain The domain
|
||
|
* @param _router The new router
|
||
|
*/
|
||
|
function _enrollRemoteRouter(uint32 _domain, bytes32 _router) internal {
|
||
|
routers[_domain] = _router;
|
||
|
emit EnrollRemoteRouter(_domain, _router);
|
||
|
}
|
||
|
|
||
4 years ago
|
/**
|
||
|
* @notice Return true if the given domain / router is the address of a remote xApp Router
|
||
|
* @param _domain The domain of the potential remote xApp Router
|
||
|
* @param _router The address of the potential remote xApp Router
|
||
|
*/
|
||
|
function _isRemoteRouter(uint32 _domain, bytes32 _router)
|
||
|
internal
|
||
|
view
|
||
|
returns (bool)
|
||
|
{
|
||
3 years ago
|
return routers[_domain] == _router;
|
||
4 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* @notice Assert that the given domain has a xApp Router registered and return its address
|
||
|
* @param _domain The domain of the chain for which to get the xApp Router
|
||
3 years ago
|
* @return _router The address of the remote xApp Router on _domain
|
||
4 years ago
|
*/
|
||
3 years ago
|
function _mustHaveRemoteRouter(uint32 _domain)
|
||
4 years ago
|
internal
|
||
|
view
|
||
3 years ago
|
returns (bytes32 _router)
|
||
|
{
|
||
|
_router = routers[_domain];
|
||
|
require(_router != bytes32(0), "!router");
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @notice Dispatches a message to an enrolled router via the local routers
|
||
|
* Outbox
|
||
|
* @dev Reverts if there is no enrolled router for _destination
|
||
|
* @param _destination The domain of the chain to which to send the message
|
||
|
* @param _msg The message to dispatch
|
||
|
*/
|
||
|
function _dispatchToRemoteRouter(uint32 _destination, bytes memory _msg)
|
||
|
internal
|
||
4 years ago
|
{
|
||
3 years ago
|
// ensure that destination chain has enrolled router
|
||
|
bytes32 _router = _mustHaveRemoteRouter(_destination);
|
||
|
_outbox().dispatch(_destination, _router, _msg);
|
||
4 years ago
|
}
|
||
|
}
|