-
Notifications
You must be signed in to change notification settings - Fork 4
/
OracleMaster.sol
331 lines (276 loc) · 11.1 KB
/
OracleMaster.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
pragma abicoder v2;
import "@openzeppelin/contracts/security/Pausable.sol";
import "@openzeppelin/contracts/proxy/Clones.sol";
import "../interfaces/IOracle.sol";
import "../interfaces/ILido.sol";
import "../interfaces/ILedger.sol";
import "../interfaces/IAuthManager.sol";
import "./utils/LedgerUtils.sol";
contract OracleMaster is Pausable {
using Clones for address;
using LedgerUtils for Types.OracleData;
event MemberAdded(address member);
event MemberRemoved(address member);
event QuorumChanged(uint8 QUORUM);
// current era id
uint64 public eraId;
// Oracle members
address[] public members;
// ledger -> oracle pairing
mapping(address => address) private oracleForLedger;
// address of oracle clone template contract
address public ORACLE_CLONE;
// Lido smart contract
address public LIDO;
// Quorum threshold
uint8 public QUORUM;
// Relay era id on updating
uint64 public ANCHOR_ERA_ID;
// Relay timestamp on updating
uint64 public ANCHOR_TIMESTAMP;
// Relay seconds per era
uint64 public SECONDS_PER_ERA;
/// Maximum number of oracle committee members
uint256 public constant MAX_MEMBERS = 255;
// Missing member index
uint256 internal constant MEMBER_NOT_FOUND = type(uint256).max;
// Spec manager role
bytes32 internal constant ROLE_SPEC_MANAGER = keccak256("ROLE_SPEC_MANAGER");
// General oracle manager role
bytes32 internal constant ROLE_PAUSE_MANAGER = keccak256("ROLE_PAUSE_MANAGER");
// Oracle members manager role
bytes32 internal constant ROLE_ORACLE_MEMBERS_MANAGER = keccak256("ROLE_ORACLE_MEMBERS_MANAGER");
// Oracle members manager role
bytes32 internal constant ROLE_ORACLE_QUORUM_MANAGER = keccak256("ROLE_ORACLE_QUORUM_MANAGER");
// Allows function calls only from member with specific role
modifier auth(bytes32 role) {
require(IAuthManager(ILido(LIDO).AUTH_MANAGER()).has(role, msg.sender), "OM: UNAUTHOROZED");
_;
}
// Allows function calls only from LIDO
modifier onlyLido() {
require(msg.sender == LIDO, "OM: CALLER_NOT_LIDO");
_;
}
/**
* @notice Initialize oracle master contract, allowed to call only once
* @param _oracleClone oracle clone contract address
* @param _quorum inital quorum threshold
*/
function initialize(
address _oracleClone,
uint8 _quorum
) external {
require(ORACLE_CLONE == address(0), "OM: ALREADY_INITIALIZED");
require(_oracleClone != address(0), "OM: INCORRECT_CLONE_ADDRESS");
require(_quorum > 0 && _quorum < MAX_MEMBERS, "OM: INCORRECT_QUORUM");
ORACLE_CLONE = _oracleClone;
QUORUM = _quorum;
}
/**
* @notice Set lido contract address, allowed to only once
* @param _lido lido contract address
*/
function setLido(address _lido) external {
require(LIDO == address(0), "OM: LIDO_ALREADY_DEFINED");
require(_lido != address(0), "OM: INCORRECT_LIDO_ADDRESS");
LIDO = _lido;
}
/**
* @notice Set the number of exactly the same reports needed to finalize the era
allowed to call only by ROLE_ORACLE_QUORUM_MANAGER
* @param _quorum new value of quorum threshold
*/
function setQuorum(uint8 _quorum) external auth(ROLE_ORACLE_QUORUM_MANAGER) {
require(_quorum > 0 && _quorum < MAX_MEMBERS, "OM: QUORUM_WONT_BE_MADE");
uint8 oldQuorum = QUORUM;
QUORUM = _quorum;
// If the QUORUM value lowered, check existing reports whether it is time to push
if (oldQuorum > _quorum) {
address[] memory ledgers = ILido(LIDO).getLedgerAddresses();
uint256 _length = ledgers.length;
for (uint256 i = 0; i < _length; ++i) {
address oracle = oracleForLedger[ledgers[i]];
if (oracle != address(0)) {
IOracle(oracle).softenQuorum(_quorum, eraId);
}
}
}
emit QuorumChanged(_quorum);
}
/**
* @notice Return oracle contract for the given ledger
* @param _ledger ledger contract address
* @return linked oracle address
*/
function getOracle(address _ledger) external view returns (address) {
return oracleForLedger[_ledger];
}
/**
* @notice Return current Era according to relay chain spec
* @return current era id
*/
function getCurrentEraId() public view returns (uint64) {
return _getCurrentEraId();
}
/**
* @notice Return relay chain stash account addresses. This function used in oracle service
* @return Array of bytes32 relaychain stash accounts
*/
function getStashAccounts() external view returns (bytes32[] memory) {
return ILido(LIDO).getStashAccounts();
}
/**
* @notice Return last reported era and oracle is already reported indicator
* @param _oracleMember - oracle member address
* @param _stash - stash account id
* @return lastEra - last reported era
* @return isReported - true if oracle member already reported for given stash, else false
*/
function isReportedLastEra(address _oracleMember, bytes32 _stash)
external
view
returns (
uint64 lastEra,
bool isReported
)
{
uint64 lastEra = eraId;
uint256 memberIdx = _getMemberId(_oracleMember);
if (memberIdx == MEMBER_NOT_FOUND) {
return (lastEra, false);
}
address ledger = ILido(LIDO).findLedger(_stash);
if (ledger == address(0)) {
return (lastEra, false);
}
return (lastEra, IOracle(oracleForLedger[ledger]).isReported(memberIdx));
}
/**
* @notice Stop pool routine operations (reportRelay), allowed to call only by ROLE_PAUSE_MANAGER
*/
function pause() external auth(ROLE_PAUSE_MANAGER) {
_pause();
}
/**
* @notice Resume pool routine operations (reportRelay), allowed to call only by ROLE_PAUSE_MANAGER
*/
function resume() external auth(ROLE_PAUSE_MANAGER) {
_unpause();
}
/**
* @notice Add new member to the oracle member committee list, allowed to call only by ROLE_ORACLE_MEMBERS_MANAGER
* @param _member proposed member address
*/
function addOracleMember(address _member) external auth(ROLE_ORACLE_MEMBERS_MANAGER) {
require(_member != address(0), "OM: BAD_ARGUMENT");
require(_getMemberId(_member) == MEMBER_NOT_FOUND, "OM: MEMBER_EXISTS");
require(members.length < MAX_MEMBERS, "OM: MEMBERS_TOO_MANY");
members.push(_member);
emit MemberAdded(_member);
}
/**
* @notice Remove `_member` from the oracle member committee list, allowed to call only by ROLE_ORACLE_MEMBERS_MANAGER
*/
function removeOracleMember(address _member) external auth(ROLE_ORACLE_MEMBERS_MANAGER) {
uint256 index = _getMemberId(_member);
require(index != MEMBER_NOT_FOUND, "OM: MEMBER_NOT_FOUND");
uint256 last = members.length - 1;
if (index != last) members[index] = members[last];
members.pop();
emit MemberRemoved(_member);
// delete the data for the last eraId, let remained oracles report it again
_clearReporting();
}
/**
* @notice Add ledger to oracle set, allowed to call only by lido contract
* @param _ledger Ledger contract
*/
function addLedger(address _ledger) external onlyLido {
require(ORACLE_CLONE != address(0), "OM: ORACLE_CLONE_UNINITIALIZED");
IOracle newOracle = IOracle(ORACLE_CLONE.cloneDeterministic(bytes32(uint256(uint160(_ledger)) << 96)));
newOracle.initialize(address(this), _ledger);
oracleForLedger[_ledger] = address(newOracle);
}
/**
* @notice Remove ledger from oracle set, allowed to call only by lido contract
* @param _ledger ledger contract
*/
function removeLedger(address _ledger) external onlyLido {
oracleForLedger[_ledger] = address(0);
}
/**
* @notice Accept oracle committee member reports from the relay side
* @param _eraId relaychain era
* @param _report relaychain data report
*/
function reportRelay(uint64 _eraId, Types.OracleData calldata _report) external whenNotPaused {
require(_report.isConsistent(), "OM: INCORRECT_REPORT");
uint256 memberIndex = _getMemberId(msg.sender);
require(memberIndex != MEMBER_NOT_FOUND, "OM: MEMBER_NOT_FOUND");
address ledger = ILido(LIDO).findLedger(_report.stashAccount);
address oracle = oracleForLedger[ledger];
require(oracle != address(0), "OM: ORACLE_FOR_LEDGER_NOT_FOUND");
require(_eraId >= eraId, "OM: ERA_TOO_OLD");
// new era
if (_eraId > eraId) {
require(_eraId <= _getCurrentEraId(), "OM: UNEXPECTED_NEW_ERA");
eraId = _eraId;
_clearReporting();
ILido(LIDO).flushStakes();
}
IOracle(oracle).reportRelay(memberIndex, QUORUM, _eraId, _report);
}
/**
* @notice Set parameters from relay chain for accurately calculation of current era id
* @param _anchorEraId - current relay chain era id
* @param _anchorTimestamp - current relay chain timestamp
* @param _secondsPerEra - current relay chain era duration in seconds
*/
function setAnchorEra(uint64 _anchorEraId, uint64 _anchorTimestamp, uint64 _secondsPerEra) external auth(ROLE_SPEC_MANAGER) {
require(_secondsPerEra > 0, "OM: BAD_SECONDS_PER_ERA");
require(uint64(block.timestamp) >= _anchorTimestamp, "OM: BAD_TIMESTAMP");
uint64 newEra = _anchorEraId + (uint64(block.timestamp) - _anchorTimestamp) / _secondsPerEra;
require(newEra >= eraId, "OM: ERA_COLLISION");
ANCHOR_ERA_ID = _anchorEraId;
ANCHOR_TIMESTAMP = _anchorTimestamp;
SECONDS_PER_ERA = _secondsPerEra;
}
/**
* @notice Return oracle instance index in the member array
* @param _member member address
* @return member index
*/
function _getMemberId(address _member) internal view returns (uint256) {
uint256 length = members.length;
for (uint256 i = 0; i < length; ++i) {
if (members[i] == _member) {
return i;
}
}
return MEMBER_NOT_FOUND;
}
/**
* @notice Calculate current expected era id
* @dev Calculation based on relaychain genesis timestamp and era duratation
* @return current era id
*/
function _getCurrentEraId() internal view returns (uint64) {
return ANCHOR_ERA_ID + (uint64(block.timestamp) - ANCHOR_TIMESTAMP) / SECONDS_PER_ERA;
}
/**
* @notice Delete interim data for current Era, free storage memory for each oracle
*/
function _clearReporting() internal {
address[] memory ledgers = ILido(LIDO).getLedgerAddresses();
uint256 _length = ledgers.length;
for (uint256 i = 0; i < _length; ++i) {
address oracle = oracleForLedger[ledgers[i]];
if (oracle != address(0)) {
IOracle(oracle).clearReporting();
}
}
}
}