-
Notifications
You must be signed in to change notification settings - Fork 31
/
HyperlaneImplementation.sol
207 lines (170 loc) · 7.75 KB
/
HyperlaneImplementation.sol
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.21;
import { IBaseStateRegistry } from "src/interfaces/IBaseStateRegistry.sol";
import { IAmbImplementation } from "src/interfaces/IAmbImplementation.sol";
import { IMailbox } from "src/vendor/hyperlane/IMailbox.sol";
import { IMessageRecipient } from "src/vendor/hyperlane/IMessageRecipient.sol";
import { ISuperRBAC } from "src/interfaces/ISuperRBAC.sol";
import { ISuperRegistry } from "src/interfaces/ISuperRegistry.sol";
import { IInterchainGasPaymaster } from "src/vendor/hyperlane/IInterchainGasPaymaster.sol";
import { AMBMessage } from "src/types/DataTypes.sol";
import { Error } from "src/utils/Error.sol";
import { DataLib } from "src/libraries/DataLib.sol";
/// @title HyperlaneImplementation
/// @author Zeropoint Labs
/// @dev allows state registries to use hyperlane for crosschain communication
contract HyperlaneImplementation is IAmbImplementation, IMessageRecipient {
using DataLib for uint256;
/*///////////////////////////////////////////////////////////////
STATE VARIABLES
//////////////////////////////////////////////////////////////*/
IMailbox public mailbox;
IInterchainGasPaymaster public igp;
ISuperRegistry public immutable superRegistry;
mapping(uint64 => uint32) public ambChainId;
mapping(uint32 => uint64) public superChainId;
mapping(uint32 => address) public authorizedImpl;
mapping(bytes32 => bool) public processedMessages;
/*///////////////////////////////////////////////////////////////
EVENTS
//////////////////////////////////////////////////////////////*/
event MailboxAdded(address _newMailbox);
event GasPayMasterAdded(address _igp);
/*///////////////////////////////////////////////////////////////
MODIFIERS
//////////////////////////////////////////////////////////////*/
modifier onlyProtocolAdmin() {
if (!ISuperRBAC(superRegistry.getAddress(keccak256("SUPER_RBAC"))).hasProtocolAdminRole(msg.sender)) {
revert Error.NOT_PROTOCOL_ADMIN();
}
_;
}
/*///////////////////////////////////////////////////////////////
CONSTRUCTOR
//////////////////////////////////////////////////////////////*/
constructor(ISuperRegistry superRegistry_) {
superRegistry = superRegistry_;
}
/*///////////////////////////////////////////////////////////////
HYPERLANE APPLICATION CONFIG
//////////////////////////////////////////////////////////////*/
/// @dev allows protocol admin to configure hyperlane mailbox and gas paymaster
/// @param mailbox_ is the address of hyperlane mailbox
/// @param igp_ is the address of hyperlane gas paymaster
function setHyperlaneConfig(IMailbox mailbox_, IInterchainGasPaymaster igp_) external onlyProtocolAdmin {
mailbox = mailbox_;
igp = igp_;
emit MailboxAdded(address(mailbox_));
emit GasPayMasterAdded(address(igp_));
}
/*///////////////////////////////////////////////////////////////
EXTERNAL FUNCTIONS
//////////////////////////////////////////////////////////////*/
/// @inheritdoc IAmbImplementation
function dispatchPayload(
address srcSender_,
uint64 dstChainId_,
bytes memory message_,
bytes memory extraData_
)
external
payable
virtual
override
{
if (!superRegistry.isValidStateRegistry(msg.sender)) {
revert Error.NOT_STATE_REGISTRY();
}
uint32 domain = ambChainId[dstChainId_];
bytes32 messageId = mailbox.dispatch(domain, _castAddr(authorizedImpl[domain]), message_);
igp.payForGas{ value: msg.value }(
messageId, domain, extraData_.length > 0 ? abi.decode(extraData_, (uint256)) : 0, srcSender_
);
}
/// @dev allows protocol admin to add new chain ids in future
/// @param superChainId_ is the identifier of the chain within superform protocol
/// @param ambChainId_ is the identifier of the chain given by the AMB
/// NOTE: cannot be defined in an interface as types vary for each message bridge (amb)
function setChainId(uint64 superChainId_, uint32 ambChainId_) external onlyProtocolAdmin {
if (superChainId_ == 0 || ambChainId_ == 0) {
revert Error.INVALID_CHAIN_ID();
}
/// @dev reset old mappings
uint64 oldSuperChainId = superChainId[ambChainId_];
uint32 oldAmbChainId = ambChainId[superChainId_];
if (oldSuperChainId != 0) {
delete ambChainId[oldSuperChainId];
}
if (oldAmbChainId != 0) {
delete superChainId[oldAmbChainId];
}
ambChainId[superChainId_] = ambChainId_;
superChainId[ambChainId_] = superChainId_;
emit ChainAdded(superChainId_);
}
/// @dev allows protocol admin to set receiver implmentation on a new chain id
/// @param domain_ is the identifier of the destination chain within hyperlane
/// @param authorizedImpl_ is the implementation of the hyperlane message bridge on the specified destination
/// NOTE: cannot be defined in an interface as types vary for each message bridge (amb)
function setReceiver(uint32 domain_, address authorizedImpl_) external onlyProtocolAdmin {
if (domain_ == 0) {
revert Error.INVALID_CHAIN_ID();
}
if (authorizedImpl_ == address(0)) {
revert Error.ZERO_ADDRESS();
}
authorizedImpl[domain_] = authorizedImpl_;
}
/// @inheritdoc IMessageRecipient
function handle(uint32 origin_, bytes32 sender_, bytes calldata body_) external override {
/// @dev 1. validate caller
/// @dev 2. validate src chain sender
/// @dev 3. validate message uniqueness
if (msg.sender != address(mailbox)) {
revert Error.CALLER_NOT_MAILBOX();
}
if (sender_ != _castAddr(authorizedImpl[origin_])) {
revert Error.INVALID_SRC_SENDER();
}
bytes32 hash = keccak256(body_);
if (processedMessages[hash]) {
revert Error.DUPLICATE_PAYLOAD();
}
processedMessages[hash] = true;
/// @dev decoding payload
AMBMessage memory decoded = abi.decode(body_, (AMBMessage));
/// NOTE: experimental split of registry contracts
(,,, uint8 registryId,,) = decoded.txInfo.decodeTxInfo();
address registryAddress = superRegistry.getStateRegistry(registryId);
IBaseStateRegistry targetRegistry = IBaseStateRegistry(registryAddress);
targetRegistry.receivePayload(superChainId[origin_], body_);
}
/*///////////////////////////////////////////////////////////////
READ-ONLY FUNCTIONS
//////////////////////////////////////////////////////////////*/
/// @inheritdoc IAmbImplementation
function estimateFees(
uint64 dstChainId_,
bytes memory,
bytes memory extraData_
)
external
view
override
returns (uint256 fees)
{
uint32 domain = ambChainId[dstChainId_];
if (domain != 0) {
fees = igp.quoteGasPayment(domain, abi.decode(extraData_, (uint256)));
}
}
/*///////////////////////////////////////////////////////////////
INTERNAL/HELPER FUNCTIONS
//////////////////////////////////////////////////////////////*/
/// @dev casts an address to bytes32
/// @param addr_ is the address to be casted
/// @return a bytes32 casted variable of the address passed in params
function _castAddr(address addr_) internal pure returns (bytes32) {
return bytes32(uint256(uint160(addr_)));
}
}