Elastic-Finance-DAO / eefi_contracts

0 stars 0 forks source link

[DLL-02C] Potential Optimization of Search #68

Closed stalker474 closed 4 months ago

stalker474 commented 5 months ago

DLL-02C: Potential Optimization of Search

Type Severity Location
Gas Optimization DepositsLinkedList.sol:L62-L67

Description:

The DepositsLinkedList::sumExpiredDeposits function will iterate through all locks inefficiently as the locks are introduced in an ascending manner based on the DepositsLinkedList::insertEnd integration by the ElasticVault::makeDeposit function.

Example:

function sumExpiredDeposits(List storage list, uint256 lock_duration) internal view returns (uint256 sum) {
    uint current = list.head;
    sum = 0;

    while (current != NULL) {
        if (lock_duration == 0 || ((block.timestamp - list.nodes[current].deposit.timestamp) > lock_duration)) {
            sum = sum.add(list.nodes[current].deposit.amount);
        }
        current = list.nodes[current].next;
    }

    return sum;
}

Recommendation:

We advise the loop to break if an unexpired deposit is identified, significantly optimizing the gas cost of the summation function.