@@ -83,7 +83,7 @@ contract MachineStateManager {
83
83
dividendBufferSize = 10000 ;
84
84
85
85
/**
86
- * We need to initialze the first plugin for both before operation
86
+ * We need to initialize the first plugin for both before operation
87
87
* plugin system and after operation plugin system.
88
88
*
89
89
* Before-op Rule: If the operation is executed by the initial owner, then skip the plugin system
@@ -167,22 +167,31 @@ contract MachineStateManager {
167
167
168
168
// 1. clone the plugin list
169
169
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 ;) {
171
171
sandboxMachineState.afterOpPlugins[i] = currentMachineState.afterOpPlugins[i];
172
+ unchecked {
173
+ ++ i;
174
+ }
172
175
}
173
176
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 ;) {
175
178
sandboxMachineState.beforeOpPlugins[i] = currentMachineState.beforeOpPlugins[i];
179
+ unchecked {
180
+ ++ i;
181
+ }
176
182
}
177
183
178
184
// 2. clone the token list
179
185
// 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 ;) {
181
187
182
188
// if the token is initialized, then clean the token info
183
189
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 ;) {
185
191
delete sandboxMachineState.tokenList[i].tokenBalance[sandboxMachineState.tokenList[i].ownerList[j]];
192
+ unchecked {
193
+ ++ j;
194
+ }
186
195
}
187
196
188
197
sandboxMachineState.tokenList[i].ownerList = new address [](0 );
@@ -193,10 +202,14 @@ contract MachineStateManager {
193
202
sandboxMachineState.tokenList[i].tokenInfo = "" ;
194
203
sandboxMachineState.tokenList[i].totalSupply = 0 ;
195
204
}
205
+
206
+ unchecked {
207
+ ++ i;
208
+ }
196
209
}
197
210
198
211
// 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 ;) {
200
213
// if the token is initialized, then clone the token info
201
214
if (currentMachineState.tokenList[i].bIsInitialized == true ) {
202
215
sandboxMachineState.tokenList[i].ownerList = currentMachineState.tokenList[i].ownerList;
@@ -207,111 +220,159 @@ contract MachineStateManager {
207
220
sandboxMachineState.tokenList[i].tokenInfo = currentMachineState.tokenList[i].tokenInfo;
208
221
sandboxMachineState.tokenList[i].totalSupply = currentMachineState.tokenList[i].totalSupply;
209
222
}
223
+
224
+ unchecked {
225
+ ++ i;
226
+ }
210
227
}
211
228
212
229
// 3. clone the member list
213
230
// 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 ;) {
215
232
delete sandboxMachineState.memberInfoMap[currentMachineState.memberList[i]];
233
+ unchecked {
234
+ ++ i;
235
+ }
216
236
}
217
237
218
238
// 3.2 delete the member list of the sandbox state and clone the member list of
219
239
// the current state to the sandbox state
220
240
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 ;) {
222
242
sandboxMachineState.memberList[i] = currentMachineState.memberList[i];
243
+ unchecked {
244
+ ++ i;
245
+ }
223
246
}
224
247
225
248
// 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 ;) {
227
250
sandboxMachineState.memberInfoMap[currentMachineState.memberList[i]]
228
251
= currentMachineState.memberInfoMap[currentMachineState.memberList[i]];
252
+ unchecked {
253
+ ++ i;
254
+ }
229
255
}
230
256
231
257
// 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 ;) {
234
260
delete sandboxMachineState.operationLogMap[sandboxMachineState.operationLogMapAddressList[i]];
261
+ unchecked {
262
+ ++ i;
263
+ }
235
264
}
236
265
// 4.2 copy the operation log address list from current machine state to sandbox
237
266
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 ;) {
239
268
sandboxMachineState.operationLogMapAddressList[i] = currentMachineState.operationLogMapAddressList[i];
269
+ unchecked {
270
+ ++ i;
271
+ }
240
272
}
241
273
// 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 ;) {
243
275
sandboxMachineState.operationLogMap[currentMachineState.operationLogMapAddressList[i]]
244
276
= currentMachineState.operationLogMap[currentMachineState.operationLogMapAddressList[i]];
277
+ unchecked {
278
+ ++ i;
279
+ }
245
280
}
246
281
247
282
// 5. Clone the machine state parameters
248
283
sandboxMachineState.machineStateParameters = currentMachineState.machineStateParameters;
249
284
250
285
// 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 ;) {
253
288
delete sandboxMachineState.withdrawableCashMap[sandboxMachineState.withdrawableCashOwnerList[i]];
289
+ unchecked {
290
+ ++ i;
291
+ }
254
292
}
255
293
256
294
// 6.2 copy the withdrawable cash owner list from current machine state to sandbox
257
295
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 ;) {
259
297
sandboxMachineState.withdrawableCashOwnerList[i] = currentMachineState.withdrawableCashOwnerList[i];
298
+ unchecked {
299
+ ++ i;
300
+ }
260
301
}
261
302
262
303
// 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 ;) {
264
305
sandboxMachineState.withdrawableCashMap[currentMachineState.withdrawableCashOwnerList[i]]
265
306
= currentMachineState.withdrawableCashMap[currentMachineState.withdrawableCashOwnerList[i]];
307
+ unchecked {
308
+ ++ i;
309
+ }
266
310
}
267
311
268
- // 7. Clone the wirhdarwable dividends from sandbox to current machine state
312
+ // 7. Clone the withdrawable dividends from sandbox to current machine state
269
313
// 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 ;) {
271
315
delete sandboxMachineState.withdrawableDividendMap[sandboxMachineState.withdrawableDividendOwnerList[i]];
316
+ unchecked {
317
+ ++ i;
318
+ }
272
319
}
273
320
274
321
// 7.2 copy the withdrawable dividends owner list from current machine state to sandbox
275
322
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 ;) {
277
324
sandboxMachineState.withdrawableDividendOwnerList[i] = currentMachineState.withdrawableDividendOwnerList[i];
325
+ unchecked {
326
+ ++ i;
327
+ }
278
328
}
279
329
280
330
// 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 ;) {
282
332
sandboxMachineState.withdrawableDividendMap[currentMachineState.withdrawableDividendOwnerList[i]]
283
333
= currentMachineState.withdrawableDividendMap[currentMachineState.withdrawableDividendOwnerList[i]];
334
+ unchecked {
335
+ ++ i;
336
+ }
284
337
}
285
338
286
339
}
287
340
288
341
289
342
/**
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
291
344
* @param bIsSandbox The flag to indicate whether is the sandbox
292
345
* @param tokenClassIndex The index of the token class
293
346
*/
294
347
function totalTokensPerTokenClass (bool bIsSandbox , uint256 tokenClassIndex ) internal view returns (uint256 ) {
295
348
if (bIsSandbox) {
296
349
uint256 numberOfOwners = sandboxMachineState.tokenList[tokenClassIndex].ownerList.length ;
297
- uint256 totalTokens = 0 ;
350
+ uint256 totalTokens;
298
351
bool bIsValid = true ;
299
- for (uint256 i = 0 ; i < numberOfOwners; i ++ ) {
352
+ for (uint256 i; i < numberOfOwners;) {
300
353
address currentOwner = sandboxMachineState.tokenList[tokenClassIndex].ownerList[i];
301
354
uint256 currentNumberOfTokens = sandboxMachineState.tokenList[tokenClassIndex].tokenBalance[currentOwner];
302
355
(bIsValid, totalTokens) = SafeMathUpgradeable.tryAdd (totalTokens, currentNumberOfTokens);
303
356
require (bIsValid, "totalTokensPerTokenClass: totalTokens overflow " );
357
+
358
+ unchecked {
359
+ ++ i;
360
+ }
304
361
}
305
362
return totalTokens;
306
363
} else {
307
364
uint256 numberOfOwners = currentMachineState.tokenList[tokenClassIndex].ownerList.length ;
308
- uint256 totalTokens = 0 ;
365
+ uint256 totalTokens;
309
366
bool bIsValid = true ;
310
- for (uint256 i = 0 ; i < numberOfOwners; i ++ ) {
367
+ for (uint256 i; i < numberOfOwners;) {
311
368
address currentOwner = currentMachineState.tokenList[tokenClassIndex].ownerList[i];
312
369
uint256 currentNumberOfTokens = currentMachineState.tokenList[tokenClassIndex].tokenBalance[currentOwner];
313
370
(bIsValid, totalTokens) = SafeMathUpgradeable.tryAdd (totalTokens, currentNumberOfTokens);
314
371
require (bIsValid, "totalTokensPerTokenClass: totalTokens overflow " );
372
+
373
+ unchecked {
374
+ ++ i;
375
+ }
315
376
}
316
377
return totalTokens;
317
378
}
@@ -326,37 +387,37 @@ contract MachineStateManager {
326
387
if (bIsSandbox) {
327
388
uint256 dividendWeightUnit = sandboxMachineState.tokenList[tokenClassIndex].dividendWeight;
328
389
bool bIsValid = true ;
329
- uint256 dividendWeight = 0 ;
390
+ uint256 dividendWeight;
330
391
(bIsValid, dividendWeight) = SafeMathUpgradeable.tryMul (dividendWeightUnit, totalTokensPerTokenClass (bIsSandbox, tokenClassIndex));
331
392
require (bIsValid, "sumDividendWeightForTokenClass: dividendWeight overflow " );
332
393
return dividendWeight;
333
394
} else {
334
395
uint256 dividendWeightUnit = currentMachineState.tokenList[tokenClassIndex].dividendWeight;
335
396
bool bIsValid = true ;
336
- uint256 dividendWeight = 0 ;
397
+ uint256 dividendWeight;
337
398
(bIsValid, dividendWeight) = SafeMathUpgradeable.tryMul (dividendWeightUnit, totalTokensPerTokenClass (bIsSandbox, tokenClassIndex));
338
399
require (bIsValid, "sumDividendWeightForTokenClass: dividendWeight overflow " );
339
400
return dividendWeight;
340
401
}
341
402
}
342
403
343
404
/**
344
- * Return the number of voting weight for a certain token class
405
+ * Return the number of voting weights for a certain token class
345
406
* @param bIsSandbox Whether is the sandbox
346
407
* @param tokenClassIndex The index of the token class
347
408
*/
348
409
function sumVotingWeightForTokenClass ( bool bIsSandbox , uint256 tokenClassIndex ) internal view returns (uint256 ) {
349
410
if (bIsSandbox) {
350
411
uint256 votingWeightUnit = sandboxMachineState.tokenList[tokenClassIndex].votingWeight;
351
412
bool bIsValid = true ;
352
- uint256 votingWeight = 0 ;
413
+ uint256 votingWeight;
353
414
(bIsValid, votingWeight) = SafeMathUpgradeable.tryMul (votingWeightUnit, totalTokensPerTokenClass (bIsSandbox, tokenClassIndex));
354
415
require (bIsValid, "sumVotingWeightForTokenClass: votingWeight overflow " );
355
416
return votingWeight;
356
417
} else {
357
418
uint256 votingWeightUnit = currentMachineState.tokenList[tokenClassIndex].votingWeight;
358
419
bool bIsValid = true ;
359
- uint256 votingWeight = 0 ;
420
+ uint256 votingWeight;
360
421
(bIsValid, votingWeight) = SafeMathUpgradeable.tryMul (votingWeightUnit, totalTokensPerTokenClass (bIsSandbox, tokenClassIndex));
361
422
require (bIsValid, "sumVotingWeightForTokenClass: votingWeight overflow " );
362
423
return votingWeight;
@@ -374,13 +435,13 @@ contract MachineStateManager {
374
435
require (sandboxMachineState.machineStateParameters.dividendPermyriadPerTransaction < 10000 ,
375
436
ErrorMsg.By (15 ));
376
437
377
- // make sure that cycle counter is less than the threashold
438
+ // make sure that cycle counter is less than the threshold
378
439
require (sandboxMachineState.machineStateParameters.dividendCycleCounter >=
379
440
sandboxMachineState.machineStateParameters.dividendCycleOfTransactions, ErrorMsg.By (16 ));
380
441
381
442
// 1. calculate the total amount of dividends to be offered
382
443
bool bIsValid = true ;
383
- uint256 totalDividends = 0 ;
444
+ uint256 totalDividends;
384
445
385
446
(bIsValid, totalDividends) = SafeMathUpgradeable.tryMul (
386
447
sandboxMachineState.machineStateParameters.currentCashBalanceForDividends,
@@ -391,10 +452,10 @@ contract MachineStateManager {
391
452
10000 );
392
453
require (bIsValid, ErrorMsg.By (12 ));
393
454
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;
396
457
397
- for (uint256 index= 0 ; index < sandboxMachineState.tokenList.length ; index ++ ) {
458
+ for (uint256 index; index < sandboxMachineState.tokenList.length ;) {
398
459
399
460
if (sandboxMachineState.tokenList[index].bIsInitialized == false ) {
400
461
break ;
@@ -404,10 +465,14 @@ contract MachineStateManager {
404
465
totalDividendsWeight,
405
466
sumDividendWeightForTokenClass (bIsSandbox, index));
406
467
require (bIsValid, ErrorMsg.By (12 ));
468
+
469
+ unchecked {
470
+ ++ index;
471
+ }
407
472
}
408
473
409
474
// 3. calculate the cash dividend per unit
410
- uint256 cashPerUnit = 0 ;
475
+ uint256 cashPerUnit;
411
476
(bIsValid, cashPerUnit) = SafeMathUpgradeable.tryDiv (
412
477
totalDividends,
413
478
totalDividendsWeight);
@@ -418,7 +483,7 @@ contract MachineStateManager {
418
483
require (currentMachineState.machineStateParameters.dividendPermyriadPerTransaction < 10000 ,
419
484
ErrorMsg.By (15 ));
420
485
421
- // make sure that cycle counter is less than the threashold
486
+ // make sure that cycle counter is less than the threshold
422
487
require (currentMachineState.machineStateParameters.dividendCycleCounter >=
423
488
currentMachineState.machineStateParameters.dividendCycleOfTransactions, ErrorMsg.By (16 ));
424
489
@@ -435,8 +500,8 @@ contract MachineStateManager {
435
500
10000 );
436
501
require (bIsValid, ErrorMsg.By (12 ));
437
502
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;
440
505
441
506
for (uint256 index= 0 ; index < currentMachineState.tokenList.length ; index++ ) {
442
507
@@ -451,12 +516,12 @@ contract MachineStateManager {
451
516
}
452
517
453
518
// 3. calculate the cash dividend per unit
454
- uint256 cashPerUnit = 0 ;
519
+ uint256 cashPerUnit;
455
520
(bIsValid, cashPerUnit) = SafeMathUpgradeable.tryDiv (
456
521
totalDividends,
457
522
totalDividendsWeight);
458
523
459
524
return (cashPerUnit);
460
525
}
461
526
}
462
- }
527
+ }
0 commit comments