code-423n4 / 2022-02-pooltogether-findings

0 stars 0 forks source link

Gas Optimizations #39

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

PoolTogether TWAB Delegator Gas Optimization Report

Unless otherwise noted, manual auditing and testing were done using Visual Studio Code and Remix. The sponsor-provided test suite was used to verify the findings.

The audit was done from February 22-24, 2022 by ye0lde through code4rena.

Findings

G-1 - Comparison with literal boolean value (TWABDelegator.sol)

Impact

While doing comparisons with literal boolean values is considered a "best practice" it also does save a small amount of gas during deployment and execution.

Below are some examples from the sponsor's test suite before and after the change:

Function Before (AVG) After (AVG)
updateDelegatee 141871 141868
withdrawDelegationToStake 189235 189231

Proof of Concept

https://github.com/pooltogether/v4-twab-delegator/blob/21bb53b2ea54a248bbd1d3170dbadd3a0c83d874/contracts/TWABDelegator.sol#L583

  function _requireDelegatorOrRepresentative(address _delegator) internal view {
    require(
      _delegator == msg.sender || representatives[_delegator][msg.sender] == true,
      "TWABDelegator/not-delegator-or-rep"
    );
  }

Recommended Mitigation Steps

I suggest the following change:

  function _requireDelegatorOrRepresentative(address _delegator) internal view {
    require(
      _delegator == msg.sender || representatives[_delegator][msg.sender],
      "TWABDelegator/not-delegator-or-rep"
    );
  }

PierrickGT commented 2 years ago

Duplicate of https://github.com/code-423n4/2022-02-pooltogether-findings/issues/15