-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSNVote.sol
770 lines (679 loc) · 27 KB
/
SNVote.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
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.6 <=0.8.19;
import "./System.sol";
contract SNVote is ISNVote, System {
// for records
mapping(uint => VoteRecord) id2record; // voter's record to supernode or proxy vote
// for voters
mapping(address => mapping(address => VoteDetail)) voter2details; // voter to details
mapping(address => uint) voter2amount; // voter to total amount
mapping(address => uint) voter2num; // voter to total votenum
mapping(address => address[]) voter2dsts; // voter to supernode or proxy list
mapping(address => uint[]) voter2ids; // voter to record list
// for supernodes or proxies
mapping(address => mapping(address => VoteDetail)) dst2details; // supernode or proxy to details
mapping(address => uint) dst2amount; // supernode or proxy to total amount
mapping(address => uint) dst2num; // supernode or proxy to total votenum
mapping(address => address[]) dst2voters; // supernode or proxy to voter list
mapping(address => uint[]) dst2ids; // supernode or proxy to record list
uint allAmount; // all supernodes received amount
uint allVoteNum; // all supernodes received voteNum
uint allProxiedAmount; // all proxies received amount
uint allProxiedVoteNum; // all proxies received voteNum
event SNVOTE_VOTE(address _voterAddr, address _snAddr, uint _recordID, uint _voteNum);
event SNVOTE_APPROVAL(address _voterAddr, address _proxyAddr, uint _recordID, uint _voteNum);
event SNVOTE_REMOVE_VOTE(address _voterAddr, address _snAddr, uint _recordID, uint _voteNum);
event SNVOTE_REMOVE_APPROVAL(address _voterAddr, address _proxyAddr, uint _recordID, uint _voteNum);
function voteOrApproval(bool _isVote, address _dstAddr, uint[] memory _recordIDs) public override {
require(!isSN(msg.sender), "supernode can't vote others");
if(_isVote) {
require(isValidSN(_dstAddr), "invalid supernode address");
} else {
require(isValidMN(_dstAddr), "invalid proxy address");
}
uint id;
for(uint i; i < _recordIDs.length; i++) {
id = _recordIDs[i];
if(_recordIDs[i] == 0) {
// generate new record id
id = getAccountManager().moveID0(msg.sender);
if(id == 0) {
continue;
}
add(msg.sender, _dstAddr, id);
} else {
remove(msg.sender, id);
add(msg.sender, _dstAddr, id);
}
}
}
function voteOrApprovalWithAmount(bool _isVote, address _dstAddr) public payable override {
require(!isSN(msg.sender), "supernode can't vote others");
if(_isVote) {
require(isValidSN(_dstAddr), "invalid supernode address");
} else {
require(isValidMN(_dstAddr), "invalid proxy address");
}
uint recordID = getAccountManager().depositReturnNewID{value: msg.value}(msg.sender);
add(msg.sender, _dstAddr, recordID);
}
function removeVoteOrApproval(uint[] memory _recordIDs) public override {
require(!isSN(msg.sender), "supernode can't remove vote");
for(uint i; i < _recordIDs.length; i++) {
if(_recordIDs[i] == 0) {
continue;
}
remove(msg.sender, _recordIDs[i]);
}
}
function removeVoteOrApproval2(address _voterAddr, uint _recordID) public override onlyAmOrSnContract {
if(_recordID == 0) {
return;
}
remove(_voterAddr, _recordID);
}
function clearVoteOrApproval(address _dstAddr) public override onlyMnOrSnContract {
uint[] memory ids = dst2ids[_dstAddr];
for(uint i; i < ids.length; i++) {
VoteRecord memory voteRecord = id2record[ids[i]];
// update voter
remove4Voter(voteRecord.voterAddr, _dstAddr, ids[i], voteRecord.amount, voteRecord.num);
// update dst
remove4Dst(_dstAddr, voteRecord.voterAddr, ids[i], voteRecord.amount, voteRecord.num);
// remove vote record
delete id2record[ids[i]];
// unfreeze record
if(isSN(_dstAddr)) { // vote
if(allAmount > voteRecord.amount) {
allAmount -= voteRecord.amount;
} else {
allAmount = 0;
}
if(allVoteNum > voteRecord.num) {
allVoteNum -= voteRecord.num;
} else {
allVoteNum = 0;
}
getAccountManager().setRecordVoteInfo(ids[i], address(0), 0);
} else { // proxy
if(allProxiedAmount > voteRecord.amount) {
allProxiedAmount -= voteRecord.amount;
} else {
allProxiedAmount = 0;
}
if(allProxiedVoteNum > voteRecord.num) {
allProxiedVoteNum -= voteRecord.num;
} else {
allProxiedVoteNum = 0;
}
}
}
}
function proxyVote(address _snAddr) public override {
require(isValidMN(msg.sender), "invalid proxy");
require(isValidSN(_snAddr), "invalid supernode");
uint recordID;
address voterAddr;
uint[] memory ids = dst2ids[msg.sender];
for(uint i; i < ids.length; i++) {
recordID = ids[i];
voterAddr = id2record[recordID].voterAddr;
remove(voterAddr, recordID); // remove vote or approval
add(voterAddr, _snAddr, recordID); // add vote
}
}
function updateDstAddr(address _oldAddr, address _newAddr) public onlyMnOrSnContract {
address[] memory voters = dst2voters[_oldAddr];
uint[] memory ids = dst2ids[_oldAddr];
// for id2record
for(uint i; i < ids.length; i++) {
id2record[ids[i]].dstAddr = _newAddr;
}
// for voters
// copy voter2details
for(uint i; i < voters.length; i++) {
address voter = voters[i];
voter2details[voter][_newAddr] = voter2details[voter][_oldAddr];
delete voter2details[voter][_oldAddr];
for(uint j; j < voter2dsts[voter].length; j++) {
if(voter2dsts[voter][j] == _oldAddr) {
voter2dsts[voter][j] = _newAddr;
}
}
}
// for dst
// copy dst2details
for(uint i; i < voters.length; i++) {
address voter = voters[i];
dst2details[_newAddr][voter] = dst2details[_oldAddr][voter];
dst2details[_newAddr][voter].addr = _newAddr;
delete dst2details[_oldAddr][voter];
}
// copy dst2amount
dst2amount[_newAddr] = dst2amount[_oldAddr];
delete dst2amount[_oldAddr];
// copy dst2num
dst2num[_newAddr] = dst2num[_oldAddr];
delete dst2num[_oldAddr];
// copy dst2voters
dst2voters[_newAddr] = dst2voters[_oldAddr];
delete dst2voters[_oldAddr];
// copy dst2ids
dst2ids[_newAddr] = dst2ids[_oldAddr];
delete dst2ids[_oldAddr];
}
function getAmount4Voter(address _voterAddr) public view override returns (uint) {
return voter2amount[_voterAddr];
}
function getVoteNum4Voter(address _voterAddr) public view override returns (uint) {
return voter2num[_voterAddr];
}
// get voter's supernode number
function getSNNum4Voter(address _voterAddr) public view override returns (uint) {
uint num;
address[] memory dsts = voter2dsts[_voterAddr];
for(uint i; i < dsts.length; i++) {
if(isSN(dsts[i])) {
num++;
}
}
return num;
}
function getSNs4Voter(address _voterAddr, uint _start, uint _count) public view override returns (address[] memory, uint[] memory) {
uint snNum = getSNNum4Voter(_voterAddr);
require(snNum > 0, "insufficient quantity");
require(_start < snNum, "invalid _start, must be in [0, getSNNum4Voter())");
require(_count > 0 && _count <= 100, "max return 100 SNs");
address[] memory tempAddrs = new address[](snNum);
uint[] memory tempNums = new uint[](snNum);
address[] memory dsts = voter2dsts[_voterAddr];
uint index;
for(uint i; i < dsts.length; i++) {
if(isSN(dsts[i])) {
tempAddrs[index] = dsts[i];
tempNums[index++] = voter2details[_voterAddr][dsts[i]].totalNum;
}
}
uint num = _count;
if(_start + _count >= snNum) {
num = snNum - _start;
}
address[] memory retAddrs = new address[](num);
uint[] memory retNums = new uint[](num);
for(uint i; i < num; i++) {
retAddrs[i] = tempAddrs[i + _start];
retNums[i] = tempNums[i + _start];
}
return(retAddrs, retNums);
}
function getProxyNum4Voter(address _voterAddr) public view override returns (uint) {
uint num;
address[] memory dsts = voter2dsts[_voterAddr];
for(uint i; i < dsts.length; i++) {
if(isMN(dsts[i])) {
num++;
}
}
return num;
}
function getProxies4Voter(address _voterAddr, uint _start, uint _count) public view override returns (address[] memory, uint[] memory) {
uint proxyNum = getProxyNum4Voter(_voterAddr);
require(proxyNum > 0, "insufficient quantity");
require(_start < proxyNum, "invalid _start, must be in [0, getProxyNum4Voter())");
require(_count > 0 && _count <= 100, "max return 100 proxies");
address[] memory tempAddrs = new address[](proxyNum);
uint[] memory tempNums = new uint[](proxyNum);
address[] memory dsts = voter2dsts[_voterAddr];
uint index;
for(uint i; i < dsts.length; i++) {
if(isMN(dsts[i])) {
tempAddrs[index] = dsts[i];
tempNums[index++] = voter2details[_voterAddr][dsts[i]].totalNum;
}
}
uint num = _count;
if(_start + _count >= proxyNum) {
num = proxyNum - _start;
}
address[] memory retAddrs = new address[](num);
uint[] memory retNums = new uint[](num);
for(uint i; i < num; i++) {
retAddrs[i] = tempAddrs[i + _start];
retNums[i] = tempNums[i + _start];
}
return (retAddrs, retNums);
}
function getVotedIDNum4Voter(address _voterAddr) public view override returns (uint) {
uint num;
uint[] memory ids = voter2ids[_voterAddr];
for(uint i; i < ids.length; i++) {
if(isSN(id2record[ids[i]].dstAddr)) {
num++;
}
}
return num;
}
function getVotedIDs4Voter(address _voterAddr, uint _start, uint _count) public view override returns (uint[] memory) {
uint idNum = getVotedIDNum4Voter(_voterAddr);
require(idNum > 0, "insufficient quantity");
require(_start < idNum, "invalid _start, must be in [0, getVotedIDNum4Voter())");
require(_count > 0 && _count <= 100, "max return 100 ids");
uint[] memory temp = new uint[](idNum);
uint[] memory ids = voter2ids[_voterAddr];
uint index;
for(uint i; i < ids.length; i++) {
if(isSN(id2record[ids[i]].dstAddr)) {
temp[index++] = ids[i];
}
}
uint num = _count;
if(_start + _count >= idNum) {
num = idNum - _start;
}
uint[] memory ret = new uint[](num);
for(uint i; i < num; i++) {
ret[i] = temp[i + _start];
}
return ret;
}
function getProxiedIDNum4Voter(address _voterAddr) public view override returns (uint) {
uint num;
uint[] memory ids = voter2ids[_voterAddr];
for(uint i; i < ids.length; i++) {
if(isMN(id2record[ids[i]].dstAddr)) {
num++;
}
}
return num;
}
function getProxiedIDs4Voter(address _voterAddr, uint _start, uint _count) public view override returns (uint[] memory) {
uint idNum = getProxiedIDNum4Voter(_voterAddr);
require(idNum > 0, "insufficient quantity");
require(_start < idNum, "invalid _start, must be in [0, getProxiedIDNum4Voter())");
require(_count > 0 && _count <= 100, "max return 100 ids");
uint[] memory temp = new uint[](idNum);
uint[] memory ids = voter2ids[_voterAddr];
uint index;
for(uint i; i < ids.length; i++) {
if(isMN(id2record[ids[i]].dstAddr)) {
temp[index++] = ids[i];
}
}
uint num = _count;
if(_start + _count >= idNum) {
num = idNum - _start;
}
uint[] memory ret = new uint[](num);
for(uint i; i < num; i++) {
ret[i] = temp[i + _start];
}
return ret;
}
function getTotalAmount(address _addr) public view override returns (uint) {
return dst2amount[_addr];
}
function getTotalVoteNum(address _addr) public view override returns (uint) {
return dst2num[_addr];
}
function getVoterNum(address _addr) public view override returns (uint) {
return dst2voters[_addr].length;
}
function getVoters(address _addr, uint _start, uint _count) public view override returns (address[] memory, uint[] memory) {
require(dst2voters[_addr].length > 0, "insufficient quantity");
require(_start < dst2voters[_addr].length, "invalid _start, must be in [0, getVoterNum())");
require(_count > 0 && _count <= 100, "max return 100 voters");
address[] memory tempAddrs = dst2voters[_addr];
uint[] memory tempNums = new uint[](tempAddrs.length);
for(uint i; i < tempAddrs.length; i++) {
tempNums[i] = dst2details[_addr][tempAddrs[i]].totalNum;
}
uint num = _count;
if(_start + _count >= tempAddrs.length) {
num = tempAddrs.length - _start;
}
address[] memory retAddrs = new address[](num);
uint[] memory retNums = new uint[](num);
for(uint i; i < num; i++) {
retAddrs[i] = tempAddrs[i + _start];
retNums[i] = tempNums[i + _start];
}
return (retAddrs, retNums);
}
function getIDNum(address _addr) public view override returns (uint) {
return dst2ids[_addr].length;
}
function getIDs(address _addr, uint _start, uint _count) public view override returns (uint[] memory) {
require(dst2ids[_addr].length > 0, "insufficient quantity");
require(_start < dst2ids[_addr].length, "invalid _start, must be in [0, getIDNum())");
require(_count > 0 && _count <= 100, "max return 100 ids");
uint num = _count;
if(_start + _count >= dst2ids[_addr].length) {
num = dst2ids[_addr].length - _start;
}
uint[] memory ret = new uint[](num);
for(uint i; i < num; i++) {
ret[i] = dst2ids[_addr][i + _start];
}
return ret;
}
function getAllAmount() public view override returns (uint) {
return allAmount;
}
function getAllVoteNum() public view override returns (uint) {
return allVoteNum;
}
function getAllProxiedAmount() public view override returns (uint) {
return allProxiedAmount;
}
function getAllProxiedVoteNum() public view override returns (uint) {
return allProxiedVoteNum;
}
function existDst4Voter(address _voterAddr, address _dstAddr) internal view returns (bool, uint) {
address[] memory dsts = voter2dsts[_voterAddr];
for(uint i; i < dsts.length; i++) {
if(dsts[i] == _dstAddr) {
return (true, i);
}
}
return (false, 0);
}
function existVoter4Dst(address _dstAddr, address _voterAddr) internal view returns (bool, uint) {
address[] memory voters = dst2voters[_dstAddr];
for(uint i; i < voters.length; i++) {
if(voters[i] == _voterAddr) {
return (true, i);
}
}
return (false, 0);
}
function existID4Voter(address _voterAddr, uint _recordID) internal view returns (bool, uint) {
uint[] memory recordIDs = voter2ids[_voterAddr];
for(uint i; i < recordIDs.length; i++) {
if(recordIDs[i] == _recordID) {
return (true, i);
}
}
return (false, 0);
}
function existID4Dst(address _dstAddr, uint _recordID) internal view returns (bool, uint) {
uint[] memory recordIDs = dst2ids[_dstAddr];
for(uint i; i < recordIDs.length; i++) {
if(recordIDs[i] == _recordID) {
return (true, i);
}
}
return (false, 0);
}
function add4Voter(address _voterAddr, address _dstAddr, uint _recordID, uint _amount, uint _num) internal {
// update detail
VoteDetail storage detail = voter2details[_voterAddr][_dstAddr];
detail.addr = _dstAddr;
detail.totalAmount += _amount;
detail.totalNum += _num;
detail.recordIDs.push(_recordID);
// update total amount
voter2amount[_voterAddr] += _amount;
// update total votenum
voter2num[_voterAddr] += _num;
// update dst list
bool flag;
uint pos;
(flag, pos) = existDst4Voter(_voterAddr, _dstAddr);
if(!flag) {
voter2dsts[_voterAddr].push(_dstAddr);
}
// update record list
(flag, pos) = existID4Voter(_voterAddr, _recordID);
if(!flag) {
voter2ids[_voterAddr].push(_recordID);
}
}
function add4Dst(address _dstAddr, address _voterAddr, uint _recordID, uint _amount, uint _num) internal {
// update detail
VoteDetail storage detail = dst2details[_dstAddr][_voterAddr];
detail.addr = _voterAddr;
detail.totalAmount += _amount;
detail.totalNum += _num;
detail.recordIDs.push(_recordID);
// update total amount
dst2amount[_dstAddr] += _amount;
// update total votenum
dst2num[_dstAddr] += _num;
// update voter list
bool flag;
uint pos;
(flag, pos) = existVoter4Dst(_dstAddr, _voterAddr);
if(!flag) {
dst2voters[_dstAddr].push(_voterAddr);
}
// update record list
(flag, pos) = existID4Dst(_dstAddr, _recordID);
if(!flag) {
dst2ids[_dstAddr].push(_recordID);
}
}
function add(address _voterAddr, address _dstAddr, uint _recordID) internal {
IAccountManager.AccountRecord memory record = getAccountManager().getRecordByID(_recordID);
if(record.addr != _voterAddr) {
return;
}
IAccountManager.RecordUseInfo memory useinfo = getAccountManager().getRecordUseInfo(_recordID);
if(block.number < useinfo.releaseHeight || isSN(useinfo.frozenAddr)) {
return;
}
uint amount = record.amount;
uint num = amount;
if(isMN(useinfo.frozenAddr)) {
num = record.amount * 2;
} else if(block.number < record.unlockHeight) {
num = record.amount * 15 / 10;
}
// update vote record
id2record[_recordID] = VoteRecord(_voterAddr, _dstAddr, amount, num, block.number);
// update voter
add4Voter(_voterAddr, _dstAddr, _recordID, amount, num);
// update dst
add4Dst(_dstAddr, _voterAddr, _recordID, amount, num);
// freeze record
if(isSN(_dstAddr)) { // vote
allAmount += amount;
allVoteNum += num;
getAccountManager().setRecordVoteInfo(_recordID, _dstAddr, getPropertyValue("record_snvote_lockday"));
emit SNVOTE_VOTE(_voterAddr, _dstAddr, _recordID, num);
} else { // approval
allProxiedAmount += amount;
allProxiedVoteNum += num;
emit SNVOTE_APPROVAL(_voterAddr, _dstAddr, _recordID, num);
}
}
function remove4Voter(address _voterAddr, address _dstAddr, uint _recordID, uint _amount, uint _num) internal {
// update detail
bool flag;
uint pos;
(flag, pos) = existDst4Voter(_voterAddr, _dstAddr);
if(!flag) {
return;
}
// remove record id
VoteDetail storage detail = voter2details[_voterAddr][_dstAddr];
uint[] storage recordIDs = detail.recordIDs;
flag = false;
pos = 0;
for(uint i; i < recordIDs.length; i++) {
if(recordIDs[i] == _recordID) {
flag = true;
pos = i;
break;
}
}
if(!flag) {
return;
}
recordIDs[pos] = recordIDs[recordIDs.length - 1];
recordIDs.pop();
if(recordIDs.length == 0) {
// remove detail
delete voter2details[_voterAddr][_dstAddr];
} else {
// remove amount & votenum
if(detail.totalAmount > _amount) {
detail.totalAmount -= _amount;
} else {
detail.totalAmount = 0;
}
if(detail.totalNum > _num) {
detail.totalNum -= _num;
} else {
detail.totalNum = 0;
}
}
// update total amount
if(voter2amount[_voterAddr] <= _amount) {
voter2amount[_voterAddr] = 0;
} else {
voter2amount[_voterAddr] -= _amount;
}
// update total votenum
if(voter2num[_voterAddr] <= _num) {
voter2num[_voterAddr] = 0;
} else {
voter2num[_voterAddr] -= _num;
}
// update dst list
if(recordIDs.length == 0) {
(flag, pos) = existDst4Voter(_voterAddr, _dstAddr);
if(flag) {
address[] storage dsts = voter2dsts[_voterAddr];
dsts[pos] = dsts[dsts.length - 1];
dsts.pop();
}
}
// update record id list
(flag, pos) = existID4Voter(_voterAddr, _recordID);
if(flag) {
uint[] storage ids = voter2ids[_voterAddr];
ids[pos] = ids[ids.length - 1];
ids.pop();
}
}
function remove4Dst(address _dstAddr, address _voterAddr, uint _recordID, uint _amount, uint _num) internal {
// update detail
bool flag;
uint pos;
(flag, pos) = existVoter4Dst(_dstAddr, _voterAddr);
if(!flag) {
return;
}
// remove record id
VoteDetail storage detail = dst2details[_dstAddr][_voterAddr];
uint[] storage recordIDs = detail.recordIDs;
flag = false;
pos = 0;
for(uint i; i < recordIDs.length; i++) {
if(recordIDs[i] == _recordID) {
flag = true;
pos = i;
break;
}
}
if(!flag) {
return;
}
recordIDs[pos] = recordIDs[recordIDs.length - 1];
recordIDs.pop();
if(recordIDs.length == 0) {
// remove detail
delete dst2details[_dstAddr][_voterAddr];
} else {
// remove amount & votenum
if(detail.totalAmount > _amount) {
detail.totalAmount -= _amount;
} else {
detail.totalAmount = 0;
}
if(detail.totalNum > _num) {
detail.totalNum -= _num;
} else {
detail.totalNum = 0;
}
}
// update total amount
if(dst2amount[_dstAddr] <= _amount) {
dst2amount[_dstAddr] = 0;
} else {
dst2amount[_dstAddr] -= _amount;
}
// update total votenum
if(dst2num[_dstAddr] <= _num) {
dst2num[_dstAddr] = 0;
} else {
dst2num[_dstAddr] -= _num;
}
// update voter list
if(recordIDs.length == 0) {
(flag, pos) = existVoter4Dst(_dstAddr, _voterAddr);
if(flag) {
address[] storage voters = dst2voters[_dstAddr];
voters[pos] = voters[voters.length - 1];
voters.pop();
}
}
// update record id list
(flag, pos) = existID4Dst(_dstAddr, _recordID);
if(flag) {
uint[] storage ids = dst2ids[_dstAddr];
ids[pos] = ids[ids.length - 1];
ids.pop();
}
}
function remove(address _voterAddr, uint _recordID) internal {
IAccountManager.AccountRecord memory record = getAccountManager().getRecordByID(_recordID);
if(record.addr != _voterAddr) {
return;
}
IAccountManager.RecordUseInfo memory useinfo = getAccountManager().getRecordUseInfo(_recordID);
if(block.number < useinfo.releaseHeight) {
return;
}
VoteRecord memory voteRecord = id2record[_recordID];
address dstAddr = voteRecord.dstAddr;
if(dstAddr == address(0) || voteRecord.voterAddr != _voterAddr) {
return;
}
uint amount = voteRecord.amount;
uint num = voteRecord.num;
// update voter
remove4Voter(_voterAddr, dstAddr, _recordID, amount, num);
// update dst
remove4Dst(dstAddr, _voterAddr, _recordID, amount, num);
// remove vote record
delete id2record[_recordID];
// unfreeze record
if(isSN(dstAddr)) { // vote
if(allAmount > amount) {
allAmount -= amount;
} else {
allAmount = 0;
}
if(allVoteNum > num) {
allVoteNum -= num;
} else {
allVoteNum = 0;
}
getAccountManager().setRecordVoteInfo(_recordID, address(0), 0);
emit SNVOTE_REMOVE_VOTE(_voterAddr, dstAddr, _recordID, num);
} else { // proxy
if(allProxiedAmount > amount) {
allProxiedAmount -= amount;
} else {
allProxiedAmount = 0;
}
if(allProxiedVoteNum > num) {
allProxiedVoteNum -= num;
} else {
allProxiedVoteNum = 0;
}
emit SNVOTE_REMOVE_APPROVAL(_voterAddr, dstAddr, _recordID, num);
}
}
}