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 savings when using ++var instead of var + 1 #14

Closed
code423n4 opened this issue Oct 12, 2021 · 2 comments
Closed

Gas savings when using ++var instead of var + 1 #14

code423n4 opened this issue Oct 12, 2021 · 2 comments
Labels
bug Warden finding duplicate Another warden found this issue G (Gas Optimization)

Comments

@code423n4
Copy link
Contributor

Handle

ye0lde

Vulnerability details

Impact

Gas savings when using ++var instead of var + 1

Proof of Concept

I noticed a typo here (rngReqeust):
https://github.com/pooltogether/v4-core/blob/35b00f710db422a6193131b7dc2de5202dc4677c/contracts/DrawBeacon.sol#L245

The line above that caught my eye and I looked into how "nextDrawId" and "_nextDrawId" were being used.
I speculated that it would be more efficient if "_nextDrawId" was eliminated in favor of using the state variable directly. Testing proved that not to be true.

But what I did notice is that the following

nextDrawId = ++_nextDrawId;

is more efficient (less gas) than the current code

nextDrawId = _nextDrawId + 1;
</code.

While there is no need to increment "_nextDrawId" it isn't used again.
And even with future changes if it is used again, if it is supposed to mirror "nextDrawId", then it would probably need to be incremented anyway.

Tools Used

Visual Studio Code, Remix

Recommended Mitigation Steps

Change this line
https://github.com/pooltogether/v4-core/blob/35b00f710db422a6193131b7dc2de5202dc4677c/contracts/DrawBeacon.sol#L243
to

nextDrawId = ++_nextDrawId;

code423n4 added a commit that referenced this issue Oct 12, 2021
@PierrickGT
Copy link
Member

PierrickGT commented Oct 12, 2021

Duplicate of #6

@PierrickGT PierrickGT added the duplicate Another warden found this issue label Oct 12, 2021
@PierrickGT PierrickGT marked this as a duplicate of #6 Oct 12, 2021
@GalloDaSballo
Copy link
Collaborator

Duplicate of #6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Warden finding duplicate Another warden found this issue G (Gas Optimization)
Projects
None yet
Development

No branches or pull requests

3 participants