forked from lidofinance/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NodeOperatorsRegistry.sol
637 lines (530 loc) · 25.2 KB
/
NodeOperatorsRegistry.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
// SPDX-FileCopyrightText: 2020 Lido <[email protected]>
// SPDX-License-Identifier: GPL-3.0
/* See contracts/COMPILERS.md */
pragma solidity 0.4.24;
import "@aragon/os/contracts/apps/AragonApp.sol";
import "@aragon/os/contracts/common/IsContract.sol";
import "@aragon/os/contracts/lib/math/SafeMath.sol";
import "@aragon/os/contracts/lib/math/SafeMath64.sol";
import "solidity-bytes-utils/contracts/BytesLib.sol";
import "../interfaces/INodeOperatorsRegistry.sol";
import "../lib/MemUtils.sol";
/**
* @title Node Operator registry implementation
*
* See the comment of `INodeOperatorsRegistry`.
*
* NOTE: the code below assumes moderate amount of node operators, e.g. up to 50.
*/
contract NodeOperatorsRegistry is INodeOperatorsRegistry, IsContract, AragonApp {
using SafeMath for uint256;
using SafeMath64 for uint64;
using UnstructuredStorage for bytes32;
/// ACL
bytes32 constant public MANAGE_SIGNING_KEYS = keccak256("MANAGE_SIGNING_KEYS");
bytes32 constant public ADD_NODE_OPERATOR_ROLE = keccak256("ADD_NODE_OPERATOR_ROLE");
bytes32 constant public SET_NODE_OPERATOR_ACTIVE_ROLE = keccak256("SET_NODE_OPERATOR_ACTIVE_ROLE");
bytes32 constant public SET_NODE_OPERATOR_NAME_ROLE = keccak256("SET_NODE_OPERATOR_NAME_ROLE");
bytes32 constant public SET_NODE_OPERATOR_ADDRESS_ROLE = keccak256("SET_NODE_OPERATOR_ADDRESS_ROLE");
bytes32 constant public SET_NODE_OPERATOR_LIMIT_ROLE = keccak256("SET_NODE_OPERATOR_LIMIT_ROLE");
bytes32 constant public REPORT_STOPPED_VALIDATORS_ROLE = keccak256("REPORT_STOPPED_VALIDATORS_ROLE");
uint256 constant public PUBKEY_LENGTH = 48;
uint256 constant public SIGNATURE_LENGTH = 96;
uint256 internal constant UINT64_MAX = uint256(uint64(-1));
bytes32 internal constant SIGNING_KEYS_MAPPING_NAME = keccak256("lido.NodeOperatorsRegistry.signingKeysMappingName");
/// @dev Node Operator parameters and internal state
struct NodeOperator {
bool active; // a flag indicating if the operator can participate in further staking and reward distribution
address rewardAddress; // Ethereum 1 address which receives steth rewards for this operator
string name; // human-readable name
uint64 stakingLimit; // the maximum number of validators to stake for this operator
uint64 stoppedValidators; // number of signing keys which stopped validation (e.g. were slashed)
uint64 totalSigningKeys; // total amount of signing keys of this operator
uint64 usedSigningKeys; // number of signing keys of this operator which were used in deposits to the Ethereum 2
}
/// @dev Memory cache entry used in the assignNextKeys function
struct DepositLookupCacheEntry {
// Makes no sense to pack types since reading memory is as fast as any op
uint256 id;
uint256 stakingLimit;
uint256 stoppedValidators;
uint256 totalSigningKeys;
uint256 usedSigningKeys;
uint256 initialUsedSigningKeys;
}
/// @dev Mapping of all node operators. Mapping is used to be able to extend the struct.
mapping(uint256 => NodeOperator) internal operators;
// @dev Total number of operators
bytes32 internal constant TOTAL_OPERATORS_COUNT_POSITION = keccak256("lido.NodeOperatorsRegistry.totalOperatorsCount");
// @dev Cached number of active operators
bytes32 internal constant ACTIVE_OPERATORS_COUNT_POSITION = keccak256("lido.NodeOperatorsRegistry.activeOperatorsCount");
/// @dev link to the Lido contract
bytes32 internal constant LIDO_POSITION = keccak256("lido.NodeOperatorsRegistry.lido");
modifier onlyLido() {
require(msg.sender == LIDO_POSITION.getStorageAddress(), "APP_AUTH_FAILED");
_;
}
modifier validAddress(address _a) {
require(_a != address(0), "EMPTY_ADDRESS");
_;
}
modifier operatorExists(uint256 _id) {
require(_id < getNodeOperatorsCount(), "NODE_OPERATOR_NOT_FOUND");
_;
}
function initialize(address _lido) public onlyInit {
TOTAL_OPERATORS_COUNT_POSITION.setStorageUint256(0);
ACTIVE_OPERATORS_COUNT_POSITION.setStorageUint256(0);
LIDO_POSITION.setStorageAddress(_lido);
initialized();
}
/**
* @notice Add node operator named `_name` with reward address `_rewardAddress` and staking limit `_stakingLimit`
* @param _name Human-readable name
* @param _rewardAddress Ethereum 1 address which receives stETH rewards for this operator
* @param _stakingLimit the maximum number of validators to stake for this operator
* @return a unique key of the added operator
*/
function addNodeOperator(string _name, address _rewardAddress, uint64 _stakingLimit) external
auth(ADD_NODE_OPERATOR_ROLE)
validAddress(_rewardAddress)
returns (uint256 id)
{
id = getNodeOperatorsCount();
TOTAL_OPERATORS_COUNT_POSITION.setStorageUint256(id.add(1));
NodeOperator storage operator = operators[id];
uint256 activeOperatorsCount = getActiveNodeOperatorsCount();
ACTIVE_OPERATORS_COUNT_POSITION.setStorageUint256(activeOperatorsCount.add(1));
operator.active = true;
operator.name = _name;
operator.rewardAddress = _rewardAddress;
operator.stakingLimit = _stakingLimit;
emit NodeOperatorAdded(id, _name, _rewardAddress, _stakingLimit);
return id;
}
/**
* @notice `_active ? 'Enable' : 'Disable'` the node operator #`_id`
*/
function setNodeOperatorActive(uint256 _id, bool _active) external
authP(SET_NODE_OPERATOR_ACTIVE_ROLE, arr(_id, _active ? uint256(1) : uint256(0)))
operatorExists(_id)
{
if (operators[_id].active != _active) {
uint256 activeOperatorsCount = getActiveNodeOperatorsCount();
if (_active)
ACTIVE_OPERATORS_COUNT_POSITION.setStorageUint256(activeOperatorsCount.add(1));
else
ACTIVE_OPERATORS_COUNT_POSITION.setStorageUint256(activeOperatorsCount.sub(1));
}
operators[_id].active = _active;
emit NodeOperatorActiveSet(_id, _active);
}
/**
* @notice Change human-readable name of the node operator #`_id` to `_name`
*/
function setNodeOperatorName(uint256 _id, string _name) external
authP(SET_NODE_OPERATOR_NAME_ROLE, arr(_id))
operatorExists(_id)
{
operators[_id].name = _name;
emit NodeOperatorNameSet(_id, _name);
}
/**
* @notice Change reward address of the node operator #`_id` to `_rewardAddress`
*/
function setNodeOperatorRewardAddress(uint256 _id, address _rewardAddress) external
authP(SET_NODE_OPERATOR_ADDRESS_ROLE, arr(_id, uint256(_rewardAddress)))
operatorExists(_id)
validAddress(_rewardAddress)
{
operators[_id].rewardAddress = _rewardAddress;
emit NodeOperatorRewardAddressSet(_id, _rewardAddress);
}
/**
* @notice Set the maximum number of validators to stake for the node operator #`_id` to `_stakingLimit`
*/
function setNodeOperatorStakingLimit(uint256 _id, uint64 _stakingLimit) external
authP(SET_NODE_OPERATOR_LIMIT_ROLE, arr(_id, uint256(_stakingLimit)))
operatorExists(_id)
{
operators[_id].stakingLimit = _stakingLimit;
emit NodeOperatorStakingLimitSet(_id, _stakingLimit);
}
/**
* @notice Report `_stoppedIncrement` more stopped validators of the node operator #`_id`
*/
function reportStoppedValidators(uint256 _id, uint64 _stoppedIncrement) external
authP(REPORT_STOPPED_VALIDATORS_ROLE, arr(_id, uint256(_stoppedIncrement)))
operatorExists(_id)
{
require(0 != _stoppedIncrement, "EMPTY_VALUE");
operators[_id].stoppedValidators = operators[_id].stoppedValidators.add(_stoppedIncrement);
require(operators[_id].stoppedValidators <= operators[_id].usedSigningKeys, "STOPPED_MORE_THAN_LAUNCHED");
emit NodeOperatorTotalStoppedValidatorsReported(_id, operators[_id].stoppedValidators);
}
/**
* @notice Remove unused signing keys
* @dev Function is used by the Lido contract
*/
function trimUnusedKeys() external onlyLido {
uint256 length = getNodeOperatorsCount();
for (uint256 operatorId = 0; operatorId < length; ++operatorId) {
if (operators[operatorId].totalSigningKeys != operators[operatorId].usedSigningKeys) // write only if update is needed
operators[operatorId].totalSigningKeys = operators[operatorId].usedSigningKeys; // discard unused keys
}
}
/**
* @notice Add `_quantity` validator signing keys of operator #`_id` to the set of usable keys. Concatenated keys are: `_pubkeys`. Can be done by the DAO in question by using the designated rewards address.
* @dev Along with each key the DAO has to provide a signatures for the
* (pubkey, withdrawal_credentials, 32000000000) message.
* Given that information, the contract'll be able to call
* deposit_contract.deposit on-chain.
* @param _operator_id Node Operator id
* @param _quantity Number of signing keys provided
* @param _pubkeys Several concatenated validator signing keys
* @param _signatures Several concatenated signatures for (pubkey, withdrawal_credentials, 32000000000) messages
*/
function addSigningKeys(uint256 _operator_id, uint256 _quantity, bytes _pubkeys, bytes _signatures) external
authP(MANAGE_SIGNING_KEYS, arr(_operator_id))
{
_addSigningKeys(_operator_id, _quantity, _pubkeys, _signatures);
}
/**
* @notice Add `_quantity` validator signing keys of operator #`_id` to the set of usable keys. Concatenated keys are: `_pubkeys`. Can be done by node operator in question by using the designated rewards address.
* @dev Along with each key the DAO has to provide a signatures for the
* (pubkey, withdrawal_credentials, 32000000000) message.
* Given that information, the contract'll be able to call
* deposit_contract.deposit on-chain.
* @param _operator_id Node Operator id
* @param _quantity Number of signing keys provided
* @param _pubkeys Several concatenated validator signing keys
* @param _signatures Several concatenated signatures for (pubkey, withdrawal_credentials, 32000000000) messages
*/
function addSigningKeysOperatorBH(
uint256 _operator_id,
uint256 _quantity,
bytes _pubkeys,
bytes _signatures
)
external
{
require(msg.sender == operators[_operator_id].rewardAddress, "APP_AUTH_FAILED");
_addSigningKeys(_operator_id, _quantity, _pubkeys, _signatures);
}
/**
* @notice Removes a validator signing key #`_index` of operator #`_id` from the set of usable keys. Executed on behalf of DAO.
* @param _operator_id Node Operator id
* @param _index Index of the key, starting with 0
*/
function removeSigningKey(uint256 _operator_id, uint256 _index)
external
authP(MANAGE_SIGNING_KEYS, arr(_operator_id))
{
_removeSigningKey(_operator_id, _index);
}
/**
* @notice Removes a validator signing key #`_index` of operator #`_id` from the set of usable keys. Executed on behalf of Node Operator.
* @param _operator_id Node Operator id
* @param _index Index of the key, starting with 0
*/
function removeSigningKeyOperatorBH(uint256 _operator_id, uint256 _index) external {
require(msg.sender == operators[_operator_id].rewardAddress, "APP_AUTH_FAILED");
_removeSigningKey(_operator_id, _index);
}
/**
* @notice Selects and returns at most `_numKeys` signing keys (as well as the corresponding
* signatures) from the set of active keys and marks the selected keys as used.
* May only be called by the Lido contract.
*
* @param _numKeys The number of keys to select. The actual number of selected keys may be less
* due to the lack of active keys.
*/
function assignNextSigningKeys(uint256 _numKeys) external onlyLido returns (bytes memory pubkeys, bytes memory signatures) {
// Memory is very cheap, although you don't want to grow it too much
DepositLookupCacheEntry[] memory cache = _loadOperatorCache();
if (0 == cache.length)
return (new bytes(0), new bytes(0));
uint256 numAssignedKeys = 0;
DepositLookupCacheEntry memory entry;
while (numAssignedKeys < _numKeys) {
// Finding the best suitable operator
uint256 bestOperatorIdx = cache.length; // 'not found' flag
uint256 smallestStake;
// The loop is ligthweight comparing to an ether transfer and .deposit invocation
for (uint256 idx = 0; idx < cache.length; ++idx) {
entry = cache[idx];
assert(entry.usedSigningKeys <= entry.totalSigningKeys);
if (entry.usedSigningKeys == entry.totalSigningKeys)
continue;
uint256 stake = entry.usedSigningKeys.sub(entry.stoppedValidators);
if (stake + 1 > entry.stakingLimit)
continue;
if (bestOperatorIdx == cache.length || stake < smallestStake) {
bestOperatorIdx = idx;
smallestStake = stake;
}
}
if (bestOperatorIdx == cache.length) // not found
break;
entry = cache[bestOperatorIdx];
assert(entry.usedSigningKeys < UINT64_MAX);
++entry.usedSigningKeys;
++numAssignedKeys;
}
if (numAssignedKeys == 0) {
return (new bytes(0), new bytes(0));
}
if (numAssignedKeys > 1) {
// we can allocate without zeroing out since we're going to rewrite the whole array
pubkeys = MemUtils.unsafeAllocateBytes(numAssignedKeys * PUBKEY_LENGTH);
signatures = MemUtils.unsafeAllocateBytes(numAssignedKeys * SIGNATURE_LENGTH);
}
uint256 numLoadedKeys = 0;
for (uint256 i = 0; i < cache.length; ++i) {
entry = cache[i];
if (entry.usedSigningKeys == entry.initialUsedSigningKeys) {
continue;
}
operators[entry.id].usedSigningKeys = uint64(entry.usedSigningKeys);
for (uint256 keyIndex = entry.initialUsedSigningKeys; keyIndex < entry.usedSigningKeys; ++keyIndex) {
(bytes memory pubkey, bytes memory signature) = _loadSigningKey(entry.id, keyIndex);
if (numAssignedKeys == 1) {
return (pubkey, signature);
} else {
MemUtils.copyBytes(pubkey, pubkeys, numLoadedKeys * PUBKEY_LENGTH);
MemUtils.copyBytes(signature, signatures, numLoadedKeys * SIGNATURE_LENGTH);
++numLoadedKeys;
}
}
if (numLoadedKeys == numAssignedKeys) {
break;
}
}
assert(numLoadedKeys == numAssignedKeys);
return (pubkeys, signatures);
}
/**
* @notice Returns the rewards distribution proportional to the effective stake for each node operator.
* @param _totalRewardShares Total amount of reward shares to distribute.
*/
function getRewardsDistribution(uint256 _totalRewardShares) external view
returns (
address[] memory recipients,
uint256[] memory shares
)
{
uint256 nodeOperatorCount = getNodeOperatorsCount();
uint256 activeCount = getActiveNodeOperatorsCount();
recipients = new address[](activeCount);
shares = new uint256[](activeCount);
uint256 idx = 0;
uint256 effectiveStakeTotal = 0;
for (uint256 operatorId = 0; operatorId < nodeOperatorCount; ++operatorId) {
NodeOperator storage operator = operators[operatorId];
if (!operator.active)
continue;
uint256 effectiveStake = operator.usedSigningKeys.sub(operator.stoppedValidators);
effectiveStakeTotal = effectiveStakeTotal.add(effectiveStake);
recipients[idx] = operator.rewardAddress;
shares[idx] = effectiveStake;
++idx;
}
if (effectiveStakeTotal == 0)
return (recipients, shares);
uint256 perValidatorReward = _totalRewardShares.div(effectiveStakeTotal);
for (idx = 0; idx < activeCount; ++idx) {
shares[idx] = shares[idx].mul(perValidatorReward);
}
return (recipients, shares);
}
/**
* @notice Returns number of active node operators
*/
function getActiveNodeOperatorsCount() public view returns (uint256) {
return ACTIVE_OPERATORS_COUNT_POSITION.getStorageUint256();
}
/**
* @notice Returns the n-th node operator
* @param _id Node Operator id
* @param _fullInfo If true, name will be returned as well
*/
function getNodeOperator(uint256 _id, bool _fullInfo) external view
operatorExists(_id)
returns
(
bool active,
string name,
address rewardAddress,
uint64 stakingLimit,
uint64 stoppedValidators,
uint64 totalSigningKeys,
uint64 usedSigningKeys
)
{
NodeOperator storage operator = operators[_id];
active = operator.active;
name = _fullInfo ? operator.name : ""; // reading name is 2+ SLOADs
rewardAddress = operator.rewardAddress;
stakingLimit = operator.stakingLimit;
stoppedValidators = operator.stoppedValidators;
totalSigningKeys = operator.totalSigningKeys;
usedSigningKeys = operator.usedSigningKeys;
}
/**
* @notice Returns total number of signing keys of the node operator #`_operator_id`
*/
function getTotalSigningKeyCount(uint256 _operator_id) external view operatorExists(_operator_id) returns (uint256) {
return operators[_operator_id].totalSigningKeys;
}
/**
* @notice Returns number of usable signing keys of the node operator #`_operator_id`
*/
function getUnusedSigningKeyCount(uint256 _operator_id) external view operatorExists(_operator_id) returns (uint256) {
return operators[_operator_id].totalSigningKeys.sub(operators[_operator_id].usedSigningKeys);
}
/**
* @notice Returns n-th signing key of the node operator #`_operator_id`
* @param _operator_id Node Operator id
* @param _index Index of the key, starting with 0
* @return key Key
* @return depositSignature Signature needed for a deposit_contract.deposit call
* @return used Flag indication if the key was used in the staking
*/
function getSigningKey(uint256 _operator_id, uint256 _index) external view
operatorExists(_operator_id)
returns (bytes key, bytes depositSignature, bool used)
{
require(_index < operators[_operator_id].totalSigningKeys, "KEY_NOT_FOUND");
(bytes memory key_, bytes memory signature) = _loadSigningKey(_operator_id, _index);
return (key_, signature, _index < operators[_operator_id].usedSigningKeys);
}
/**
* @notice Returns total number of node operators
*/
function getNodeOperatorsCount() public view returns (uint256) {
return TOTAL_OPERATORS_COUNT_POSITION.getStorageUint256();
}
function _isEmptySigningKey(bytes memory _key) internal pure returns (bool) {
assert(_key.length == PUBKEY_LENGTH);
// algorithm applicability constraint
assert(PUBKEY_LENGTH >= 32 && PUBKEY_LENGTH <= 64);
uint256 k1;
uint256 k2;
assembly {
k1 := mload(add(_key, 0x20))
k2 := mload(add(_key, 0x40))
}
return 0 == k1 && 0 == (k2 >> ((2 * 32 - PUBKEY_LENGTH) * 8));
}
function to64(uint256 v) internal pure returns (uint64) {
assert(v <= uint256(uint64(-1)));
return uint64(v);
}
function _signingKeyOffset(uint256 _operator_id, uint256 _keyIndex) internal pure returns (uint256) {
return uint256(keccak256(abi.encodePacked(SIGNING_KEYS_MAPPING_NAME, _operator_id, _keyIndex)));
}
function _storeSigningKey(uint256 _operator_id, uint256 _keyIndex, bytes memory _key, bytes memory _signature) internal {
assert(_key.length == PUBKEY_LENGTH);
assert(_signature.length == SIGNATURE_LENGTH);
// algorithm applicability constraints
assert(PUBKEY_LENGTH >= 32 && PUBKEY_LENGTH <= 64);
assert(0 == SIGNATURE_LENGTH % 32);
// key
uint256 offset = _signingKeyOffset(_operator_id, _keyIndex);
uint256 keyExcessBits = (2 * 32 - PUBKEY_LENGTH) * 8;
assembly {
sstore(offset, mload(add(_key, 0x20)))
sstore(add(offset, 1), shl(keyExcessBits, shr(keyExcessBits, mload(add(_key, 0x40)))))
}
offset += 2;
// signature
for (uint256 i = 0; i < SIGNATURE_LENGTH; i += 32) {
assembly {
sstore(offset, mload(add(_signature, add(0x20, i))))
}
offset++;
}
}
function _addSigningKeys(uint256 _operator_id, uint256 _quantity, bytes _pubkeys, bytes _signatures) internal
operatorExists(_operator_id)
{
require(_quantity != 0, "NO_KEYS");
require(_pubkeys.length == _quantity.mul(PUBKEY_LENGTH), "INVALID_LENGTH");
require(_signatures.length == _quantity.mul(SIGNATURE_LENGTH), "INVALID_LENGTH");
for (uint256 i = 0; i < _quantity; ++i) {
bytes memory key = BytesLib.slice(_pubkeys, i * PUBKEY_LENGTH, PUBKEY_LENGTH);
require(!_isEmptySigningKey(key), "EMPTY_KEY");
bytes memory sig = BytesLib.slice(_signatures, i * SIGNATURE_LENGTH, SIGNATURE_LENGTH);
_storeSigningKey(_operator_id, operators[_operator_id].totalSigningKeys + i, key, sig);
emit SigningKeyAdded(_operator_id, key);
}
operators[_operator_id].totalSigningKeys = operators[_operator_id].totalSigningKeys.add(to64(_quantity));
}
function _removeSigningKey(uint256 _operator_id, uint256 _index) internal
operatorExists(_operator_id)
{
require(_index < operators[_operator_id].totalSigningKeys, "KEY_NOT_FOUND");
require(_index >= operators[_operator_id].usedSigningKeys, "KEY_WAS_USED");
(bytes memory removedKey, ) = _loadSigningKey(_operator_id, _index);
uint256 lastIndex = operators[_operator_id].totalSigningKeys.sub(1);
if (_index < lastIndex) {
(bytes memory key, bytes memory signature) = _loadSigningKey(_operator_id, lastIndex);
_storeSigningKey(_operator_id, _index, key, signature);
}
_deleteSigningKey(_operator_id, lastIndex);
operators[_operator_id].totalSigningKeys = operators[_operator_id].totalSigningKeys.sub(1);
emit SigningKeyRemoved(_operator_id, removedKey);
}
function _deleteSigningKey(uint256 _operator_id, uint256 _keyIndex) internal {
uint256 offset = _signingKeyOffset(_operator_id, _keyIndex);
for (uint256 i = 0; i < (PUBKEY_LENGTH + SIGNATURE_LENGTH) / 32 + 1; ++i) {
assembly {
sstore(add(offset, i), 0)
}
}
}
function _loadSigningKey(uint256 _operator_id, uint256 _keyIndex) internal view returns (bytes memory key, bytes memory signature) {
// algorithm applicability constraints
assert(PUBKEY_LENGTH >= 32 && PUBKEY_LENGTH <= 64);
assert(0 == SIGNATURE_LENGTH % 32);
uint256 offset = _signingKeyOffset(_operator_id, _keyIndex);
// key
bytes memory tmpKey = new bytes(64);
assembly {
mstore(add(tmpKey, 0x20), sload(offset))
mstore(add(tmpKey, 0x40), sload(add(offset, 1)))
}
offset += 2;
key = BytesLib.slice(tmpKey, 0, PUBKEY_LENGTH);
// signature
signature = new bytes(SIGNATURE_LENGTH);
for (uint256 i = 0; i < SIGNATURE_LENGTH; i += 32) {
assembly {
mstore(add(signature, add(0x20, i)), sload(offset))
}
offset++;
}
return (key, signature);
}
function _loadOperatorCache() internal view returns (DepositLookupCacheEntry[] memory cache) {
cache = new DepositLookupCacheEntry[](getActiveNodeOperatorsCount());
if (0 == cache.length)
return cache;
uint256 totalOperators = getNodeOperatorsCount();
uint256 idx = 0;
for (uint256 operatorId = 0; operatorId < totalOperators; ++operatorId) {
NodeOperator storage operator = operators[operatorId];
if (!operator.active)
continue;
DepositLookupCacheEntry memory entry = cache[idx++];
entry.id = operatorId;
entry.stakingLimit = operator.stakingLimit;
entry.stoppedValidators = operator.stoppedValidators;
entry.totalSigningKeys = operator.totalSigningKeys;
entry.usedSigningKeys = operator.usedSigningKeys;
entry.initialUsedSigningKeys = entry.usedSigningKeys;
}
require(idx == cache.length, "INCOSISTENT_ACTIVE_COUNT");
return cache;
}
}