Skip to content

Commit e27aafa

Browse files
authored
Merge pull request #81 from Aryan9592/patch-1
Update MachineStateManager.sol: Enhanced 'for' loops
2 parents 8ef0644 + 60375bb commit e27aafa

File tree

3 files changed

+181
-75
lines changed

3 files changed

+181
-75
lines changed

darc-protocol/contracts/protocol/Dashboard/Dashboard.sol

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import "../Runtime/Runtime.sol";
88
* @author DARC Team
99
* @notice The dashboard (a set of view functions) of the DARC machine state,
1010
* which is used to read the machine state, including machine state, voting state,
11-
* token informatin, plugin information, etc.
11+
* token information, plugin information, etc.
1212
*/
1313
contract Dashboard is MachineStateManager {
1414

@@ -20,14 +20,18 @@ contract Dashboard is MachineStateManager {
2020
}
2121

2222
/**
23-
* @notice Get the avaiilable token classes
23+
* @notice Get the available token classes
2424
*/
2525
function getNumberOfTokenClasses() public view returns (uint256) {
26-
uint256 i = 0;
27-
for (; i < currentMachineState.tokenList.length; i++) {
26+
uint256 i;
27+
for (; i < currentMachineState.tokenList.length;) {
2828
if (!currentMachineState.tokenList[i].bIsInitialized) {
2929
break;
3030
}
31+
32+
unchecked {
33+
++i;
34+
}
3135
}
3236
return i;
3337
}

darc-protocol/contracts/protocol/MachineStateManager.sol

Lines changed: 107 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ contract MachineStateManager {
8383
dividendBufferSize = 10000;
8484

8585
/**
86-
* We need to initialze the first plugin for both before operation
86+
* We need to initialize the first plugin for both before operation
8787
* plugin system and after operation plugin system.
8888
*
8989
* Before-op Rule: If the operation is executed by the initial owner, then skip the plugin system
@@ -167,22 +167,31 @@ contract MachineStateManager {
167167

168168
// 1. clone the plugin list
169169
sandboxMachineState.afterOpPlugins = new Plugin[](currentMachineState.afterOpPlugins.length);
170-
for (uint256 i = 0; i < currentMachineState.afterOpPlugins.length; i++) {
170+
for (uint256 i; i < currentMachineState.afterOpPlugins.length;) {
171171
sandboxMachineState.afterOpPlugins[i] = currentMachineState.afterOpPlugins[i];
172+
unchecked {
173+
++i;
174+
}
172175
}
173176
sandboxMachineState.beforeOpPlugins = new Plugin[](currentMachineState.beforeOpPlugins.length);
174-
for (uint256 i = 0; i < currentMachineState.beforeOpPlugins.length; i++) {
177+
for (uint256 i; i < currentMachineState.beforeOpPlugins.length;) {
175178
sandboxMachineState.beforeOpPlugins[i] = currentMachineState.beforeOpPlugins[i];
179+
unchecked {
180+
++i;
181+
}
176182
}
177183

178184
// 2. clone the token list
179185
// 2.1 clean all the token info from the sandbox token info map
180-
for (uint256 i = 0; i < sandboxMachineState.tokenList.length; i++) {
186+
for (uint256 i; i < sandboxMachineState.tokenList.length;) {
181187

182188
// if the token is initialized, then clean the token info
183189
if (sandboxMachineState.tokenList[i].bIsInitialized == true) {
184-
for (uint256 j = 0; j < sandboxMachineState.tokenList[i].ownerList.length; j++) {
190+
for (uint256 j; j < sandboxMachineState.tokenList[i].ownerList.length;) {
185191
delete sandboxMachineState.tokenList[i].tokenBalance[sandboxMachineState.tokenList[i].ownerList[j]];
192+
unchecked {
193+
++j;
194+
}
186195
}
187196

188197
sandboxMachineState.tokenList[i].ownerList = new address[](0);
@@ -193,10 +202,14 @@ contract MachineStateManager {
193202
sandboxMachineState.tokenList[i].tokenInfo = "";
194203
sandboxMachineState.tokenList[i].totalSupply = 0;
195204
}
205+
206+
unchecked {
207+
++i;
208+
}
196209
}
197210

198211
// 2.2 Clone the token list of the current state to the sandbox state
199-
for (uint256 i = 0; i < currentMachineState.tokenList.length; i++) {
212+
for (uint256 i; i < currentMachineState.tokenList.length;) {
200213
// if the token is initialized, then clone the token info
201214
if (currentMachineState.tokenList[i].bIsInitialized == true) {
202215
sandboxMachineState.tokenList[i].ownerList = currentMachineState.tokenList[i].ownerList;
@@ -207,111 +220,159 @@ contract MachineStateManager {
207220
sandboxMachineState.tokenList[i].tokenInfo = currentMachineState.tokenList[i].tokenInfo;
208221
sandboxMachineState.tokenList[i].totalSupply = currentMachineState.tokenList[i].totalSupply;
209222
}
223+
224+
unchecked {
225+
++i;
226+
}
210227
}
211228

212229
// 3. clone the member list
213230
// 3.1 clean all the member info from the member info map
214-
for (uint256 i = 0; i < currentMachineState.memberList.length; i++) {
231+
for (uint256 i; i < currentMachineState.memberList.length;) {
215232
delete sandboxMachineState.memberInfoMap[currentMachineState.memberList[i]];
233+
unchecked {
234+
++i;
235+
}
216236
}
217237

218238
// 3.2 delete the member list of the sandbox state and clone the member list of
219239
// the current state to the sandbox state
220240
sandboxMachineState.memberList = new address[](currentMachineState.memberList.length);
221-
for (uint256 i = 0; i < currentMachineState.memberList.length; i++) {
241+
for (uint256 i; i < currentMachineState.memberList.length;) {
222242
sandboxMachineState.memberList[i] = currentMachineState.memberList[i];
243+
unchecked {
244+
++i;
245+
}
223246
}
224247

225248
// 3.3 clone the member info map of the current state to the sandbox state
226-
for (uint256 i = 0; i < currentMachineState.memberList.length; i++) {
249+
for (uint256 i; i < currentMachineState.memberList.length;) {
227250
sandboxMachineState.memberInfoMap[currentMachineState.memberList[i]]
228251
= currentMachineState.memberInfoMap[currentMachineState.memberList[i]];
252+
unchecked {
253+
++i;
254+
}
229255
}
230256

231257
// 4. Clone the operationLogMap
232-
// 4.1 clean all the operation log from the operation log map
233-
for (uint256 i = 0; i < sandboxMachineState.operationLogMapAddressList.length; i++) {
258+
// 4.1 clean all the operation logs from the operation log map
259+
for (uint256 i; i < sandboxMachineState.operationLogMapAddressList.length;) {
234260
delete sandboxMachineState.operationLogMap[sandboxMachineState.operationLogMapAddressList[i]];
261+
unchecked {
262+
++i;
263+
}
235264
}
236265
// 4.2 copy the operation log address list from current machine state to sandbox
237266
sandboxMachineState.operationLogMapAddressList = new address[](currentMachineState.operationLogMapAddressList.length);
238-
for (uint256 i = 0; i < currentMachineState.operationLogMapAddressList.length; i++) {
267+
for (uint256 i; i < currentMachineState.operationLogMapAddressList.length;) {
239268
sandboxMachineState.operationLogMapAddressList[i] = currentMachineState.operationLogMapAddressList[i];
269+
unchecked {
270+
++i;
271+
}
240272
}
241273
// 4.3 copy the operation log map from current machine state to sandbox
242-
for (uint256 i = 0; i < currentMachineState.operationLogMapAddressList.length; i++) {
274+
for (uint256 i; i < currentMachineState.operationLogMapAddressList.length;) {
243275
sandboxMachineState.operationLogMap[currentMachineState.operationLogMapAddressList[i]]
244276
= currentMachineState.operationLogMap[currentMachineState.operationLogMapAddressList[i]];
277+
unchecked {
278+
++i;
279+
}
245280
}
246281

247282
// 5. Clone the machine state parameters
248283
sandboxMachineState.machineStateParameters = currentMachineState.machineStateParameters;
249284

250285
// 6. Clone the withdrawable cash from sandbox to current machine state
251-
// 6.1 clean all value in sandbox
252-
for (uint256 i = 0; i < sandboxMachineState.withdrawableCashOwnerList.length; i++) {
286+
// 6.1 clean all values in sandbox
287+
for (uint256 i; i < sandboxMachineState.withdrawableCashOwnerList.length;) {
253288
delete sandboxMachineState.withdrawableCashMap[sandboxMachineState.withdrawableCashOwnerList[i]];
289+
unchecked {
290+
++i;
291+
}
254292
}
255293

256294
// 6.2 copy the withdrawable cash owner list from current machine state to sandbox
257295
sandboxMachineState.withdrawableCashOwnerList = new address[](currentMachineState.withdrawableCashOwnerList.length);
258-
for (uint256 i = 0; i < currentMachineState.withdrawableCashOwnerList.length; i++) {
296+
for (uint256 i; i < currentMachineState.withdrawableCashOwnerList.length;) {
259297
sandboxMachineState.withdrawableCashOwnerList[i] = currentMachineState.withdrawableCashOwnerList[i];
298+
unchecked {
299+
++i;
300+
}
260301
}
261302

262303
// 6.3 copy the withdrawable cash map from current machine state to sandbox
263-
for (uint256 i = 0; i < currentMachineState.withdrawableCashOwnerList.length; i++) {
304+
for (uint256 i; i < currentMachineState.withdrawableCashOwnerList.length;) {
264305
sandboxMachineState.withdrawableCashMap[currentMachineState.withdrawableCashOwnerList[i]]
265306
= currentMachineState.withdrawableCashMap[currentMachineState.withdrawableCashOwnerList[i]];
307+
unchecked {
308+
++i;
309+
}
266310
}
267311

268-
// 7. Clone the wirhdarwable dividends from sandbox to current machine state
312+
// 7. Clone the withdrawable dividends from sandbox to current machine state
269313
// 7.1 clean all value in sandbox
270-
for (uint256 i = 0; i < sandboxMachineState.withdrawableDividendOwnerList.length; i++) {
314+
for (uint256 i; i < sandboxMachineState.withdrawableDividendOwnerList.length;) {
271315
delete sandboxMachineState.withdrawableDividendMap[sandboxMachineState.withdrawableDividendOwnerList[i]];
316+
unchecked {
317+
++i;
318+
}
272319
}
273320

274321
// 7.2 copy the withdrawable dividends owner list from current machine state to sandbox
275322
sandboxMachineState.withdrawableDividendOwnerList = new address[](currentMachineState.withdrawableDividendOwnerList.length);
276-
for (uint256 i = 0; i < currentMachineState.withdrawableDividendOwnerList.length; i++) {
323+
for (uint256 i; i < currentMachineState.withdrawableDividendOwnerList.length;) {
277324
sandboxMachineState.withdrawableDividendOwnerList[i] = currentMachineState.withdrawableDividendOwnerList[i];
325+
unchecked {
326+
++i;
327+
}
278328
}
279329

280330
// 7.3 copy the withdrawable dividends map from current machine state to sandbox
281-
for (uint256 i = 0; i < currentMachineState.withdrawableDividendOwnerList.length; i++) {
331+
for (uint256 i; i < currentMachineState.withdrawableDividendOwnerList.length;) {
282332
sandboxMachineState.withdrawableDividendMap[currentMachineState.withdrawableDividendOwnerList[i]]
283333
= currentMachineState.withdrawableDividendMap[currentMachineState.withdrawableDividendOwnerList[i]];
334+
unchecked {
335+
++i;
336+
}
284337
}
285338

286339
}
287340

288341

289342
/**
290-
* This function that get the total number of tokens for a certain token class
343+
* This function gets the total number of tokens for a certain token class
291344
* @param bIsSandbox The flag to indicate whether is the sandbox
292345
* @param tokenClassIndex The index of the token class
293346
*/
294347
function totalTokensPerTokenClass(bool bIsSandbox, uint256 tokenClassIndex) internal view returns (uint256) {
295348
if (bIsSandbox) {
296349
uint256 numberOfOwners = sandboxMachineState.tokenList[tokenClassIndex].ownerList.length;
297-
uint256 totalTokens = 0;
350+
uint256 totalTokens;
298351
bool bIsValid = true;
299-
for (uint256 i = 0; i < numberOfOwners; i++) {
352+
for (uint256 i; i < numberOfOwners;) {
300353
address currentOwner = sandboxMachineState.tokenList[tokenClassIndex].ownerList[i];
301354
uint256 currentNumberOfTokens = sandboxMachineState.tokenList[tokenClassIndex].tokenBalance[currentOwner];
302355
(bIsValid, totalTokens) = SafeMathUpgradeable.tryAdd(totalTokens, currentNumberOfTokens);
303356
require(bIsValid, "totalTokensPerTokenClass: totalTokens overflow");
357+
358+
unchecked {
359+
++i;
360+
}
304361
}
305362
return totalTokens;
306363
} else {
307364
uint256 numberOfOwners = currentMachineState.tokenList[tokenClassIndex].ownerList.length;
308-
uint256 totalTokens = 0;
365+
uint256 totalTokens;
309366
bool bIsValid = true;
310-
for (uint256 i = 0; i < numberOfOwners; i++) {
367+
for (uint256 i; i < numberOfOwners;) {
311368
address currentOwner = currentMachineState.tokenList[tokenClassIndex].ownerList[i];
312369
uint256 currentNumberOfTokens = currentMachineState.tokenList[tokenClassIndex].tokenBalance[currentOwner];
313370
(bIsValid, totalTokens) = SafeMathUpgradeable.tryAdd(totalTokens, currentNumberOfTokens);
314371
require(bIsValid, "totalTokensPerTokenClass: totalTokens overflow");
372+
373+
unchecked {
374+
++i;
375+
}
315376
}
316377
return totalTokens;
317378
}
@@ -326,37 +387,37 @@ contract MachineStateManager {
326387
if (bIsSandbox) {
327388
uint256 dividendWeightUnit = sandboxMachineState.tokenList[tokenClassIndex].dividendWeight;
328389
bool bIsValid = true;
329-
uint256 dividendWeight = 0;
390+
uint256 dividendWeight;
330391
(bIsValid, dividendWeight) = SafeMathUpgradeable.tryMul(dividendWeightUnit, totalTokensPerTokenClass(bIsSandbox, tokenClassIndex));
331392
require(bIsValid, "sumDividendWeightForTokenClass: dividendWeight overflow");
332393
return dividendWeight;
333394
} else {
334395
uint256 dividendWeightUnit = currentMachineState.tokenList[tokenClassIndex].dividendWeight;
335396
bool bIsValid = true;
336-
uint256 dividendWeight = 0;
397+
uint256 dividendWeight;
337398
(bIsValid, dividendWeight) = SafeMathUpgradeable.tryMul(dividendWeightUnit, totalTokensPerTokenClass(bIsSandbox, tokenClassIndex));
338399
require(bIsValid, "sumDividendWeightForTokenClass: dividendWeight overflow");
339400
return dividendWeight;
340401
}
341402
}
342403

343404
/**
344-
* Return the number of voting weight for a certain token class
405+
* Return the number of voting weights for a certain token class
345406
* @param bIsSandbox Whether is the sandbox
346407
* @param tokenClassIndex The index of the token class
347408
*/
348409
function sumVotingWeightForTokenClass ( bool bIsSandbox, uint256 tokenClassIndex ) internal view returns (uint256) {
349410
if (bIsSandbox) {
350411
uint256 votingWeightUnit = sandboxMachineState.tokenList[tokenClassIndex].votingWeight;
351412
bool bIsValid = true;
352-
uint256 votingWeight = 0;
413+
uint256 votingWeight;
353414
(bIsValid, votingWeight) = SafeMathUpgradeable.tryMul(votingWeightUnit, totalTokensPerTokenClass(bIsSandbox, tokenClassIndex));
354415
require(bIsValid, "sumVotingWeightForTokenClass: votingWeight overflow");
355416
return votingWeight;
356417
} else {
357418
uint256 votingWeightUnit = currentMachineState.tokenList[tokenClassIndex].votingWeight;
358419
bool bIsValid = true;
359-
uint256 votingWeight = 0;
420+
uint256 votingWeight;
360421
(bIsValid, votingWeight) = SafeMathUpgradeable.tryMul(votingWeightUnit, totalTokensPerTokenClass(bIsSandbox, tokenClassIndex));
361422
require(bIsValid, "sumVotingWeightForTokenClass: votingWeight overflow");
362423
return votingWeight;
@@ -374,13 +435,13 @@ contract MachineStateManager {
374435
require(sandboxMachineState.machineStateParameters.dividendPermyriadPerTransaction < 10000,
375436
ErrorMsg.By(15));
376437

377-
// make sure that cycle counter is less than the threashold
438+
// make sure that cycle counter is less than the threshold
378439
require(sandboxMachineState.machineStateParameters.dividendCycleCounter >=
379440
sandboxMachineState.machineStateParameters.dividendCycleOfTransactions, ErrorMsg.By(16));
380441

381442
// 1. calculate the total amount of dividends to be offered
382443
bool bIsValid = true;
383-
uint256 totalDividends = 0;
444+
uint256 totalDividends;
384445

385446
(bIsValid, totalDividends) = SafeMathUpgradeable.tryMul(
386447
sandboxMachineState.machineStateParameters.currentCashBalanceForDividends,
@@ -391,10 +452,10 @@ contract MachineStateManager {
391452
10000);
392453
require (bIsValid, ErrorMsg.By(12));
393454

394-
// 2. calculate the total dividends weight of all dividendable tokens
395-
uint256 totalDividendsWeight = 0;
455+
// 2. calculate the total dividend weight of all dividendable tokens
456+
uint256 totalDividendsWeight;
396457

397-
for (uint256 index=0; index < sandboxMachineState.tokenList.length; index++) {
458+
for (uint256 index; index < sandboxMachineState.tokenList.length;) {
398459

399460
if (sandboxMachineState.tokenList[index].bIsInitialized == false) {
400461
break;
@@ -404,10 +465,14 @@ contract MachineStateManager {
404465
totalDividendsWeight,
405466
sumDividendWeightForTokenClass(bIsSandbox, index));
406467
require(bIsValid, ErrorMsg.By(12));
468+
469+
unchecked {
470+
++index;
471+
}
407472
}
408473

409474
// 3. calculate the cash dividend per unit
410-
uint256 cashPerUnit = 0;
475+
uint256 cashPerUnit;
411476
(bIsValid, cashPerUnit) = SafeMathUpgradeable.tryDiv(
412477
totalDividends,
413478
totalDividendsWeight);
@@ -418,7 +483,7 @@ contract MachineStateManager {
418483
require(currentMachineState.machineStateParameters.dividendPermyriadPerTransaction < 10000,
419484
ErrorMsg.By(15));
420485

421-
// make sure that cycle counter is less than the threashold
486+
// make sure that cycle counter is less than the threshold
422487
require(currentMachineState.machineStateParameters.dividendCycleCounter >=
423488
currentMachineState.machineStateParameters.dividendCycleOfTransactions, ErrorMsg.By(16));
424489

@@ -435,8 +500,8 @@ contract MachineStateManager {
435500
10000);
436501
require (bIsValid, ErrorMsg.By(12));
437502

438-
// 2. calculate the total dividends weight of all dividendable tokens
439-
uint256 totalDividendsWeight = 0;
503+
// 2. calculate the total dividend weight of all dividendable tokens
504+
uint256 totalDividendsWeight;
440505

441506
for (uint256 index=0; index < currentMachineState.tokenList.length; index++) {
442507

@@ -451,12 +516,12 @@ contract MachineStateManager {
451516
}
452517

453518
// 3. calculate the cash dividend per unit
454-
uint256 cashPerUnit = 0;
519+
uint256 cashPerUnit;
455520
(bIsValid, cashPerUnit) = SafeMathUpgradeable.tryDiv(
456521
totalDividends,
457522
totalDividendsWeight);
458523

459524
return (cashPerUnit);
460525
}
461526
}
462-
}
527+
}

0 commit comments

Comments
 (0)