Rigor Protocol contest - ret2basic's results

Community lending and instant payments for new home construction.

General Information

Platform: Code4rena

Start Date: 01/08/2022

Pot Size: $50,000 USDC

Total HM: 26

Participants: 133

Period: 5 days

Judge: Jack the Pug

Total Solo HM: 6

Id: 151

League: ETH

Rigor Protocol

Findings Distribution

Researcher Performance

Rank: 121/133

Findings: 1

Award: $21.73

🌟 Selected for report: 0

🚀 Solo Findings: 0

Rigor Protocol Gas Optimization Report

Issue

  1. Use latest version of Solidity (1 instance)
  2. Cache array length (3 instances)
  3. Use != 0 instead of > 0 for uint comparison (10 instances)
  4. x += y costs more gas than x = x + y for state variables (7 instances)
  5. Use ++i/--i instead of i++/i-- in for loop (7 instances)
  6. Use unchecked {++i} in for loop (7 instance)

Total 35 of 6 issues.

1. Use latest version of Solidity

New version of Solidity provides bug fix / gas optimization. The latest stable version is 0.8.15. Also, use the ^ sign before Solidity version to indicate "use at least this version".

File: 2022-08-rigor/contracts/Community.sol

3:	pragma solidity 0.8.6;

2. Cache array length

Caching the array length outside a loop saves reading it on each iteration, as long as the array's length is not changed during the loop.

File: 2022-08-rigor/contracts/Project.sol

592:    taskCount - j + _changeOrderedTask.length - i

601: 	if (_changeOrderedTask.length > 0) {

603: 	for (; i < _changeOrderedTask.length; i++) {

3. Use != 0 instead of > 0 for uint comparison

When dealing with unsigned integer types, comparisons with != 0 are cheaper then with > 0.

File: 2022-08-rigor/contracts/Community.sol

261:	if (projectPublished[_project] > 0) {

427: 	_communities[_communityID].projectDetails[_project].lentAmount > 0

764:	require(_repayAmount > 0, "Community::!repay");

840:    if (_interestEarned > 0) {
File: 2022-08-rigor/contracts/Disputes.sol

107:	_actionType > 0 && _actionType <= uint8(ActionType.TaskPay),
File: 2022-08-rigor/contracts/HomeFi.sol

245:	return projectTokenId[_project] > 0;
File: 2022-08-rigor/contracts/Project.sol

195:	equire(_cost > 0, "Project::!value>0");

380:	if (_leftOutTokens > 0) {

601:	if (_changeOrderedTask.length > 0) {

691:	if (_loopCount > 0) emit TaskAllocated(_tasksAllocated);

4. x += y costs more gas than x = x + y for state variables

Replace x += y with x = x + y to save gas.

File: 2022-08-rigor/contracts/HomeFi.sol

289:	projectCount += 1;
File: 2022-08-rigor/contracts/Project.sol

179:	hashChangeNonce += 1;

290:	hashChangeNonce += 1;

431:	totalAllocated -= _withdrawDifference;

440:	totalAllocated += _newCost - _taskCost;

456:	totalAllocated -= _taskCost;

772:	totalLent -= _amount;

5. Use ++i/--i instead of i++/i-- in for loop

In for loops, ++i costs less gas than i++.

File: 2022-08-rigor/contracts/Community.sol

624:	for (uint256 i = 0; i < _communities[_communityID].memberCount; i++) {
File: 2022-08-rigor/contracts/HomeFiProxy.sol

87:     for (uint256 i = 0; i < _length; i++) {

136:	for (uint256 i = 0; i < _length; i++) {
File: 2022-08-rigor/contracts/Project.sol

248:	for (uint256 i = 0; i < _length; i++) {

311:	for (uint256 i = 0; i < _length; i++) {

322:	for (uint256 i = 0; i < _length; i++) {

603:	for (; i < _changeOrderedTask.length; i++) {

6. Use unchecked {++i} in for loop

In for loops, unchecked {++i} skips overflow/underflow check. Since this saves gas for each iteration, the total amount of gas saved is huge.

File: 2022-08-rigor/contracts/Community.sol

624:	for (uint256 i = 0; i < _communities[_communityID].memberCount; i++) {
File: 2022-08-rigor/contracts/HomeFiProxy.sol

87:     for (uint256 i = 0; i < _length; i++) {

136:	for (uint256 i = 0; i < _length; i++) {
File: 2022-08-rigor/contracts/Project.sol

248:	for (uint256 i = 0; i < _length; i++) {

311:	for (uint256 i = 0; i < _length; i++) {

322:	for (uint256 i = 0; i < _length; i++) {

603:	for (; i < _changeOrderedTask.length; i++) {
AuditHub

A portfolio for auditors, a security profile for protocols, a hub for web3 security.

Built bymalatrax © 2024

Auditors

Browse

Contests

Browse

Get in touch

ContactTwitter