-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMasterNodeLogic.sol
208 lines (189 loc) · 12.7 KB
/
MasterNodeLogic.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
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.6 <=0.8.19;
import "./System.sol";
import "./utils/ArrayUtil.sol";
import "./utils/RewardUtil.sol";
contract MasterNodeLogic is IMasterNodeLogic, System {
event MNRegister(address _addr, address _operator, uint _amount, uint _lockDay, uint _lockID);
event MNAppendRegister(address _addr, address _operator, uint _amount, uint _lockDay, uint _lockID);
event MNStateUpdate(address _addr, uint _newState, uint _oldState);
event SystemReward(address _nodeAddr, uint _nodeType, address[] _addrs, uint[] _rewardTypes, uint[] _amounts);
function register(bool _isUnion, address _addr, uint _lockDay, string memory _enode, string memory _description, uint _creatorIncentive, uint _partnerIncentive) public payable override {
require(_addr != address(0), "invalid address");
require(_addr != msg.sender, "address can't be caller");
require(!getMasterNodeStorage().existNodeAddress(_addr), "existent address");
require(!getMasterNodeStorage().existNodeFounder(_addr), "address can't be founder of supernode and masternode");
require(!getMasterNodeStorage().existNodeAddress(msg.sender), "caller can't be supernode and masternode");
if(!_isUnion) {
require(msg.value >= getPropertyValue("masternode_min_amount") * Constant.COIN, "less than min lock amount");
} else {
require(msg.value >= getPropertyValue("masternode_union_min_amount") * Constant.COIN, "less than min union lock amount");
}
require(_lockDay >= getPropertyValue("masternode_min_lockday"), "less than min lock day");
require(bytes(_enode).length >= Constant.MIN_NODE_ENODE_LEN && bytes(_enode).length <= Constant.MAX_NODE_ENODE_LEN, "invalid enode");
require(!getMasterNodeStorage().existNodeEnode(_enode), "existent enode");
require(bytes(_description).length <= Constant.MAX_NODE_DESCRIPTION_LEN, "invalid description");
if(!_isUnion) {
require(_creatorIncentive == Constant.MAX_INCENTIVE && _partnerIncentive == 0, "invalid incentive");
} else {
require(_creatorIncentive > 0 && _creatorIncentive <= Constant.MAX_MN_CREATOR_INCENTIVE && _creatorIncentive + _partnerIncentive == Constant.MAX_INCENTIVE, "invalid incentive");
}
uint lockID = getAccountManager().deposit{value: msg.value}(msg.sender, _lockDay);
getMasterNodeStorage().create(_addr, _isUnion, msg.sender, lockID, msg.value, _enode, _description, IMasterNodeStorage.IncentivePlan(_creatorIncentive, _partnerIncentive, 0));
getAccountManager().setRecordFreezeInfo(lockID, _addr, _lockDay); // creator's lock id can't register other masternode again
emit MNRegister(_addr, msg.sender, msg.value, _lockDay, lockID);
}
function appendRegister(address _addr, uint _lockDay) public payable override {
require(getMasterNodeStorage().exist(_addr), "non-existent masternode");
require(!getMasterNodeStorage().existNodeAddress(msg.sender), "caller can't be supernode and masternode");
require(getMasterNodeStorage().isUnion(_addr), "can't append-register independent masternode");
require(msg.value >= getPropertyValue("masternode_append_min_amount") * Constant.COIN, "less than min append lock amount");
require(_lockDay >= getPropertyValue("masternode_append_min_lockday"), "less than min append lock day");
uint lockID = getAccountManager().deposit{value: msg.value}(msg.sender, _lockDay);
getMasterNodeStorage().append(_addr, lockID, msg.value);
getAccountManager().setRecordFreezeInfo(lockID, _addr, getPropertyValue("record_masternode_freezeday")); // partner's lock id can‘t register other masternode until unfreeze it
emit MNAppendRegister(_addr, msg.sender, msg.value, _lockDay, lockID);
}
function turnRegister(address _addr, uint _lockID) public override {
require(getMasterNodeStorage().exist(_addr), "non-existent masternode");
require(!getMasterNodeStorage().existNodeAddress(msg.sender), "caller can't be supernode and masternode");
require(getMasterNodeStorage().isUnion(_addr), "can't turn-register independent masternode");
IAccountManager.AccountRecord memory record = getAccountManager().getRecordByID(_lockID);
require(record.addr == msg.sender, "you aren't record owner");
require(block.number < record.unlockHeight, "record isn't locked");
require(record.amount >= getPropertyValue("masternode_append_min_amount") * Constant.COIN, "less than min append lock amount");
require(record.lockDay >= getPropertyValue("masternode_append_min_lockday"), "less than min append lock day");
IAccountManager.RecordUseInfo memory useinfo = getAccountManager().getRecordUseInfo(_lockID);
require(block.number >= useinfo.unfreezeHeight, "record is freezen");
if(isSN(useinfo.frozenAddr)) {
getSuperNodeLogic().removeMember(useinfo.frozenAddr, _lockID);
} else if(isMN(useinfo.frozenAddr)) {
getMasterNodeLogic().removeMember(useinfo.frozenAddr, _lockID);
}
getMasterNodeStorage().append(_addr, _lockID, record.amount);
getAccountManager().setRecordFreezeInfo(_lockID, _addr, getPropertyValue("record_masternode_freezeday")); // partner's lock id can't register other masternode until unfreeze it
emit MNAppendRegister(_addr, msg.sender, record.amount, record.lockDay, _lockID);
}
function reward(address _addr) public payable override onlySystemRewardContract {
require(getMasterNodeStorage().exist(_addr), "non-existent masternode");
require(msg.value >= RewardUtil.getMNReward(block.number, getPropertyValue("block_space")), "invalid reward");
IMasterNodeStorage.MasterNodeInfo memory info = getMasterNodeStorage().getInfo(_addr);
uint creatorReward = msg.value * info.incentivePlan.creator / Constant.MAX_INCENTIVE;
uint partnerReward = msg.value - creatorReward;
rewardCreator(info, creatorReward);
rewardFounders(info, partnerReward);
getMasterNodeStorage().updateLastRewardHeight(_addr, block.number);
}
function removeMember(address _addr, uint _lockID) public override onlyMnSnAmContract {
IMasterNodeStorage.MasterNodeInfo memory info = getMasterNodeStorage().getInfo(_addr);
for(uint i; i < info.founders.length; i++) {
if(info.founders[i].lockID == _lockID) {
if(i == 0) {
// unfreeze partner
for(uint k = 1; k < info.founders.length; k++) {
getAccountManager().setRecordFreezeInfo(info.founders[k].lockID, address(0), 0);
}
// clear snvote
getSNVote().clearVoteOrApproval(_addr);
}
getMasterNodeStorage().removeMember(_addr, i);
return;
}
}
}
function fromSafe3(address _addr, address _creator, uint _amount, uint _lockDay, uint _lockID, string memory _enode) public override onlySafe3Contract {
require(!getMasterNodeStorage().existNodeAddress(_addr), "existent address");
require(_amount >= getPropertyValue("masternode_min_amount") * Constant.COIN, "less than min lock amount");
IAccountManager.AccountRecord memory record = getAccountManager().getRecordByID(_lockID);
require(record.addr == _creator, "lockID is conflicted with creator");
require(record.amount == _amount, "lockID is conflicted with amount");
require(record.lockDay == _lockDay, "lockID is conflicted with lockDay");
getMasterNodeStorage().create(_addr, false, _creator, _lockID, _amount, _enode, "MasterNode from Safe3", IMasterNodeStorage.IncentivePlan(Constant.MAX_INCENTIVE, 0, 0));
getMasterNodeStorage().updateState(_addr, Constant.NODE_STATE_START);
getAccountManager().setRecordFreezeInfo(_lockID, _addr, _lockDay);
emit MNRegister(_addr, _creator, _amount, _lockDay, _lockID);
}
function changeAddress(address _addr, address _newAddr) public override {
require(getMasterNodeStorage().exist(_addr), "non-existent masternode");
require(_newAddr != address(0), "invalid new address");
require(_newAddr != msg.sender, "new address can't be caller");
require(!getMasterNodeStorage().existNodeAddress(_newAddr), "existent new address");
require(!getMasterNodeStorage().existNodeFounder(_newAddr), "new address can't be founder of supernode and masternode");
require(msg.sender == getMasterNodeStorage().getInfo(_addr).creator, "caller isn't masternode creator");
getMasterNodeStorage().updateAddress(_addr, _newAddr);
IMasterNodeStorage.MasterNodeInfo memory info = getMasterNodeStorage().getInfo(_newAddr);
for(uint i; i < info.founders.length; i++) {
getAccountManager().updateRecordFreezeAddr(info.founders[i].lockID, _newAddr);
}
getSNVote().updateDstAddr(_addr, _newAddr);
}
function changeEnode(address _addr, string memory _enode) public override {
require(getMasterNodeStorage().exist(_addr), "non-existent masternode");
require(bytes(_enode).length >= Constant.MIN_NODE_ENODE_LEN && bytes(_enode).length <= Constant.MAX_NODE_ENODE_LEN, "invalid enode");
require(!getMasterNodeStorage().existNodeEnode(_enode), "existent enode");
require(msg.sender == getMasterNodeStorage().getInfo(_addr).creator || msg.sender == Constant.SAFE3_ADDR, "caller isn't masternode creator");
getMasterNodeStorage().updateEnode(_addr, _enode);
}
function changeDescription(address _addr, string memory _description) public override {
require(getMasterNodeStorage().exist(_addr), "non-existent masternode");
require(bytes(_description).length <= Constant.MAX_NODE_DESCRIPTION_LEN, "invalid description");
require(msg.sender == getMasterNodeStorage().getInfo(_addr).creator, "caller isn't masternode creator");
getMasterNodeStorage().updateDescription(_addr, _description);
}
function changeIsOfficial(address _addr, bool _flag) public override onlyOwner {
require(getMasterNodeStorage().exist(_addr), "non-existent masternode");
getMasterNodeStorage().updateIsOfficial(_addr, _flag);
}
function changeState(uint _id, uint _state) public override onlyMasterNodeStateContract {
if(!getMasterNodeStorage().existID(_id)) {
return;
}
IMasterNodeStorage.MasterNodeInfo memory info = getMasterNodeStorage().getInfoByID(_id);
uint oldState = info.state;
getMasterNodeStorage().updateState(info.addr, _state);
emit MNStateUpdate(info.addr, _state, oldState);
}
function rewardCreator(IMasterNodeStorage.MasterNodeInfo memory _info, uint _amount) internal {
if(_amount == 0) {
return;
}
address[] memory rewardAddrs = new address[](1);
uint[] memory rewardAmounts = new uint[](1);
uint[] memory rewardTypes = new uint[](1);
rewardAddrs[0] = _info.creator;
rewardAmounts[0] = _amount;
rewardTypes[0] = Constant.REWARD_CREATOR;
getAccountManager().reward{value: _amount}(rewardAddrs, rewardAmounts);
emit SystemReward(_info.addr, Constant.REWARD_MN, rewardAddrs, rewardTypes, rewardAmounts);
}
function rewardFounders(IMasterNodeStorage.MasterNodeInfo memory _info, uint _amount) internal {
if(_amount == 0) {
return;
}
uint num;
uint totalAmount;
uint minAmount = getPropertyValue("masternode_min_amount") * Constant.COIN;
for(; num < _info.founders.length; num++) {
if(totalAmount >= minAmount) {
break;
}
totalAmount += _info.founders[num].amount;
}
address[] memory rewardAddrs = new address[](num);
uint[] memory rewardAmounts = new uint[](num);
uint[] memory rewardTypes = new uint[](num);
totalAmount = 0;
for(uint i; i < num; i++) {
rewardAddrs[i] = _info.founders[i].addr;
if(totalAmount + _info.founders[i].amount <= minAmount) {
rewardAmounts[i]= _amount * _info.founders[i].amount / minAmount;
} else {
rewardAmounts[i]= _amount * (minAmount - totalAmount) / minAmount;
}
rewardTypes[i] = Constant.REWARD_PARTNER;
totalAmount += _info.founders[i].amount;
}
getAccountManager().reward{value: _amount}(rewardAddrs, rewardAmounts);
emit SystemReward(_info.addr, Constant.REWARD_MN, rewardAddrs, rewardTypes, rewardAmounts);
}
}