-
Notifications
You must be signed in to change notification settings - Fork 6
/
StaderOracle.sol
717 lines (636 loc) · 27.3 KB
/
StaderOracle.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
// SPDX-License-Identifier: MIT
pragma solidity 0.8.16;
import './library/UtilLib.sol';
import './interfaces/IPoolUtils.sol';
import './interfaces/IStaderOracle.sol';
import './interfaces/ISocializingPool.sol';
import './interfaces/INodeRegistry.sol';
import './interfaces/IStaderStakePoolManager.sol';
import '@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol';
import '@openzeppelin/contracts-upgradeable/security/PausableUpgradeable.sol';
import '@openzeppelin/contracts-upgradeable/access/AccessControlUpgradeable.sol';
import '@openzeppelin/contracts-upgradeable/security/ReentrancyGuardUpgradeable.sol';
contract StaderOracle is IStaderOracle, AccessControlUpgradeable, PausableUpgradeable, ReentrancyGuardUpgradeable {
bool public override erInspectionMode;
bool public override isPORFeedBasedERData;
SDPriceData public lastReportedSDPriceData;
IStaderConfig public override staderConfig;
ExchangeRate public inspectionModeExchangeRate;
ExchangeRate public exchangeRate;
ValidatorStats public validatorStats;
uint256 public constant MAX_ER_UPDATE_FREQUENCY = 7200 * 7; // 7 days
uint256 public constant ER_CHANGE_MAX_BPS = 10000;
uint256 public override erChangeLimit;
uint256 public constant MIN_TRUSTED_NODES = 5;
/// @inheritdoc IStaderOracle
uint256 public override reportingBlockNumberForWithdrawnValidators;
/// @inheritdoc IStaderOracle
uint256 public override trustedNodesCount;
/// @inheritdoc IStaderOracle
uint256 public override lastReportedMAPDIndex;
uint256 public override erInspectionModeStartBlock;
// indicate the health of protocol on beacon chain
// enabled by `MANAGER` if heavy slashing on protocol on beacon chain
bool public override safeMode;
/// @inheritdoc IStaderOracle
mapping(address => bool) public override isTrustedNode;
mapping(bytes32 => bool) private nodeSubmissionKeys;
mapping(bytes32 => uint8) private submissionCountKeys;
mapping(bytes32 => uint16) public override missedAttestationPenalty;
uint256[] private sdPrices;
bytes32 public constant ETHX_ER_UF = keccak256('ETHX_ER_UF'); // ETHx Exchange Rate, Balances Update Frequency
bytes32 public constant SD_PRICE_UF = keccak256('SD_PRICE_UF'); // SD Price Update Frequency Key
bytes32 public constant VALIDATOR_STATS_UF = keccak256('VALIDATOR_STATS_UF'); // Validator Status Update Frequency Key
bytes32 public constant WITHDRAWN_VALIDATORS_UF = keccak256('WITHDRAWN_VALIDATORS_UF'); // Withdrawn Validator Update Frequency Key
bytes32 public constant MISSED_ATTESTATION_PENALTY_UF = keccak256('MISSED_ATTESTATION_PENALTY_UF'); // Missed Attestation Penalty Update Frequency Key
mapping(bytes32 => uint256) public updateFrequencyMap;
/// @custom:oz-upgrades-unsafe-allow constructor
constructor() {
_disableInitializers();
}
function initialize(address _admin, address _staderConfig) public initializer {
UtilLib.checkNonZeroAddress(_admin);
UtilLib.checkNonZeroAddress(_staderConfig);
__AccessControl_init();
__Pausable_init();
__ReentrancyGuard_init();
erChangeLimit = 500; //5% deviation threshold
setUpdateFrequency(ETHX_ER_UF, 7200);
setUpdateFrequency(SD_PRICE_UF, 7200);
setUpdateFrequency(VALIDATOR_STATS_UF, 7200);
setUpdateFrequency(WITHDRAWN_VALIDATORS_UF, 14400);
setUpdateFrequency(MISSED_ATTESTATION_PENALTY_UF, 50400);
staderConfig = IStaderConfig(_staderConfig);
_grantRole(DEFAULT_ADMIN_ROLE, _admin);
emit UpdatedStaderConfig(_staderConfig);
}
/// @inheritdoc IStaderOracle
function addTrustedNode(address _nodeAddress) external override {
UtilLib.onlyManagerRole(msg.sender, staderConfig);
UtilLib.checkNonZeroAddress(_nodeAddress);
if (isTrustedNode[_nodeAddress]) {
revert NodeAlreadyTrusted();
}
isTrustedNode[_nodeAddress] = true;
trustedNodesCount++;
emit TrustedNodeAdded(_nodeAddress);
}
/// @inheritdoc IStaderOracle
function removeTrustedNode(address _nodeAddress) external override {
UtilLib.onlyManagerRole(msg.sender, staderConfig);
UtilLib.checkNonZeroAddress(_nodeAddress);
if (!isTrustedNode[_nodeAddress]) {
revert NodeNotTrusted();
}
isTrustedNode[_nodeAddress] = false;
trustedNodesCount--;
emit TrustedNodeRemoved(_nodeAddress);
}
/// @inheritdoc IStaderOracle
function submitExchangeRateData(ExchangeRate calldata _exchangeRate)
external
override
trustedNodeOnly
checkMinTrustedNodes
checkERInspectionMode
whenNotPaused
{
if (isPORFeedBasedERData) {
revert InvalidERDataSource();
}
if (_exchangeRate.reportingBlockNumber >= block.number) {
revert ReportingFutureBlockData();
}
if (_exchangeRate.reportingBlockNumber % updateFrequencyMap[ETHX_ER_UF] > 0) {
revert InvalidReportingBlock();
}
// Get submission keys
bytes32 nodeSubmissionKey = keccak256(
abi.encode(
msg.sender,
_exchangeRate.reportingBlockNumber,
_exchangeRate.totalETHBalance,
_exchangeRate.totalETHXSupply
)
);
bytes32 submissionCountKey = keccak256(
abi.encode(_exchangeRate.reportingBlockNumber, _exchangeRate.totalETHBalance, _exchangeRate.totalETHXSupply)
);
uint8 submissionCount = attestSubmission(nodeSubmissionKey, submissionCountKey);
// Emit balances submitted event
emit ExchangeRateSubmitted(
msg.sender,
_exchangeRate.reportingBlockNumber,
_exchangeRate.totalETHBalance,
_exchangeRate.totalETHXSupply,
block.timestamp
);
if (
submissionCount == trustedNodesCount / 2 + 1 &&
_exchangeRate.reportingBlockNumber > exchangeRate.reportingBlockNumber
) {
updateWithInLimitER(
_exchangeRate.totalETHBalance,
_exchangeRate.totalETHXSupply,
_exchangeRate.reportingBlockNumber
);
}
}
/// @inheritdoc IStaderOracle
function updateERFromPORFeed() external override checkERInspectionMode whenNotPaused {
if (!isPORFeedBasedERData) {
revert InvalidERDataSource();
}
(uint256 newTotalETHBalance, uint256 newTotalETHXSupply, uint256 reportingBlockNumber) = getPORFeedData();
updateWithInLimitER(newTotalETHBalance, newTotalETHXSupply, reportingBlockNumber);
}
/**
* @notice update the exchange rate when er change limit crossed, after verifying `inspectionModeExchangeRate` data
* @dev `erInspectionMode` must be true to call this function
*/
function closeERInspectionMode() external override whenNotPaused {
if (!erInspectionMode) {
revert ERChangeLimitNotCrossed();
}
disableERInspectionMode();
_updateExchangeRate(
inspectionModeExchangeRate.totalETHBalance,
inspectionModeExchangeRate.totalETHXSupply,
inspectionModeExchangeRate.reportingBlockNumber
);
}
// turn off erInspectionMode if `inspectionModeExchangeRate` is incorrect so that oracle/POR can push new data
function disableERInspectionMode() public override whenNotPaused {
if (
!staderConfig.onlyManagerRole(msg.sender) &&
erInspectionModeStartBlock + MAX_ER_UPDATE_FREQUENCY > block.number
) {
revert CooldownNotComplete();
}
erInspectionMode = false;
}
/// @notice submits merkle root and handles reward
/// sends user rewards to Stader Stake Pool Manager
/// sends protocol rewards to stader treasury
/// updates operator reward balances on socializing pool
/// @param _rewardsData contains rewards merkleRoot and rewards split info
/// @dev _rewardsData.index should not be zero
function submitSocializingRewardsMerkleRoot(RewardsData calldata _rewardsData)
external
override
trustedNodeOnly
checkMinTrustedNodes
whenNotPaused
nonReentrant
{
if (_rewardsData.reportingBlockNumber >= block.number) {
revert ReportingFutureBlockData();
}
if (_rewardsData.reportingBlockNumber != getMerkleRootReportableBlockByPoolId(_rewardsData.poolId)) {
revert InvalidReportingBlock();
}
if (_rewardsData.index != getCurrentRewardsIndexByPoolId(_rewardsData.poolId)) {
revert InvalidMerkleRootIndex();
}
// Get submission keys
bytes32 nodeSubmissionKey = keccak256(
abi.encode(
msg.sender,
_rewardsData.index,
_rewardsData.merkleRoot,
_rewardsData.poolId,
_rewardsData.operatorETHRewards,
_rewardsData.userETHRewards,
_rewardsData.protocolETHRewards,
_rewardsData.operatorSDRewards
)
);
bytes32 submissionCountKey = keccak256(
abi.encode(
_rewardsData.index,
_rewardsData.merkleRoot,
_rewardsData.poolId,
_rewardsData.operatorETHRewards,
_rewardsData.userETHRewards,
_rewardsData.protocolETHRewards,
_rewardsData.operatorSDRewards
)
);
// Emit merkle root submitted event
emit SocializingRewardsMerkleRootSubmitted(
msg.sender,
_rewardsData.index,
_rewardsData.merkleRoot,
_rewardsData.poolId,
block.number
);
uint8 submissionCount = attestSubmission(nodeSubmissionKey, submissionCountKey);
if ((submissionCount == trustedNodesCount / 2 + 1)) {
address socializingPool = IPoolUtils(staderConfig.getPoolUtils()).getSocializingPoolAddress(
_rewardsData.poolId
);
ISocializingPool(socializingPool).handleRewards(_rewardsData);
emit SocializingRewardsMerkleRootUpdated(
_rewardsData.index,
_rewardsData.merkleRoot,
_rewardsData.poolId,
block.number
);
}
}
function submitSDPrice(SDPriceData calldata _sdPriceData) external override trustedNodeOnly checkMinTrustedNodes {
if (_sdPriceData.reportingBlockNumber >= block.number) {
revert ReportingFutureBlockData();
}
if (_sdPriceData.reportingBlockNumber % updateFrequencyMap[SD_PRICE_UF] > 0) {
revert InvalidReportingBlock();
}
if (_sdPriceData.reportingBlockNumber <= lastReportedSDPriceData.reportingBlockNumber) {
revert ReportingPreviousCycleData();
}
// Get submission keys
bytes32 nodeSubmissionKey = keccak256(abi.encode(msg.sender, _sdPriceData.reportingBlockNumber));
bytes32 submissionCountKey = keccak256(abi.encode(_sdPriceData.reportingBlockNumber));
uint8 submissionCount = attestSubmission(nodeSubmissionKey, submissionCountKey);
insertSDPrice(_sdPriceData.sdPriceInETH);
// Emit SD Price submitted event
emit SDPriceSubmitted(msg.sender, _sdPriceData.sdPriceInETH, _sdPriceData.reportingBlockNumber, block.number);
// price can be derived once more than 66% percent oracles have submitted price
if ((submissionCount == (2 * trustedNodesCount) / 3 + 1)) {
lastReportedSDPriceData = _sdPriceData;
lastReportedSDPriceData.sdPriceInETH = getMedianValue(sdPrices);
delete sdPrices;
// Emit SD Price updated event
emit SDPriceUpdated(_sdPriceData.sdPriceInETH, _sdPriceData.reportingBlockNumber, block.number);
}
}
function insertSDPrice(uint256 _sdPrice) internal {
sdPrices.push(_sdPrice);
if (sdPrices.length == 1) return;
uint256 j = sdPrices.length - 1;
while ((j >= 1) && (_sdPrice < sdPrices[j - 1])) {
sdPrices[j] = sdPrices[j - 1];
j--;
}
sdPrices[j] = _sdPrice;
}
function getMedianValue(uint256[] storage dataArray) internal view returns (uint256 _medianValue) {
uint256 len = dataArray.length;
return (dataArray[(len - 1) / 2] + dataArray[len / 2]) / 2;
}
/// @inheritdoc IStaderOracle
function submitValidatorStats(ValidatorStats calldata _validatorStats)
external
override
trustedNodeOnly
checkMinTrustedNodes
whenNotPaused
{
if (_validatorStats.reportingBlockNumber >= block.number) {
revert ReportingFutureBlockData();
}
if (_validatorStats.reportingBlockNumber % updateFrequencyMap[VALIDATOR_STATS_UF] > 0) {
revert InvalidReportingBlock();
}
// Get submission keys
bytes32 nodeSubmissionKey = keccak256(
abi.encode(
msg.sender,
_validatorStats.reportingBlockNumber,
_validatorStats.exitingValidatorsBalance,
_validatorStats.exitedValidatorsBalance,
_validatorStats.slashedValidatorsBalance,
_validatorStats.exitingValidatorsCount,
_validatorStats.exitedValidatorsCount,
_validatorStats.slashedValidatorsCount
)
);
bytes32 submissionCountKey = keccak256(
abi.encode(
_validatorStats.reportingBlockNumber,
_validatorStats.exitingValidatorsBalance,
_validatorStats.exitedValidatorsBalance,
_validatorStats.slashedValidatorsBalance,
_validatorStats.exitingValidatorsCount,
_validatorStats.exitedValidatorsCount,
_validatorStats.slashedValidatorsCount
)
);
uint8 submissionCount = attestSubmission(nodeSubmissionKey, submissionCountKey);
// Emit validator stats submitted event
emit ValidatorStatsSubmitted(
msg.sender,
_validatorStats.reportingBlockNumber,
_validatorStats.exitingValidatorsBalance,
_validatorStats.exitedValidatorsBalance,
_validatorStats.slashedValidatorsBalance,
_validatorStats.exitingValidatorsCount,
_validatorStats.exitedValidatorsCount,
_validatorStats.slashedValidatorsCount,
block.timestamp
);
if (
submissionCount == trustedNodesCount / 2 + 1 &&
_validatorStats.reportingBlockNumber > validatorStats.reportingBlockNumber
) {
validatorStats = _validatorStats;
// Emit stats updated event
emit ValidatorStatsUpdated(
_validatorStats.reportingBlockNumber,
_validatorStats.exitingValidatorsBalance,
_validatorStats.exitedValidatorsBalance,
_validatorStats.slashedValidatorsBalance,
_validatorStats.exitingValidatorsCount,
_validatorStats.exitedValidatorsCount,
_validatorStats.slashedValidatorsCount,
block.timestamp
);
}
}
/// @inheritdoc IStaderOracle
function submitWithdrawnValidators(WithdrawnValidators calldata _withdrawnValidators)
external
override
trustedNodeOnly
checkMinTrustedNodes
whenNotPaused
nonReentrant
{
if (_withdrawnValidators.reportingBlockNumber >= block.number) {
revert ReportingFutureBlockData();
}
if (_withdrawnValidators.reportingBlockNumber % updateFrequencyMap[WITHDRAWN_VALIDATORS_UF] > 0) {
revert InvalidReportingBlock();
}
bytes memory encodedPubkeys = abi.encode(_withdrawnValidators.sortedPubkeys);
// Get submission keys
bytes32 nodeSubmissionKey = keccak256(
abi.encode(
msg.sender,
_withdrawnValidators.reportingBlockNumber,
_withdrawnValidators.nodeRegistry,
encodedPubkeys
)
);
bytes32 submissionCountKey = keccak256(
abi.encode(_withdrawnValidators.reportingBlockNumber, _withdrawnValidators.nodeRegistry, encodedPubkeys)
);
uint8 submissionCount = attestSubmission(nodeSubmissionKey, submissionCountKey);
// Emit withdrawn validators submitted event
emit WithdrawnValidatorsSubmitted(
msg.sender,
_withdrawnValidators.reportingBlockNumber,
_withdrawnValidators.nodeRegistry,
_withdrawnValidators.sortedPubkeys,
block.timestamp
);
if (
submissionCount == trustedNodesCount / 2 + 1 &&
_withdrawnValidators.reportingBlockNumber > reportingBlockNumberForWithdrawnValidators
) {
reportingBlockNumberForWithdrawnValidators = _withdrawnValidators.reportingBlockNumber;
INodeRegistry(_withdrawnValidators.nodeRegistry).withdrawnValidators(_withdrawnValidators.sortedPubkeys);
// Emit withdrawn validators updated event
emit WithdrawnValidatorsUpdated(
_withdrawnValidators.reportingBlockNumber,
_withdrawnValidators.nodeRegistry,
_withdrawnValidators.sortedPubkeys,
block.timestamp
);
}
}
/// @inheritdoc IStaderOracle
function submitMissedAttestationPenalties(MissedAttestationPenaltyData calldata _mapd)
external
override
trustedNodeOnly
checkMinTrustedNodes
whenNotPaused
{
if (_mapd.reportingBlockNumber >= block.number) {
revert ReportingFutureBlockData();
}
if (_mapd.reportingBlockNumber != getMissedAttestationPenaltyReportableBlock()) {
revert InvalidReportingBlock();
}
if (_mapd.index != lastReportedMAPDIndex + 1) {
revert InvalidMAPDIndex();
}
bytes memory encodedPubkeys = abi.encode(_mapd.sortedPubkeys);
// Get submission keys
bytes32 nodeSubmissionKey = keccak256(abi.encode(msg.sender, _mapd.index, encodedPubkeys));
bytes32 submissionCountKey = keccak256(abi.encode(_mapd.index, encodedPubkeys));
uint8 submissionCount = attestSubmission(nodeSubmissionKey, submissionCountKey);
// Emit missed attestation penalty submitted event
emit MissedAttestationPenaltySubmitted(
msg.sender,
_mapd.index,
block.number,
_mapd.reportingBlockNumber,
_mapd.sortedPubkeys
);
if ((submissionCount == trustedNodesCount / 2 + 1)) {
lastReportedMAPDIndex = _mapd.index;
uint256 keyCount = _mapd.sortedPubkeys.length;
for (uint256 i; i < keyCount; ) {
bytes32 pubkeyRoot = UtilLib.getPubkeyRoot(_mapd.sortedPubkeys[i]);
missedAttestationPenalty[pubkeyRoot]++;
unchecked {
++i;
}
}
emit MissedAttestationPenaltyUpdated(_mapd.index, block.number, _mapd.sortedPubkeys);
}
}
/// @inheritdoc IStaderOracle
function enableSafeMode() external override {
UtilLib.onlyManagerRole(msg.sender, staderConfig);
safeMode = true;
emit SafeModeEnabled();
}
function disableSafeMode() external override onlyRole(DEFAULT_ADMIN_ROLE) {
safeMode = false;
emit SafeModeDisabled();
}
//update the address of staderConfig
function updateStaderConfig(address _staderConfig) external onlyRole(DEFAULT_ADMIN_ROLE) {
UtilLib.checkNonZeroAddress(_staderConfig);
staderConfig = IStaderConfig(_staderConfig);
emit UpdatedStaderConfig(_staderConfig);
}
function setERUpdateFrequency(uint256 _updateFrequency) external override {
UtilLib.onlyManagerRole(msg.sender, staderConfig);
if (_updateFrequency > MAX_ER_UPDATE_FREQUENCY) {
revert InvalidUpdate();
}
setUpdateFrequency(ETHX_ER_UF, _updateFrequency);
}
function togglePORFeedBasedERData() external override checkERInspectionMode {
UtilLib.onlyManagerRole(msg.sender, staderConfig);
isPORFeedBasedERData = !isPORFeedBasedERData;
emit ERDataSourceToggled(isPORFeedBasedERData);
}
//update the deviation threshold value, 0 deviationThreshold not allowed
function updateERChangeLimit(uint256 _erChangeLimit) external override {
UtilLib.onlyManagerRole(msg.sender, staderConfig);
if (_erChangeLimit == 0 || _erChangeLimit > ER_CHANGE_MAX_BPS) {
revert ERPermissibleChangeOutofBounds();
}
erChangeLimit = _erChangeLimit;
emit UpdatedERChangeLimit(erChangeLimit);
}
function setSDPriceUpdateFrequency(uint256 _updateFrequency) external override {
UtilLib.onlyManagerRole(msg.sender, staderConfig);
setUpdateFrequency(SD_PRICE_UF, _updateFrequency);
}
function setValidatorStatsUpdateFrequency(uint256 _updateFrequency) external override {
UtilLib.onlyManagerRole(msg.sender, staderConfig);
setUpdateFrequency(VALIDATOR_STATS_UF, _updateFrequency);
}
function setWithdrawnValidatorsUpdateFrequency(uint256 _updateFrequency) external override {
UtilLib.onlyManagerRole(msg.sender, staderConfig);
setUpdateFrequency(WITHDRAWN_VALIDATORS_UF, _updateFrequency);
}
function setMissedAttestationPenaltyUpdateFrequency(uint256 _updateFrequency) external override {
UtilLib.onlyManagerRole(msg.sender, staderConfig);
setUpdateFrequency(MISSED_ATTESTATION_PENALTY_UF, _updateFrequency);
}
function setUpdateFrequency(bytes32 _key, uint256 _updateFrequency) internal {
if (_updateFrequency == 0) {
revert ZeroFrequency();
}
if (_updateFrequency == updateFrequencyMap[_key]) {
revert FrequencyUnchanged();
}
updateFrequencyMap[_key] = _updateFrequency;
emit UpdateFrequencyUpdated(_updateFrequency);
}
function getERReportableBlock() public view override returns (uint256) {
return getReportableBlockFor(ETHX_ER_UF);
}
function getMerkleRootReportableBlockByPoolId(uint8 _poolId) public view override returns (uint256) {
(, , uint256 currentEndBlock) = ISocializingPool(
IPoolUtils(staderConfig.getPoolUtils()).getSocializingPoolAddress(_poolId)
).getRewardDetails();
return currentEndBlock;
}
function getSDPriceReportableBlock() public view override returns (uint256) {
return getReportableBlockFor(SD_PRICE_UF);
}
function getValidatorStatsReportableBlock() public view override returns (uint256) {
return getReportableBlockFor(VALIDATOR_STATS_UF);
}
function getWithdrawnValidatorReportableBlock() public view override returns (uint256) {
return getReportableBlockFor(WITHDRAWN_VALIDATORS_UF);
}
function getMissedAttestationPenaltyReportableBlock() public view override returns (uint256) {
return getReportableBlockFor(MISSED_ATTESTATION_PENALTY_UF);
}
function getReportableBlockFor(bytes32 _key) internal view returns (uint256) {
uint256 updateFrequency = updateFrequencyMap[_key];
if (updateFrequency == 0) {
revert UpdateFrequencyNotSet();
}
return (block.number / updateFrequency) * updateFrequency;
}
function getCurrentRewardsIndexByPoolId(uint8 _poolId) public view returns (uint256) {
return
ISocializingPool(IPoolUtils(staderConfig.getPoolUtils()).getSocializingPoolAddress(_poolId))
.getCurrentRewardsIndex();
}
function getValidatorStats() external view override returns (ValidatorStats memory) {
return (validatorStats);
}
function getExchangeRate() external view override returns (ExchangeRate memory) {
return (exchangeRate);
}
function attestSubmission(bytes32 _nodeSubmissionKey, bytes32 _submissionCountKey)
internal
returns (uint8 _submissionCount)
{
// Check & update node submission status
if (nodeSubmissionKeys[_nodeSubmissionKey]) {
revert DuplicateSubmissionFromNode();
}
nodeSubmissionKeys[_nodeSubmissionKey] = true;
submissionCountKeys[_submissionCountKey]++;
_submissionCount = submissionCountKeys[_submissionCountKey];
}
function getSDPriceInETH() external view override returns (uint256) {
return lastReportedSDPriceData.sdPriceInETH;
}
function getPORFeedData()
internal
view
returns (
uint256,
uint256,
uint256
)
{
(, int256 totalETHBalanceInInt, , , ) = AggregatorV3Interface(staderConfig.getETHBalancePORFeedProxy())
.latestRoundData();
(, int256 totalETHXSupplyInInt, , , ) = AggregatorV3Interface(staderConfig.getETHXSupplyPORFeedProxy())
.latestRoundData();
return (uint256(totalETHBalanceInInt), uint256(totalETHXSupplyInInt), block.number);
}
function updateWithInLimitER(
uint256 _newTotalETHBalance,
uint256 _newTotalETHXSupply,
uint256 _reportingBlockNumber
) internal {
uint256 currentExchangeRate = UtilLib.computeExchangeRate(
exchangeRate.totalETHBalance,
exchangeRate.totalETHXSupply,
staderConfig
);
uint256 newExchangeRate = UtilLib.computeExchangeRate(_newTotalETHBalance, _newTotalETHXSupply, staderConfig);
if (
!(newExchangeRate >= (currentExchangeRate * (ER_CHANGE_MAX_BPS - erChangeLimit)) / ER_CHANGE_MAX_BPS &&
newExchangeRate <= ((currentExchangeRate * (ER_CHANGE_MAX_BPS + erChangeLimit)) / ER_CHANGE_MAX_BPS))
) {
erInspectionMode = true;
erInspectionModeStartBlock = block.number;
inspectionModeExchangeRate.totalETHBalance = _newTotalETHBalance;
inspectionModeExchangeRate.totalETHXSupply = _newTotalETHXSupply;
inspectionModeExchangeRate.reportingBlockNumber = _reportingBlockNumber;
emit ERInspectionModeActivated(erInspectionMode, block.timestamp);
return;
}
_updateExchangeRate(_newTotalETHBalance, _newTotalETHXSupply, _reportingBlockNumber);
}
function _updateExchangeRate(
uint256 _totalETHBalance,
uint256 _totalETHXSupply,
uint256 _reportingBlockNumber
) internal {
exchangeRate.totalETHBalance = _totalETHBalance;
exchangeRate.totalETHXSupply = _totalETHXSupply;
exchangeRate.reportingBlockNumber = _reportingBlockNumber;
// Emit balances updated event
emit ExchangeRateUpdated(
exchangeRate.reportingBlockNumber,
exchangeRate.totalETHBalance,
exchangeRate.totalETHXSupply,
block.timestamp
);
}
modifier checkERInspectionMode() {
if (erInspectionMode) {
revert InspectionModeActive();
}
_;
}
modifier trustedNodeOnly() {
if (!isTrustedNode[msg.sender]) {
revert NotATrustedNode();
}
_;
}
modifier checkMinTrustedNodes() {
if (trustedNodesCount < MIN_TRUSTED_NODES) {
revert InsufficientTrustedNodes();
}
_;
}
}