1
1
// SPDX-License-Identifier: UNLICENSED
2
2
pragma solidity ^ 0.8.9 ;
3
3
4
- import "../common/Initializable.sol " ;
5
- import "../ownership/Ownable.sol " ;
6
- import "./SFCI.sol " ;
7
-
8
- interface NodeDriverExecutable {
9
- function execute () external ;
10
- }
11
-
12
- contract NodeDriverAuth is Initializable , Ownable {
13
- SFCI internal sfc;
14
- NodeDriver internal driver;
15
-
16
- // Initialize NodeDriverAuth, NodeDriver and SFC in one call to allow fewer genesis transactions
17
- function initialize (address payable _sfc , address _driver , address _owner ) external initializer {
18
- Ownable.initialize (_owner);
19
- driver = NodeDriver (_driver);
20
- sfc = SFCI (_sfc);
21
- }
22
-
23
- modifier onlySFC () {
24
- require (msg .sender == address (sfc), "caller is not the SFC contract " );
25
- _;
26
- }
27
-
28
- modifier onlyDriver () {
29
- require (msg .sender == address (driver), "caller is not the NodeDriver contract " );
30
- _;
31
- }
32
-
33
- function migrateTo (address newDriverAuth ) external onlyOwner {
34
- driver.setBackend (newDriverAuth);
35
- }
36
-
37
- function _execute (address executable , address newOwner , bytes32 selfCodeHash , bytes32 driverCodeHash ) internal {
38
- _transferOwnership (executable);
39
- NodeDriverExecutable (executable).execute ();
40
- _transferOwnership (newOwner);
41
- //require(driver.backend() == address(this), "ownership of driver is lost");
42
- require (_getCodeHash (address (this )) == selfCodeHash, "self code hash doesn't match " );
43
- require (_getCodeHash (address (driver)) == driverCodeHash, "driver code hash doesn't match " );
44
- }
45
-
46
- function execute (address executable ) external onlyOwner {
47
- _execute (executable, owner (), _getCodeHash (address (this )), _getCodeHash (address (driver)));
48
- }
49
-
50
- function mutExecute (
51
- address executable ,
52
- address newOwner ,
53
- bytes32 selfCodeHash ,
54
- bytes32 driverCodeHash
55
- ) external onlyOwner {
56
- _execute (executable, newOwner, selfCodeHash, driverCodeHash);
57
- }
58
-
59
- function incBalance (address acc , uint256 diff ) external onlySFC {
60
- require (acc == address (sfc), "recipient is not the SFC contract " );
61
- driver.setBalance (acc, address (acc).balance + diff);
62
- }
63
-
64
- function upgradeCode (address acc , address from ) external onlyOwner {
65
- require (isContract (acc) && isContract (from), "not a contract " );
66
- driver.copyCode (acc, from);
67
- }
68
-
69
- function copyCode (address acc , address from ) external onlyOwner {
70
- driver.copyCode (acc, from);
71
- }
72
-
73
- function incNonce (address acc , uint256 diff ) external onlyOwner {
74
- driver.incNonce (acc, diff);
75
- }
76
-
77
- function updateNetworkRules (bytes calldata diff ) external onlyOwner {
78
- driver.updateNetworkRules (diff);
79
- }
80
-
81
- function updateMinGasPrice (uint256 minGasPrice ) external onlySFC {
82
- // prettier-ignore
83
- driver.updateNetworkRules (bytes (strConcat ("{ \" Economy \" :{ \" MinGasPrice \" : " , uint256ToStr (minGasPrice), "}} " )));
84
- }
85
-
86
- function updateNetworkVersion (uint256 version ) external onlyOwner {
87
- driver.updateNetworkVersion (version);
88
- }
89
-
90
- function advanceEpochs (uint256 num ) external onlyOwner {
91
- driver.advanceEpochs (num);
92
- }
93
-
94
- function updateValidatorWeight (uint256 validatorID , uint256 value ) external onlySFC {
95
- driver.updateValidatorWeight (validatorID, value);
96
- }
97
-
98
- function updateValidatorPubkey (uint256 validatorID , bytes calldata pubkey ) external onlySFC {
99
- driver.updateValidatorPubkey (validatorID, pubkey);
100
- }
101
-
102
- function setGenesisValidator (
103
- address _auth ,
104
- uint256 validatorID ,
105
- bytes calldata pubkey ,
106
- uint256 status ,
107
- uint256 createdEpoch ,
108
- uint256 createdTime ,
109
- uint256 deactivatedEpoch ,
110
- uint256 deactivatedTime
111
- ) external onlyDriver {
112
- sfc.setGenesisValidator (
113
- _auth,
114
- validatorID,
115
- pubkey,
116
- status,
117
- createdEpoch,
118
- createdTime,
119
- deactivatedEpoch,
120
- deactivatedTime
121
- );
122
- }
123
-
124
- function setGenesisDelegation (
125
- address delegator ,
126
- uint256 toValidatorID ,
127
- uint256 stake ,
128
- uint256 lockedStake ,
129
- uint256 lockupFromEpoch ,
130
- uint256 lockupEndTime ,
131
- uint256 lockupDuration ,
132
- uint256 earlyUnlockPenalty ,
133
- uint256 rewards
134
- ) external onlyDriver {
135
- sfc.setGenesisDelegation (
136
- delegator,
137
- toValidatorID,
138
- stake,
139
- lockedStake,
140
- lockupFromEpoch,
141
- lockupEndTime,
142
- lockupDuration,
143
- earlyUnlockPenalty,
144
- rewards
145
- );
146
- }
147
-
148
- function deactivateValidator (uint256 validatorID , uint256 status ) external onlyDriver {
149
- sfc.deactivateValidator (validatorID, status);
150
- }
151
-
152
- function sealEpochValidators (uint256 [] calldata nextValidatorIDs ) external onlyDriver {
153
- sfc.sealEpochValidators (nextValidatorIDs);
154
- }
155
-
156
- function sealEpoch (
157
- uint256 [] calldata offlineTimes ,
158
- uint256 [] calldata offlineBlocks ,
159
- uint256 [] calldata uptimes ,
160
- uint256 [] calldata originatedTxsFee ,
161
- uint256 usedGas
162
- ) external onlyDriver {
163
- sfc.sealEpoch (offlineTimes, offlineBlocks, uptimes, originatedTxsFee, usedGas);
164
- }
165
-
166
- function isContract (address account ) internal view returns (bool ) {
167
- uint256 size;
168
- // solhint-disable-next-line no-inline-assembly
169
- assembly {
170
- size := extcodesize (account)
171
- }
172
- return size > 0 ;
173
- }
174
-
175
- function decimalsNum (uint256 num ) internal pure returns (uint256 ) {
176
- uint decimals;
177
- while (num != 0 ) {
178
- decimals++ ;
179
- num /= 10 ;
180
- }
181
- return decimals;
182
- }
183
-
184
- function uint256ToStr (uint256 num ) internal pure returns (string memory ) {
185
- if (num == 0 ) {
186
- return "0 " ;
187
- }
188
- uint decimals = decimalsNum (num);
189
- bytes memory bstr = new bytes (decimals);
190
- uint strIdx = decimals - 1 ;
191
- while (num != 0 ) {
192
- bstr[strIdx] = bytes1 (uint8 (48 + (num % 10 )));
193
- num /= 10 ;
194
- if (strIdx > 0 ) {
195
- strIdx-- ;
196
- }
197
- }
198
- return string (bstr);
199
- }
200
-
201
- function strConcat (string memory _a , string memory _b , string memory _c ) internal pure returns (string memory ) {
202
- bytes memory _ba = bytes (_a);
203
- bytes memory _bb = bytes (_b);
204
- bytes memory _bc = bytes (_c);
205
- string memory abc = new string (_ba.length + _bb.length + _bc.length );
206
- bytes memory babc = bytes (abc);
207
- uint k = 0 ;
208
- uint i = 0 ;
209
- for (i = 0 ; i < _ba.length ; i++ ) {
210
- babc[k++ ] = _ba[i];
211
- }
212
- for (i = 0 ; i < _bb.length ; i++ ) {
213
- babc[k++ ] = _bb[i];
214
- }
215
- for (i = 0 ; i < _bc.length ; i++ ) {
216
- babc[k++ ] = _bc[i];
217
- }
218
- return string (babc);
219
- }
220
-
221
- function _getCodeHash (address addr ) internal view returns (bytes32 ) {
222
- bytes32 codeHash;
223
- assembly {
224
- codeHash := extcodehash (addr)
225
- }
226
- return codeHash;
227
- }
228
- }
4
+ import {Initializable} from "../common/Initializable.sol " ;
5
+ import {NodeDriverAuth} from "./NodeDriverAuth.sol " ;
6
+ import {IEvmWriter} from "../interfaces/IEVMWriter.sol " ;
229
7
230
8
contract NodeDriver is Initializable {
231
9
NodeDriverAuth internal backend;
232
- EVMWriter internal evmWriter;
10
+ IEvmWriter internal evmWriter;
233
11
234
12
event UpdatedBackend (address indexed backend );
235
13
@@ -253,7 +31,7 @@ contract NodeDriver is Initializable {
253
31
function initialize (address _backend , address _evmWriterAddress ) external initializer {
254
32
backend = NodeDriverAuth (_backend);
255
33
emit UpdatedBackend (_backend);
256
- evmWriter = EVMWriter (_evmWriterAddress);
34
+ evmWriter = IEvmWriter (_evmWriterAddress);
257
35
}
258
36
259
37
function setBalance (address acc , uint256 value ) external onlyBackend {
@@ -376,15 +154,3 @@ contract NodeDriver is Initializable {
376
154
backend.sealEpoch (offlineTimes, offlineBlocks, uptimes, originatedTxsFee, usedGas);
377
155
}
378
156
}
379
-
380
- interface EVMWriter {
381
- function setBalance (address acc , uint256 value ) external ;
382
-
383
- function copyCode (address acc , address from ) external ;
384
-
385
- function swapCode (address acc , address where ) external ;
386
-
387
- function setStorage (address acc , bytes32 key , bytes32 value ) external ;
388
-
389
- function incNonce (address acc , uint256 diff ) external ;
390
- }
0 commit comments