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

TimeswapPair.sol modifier lock: Switching between 1, 2 instead of 0, 1 is more gas efficient #87

Open
code423n4 opened this issue Jan 6, 2022 · 1 comment
Labels
bug Something isn't working G (Gas Optimization) resolved Finding has been patched by sponsor (sponsor pls link to PR containing fix) sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")

Comments

@code423n4
Copy link
Contributor

Handle

bitbopper

Vulnerability details

Impact

https://github.com/code-423n4/2022-01-timeswap/blob/5960e07d39f2b4a60cfabde1bd51f4b1e62e7e85/Timeswap/Timeswap-V1-Core/contracts/TimeswapPair.sol#L121:L126 could be more gas efficient

Proof of Concept

Version as in Repo

// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity =0.8.4;

contract LockProofOld {
	uint256 locked = 0;

	modifier lock() {
	    require(locked == 0, 'E211');
	    locked = 1;
	    _;
	    locked = 0;
	}

	function test() public lock {
	}

}

Proposed Version

// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity =0.8.4;

contract LockProofNew {
	uint256 locked = 1;

	modifier lock() {
	    require(locked == 1, 'E211');
	    locked = 2;
	    _;
	    locked = 1;
	}

	function test() public lock {
	}

}

Comparison

Test harness

// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity =0.8.4;

import "ds-test/test.sol";

import "./LockProofOld.sol";
import "./LockProofNew.sol";

contract LockProofTest is DSTest {
    LockProofOld lockproofold;
    LockProofNew lockproofnew;

    function setUp() public {
        lockproofold = new LockProofOld();
        lockproofnew = new LockProofNew();
    }

    function test_old() public {
		lockproofold.test();
    }

    function test_new() public {
		lockproofnew.test();
    }
}

Output

dapp test
Running 2 tests for src/LockProof.t.sol:LockProofTest
[PASS] test_old() (gas: 21042)
[PASS] test_new() (gas: 1136)
@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Jan 6, 2022
code423n4 added a commit that referenced this issue Jan 6, 2022
@Mathepreneur Mathepreneur added the sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity") label Jan 15, 2022
@Mathepreneur
Copy link
Collaborator

@Mathepreneur Mathepreneur added the resolved Finding has been patched by sponsor (sponsor pls link to PR containing fix) label Jan 17, 2022
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) resolved Finding has been patched by sponsor (sponsor pls link to PR containing fix) sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")
Projects
None yet
Development

No branches or pull requests

2 participants