code-423n4 / 2021-09-defiprotocol-findings

1 stars 0 forks source link

Avoiding state variables in emits saves gas
 #143

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

Handle

0xRajeev

Vulnerability details

Impact

Public/external setter functions or any others that emit events to indicate the newly set values can use the function parameters (from memory/calldata) or their cached local versions as emit arguments instead of the state variables that were just written to because SLOADs cost 2100/100 gas depending on cold/warm reads from Berlin upgrade onwards.

Proof of Concept

https://github.com/code-423n4/2021-09-defiProtocol/blob/52b74824c42acbcd64248f68c40128fe3a82caf6/contracts/contracts/Auction.sol#L66

https://github.com/code-423n4/2021-09-defiProtocol/blob/52b74824c42acbcd64248f68c40128fe3a82caf6/contracts/contracts/Auction.sol#L121

https://github.com/code-423n4/2021-09-defiProtocol/blob/52b74824c42acbcd64248f68c40128fe3a82caf6/contracts/contracts/Basket.sol#L127

https://github.com/code-423n4/2021-09-defiProtocol/blob/52b74824c42acbcd64248f68c40128fe3a82caf6/contracts/contracts/Basket.sol#L143

https://github.com/code-423n4/2021-09-defiProtocol/blob/52b74824c42acbcd64248f68c40128fe3a82caf6/contracts/contracts/Basket.sol#L161

https://github.com/code-423n4/2021-09-defiProtocol/blob/52b74824c42acbcd64248f68c40128fe3a82caf6/contracts/contracts/Basket.sol#L178

https://github.com/code-423n4/2021-09-defiProtocol/blob/52b74824c42acbcd64248f68c40128fe3a82caf6/contracts/contracts/Basket.sol#L213

https://github.com/code-423n4/2021-09-defiProtocol/blob/52b74824c42acbcd64248f68c40128fe3a82caf6/contracts/contracts/Basket.sol#L219

Tools Used

Manual Analysis

Recommended Mitigation Steps

Use function parameters or cached local variables in emits to save gas.

GalloDaSballo commented 2 years ago

Absolutely agree with the warden, I appreciate the list of links as well as the note on the 2.1k (cold SLOAD) and (100 hot SLOAD)