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

Gas Optimization: Cache result of BlockNumber.get() #142

Open
code423n4 opened this issue Jan 10, 2022 · 2 comments
Open

Gas Optimization: Cache result of BlockNumber.get() #142

code423n4 opened this issue Jan 10, 2022 · 2 comments
Labels
bug Something isn't working G (Gas Optimization) sponsor disputed Sponsor cannot duplicate the issue, or otherwise disagrees this is an issue

Comments

@code423n4
Copy link
Contributor

Handle

gzeon

Vulnerability details

Impact

Result of BlockNumber.get() can be cached before the for loop.

        for (uint256 i; i < ids.length; i++) {
            Due storage due = dues[ids[i]];
            require(due.startBlock != BlockNumber.get(), 'E207');

to

        uint32 bn32 = BlockNumber.get();
        for (uint256 i; i < ids.length; i++) {
            Due storage due = dues[ids[i]];
            require(due.startBlock != bn32, 'E207');

Proof of Concept

https://github.com/code-423n4/2022-01-timeswap/blob/bf50d2a8bb93a5571f35f96bd74af54d9c92a210/Timeswap/Timeswap-V1-Core/contracts/TimeswapPair.sol#L361

@Mathepreneur
Copy link
Collaborator

We get stack too deep error.

@0xean
Copy link
Collaborator

0xean commented Jan 26, 2022

ditto previous comments....

To prove the point to the sponsor this compiles currently by scoping in brackets.

      
        Due[] storage dues = pool.dues[owner];
        {
          uint32 bn32 = BlockNumber.get();
          for (uint256 i; i < ids.length; i++) {
              Due storage due = dues[ids[i]];
              require(due.startBlock != bn32, 'E207');
              if (owner != msg.sender) require(collateralsOut[i] == 0, 'E213');
              PayMath.checkProportional(assetsIn[i], collateralsOut[i], due);
              due.debt -= assetsIn[i];
              due.collateral -= collateralsOut[i];
              assetIn += assetsIn[i];
              collateralOut += collateralsOut[i];
          }
        }
        if (assetIn > 0) Callback.pay(asset, assetIn, data);

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) sponsor disputed Sponsor cannot duplicate the issue, or otherwise disagrees this is an issue
Projects
None yet
Development

No branches or pull requests

3 participants