code-423n4 / 2022-01-xdefi-findings

0 stars 0 forks source link

`noReenter` gas optimization #200

Closed code423n4 closed 2 years ago

code423n4 commented 2 years ago

Handle

0xsanson

Vulnerability details

The current implementation uses _locked = 0 for the unlocked state and _locked = 1 for the locked one. It's better to have two non-zero values from an optimization point of view; it can surprisingly save thousands of gas in some test cases.

Consider rewriting the modifier to:

modifier noReenter() {
    require(_locked == 1, "LOCKED");
    _locked = uint256(2);
    _;
    _locked = uint256(1);
}

(also add a _locked = 1 in the constructor).

Tools Used

hardhat gas reporter

deluca-mike commented 2 years ago

Agreed. While not always cheaper, it is cheaper in this case for batch unlocks or relocks. Will implement this.

deluca-mike commented 2 years ago

Duplicate #1