-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathPhatRollupAnchor.sol
279 lines (251 loc) · 10.9 KB
/
PhatRollupAnchor.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.9;
import "@openzeppelin/contracts/access/AccessControl.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import "./MetaTransaction.sol";
// Uncomment this line to use console.log
// import "hardhat/console.sol";
/// Adds the Offchain Rollup functionalities to your contract
///
/// Phat Offchain Rollup Anchor implements the rollup functions to allow your contract to
/// integrate. It implements the basic kv-store, the rollup transaction handling, and also allow
/// you to interact with the Phat Contract in a request-response style.
///
/// ## Solidity Usage
///
/// ```solidity
/// contract ConsumerContract is PhatRollupAnchor {
/// constructor(address attestor) {
/// _grantRole(PhatRollupAnchor.ATTESTOR_ROLE, attestor);
/// }
/// function _onMessageReceived(bytes calldata action) internal override {
/// emit MsgReceived(action);
/// }
/// }
/// ```
///
/// Inherit this abstract contract in your consumer contract. To allow the Phat Contract to connect
/// to your consumer contract properly, you will need to specify `attestor`, an address generated
/// and controlled by the Phat Contract as its credential.
///
/// Add a attestor by `_grantRole()` as above. The attestors are controlled by OpenZeppelin's
/// `AccessControl` library. It allows to add and remove members to the role. You should have at
/// least one `attestor` to receive response from Phat Contract.
///
/// Then you should implement `_onMessageReceived()` to receive response. The parameter `action` is
/// the raw data provided by the Phat Contract. Usually it's encoded meaningful data in some
/// predefined schema (e.g. `abi.encode()`).
///
/// Call `_pushMessage(data)` to push the raw message to the Phat Contract. It returns the request
/// id, which can be used to link the response to the request later.
///
/// ## Phat Contract Usage
///
/// On the Phat Contract side, when some requests are processed, it should send an action
/// `ACTION_SET_QUEUE_HEAD` to removed the finished requests.
///
/// ## Storage layout
///
/// - `<lockKey>`: `uint` - the version of the queue lock
/// - `<prefix>/_head`: `uint` - index of the first element
/// - `<prefix>/_tail`: `uint` - index of the next element to push to the queue
/// - `<prefix/<n>`: `bytes` - the `n`-th message; `n` is encoded as uint32
abstract contract PhatRollupAnchor is ReentrancyGuard, MetaTxReceiver, AccessControl {
// Constants aligned with the Phat Contract rollup queue implementation.
bytes constant QUEUE_PREFIX = "q/";
bytes constant KEY_HEAD = "_head";
bytes constant KEY_TAIL = "_tail";
// Only submission from attestor is allowed.
bytes32 public constant ATTESTOR_ROLE = keccak256("ATTESTOR_ROLE");
event MetaTxDecoded();
event MessageQueued(uint256 idx, bytes data);
event MessageProcessedTo(uint256);
error BadAttestor();
error BadCondLen(uint kenLen, uint valueLen);
error BadUpdateLen(uint kenLen, uint valueLen);
error CondNotMet(bytes cond, uint32 expected, uint32 actual);
error CannotDecodeAction(uint8 actionId);
error UnsupportedAction(uint8 actionId);
error Internal_toUint32Strict_outOfBounds(bytes data);
error InvalidPopTarget(uint256 targetIdx, uint256 tailIdx);
uint8 constant ACTION_REPLY = 0;
uint8 constant ACTION_SET_QUEUE_HEAD = 1;
uint8 constant ACTION_GRANT_ATTESTOR = 10;
uint8 constant ACTION_REVOKE_ATTESTOR = 11;
mapping (bytes => bytes) kvStore;
/// Triggers a rollup transaction with `eq` conditoin check on uint256 values
///
/// - actions: Starts with one byte to define the action type and followed by the parameter of
/// the actions. Supported actions: ACTION_REPLY, ACTION_SET_QUEUE_HEAD
///
/// Note that calling from `address(this)` is allowed to make parameters a calldata. Don't
/// abuse it.
function rollupU256CondEq(
bytes[] calldata condKeys,
bytes[] calldata condValues,
bytes[] calldata updateKeys,
bytes[] calldata updateValues,
bytes[] calldata actions
) public returns (bool) {
// Allow meta tx to call itself
if (msg.sender != address(this) && !hasRole(ATTESTOR_ROLE, msg.sender)) {
revert BadAttestor();
}
return _rollupU256CondEqInternal(condKeys, condValues, updateKeys, updateValues, actions);
}
/// Triggers a rollup transaction similar to `rollupU256CondEq` but with meta-tx.
///
/// Note to error handling. Most of the errors are propagated to the transaction error.
/// However in case of out-of-gas, the error will not be propagated. It results in a bare
/// "reverted" in etherscan. It's hard to debug, but you will find the gas is 100% used like
/// [this tx](https://mumbai.polygonscan.com/tx/0x0abe643ada209ec31a0a6da4fab546b7071e1cf265f3b4681b9bede209c400c9).
function metaTxRollupU256CondEq(
ForwardRequest calldata req,
bytes calldata signature
) public useMetaTx(req, signature) returns (bool) {
if (!hasRole(ATTESTOR_ROLE, req.from)) {
revert BadAttestor();
}
(
bytes[] memory condKeys,
bytes[] memory condValues,
bytes[] memory updateKeys,
bytes[] memory updateValues,
bytes[] memory actions
) = abi.decode(req.data, (bytes[], bytes[], bytes[], bytes[], bytes[]));
emit MetaTxDecoded();
// Self-call to move memory bytes to calldata. Check "error handling" notes in docstring
// to learn more.
return this.rollupU256CondEq(condKeys, condValues, updateKeys, updateValues, actions);
}
function _rollupU256CondEqInternal(
bytes[] calldata condKeys,
bytes[] calldata condValues,
bytes[] calldata updateKeys,
bytes[] calldata updateValues,
bytes[] calldata actions
) internal nonReentrant() returns (bool) {
if (condKeys.length != condValues.length) {
revert BadCondLen(condKeys.length, condValues.length);
}
if (updateKeys.length != updateValues.length) {
revert BadUpdateLen(updateKeys.length, updateValues.length);
}
// check cond
for (uint i = 0; i < condKeys.length; i++) {
uint32 value = toUint32Strict(kvStore[condKeys[i]]);
uint32 expected = toUint32Strict(condValues[i]);
if (value != expected) {
revert CondNotMet(condKeys[i], expected, value);
}
}
// apply updates
for (uint i = 0; i < updateKeys.length; i++) {
kvStore[updateKeys[i]] = updateValues[i];
}
// apply actions
for (uint i = 0; i < actions.length; i++) {
handleAction(actions[i]);
}
return true;
}
function handleAction(bytes calldata action) private {
uint8 actionType = uint8(action[0]);
if (actionType == ACTION_REPLY) {
_onMessageReceived(action[1:]);
} else if (actionType == ACTION_SET_QUEUE_HEAD) {
if (action.length < 1 + 32) {
revert CannotDecodeAction(ACTION_SET_QUEUE_HEAD);
}
uint32 targetIdx = abi.decode(action[1:], (uint32));
_popTo(targetIdx);
} else if (actionType == ACTION_GRANT_ATTESTOR) {
if (action.length < 1 + 20) {
revert CannotDecodeAction(ACTION_GRANT_ATTESTOR);
}
address attestor = abi.decode(action[1:], (address));
_grantRole(ATTESTOR_ROLE, attestor);
} else if (actionType == ACTION_REVOKE_ATTESTOR) {
if (action.length < 1 + 20) {
revert CannotDecodeAction(ACTION_REVOKE_ATTESTOR);
}
address attestor = abi.decode(action[1:], (address));
_revokeRole(ATTESTOR_ROLE, attestor);
} else {
revert UnsupportedAction(actionType);
}
}
function getStorage(bytes memory key) public view returns(bytes memory) {
return kvStore[key];
}
function toUint32Strict(bytes memory _bytes) public pure returns (uint32) {
if (_bytes.length == 0) {
return 0;
}
if (_bytes.length != 32) {
revert Internal_toUint32Strict_outOfBounds(_bytes);
}
uint32 v = abi.decode(_bytes, (uint32));
return v;
}
// Queue functions
/// Pushes a request to the queue waiting for the Phat Contract to process
///
/// Returns the index of the reqeust.
function _pushMessage(bytes memory data) internal returns (uint32) {
uint32 tail = queueGetUint(KEY_TAIL);
bytes memory itemKey = abi.encode(tail);
queueSetBytes(itemKey, data);
queueSetUint(KEY_TAIL, tail + 1);
emit MessageQueued(tail, data);
return tail;
}
function _popTo(uint32 targetIdx) internal {
uint32 curTail = queueGetUint(KEY_TAIL);
if (targetIdx > curTail) {
revert InvalidPopTarget(targetIdx, curTail);
}
for (uint32 i = queueGetUint(KEY_HEAD); i < targetIdx; i++) {
queueRemoveItem(i);
}
queueSetUint(KEY_HEAD, targetIdx);
emit MessageProcessedTo(targetIdx);
}
/// The handler to be called when a message is received from a Phat Contract
///
/// Reverting in this function resulting the revert of the offchain rollup transaction.
function _onMessageReceived(bytes calldata action) internal virtual;
/// Returns the prefix of the queue related keys
///
/// The queue is persisted in the rollup kv store with all its keys prefixed. This function
/// returns the prefix.
function queueGetPrefix() public pure returns (bytes memory) {
return QUEUE_PREFIX;
}
/// Returns the raw bytes value stored in the queue kv store
function queueGetBytes(bytes memory key) public view returns (bytes memory) {
bytes memory storageKey = bytes.concat(QUEUE_PREFIX, key);
return kvStore[storageKey];
}
/// Returns the uint32 repr of the data stored in the queue kv store
function queueGetUint(bytes memory key) public view returns (uint32) {
bytes memory storageKey = bytes.concat(QUEUE_PREFIX, key);
return toUint32Strict(kvStore[storageKey]);
}
/// Stores a raw bytes value to the queue kv store
function queueSetBytes(bytes memory key, bytes memory value) internal {
bytes memory storageKey = bytes.concat(QUEUE_PREFIX, key);
kvStore[storageKey] = value;
}
/// Stores a uint32 value to the queue kv store
function queueSetUint(bytes memory key, uint32 value) internal {
bytes memory storageKey = bytes.concat(QUEUE_PREFIX, key);
kvStore[storageKey] = abi.encode(value);
}
/// Removes a queue item
function queueRemoveItem(uint32 idx) internal {
bytes memory key = abi.encode(idx);
bytes memory storageKey = bytes.concat(QUEUE_PREFIX, key);
delete kvStore[storageKey];
}
}