-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNexus.sol
206 lines (178 loc) · 8.2 KB
/
Nexus.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
//SPDX-License-Identifier: AGPL-3.0-only
pragma solidity ^0.8.19;
import {EnumerableSet} from "@openzeppelin/contracts/utils/structs/EnumerableSet.sol";
import {INexusBridge} from "./interfaces/INexusBridge.sol";
import {INodeOperator} from "./interfaces/INodeOperator.sol";
import {Ownable} from "./utils/NexusOwnable.sol";
import {UUPSUpgreadable} from "./utils/UUPSUpgreadable.sol";
import {ISSVNetworkCore} from "./interfaces/ISSVNetwork.sol";
import {INexusInterface} from "./interfaces/INexusInterface.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {BytesArrayLibrary} from "./libraries/BytesArrayLibrary.sol";
/**
* @title Nexus Core Contract
* @author RohitAudit
* @dev This contract is heart and soul of Nexus Network and is used for Operations like
* 1. Onboarding of rollup to Nexus Network
* 2. Change parameters for rollup
* 3. Whitelisting rollup address
* 4. Submitting keys to rollup bridge
* 5. Submitting keyshares to SSV contract
* 6. Recharge funds in SSV contract for validator operation
* 7. Keep track of validator status and exits
*/
contract Nexus is INexusInterface, Ownable, UUPSUpgreadable {
using EnumerableSet for EnumerableSet.AddressSet;
using EnumerableSet for EnumerableSet.UintSet;
using BytesArrayLibrary for bytes[];
EnumerableSet.AddressSet private whitelistedRollups;
address public offChainBot;
mapping(address => Rollup) public rollups;
mapping(bytes=>ValidatorStatus) public validators;
mapping(uint256=>uint16) polygonCDKPartners;
address public NodeOperatorContract;
// change these addresses to mainnet address when deploying on mainnet
address private constant SSV_NETWORK =
0xC3CD9A0aE89Fff83b71b58b6512D43F8a41f363D;
address private constant SSV_TOKEN =
0x3a9f01091C446bdE031E39ea8354647AFef091E7;
uint16 private constant BASIS_POINT = 10000;
modifier onlyOffChainBot() {
if (msg.sender != offChainBot) revert NotNexusBot();
_;
}
modifier onlyWhitelistedRollup() {
if (!whitelistedRollups.contains(msg.sender))
revert AddressNotWhitelisted();
_;
}
modifier nonZeroAddress(address _contract) {
if (_contract ==address(0)) revert IncorrectAddress();
_;
}
function initialize() public initilizeOnce {
_ownableInit(msg.sender);
}
// admin related functions
function whitelistRollup(
string calldata name,
address rollupAddress
) external onlyOwner{
if (whitelistedRollups.add(rollupAddress)) {
emit RollupWhitelisted(name, rollupAddress);
} else {
revert AddressAlreadyWhitelisted();
}
}
function setOffChainBot(address _botAddress) external onlyOwner nonZeroAddress(_botAddress){
offChainBot = _botAddress;
}
function updateProxy(address newImplemetation) public onlyOwner nonZeroAddress(newImplemetation){
updateCodeAddress(newImplemetation);
}
function setNodeOperatorContract(address _nodeOperator) external onlyOwner nonZeroAddress(_nodeOperator){
NodeOperatorContract=_nodeOperator;
emit NodeOperatorContractChanged(_nodeOperator);
}
function changeExecutionFeeAddress(address _execution_fee_address) external onlyOwner nonZeroAddress(_execution_fee_address){
ISSVNetworkCore(SSV_NETWORK).setFeeRecipientAddress(_execution_fee_address);
}
// rollup related functions
function isRollupWhitelisted(
address rollupAddress
) external view returns (bool) {
return whitelistedRollups.contains(rollupAddress);
}
function registerRollup(
address bridgeContract,
uint64 operatorCluster,
uint256 nexusFee,
uint16 stakingLimit
) external onlyWhitelistedRollup{
if (rollups[msg.sender].bridgeContract != address(0))
revert RollupAlreadyRegistered();
if (INexusBridge(bridgeContract).NEXUS_NETWORK()!=address(this)) revert NexusAddressNotFound();
if (stakingLimit>BASIS_POINT) revert IncorrectStakingLimit();
INexusBridge(bridgeContract).setNexusFee(nexusFee);
INodeOperator(NodeOperatorContract).getCluster(operatorCluster);
rollups[msg.sender] = Rollup(
bridgeContract,
stakingLimit,
operatorCluster
);
emit RollupRegistered(msg.sender, bridgeContract,stakingLimit,operatorCluster,nexusFee);
}
function changeStakingLimit(
uint16 newStakingLimit
) external onlyWhitelistedRollup {
if (newStakingLimit>BASIS_POINT) revert IncorrectStakingLimit();
rollups[msg.sender].stakingLimit = newStakingLimit;
emit StakingLimitChanged(
msg.sender,
newStakingLimit
);
}
function changeNexusFee(uint256 _new_fee) external onlyWhitelistedRollup{
INexusBridge(rollups[msg.sender].bridgeContract).setNexusFee(_new_fee);
emit NexusFeeChanged(msg.sender,_new_fee);
}
function changeCluster(uint64 operatorCluster) external onlyWhitelistedRollup{
INodeOperator(NodeOperatorContract).getCluster(operatorCluster);
rollups[msg.sender].operatorCluster = operatorCluster;
emit RollupOperatorClusterChanged(msg.sender,operatorCluster);
}
// validator realted function
function depositValidatorRollup(
address _rollupAdmin,
Validator[] calldata _validators
) external override onlyOffChainBot {
for (uint i = 0; i < _validators.length; i++) {
if(validators[_validators[i].pubKey]!=ValidatorStatus.INACTIVE) revert IncorrectValidatorStatus();
validators[_validators[i].pubKey] = ValidatorStatus.DEPOSITED;
emit ValidatorSubmitted(_validators[i].pubKey, _rollupAdmin);
}
INexusBridge(rollups[_rollupAdmin].bridgeContract)
.depositValidatorNexus(
_validators,
uint256(rollups[_rollupAdmin].stakingLimit)
);
}
function depositValidatorShares(
address _rollupAdmin,
ValidatorShares calldata _validatorShare
) external override onlyOffChainBot {
if(validators[_validatorShare.pubKey]!=ValidatorStatus.DEPOSITED) revert IncorrectValidatorStatus();
IERC20(SSV_TOKEN).approve(SSV_NETWORK, _validatorShare.amount);
ISSVNetworkCore(SSV_NETWORK).registerValidator(
_validatorShare.pubKey,
_validatorShare.operatorIds,
_validatorShare.sharesEncrypted,
_validatorShare.amount,
_validatorShare.cluster
);
validators[_validatorShare.pubKey] = ValidatorStatus.SHARE_DEPOSITED;
emit ValidatorShareSubmitted(_validatorShare.pubKey, _rollupAdmin,_validatorShare.amount);
}
function validatorExit(address rollupAdmin,bytes[] calldata pubkeys) external onlyOffChainBot{
for(uint i=0;i<pubkeys.length;i++){
if(validators[pubkeys[i]]!=ValidatorStatus.SHARE_DEPOSITED) revert IncorrectValidatorStatus();
validators[pubkeys[i]] = ValidatorStatus.VALIDATOR_EXIT_SUBMITTED;
}
}
function validatorExitBalanceTransferred(address rollupAdmin,bytes calldata pubkey, uint64[] memory operatorIds, ISSVNetworkCore.Cluster memory cluster) external onlyOffChainBot{
if(validators[pubkey]!=ValidatorStatus.VALIDATOR_EXIT_SUBMITTED) revert IncorrectValidatorStatus();
ISSVNetworkCore(SSV_NETWORK).removeValidator(pubkey, operatorIds, cluster);
INexusBridge(rollups[rollupAdmin].bridgeContract).updateExitedValidators();
validators[pubkey] = ValidatorStatus.VALIDATOR_EXITED;
emit ValidatorExited(rollupAdmin,pubkey);
}
function validatorSlashed(address rollupAdmin, uint256 amountSlashed) external onlyOffChainBot{
INexusBridge(rollups[rollupAdmin].bridgeContract).validatorsSlashed(amountSlashed);
emit RollupValidatorSlashed(rollupAdmin,amountSlashed);
}
// cluster related functions
function rechargeCluster(uint64 clusterId, uint256 amount,ISSVNetworkCore.Cluster memory cluster) external onlyOffChainBot{
ISSVNetworkCore(SSV_NETWORK).deposit(address(this),INodeOperator(NodeOperatorContract).getCluster(clusterId),amount,cluster);
emit ClusterRecharged(clusterId,amount);
}
}