-
Notifications
You must be signed in to change notification settings - Fork 579
Expand file tree
/
Copy pathDomainRoutingIsm.sol
More file actions
165 lines (146 loc) · 5.28 KB
/
DomainRoutingIsm.sol
File metadata and controls
165 lines (146 loc) · 5.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.8.0;
// ============ External Imports ============
import {Address} from "@openzeppelin/contracts/utils/Address.sol";
import {OwnableUpgradeable} from "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
import {Strings} from "@openzeppelin/contracts/utils/Strings.sol";
// ============ Internal Imports ============
import {AbstractRoutingIsm} from "./AbstractRoutingIsm.sol";
import {IInterchainSecurityModule} from "../../interfaces/IInterchainSecurityModule.sol";
import {Message} from "../../libs/Message.sol";
import {TypeCasts} from "../../libs/TypeCasts.sol";
import {EnumerableMapExtended} from "../../libs/EnumerableMapExtended.sol";
import {PackageVersioned} from "../../PackageVersioned.sol";
/**
* @title DomainRoutingIsm
*/
contract DomainRoutingIsm is
AbstractRoutingIsm,
OwnableUpgradeable,
PackageVersioned
{
using EnumerableMapExtended for EnumerableMapExtended.UintToBytes32Map;
using Message for bytes;
using TypeCasts for bytes32;
using TypeCasts for address;
using Address for address;
using Strings for uint32;
// ============ Mutable Storage ============
EnumerableMapExtended.UintToBytes32Map internal _modules;
// ============ Structs ============
struct DomainModule {
uint32 domain;
IInterchainSecurityModule module;
}
// ============ External Functions ============
/**
* @param _owner The owner of the contract.
*/
function initialize(address _owner) public initializer {
__Ownable_init();
_transferOwnership(_owner);
}
/**
* @notice Sets the ISMs to be used for the specified origin domains
* @param _owner The owner of the contract.
* @param _domains The origin domains
* @param __modules The ISMs to use to verify messages
*/
function initialize(
address _owner,
uint32[] calldata _domains,
IInterchainSecurityModule[] calldata __modules
) public initializer {
__Ownable_init();
require(_domains.length == __modules.length, "length mismatch");
uint256 _length = _domains.length;
for (uint256 i = 0; i < _length; ++i) {
_set(_domains[i], address(__modules[i]));
}
_transferOwnership(_owner);
}
/**
* @notice Sets the ISM to be used for the specified origin domain
* @param _domain The origin domain
* @param _module The ISM to use to verify messages
*/
function set(
uint32 _domain,
IInterchainSecurityModule _module
) external onlyOwner {
_set(_domain, address(_module));
}
/**
* @notice Sets the ISMs to be used for the specified origin domains
* @param _domainModules The origin domains and ISMs to enroll
*/
function setBatch(
DomainModule[] calldata _domainModules
) external onlyOwner {
for (uint256 i = 0; i < _domainModules.length; ++i) {
_set(_domainModules[i].domain, address(_domainModules[i].module));
}
}
/**
* @notice Removes the specified origin domain
* @param _domain The origin domain
*/
function remove(uint32 _domain) external onlyOwner {
_remove(_domain);
}
/**
* @notice Removes the specified origin domains
* @param _domains The origin domains to remove
*/
function removeBatch(uint32[] calldata _domains) external onlyOwner {
for (uint256 i = 0; i < _domains.length; ++i) {
_remove(_domains[i]);
}
}
function domains() external view returns (uint256[] memory) {
return _modules.keys();
}
function module(
uint32 origin
) public view virtual returns (IInterchainSecurityModule) {
(bool contained, bytes32 _module) = _modules.tryGet(origin);
if (contained) {
return IInterchainSecurityModule(_module.bytes32ToAddress());
}
revert(_originNotFoundError(origin));
}
// ============ Public Functions ============
/**
* @notice Returns the ISM responsible for verifying _message
* @dev Can change based on the content of _message
* @param _message Formatted Hyperlane message (see Message.sol).
* @return module The ISM to use to verify _message
*/
function route(
bytes calldata _message
) public view override returns (IInterchainSecurityModule) {
return module(_message.origin());
}
// ============ Internal Functions ============
/**
* @notice Removes the specified origin domain's ISM
* @param _domain The origin domain
*/
function _remove(uint32 _domain) internal virtual {
require(_modules.remove(_domain), _originNotFoundError(_domain));
}
function _originNotFoundError(
uint32 _origin
) internal pure returns (string memory) {
return string.concat("No ISM found for origin: ", _origin.toString());
}
/**
* @notice Sets the ISM to be used for the specified origin domain
* @param _domain The origin domain
* @param _module The ISM to use to verify messages
*/
function _set(uint32 _domain, address _module) internal virtual {
require(_module.isContract(), "ISM must be a contract");
_modules.set(_domain, _module.addressToBytes32());
}
}