-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMasterNodeStorage.sol
380 lines (341 loc) · 13.7 KB
/
MasterNodeStorage.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
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.6 <=0.8.19;
import "./System.sol";
import "./utils/ArrayUtil.sol";
contract MasterNodeStorage is IMasterNodeStorage, System {
uint no; // masternode no.
mapping(address => MasterNodeInfo) addr2info;
uint[] ids;
mapping(uint => address) id2addr;
mapping(string => address) enode2addr;
function create(address _addr, bool _isUnion, address _creator, uint _lockID, uint _amount, string memory _enode, string memory _description, IncentivePlan memory plan) public override onlyMasterNodeLogic {
MasterNodeInfo storage info = addr2info[_addr];
info.id = ++no;
info.addr = _addr;
info.isUnion = _isUnion;
info.creator = _creator;
info.enode = _enode;
info.description = _description;
info.isOfficial = false;
info.state = Constant.NODE_STATE_INIT;
info.founders.push(MemberInfo(_lockID, _creator, _amount, block.number));
info.incentivePlan = plan;
info.lastRewardHeight = 0;
info.createHeight = block.number;
info.updateHeight = 0;
ids.push(info.id);
id2addr[info.id] = _addr;
enode2addr[info.enode] = _addr;
}
function append(address _addr, uint _lockID, uint _amount) public override onlyMasterNodeLogic {
addr2info[_addr].founders.push(MemberInfo(_lockID, tx.origin, _amount, block.number));
addr2info[_addr].updateHeight = block.number;
}
function updateAddress(address _addr, address _newAddr) public override onlyMasterNodeLogic {
addr2info[_newAddr] = addr2info[_addr];
addr2info[_newAddr].addr = _newAddr;
addr2info[_newAddr].updateHeight = 0;
delete addr2info[_addr];
id2addr[addr2info[_newAddr].id] = _newAddr;
enode2addr[addr2info[_newAddr].enode] = _newAddr;
}
function updateEnode(address _addr, string memory _enode) public override onlyMasterNodeLogic {
string memory oldEnode = addr2info[_addr].enode;
addr2info[_addr].enode = _enode;
addr2info[_addr].updateHeight = block.number;
enode2addr[_enode] = _addr;
delete enode2addr[oldEnode];
}
function updateDescription(address _addr, string memory _description) public override onlyMasterNodeLogic {
addr2info[_addr].description = _description;
addr2info[_addr].updateHeight = block.number;
}
function updateIsOfficial(address _addr, bool _flag) public override onlyMasterNodeLogic {
if(tx.origin != owner()) {
return;
}
addr2info[_addr].isOfficial = _flag;
addr2info[_addr].updateHeight = block.number;
}
function updateState(address _addr, uint _state) public override onlyMasterNodeLogic {
addr2info[_addr].state = _state;
addr2info[_addr].updateHeight = block.number;
}
function removeMember(address _addr, uint _index) public override onlyMasterNodeLogic {
if(_index == 0) {
dissolve(_addr);
} else {
MasterNodeInfo storage info = addr2info[_addr];
if(_index >= info.founders.length) {
return;
}
for(uint i = _index; i < info.founders.length - 1; i++) { // by order
info.founders[i] = info.founders[i + 1];
}
info.founders.pop();
info.updateHeight = block.number;
}
}
function dissolve(address _addr) public override onlyMasterNodeLogic {
MasterNodeInfo memory info = addr2info[_addr];
// remove id
uint pos;
for(uint i; i < ids.length; i++) {
if(ids[i] == info.id) {
pos = i;
break;
}
}
for(; pos < ids.length - 1; pos++) {
ids[pos] = ids[pos + 1];
}
ids.pop();
// remove id2addr
delete id2addr[info.id];
// remove enode2addr
delete enode2addr[info.enode];
// remove info
delete addr2info[_addr];
}
function updateLastRewardHeight(address _addr, uint _height) public override onlyMasterNodeLogic {
addr2info[_addr].lastRewardHeight = _height;
addr2info[_addr].updateHeight = block.number;
}
function getInfo(address _addr) public view override returns (MasterNodeInfo memory) {
return addr2info[_addr];
}
function getInfoByID(uint _id) public view override returns (MasterNodeInfo memory) {
return addr2info[id2addr[_id]];
}
function getNext() public view override returns (address) {
uint minAmount = getPropertyValue("masternode_min_amount") * Constant.COIN;
uint count;
MasterNodeInfo[] memory mns = new MasterNodeInfo[](ids.length);
for(uint i; i < ids.length; i++) {
MasterNodeInfo memory info = addr2info[id2addr[ids[i]]];
if(info.state != Constant.NODE_STATE_START) {
continue;
}
uint lockAmount;
// check creator
if(block.number >= getAccountManager().getRecordByID(info.founders[0].lockID).unlockHeight) { // creator must be locked
continue;
}
lockAmount += info.founders[0].amount;
// check partner
for(uint k = 1; k < info.founders.length; k++) {
if(block.number < getAccountManager().getRecordByID(info.founders[k].lockID).unlockHeight) {
lockAmount += info.founders[k].amount;
}
}
if(lockAmount < minAmount) {
continue;
}
mns[count++] = info;
}
if(count != 0) {
return selectNext(mns, count).addr;
}
// select official addr2info
// select official addr2info
address[] memory officials = getOfficials();
if(officials.length != 0) {
return selectNext2(officials, officials.length);
} else {
return id2addr[(block.number % ids.length) + 1];
}
}
function getNum() public view override returns (uint) {
return ids.length;
}
function getAll(uint _start, uint _count) public view override returns (address[] memory) {
require(ids.length > 0, "insufficient quantity");
require(_start < ids.length, "invalid _start, must be in [0, getNum())");
require(_count > 0 && _count <= 100, "max return 100 masternodes");
uint num = _count;
if(_start + _count >= ids.length) {
num = ids.length - _start;
}
address[] memory ret = new address[](num);
for(uint i; i < num; i++) {
ret[i] = id2addr[ids[i + _start]];
}
return ret;
}
function getAddrNum4Creator(address _creator) public view override returns (uint) {
uint num;
for(uint i; i < ids.length; i++) {
if(addr2info[id2addr[ids[i]]].creator == _creator) {
num++;
}
}
return num;
}
function getAddrs4Creator(address _creator, uint _start, uint _count) public view override returns (address[] memory) {
uint addrNum = getAddrNum4Creator(_creator);
require(addrNum > 0, "insufficient quantity");
require(_start < addrNum, "invalid _start, must be in [0, getAddrNum4Creator())");
require(_count > 0 && _count <= 100, "max return 100 masternodes");
address[] memory temp = new address[](addrNum);
uint index;
for(uint i; i < ids.length; i++) {
if(addr2info[id2addr[ids[i]]].creator == _creator) {
temp[index++] = id2addr[ids[i]];
}
}
uint num = _count;
if(_start + _count >= addrNum) {
num = addrNum - _start;
}
index = 0;
address[] memory ret = new address[](num);
for(uint i; i < num; i++) {
ret[index++] = temp[_start + i];
}
return ret;
}
function getAddrNum4Partner(address _partner) public view override returns (uint) {
uint num;
for(uint i; i < ids.length; i++) {
MasterNodeInfo memory info = addr2info[id2addr[ids[i]]];
if(info.creator == _partner) {
continue;
}
for(uint k; k < info.founders.length; k++) {
if(info.founders[k].addr == _partner) {
num++;
break;
}
}
}
return num;
}
function getAddrs4Partner(address _partner, uint _start, uint _count) public view override returns (address[] memory) {
uint addrNum = getAddrNum4Partner(_partner);
require(addrNum > 0, "insufficient quantity");
require(_start < addrNum, "invalid _start, must be in [0, getAddrNum4Partner())");
require(_count > 0 && _count <= 100, "max return 100 supernodes");
address[] memory temp = new address[](addrNum);
uint index;
for(uint i; i < ids.length; i++) {
MasterNodeInfo memory info = addr2info[id2addr[ids[i]]];
if(info.creator == _partner) {
continue;
}
for(uint k; k < info.founders.length; k++) {
if(info.founders[k].addr == _partner) {
temp[index++] = info.addr;
break;
}
}
}
uint num = _count;
if(_start + _count >= addrNum) {
num = addrNum - _start;
}
index = 0;
address[] memory ret = new address[](num);
for(uint i; i < num; i++) {
ret[index++] = temp[_start + i];
}
return ret;
}
function getOfficials() public view override returns (address[] memory) {
uint num;
for(uint i; i < ids.length; i++) {
if(addr2info[id2addr[ids[i]]].isOfficial) {
num++;
}
}
address[] memory ret = new address[](num);
uint index;
for(uint i; i < ids.length; i++) {
if(addr2info[id2addr[ids[i]]].isOfficial) {
ret[index++] = id2addr[ids[i]];
}
}
return ret;
}
function exist(address _addr) public view override returns (bool) {
return addr2info[_addr].id != 0;
}
function existID(uint _id) public view override returns (bool) {
return id2addr[_id] != address(0);
}
function existEnode(string memory _enode) public view override returns (bool) {
return enode2addr[_enode] != address(0);
}
function existLockID(address _addr, uint _lockID) public view override returns (bool) {
MasterNodeInfo memory mn = addr2info[_addr];
for(uint i; i < mn.founders.length; i++) {
if(mn.founders[i].lockID == _lockID) {
return true;
}
}
return false;
}
function existFounder(address _founder) public view override returns (bool) {
for(uint i; i < ids.length; i++) {
MasterNodeInfo memory info = addr2info[id2addr[ids[i]]];
for(uint k; k < info.founders.length; k++) {
if(info.founders[k].addr == _founder) {
return true;
}
}
}
return false;
}
function isValid(address _addr) public view override returns (bool) {
MasterNodeInfo memory info = addr2info[_addr];
if(info.id == 0) {
return false;
}
if(block.number >= getAccountManager().getRecordByID(info.founders[0].lockID).unlockHeight) { // creator must be locked
return false;
}
uint lockAmount = info.founders[0].amount;
for(uint i = 1; i < info.founders.length; i++) {
if(block.number < getAccountManager().getRecordByID(info.founders[i].lockID).unlockHeight) {
lockAmount += info.founders[i].amount;
}
}
if(lockAmount < getPropertyValue("masternode_min_amount") * Constant.COIN) {
return false;
}
return true;
}
function isUnion(address _addr) public view override returns (bool) {
return addr2info[_addr].isUnion;
}
function existNodeAddress(address _addr) public view override returns (bool) {
return exist(_addr) || getSuperNodeStorage().exist(_addr);
}
function existNodeEnode(string memory _enode) public view override returns (bool) {
return existEnode(_enode) || getSuperNodeStorage().existEnode(_enode);
}
function existNodeFounder(address _founder) public view override returns (bool) {
return existFounder(_founder) || getSuperNodeStorage().existFounder(_founder);
}
function selectNext(MasterNodeInfo[] memory _arr, uint len) internal pure returns (MasterNodeInfo memory) {
uint pos;
uint temp = _arr[pos].lastRewardHeight;
for(uint i = 1; i < len; i++) {
if(temp > _arr[i].lastRewardHeight) {
pos = i;
temp = _arr[i].lastRewardHeight;
}
}
return _arr[pos];
}
function selectNext2(address[] memory _arr, uint len) internal view returns (address) {
uint pos;
uint temp = getInfo(_arr[pos]).lastRewardHeight;
for(uint i = 1; i < len; i++) {
if(temp > getInfo(_arr[i]).lastRewardHeight) {
pos = i;
temp = getInfo(_arr[i]).lastRewardHeight;
}
}
return _arr[pos];
}
}