-
Notifications
You must be signed in to change notification settings - Fork 7
/
UpdateTargetValidatorLimits.sol
125 lines (103 loc) · 4.19 KB
/
UpdateTargetValidatorLimits.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
// SPDX-FileCopyrightText: 2023 Lido <info@lido.fi>
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.6;
import "../TrustedCaller.sol";
import "../libraries/EVMScriptCreator.sol";
import "../interfaces/IEVMScriptFactory.sol";
import "../interfaces/INodeOperatorsRegistry.sol";
/// @notice Creates EVMScript to set node operators target validators limit
contract UpdateTargetValidatorLimits is TrustedCaller, IEVMScriptFactory {
struct TargetValidatorsLimit {
uint256 nodeOperatorId;
uint256 targetLimitMode;
uint256 targetLimit;
}
// -------------
// CONSTANTS
// -------------
uint256 internal constant UINT64_MAX = 0xFFFFFFFFFFFFFFFF;
// -------------
// ERRORS
// -------------
string private constant ERROR_NODE_OPERATOR_INDEX_OUT_OF_RANGE =
"NODE_OPERATOR_INDEX_OUT_OF_RANGE";
string private constant ERROR_NODE_OPERATORS_IS_NOT_SORTED = "NODE_OPERATORS_IS_NOT_SORTED";
string private constant ERROR_TARGET_LIMIT_GREATER_THEN_UINT64 =
"TARGET_LIMIT_GREATER_THEN_UINT64";
string private constant ERROR_EMPTY_CALLDATA = "EMPTY_CALLDATA";
// -------------
// VARIABLES
// -------------
/// @notice Address of NodeOperatorsRegistry contract
INodeOperatorsRegistry public immutable nodeOperatorsRegistry;
// -------------
// CONSTRUCTOR
// -------------
constructor(
address _trustedCaller,
address _nodeOperatorsRegistry
) TrustedCaller(_trustedCaller) {
nodeOperatorsRegistry = INodeOperatorsRegistry(_nodeOperatorsRegistry);
}
// -------------
// EXTERNAL METHODS
// -------------
/// @notice Creates EVMScript to set node operators target validators limit
/// @param _creator Address who creates EVMScript
/// @param _evmScriptCallData Encoded (TargetValidatorsLimit[])
function createEVMScript(
address _creator,
bytes memory _evmScriptCallData
) external view override onlyTrustedCaller(_creator) returns (bytes memory) {
TargetValidatorsLimit[] memory decodedCallData = abi.decode(
_evmScriptCallData,
(TargetValidatorsLimit[])
);
_validateInputData(decodedCallData);
bytes[] memory updateTargetLimitsCallData = new bytes[](decodedCallData.length);
for (uint256 i = 0; i < decodedCallData.length; ++i) {
updateTargetLimitsCallData[i] = abi.encode(decodedCallData[i]);
}
return
EVMScriptCreator.createEVMScript(
address(nodeOperatorsRegistry),
nodeOperatorsRegistry.updateTargetValidatorsLimits.selector,
updateTargetLimitsCallData
);
}
/// @notice Decodes call data used by createEVMScript method
/// @param _evmScriptCallData Encoded (TargetValidatorsLimit[])
/// @return TargetValidatorsLimit[]
function decodeEVMScriptCallData(
bytes memory _evmScriptCallData
) external pure returns (TargetValidatorsLimit[] memory) {
return _decodeEVMScriptCallData(_evmScriptCallData);
}
// ------------------
// PRIVATE METHODS
// ------------------
function _decodeEVMScriptCallData(
bytes memory _evmScriptCallData
) private pure returns (TargetValidatorsLimit[] memory) {
return abi.decode(_evmScriptCallData, (TargetValidatorsLimit[]));
}
function _validateInputData(TargetValidatorsLimit[] memory _decodedCallData) private view {
uint256 nodeOperatorsCount = nodeOperatorsRegistry.getNodeOperatorsCount();
require(_decodedCallData.length > 0, ERROR_EMPTY_CALLDATA);
require(
_decodedCallData[_decodedCallData.length - 1].nodeOperatorId < nodeOperatorsCount,
ERROR_NODE_OPERATOR_INDEX_OUT_OF_RANGE
);
for (uint256 i = 0; i < _decodedCallData.length; ++i) {
require(
i == 0 ||
_decodedCallData[i].nodeOperatorId > _decodedCallData[i - 1].nodeOperatorId,
ERROR_NODE_OPERATORS_IS_NOT_SORTED
);
require(
_decodedCallData[i].targetLimit <= UINT64_MAX,
ERROR_TARGET_LIMIT_GREATER_THEN_UINT64
);
}
}
}