15 | | - | func getNumberByKey (key) = valueOrElse(getInteger(this, key), 0) |
---|
16 | | - | |
---|
17 | | - | |
---|
18 | | - | func getLeaseGroupNodeListKey (groupNum) = makeString(["%s%d%s", "leaseGroup", toString(groupNum), "nodeList"], SEP) |
---|
19 | | - | |
---|
20 | | - | |
---|
21 | | - | func getLeaseAmountKey (nodeIndex) = makeString(["%s%d%s", "lease", toString(nodeIndex), "amount"], SEP) |
---|
22 | | - | |
---|
23 | | - | |
---|
24 | | - | func getLeaseIdKey (nodeIndex) = makeString(["%s%d%s", "lease", toString(nodeIndex), "id"], SEP) |
---|
25 | | - | |
---|
26 | | - | |
---|
27 | | - | func getLeaseIdByAddressKey (nodeAddress) = makeString(["%s%s%s", "leaseByAddress", nodeAddress, "id"], SEP) |
---|
28 | | - | |
---|
29 | | - | |
---|
30 | | - | func getLeaseAmountByAddressKey (nodeAddress) = makeString(["%s%s%s", "leaseByAddress", nodeAddress, "amount"], SEP) |
---|
31 | | - | |
---|
32 | | - | |
---|
33 | | - | func getStakingNodeByIndex (idx) = valueOrElse(getString(makeString(["%s%d%s", "lease", toString(idx), "nodeAddress"], SEP)), "") |
---|
34 | | - | |
---|
35 | | - | |
---|
36 | | - | func readNodeInfo (nodeIdx) = { |
---|
37 | | - | let nodeAddress = addressFromStringValue(getStakingNodeByIndex(nodeIdx)) |
---|
38 | | - | let leasedAmtKEY = getLeaseAmountKey(nodeIdx) |
---|
39 | | - | let leasedAmt = getNumberByKey(leasedAmtKEY) |
---|
40 | | - | let leaseIdKEY = getLeaseIdKey(nodeIdx) |
---|
41 | | - | let leaseId = value(getBinary(this, leaseIdKEY)) |
---|
42 | | - | $Tuple5(nodeAddress, leasedAmtKEY, leasedAmt, leaseIdKEY, leaseId) |
---|
| 9 | + | func getRandomNumber (maxValue,finishHeight) = { |
---|
| 10 | + | let randomSeedBlock = value(blockInfoByHeight(finishHeight)) |
---|
| 11 | + | let randomHash = sha256(value(randomSeedBlock.vrf)) |
---|
| 12 | + | (toInt(randomHash, 16) % maxValue) |
---|
46 | | - | let nRegIdxTxAdd = 1 |
---|
47 | | - | |
---|
48 | | - | let nRegIdxHeightAdd = 2 |
---|
49 | | - | |
---|
50 | | - | let nRegIdxTimeAdd = 3 |
---|
51 | | - | |
---|
52 | | - | let nRegIdxDepoAmt = 4 |
---|
53 | | - | |
---|
54 | | - | let nRegIdxDepoAmtLeft = 5 |
---|
55 | | - | |
---|
56 | | - | let nRegIdxStatus = 6 |
---|
57 | | - | |
---|
58 | | - | let nRegIdxStatusChangeHeight = 7 |
---|
59 | | - | |
---|
60 | | - | let nRegIdxStatusChangeTime = 8 |
---|
61 | | - | |
---|
62 | | - | func keyNodeRegistry (address) = ("%s__" + address) |
---|
63 | | - | |
---|
64 | | - | |
---|
65 | | - | func keyApprovedNodeList () = "%s__approvedNodeList" |
---|
66 | | - | |
---|
67 | | - | |
---|
68 | | - | func keyBeneficiaryAddress () = "%s%s__cfg__beneficiaryAddress" |
---|
69 | | - | |
---|
70 | | - | |
---|
71 | | - | func readNodeDataAndValidateStatus (nodeAddr,expectedStatus) = { |
---|
72 | | - | let nodeDataOpt = getString(keyNodeRegistry(nodeAddr)) |
---|
73 | | - | if (!(isDefined(nodeDataOpt))) |
---|
74 | | - | then throw(("Unknown Node " + nodeAddr)) |
---|
75 | | - | else { |
---|
76 | | - | let nodeDataStr = value(nodeDataOpt) |
---|
77 | | - | let nodeData = split(nodeDataStr, SEP) |
---|
78 | | - | let nodeStatus = if ((size(nodeData) >= 9)) |
---|
79 | | - | then nodeData[nRegIdxStatus] |
---|
80 | | - | else "PENDING" |
---|
81 | | - | if ((nodeStatus != expectedStatus)) |
---|
82 | | - | then throw(((((("Node " + nodeAddr) + " status should be ") + expectedStatus) + ", but actually ") + nodeStatus)) |
---|
83 | | - | else nodeDataStr |
---|
84 | | - | } |
---|
85 | | - | } |
---|
86 | | - | |
---|
87 | | - | |
---|
88 | | - | func formatDepositChangeStatus (origS,newStatus) = { |
---|
89 | | - | let orig = split(origS, SEP) |
---|
90 | | - | makeString(["%s%d%d%d%d%s%d%d", orig[nRegIdxTxAdd], orig[nRegIdxHeightAdd], orig[nRegIdxTimeAdd], orig[nRegIdxDepoAmt], orig[nRegIdxDepoAmtLeft], newStatus, toString(lastBlock.height), toString(lastBlock.timestamp)], SEP) |
---|
91 | | - | } |
---|
92 | | - | |
---|
93 | | - | |
---|
94 | | - | func resolveBeneficiaries (minerAddress,beneficiaryFromParam) = { |
---|
95 | | - | let beneficiaryFromMinerState = addressFromStringValue(getStringOrFail(minerAddress, keyBeneficiaryAddress())) |
---|
96 | | - | if ((scriptHash(minerAddress) == unit)) |
---|
97 | | - | then beneficiaryFromParam |
---|
98 | | - | else beneficiaryFromMinerState |
---|
99 | | - | } |
---|
100 | | - | |
---|
101 | | - | |
---|
102 | | - | func LeasingRelatedActions (nodeAddr,newAmount,oldId,newIdKey,newAmtKey) = { |
---|
103 | | - | let lease = Lease(nodeAddr, newAmount) |
---|
104 | | - | [LeaseCancel(oldId), lease, BinaryEntry(newIdKey, calculateLeaseId(lease)), IntegerEntry(newAmtKey, newAmount)] |
---|
| 16 | + | func pseudo (n) = { |
---|
| 17 | + | let randomHash = sha256((value(value(blockInfoByHeight(height)).vrf) + toBytes(n))) |
---|
| 18 | + | (toInt(randomHash, 25) % 37) |
---|
109 | | - | func rebalanceLeasings (amount,groupNum) = { |
---|
110 | | - | let mngPubS = valueOrElse(getString("%s%s__cfg__leasingManagerPub"), "7yMWoR9Je2xrAcgGg9QrqLAQqEGhyfCjfATjktaNdqpk") |
---|
111 | | - | if ((i.callerPublicKey != fromBase58String(mngPubS))) |
---|
112 | | - | then throw("rebalanceLeasings not authorized") |
---|
113 | | - | else { |
---|
114 | | - | let lGroupNodeListKEY = getLeaseGroupNodeListKey(groupNum) |
---|
115 | | - | let nList = split(getStringOrFail(this, lGroupNodeListKEY), SEP) |
---|
116 | | - | if ((1 > size(nList))) |
---|
117 | | - | then throw(("Empty group " + toString(groupNum))) |
---|
118 | | - | else { |
---|
119 | | - | let unleaseAmt = ((amount / size(nList)) + 1) |
---|
120 | | - | let $t044464570 = readNodeInfo(0) |
---|
121 | | - | let nAddr0 = $t044464570._1 |
---|
122 | | - | let lAmtKEY0 = $t044464570._2 |
---|
123 | | - | let lAmt0 = $t044464570._3 |
---|
124 | | - | let lIdKEY0 = $t044464570._4 |
---|
125 | | - | let lId0 = $t044464570._5 |
---|
126 | | - | func forEachNodeDoUnlease (a,node) = { |
---|
127 | | - | let lIdKEY = getLeaseIdByAddressKey(node) |
---|
128 | | - | let lId = getBinaryValue(this, lIdKEY) |
---|
129 | | - | let lAmtKEY = getLeaseAmountByAddressKey(node) |
---|
130 | | - | let lAmt = getIntegerValue(this, lAmtKEY) |
---|
131 | | - | (a ++ LeasingRelatedActions(addressFromStringValue(node), (lAmt - unleaseAmt), lId, lIdKEY, lAmtKEY)) |
---|
132 | | - | } |
---|
133 | | - | |
---|
134 | | - | ({ |
---|
135 | | - | let $l = nList |
---|
136 | | - | let $s = size($l) |
---|
137 | | - | let $acc0 = nil |
---|
138 | | - | func $f0_1 ($a,$i) = if (($i >= $s)) |
---|
139 | | - | then $a |
---|
140 | | - | else forEachNodeDoUnlease($a, $l[$i]) |
---|
141 | | - | |
---|
142 | | - | func $f0_2 ($a,$i) = if (($i >= $s)) |
---|
143 | | - | then $a |
---|
144 | | - | else throw("List size exceeds 8") |
---|
145 | | - | |
---|
146 | | - | $f0_2($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($acc0, 0), 1), 2), 3), 4), 5), 6), 7), 8) |
---|
147 | | - | } ++ LeasingRelatedActions(nAddr0, (lAmt0 + (unleaseAmt * size(nList))), lId0, lIdKEY0, lAmtKEY0)) |
---|
148 | | - | } |
---|
149 | | - | } |
---|
| 23 | + | func commit () = { |
---|
| 24 | + | let finishBlock = (height + randomDelay) |
---|
| 25 | + | $Tuple2([IntegerEntry(keyCommit(toString(i.caller)), finishBlock)], finishBlock) |
---|
161 | | - | let groupKey = getLeaseGroupNodeListKey(groupNum) |
---|
162 | | - | let groupNodeListS = getStringOrFail(this, groupKey) |
---|
163 | | - | if (!(contains(groupNodeListS, oldAddrStr))) |
---|
164 | | - | then throw(((("Group " + toString(groupNum)) + " does not contain address ") + oldAddrStr)) |
---|
165 | | - | else { |
---|
166 | | - | let doReplace = invoke(nodeRegistryContract, "replaceApprovedNode", [oldAddrStr, newAddrStr, groupNum, penaltyAmount], nil) |
---|
167 | | - | if ((doReplace == doReplace)) |
---|
168 | | - | then { |
---|
169 | | - | let oldLeaseIdKey = getLeaseIdByAddressKey(oldAddrStr) |
---|
170 | | - | let oldLeaseAmtKey = getLeaseAmountByAddressKey(oldAddrStr) |
---|
171 | | - | let leaseAmt = getIntegerValue(oldLeaseAmtKey) |
---|
172 | | - | let newLeaseIdKey = getLeaseIdByAddressKey(newAddrStr) |
---|
173 | | - | let newLeaseAmtKey = getLeaseAmountByAddressKey(newAddrStr) |
---|
174 | | - | let updatedGroupNodeListS = makeString(split(groupNodeListS, oldAddrStr), newAddrStr) |
---|
175 | | - | $Tuple2(([DeleteEntry(oldLeaseIdKey), DeleteEntry(oldLeaseAmtKey), StringEntry(groupKey, updatedGroupNodeListS)] ++ LeasingRelatedActions(addressFromStringValue(newAddrStr), leaseAmt, getBinaryValue(oldLeaseIdKey), newLeaseIdKey, newLeaseAmtKey)), unit) |
---|
176 | | - | } |
---|
177 | | - | else throw("Strict value is not equal to itself.") |
---|
178 | | - | } |
---|
| 37 | + | let rand = getRandomNumber(maxValue, finishBlock) |
---|
| 38 | + | $Tuple2([DeleteEntry(commKey)], rand) |
---|
181 | | - | |
---|
182 | | - | |
---|
183 | | - | |
---|
184 | | - | @Callable(i) |
---|
185 | | - | func removeCommunityNode (oldAddrStr,groupNum,penaltyAmount) = { |
---|
186 | | - | let mngPubS = valueOrElse(getString("%s%s__cfg__leasingManagerPub"), "7yMWoR9Je2xrAcgGg9QrqLAQqEGhyfCjfATjktaNdqpk") |
---|
187 | | - | let mngPub = fromBase58String(mngPubS) |
---|
188 | | - | if ((i.callerPublicKey != mngPub)) |
---|
189 | | - | then throw("removeCommunityNode not authorized") |
---|
190 | | - | else { |
---|
191 | | - | let groupKey = getLeaseGroupNodeListKey(groupNum) |
---|
192 | | - | let groupNodeListS = getStringOrFail(this, groupKey) |
---|
193 | | - | if (!(contains(groupNodeListS, oldAddrStr))) |
---|
194 | | - | then throw(((("Group " + toString(groupNum)) + " does not contain address ") + oldAddrStr)) |
---|
195 | | - | else { |
---|
196 | | - | let doRemove = invoke(nodeRegistryContract, "removeApprovedNode", [oldAddrStr, penaltyAmount], nil) |
---|
197 | | - | if ((doRemove == doRemove)) |
---|
198 | | - | then { |
---|
199 | | - | let oldLeaseIdKey = getLeaseIdByAddressKey(oldAddrStr) |
---|
200 | | - | let oldLeaseAmtKey = getLeaseAmountByAddressKey(oldAddrStr) |
---|
201 | | - | let leaseAmt = getIntegerValue(oldLeaseAmtKey) |
---|
202 | | - | let $t074507574 = readNodeInfo(0) |
---|
203 | | - | let nAddr0 = $t074507574._1 |
---|
204 | | - | let lAmtKEY0 = $t074507574._2 |
---|
205 | | - | let lAmt0 = $t074507574._3 |
---|
206 | | - | let lIdKEY0 = $t074507574._4 |
---|
207 | | - | let lId0 = $t074507574._5 |
---|
208 | | - | let updatedGroupNodeListS = makeString(split(groupNodeListS, oldAddrStr), "") |
---|
209 | | - | $Tuple2(([DeleteEntry(oldLeaseIdKey), DeleteEntry(oldLeaseAmtKey), StringEntry(groupKey, updatedGroupNodeListS)] ++ LeasingRelatedActions(nAddr0, (lAmt0 + leaseAmt), getBinaryValue(oldLeaseIdKey), lIdKEY0, lAmtKEY0)), unit) |
---|
210 | | - | } |
---|
211 | | - | else throw("Strict value is not equal to itself.") |
---|
212 | | - | } |
---|
213 | | - | } |
---|
214 | | - | } |
---|
215 | | - | |
---|
216 | | - | |
---|
217 | | - | |
---|
218 | | - | @Callable(i) |
---|
219 | | - | func replaceApprovedNode (oldNodeAddrStr,newNodeAddrStr,groupNum,penaltyAmount) = if ((i.caller != neutrinoContract)) |
---|
220 | | - | then throw("permissions denied") |
---|
221 | | - | else { |
---|
222 | | - | let oldDataStr = readNodeDataAndValidateStatus(oldNodeAddrStr, "APPROVED") |
---|
223 | | - | let newDataStr = readNodeDataAndValidateStatus(newNodeAddrStr, "PENDING") |
---|
224 | | - | let approvedNodeListS = getStringOrFail(this, keyApprovedNodeList()) |
---|
225 | | - | if (!(contains(approvedNodeListS, oldNodeAddrStr))) |
---|
226 | | - | then throw(("approvedNodeList does not contain " + oldNodeAddrStr)) |
---|
227 | | - | else { |
---|
228 | | - | let updatedNodeListS = makeString_2C(split_4C(approvedNodeListS, oldNodeAddrStr), newNodeAddrStr) |
---|
229 | | - | let oldNodeAddr = addressFromStringValue(oldNodeAddrStr) |
---|
230 | | - | let beneficiaryAddress = resolveBeneficiaries(oldNodeAddr, oldNodeAddr) |
---|
231 | | - | let depoAmount = parseIntValue(split(oldDataStr, SEP)[nRegIdxDepoAmt]) |
---|
232 | | - | let actions = [StringEntry(keyApprovedNodeList(), updatedNodeListS), DeleteEntry(keyNodeRegistry(oldNodeAddrStr)), StringEntry(keyNodeRegistry(newNodeAddrStr), formatDepositChangeStatus(newDataStr, "APPROVED"))] |
---|
233 | | - | $Tuple2(if ((depoAmount > penaltyAmount)) |
---|
234 | | - | then (actions :+ ScriptTransfer(beneficiaryAddress, (depoAmount - penaltyAmount), unit)) |
---|
235 | | - | else actions, unit) |
---|
236 | | - | } |
---|
237 | | - | } |
---|
238 | | - | |
---|
239 | | - | |
---|
240 | | - | |
---|
241 | | - | @Callable(i) |
---|
242 | | - | func removeApprovedNode (nodeAddrStr,penaltyAmount) = if ((i.caller != neutrinoContract)) |
---|
243 | | - | then throw("permissions denied") |
---|
244 | | - | else { |
---|
245 | | - | let oldDataStr = readNodeDataAndValidateStatus(nodeAddrStr, "APPROVED") |
---|
246 | | - | let approvedNodeListS = getStringOrFail(this, keyApprovedNodeList()) |
---|
247 | | - | if (!(contains(approvedNodeListS, nodeAddrStr))) |
---|
248 | | - | then throw(("approvedNodeList does not contain " + nodeAddrStr)) |
---|
249 | | - | else { |
---|
250 | | - | let updatedNodeListS = makeString_2C(split_4C(approvedNodeListS, nodeAddrStr), "") |
---|
251 | | - | let nodeAddr = addressFromStringValue(nodeAddrStr) |
---|
252 | | - | let beneficiaryAddress = resolveBeneficiaries(nodeAddr, nodeAddr) |
---|
253 | | - | let depoAmount = parseIntValue(split(oldDataStr, SEP)[nRegIdxDepoAmt]) |
---|
254 | | - | let actions = [StringEntry(keyApprovedNodeList(), updatedNodeListS), DeleteEntry(keyNodeRegistry(nodeAddrStr))] |
---|
255 | | - | $Tuple2(if ((depoAmount > penaltyAmount)) |
---|
256 | | - | then (actions :+ ScriptTransfer(beneficiaryAddress, (depoAmount - penaltyAmount), unit)) |
---|
257 | | - | else actions, unit) |
---|
258 | | - | } |
---|
259 | | - | } |
---|
260 | | - | |
---|
261 | | - | |
---|
262 | | - | |
---|
263 | | - | @Callable(i) |
---|
264 | | - | func returnDeposit (nodeAddressStr) = if ((size(i.payments) != 0)) |
---|
265 | | - | then throw("No payments allowed") |
---|
266 | | - | else { |
---|
267 | | - | let callerAddress = i.caller |
---|
268 | | - | let nodeRegistryKEY = keyNodeRegistry(nodeAddressStr) |
---|
269 | | - | let nodeDataOpt = getString(this, nodeRegistryKEY) |
---|
270 | | - | if (!(isDefined(nodeDataOpt))) |
---|
271 | | - | then throw(("Address is not applied as neutrino node: " + nodeAddressStr)) |
---|
272 | | - | else { |
---|
273 | | - | let approvedNodeList = getStringOrFail(this, keyApprovedNodeList()) |
---|
274 | | - | if (contains(approvedNodeList, nodeAddressStr)) |
---|
275 | | - | then throw("Impossible to return deposit from approved Node, please contact support team") |
---|
276 | | - | else { |
---|
277 | | - | let nodeData = split(value(nodeDataOpt), SEP) |
---|
278 | | - | let status = if ((size(nodeData) >= 9)) |
---|
279 | | - | then nodeData[nRegIdxStatus] |
---|
280 | | - | else "PENDING" |
---|
281 | | - | if ((status != "PENDING")) |
---|
282 | | - | then throw("Application status is not PENDING!") |
---|
283 | | - | else { |
---|
284 | | - | let depoAmount = parseIntValue(nodeData[nRegIdxDepoAmt]) |
---|
285 | | - | let nodeAddress = addressFromStringValue(nodeAddressStr) |
---|
286 | | - | let beneficiaryAddress = resolveBeneficiaries(nodeAddress, nodeAddress) |
---|
287 | | - | if (!(if (if ((callerAddress == this)) |
---|
288 | | - | then true |
---|
289 | | - | else if ((beneficiaryAddress != nodeAddress)) |
---|
290 | | - | then (callerAddress == beneficiaryAddress) |
---|
291 | | - | else false) |
---|
292 | | - | then true |
---|
293 | | - | else if ((beneficiaryAddress == nodeAddress)) |
---|
294 | | - | then (callerAddress == nodeAddress) |
---|
295 | | - | else false)) |
---|
296 | | - | then throw("returnDeposit permissions denied") |
---|
297 | | - | else [DeleteEntry(nodeRegistryKEY), ScriptTransfer(beneficiaryAddress, depoAmount, unit)] |
---|
298 | | - | } |
---|
299 | | - | } |
---|
300 | | - | } |
---|
301 | | - | } |
---|