Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NestedFactory#removeOperator() Avoid empty items can save gas #170

Open
code423n4 opened this issue Nov 17, 2021 · 3 comments
Open

NestedFactory#removeOperator() Avoid empty items can save gas #170

code423n4 opened this issue Nov 17, 2021 · 3 comments
Assignees
Labels
bug Something isn't working G (Gas Optimization)

Comments

@code423n4
Copy link
Contributor

Handle

WatchPug

Vulnerability details

The for loop in rebuildCache() will cost more gas if removeOperator() wont decrease operators.length.

https://github.com/code-423n4/2021-11-nested/blob/f646002b692ca5fa3631acfff87dda897541cf41/contracts/MixinOperatorResolver.sol#L29-L43

function rebuildCache() public {
    bytes32[] memory requiredAddresses = resolverAddressesRequired();
    // The resolver must call this function whenever it updates its state
    for (uint256 i = 0; i < requiredAddresses.length; i++) {
        bytes32 name = requiredAddresses[i];
        // Note: can only be invoked once the resolver has all the targets needed added
        address destination = resolver.getAddress(name);
        if (destination != address(0)) {
            addressCache[name] = destination;
        } else {
            delete addressCache[name];
        }
        emit CacheUpdated(name, destination);
    }
}

https://github.com/code-423n4/2021-11-nested/blob/f646002b692ca5fa3631acfff87dda897541cf41/contracts/NestedFactory.sol#L78-L86

/// @inheritdoc INestedFactory
function removeOperator(bytes32 operator) external override onlyOwner {
    uint256 i = 0;
    while (operators[i] != operator) {
        i++;
    }
    require(i > 0, "NestedFactory::removeOperator: Cant remove non-existent operator");
    delete operators[i];
}

Recommendation

Change to:

/// @inheritdoc INestedFactory
function removeOperator(bytes32 operator) external override onlyOwner {
    uint256 length = operators.length;
    for (uint256 i = 0; i < length; i++) {
        if (operators[i] == operator) {
            operators[i] = operators[length - 1];
            operators.pop();
            return;
        }
    }
    revert("NestedFactory::removeOperator: Cant remove non-existent operator");
}
@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Nov 17, 2021
code423n4 added a commit that referenced this issue Nov 17, 2021
@maximebrugel maximebrugel added the duplicate This issue or pull request already exists label Nov 17, 2021
@maximebrugel
Copy link
Collaborator

Duplicated : #58

@alcueca
Copy link
Collaborator

alcueca commented Dec 3, 2021

Part of #220

@alcueca
Copy link
Collaborator

alcueca commented Dec 3, 2021

Actually, even if caused by the same code, this is a different issue.

@alcueca alcueca reopened this Dec 3, 2021
@alcueca alcueca removed the duplicate This issue or pull request already exists label Dec 3, 2021
@maximebrugel maximebrugel self-assigned this Dec 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working G (Gas Optimization)
Projects
None yet
Development

No branches or pull requests

3 participants